xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 3a7fca6b988f1faea172e46abdf950477dbfaf76)
1*3a7fca6bSBarry Smith 
2*3a7fca6bSBarry Smith /*$Id: aij.c,v 1.373 2001/05/31 21:33:53 balay Exp bsmith $*/
3d5d45c9bSBarry Smith /*
43369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
5d5d45c9bSBarry Smith   matrix storage format.
6d5d45c9bSBarry Smith */
73369ce9aSBarry Smith 
8a375949dSSatish Balay #include "petscsys.h"                           /*I "petscmat.h" I*/
970f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h"
10f5eb4b81SSatish Balay #include "src/vec/vecimpl.h"
11f5eb4b81SSatish Balay #include "src/inline/spops.h"
128d195f9aSBarry Smith #include "src/inline/dot.h"
130a835dfdSSatish Balay #include "petscbt.h"
1417ab2063SBarry Smith 
15a2ce50c7SBarry Smith 
16ca44d042SBarry Smith EXTERN int MatToSymmetricIJ_SeqAIJ(int,int*,int*,int,int,int**,int**);
1717ab2063SBarry Smith 
184a2ae208SSatish Balay #undef __FUNCT__
194a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
2036db0b34SBarry Smith int MatGetRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *m,int **ia,int **ja,PetscTruth *done)
2117ab2063SBarry Smith {
22416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
236945ee14SBarry Smith   int        ierr,i,ishift;
2417ab2063SBarry Smith 
253a40ed3dSBarry Smith   PetscFunctionBegin;
2631625ec5SSatish Balay   *m     = A->m;
273a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
286945ee14SBarry Smith   ishift = a->indexshift;
2953e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
30273d9f13SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
316945ee14SBarry Smith   } else if (oshift == 0 && ishift == -1) {
32435da068SBarry Smith     int nz = a->i[A->m] - 1;
333b2fbd54SBarry Smith     /* malloc space and  subtract 1 from i and j indices */
34b0a32e0cSBarry Smith     ierr = PetscMalloc((A->m+1)*sizeof(int),ia);CHKERRQ(ierr);
35b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),ja);CHKERRQ(ierr);
363b2fbd54SBarry Smith     for (i=0; i<nz; i++) (*ja)[i] = a->j[i] - 1;
37273d9f13SBarry Smith     for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] - 1;
386945ee14SBarry Smith   } else if (oshift == 1 && ishift == 0) {
39435da068SBarry Smith     int nz = a->i[A->m];
403b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
411bc30dd5SBarry Smith     ierr = PetscMalloc((A->m+1)*sizeof(int),ia);CHKERRQ(ierr);
42b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),ja);CHKERRQ(ierr);
433b2fbd54SBarry Smith     for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
44273d9f13SBarry Smith     for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] + 1;
456945ee14SBarry Smith   } else {
466945ee14SBarry Smith     *ia = a->i; *ja = a->j;
47a2ce50c7SBarry Smith   }
483a40ed3dSBarry Smith   PetscFunctionReturn(0);
49a2744918SBarry Smith }
50a2744918SBarry Smith 
514a2ae208SSatish Balay #undef __FUNCT__
524a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
5336db0b34SBarry Smith int MatRestoreRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja,PetscTruth *done)
546945ee14SBarry Smith {
556945ee14SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
56606d414cSSatish Balay   int        ishift = a->indexshift,ierr;
576945ee14SBarry Smith 
583a40ed3dSBarry Smith   PetscFunctionBegin;
593a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
6053e63a63SBarry Smith   if ((symmetric && !A->structurally_symmetric) || (oshift == 0 && ishift == -1) || (oshift == 1 && ishift == 0)) {
61606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
62606d414cSSatish Balay     ierr = PetscFree(*ja);CHKERRQ(ierr);
63bcd2baecSBarry Smith   }
643a40ed3dSBarry Smith   PetscFunctionReturn(0);
6517ab2063SBarry Smith }
6617ab2063SBarry Smith 
674a2ae208SSatish Balay #undef __FUNCT__
684a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
6936db0b34SBarry Smith int MatGetColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *nn,int **ia,int **ja,PetscTruth *done)
703b2fbd54SBarry Smith {
713b2fbd54SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
72a93ec695SBarry Smith   int        ierr,i,ishift = a->indexshift,*collengths,*cia,*cja,n = A->n,m = A->m;
73a93ec695SBarry Smith   int        nz = a->i[m]+ishift,row,*jj,mr,col;
743b2fbd54SBarry Smith 
753a40ed3dSBarry Smith   PetscFunctionBegin;
763b2fbd54SBarry Smith   *nn     = A->n;
773a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
783b2fbd54SBarry Smith   if (symmetric) {
79273d9f13SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
803b2fbd54SBarry Smith   } else {
81b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(int),&collengths);CHKERRQ(ierr);
82549d3d68SSatish Balay     ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr);
83b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(int),&cia);CHKERRQ(ierr);
84b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),&cja);CHKERRQ(ierr);
853b2fbd54SBarry Smith     jj = a->j;
863b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
873b2fbd54SBarry Smith       collengths[jj[i] + ishift]++;
883b2fbd54SBarry Smith     }
893b2fbd54SBarry Smith     cia[0] = oshift;
903b2fbd54SBarry Smith     for (i=0; i<n; i++) {
913b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
923b2fbd54SBarry Smith     }
93549d3d68SSatish Balay     ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr);
943b2fbd54SBarry Smith     jj   = a->j;
95a93ec695SBarry Smith     for (row=0; row<m; row++) {
96a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
97a93ec695SBarry Smith       for (i=0; i<mr; i++) {
983b2fbd54SBarry Smith         col = *jj++ + ishift;
993b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1003b2fbd54SBarry Smith       }
1013b2fbd54SBarry Smith     }
102606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
1033b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1043b2fbd54SBarry Smith   }
1053a40ed3dSBarry Smith   PetscFunctionReturn(0);
1063b2fbd54SBarry Smith }
1073b2fbd54SBarry Smith 
1084a2ae208SSatish Balay #undef __FUNCT__
1094a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
11036db0b34SBarry Smith int MatRestoreColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja,PetscTruth *done)
1113b2fbd54SBarry Smith {
112606d414cSSatish Balay   int ierr;
113606d414cSSatish Balay 
1143a40ed3dSBarry Smith   PetscFunctionBegin;
1153a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1163b2fbd54SBarry Smith 
117606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
118606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1193b2fbd54SBarry Smith 
1203a40ed3dSBarry Smith   PetscFunctionReturn(0);
1213b2fbd54SBarry Smith }
1223b2fbd54SBarry Smith 
123227d817aSBarry Smith #define CHUNKSIZE   15
12417ab2063SBarry Smith 
1254a2ae208SSatish Balay #undef __FUNCT__
1264a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
12761d2ded1SBarry Smith int MatSetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *v,InsertMode is)
12817ab2063SBarry Smith {
129416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
130416022c9SBarry Smith   int        *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,sorted = a->sorted;
131273d9f13SBarry Smith   int        *imax = a->imax,*ai = a->i,*ailen = a->ilen;
132549d3d68SSatish Balay   int        *aj = a->j,nonew = a->nonew,shift = a->indexshift,ierr;
133416022c9SBarry Smith   Scalar     *ap,value,*aa = a->a;
13436db0b34SBarry Smith   PetscTruth ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE);
135273d9f13SBarry Smith   PetscTruth roworiented = a->roworiented;
13617ab2063SBarry Smith 
1373a40ed3dSBarry Smith   PetscFunctionBegin;
13817ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
139416022c9SBarry Smith     row  = im[k];
1405ef9f2a5SBarry Smith     if (row < 0) continue;
141aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
142273d9f13SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",row,A->m);
1433b2fbd54SBarry Smith #endif
14417ab2063SBarry Smith     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
14517ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
146416022c9SBarry Smith     low = 0;
14717ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1485ef9f2a5SBarry Smith       if (in[l] < 0) continue;
149aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
150273d9f13SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[l],A->n);
1513b2fbd54SBarry Smith #endif
1524b0e389bSBarry Smith       col = in[l] - shift;
1534b0e389bSBarry Smith       if (roworiented) {
1545ef9f2a5SBarry Smith         value = v[l + k*n];
155bef8e0ddSBarry Smith       } else {
1564b0e389bSBarry Smith         value = v[k + l*m];
1574b0e389bSBarry Smith       }
15836db0b34SBarry Smith       if (value == 0.0 && ignorezeroentries) continue;
15936db0b34SBarry Smith 
160416022c9SBarry Smith       if (!sorted) low = 0; high = nrow;
161416022c9SBarry Smith       while (high-low > 5) {
162416022c9SBarry Smith         t = (low+high)/2;
163416022c9SBarry Smith         if (rp[t] > col) high = t;
164416022c9SBarry Smith         else             low  = t;
16517ab2063SBarry Smith       }
166416022c9SBarry Smith       for (i=low; i<high; i++) {
16717ab2063SBarry Smith         if (rp[i] > col) break;
16817ab2063SBarry Smith         if (rp[i] == col) {
169416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
17017ab2063SBarry Smith           else                  ap[i] = value;
17117ab2063SBarry Smith           goto noinsert;
17217ab2063SBarry Smith         }
17317ab2063SBarry Smith       }
174c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
17529bbc08cSBarry Smith       else if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix",row,col);
17617ab2063SBarry Smith       if (nrow >= rmax) {
17717ab2063SBarry Smith         /* there is no extra room in row, therefore enlarge */
178273d9f13SBarry Smith         int    new_nz = ai[A->m] + CHUNKSIZE,len,*new_i,*new_j;
17917ab2063SBarry Smith         Scalar *new_a;
18017ab2063SBarry Smith 
18129bbc08cSBarry Smith         if (nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix requiring new malloc()",row,col);
18296854ed6SLois Curfman McInnes 
18317ab2063SBarry Smith         /* malloc new storage space */
184273d9f13SBarry Smith         len     = new_nz*(sizeof(int)+sizeof(Scalar))+(A->m+1)*sizeof(int);
185b0a32e0cSBarry Smith 	ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr);
18617ab2063SBarry Smith         new_j   = (int*)(new_a + new_nz);
18717ab2063SBarry Smith         new_i   = new_j + new_nz;
18817ab2063SBarry Smith 
18917ab2063SBarry Smith         /* copy over old data into new slots */
19017ab2063SBarry Smith         for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];}
191273d9f13SBarry Smith         for (ii=row+1; ii<A->m+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;}
192549d3d68SSatish Balay         ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr);
193416022c9SBarry Smith         len  = (new_nz - CHUNKSIZE - ai[row] - nrow - shift);
194549d3d68SSatish Balay         ierr = PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow,len*sizeof(int));CHKERRQ(ierr);
195549d3d68SSatish Balay         ierr = PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar));CHKERRQ(ierr);
196549d3d68SSatish Balay         ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow,len*sizeof(Scalar));CHKERRQ(ierr);
19717ab2063SBarry Smith         /* free up old matrix storage */
198606d414cSSatish Balay         ierr = PetscFree(a->a);CHKERRQ(ierr);
199606d414cSSatish Balay         if (!a->singlemalloc) {
200606d414cSSatish Balay           ierr = PetscFree(a->i);CHKERRQ(ierr);
201606d414cSSatish Balay           ierr = PetscFree(a->j);CHKERRQ(ierr);
202606d414cSSatish Balay         }
203416022c9SBarry Smith         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;
204f1e2ffcdSBarry Smith         a->singlemalloc = PETSC_TRUE;
20517ab2063SBarry Smith 
20617ab2063SBarry Smith         rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
207416022c9SBarry Smith         rmax = imax[row] = imax[row] + CHUNKSIZE;
208b0a32e0cSBarry Smith         PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(Scalar)));
209416022c9SBarry Smith         a->maxnz += CHUNKSIZE;
210b810aeb4SBarry Smith         a->reallocs++;
21117ab2063SBarry Smith       }
212416022c9SBarry Smith       N = nrow++ - 1; a->nz++;
213416022c9SBarry Smith       /* shift up all the later entries in this row */
214416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
21517ab2063SBarry Smith         rp[ii+1] = rp[ii];
21617ab2063SBarry Smith         ap[ii+1] = ap[ii];
21717ab2063SBarry Smith       }
21817ab2063SBarry Smith       rp[i] = col;
21917ab2063SBarry Smith       ap[i] = value;
22017ab2063SBarry Smith       noinsert:;
221416022c9SBarry Smith       low = i + 1;
22217ab2063SBarry Smith     }
22317ab2063SBarry Smith     ailen[row] = nrow;
22417ab2063SBarry Smith   }
2253a40ed3dSBarry Smith   PetscFunctionReturn(0);
22617ab2063SBarry Smith }
22717ab2063SBarry Smith 
2284a2ae208SSatish Balay #undef __FUNCT__
2294a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
2308f6be9afSLois Curfman McInnes int MatGetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *v)
2317eb43aa7SLois Curfman McInnes {
2327eb43aa7SLois Curfman McInnes   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
233b49de8d1SLois Curfman McInnes   int        *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
2347eb43aa7SLois Curfman McInnes   int        *ai = a->i,*ailen = a->ilen,shift = a->indexshift;
2357eb43aa7SLois Curfman McInnes   Scalar     *ap,*aa = a->a,zero = 0.0;
2367eb43aa7SLois Curfman McInnes 
2373a40ed3dSBarry Smith   PetscFunctionBegin;
2387eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2397eb43aa7SLois Curfman McInnes     row  = im[k];
24029bbc08cSBarry Smith     if (row < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %d",row);
241273d9f13SBarry Smith     if (row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: %d",row);
2427eb43aa7SLois Curfman McInnes     rp   = aj + ai[row] + shift; ap = aa + ai[row] + shift;
2437eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2447eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
24529bbc08cSBarry Smith       if (in[l] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %d",in[l]);
246273d9f13SBarry Smith       if (in[l] >= A->n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: %d",in[l]);
2477eb43aa7SLois Curfman McInnes       col = in[l] - shift;
2487eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2497eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2507eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2517eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2527eb43aa7SLois Curfman McInnes         else             low  = t;
2537eb43aa7SLois Curfman McInnes       }
2547eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2557eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2567eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
257b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2587eb43aa7SLois Curfman McInnes           goto finished;
2597eb43aa7SLois Curfman McInnes         }
2607eb43aa7SLois Curfman McInnes       }
261b49de8d1SLois Curfman McInnes       *v++ = zero;
2627eb43aa7SLois Curfman McInnes       finished:;
2637eb43aa7SLois Curfman McInnes     }
2647eb43aa7SLois Curfman McInnes   }
2653a40ed3dSBarry Smith   PetscFunctionReturn(0);
2667eb43aa7SLois Curfman McInnes }
2677eb43aa7SLois Curfman McInnes 
26817ab2063SBarry Smith 
2694a2ae208SSatish Balay #undef __FUNCT__
2704a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
271b0a32e0cSBarry Smith int MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
27217ab2063SBarry Smith {
273416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
274416022c9SBarry Smith   int        i,fd,*col_lens,ierr;
27517ab2063SBarry Smith 
2763a40ed3dSBarry Smith   PetscFunctionBegin;
277b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
278b0a32e0cSBarry Smith   ierr = PetscMalloc((4+A->m)*sizeof(int),&col_lens);CHKERRQ(ierr);
279416022c9SBarry Smith   col_lens[0] = MAT_COOKIE;
280273d9f13SBarry Smith   col_lens[1] = A->m;
281273d9f13SBarry Smith   col_lens[2] = A->n;
282416022c9SBarry Smith   col_lens[3] = a->nz;
283416022c9SBarry Smith 
284416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
285273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
286416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
28717ab2063SBarry Smith   }
288273d9f13SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->m,PETSC_INT,1);CHKERRQ(ierr);
289606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
290416022c9SBarry Smith 
291416022c9SBarry Smith   /* store column indices (zero start index) */
292416022c9SBarry Smith   if (a->indexshift) {
293416022c9SBarry Smith     for (i=0; i<a->nz; i++) a->j[i]--;
29417ab2063SBarry Smith   }
2950752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,0);CHKERRQ(ierr);
296416022c9SBarry Smith   if (a->indexshift) {
297416022c9SBarry Smith     for (i=0; i<a->nz; i++) a->j[i]++;
29817ab2063SBarry Smith   }
299416022c9SBarry Smith 
300416022c9SBarry Smith   /* store nonzero values */
3010752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,0);CHKERRQ(ierr);
3023a40ed3dSBarry Smith   PetscFunctionReturn(0);
30317ab2063SBarry Smith }
304416022c9SBarry Smith 
3054a2ae208SSatish Balay #undef __FUNCT__
3064a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
307b0a32e0cSBarry Smith int MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
308416022c9SBarry Smith {
309416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
310fb9695e5SSatish Balay   int               ierr,i,j,m = A->m,shift = a->indexshift;
311fb9695e5SSatish Balay   char              *name;
312f3ef73ceSBarry Smith   PetscViewerFormat format;
31317ab2063SBarry Smith 
3143a40ed3dSBarry Smith   PetscFunctionBegin;
315435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
316b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
317fb9695e5SSatish Balay   if (format == PETSC_VIEWER_ASCII_INFO_LONG || format == PETSC_VIEWER_ASCII_INFO) {
3186831982aSBarry Smith     if (a->inode.size) {
319b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"using I-node routines: found %d nodes, limit used is %d\n",a->inode.node_count,a->inode.limit);CHKERRQ(ierr);
3206831982aSBarry Smith     } else {
321b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"not using I-node routines\n");CHKERRQ(ierr);
3226831982aSBarry Smith     }
323fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
324d00d2cf4SBarry Smith     int nofinalvalue = 0;
325273d9f13SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->n-!shift)) {
326d00d2cf4SBarry Smith       nofinalvalue = 1;
327d00d2cf4SBarry Smith     }
328b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
329b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %d %d \n",m,A->n);CHKERRQ(ierr);
330b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %d \n",a->nz);CHKERRQ(ierr);
331b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%d,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
332b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
33317ab2063SBarry Smith 
33417ab2063SBarry Smith     for (i=0; i<m; i++) {
335416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
336aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
337b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%d %d  %18.16e + %18.16ei \n",i+1,a->j[j]+!shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
33817ab2063SBarry Smith #else
339b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
34017ab2063SBarry Smith #endif
34117ab2063SBarry Smith       }
34217ab2063SBarry Smith     }
343d00d2cf4SBarry Smith     if (nofinalvalue) {
344b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",m,A->n,0.0);CHKERRQ(ierr);
345d00d2cf4SBarry Smith     }
346fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
347b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
348fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
349b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
35044cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
351b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr);
35244cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
353aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
35436db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
355b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %d %g + %g i",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
35636db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
357b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %d %g - %g i",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
35836db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
359b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3606831982aSBarry Smith         }
36144cd7ae7SLois Curfman McInnes #else
362b0a32e0cSBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
36344cd7ae7SLois Curfman McInnes #endif
36444cd7ae7SLois Curfman McInnes       }
365b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
36644cd7ae7SLois Curfman McInnes     }
367b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
368fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
369496be53dSLois Curfman McInnes     int nzd=0,fshift=1,*sptr;
370b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
371b0a32e0cSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(int),&sptr);CHKERRQ(ierr);
372496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
373496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
374496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
375496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
376aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
37736db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
378496be53dSLois Curfman McInnes #else
379496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
380496be53dSLois Curfman McInnes #endif
381496be53dSLois Curfman McInnes         }
382496be53dSLois Curfman McInnes       }
383496be53dSLois Curfman McInnes     }
3842e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
385b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %d %d\n\n",m,nzd);CHKERRQ(ierr);
3862e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
387b0a32e0cSBarry Smith       if (i+4<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4],sptr[i+5]);CHKERRQ(ierr);}
388b0a32e0cSBarry Smith       else if (i+3<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);CHKERRQ(ierr);}
389b0a32e0cSBarry Smith       else if (i+2<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);}
390b0a32e0cSBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
391b0a32e0cSBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %d %d\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
392b0a32e0cSBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %d\n",sptr[i]);CHKERRQ(ierr);}
393496be53dSLois Curfman McInnes     }
394b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
395606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
396496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
397496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
398b0a32e0cSBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %d ",a->j[j]+fshift);CHKERRQ(ierr);}
399496be53dSLois Curfman McInnes       }
400b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
401496be53dSLois Curfman McInnes     }
402b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
403496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
404496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
405496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
406aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
40736db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
408b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4096831982aSBarry Smith           }
410496be53dSLois Curfman McInnes #else
411b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
412496be53dSLois Curfman McInnes #endif
413496be53dSLois Curfman McInnes         }
414496be53dSLois Curfman McInnes       }
415b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
416496be53dSLois Curfman McInnes     }
417b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
418fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
41902594712SBarry Smith     int    cnt = 0,jcnt;
42002594712SBarry Smith     Scalar value;
42102594712SBarry Smith 
422b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
42302594712SBarry Smith     for (i=0; i<m; i++) {
42402594712SBarry Smith       jcnt = 0;
425273d9f13SBarry Smith       for (j=0; j<A->n; j++) {
426e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
42702594712SBarry Smith           value = a->a[cnt++];
428e24b481bSBarry Smith           jcnt++;
42902594712SBarry Smith         } else {
43002594712SBarry Smith           value = 0.0;
43102594712SBarry Smith         }
432aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
433b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
43402594712SBarry Smith #else
435b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
43602594712SBarry Smith #endif
43702594712SBarry Smith       }
438b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
43902594712SBarry Smith     }
440b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4413a40ed3dSBarry Smith   } else {
442b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
44317ab2063SBarry Smith     for (i=0; i<m; i++) {
444b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr);
445416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
446aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
44736db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
448b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %d %g + %g i",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
44936db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
450b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %d %g - %g i",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4513a40ed3dSBarry Smith         } else {
452b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
45317ab2063SBarry Smith         }
45417ab2063SBarry Smith #else
455b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
45617ab2063SBarry Smith #endif
45717ab2063SBarry Smith       }
458b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
45917ab2063SBarry Smith     }
460b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
46117ab2063SBarry Smith   }
462b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4633a40ed3dSBarry Smith   PetscFunctionReturn(0);
464416022c9SBarry Smith }
465416022c9SBarry Smith 
4664a2ae208SSatish Balay #undef __FUNCT__
4674a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
468b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
469416022c9SBarry Smith {
470480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
471416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
472273d9f13SBarry Smith   int               ierr,i,j,m = A->m,shift = a->indexshift,color;
47336db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
474b0a32e0cSBarry Smith   PetscViewer       viewer;
475f3ef73ceSBarry Smith   PetscViewerFormat format;
476cddf8d76SBarry Smith 
4773a40ed3dSBarry Smith   PetscFunctionBegin;
478480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
479b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
48019bcc07fSBarry Smith 
481b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
482416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4830513a670SBarry Smith 
484fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
4850513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
486b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
487416022c9SBarry Smith     for (i=0; i<m; i++) {
488cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
489416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
490cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
491aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
49236db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
493cddf8d76SBarry Smith #else
494cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
495cddf8d76SBarry Smith #endif
496b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
497cddf8d76SBarry Smith       }
498cddf8d76SBarry Smith     }
499b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
500cddf8d76SBarry Smith     for (i=0; i<m; i++) {
501cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
502cddf8d76SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
503cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
504cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
505b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
506cddf8d76SBarry Smith       }
507cddf8d76SBarry Smith     }
508b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
509cddf8d76SBarry Smith     for (i=0; i<m; i++) {
510cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
511cddf8d76SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
512cddf8d76SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
513aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
51436db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
515cddf8d76SBarry Smith #else
516cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
517cddf8d76SBarry Smith #endif
518b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
519416022c9SBarry Smith       }
520416022c9SBarry Smith     }
5210513a670SBarry Smith   } else {
5220513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5230513a670SBarry Smith     /* first determine max of all nonzero values */
5240513a670SBarry Smith     int    nz = a->nz,count;
525b0a32e0cSBarry Smith     PetscDraw   popup;
52636db0b34SBarry Smith     PetscReal scale;
5270513a670SBarry Smith 
5280513a670SBarry Smith     for (i=0; i<nz; i++) {
5290513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5300513a670SBarry Smith     }
531b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
532b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
533b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5340513a670SBarry Smith     count = 0;
5350513a670SBarry Smith     for (i=0; i<m; i++) {
5360513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
5370513a670SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
5380513a670SBarry Smith         x_l = a->j[j] + shift; x_r = x_l + 1.0;
539b0a32e0cSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(a->a[count]));
540b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5410513a670SBarry Smith         count++;
5420513a670SBarry Smith       }
5430513a670SBarry Smith     }
5440513a670SBarry Smith   }
545480ef9eaSBarry Smith   PetscFunctionReturn(0);
546480ef9eaSBarry Smith }
547cddf8d76SBarry Smith 
5484a2ae208SSatish Balay #undef __FUNCT__
5494a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
550b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
551480ef9eaSBarry Smith {
552480ef9eaSBarry Smith   int        ierr;
553b0a32e0cSBarry Smith   PetscDraw  draw;
55436db0b34SBarry Smith   PetscReal  xr,yr,xl,yl,h,w;
555480ef9eaSBarry Smith   PetscTruth isnull;
556480ef9eaSBarry Smith 
557480ef9eaSBarry Smith   PetscFunctionBegin;
558b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
559b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
560480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
561480ef9eaSBarry Smith 
562480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
563273d9f13SBarry Smith   xr  = A->n; yr = A->m; h = yr/10.0; w = xr/10.0;
564480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
565b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
566b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
567480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5683a40ed3dSBarry Smith   PetscFunctionReturn(0);
569416022c9SBarry Smith }
570416022c9SBarry Smith 
5714a2ae208SSatish Balay #undef __FUNCT__
5724a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
573b0a32e0cSBarry Smith int MatView_SeqAIJ(Mat A,PetscViewer viewer)
574416022c9SBarry Smith {
575416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
576bcd2baecSBarry Smith   int         ierr;
5776831982aSBarry Smith   PetscTruth  issocket,isascii,isbinary,isdraw;
578416022c9SBarry Smith 
5793a40ed3dSBarry Smith   PetscFunctionBegin;
580b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
581b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
582fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
583fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
5840f5bd95cSBarry Smith   if (issocket) {
58501820c62SBarry Smith     if (a->indexshift) {
58629bbc08cSBarry Smith       SETERRQ(1,"Can only socket send sparse matrix with 0 based indexing");
58701820c62SBarry Smith     }
588b0a32e0cSBarry Smith     ierr = PetscViewerSocketPutSparse_Private(viewer,A->m,A->n,a->nz,a->a,a->i,a->j);CHKERRQ(ierr);
5890f5bd95cSBarry Smith   } else if (isascii) {
5903a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
5910f5bd95cSBarry Smith   } else if (isbinary) {
5923a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
5930f5bd95cSBarry Smith   } else if (isdraw) {
5943a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
5955cd90555SBarry Smith   } else {
59629bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
59717ab2063SBarry Smith   }
5983a40ed3dSBarry Smith   PetscFunctionReturn(0);
59917ab2063SBarry Smith }
60019bcc07fSBarry Smith 
601*3a7fca6bSBarry Smith EXTERN int Mat_AIJ_CheckInode(Mat,PetscTruth);
6024a2ae208SSatish Balay #undef __FUNCT__
6034a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
6048f6be9afSLois Curfman McInnes int MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
60517ab2063SBarry Smith {
606416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
60741c01911SSatish Balay   int        fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax,ierr;
608273d9f13SBarry Smith   int        m = A->m,*ip,N,*ailen = a->ilen,shift = a->indexshift,rmax = 0;
609416022c9SBarry Smith   Scalar     *aa = a->a,*ap;
61017ab2063SBarry Smith 
6113a40ed3dSBarry Smith   PetscFunctionBegin;
6123a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
61317ab2063SBarry Smith 
61443ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
61517ab2063SBarry Smith   for (i=1; i<m; i++) {
616416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
61717ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
61894a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
61917ab2063SBarry Smith     if (fshift) {
6200f5bd95cSBarry Smith       ip = aj + ai[i] + shift;
6210f5bd95cSBarry Smith       ap = aa + ai[i] + shift;
62217ab2063SBarry Smith       N  = ailen[i];
62317ab2063SBarry Smith       for (j=0; j<N; j++) {
62417ab2063SBarry Smith         ip[j-fshift] = ip[j];
62517ab2063SBarry Smith         ap[j-fshift] = ap[j];
62617ab2063SBarry Smith       }
62717ab2063SBarry Smith     }
62817ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
62917ab2063SBarry Smith   }
63017ab2063SBarry Smith   if (m) {
63117ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
63217ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
63317ab2063SBarry Smith   }
63417ab2063SBarry Smith   /* reset ilen and imax for each row */
63517ab2063SBarry Smith   for (i=0; i<m; i++) {
63617ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
63717ab2063SBarry Smith   }
638416022c9SBarry Smith   a->nz = ai[m] + shift;
63917ab2063SBarry Smith 
64017ab2063SBarry Smith   /* diagonals may have moved, so kill the diagonal pointers */
641416022c9SBarry Smith   if (fshift && a->diag) {
642606d414cSSatish Balay     ierr = PetscFree(a->diag);CHKERRQ(ierr);
643b0a32e0cSBarry Smith     PetscLogObjectMemory(A,-(m+1)*sizeof(int));
644416022c9SBarry Smith     a->diag = 0;
64517ab2063SBarry Smith   }
646b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded,%d used\n",m,A->n,fshift,a->nz);
647b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %d\n",a->reallocs);
648b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Most nonzeros in any row is %d\n",rmax);
649dd5f02e7SSatish Balay   a->reallocs          = 0;
6504e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
65136db0b34SBarry Smith   a->rmax              = rmax;
6524e220ebcSLois Curfman McInnes 
65376dd722bSSatish Balay   /* check out for identical nodes. If found, use inode functions */
654*3a7fca6bSBarry Smith   ierr = Mat_AIJ_CheckInode(A,(PetscTruth)(!fshift));CHKERRQ(ierr);
6553a40ed3dSBarry Smith   PetscFunctionReturn(0);
65617ab2063SBarry Smith }
65717ab2063SBarry Smith 
6584a2ae208SSatish Balay #undef __FUNCT__
6594a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
6608f6be9afSLois Curfman McInnes int MatZeroEntries_SeqAIJ(Mat A)
66117ab2063SBarry Smith {
662416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
663549d3d68SSatish Balay   int        ierr;
6643a40ed3dSBarry Smith 
6653a40ed3dSBarry Smith   PetscFunctionBegin;
666273d9f13SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->m]+a->indexshift)*sizeof(Scalar));CHKERRQ(ierr);
6673a40ed3dSBarry Smith   PetscFunctionReturn(0);
66817ab2063SBarry Smith }
669416022c9SBarry Smith 
6704a2ae208SSatish Balay #undef __FUNCT__
6714a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
672e1311b90SBarry Smith int MatDestroy_SeqAIJ(Mat A)
67317ab2063SBarry Smith {
674416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
67582bf6240SBarry Smith   int        ierr;
676d5d45c9bSBarry Smith 
6773a40ed3dSBarry Smith   PetscFunctionBegin;
678aa482453SBarry Smith #if defined(PETSC_USE_LOG)
679b0a32e0cSBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%d, Cols=%d, NZ=%d",A->m,A->n,a->nz);
68017ab2063SBarry Smith #endif
68136db0b34SBarry Smith   if (a->freedata) {
682606d414cSSatish Balay     ierr = PetscFree(a->a);CHKERRQ(ierr);
683606d414cSSatish Balay     if (!a->singlemalloc) {
684606d414cSSatish Balay       ierr = PetscFree(a->i);CHKERRQ(ierr);
685606d414cSSatish Balay       ierr = PetscFree(a->j);CHKERRQ(ierr);
686606d414cSSatish Balay     }
68736db0b34SBarry Smith   }
688c38d4ed2SBarry Smith   if (a->row) {
689c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
690c38d4ed2SBarry Smith   }
691c38d4ed2SBarry Smith   if (a->col) {
692c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
693c38d4ed2SBarry Smith   }
694606d414cSSatish Balay   if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
695606d414cSSatish Balay   if (a->ilen) {ierr = PetscFree(a->ilen);CHKERRQ(ierr);}
696606d414cSSatish Balay   if (a->imax) {ierr = PetscFree(a->imax);CHKERRQ(ierr);}
697273d9f13SBarry Smith   if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);}
698606d414cSSatish Balay   if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);}
699606d414cSSatish Balay   if (a->inode.size) {ierr = PetscFree(a->inode.size);CHKERRQ(ierr);}
70082bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
701606d414cSSatish Balay   if (a->saved_values) {ierr = PetscFree(a->saved_values);CHKERRQ(ierr);}
702cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
703606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
7043a40ed3dSBarry Smith   PetscFunctionReturn(0);
70517ab2063SBarry Smith }
70617ab2063SBarry Smith 
7074a2ae208SSatish Balay #undef __FUNCT__
7084a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
7098f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A)
71017ab2063SBarry Smith {
7113a40ed3dSBarry Smith   PetscFunctionBegin;
7123a40ed3dSBarry Smith   PetscFunctionReturn(0);
71317ab2063SBarry Smith }
71417ab2063SBarry Smith 
7154a2ae208SSatish Balay #undef __FUNCT__
7164a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
7178f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op)
71817ab2063SBarry Smith {
719416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7203a40ed3dSBarry Smith 
7213a40ed3dSBarry Smith   PetscFunctionBegin;
722f1e2ffcdSBarry Smith   if      (op == MAT_ROW_ORIENTED)                 a->roworiented       = PETSC_TRUE;
723f1e2ffcdSBarry Smith   else if (op == MAT_KEEP_ZEROED_ROWS)             a->keepzeroedrows    = PETSC_TRUE;
724f1e2ffcdSBarry Smith   else if (op == MAT_COLUMN_ORIENTED)              a->roworiented       = PETSC_FALSE;
725f1e2ffcdSBarry Smith   else if (op == MAT_COLUMNS_SORTED)               a->sorted            = PETSC_TRUE;
726f1e2ffcdSBarry Smith   else if (op == MAT_COLUMNS_UNSORTED)             a->sorted            = PETSC_FALSE;
7276d4a8577SBarry Smith   else if (op == MAT_NO_NEW_NONZERO_LOCATIONS)     a->nonew             = 1;
7284787f768SSatish Balay   else if (op == MAT_NEW_NONZERO_LOCATION_ERR)     a->nonew             = -1;
7294787f768SSatish Balay   else if (op == MAT_NEW_NONZERO_ALLOCATION_ERR)   a->nonew             = -2;
7306d4a8577SBarry Smith   else if (op == MAT_YES_NEW_NONZERO_LOCATIONS)    a->nonew             = 0;
73136db0b34SBarry Smith   else if (op == MAT_IGNORE_ZERO_ENTRIES)          a->ignorezeroentries = PETSC_TRUE;
732b65db4caSBarry Smith   else if (op == MAT_USE_INODES)                   a->inode.use         = PETSC_TRUE;
733b65db4caSBarry Smith   else if (op == MAT_DO_NOT_USE_INODES)            a->inode.use         = PETSC_FALSE;
7346d4a8577SBarry Smith   else if (op == MAT_ROWS_SORTED ||
735219d9a1aSLois Curfman McInnes            op == MAT_ROWS_UNSORTED ||
73690f02eecSBarry Smith            op == MAT_YES_NEW_DIAGONALS ||
737b51ba29fSSatish Balay            op == MAT_IGNORE_OFF_PROC_ENTRIES||
738b51ba29fSSatish Balay            op == MAT_USE_HASH_TABLE)
739b0a32e0cSBarry Smith     PetscLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n");
7403a40ed3dSBarry Smith   else if (op == MAT_NO_NEW_DIAGONALS) {
74129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
7423a40ed3dSBarry Smith   } else if (op == MAT_INODE_LIMIT_1)          a->inode.limit  = 1;
7436d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_2)            a->inode.limit  = 2;
7446d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_3)            a->inode.limit  = 3;
7456d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_4)            a->inode.limit  = 4;
7466d4a8577SBarry Smith   else if (op == MAT_INODE_LIMIT_5)            a->inode.limit  = 5;
74729bbc08cSBarry Smith   else SETERRQ(PETSC_ERR_SUP,"unknown option");
7483a40ed3dSBarry Smith   PetscFunctionReturn(0);
74917ab2063SBarry Smith }
75017ab2063SBarry Smith 
7514a2ae208SSatish Balay #undef __FUNCT__
7524a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
7538f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v)
75417ab2063SBarry Smith {
755416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7563a40ed3dSBarry Smith   int        i,j,n,shift = a->indexshift,ierr;
75717ab2063SBarry Smith   Scalar     *x,zero = 0.0;
75817ab2063SBarry Smith 
7593a40ed3dSBarry Smith   PetscFunctionBegin;
7603a40ed3dSBarry Smith   ierr = VecSet(&zero,v);CHKERRQ(ierr);
76136db0b34SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
76236db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
763273d9f13SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
764273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
765416022c9SBarry Smith     for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
766416022c9SBarry Smith       if (a->j[j]+shift == i) {
767416022c9SBarry Smith         x[i] = a->a[j];
76817ab2063SBarry Smith         break;
76917ab2063SBarry Smith       }
77017ab2063SBarry Smith     }
77117ab2063SBarry Smith   }
77236db0b34SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
7733a40ed3dSBarry Smith   PetscFunctionReturn(0);
77417ab2063SBarry Smith }
77517ab2063SBarry Smith 
7764a2ae208SSatish Balay #undef __FUNCT__
7774a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_SeqAIJ"
7787c922b88SBarry Smith int MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
77917ab2063SBarry Smith {
780416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
781f1af5d2fSBarry Smith   Scalar     *x,*y,*v,alpha,zero = 0.0;
782273d9f13SBarry Smith   int        ierr,m = A->m,n,i,*idx,shift = a->indexshift;
78317ab2063SBarry Smith 
7843a40ed3dSBarry Smith   PetscFunctionBegin;
785f1af5d2fSBarry Smith   ierr = VecSet(&zero,yy);CHKERRQ(ierr);
786e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
787e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
78817ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
78917ab2063SBarry Smith   for (i=0; i<m; i++) {
790416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
791416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
792416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
79317ab2063SBarry Smith     alpha = x[i];
79417ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
79517ab2063SBarry Smith   }
796b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz - A->n);
797e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
798e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
7993a40ed3dSBarry Smith   PetscFunctionReturn(0);
80017ab2063SBarry Smith }
801d5d45c9bSBarry Smith 
8024a2ae208SSatish Balay #undef __FUNCT__
8034a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
8047c922b88SBarry Smith int MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
80517ab2063SBarry Smith {
806416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
80717ab2063SBarry Smith   Scalar     *x,*y,*v,alpha;
808273d9f13SBarry Smith   int        ierr,m = A->m,n,i,*idx,shift = a->indexshift;
80917ab2063SBarry Smith 
8103a40ed3dSBarry Smith   PetscFunctionBegin;
8112e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
812e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
813e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
81417ab2063SBarry Smith   y = y + shift; /* shift for Fortran start by 1 indexing */
81517ab2063SBarry Smith   for (i=0; i<m; i++) {
816416022c9SBarry Smith     idx   = a->j + a->i[i] + shift;
817416022c9SBarry Smith     v     = a->a + a->i[i] + shift;
818416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
81917ab2063SBarry Smith     alpha = x[i];
82017ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
82117ab2063SBarry Smith   }
822b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
823e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
824e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8253a40ed3dSBarry Smith   PetscFunctionReturn(0);
82617ab2063SBarry Smith }
82717ab2063SBarry Smith 
8284a2ae208SSatish Balay #undef __FUNCT__
8294a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
83044cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
83117ab2063SBarry Smith {
832416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
83317ab2063SBarry Smith   Scalar     *x,*y,*v,sum;
834273d9f13SBarry Smith   int        ierr,m = A->m,*idx,shift = a->indexshift,*ii;
835aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
836e36a17ebSSatish Balay   int        n,i,jrow,j;
837e36a17ebSSatish Balay #endif
83817ab2063SBarry Smith 
839aa482453SBarry Smith #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
840fee21e36SBarry Smith #pragma disjoint(*x,*y,*v)
841fee21e36SBarry Smith #endif
842fee21e36SBarry Smith 
8433a40ed3dSBarry Smith   PetscFunctionBegin;
844e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
845e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
84617ab2063SBarry Smith   x    = x + shift;    /* shift for Fortran start by 1 indexing */
847416022c9SBarry Smith   idx  = a->j;
848d64ed03dSBarry Smith   v    = a->a;
849416022c9SBarry Smith   ii   = a->i;
850aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
85129b5ca8aSSatish Balay   fortranmultaij_(&m,x,ii,idx+shift,v+shift,y);
8528d195f9aSBarry Smith #else
853d64ed03dSBarry Smith   v    += shift; /* shift for Fortran start by 1 indexing */
854d64ed03dSBarry Smith   idx  += shift;
85517ab2063SBarry Smith   for (i=0; i<m; i++) {
8569ea0dfa2SSatish Balay     jrow = ii[i];
8579ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
85817ab2063SBarry Smith     sum  = 0.0;
8599ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
8609ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
8619ea0dfa2SSatish Balay      }
86217ab2063SBarry Smith     y[i] = sum;
86317ab2063SBarry Smith   }
8648d195f9aSBarry Smith #endif
865b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz - m);
866e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
867e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8683a40ed3dSBarry Smith   PetscFunctionReturn(0);
86917ab2063SBarry Smith }
87017ab2063SBarry Smith 
8714a2ae208SSatish Balay #undef __FUNCT__
8724a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
87344cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
87417ab2063SBarry Smith {
875416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
87617ab2063SBarry Smith   Scalar     *x,*y,*z,*v,sum;
877273d9f13SBarry Smith   int        ierr,m = A->m,*idx,shift = a->indexshift,*ii;
878aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
879e36a17ebSSatish Balay   int        n,i,jrow,j;
880e36a17ebSSatish Balay #endif
8819ea0dfa2SSatish Balay 
8823a40ed3dSBarry Smith   PetscFunctionBegin;
883e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
884e1311b90SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
8852e8a6d31SBarry Smith   if (zz != yy) {
886e1311b90SBarry Smith     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
8872e8a6d31SBarry Smith   } else {
8882e8a6d31SBarry Smith     z = y;
8892e8a6d31SBarry Smith   }
89017ab2063SBarry Smith   x    = x + shift; /* shift for Fortran start by 1 indexing */
891cddf8d76SBarry Smith   idx  = a->j;
892d64ed03dSBarry Smith   v    = a->a;
893cddf8d76SBarry Smith   ii   = a->i;
894aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
89529b5ca8aSSatish Balay   fortranmultaddaij_(&m,x,ii,idx+shift,v+shift,y,z);
89602ab625aSSatish Balay #else
897d64ed03dSBarry Smith   v   += shift; /* shift for Fortran start by 1 indexing */
898d64ed03dSBarry Smith   idx += shift;
89917ab2063SBarry Smith   for (i=0; i<m; i++) {
9009ea0dfa2SSatish Balay     jrow = ii[i];
9019ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
90217ab2063SBarry Smith     sum  = y[i];
9039ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
9049ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
9059ea0dfa2SSatish Balay      }
90617ab2063SBarry Smith     z[i] = sum;
90717ab2063SBarry Smith   }
90802ab625aSSatish Balay #endif
909b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
910e1311b90SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
911e1311b90SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9122e8a6d31SBarry Smith   if (zz != yy) {
913e1311b90SBarry Smith     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
9142e8a6d31SBarry Smith   }
9153a40ed3dSBarry Smith   PetscFunctionReturn(0);
91617ab2063SBarry Smith }
91717ab2063SBarry Smith 
91817ab2063SBarry Smith /*
91917ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
92017ab2063SBarry Smith */
9214a2ae208SSatish Balay #undef __FUNCT__
9224a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
923f1e2ffcdSBarry Smith int MatMarkDiagonal_SeqAIJ(Mat A)
92417ab2063SBarry Smith {
925416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
926b0a32e0cSBarry Smith   int        i,j,*diag,m = A->m,shift = a->indexshift,ierr;
92717ab2063SBarry Smith 
9283a40ed3dSBarry Smith   PetscFunctionBegin;
929f1e2ffcdSBarry Smith   if (a->diag) PetscFunctionReturn(0);
930f1e2ffcdSBarry Smith 
931b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&diag);CHKERRQ(ierr);
932b0a32e0cSBarry Smith   PetscLogObjectMemory(A,(m+1)*sizeof(int));
933273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
93435b0346bSBarry Smith     diag[i] = a->i[i+1];
935416022c9SBarry Smith     for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
936416022c9SBarry Smith       if (a->j[j]+shift == i) {
93717ab2063SBarry Smith         diag[i] = j - shift;
93817ab2063SBarry Smith         break;
93917ab2063SBarry Smith       }
94017ab2063SBarry Smith     }
94117ab2063SBarry Smith   }
942416022c9SBarry Smith   a->diag = diag;
9433a40ed3dSBarry Smith   PetscFunctionReturn(0);
94417ab2063SBarry Smith }
94517ab2063SBarry Smith 
946be5855fcSBarry Smith /*
947be5855fcSBarry Smith      Checks for missing diagonals
948be5855fcSBarry Smith */
9494a2ae208SSatish Balay #undef __FUNCT__
9504a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
951f1e2ffcdSBarry Smith int MatMissingDiagonal_SeqAIJ(Mat A)
952be5855fcSBarry Smith {
953be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
954f1e2ffcdSBarry Smith   int        *diag,*jj = a->j,i,shift = a->indexshift,ierr;
955be5855fcSBarry Smith 
956be5855fcSBarry Smith   PetscFunctionBegin;
957f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
958f1e2ffcdSBarry Smith   diag = a->diag;
959273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
960be5855fcSBarry Smith     if (jj[diag[i]+shift] != i-shift) {
96129bbc08cSBarry Smith       SETERRQ1(1,"Matrix is missing diagonal number %d",i);
962be5855fcSBarry Smith     }
963be5855fcSBarry Smith   }
964be5855fcSBarry Smith   PetscFunctionReturn(0);
965be5855fcSBarry Smith }
966be5855fcSBarry Smith 
9674a2ae208SSatish Balay #undef __FUNCT__
9684a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
96936db0b34SBarry Smith int MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,Vec xx)
97017ab2063SBarry Smith {
971416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
972d6ed3216SSatish Balay   Scalar     *x,*b,*bs, d,*xs,sum,*v = a->a,*t=0,scale,*ts,*xb,*idiag=0;
973273d9f13SBarry Smith   int        ierr,*idx,*diag,n = A->n,m = A->m,i,shift = a->indexshift;
97417ab2063SBarry Smith 
9753a40ed3dSBarry Smith   PetscFunctionBegin;
976e1311b90SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
977fb2e594dSBarry Smith   if (xx != bb) {
978e1311b90SBarry Smith     ierr = VecGetArray(bb,&b);CHKERRQ(ierr);
979fb2e594dSBarry Smith   } else {
980fb2e594dSBarry Smith     b = x;
981fb2e594dSBarry Smith   }
982fb2e594dSBarry Smith 
983f1e2ffcdSBarry Smith   if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);}
984416022c9SBarry Smith   diag = a->diag;
985416022c9SBarry Smith   xs   = x + shift; /* shifted by one for index start of a or a->j*/
98617ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
98717ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
98817ab2063SBarry Smith     bs = b + shift;
98917ab2063SBarry Smith     for (i=0; i<m; i++) {
990416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
991416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
992b0a32e0cSBarry Smith 	PetscLogFlops(2*n-1);
993416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
994416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
99517ab2063SBarry Smith         sum  = b[i]*d/omega;
99617ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
99717ab2063SBarry Smith         x[i] = sum;
99817ab2063SBarry Smith     }
999cb5b572fSBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1000fb2e594dSBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
10013a40ed3dSBarry Smith     PetscFunctionReturn(0);
100217ab2063SBarry Smith   }
1003c783ea89SBarry Smith 
1004c783ea89SBarry Smith   /* setup workspace for Eisenstat */
1005c783ea89SBarry Smith   if (flag & SOR_EISENSTAT) {
1006c783ea89SBarry Smith     if (!a->idiag) {
1007b0a32e0cSBarry Smith       ierr     = PetscMalloc(2*m*sizeof(Scalar),&a->idiag);CHKERRQ(ierr);
1008c783ea89SBarry Smith       a->ssor  = a->idiag + m;
1009c783ea89SBarry Smith       v        = a->a;
1010c783ea89SBarry Smith       for (i=0; i<m; i++) { a->idiag[i] = 1.0/v[diag[i]];}
1011c783ea89SBarry Smith     }
1012c783ea89SBarry Smith     t     = a->ssor;
1013c783ea89SBarry Smith     idiag = a->idiag;
1014c783ea89SBarry Smith   }
1015fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1016fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1017fc3d8934SBarry Smith 
1018fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1019fc3d8934SBarry Smith 
1020fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1021fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
102248af12d7SBarry Smith     is the relaxation factor.
1023fc3d8934SBarry Smith     */
1024fc3d8934SBarry Smith 
102548af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
102629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
102748af12d7SBarry Smith   } else if ((flag & SOR_EISENSTAT) && omega == 1.0 && shift == 0 && fshift == 0.0) {
102848af12d7SBarry Smith     /* special case for omega = 1.0 saves flops and some integer ops */
1029733d66baSBarry Smith     Scalar *v2;
103048af12d7SBarry Smith 
1031733d66baSBarry Smith     v2    = a->a;
1032fc3d8934SBarry Smith     /*  x = (E + U)^{-1} b */
1033fc3d8934SBarry Smith     for (i=m-1; i>=0; i--) {
1034fc3d8934SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1035fc3d8934SBarry Smith       idx  = a->j + diag[i] + 1;
1036fc3d8934SBarry Smith       v    = a->a + diag[i] + 1;
1037fc3d8934SBarry Smith       sum  = b[i];
1038fc3d8934SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1039b4632166SBarry Smith       x[i] = sum*idiag[i];
1040fc3d8934SBarry Smith 
1041fc3d8934SBarry Smith       /*  t = b - (2*E - D)x */
1042733d66baSBarry Smith       t[i] = b[i] - (v2[diag[i]])*x[i];
1043733d66baSBarry Smith     }
1044fc3d8934SBarry Smith 
1045fc3d8934SBarry Smith     /*  t = (E + L)^{-1}t */
1046fc3d8934SBarry Smith     diag = a->diag;
1047fc3d8934SBarry Smith     for (i=0; i<m; i++) {
1048fc3d8934SBarry Smith       n    = diag[i] - a->i[i];
1049fc3d8934SBarry Smith       idx  = a->j + a->i[i];
1050fc3d8934SBarry Smith       v    = a->a + a->i[i];
1051fc3d8934SBarry Smith       sum  = t[i];
1052fc3d8934SBarry Smith       SPARSEDENSEMDOT(sum,t,v,idx,n);
1053b4632166SBarry Smith       t[i]  = sum*idiag[i];
1054fc3d8934SBarry Smith 
1055fc3d8934SBarry Smith       /*  x = x + t */
1056733d66baSBarry Smith       x[i] += t[i];
1057733d66baSBarry Smith     }
1058733d66baSBarry Smith 
1059b0a32e0cSBarry Smith     PetscLogFlops(3*m-1 + 2*a->nz);
1060fc3d8934SBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1061fc3d8934SBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
1062fc3d8934SBarry Smith     PetscFunctionReturn(0);
10633a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
106417ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
106517ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
106617ab2063SBarry Smith 
106717ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
106817ab2063SBarry Smith 
106917ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
107017ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
107117ab2063SBarry Smith     is the relaxation factor.
107217ab2063SBarry Smith     */
107317ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
107417ab2063SBarry Smith 
107517ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
107617ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1077416022c9SBarry Smith       d    = fshift + a->a[diag[i] + shift];
1078416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1079416022c9SBarry Smith       idx  = a->j + diag[i] + (!shift);
1080416022c9SBarry Smith       v    = a->a + diag[i] + (!shift);
108117ab2063SBarry Smith       sum  = b[i];
108217ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
108317ab2063SBarry Smith       x[i] = omega*(sum/d);
108417ab2063SBarry Smith     }
108517ab2063SBarry Smith 
108617ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1087416022c9SBarry Smith     v = a->a;
108817ab2063SBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++ + shift])*x[i]; }
108917ab2063SBarry Smith 
109017ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1091416022c9SBarry Smith     ts = t + shift; /* shifted by one for index start of a or a->j*/
1092416022c9SBarry Smith     diag = a->diag;
109317ab2063SBarry Smith     for (i=0; i<m; i++) {
1094416022c9SBarry Smith       d    = fshift + a->a[diag[i]+shift];
1095416022c9SBarry Smith       n    = diag[i] - a->i[i];
1096416022c9SBarry Smith       idx  = a->j + a->i[i] + shift;
1097416022c9SBarry Smith       v    = a->a + a->i[i] + shift;
109817ab2063SBarry Smith       sum  = t[i];
109917ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
110017ab2063SBarry Smith       t[i] = omega*(sum/d);
1101733d66baSBarry Smith       /*  x = x + t */
1102733d66baSBarry Smith       x[i] += t[i];
110317ab2063SBarry Smith     }
110417ab2063SBarry Smith 
1105b0a32e0cSBarry Smith     PetscLogFlops(6*m-1 + 2*a->nz);
1106cb5b572fSBarry Smith     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1107fb2e594dSBarry Smith     if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
11083a40ed3dSBarry Smith     PetscFunctionReturn(0);
110917ab2063SBarry Smith   }
111017ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
111117ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
111217ab2063SBarry Smith       for (i=0; i<m; i++) {
1113416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1114416022c9SBarry Smith         n    = diag[i] - a->i[i];
1115b0a32e0cSBarry Smith 	PetscLogFlops(2*n-1);
1116416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1117416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
111817ab2063SBarry Smith         sum  = b[i];
111917ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
112017ab2063SBarry Smith         x[i] = omega*(sum/d);
112117ab2063SBarry Smith       }
112217ab2063SBarry Smith       xb = x;
11233a40ed3dSBarry Smith     } else xb = b;
112417ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
112517ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
112617ab2063SBarry Smith       for (i=0; i<m; i++) {
1127416022c9SBarry Smith         x[i] *= a->a[diag[i]+shift];
112817ab2063SBarry Smith       }
1129b0a32e0cSBarry Smith       PetscLogFlops(m);
113017ab2063SBarry Smith     }
113117ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
113217ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1133416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1134416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1135b0a32e0cSBarry Smith 	PetscLogFlops(2*n-1);
1136416022c9SBarry Smith         idx  = a->j + diag[i] + (!shift);
1137416022c9SBarry Smith         v    = a->a + diag[i] + (!shift);
113817ab2063SBarry Smith         sum  = xb[i];
113917ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
114017ab2063SBarry Smith         x[i] = omega*(sum/d);
114117ab2063SBarry Smith       }
114217ab2063SBarry Smith     }
114317ab2063SBarry Smith     its--;
114417ab2063SBarry Smith   }
114517ab2063SBarry Smith   while (its--) {
114617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
114717ab2063SBarry Smith       for (i=0; i<m; i++) {
1148416022c9SBarry Smith         d    = fshift + a->a[diag[i]+shift];
1149416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1150b0a32e0cSBarry Smith 	PetscLogFlops(2*n-1);
1151416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1152416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
115317ab2063SBarry Smith         sum  = b[i];
115417ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
11557e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
115617ab2063SBarry Smith       }
115717ab2063SBarry Smith     }
115817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
115917ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1160416022c9SBarry Smith         d    = fshift + a->a[diag[i] + shift];
1161416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1162b0a32e0cSBarry Smith 	PetscLogFlops(2*n-1);
1163416022c9SBarry Smith         idx  = a->j + a->i[i] + shift;
1164416022c9SBarry Smith         v    = a->a + a->i[i] + shift;
116517ab2063SBarry Smith         sum  = b[i];
116617ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
11677e33a6baSBarry Smith         x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d;
116817ab2063SBarry Smith       }
116917ab2063SBarry Smith     }
117017ab2063SBarry Smith   }
1171cb5b572fSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1172fb2e594dSBarry Smith   if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);}
11733a40ed3dSBarry Smith   PetscFunctionReturn(0);
117417ab2063SBarry Smith }
117517ab2063SBarry Smith 
11764a2ae208SSatish Balay #undef __FUNCT__
11774a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
11788f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
117917ab2063SBarry Smith {
1180416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
11814e220ebcSLois Curfman McInnes 
11823a40ed3dSBarry Smith   PetscFunctionBegin;
1183273d9f13SBarry Smith   info->rows_global    = (double)A->m;
1184273d9f13SBarry Smith   info->columns_global = (double)A->n;
1185273d9f13SBarry Smith   info->rows_local     = (double)A->m;
1186273d9f13SBarry Smith   info->columns_local  = (double)A->n;
11874e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
11884e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
11894e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
11904e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
11914e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
11924e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
11934e220ebcSLois Curfman McInnes   info->memory         = A->mem;
11944e220ebcSLois Curfman McInnes   if (A->factor) {
11954e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
11964e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
11974e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
11984e220ebcSLois Curfman McInnes   } else {
11994e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12004e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12014e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12024e220ebcSLois Curfman McInnes   }
12033a40ed3dSBarry Smith   PetscFunctionReturn(0);
120417ab2063SBarry Smith }
120517ab2063SBarry Smith 
1206b9b97703SBarry Smith EXTERN int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,MatLUInfo*,Mat*);
1207ca44d042SBarry Smith EXTERN int MatLUFactorNumeric_SeqAIJ(Mat,Mat*);
1208b9b97703SBarry Smith EXTERN int MatLUFactor_SeqAIJ(Mat,IS,IS,MatLUInfo*);
1209ca44d042SBarry Smith EXTERN int MatSolve_SeqAIJ(Mat,Vec,Vec);
1210ca44d042SBarry Smith EXTERN int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec);
1211ca44d042SBarry Smith EXTERN int MatSolveTranspose_SeqAIJ(Mat,Vec,Vec);
1212ca44d042SBarry Smith EXTERN int MatSolveTransposeAdd_SeqAIJ(Mat,Vec,Vec,Vec);
121317ab2063SBarry Smith 
12144a2ae208SSatish Balay #undef __FUNCT__
12154a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
12168f6be9afSLois Curfman McInnes int MatZeroRows_SeqAIJ(Mat A,IS is,Scalar *diag)
121717ab2063SBarry Smith {
1218416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1219273d9f13SBarry Smith   int         i,ierr,N,*rows,m = A->m - 1,shift = a->indexshift;
122017ab2063SBarry Smith 
12213a40ed3dSBarry Smith   PetscFunctionBegin;
1222b9b97703SBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
122317ab2063SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
1224f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1225f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
122629bbc08cSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
1227f1e2ffcdSBarry Smith       ierr = PetscMemzero(&a->a[a->i[rows[i]]+shift],a->ilen[rows[i]]*sizeof(Scalar));CHKERRQ(ierr);
1228f1e2ffcdSBarry Smith     }
1229f1e2ffcdSBarry Smith     if (diag) {
1230f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1231f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1232f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1233f1e2ffcdSBarry Smith         a->a[a->diag[rows[i]]] = *diag;
1234f1e2ffcdSBarry Smith       }
1235f1e2ffcdSBarry Smith     }
1236f1e2ffcdSBarry Smith   } else {
123717ab2063SBarry Smith     if (diag) {
123817ab2063SBarry Smith       for (i=0; i<N; i++) {
123929bbc08cSBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
12407ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1241416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1242416022c9SBarry Smith           a->a[a->i[rows[i]]+shift] = *diag;
1243416022c9SBarry Smith           a->j[a->i[rows[i]]+shift] = rows[i]+shift;
12447ae801bdSBarry Smith         } else { /* in case row was completely empty */
1245d64ed03dSBarry Smith           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr);
124617ab2063SBarry Smith         }
124717ab2063SBarry Smith       }
12483a40ed3dSBarry Smith     } else {
124917ab2063SBarry Smith       for (i=0; i<N; i++) {
125029bbc08cSBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
1251416022c9SBarry Smith         a->ilen[rows[i]] = 0;
125217ab2063SBarry Smith       }
125317ab2063SBarry Smith     }
1254f1e2ffcdSBarry Smith   }
12557ae801bdSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
125643a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12573a40ed3dSBarry Smith   PetscFunctionReturn(0);
125817ab2063SBarry Smith }
125917ab2063SBarry Smith 
12604a2ae208SSatish Balay #undef __FUNCT__
12614a2ae208SSatish Balay #define __FUNCT__ "MatGetOwnershipRange_SeqAIJ"
12628f6be9afSLois Curfman McInnes int MatGetOwnershipRange_SeqAIJ(Mat A,int *m,int *n)
126317ab2063SBarry Smith {
12643a40ed3dSBarry Smith   PetscFunctionBegin;
12654c49b128SBarry Smith   if (m) *m = 0;
1266273d9f13SBarry Smith   if (n) *n = A->m;
12673a40ed3dSBarry Smith   PetscFunctionReturn(0);
126817ab2063SBarry Smith }
1269026e39d0SSatish Balay 
12704a2ae208SSatish Balay #undef __FUNCT__
12714a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
12724e093b46SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v)
127317ab2063SBarry Smith {
1274416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1275b0a32e0cSBarry Smith   int        *itmp,i,shift = a->indexshift,ierr;
127617ab2063SBarry Smith 
12773a40ed3dSBarry Smith   PetscFunctionBegin;
1278273d9f13SBarry Smith   if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %d out of range",row);
127917ab2063SBarry Smith 
1280416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1281416022c9SBarry Smith   if (v) *v = a->a + a->i[row] + shift;
128217ab2063SBarry Smith   if (idx) {
1283416022c9SBarry Smith     itmp = a->j + a->i[row] + shift;
12844e093b46SBarry Smith     if (*nz && shift) {
1285b0a32e0cSBarry Smith       ierr = PetscMalloc((*nz)*sizeof(int),idx);CHKERRQ(ierr);
128617ab2063SBarry Smith       for (i=0; i<(*nz); i++) {(*idx)[i] = itmp[i] + shift;}
12874e093b46SBarry Smith     } else if (*nz) {
12884e093b46SBarry Smith       *idx = itmp;
128917ab2063SBarry Smith     }
129017ab2063SBarry Smith     else *idx = 0;
129117ab2063SBarry Smith   }
12923a40ed3dSBarry Smith   PetscFunctionReturn(0);
129317ab2063SBarry Smith }
129417ab2063SBarry Smith 
12954a2ae208SSatish Balay #undef __FUNCT__
12964a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
12974e093b46SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v)
129817ab2063SBarry Smith {
12994e093b46SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1300606d414cSSatish Balay   int ierr;
13013a40ed3dSBarry Smith 
13023a40ed3dSBarry Smith   PetscFunctionBegin;
1303606d414cSSatish Balay   if (idx) {if (*idx && a->indexshift) {ierr = PetscFree(*idx);CHKERRQ(ierr);}}
13043a40ed3dSBarry Smith   PetscFunctionReturn(0);
130517ab2063SBarry Smith }
130617ab2063SBarry Smith 
13074a2ae208SSatish Balay #undef __FUNCT__
13084a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
130936db0b34SBarry Smith int MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *norm)
131017ab2063SBarry Smith {
1311416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1312416022c9SBarry Smith   Scalar     *v = a->a;
131336db0b34SBarry Smith   PetscReal  sum = 0.0;
1314549d3d68SSatish Balay   int        i,j,shift = a->indexshift,ierr;
131517ab2063SBarry Smith 
13163a40ed3dSBarry Smith   PetscFunctionBegin;
131717ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1318416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1319aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
132036db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
132117ab2063SBarry Smith #else
132217ab2063SBarry Smith       sum += (*v)*(*v); v++;
132317ab2063SBarry Smith #endif
132417ab2063SBarry Smith     }
132517ab2063SBarry Smith     *norm = sqrt(sum);
13263a40ed3dSBarry Smith   } else if (type == NORM_1) {
132736db0b34SBarry Smith     PetscReal *tmp;
1328416022c9SBarry Smith     int    *jj = a->j;
1329b0a32e0cSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1330273d9f13SBarry Smith     ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr);
133117ab2063SBarry Smith     *norm = 0.0;
1332416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1333a2744918SBarry Smith         tmp[*jj++ + shift] += PetscAbsScalar(*v);  v++;
133417ab2063SBarry Smith     }
1335273d9f13SBarry Smith     for (j=0; j<A->n; j++) {
133617ab2063SBarry Smith       if (tmp[j] > *norm) *norm = tmp[j];
133717ab2063SBarry Smith     }
1338606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
13393a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
134017ab2063SBarry Smith     *norm = 0.0;
1341273d9f13SBarry Smith     for (j=0; j<A->m; j++) {
1342416022c9SBarry Smith       v = a->a + a->i[j] + shift;
134317ab2063SBarry Smith       sum = 0.0;
1344416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1345cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
134617ab2063SBarry Smith       }
134717ab2063SBarry Smith       if (sum > *norm) *norm = sum;
134817ab2063SBarry Smith     }
13493a40ed3dSBarry Smith   } else {
135029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
135117ab2063SBarry Smith   }
13523a40ed3dSBarry Smith   PetscFunctionReturn(0);
135317ab2063SBarry Smith }
135417ab2063SBarry Smith 
13554a2ae208SSatish Balay #undef __FUNCT__
13564a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
13578f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B)
135817ab2063SBarry Smith {
1359416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1360416022c9SBarry Smith   Mat        C;
1361273d9f13SBarry Smith   int        i,ierr,*aj = a->j,*ai = a->i,m = A->m,len,*col;
1362273d9f13SBarry Smith   int        shift = a->indexshift;
1363d5d45c9bSBarry Smith   Scalar     *array = a->a;
136417ab2063SBarry Smith 
13653a40ed3dSBarry Smith   PetscFunctionBegin;
1366273d9f13SBarry Smith   if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1367b0a32e0cSBarry Smith   ierr = PetscMalloc((1+A->n)*sizeof(int),&col);CHKERRQ(ierr);
1368273d9f13SBarry Smith   ierr = PetscMemzero(col,(1+A->n)*sizeof(int));CHKERRQ(ierr);
136917ab2063SBarry Smith   if (shift) {
137017ab2063SBarry Smith     for (i=0; i<ai[m]-1; i++) aj[i] -= 1;
137117ab2063SBarry Smith   }
137217ab2063SBarry Smith   for (i=0; i<ai[m]+shift; i++) col[aj[i]] += 1;
1373273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,A->n,m,0,col,&C);CHKERRQ(ierr);
1374606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
137517ab2063SBarry Smith   for (i=0; i<m; i++) {
137617ab2063SBarry Smith     len    = ai[i+1]-ai[i];
1377416022c9SBarry Smith     ierr   = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1378b9b97703SBarry Smith     array += len;
1379b9b97703SBarry Smith     aj    += len;
138017ab2063SBarry Smith   }
138117ab2063SBarry Smith   if (shift) {
138217ab2063SBarry Smith     for (i=0; i<ai[m]-1; i++) aj[i] += 1;
138317ab2063SBarry Smith   }
138417ab2063SBarry Smith 
13856d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13866d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
138717ab2063SBarry Smith 
1388f1e2ffcdSBarry Smith   if (B) {
1389416022c9SBarry Smith     *B = C;
139017ab2063SBarry Smith   } else {
1391273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
139217ab2063SBarry Smith   }
13933a40ed3dSBarry Smith   PetscFunctionReturn(0);
139417ab2063SBarry Smith }
139517ab2063SBarry Smith 
13964a2ae208SSatish Balay #undef __FUNCT__
13974a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
13988f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
139917ab2063SBarry Smith {
1400416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
140117ab2063SBarry Smith   Scalar     *l,*r,x,*v;
1402273d9f13SBarry Smith   int        ierr,i,j,m = A->m,n = A->n,M,nz = a->nz,*jj,shift = a->indexshift;
140317ab2063SBarry Smith 
14043a40ed3dSBarry Smith   PetscFunctionBegin;
140517ab2063SBarry Smith   if (ll) {
14063ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
14073ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1408e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1409273d9f13SBarry Smith     if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
1410e1311b90SBarry Smith     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1411416022c9SBarry Smith     v = a->a;
141217ab2063SBarry Smith     for (i=0; i<m; i++) {
141317ab2063SBarry Smith       x = l[i];
1414416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
141517ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
141617ab2063SBarry Smith     }
1417e1311b90SBarry Smith     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1418b0a32e0cSBarry Smith     PetscLogFlops(nz);
141917ab2063SBarry Smith   }
142017ab2063SBarry Smith   if (rr) {
1421e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1422273d9f13SBarry Smith     if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
1423e1311b90SBarry Smith     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1424416022c9SBarry Smith     v = a->a; jj = a->j;
142517ab2063SBarry Smith     for (i=0; i<nz; i++) {
142617ab2063SBarry Smith       (*v++) *= r[*jj++ + shift];
142717ab2063SBarry Smith     }
1428e1311b90SBarry Smith     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1429b0a32e0cSBarry Smith     PetscLogFlops(nz);
143017ab2063SBarry Smith   }
14313a40ed3dSBarry Smith   PetscFunctionReturn(0);
143217ab2063SBarry Smith }
143317ab2063SBarry Smith 
14344a2ae208SSatish Balay #undef __FUNCT__
14354a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
14367b2a1423SBarry Smith int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,int csize,MatReuse scall,Mat *B)
143717ab2063SBarry Smith {
1438db02288aSLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data,*c;
1439273d9f13SBarry Smith   int          *smap,i,k,kstart,kend,ierr,oldcols = A->n,*lens;
144036db0b34SBarry Smith   int          row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
144199141d43SSatish Balay   int          *irow,*icol,nrows,ncols,shift = a->indexshift,*ssmap;
144299141d43SSatish Balay   int          *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
144399141d43SSatish Balay   Scalar       *a_new,*mat_a;
1444416022c9SBarry Smith   Mat          C;
1445fee21e36SBarry Smith   PetscTruth   stride;
144617ab2063SBarry Smith 
14473a40ed3dSBarry Smith   PetscFunctionBegin;
1448d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
144929bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1450d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
145129bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
145299141d43SSatish Balay 
145317ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1454b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1455b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
145617ab2063SBarry Smith 
1457fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1458fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1459fee21e36SBarry Smith   if (stride && step == 1) {
146002834360SBarry Smith     /* special case of contiguous rows */
1461b0a32e0cSBarry Smith     ierr   = PetscMalloc((ncols+nrows+1)*sizeof(int),&lens);CHKERRQ(ierr);
146202834360SBarry Smith     starts = lens + ncols;
146302834360SBarry Smith     /* loop over new rows determining lens and starting points */
146402834360SBarry Smith     for (i=0; i<nrows; i++) {
1465a2744918SBarry Smith       kstart  = ai[irow[i]]+shift;
1466a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
146702834360SBarry Smith       for (k=kstart; k<kend; k++) {
1468d8ced48eSBarry Smith         if (aj[k]+shift >= first) {
146902834360SBarry Smith           starts[i] = k;
147002834360SBarry Smith           break;
147102834360SBarry Smith 	}
147202834360SBarry Smith       }
1473a2744918SBarry Smith       sum = 0;
147402834360SBarry Smith       while (k < kend) {
1475d8ced48eSBarry Smith         if (aj[k++]+shift >= first+ncols) break;
1476a2744918SBarry Smith         sum++;
147702834360SBarry Smith       }
1478a2744918SBarry Smith       lens[i] = sum;
147902834360SBarry Smith     }
148002834360SBarry Smith     /* create submatrix */
1481cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
148208480c60SBarry Smith       int n_cols,n_rows;
148308480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
148429bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1485d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
148608480c60SBarry Smith       C = *B;
14873a40ed3dSBarry Smith     } else {
148802834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
148908480c60SBarry Smith     }
1490db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1491db02288aSLois Curfman McInnes 
149202834360SBarry Smith     /* loop over rows inserting into submatrix */
1493db02288aSLois Curfman McInnes     a_new    = c->a;
1494db02288aSLois Curfman McInnes     j_new    = c->j;
1495db02288aSLois Curfman McInnes     i_new    = c->i;
1496db02288aSLois Curfman McInnes     i_new[0] = -shift;
149702834360SBarry Smith     for (i=0; i<nrows; i++) {
1498a2744918SBarry Smith       ii    = starts[i];
1499a2744918SBarry Smith       lensi = lens[i];
1500a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1501a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
150202834360SBarry Smith       }
1503549d3d68SSatish Balay       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(Scalar));CHKERRQ(ierr);
1504a2744918SBarry Smith       a_new      += lensi;
1505a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1506a2744918SBarry Smith       c->ilen[i]  = lensi;
150702834360SBarry Smith     }
1508606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
15093a40ed3dSBarry Smith   } else {
151002834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
1511b0a32e0cSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(int),&smap);CHKERRQ(ierr);
151202834360SBarry Smith     ssmap = smap + shift;
1513b0a32e0cSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(int),&lens);CHKERRQ(ierr);
1514549d3d68SSatish Balay     ierr  = PetscMemzero(smap,oldcols*sizeof(int));CHKERRQ(ierr);
151517ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
151602834360SBarry Smith     /* determine lens of each row */
151702834360SBarry Smith     for (i=0; i<nrows; i++) {
1518d8ced48eSBarry Smith       kstart  = ai[irow[i]]+shift;
151902834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
152002834360SBarry Smith       lens[i] = 0;
152102834360SBarry Smith       for (k=kstart; k<kend; k++) {
1522d8ced48eSBarry Smith         if (ssmap[aj[k]]) {
152302834360SBarry Smith           lens[i]++;
152402834360SBarry Smith         }
152502834360SBarry Smith       }
152602834360SBarry Smith     }
152717ab2063SBarry Smith     /* Create and fill new matrix */
1528a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
15290f5bd95cSBarry Smith       PetscTruth equal;
15300f5bd95cSBarry Smith 
153199141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1532273d9f13SBarry Smith       if ((*B)->m  != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1533273d9f13SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(int),&equal);CHKERRQ(ierr);
15340f5bd95cSBarry Smith       if (!equal) {
153529bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
153699141d43SSatish Balay       }
1537273d9f13SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(int));CHKERRQ(ierr);
153808480c60SBarry Smith       C = *B;
15393a40ed3dSBarry Smith     } else {
154002834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
154108480c60SBarry Smith     }
154299141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
154317ab2063SBarry Smith     for (i=0; i<nrows; i++) {
154499141d43SSatish Balay       row    = irow[i];
154599141d43SSatish Balay       kstart = ai[row]+shift;
154699141d43SSatish Balay       kend   = kstart + a->ilen[row];
154799141d43SSatish Balay       mat_i  = c->i[i]+shift;
154899141d43SSatish Balay       mat_j  = c->j + mat_i;
154999141d43SSatish Balay       mat_a  = c->a + mat_i;
155099141d43SSatish Balay       mat_ilen = c->ilen + i;
155117ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
155299141d43SSatish Balay         if ((tcol=ssmap[a->j[k]])) {
155399141d43SSatish Balay           *mat_j++ = tcol - (!shift);
155499141d43SSatish Balay           *mat_a++ = a->a[k];
155599141d43SSatish Balay           (*mat_ilen)++;
155699141d43SSatish Balay 
155717ab2063SBarry Smith         }
155817ab2063SBarry Smith       }
155917ab2063SBarry Smith     }
156002834360SBarry Smith     /* Free work space */
156102834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1562606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1563606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
156402834360SBarry Smith   }
15656d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15666d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
156717ab2063SBarry Smith 
156817ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1569416022c9SBarry Smith   *B = C;
15703a40ed3dSBarry Smith   PetscFunctionReturn(0);
157117ab2063SBarry Smith }
157217ab2063SBarry Smith 
1573a871dcd8SBarry Smith /*
1574a871dcd8SBarry Smith */
15754a2ae208SSatish Balay #undef __FUNCT__
15764a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
15775ef9f2a5SBarry Smith int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatILUInfo *info)
1578a871dcd8SBarry Smith {
157963b91edcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
158008480c60SBarry Smith   int        ierr;
158163b91edcSBarry Smith   Mat        outA;
1582b8a78c4aSBarry Smith   PetscTruth row_identity,col_identity;
158363b91edcSBarry Smith 
15843a40ed3dSBarry Smith   PetscFunctionBegin;
158529bbc08cSBarry Smith   if (info && info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1586b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1587b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1588b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
158929bbc08cSBarry Smith     SETERRQ(1,"Row and column permutations must be identity for in-place ILU");
1590b8a78c4aSBarry Smith   }
1591a871dcd8SBarry Smith 
159263b91edcSBarry Smith   outA          = inA;
159363b91edcSBarry Smith   inA->factor   = FACTOR_LU;
159463b91edcSBarry Smith   a->row        = row;
159563b91edcSBarry Smith   a->col        = col;
1596c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1597c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
159863b91edcSBarry Smith 
159936db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1600b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
16014c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
1602b0a32e0cSBarry Smith   PetscLogObjectParent(inA,a->icol);
1603f0ec6fceSSatish Balay 
160494a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1605b0a32e0cSBarry Smith      ierr = PetscMalloc((inA->m+1)*sizeof(Scalar),&a->solve_work);CHKERRQ(ierr);
160694a9d846SBarry Smith   }
160763b91edcSBarry Smith 
160808480c60SBarry Smith   if (!a->diag) {
1609f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
161063b91edcSBarry Smith   }
161163b91edcSBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA);CHKERRQ(ierr);
16123a40ed3dSBarry Smith   PetscFunctionReturn(0);
1613a871dcd8SBarry Smith }
1614a871dcd8SBarry Smith 
1615d9eff348SSatish Balay #include "petscblaslapack.h"
16164a2ae208SSatish Balay #undef __FUNCT__
16174a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
16188f6be9afSLois Curfman McInnes int MatScale_SeqAIJ(Scalar *alpha,Mat inA)
1619f0b747eeSBarry Smith {
1620f0b747eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
1621f0b747eeSBarry Smith   int        one = 1;
16223a40ed3dSBarry Smith 
16233a40ed3dSBarry Smith   PetscFunctionBegin;
1624f0b747eeSBarry Smith   BLscal_(&a->nz,alpha,a->a,&one);
1625b0a32e0cSBarry Smith   PetscLogFlops(a->nz);
16263a40ed3dSBarry Smith   PetscFunctionReturn(0);
1627f0b747eeSBarry Smith }
1628f0b747eeSBarry Smith 
16294a2ae208SSatish Balay #undef __FUNCT__
16304a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
16317b2a1423SBarry Smith int MatGetSubMatrices_SeqAIJ(Mat A,int n,IS *irow,IS *icol,MatReuse scall,Mat **B)
1632cddf8d76SBarry Smith {
1633cddf8d76SBarry Smith   int ierr,i;
1634cddf8d76SBarry Smith 
16353a40ed3dSBarry Smith   PetscFunctionBegin;
1636cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1637b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1638cddf8d76SBarry Smith   }
1639cddf8d76SBarry Smith 
1640cddf8d76SBarry Smith   for (i=0; i<n; i++) {
16416a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1642cddf8d76SBarry Smith   }
16433a40ed3dSBarry Smith   PetscFunctionReturn(0);
1644cddf8d76SBarry Smith }
1645cddf8d76SBarry Smith 
16464a2ae208SSatish Balay #undef __FUNCT__
16474a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_SeqAIJ"
16488f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A,int *bs)
16495a838052SSatish Balay {
1650f830108cSBarry Smith   PetscFunctionBegin;
16515a838052SSatish Balay   *bs = 1;
16523a40ed3dSBarry Smith   PetscFunctionReturn(0);
16535a838052SSatish Balay }
16545a838052SSatish Balay 
16554a2ae208SSatish Balay #undef __FUNCT__
16564a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
16578f6be9afSLois Curfman McInnes int MatIncreaseOverlap_SeqAIJ(Mat A,int is_max,IS *is,int ov)
16584dcbc457SBarry Smith {
1659e4d965acSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
166006763907SSatish Balay   int        shift,row,i,j,k,l,m,n,*idx,ierr,*nidx,isz,val;
16618a047759SSatish Balay   int        start,end,*ai,*aj;
1662f1af5d2fSBarry Smith   PetscBT    table;
1663bbd702dbSSatish Balay 
16643a40ed3dSBarry Smith   PetscFunctionBegin;
16658a047759SSatish Balay   shift = a->indexshift;
1666273d9f13SBarry Smith   m     = A->m;
1667e4d965acSSatish Balay   ai    = a->i;
16688a047759SSatish Balay   aj    = a->j+shift;
16698a047759SSatish Balay 
167029bbc08cSBarry Smith   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal overlap value used");
167106763907SSatish Balay 
1672b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&nidx);CHKERRQ(ierr);
16736831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
167406763907SSatish Balay 
1675e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1676b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1677e4d965acSSatish Balay     isz  = 0;
16786831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1679e4d965acSSatish Balay 
1680e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
16814dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1682b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1683e4d965acSSatish Balay 
1684dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1685e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1686f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
16874dcbc457SBarry Smith     }
168806763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
168906763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1690e4d965acSSatish Balay 
169104a348a9SBarry Smith     k = 0;
169204a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
169304a348a9SBarry Smith       n = isz;
169406763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1695e4d965acSSatish Balay         row   = nidx[k];
1696e4d965acSSatish Balay         start = ai[row];
1697e4d965acSSatish Balay         end   = ai[row+1];
169804a348a9SBarry Smith         for (l = start; l<end ; l++){
16998a047759SSatish Balay           val = aj[l] + shift;
1700f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1701e4d965acSSatish Balay         }
1702e4d965acSSatish Balay       }
1703e4d965acSSatish Balay     }
1704029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1705e4d965acSSatish Balay   }
17066831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1707606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
17083a40ed3dSBarry Smith   PetscFunctionReturn(0);
17094dcbc457SBarry Smith }
171017ab2063SBarry Smith 
17110513a670SBarry Smith /* -------------------------------------------------------------- */
17124a2ae208SSatish Balay #undef __FUNCT__
17134a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
17140513a670SBarry Smith int MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
17150513a670SBarry Smith {
17160513a670SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
17170513a670SBarry Smith   Scalar     *vwork;
1718273d9f13SBarry Smith   int        i,ierr,nz,m = A->m,n = A->n,*cwork;
17190513a670SBarry Smith   int        *row,*col,*cnew,j,*lens;
172056cd22aeSBarry Smith   IS         icolp,irowp;
17210513a670SBarry Smith 
17223a40ed3dSBarry Smith   PetscFunctionBegin;
17234c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
172456cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
17254c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
172656cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
17270513a670SBarry Smith 
17280513a670SBarry Smith   /* determine lengths of permuted rows */
1729b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&lens);CHKERRQ(ierr);
17300513a670SBarry Smith   for (i=0; i<m; i++) {
17310513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
17320513a670SBarry Smith   }
17330513a670SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr);
1734606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
17350513a670SBarry Smith 
1736b0a32e0cSBarry Smith   ierr = PetscMalloc(n*sizeof(int),&cnew);CHKERRQ(ierr);
17370513a670SBarry Smith   for (i=0; i<m; i++) {
17380513a670SBarry Smith     ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
17390513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
17400513a670SBarry Smith     ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
17410513a670SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
17420513a670SBarry Smith   }
1743606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
17440513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17450513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
174656cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
174756cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
174856cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
174956cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
17503a40ed3dSBarry Smith   PetscFunctionReturn(0);
17510513a670SBarry Smith }
17520513a670SBarry Smith 
17534a2ae208SSatish Balay #undef __FUNCT__
17544a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ"
1755682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A)
1756682d7d0cSBarry Smith {
1757c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1758682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1759d132466eSBarry Smith   int               ierr;
1760682d7d0cSBarry Smith 
17613a40ed3dSBarry Smith   PetscFunctionBegin;
1762c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1763d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1764d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1765d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
1766d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_no_inode: Do not use inodes\n");CHKERRQ(ierr);
1767d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n");CHKERRQ(ierr);
1768aa482453SBarry Smith #if defined(PETSC_HAVE_ESSL)
1769d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_essl: Use IBM sparse LU factorization and solve.\n");CHKERRQ(ierr);
1770682d7d0cSBarry Smith #endif
1771cd5578b5SBarry Smith #if defined(PETSC_HAVE_LUSOL)
1772cd5578b5SBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_lusol: Use the Stanford LUSOL sparse factorization and solve.\n");CHKERRQ(ierr);
1773cd5578b5SBarry Smith #endif
177466826eb6SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX)
1775ca44d042SBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_matlab: Use Matlab engine sparse LU factorization and solve.\n");CHKERRQ(ierr);
1776ca44d042SBarry Smith #endif
17773a40ed3dSBarry Smith   PetscFunctionReturn(0);
1778682d7d0cSBarry Smith }
1779ca44d042SBarry Smith EXTERN int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg);
1780ca44d042SBarry Smith EXTERN int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
1781ca44d042SBarry Smith EXTERN int MatILUDTFactor_SeqAIJ(Mat,MatILUInfo*,IS,IS,Mat*);
17824a2ae208SSatish Balay #undef __FUNCT__
17834a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1784cb5b572fSBarry Smith int MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1785cb5b572fSBarry Smith {
1786be6bf707SBarry Smith   int        ierr;
1787273d9f13SBarry Smith   PetscTruth flg;
1788cb5b572fSBarry Smith 
1789cb5b572fSBarry Smith   PetscFunctionBegin;
1790273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flg);CHKERRQ(ierr);
1791273d9f13SBarry Smith   if (str == SAME_NONZERO_PATTERN && flg) {
1792be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1793be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1794be6bf707SBarry Smith 
1795273d9f13SBarry Smith     if (a->i[A->m]+a->indexshift != b->i[B->m]+a->indexshift) {
179629bbc08cSBarry Smith       SETERRQ(1,"Number of nonzeros in two matrices are different");
1797cb5b572fSBarry Smith     }
1798273d9f13SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->m]+a->indexshift)*sizeof(Scalar));CHKERRQ(ierr);
1799cb5b572fSBarry Smith   } else {
1800cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1801cb5b572fSBarry Smith   }
1802cb5b572fSBarry Smith   PetscFunctionReturn(0);
1803cb5b572fSBarry Smith }
1804cb5b572fSBarry Smith 
18054a2ae208SSatish Balay #undef __FUNCT__
18064a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
1807273d9f13SBarry Smith int MatSetUpPreallocation_SeqAIJ(Mat A)
1808273d9f13SBarry Smith {
1809273d9f13SBarry Smith   int        ierr;
1810273d9f13SBarry Smith 
1811273d9f13SBarry Smith   PetscFunctionBegin;
1812273d9f13SBarry Smith   ierr =  MatSeqAIJSetPreallocation(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
1813273d9f13SBarry Smith   PetscFunctionReturn(0);
1814273d9f13SBarry Smith }
1815273d9f13SBarry Smith 
18164a2ae208SSatish Balay #undef __FUNCT__
18174a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
18186c0721eeSBarry Smith int MatGetArray_SeqAIJ(Mat A,Scalar **array)
18196c0721eeSBarry Smith {
18206c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
18216c0721eeSBarry Smith   PetscFunctionBegin;
18226c0721eeSBarry Smith   *array = a->a;
18236c0721eeSBarry Smith   PetscFunctionReturn(0);
18246c0721eeSBarry Smith }
18256c0721eeSBarry Smith 
18264a2ae208SSatish Balay #undef __FUNCT__
18274a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
18286c0721eeSBarry Smith int MatRestoreArray_SeqAIJ(Mat A,Scalar **array)
18296c0721eeSBarry Smith {
18306c0721eeSBarry Smith   PetscFunctionBegin;
18316c0721eeSBarry Smith   PetscFunctionReturn(0);
18326c0721eeSBarry Smith }
1833273d9f13SBarry Smith 
1834ee4f033dSBarry Smith #undef __FUNCT__
1835ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
1836ee4f033dSBarry Smith int MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
1837ee4f033dSBarry Smith {
1838ee4f033dSBarry Smith   int           (*f)(void *,Vec,Vec,void*) = (int (*)(void *,Vec,Vec,void *))coloring->f;
1839ee4f033dSBarry Smith   int           k,ierr,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
1840ee4f033dSBarry Smith   Scalar        dx,mone = -1.0,*y,*xx,*w3_array;
1841ee4f033dSBarry Smith   Scalar        *vscale_array;
1842ee4f033dSBarry Smith   PetscReal     epsilon = coloring->error_rel,umin = coloring->umin;
1843ee4f033dSBarry Smith   Vec           w1,w2,w3;
1844ee4f033dSBarry Smith   void          *fctx = coloring->fctx;
1845ee4f033dSBarry Smith   PetscTruth    flg;
1846ee4f033dSBarry Smith 
1847ee4f033dSBarry Smith   PetscFunctionBegin;
1848ee4f033dSBarry Smith   if (!coloring->w1) {
1849ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
1850ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w1);
1851ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
1852ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w2);
1853ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
1854ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w3);
1855ee4f033dSBarry Smith   }
1856ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
1857ee4f033dSBarry Smith 
1858ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
1859ee4f033dSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
1860ee4f033dSBarry Smith   if (flg) {
1861ee4f033dSBarry Smith     PetscLogInfo(coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n");
1862ee4f033dSBarry Smith   } else {
1863ee4f033dSBarry Smith     ierr = MatZeroEntries(J);CHKERRQ(ierr);
1864ee4f033dSBarry Smith   }
1865ee4f033dSBarry Smith 
1866ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
1867ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
1868ee4f033dSBarry Smith 
1869ee4f033dSBarry Smith   /*
1870ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
1871ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
1872ee4f033dSBarry Smith   */
1873ee4f033dSBarry Smith   if (coloring->F) {
1874ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
1875ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
1876ee4f033dSBarry Smith     if (m1 != m2) {
1877ee4f033dSBarry Smith       coloring->F = 0;
1878ee4f033dSBarry Smith     }
1879ee4f033dSBarry Smith   }
1880ee4f033dSBarry Smith 
1881ee4f033dSBarry Smith   if (coloring->F) {
1882ee4f033dSBarry Smith     w1          = coloring->F;
1883ee4f033dSBarry Smith     coloring->F = 0;
1884ee4f033dSBarry Smith   } else {
1885ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
1886ee4f033dSBarry Smith   }
1887ee4f033dSBarry Smith 
1888ee4f033dSBarry Smith   /*
1889ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
1890ee4f033dSBarry Smith   */
1891ee4f033dSBarry Smith   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
1892ee4f033dSBarry Smith   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
1893ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
1894ee4f033dSBarry Smith     /*
1895ee4f033dSBarry Smith        Loop over each column associated with color adding the
1896ee4f033dSBarry Smith        perturbation to the vector w3.
1897ee4f033dSBarry Smith     */
1898ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
1899ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
1900ee4f033dSBarry Smith       dx  = xx[col];
1901ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
1902ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
1903ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
1904ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
1905ee4f033dSBarry Smith #else
1906ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
1907ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
1908ee4f033dSBarry Smith #endif
1909ee4f033dSBarry Smith       dx                *= epsilon;
1910ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
1911ee4f033dSBarry Smith     }
1912ee4f033dSBarry Smith   }
1913ee4f033dSBarry Smith   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
1914ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1915ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1916ee4f033dSBarry Smith 
1917ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
1918ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
1919ee4f033dSBarry Smith 
1920ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
1921ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
1922ee4f033dSBarry Smith 
1923ee4f033dSBarry Smith   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
1924ee4f033dSBarry Smith   /*
1925ee4f033dSBarry Smith       Loop over each color
1926ee4f033dSBarry Smith   */
1927ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
1928ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
1929ee4f033dSBarry Smith     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
1930ee4f033dSBarry Smith     /*
1931ee4f033dSBarry Smith        Loop over each column associated with color adding the
1932ee4f033dSBarry Smith        perturbation to the vector w3.
1933ee4f033dSBarry Smith     */
1934ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
1935ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
1936ee4f033dSBarry Smith       dx  = xx[col];
1937ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
1938ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
1939ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
1940ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
1941ee4f033dSBarry Smith #else
1942ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
1943ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
1944ee4f033dSBarry Smith #endif
1945ee4f033dSBarry Smith       dx            *= epsilon;
1946ee4f033dSBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(1,"Computed 0 differencing parameter");
1947ee4f033dSBarry Smith       w3_array[col] += dx;
1948ee4f033dSBarry Smith     }
1949ee4f033dSBarry Smith     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
1950ee4f033dSBarry Smith 
1951ee4f033dSBarry Smith     /*
1952ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
1953ee4f033dSBarry Smith     */
1954ee4f033dSBarry Smith 
1955ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
1956ee4f033dSBarry Smith     ierr = VecAXPY(&mone,w1,w2);CHKERRQ(ierr);
1957ee4f033dSBarry Smith 
1958ee4f033dSBarry Smith     /*
1959ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
1960ee4f033dSBarry Smith     */
1961ee4f033dSBarry Smith     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
1962ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
1963ee4f033dSBarry Smith       row    = coloring->rows[k][l];
1964ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
1965ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
1966ee4f033dSBarry Smith       srow   = row + start;
1967ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
1968ee4f033dSBarry Smith     }
1969ee4f033dSBarry Smith     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
1970ee4f033dSBarry Smith   }
1971ee4f033dSBarry Smith   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
1972ee4f033dSBarry Smith   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
1973ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1974ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1975ee4f033dSBarry Smith   PetscFunctionReturn(0);
1976ee4f033dSBarry Smith }
1977ee4f033dSBarry Smith 
1978682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
19790a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
1980cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
1981cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
1982cb5b572fSBarry Smith        MatMult_SeqAIJ,
1983cb5b572fSBarry Smith        MatMultAdd_SeqAIJ,
19847c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
19857c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
1986cb5b572fSBarry Smith        MatSolve_SeqAIJ,
1987cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
19887c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
19897c922b88SBarry Smith        MatSolveTransposeAdd_SeqAIJ,
1990cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
1991cb5b572fSBarry Smith        0,
199217ab2063SBarry Smith        MatRelax_SeqAIJ,
199317ab2063SBarry Smith        MatTranspose_SeqAIJ,
1994cb5b572fSBarry Smith        MatGetInfo_SeqAIJ,
1995cb5b572fSBarry Smith        MatEqual_SeqAIJ,
1996cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
1997cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
1998cb5b572fSBarry Smith        MatNorm_SeqAIJ,
1999cb5b572fSBarry Smith        0,
2000cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
200117ab2063SBarry Smith        MatCompress_SeqAIJ,
2002cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2003cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2004cb5b572fSBarry Smith        MatZeroRows_SeqAIJ,
2005cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2006cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2007cb5b572fSBarry Smith        0,
2008cb5b572fSBarry Smith        0,
2009273d9f13SBarry Smith        MatSetUpPreallocation_SeqAIJ,
2010273d9f13SBarry Smith        0,
2011cb5b572fSBarry Smith        MatGetOwnershipRange_SeqAIJ,
2012cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2013cb5b572fSBarry Smith        0,
20146c0721eeSBarry Smith        MatGetArray_SeqAIJ,
20156c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2016be6bf707SBarry Smith        MatDuplicate_SeqAIJ,
2017cb5b572fSBarry Smith        0,
2018cb5b572fSBarry Smith        0,
2019cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2020cb5b572fSBarry Smith        0,
2021cb5b572fSBarry Smith        0,
2022cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2023cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2024cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2025cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2026f0b747eeSBarry Smith        MatPrintHelp_SeqAIJ,
2027cb5b572fSBarry Smith        MatScale_SeqAIJ,
2028cb5b572fSBarry Smith        0,
2029cb5b572fSBarry Smith        0,
20306945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
20316945ee14SBarry Smith        MatGetBlockSize_SeqAIJ,
20323b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
20333b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
20343b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2035a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2036a93ec695SBarry Smith        MatFDColoringCreate_SeqAIJ,
2037b9617806SBarry Smith        0,
20380513a670SBarry Smith        0,
2039cda55fadSBarry Smith        MatPermute_SeqAIJ,
2040cda55fadSBarry Smith        0,
2041cda55fadSBarry Smith        0,
2042b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2043b9b97703SBarry Smith        MatView_SeqAIJ,
2044ee4f033dSBarry Smith        MatGetMaps_Petsc,
2045ee4f033dSBarry Smith        0,
2046ee4f033dSBarry Smith        0,
2047ee4f033dSBarry Smith        0,
2048ee4f033dSBarry Smith        0,
2049ee4f033dSBarry Smith        0,
2050ee4f033dSBarry Smith        0,
2051ee4f033dSBarry Smith        0,
2052ee4f033dSBarry Smith        0,
2053ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2054ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2055ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
2056ee4f033dSBarry Smith        MatFDColoringApply_SeqAIJ};
205717ab2063SBarry Smith 
2058ca44d042SBarry Smith EXTERN int MatUseSuperLU_SeqAIJ(Mat);
2059ca44d042SBarry Smith EXTERN int MatUseEssl_SeqAIJ(Mat);
2060cd5578b5SBarry Smith EXTERN int MatUseLUSOL_SeqAIJ(Mat);
2061ca44d042SBarry Smith EXTERN int MatUseMatlab_SeqAIJ(Mat);
2062ca44d042SBarry Smith EXTERN int MatUseDXML_SeqAIJ(Mat);
206317ab2063SBarry Smith 
2064fb2e594dSBarry Smith EXTERN_C_BEGIN
20654a2ae208SSatish Balay #undef __FUNCT__
20664a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
206715091d37SBarry Smith 
2068bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,int *indices)
2069bef8e0ddSBarry Smith {
2070bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2071bef8e0ddSBarry Smith   int        i,nz,n;
2072bef8e0ddSBarry Smith 
2073bef8e0ddSBarry Smith   PetscFunctionBegin;
2074bef8e0ddSBarry Smith 
2075bef8e0ddSBarry Smith   nz = aij->maxnz;
2076273d9f13SBarry Smith   n  = mat->n;
2077bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2078bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2079bef8e0ddSBarry Smith   }
2080bef8e0ddSBarry Smith   aij->nz = nz;
2081bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2082bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2083bef8e0ddSBarry Smith   }
2084bef8e0ddSBarry Smith 
2085bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2086bef8e0ddSBarry Smith }
2087fb2e594dSBarry Smith EXTERN_C_END
2088bef8e0ddSBarry Smith 
20894a2ae208SSatish Balay #undef __FUNCT__
20904a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2091bef8e0ddSBarry Smith /*@
2092bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2093bef8e0ddSBarry Smith        in the matrix.
2094bef8e0ddSBarry Smith 
2095bef8e0ddSBarry Smith   Input Parameters:
2096bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2097bef8e0ddSBarry Smith -  indices - the column indices
2098bef8e0ddSBarry Smith 
209915091d37SBarry Smith   Level: advanced
210015091d37SBarry Smith 
2101bef8e0ddSBarry Smith   Notes:
2102bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2103bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2104bef8e0ddSBarry Smith   of the MatSetValues() operation.
2105bef8e0ddSBarry Smith 
2106bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2107bef8e0ddSBarry Smith   MatCreateSeqAIJ().
2108bef8e0ddSBarry Smith 
2109bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2110bef8e0ddSBarry Smith 
2111b9617806SBarry Smith     The indices should start with zero, not one.
2112b9617806SBarry Smith 
2113bef8e0ddSBarry Smith @*/
2114bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices(Mat mat,int *indices)
2115bef8e0ddSBarry Smith {
2116bef8e0ddSBarry Smith   int ierr,(*f)(Mat,int *);
2117bef8e0ddSBarry Smith 
2118bef8e0ddSBarry Smith   PetscFunctionBegin;
2119bef8e0ddSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2120b9617806SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)())&f);CHKERRQ(ierr);
2121bef8e0ddSBarry Smith   if (f) {
2122bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2123bef8e0ddSBarry Smith   } else {
212429bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to set column indices");
2125bef8e0ddSBarry Smith   }
2126bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2127bef8e0ddSBarry Smith }
2128bef8e0ddSBarry Smith 
2129be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2130be6bf707SBarry Smith 
2131fb2e594dSBarry Smith EXTERN_C_BEGIN
21324a2ae208SSatish Balay #undef __FUNCT__
21334a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2134be6bf707SBarry Smith int MatStoreValues_SeqAIJ(Mat mat)
2135be6bf707SBarry Smith {
2136be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2137273d9f13SBarry Smith   int        nz = aij->i[mat->m]+aij->indexshift,ierr;
2138be6bf707SBarry Smith 
2139be6bf707SBarry Smith   PetscFunctionBegin;
2140be6bf707SBarry Smith   if (aij->nonew != 1) {
214129bbc08cSBarry Smith     SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2142be6bf707SBarry Smith   }
2143be6bf707SBarry Smith 
2144be6bf707SBarry Smith   /* allocate space for values if not already there */
2145be6bf707SBarry Smith   if (!aij->saved_values) {
214666826eb6SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(Scalar),&aij->saved_values);CHKERRQ(ierr);
2147be6bf707SBarry Smith   }
2148be6bf707SBarry Smith 
2149be6bf707SBarry Smith   /* copy values over */
2150549d3d68SSatish Balay   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(Scalar));CHKERRQ(ierr);
2151be6bf707SBarry Smith   PetscFunctionReturn(0);
2152be6bf707SBarry Smith }
2153fb2e594dSBarry Smith EXTERN_C_END
2154be6bf707SBarry Smith 
21554a2ae208SSatish Balay #undef __FUNCT__
2156b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2157be6bf707SBarry Smith /*@
2158be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2159be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2160be6bf707SBarry Smith        nonlinear portion.
2161be6bf707SBarry Smith 
2162be6bf707SBarry Smith    Collect on Mat
2163be6bf707SBarry Smith 
2164be6bf707SBarry Smith   Input Parameters:
2165be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2166be6bf707SBarry Smith 
216715091d37SBarry Smith   Level: advanced
216815091d37SBarry Smith 
2169be6bf707SBarry Smith   Common Usage, with SNESSolve():
2170be6bf707SBarry Smith $    Create Jacobian matrix
2171be6bf707SBarry Smith $    Set linear terms into matrix
2172be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2173be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2174be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2175be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2176be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2177be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2178be6bf707SBarry Smith $    In your Jacobian routine
2179be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2180be6bf707SBarry Smith $      Set nonlinear terms in matrix
2181be6bf707SBarry Smith 
2182be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2183be6bf707SBarry Smith $    // build linear portion of Jacobian
2184be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2185be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2186be6bf707SBarry Smith $    loop over nonlinear iterations
2187be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2188be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2189be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2190be6bf707SBarry Smith $       Solve linear system with Jacobian
2191be6bf707SBarry Smith $    endloop
2192be6bf707SBarry Smith 
2193be6bf707SBarry Smith   Notes:
2194be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2195be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2196be6bf707SBarry Smith     calling this routine.
2197be6bf707SBarry Smith 
2198be6bf707SBarry Smith .seealso: MatRetrieveValues()
2199be6bf707SBarry Smith 
2200be6bf707SBarry Smith @*/
2201be6bf707SBarry Smith int MatStoreValues(Mat mat)
2202be6bf707SBarry Smith {
2203be6bf707SBarry Smith   int ierr,(*f)(Mat);
2204be6bf707SBarry Smith 
2205be6bf707SBarry Smith   PetscFunctionBegin;
2206be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
220729bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
220829bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2209be6bf707SBarry Smith 
2210b9617806SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)())&f);CHKERRQ(ierr);
2211be6bf707SBarry Smith   if (f) {
2212be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2213be6bf707SBarry Smith   } else {
221429bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to store values");
2215be6bf707SBarry Smith   }
2216be6bf707SBarry Smith   PetscFunctionReturn(0);
2217be6bf707SBarry Smith }
2218be6bf707SBarry Smith 
2219fb2e594dSBarry Smith EXTERN_C_BEGIN
22204a2ae208SSatish Balay #undef __FUNCT__
22214a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2222be6bf707SBarry Smith int MatRetrieveValues_SeqAIJ(Mat mat)
2223be6bf707SBarry Smith {
2224be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2225273d9f13SBarry Smith   int        nz = aij->i[mat->m]+aij->indexshift,ierr;
2226be6bf707SBarry Smith 
2227be6bf707SBarry Smith   PetscFunctionBegin;
2228be6bf707SBarry Smith   if (aij->nonew != 1) {
222929bbc08cSBarry Smith     SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2230be6bf707SBarry Smith   }
2231be6bf707SBarry Smith   if (!aij->saved_values) {
223229bbc08cSBarry Smith     SETERRQ(1,"Must call MatStoreValues(A);first");
2233be6bf707SBarry Smith   }
2234be6bf707SBarry Smith 
2235be6bf707SBarry Smith   /* copy values over */
2236549d3d68SSatish Balay   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(Scalar));CHKERRQ(ierr);
2237be6bf707SBarry Smith   PetscFunctionReturn(0);
2238be6bf707SBarry Smith }
2239fb2e594dSBarry Smith EXTERN_C_END
2240be6bf707SBarry Smith 
22414a2ae208SSatish Balay #undef __FUNCT__
22424a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2243be6bf707SBarry Smith /*@
2244be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2245be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2246be6bf707SBarry Smith        nonlinear portion.
2247be6bf707SBarry Smith 
2248be6bf707SBarry Smith    Collect on Mat
2249be6bf707SBarry Smith 
2250be6bf707SBarry Smith   Input Parameters:
2251be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2252be6bf707SBarry Smith 
225315091d37SBarry Smith   Level: advanced
225415091d37SBarry Smith 
2255be6bf707SBarry Smith .seealso: MatStoreValues()
2256be6bf707SBarry Smith 
2257be6bf707SBarry Smith @*/
2258be6bf707SBarry Smith int MatRetrieveValues(Mat mat)
2259be6bf707SBarry Smith {
2260be6bf707SBarry Smith   int ierr,(*f)(Mat);
2261be6bf707SBarry Smith 
2262be6bf707SBarry Smith   PetscFunctionBegin;
2263be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
226429bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
226529bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2266be6bf707SBarry Smith 
2267b9617806SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)())&f);CHKERRQ(ierr);
2268be6bf707SBarry Smith   if (f) {
2269be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2270be6bf707SBarry Smith   } else {
227129bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to retrieve values");
2272be6bf707SBarry Smith   }
2273be6bf707SBarry Smith   PetscFunctionReturn(0);
2274be6bf707SBarry Smith }
2275be6bf707SBarry Smith 
2276f83d6046SBarry Smith /*
2277f83d6046SBarry Smith    This allows SeqAIJ matrices to be passed to the matlab engine
2278f83d6046SBarry Smith */
227966826eb6SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX)
2280f83d6046SBarry Smith #include "engine.h"   /* Matlab include file */
2281f83d6046SBarry Smith #include "mex.h"      /* Matlab include file */
2282f83d6046SBarry Smith EXTERN_C_BEGIN
22834a2ae208SSatish Balay #undef __FUNCT__
22844a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEnginePut_SeqAIJ"
2285f83d6046SBarry Smith int MatMatlabEnginePut_SeqAIJ(PetscObject obj,void *engine)
2286f83d6046SBarry Smith {
228766826eb6SBarry Smith   int        ierr,i,*ai,*aj;
2288f83d6046SBarry Smith   Mat        B = (Mat)obj;
2289f83d6046SBarry Smith   Scalar     *array;
2290f83d6046SBarry Smith   mxArray    *mat;
2291d79a51d8SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)B->data;
2292d79a51d8SBarry Smith 
2293f83d6046SBarry Smith   PetscFunctionBegin;
229401820c62SBarry Smith   mat  = mxCreateSparse(B->n,B->m,aij->nz,mxREAL);
2295d79a51d8SBarry Smith   ierr = PetscMemcpy(mxGetPr(mat),aij->a,aij->nz*sizeof(Scalar));CHKERRQ(ierr);
2296d79a51d8SBarry Smith   /* Matlab stores by column, not row so we pass in the transpose of the matrix */
229766826eb6SBarry Smith   ierr = PetscMemcpy(mxGetIr(mat),aij->j,aij->nz*sizeof(int));CHKERRQ(ierr);
229866826eb6SBarry Smith   ierr = PetscMemcpy(mxGetJc(mat),aij->i,(B->m+1)*sizeof(int));CHKERRQ(ierr);
2299f83d6046SBarry Smith 
230001820c62SBarry Smith   /* Matlab indices start at 0 for sparse (what a surprise) */
230101820c62SBarry Smith   if (aij->indexshift) {
230201820c62SBarry Smith     for (i=0; i<B->m+1; i++) {
230301820c62SBarry Smith       ai[i]--;
2304d79a51d8SBarry Smith     }
2305d79a51d8SBarry Smith     for (i=0; i<aij->nz; i++) {
230601820c62SBarry Smith       aj[i]--;
2307d79a51d8SBarry Smith     }
2308d79a51d8SBarry Smith   }
2309ca44d042SBarry Smith   ierr = PetscObjectName(obj);CHKERRQ(ierr);
2310f83d6046SBarry Smith   mxSetName(mat,obj->name);
2311f83d6046SBarry Smith   engPutArray((Engine *)engine,mat);
2312f83d6046SBarry Smith   PetscFunctionReturn(0);
2313f83d6046SBarry Smith }
2314f83d6046SBarry Smith EXTERN_C_END
231566826eb6SBarry Smith 
231666826eb6SBarry Smith EXTERN_C_BEGIN
23174a2ae208SSatish Balay #undef __FUNCT__
23184a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEngineGet_SeqAIJ"
231966826eb6SBarry Smith int MatMatlabEngineGet_SeqAIJ(PetscObject obj,void *engine)
232066826eb6SBarry Smith {
232166826eb6SBarry Smith   int        ierr,ii;
232266826eb6SBarry Smith   Mat        mat = (Mat)obj;
232366826eb6SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
232466826eb6SBarry Smith   mxArray    *mmat;
232566826eb6SBarry Smith 
23266c0721eeSBarry Smith   PetscFunctionBegin;
232766826eb6SBarry Smith   ierr = PetscFree(aij->a);CHKERRQ(ierr);
232866826eb6SBarry Smith   aij->indexshift = 0;
232966826eb6SBarry Smith 
233066826eb6SBarry Smith   mmat = engGetArray((Engine *)engine,obj->name);
233166826eb6SBarry Smith 
2332a65776f3SBarry Smith   aij->nz           = (mxGetJc(mmat))[mat->m];
233366826eb6SBarry Smith   ierr              = PetscMalloc(aij->nz*(sizeof(int)+sizeof(Scalar))+(mat->m+1)*sizeof(int),&aij->a);CHKERRQ(ierr);
233466826eb6SBarry Smith   aij->j            = (int*)(aij->a + aij->nz);
233566826eb6SBarry Smith   aij->i            = aij->j + aij->nz;
233666826eb6SBarry Smith   aij->singlemalloc = PETSC_TRUE;
233766826eb6SBarry Smith   aij->freedata     = PETSC_TRUE;
233866826eb6SBarry Smith 
23396c0721eeSBarry Smith   ierr = PetscMemcpy(aij->a,mxGetPr(mmat),aij->nz*sizeof(Scalar));CHKERRQ(ierr);
234066826eb6SBarry Smith   /* Matlab stores by column, not row so we pass in the transpose of the matrix */
234166826eb6SBarry Smith   ierr = PetscMemcpy(aij->j,mxGetIr(mmat),aij->nz*sizeof(int));CHKERRQ(ierr);
234266826eb6SBarry Smith   ierr = PetscMemcpy(aij->i,mxGetJc(mmat),(mat->m+1)*sizeof(int));CHKERRQ(ierr);
234366826eb6SBarry Smith 
234466826eb6SBarry Smith   for (ii=0; ii<mat->m; ii++) {
234566826eb6SBarry Smith     aij->ilen[ii] = aij->imax[ii] = aij->i[ii+1] - aij->i[ii];
234666826eb6SBarry Smith   }
234766826eb6SBarry Smith 
234866826eb6SBarry Smith   ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
234966826eb6SBarry Smith   ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
235066826eb6SBarry Smith 
235166826eb6SBarry Smith   PetscFunctionReturn(0);
235266826eb6SBarry Smith }
235366826eb6SBarry Smith EXTERN_C_END
2354f83d6046SBarry Smith #endif
2355f83d6046SBarry Smith 
2356be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
2357cb5b572fSBarry Smith 
23584a2ae208SSatish Balay #undef __FUNCT__
23594a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
236017ab2063SBarry Smith /*@C
2361682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
23620d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
23636e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
236451c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
23652bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
236617ab2063SBarry Smith 
2367db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2368db81eaa0SLois Curfman McInnes 
236917ab2063SBarry Smith    Input Parameters:
2370db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
237117ab2063SBarry Smith .  m - number of rows
237217ab2063SBarry Smith .  n - number of columns
237317ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
237451c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
23752bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
237617ab2063SBarry Smith 
237717ab2063SBarry Smith    Output Parameter:
2378416022c9SBarry Smith .  A - the matrix
237917ab2063SBarry Smith 
2380b259b22eSLois Curfman McInnes    Notes:
238117ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
238217ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
23830002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
238444cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
238517ab2063SBarry Smith 
238617ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2387a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
23883d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
23896da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
239017ab2063SBarry Smith 
2391682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
23924fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2393682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
23946c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
23956c7ebb05SLois Curfman McInnes 
23966c7ebb05SLois Curfman McInnes    Options Database Keys:
2397db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
2398db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2399db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2400db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2401db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
240217ab2063SBarry Smith 
2403027ccd11SLois Curfman McInnes    Level: intermediate
2404027ccd11SLois Curfman McInnes 
240536db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
240636db0b34SBarry Smith 
240717ab2063SBarry Smith @*/
2408416022c9SBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,int *nnz,Mat *A)
240917ab2063SBarry Smith {
2410273d9f13SBarry Smith   int ierr;
24116945ee14SBarry Smith 
24123a40ed3dSBarry Smith   PetscFunctionBegin;
2413273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,m,n,A);CHKERRQ(ierr);
2414273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2415273d9f13SBarry Smith   ierr = MatSeqAIJSetPreallocation(*A,nz,nnz);CHKERRQ(ierr);
2416273d9f13SBarry Smith   PetscFunctionReturn(0);
2417273d9f13SBarry Smith }
2418273d9f13SBarry Smith 
24194a2ae208SSatish Balay #undef __FUNCT__
24204a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2421273d9f13SBarry Smith /*@C
2422273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2423273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2424273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2425273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2426273d9f13SBarry Smith 
2427273d9f13SBarry Smith    Collective on MPI_Comm
2428273d9f13SBarry Smith 
2429273d9f13SBarry Smith    Input Parameters:
2430273d9f13SBarry Smith +  comm - MPI communicator, set to PETSC_COMM_SELF
2431273d9f13SBarry Smith .  m - number of rows
2432273d9f13SBarry Smith .  n - number of columns
2433273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2434273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2435273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2436273d9f13SBarry Smith 
2437273d9f13SBarry Smith    Output Parameter:
2438273d9f13SBarry Smith .  A - the matrix
2439273d9f13SBarry Smith 
2440273d9f13SBarry Smith    Notes:
2441273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2442273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2443273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2444273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2445273d9f13SBarry Smith 
2446273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2447273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2448273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2449273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2450273d9f13SBarry Smith 
2451273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2452273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2453273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2454273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2455273d9f13SBarry Smith 
2456273d9f13SBarry Smith    Options Database Keys:
2457273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2458273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2459273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2460273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2461273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2462273d9f13SBarry Smith 
2463273d9f13SBarry Smith    Level: intermediate
2464273d9f13SBarry Smith 
2465273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
2466273d9f13SBarry Smith 
2467273d9f13SBarry Smith @*/
2468273d9f13SBarry Smith int MatSeqAIJSetPreallocation(Mat B,int nz,int *nnz)
2469273d9f13SBarry Smith {
2470273d9f13SBarry Smith   Mat_SeqAIJ *b;
2471273d9f13SBarry Smith   int        i,len,ierr;
2472273d9f13SBarry Smith   PetscTruth flg2;
2473273d9f13SBarry Smith 
2474273d9f13SBarry Smith   PetscFunctionBegin;
2475273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flg2);CHKERRQ(ierr);
2476273d9f13SBarry Smith   if (!flg2) PetscFunctionReturn(0);
2477d5d45c9bSBarry Smith 
2478435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2479435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2480b73539f3SBarry Smith   if (nnz) {
2481273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
248229bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
2483*3a7fca6bSBarry Smith       if (nnz[i] > B->n) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->n);
2484b73539f3SBarry Smith     }
2485b73539f3SBarry Smith   }
2486b73539f3SBarry Smith 
2487273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2488273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2489273d9f13SBarry Smith 
2490b0a32e0cSBarry Smith   ierr = PetscMalloc((B->m+1)*sizeof(int),&b->imax);CHKERRQ(ierr);
2491273d9f13SBarry Smith   if (!nnz) {
2492435da068SBarry Smith     if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2493273d9f13SBarry Smith     else if (nz <= 0)        nz = 1;
2494273d9f13SBarry Smith     for (i=0; i<B->m; i++) b->imax[i] = nz;
2495273d9f13SBarry Smith     nz = nz*B->m;
2496273d9f13SBarry Smith   } else {
2497273d9f13SBarry Smith     nz = 0;
2498273d9f13SBarry Smith     for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2499273d9f13SBarry Smith   }
2500273d9f13SBarry Smith 
2501273d9f13SBarry Smith   /* allocate the matrix space */
2502273d9f13SBarry Smith   len             = nz*(sizeof(int) + sizeof(Scalar)) + (B->m+1)*sizeof(int);
2503b0a32e0cSBarry Smith   ierr            = PetscMalloc(len,&b->a);CHKERRQ(ierr);
2504273d9f13SBarry Smith   b->j            = (int*)(b->a + nz);
2505273d9f13SBarry Smith   ierr            = PetscMemzero(b->j,nz*sizeof(int));CHKERRQ(ierr);
2506273d9f13SBarry Smith   b->i            = b->j + nz;
2507273d9f13SBarry Smith   b->singlemalloc = PETSC_TRUE;
2508273d9f13SBarry Smith   b->freedata     = PETSC_TRUE;
2509273d9f13SBarry Smith 
2510273d9f13SBarry Smith   b->i[0] = -b->indexshift;
2511273d9f13SBarry Smith   for (i=1; i<B->m+1; i++) {
2512273d9f13SBarry Smith     b->i[i] = b->i[i-1] + b->imax[i-1];
2513273d9f13SBarry Smith   }
2514273d9f13SBarry Smith 
2515273d9f13SBarry Smith   /* b->ilen will count nonzeros in each row so far. */
2516b0a32e0cSBarry Smith   ierr = PetscMalloc((B->m+1)*sizeof(int),&b->ilen);CHKERRQ(ierr);
2517b0a32e0cSBarry Smith   PetscLogObjectMemory(B,len+2*(B->m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2518273d9f13SBarry Smith   for (i=0; i<B->m; i++) { b->ilen[i] = 0;}
2519273d9f13SBarry Smith 
2520273d9f13SBarry Smith   b->nz                = 0;
2521273d9f13SBarry Smith   b->maxnz             = nz;
2522273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2523273d9f13SBarry Smith   PetscFunctionReturn(0);
2524273d9f13SBarry Smith }
2525273d9f13SBarry Smith 
2526273d9f13SBarry Smith EXTERN_C_BEGIN
25274a2ae208SSatish Balay #undef __FUNCT__
25284a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
2529273d9f13SBarry Smith int MatCreate_SeqAIJ(Mat B)
2530273d9f13SBarry Smith {
2531273d9f13SBarry Smith   Mat_SeqAIJ *b;
2532273d9f13SBarry Smith   int        ierr,size;
2533273d9f13SBarry Smith   PetscTruth flg;
2534273d9f13SBarry Smith 
2535273d9f13SBarry Smith   PetscFunctionBegin;
2536273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
2537273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
2538273d9f13SBarry Smith 
2539273d9f13SBarry Smith   B->m = B->M = PetscMax(B->m,B->M);
2540273d9f13SBarry Smith   B->n = B->N = PetscMax(B->n,B->N);
2541273d9f13SBarry Smith 
2542b0a32e0cSBarry Smith   ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr);
2543b0a32e0cSBarry Smith   B->data             = (void*)b;
2544549d3d68SSatish Balay   ierr = PetscMemzero(b,sizeof(Mat_SeqAIJ));CHKERRQ(ierr);
2545549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2546416022c9SBarry Smith   B->factor           = 0;
2547416022c9SBarry Smith   B->lupivotthreshold = 1.0;
254890f02eecSBarry Smith   B->mapping          = 0;
2549b0a32e0cSBarry Smith   ierr = PetscOptionsGetDouble(PETSC_NULL,"-mat_lu_pivotthreshold",&B->lupivotthreshold,PETSC_NULL);CHKERRQ(ierr);
2550b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-pc_ilu_preserve_row_sums",&b->ilu_preserve_row_sums);CHKERRQ(ierr);
2551416022c9SBarry Smith   b->row              = 0;
2552416022c9SBarry Smith   b->col              = 0;
255382bf6240SBarry Smith   b->icol             = 0;
2554416022c9SBarry Smith   b->indexshift       = 0;
2555b810aeb4SBarry Smith   b->reallocs         = 0;
2556b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_oneindex",&flg);CHKERRQ(ierr);
255769957df2SSatish Balay   if (flg) b->indexshift = -1;
255817ab2063SBarry Smith 
2559273d9f13SBarry Smith   ierr = MapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr);
2560273d9f13SBarry Smith   ierr = MapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr);
2561a5ae1ecdSBarry Smith 
2562f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
256336db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2564f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2565416022c9SBarry Smith   b->nonew             = 0;
2566416022c9SBarry Smith   b->diag              = 0;
2567416022c9SBarry Smith   b->solve_work        = 0;
256871bd300dSLois Curfman McInnes   b->spptr             = 0;
2569b65db4caSBarry Smith   b->inode.use         = PETSC_TRUE;
2570754ec7b1SSatish Balay   b->inode.node_count  = 0;
2571754ec7b1SSatish Balay   b->inode.size        = 0;
25726c7ebb05SLois Curfman McInnes   b->inode.limit       = 5;
25736c7ebb05SLois Curfman McInnes   b->inode.max_limit   = 5;
2574be6bf707SBarry Smith   b->saved_values      = 0;
2575d7f994e1SBarry Smith   b->idiag             = 0;
2576d7f994e1SBarry Smith   b->ssor              = 0;
2577f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
257817ab2063SBarry Smith 
257935d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
258035d8aa7fSBarry Smith 
2581aa482453SBarry Smith #if defined(PETSC_HAVE_SUPERLU)
2582b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_superlu",&flg);CHKERRQ(ierr);
258369957df2SSatish Balay   if (flg) { ierr = MatUseSuperLU_SeqAIJ(B);CHKERRQ(ierr); }
25844b14c69eSBarry Smith #endif
2585b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_essl",&flg);CHKERRQ(ierr);
258669957df2SSatish Balay   if (flg) { ierr = MatUseEssl_SeqAIJ(B);CHKERRQ(ierr); }
2587b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_lusol",&flg);CHKERRQ(ierr);
2588cd5578b5SBarry Smith   if (flg) { ierr = MatUseLUSOL_SeqAIJ(B);CHKERRQ(ierr); }
2589b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_matlab",&flg);CHKERRQ(ierr);
2590ca44d042SBarry Smith   if (flg) {ierr = MatUseMatlab_SeqAIJ(B);CHKERRQ(ierr);}
2591b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_dxml",&flg);CHKERRQ(ierr);
259269957df2SSatish Balay   if (flg) {
259329bbc08cSBarry Smith     if (!b->indexshift) SETERRQ(PETSC_ERR_LIB,"need -mat_aij_oneindex with -mat_aij_dxml");
2594416022c9SBarry Smith     ierr = MatUseDXML_SeqAIJ(B);CHKERRQ(ierr);
259517ab2063SBarry Smith   }
2596f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2597bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2598bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2599f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2600be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2601bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2602f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2603be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2604bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
260566826eb6SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX)
2606f83d6046SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatMatlabEnginePut_SeqAIJ",MatMatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
260766826eb6SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatMatlabEngineGet_SeqAIJ",MatMatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
2608f83d6046SBarry Smith #endif
26093a40ed3dSBarry Smith   PetscFunctionReturn(0);
261017ab2063SBarry Smith }
2611273d9f13SBarry Smith EXTERN_C_END
261217ab2063SBarry Smith 
26134a2ae208SSatish Balay #undef __FUNCT__
26144a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ"
2615be6bf707SBarry Smith int MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
261617ab2063SBarry Smith {
2617416022c9SBarry Smith   Mat        C;
2618416022c9SBarry Smith   Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data;
2619273d9f13SBarry Smith   int        i,len,m = A->m,shift = a->indexshift,ierr;
262017ab2063SBarry Smith 
26213a40ed3dSBarry Smith   PetscFunctionBegin;
26224043dd9cSLois Curfman McInnes   *B = 0;
2623273d9f13SBarry Smith   ierr = MatCreate(A->comm,A->m,A->n,A->m,A->n,&C);CHKERRQ(ierr);
2624273d9f13SBarry Smith   ierr = MatSetType(C,MATSEQAIJ);CHKERRQ(ierr);
2625273d9f13SBarry Smith   c    = (Mat_SeqAIJ*)C->data;
2626273d9f13SBarry Smith 
2627416022c9SBarry Smith   C->factor         = A->factor;
2628416022c9SBarry Smith   c->row            = 0;
2629416022c9SBarry Smith   c->col            = 0;
263082bf6240SBarry Smith   c->icol           = 0;
2631416022c9SBarry Smith   c->indexshift     = shift;
2632f1e2ffcdSBarry Smith   c->keepzeroedrows = a->keepzeroedrows;
2633c456f294SBarry Smith   C->assembled      = PETSC_TRUE;
263417ab2063SBarry Smith 
2635273d9f13SBarry Smith   C->M          = A->m;
2636273d9f13SBarry Smith   C->N          = A->n;
263717ab2063SBarry Smith 
2638b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&c->imax);CHKERRQ(ierr);
2639b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&c->ilen);CHKERRQ(ierr);
264017ab2063SBarry Smith   for (i=0; i<m; i++) {
2641416022c9SBarry Smith     c->imax[i] = a->imax[i];
2642416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
264317ab2063SBarry Smith   }
264417ab2063SBarry Smith 
264517ab2063SBarry Smith   /* allocate the matrix space */
2646f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
2647416022c9SBarry Smith   len   = (m+1)*sizeof(int)+(a->i[m])*(sizeof(Scalar)+sizeof(int));
2648b0a32e0cSBarry Smith   ierr  = PetscMalloc(len,&c->a);CHKERRQ(ierr);
2649416022c9SBarry Smith   c->j  = (int*)(c->a + a->i[m] + shift);
2650416022c9SBarry Smith   c->i  = c->j + a->i[m] + shift;
2651549d3d68SSatish Balay   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(int));CHKERRQ(ierr);
265217ab2063SBarry Smith   if (m > 0) {
2653549d3d68SSatish Balay     ierr = PetscMemcpy(c->j,a->j,(a->i[m]+shift)*sizeof(int));CHKERRQ(ierr);
2654be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2655549d3d68SSatish Balay       ierr = PetscMemcpy(c->a,a->a,(a->i[m]+shift)*sizeof(Scalar));CHKERRQ(ierr);
2656be6bf707SBarry Smith     } else {
2657549d3d68SSatish Balay       ierr = PetscMemzero(c->a,(a->i[m]+shift)*sizeof(Scalar));CHKERRQ(ierr);
265817ab2063SBarry Smith     }
265908480c60SBarry Smith   }
266017ab2063SBarry Smith 
2661b0a32e0cSBarry Smith   PetscLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2662416022c9SBarry Smith   c->sorted      = a->sorted;
2663416022c9SBarry Smith   c->roworiented = a->roworiented;
2664416022c9SBarry Smith   c->nonew       = a->nonew;
26657a743949SBarry Smith   c->ilu_preserve_row_sums = a->ilu_preserve_row_sums;
2666be6bf707SBarry Smith   c->saved_values = 0;
2667d7f994e1SBarry Smith   c->idiag        = 0;
2668d7f994e1SBarry Smith   c->ssor         = 0;
266936db0b34SBarry Smith   c->ignorezeroentries = a->ignorezeroentries;
267036db0b34SBarry Smith   c->freedata     = PETSC_TRUE;
267117ab2063SBarry Smith 
2672416022c9SBarry Smith   if (a->diag) {
2673b0a32e0cSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(int),&c->diag);CHKERRQ(ierr);
2674b0a32e0cSBarry Smith     PetscLogObjectMemory(C,(m+1)*sizeof(int));
267517ab2063SBarry Smith     for (i=0; i<m; i++) {
2676416022c9SBarry Smith       c->diag[i] = a->diag[i];
267717ab2063SBarry Smith     }
26783a40ed3dSBarry Smith   } else c->diag        = 0;
2679b65db4caSBarry Smith   c->inode.use          = a->inode.use;
26806c7ebb05SLois Curfman McInnes   c->inode.limit        = a->inode.limit;
26816c7ebb05SLois Curfman McInnes   c->inode.max_limit    = a->inode.max_limit;
2682754ec7b1SSatish Balay   if (a->inode.size){
2683b0a32e0cSBarry Smith     ierr                = PetscMalloc((m+1)*sizeof(int),&c->inode.size);CHKERRQ(ierr);
2684754ec7b1SSatish Balay     c->inode.node_count = a->inode.node_count;
2685549d3d68SSatish Balay     ierr                = PetscMemcpy(c->inode.size,a->inode.size,(m+1)*sizeof(int));CHKERRQ(ierr);
2686754ec7b1SSatish Balay   } else {
2687754ec7b1SSatish Balay     c->inode.size       = 0;
2688754ec7b1SSatish Balay     c->inode.node_count = 0;
2689754ec7b1SSatish Balay   }
2690416022c9SBarry Smith   c->nz                 = a->nz;
2691416022c9SBarry Smith   c->maxnz              = a->maxnz;
2692416022c9SBarry Smith   c->solve_work         = 0;
269376dd722bSSatish Balay   c->spptr              = 0;      /* Dangerous -I'm throwing away a->spptr */
2694273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2695754ec7b1SSatish Balay 
2696416022c9SBarry Smith   *B = C;
2697b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
26983a40ed3dSBarry Smith   PetscFunctionReturn(0);
269917ab2063SBarry Smith }
270017ab2063SBarry Smith 
2701273d9f13SBarry Smith EXTERN_C_BEGIN
27024a2ae208SSatish Balay #undef __FUNCT__
27034a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
2704b0a32e0cSBarry Smith int MatLoad_SeqAIJ(PetscViewer viewer,MatType type,Mat *A)
270517ab2063SBarry Smith {
2706416022c9SBarry Smith   Mat_SeqAIJ   *a;
2707416022c9SBarry Smith   Mat          B;
270817699dbbSLois Curfman McInnes   int          i,nz,ierr,fd,header[4],size,*rowlengths = 0,M,N,shift;
2709bcd2baecSBarry Smith   MPI_Comm     comm;
271017ab2063SBarry Smith 
27113a40ed3dSBarry Smith   PetscFunctionBegin;
2712e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
2713d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
271429bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
2715b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
27160752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
271729bbc08cSBarry Smith   if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
271817ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
271917ab2063SBarry Smith 
2720d64ed03dSBarry Smith   if (nz < 0) {
272129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
2722d64ed03dSBarry Smith   }
2723d64ed03dSBarry Smith 
272417ab2063SBarry Smith   /* read in row lengths */
2725b0a32e0cSBarry Smith   ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr);
27260752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
272717ab2063SBarry Smith 
272817ab2063SBarry Smith   /* create our matrix */
2729416022c9SBarry Smith   ierr = MatCreateSeqAIJ(comm,M,N,0,rowlengths,A);CHKERRQ(ierr);
2730416022c9SBarry Smith   B = *A;
2731416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
2732416022c9SBarry Smith   shift = a->indexshift;
273317ab2063SBarry Smith 
273417ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
27350752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
273617ab2063SBarry Smith   if (shift) {
273717ab2063SBarry Smith     for (i=0; i<nz; i++) {
2738416022c9SBarry Smith       a->j[i] += 1;
273917ab2063SBarry Smith     }
274017ab2063SBarry Smith   }
274117ab2063SBarry Smith 
274217ab2063SBarry Smith   /* read in nonzero values */
27430752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
274417ab2063SBarry Smith 
274517ab2063SBarry Smith   /* set matrix "i" values */
2746416022c9SBarry Smith   a->i[0] = -shift;
274717ab2063SBarry Smith   for (i=1; i<= M; i++) {
2748416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2749416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
275017ab2063SBarry Smith   }
2751606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
275217ab2063SBarry Smith 
27536d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27546d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27553a40ed3dSBarry Smith   PetscFunctionReturn(0);
275617ab2063SBarry Smith }
2757273d9f13SBarry Smith EXTERN_C_END
275817ab2063SBarry Smith 
27594a2ae208SSatish Balay #undef __FUNCT__
2760b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
27618f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
27627264ac53SSatish Balay {
27637264ac53SSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
27640f5bd95cSBarry Smith   int        ierr;
2765273d9f13SBarry Smith   PetscTruth flag;
27667264ac53SSatish Balay 
27673a40ed3dSBarry Smith   PetscFunctionBegin;
2768273d9f13SBarry Smith   ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flag);CHKERRQ(ierr);
2769273d9f13SBarry Smith   if (!flag) SETERRQ(PETSC_ERR_ARG_INCOMP,"Matrices must be same type");
27707264ac53SSatish Balay 
27717264ac53SSatish Balay   /* If the  matrix dimensions are not equal,or no of nonzeros or shift */
2772273d9f13SBarry Smith   if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)|| (a->indexshift != b->indexshift)) {
2773ca44d042SBarry Smith     *flg = PETSC_FALSE;
2774ca44d042SBarry Smith     PetscFunctionReturn(0);
2775bcd2baecSBarry Smith   }
27767264ac53SSatish Balay 
27777264ac53SSatish Balay   /* if the a->i are the same */
2778273d9f13SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(int),flg);CHKERRQ(ierr);
27790f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
27807264ac53SSatish Balay 
27817264ac53SSatish Balay   /* if a->j are the same */
27820f5bd95cSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(int),flg);CHKERRQ(ierr);
27830f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
2784bcd2baecSBarry Smith 
2785bcd2baecSBarry Smith   /* if a->a are the same */
27860f5bd95cSBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(Scalar),flg);CHKERRQ(ierr);
27870f5bd95cSBarry Smith 
27883a40ed3dSBarry Smith   PetscFunctionReturn(0);
27897264ac53SSatish Balay 
27907264ac53SSatish Balay }
279136db0b34SBarry Smith 
27924a2ae208SSatish Balay #undef __FUNCT__
27934a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
279436db0b34SBarry Smith /*@C
279536db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
279636db0b34SBarry Smith               provided by the user.
279736db0b34SBarry Smith 
279836db0b34SBarry Smith       Coolective on MPI_Comm
279936db0b34SBarry Smith 
280036db0b34SBarry Smith    Input Parameters:
280136db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
280236db0b34SBarry Smith .   m - number of rows
280336db0b34SBarry Smith .   n - number of columns
280436db0b34SBarry Smith .   i - row indices
280536db0b34SBarry Smith .   j - column indices
280636db0b34SBarry Smith -   a - matrix values
280736db0b34SBarry Smith 
280836db0b34SBarry Smith    Output Parameter:
280936db0b34SBarry Smith .   mat - the matrix
281036db0b34SBarry Smith 
281136db0b34SBarry Smith    Level: intermediate
281236db0b34SBarry Smith 
281336db0b34SBarry Smith    Notes:
28140551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
281536db0b34SBarry Smith     once the matrix is destroyed
281636db0b34SBarry Smith 
281736db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
281836db0b34SBarry Smith 
281936db0b34SBarry Smith        The i and j indices can be either 0- or 1 based
282036db0b34SBarry Smith 
282136db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
282236db0b34SBarry Smith 
282336db0b34SBarry Smith @*/
282436db0b34SBarry Smith int MatCreateSeqAIJWithArrays(MPI_Comm comm,int m,int n,int* i,int*j,Scalar *a,Mat *mat)
282536db0b34SBarry Smith {
282636db0b34SBarry Smith   int        ierr,ii;
282736db0b34SBarry Smith   Mat_SeqAIJ *aij;
282836db0b34SBarry Smith 
282936db0b34SBarry Smith   PetscFunctionBegin;
283036db0b34SBarry Smith   ierr = MatCreateSeqAIJ(comm,m,n,0,0,mat);CHKERRQ(ierr);
283136db0b34SBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
283236db0b34SBarry Smith   ierr = PetscFree(aij->a);CHKERRQ(ierr);
283336db0b34SBarry Smith 
283436db0b34SBarry Smith   if (i[0] == 1) {
283536db0b34SBarry Smith     aij->indexshift = -1;
283636db0b34SBarry Smith   } else if (i[0]) {
283729bbc08cSBarry Smith     SETERRQ(1,"i (row indices) do not start with 0 or 1");
283836db0b34SBarry Smith   }
283936db0b34SBarry Smith   aij->i = i;
284036db0b34SBarry Smith   aij->j = j;
284136db0b34SBarry Smith   aij->a = a;
284236db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
284336db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
284436db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
284536db0b34SBarry Smith 
284636db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
284736db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
28489860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g)
284929bbc08cSBarry Smith     if (i[ii+1] - i[ii] < 0) SETERRQ2(1,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
285036db0b34SBarry Smith #endif
285136db0b34SBarry Smith   }
28529860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g)
285336db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
285429bbc08cSBarry Smith     if (j[ii] < -aij->indexshift) SETERRQ2(1,"Negative column index at location = %d index = %d",ii,j[ii]);
285529bbc08cSBarry Smith     if (j[ii] > n - 1 -aij->indexshift) SETERRQ2(1,"Column index to large at location = %d index = %d",ii,j[ii]);
285636db0b34SBarry Smith   }
285736db0b34SBarry Smith #endif
285836db0b34SBarry Smith 
2859b65db4caSBarry Smith   /* changes indices to start at 0 */
2860b65db4caSBarry Smith   if (i[0]) {
2861b65db4caSBarry Smith     aij->indexshift = 0;
2862b65db4caSBarry Smith     for (ii=0; ii<m; ii++) {
2863b65db4caSBarry Smith       i[ii]--;
2864b65db4caSBarry Smith     }
2865b65db4caSBarry Smith     for (ii=0; ii<i[m]; ii++) {
2866b65db4caSBarry Smith       j[ii]--;
2867b65db4caSBarry Smith     }
2868b65db4caSBarry Smith   }
2869b65db4caSBarry Smith 
2870b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2871b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
287236db0b34SBarry Smith   PetscFunctionReturn(0);
287336db0b34SBarry Smith }
287436db0b34SBarry Smith 
2875cc8ba8e1SBarry Smith #undef __FUNCT__
2876ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
2877ee4f033dSBarry Smith int MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
2878cc8ba8e1SBarry Smith {
2879cc8ba8e1SBarry Smith   int        ierr;
2880cc8ba8e1SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
288136db0b34SBarry Smith 
2882cc8ba8e1SBarry Smith   PetscFunctionBegin;
2883cc8ba8e1SBarry Smith   ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
2884cc8ba8e1SBarry Smith   a->coloring = coloring;
2885cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
2886cc8ba8e1SBarry Smith }
2887cc8ba8e1SBarry Smith 
2888ee4f033dSBarry Smith #if defined(PETSC_HAVE_ADIC) && !defined(PETSC_USE_COMPLEX)
2889ee4f033dSBarry Smith EXTERN_C_BEGIN
2890cc8ba8e1SBarry Smith #include "adic_utils.h"
2891ee4f033dSBarry Smith EXTERN_C_END
2892cc8ba8e1SBarry Smith 
2893cc8ba8e1SBarry Smith #undef __FUNCT__
2894ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
2895ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
2896cc8ba8e1SBarry Smith {
2897cc8ba8e1SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2898ee4f033dSBarry Smith   int        m = A->m,*ii = a->i,*jj = a->j,nz,i,*color,j,nlen;
2899cc8ba8e1SBarry Smith   Scalar     *v = a->a,*values;
2900ee4f033dSBarry Smith   char       *cadvalues = (char *)advalues;
2901cc8ba8e1SBarry Smith 
2902cc8ba8e1SBarry Smith   PetscFunctionBegin;
2903cc8ba8e1SBarry Smith   if (!a->coloring) SETERRQ(1,"Coloring not set for matrix");
2904ee4f033dSBarry Smith   nlen  = my_AD_GetDerivTypeSize();
2905cc8ba8e1SBarry Smith   color = a->coloring->colors;
2906cc8ba8e1SBarry Smith   /* loop over rows */
2907cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
2908cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
2909cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
2910ee4f033dSBarry Smith     values = my_AD_GetGradArray(cadvalues);
2911cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
2912cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
2913cc8ba8e1SBarry Smith     }
2914ee4f033dSBarry Smith     cadvalues += nlen; /* jump to next row of derivatives */
2915ee4f033dSBarry Smith   }
2916ee4f033dSBarry Smith   PetscFunctionReturn(0);
2917ee4f033dSBarry Smith }
2918ee4f033dSBarry Smith 
2919ee4f033dSBarry Smith #else
2920ee4f033dSBarry Smith 
2921ee4f033dSBarry Smith #undef __FUNCT__
2922ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
2923ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
2924ee4f033dSBarry Smith {
2925ee4f033dSBarry Smith   PetscFunctionBegin;
2926ee4f033dSBarry Smith   SETERRQ(1,"PETSc installed without ADIC");
2927ee4f033dSBarry Smith }
2928ee4f033dSBarry Smith 
2929ee4f033dSBarry Smith #endif
2930ee4f033dSBarry Smith 
2931ee4f033dSBarry Smith #undef __FUNCT__
2932ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
2933ee4f033dSBarry Smith int MatSetValuesAdifor_SeqAIJ(Mat A,int nl,void *advalues)
2934ee4f033dSBarry Smith {
2935ee4f033dSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2936ee4f033dSBarry Smith   int        m = A->m,*ii = a->i,*jj = a->j,nz,i,*color,j;
2937ee4f033dSBarry Smith   Scalar     *v = a->a,*values = (Scalar *)advalues;
2938ee4f033dSBarry Smith 
2939ee4f033dSBarry Smith   PetscFunctionBegin;
2940ee4f033dSBarry Smith   if (!a->coloring) SETERRQ(1,"Coloring not set for matrix");
2941ee4f033dSBarry Smith   color = a->coloring->colors;
2942ee4f033dSBarry Smith   /* loop over rows */
2943ee4f033dSBarry Smith   for (i=0; i<m; i++) {
2944ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
2945ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
2946ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
2947ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
2948ee4f033dSBarry Smith     }
2949ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
2950cc8ba8e1SBarry Smith   }
2951cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
2952cc8ba8e1SBarry Smith }
295336db0b34SBarry Smith 
2954