xref: /petsc/src/mat/impls/aij/seq/aij.c (revision beeb8507e958b1f7939765876d92401c4f2d0570)
173f4d377SMatthew Knepley /*$Id: aij.c,v 1.385 2001/09/07 20:09:22 bsmith Exp $*/
2d5d45c9bSBarry Smith /*
33369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
4d5d45c9bSBarry Smith   matrix storage format.
5d5d45c9bSBarry Smith */
63369ce9aSBarry Smith 
79e070d67SMatthew Knepley #include "src/mat/impls/aij/seq/aij.h"          /*I "petscmat.h" I*/
8f5eb4b81SSatish Balay #include "src/vec/vecimpl.h"
9f5eb4b81SSatish Balay #include "src/inline/spops.h"
108d195f9aSBarry Smith #include "src/inline/dot.h"
110a835dfdSSatish Balay #include "petscbt.h"
1217ab2063SBarry Smith 
13a2ce50c7SBarry Smith 
14ca44d042SBarry Smith EXTERN int MatToSymmetricIJ_SeqAIJ(int,int*,int*,int,int,int**,int**);
1517ab2063SBarry Smith 
164a2ae208SSatish Balay #undef __FUNCT__
174a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
184e7234bfSBarry Smith int MatGetRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *m,int *ia[],int *ja[],PetscTruth *done)
1917ab2063SBarry Smith {
20416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
216945ee14SBarry Smith   int        ierr,i,ishift;
2217ab2063SBarry Smith 
233a40ed3dSBarry Smith   PetscFunctionBegin;
2431625ec5SSatish Balay   *m     = A->m;
253a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
26bfeeae90SHong Zhang   ishift = 0;
2753e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
28273d9f13SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
29bfeeae90SHong Zhang   } else if (oshift == 1) {
30435da068SBarry Smith     int nz = a->i[A->m];
313b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
321bc30dd5SBarry Smith     ierr = PetscMalloc((A->m+1)*sizeof(int),ia);CHKERRQ(ierr);
33b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),ja);CHKERRQ(ierr);
343b2fbd54SBarry Smith     for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
35273d9f13SBarry Smith     for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] + 1;
366945ee14SBarry Smith   } else {
376945ee14SBarry Smith     *ia = a->i; *ja = a->j;
38a2ce50c7SBarry Smith   }
393a40ed3dSBarry Smith   PetscFunctionReturn(0);
40a2744918SBarry Smith }
41a2744918SBarry Smith 
424a2ae208SSatish Balay #undef __FUNCT__
434a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
444e7234bfSBarry Smith int MatRestoreRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int *ia[],int *ja[],PetscTruth *done)
456945ee14SBarry Smith {
46bfeeae90SHong Zhang   int ierr;
476945ee14SBarry Smith 
483a40ed3dSBarry Smith   PetscFunctionBegin;
493a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
50bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
51606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
52606d414cSSatish Balay     ierr = PetscFree(*ja);CHKERRQ(ierr);
53bcd2baecSBarry Smith   }
543a40ed3dSBarry Smith   PetscFunctionReturn(0);
5517ab2063SBarry Smith }
5617ab2063SBarry Smith 
574a2ae208SSatish Balay #undef __FUNCT__
584a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
594e7234bfSBarry Smith int MatGetColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *nn,int *ia[],int *ja[],PetscTruth *done)
603b2fbd54SBarry Smith {
613b2fbd54SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
62bfeeae90SHong Zhang   int        ierr,i,*collengths,*cia,*cja,n = A->n,m = A->m;
63bfeeae90SHong Zhang   int        nz = a->i[m],row,*jj,mr,col;
643b2fbd54SBarry Smith 
653a40ed3dSBarry Smith   PetscFunctionBegin;
663b2fbd54SBarry Smith   *nn     = A->n;
673a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
683b2fbd54SBarry Smith   if (symmetric) {
69bfeeae90SHong Zhang     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
703b2fbd54SBarry Smith   } else {
71b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(int),&collengths);CHKERRQ(ierr);
72549d3d68SSatish Balay     ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr);
73b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(int),&cia);CHKERRQ(ierr);
74b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),&cja);CHKERRQ(ierr);
753b2fbd54SBarry Smith     jj = a->j;
763b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
77bfeeae90SHong Zhang       collengths[jj[i]]++;
783b2fbd54SBarry Smith     }
793b2fbd54SBarry Smith     cia[0] = oshift;
803b2fbd54SBarry Smith     for (i=0; i<n; i++) {
813b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
823b2fbd54SBarry Smith     }
83549d3d68SSatish Balay     ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr);
843b2fbd54SBarry Smith     jj   = a->j;
85a93ec695SBarry Smith     for (row=0; row<m; row++) {
86a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
87a93ec695SBarry Smith       for (i=0; i<mr; i++) {
88bfeeae90SHong Zhang         col = *jj++;
893b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
903b2fbd54SBarry Smith       }
913b2fbd54SBarry Smith     }
92606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
933b2fbd54SBarry Smith     *ia = cia; *ja = cja;
943b2fbd54SBarry Smith   }
953a40ed3dSBarry Smith   PetscFunctionReturn(0);
963b2fbd54SBarry Smith }
973b2fbd54SBarry Smith 
984a2ae208SSatish Balay #undef __FUNCT__
994a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
1004e7234bfSBarry Smith int MatRestoreColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int *ia[],int *ja[],PetscTruth *done)
1013b2fbd54SBarry Smith {
102606d414cSSatish Balay   int ierr;
103606d414cSSatish Balay 
1043a40ed3dSBarry Smith   PetscFunctionBegin;
1053a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1063b2fbd54SBarry Smith 
107606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
108606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1093b2fbd54SBarry Smith 
1103a40ed3dSBarry Smith   PetscFunctionReturn(0);
1113b2fbd54SBarry Smith }
1123b2fbd54SBarry Smith 
113227d817aSBarry Smith #define CHUNKSIZE   15
11417ab2063SBarry Smith 
1154a2ae208SSatish Balay #undef __FUNCT__
1164a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
117f15d580aSBarry Smith int MatSetValues_SeqAIJ(Mat A,int m,const int im[],int n,const int in[],const PetscScalar v[],InsertMode is)
11817ab2063SBarry Smith {
119416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
120416022c9SBarry Smith   int         *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,sorted = a->sorted;
121273d9f13SBarry Smith   int         *imax = a->imax,*ai = a->i,*ailen = a->ilen;
122bfeeae90SHong Zhang   int         *aj = a->j,nonew = a->nonew,ierr;
12387828ca2SBarry Smith   PetscScalar *ap,value,*aa = a->a;
12436db0b34SBarry Smith   PetscTruth  ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE);
125273d9f13SBarry Smith   PetscTruth  roworiented = a->roworiented;
12617ab2063SBarry Smith 
1273a40ed3dSBarry Smith   PetscFunctionBegin;
12817ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
129416022c9SBarry Smith     row  = im[k];
1305ef9f2a5SBarry Smith     if (row < 0) continue;
131aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
132590ac198SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",row,A->m-1);
1333b2fbd54SBarry Smith #endif
134bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
13517ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
136416022c9SBarry Smith     low = 0;
13717ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1385ef9f2a5SBarry Smith       if (in[l] < 0) continue;
139aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
140590ac198SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[l],A->n-1);
1413b2fbd54SBarry Smith #endif
142bfeeae90SHong Zhang       col = in[l];
1434b0e389bSBarry Smith       if (roworiented) {
1445ef9f2a5SBarry Smith         value = v[l + k*n];
145bef8e0ddSBarry Smith       } else {
1464b0e389bSBarry Smith         value = v[k + l*m];
1474b0e389bSBarry Smith       }
14836db0b34SBarry Smith       if (value == 0.0 && ignorezeroentries) continue;
14936db0b34SBarry Smith 
150416022c9SBarry Smith       if (!sorted) low = 0; high = nrow;
151416022c9SBarry Smith       while (high-low > 5) {
152416022c9SBarry Smith         t = (low+high)/2;
153416022c9SBarry Smith         if (rp[t] > col) high = t;
154416022c9SBarry Smith         else             low  = t;
15517ab2063SBarry Smith       }
156416022c9SBarry Smith       for (i=low; i<high; i++) {
15717ab2063SBarry Smith         if (rp[i] > col) break;
15817ab2063SBarry Smith         if (rp[i] == col) {
159416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
16017ab2063SBarry Smith           else                  ap[i] = value;
16117ab2063SBarry Smith           goto noinsert;
16217ab2063SBarry Smith         }
16317ab2063SBarry Smith       }
164c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
16529bbc08cSBarry Smith       else if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix",row,col);
16617ab2063SBarry Smith       if (nrow >= rmax) {
16717ab2063SBarry Smith         /* there is no extra room in row, therefore enlarge */
168a7ed9263SMatthew Knepley         int         new_nz = ai[A->m] + CHUNKSIZE,*new_i,*new_j;
169a7ed9263SMatthew Knepley         size_t      len;
17087828ca2SBarry Smith         PetscScalar *new_a;
17117ab2063SBarry Smith 
17229bbc08cSBarry Smith         if (nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix requiring new malloc()",row,col);
17396854ed6SLois Curfman McInnes 
17417ab2063SBarry Smith         /* malloc new storage space */
175a7ed9263SMatthew Knepley         len     = ((size_t) new_nz)*(sizeof(int)+sizeof(PetscScalar))+(A->m+1)*sizeof(int);
176b0a32e0cSBarry Smith 	ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr);
17717ab2063SBarry Smith         new_j   = (int*)(new_a + new_nz);
17817ab2063SBarry Smith         new_i   = new_j + new_nz;
17917ab2063SBarry Smith 
18017ab2063SBarry Smith         /* copy over old data into new slots */
18117ab2063SBarry Smith         for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];}
182273d9f13SBarry Smith         for (ii=row+1; ii<A->m+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;}
183bfeeae90SHong Zhang         ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow)*sizeof(int));CHKERRQ(ierr);
184bfeeae90SHong Zhang         len  = (((size_t) new_nz) - CHUNKSIZE - ai[row] - nrow );
185bfeeae90SHong Zhang         ierr = PetscMemcpy(new_j+ai[row]+nrow+CHUNKSIZE,aj+ai[row]+nrow,len*sizeof(int));CHKERRQ(ierr);
186bfeeae90SHong Zhang         ierr = PetscMemcpy(new_a,aa,(((size_t) ai[row])+nrow)*sizeof(PetscScalar));CHKERRQ(ierr);
187bfeeae90SHong Zhang         ierr = PetscMemcpy(new_a+ai[row]+nrow+CHUNKSIZE,aa+ai[row]+nrow,len*sizeof(PetscScalar));CHKERRQ(ierr);
18817ab2063SBarry Smith         /* free up old matrix storage */
189606d414cSSatish Balay         ierr = PetscFree(a->a);CHKERRQ(ierr);
190606d414cSSatish Balay         if (!a->singlemalloc) {
191606d414cSSatish Balay           ierr = PetscFree(a->i);CHKERRQ(ierr);
192606d414cSSatish Balay           ierr = PetscFree(a->j);CHKERRQ(ierr);
193606d414cSSatish Balay         }
194416022c9SBarry Smith         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;
195f1e2ffcdSBarry Smith         a->singlemalloc = PETSC_TRUE;
19617ab2063SBarry Smith 
197bfeeae90SHong Zhang         rp   = aj + ai[row]; ap = aa + ai[row] ;
198416022c9SBarry Smith         rmax = imax[row] = imax[row] + CHUNKSIZE;
19987828ca2SBarry Smith         PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar)));
200416022c9SBarry Smith         a->maxnz += CHUNKSIZE;
201b810aeb4SBarry Smith         a->reallocs++;
20217ab2063SBarry Smith       }
203416022c9SBarry Smith       N = nrow++ - 1; a->nz++;
204416022c9SBarry Smith       /* shift up all the later entries in this row */
205416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
20617ab2063SBarry Smith         rp[ii+1] = rp[ii];
20717ab2063SBarry Smith         ap[ii+1] = ap[ii];
20817ab2063SBarry Smith       }
20917ab2063SBarry Smith       rp[i] = col;
21017ab2063SBarry Smith       ap[i] = value;
21117ab2063SBarry Smith       noinsert:;
212416022c9SBarry Smith       low = i + 1;
21317ab2063SBarry Smith     }
21417ab2063SBarry Smith     ailen[row] = nrow;
21517ab2063SBarry Smith   }
2163a40ed3dSBarry Smith   PetscFunctionReturn(0);
21717ab2063SBarry Smith }
21817ab2063SBarry Smith 
2194a2ae208SSatish Balay #undef __FUNCT__
2204a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
221f15d580aSBarry Smith int MatGetValues_SeqAIJ(Mat A,int m,const int im[],int n,const int in[],PetscScalar v[])
2227eb43aa7SLois Curfman McInnes {
2237eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
224b49de8d1SLois Curfman McInnes   int          *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
225bfeeae90SHong Zhang   int          *ai = a->i,*ailen = a->ilen;
22687828ca2SBarry Smith   PetscScalar  *ap,*aa = a->a,zero = 0.0;
2277eb43aa7SLois Curfman McInnes 
2283a40ed3dSBarry Smith   PetscFunctionBegin;
2297eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2307eb43aa7SLois Curfman McInnes     row  = im[k];
23129bbc08cSBarry Smith     if (row < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %d",row);
232590ac198SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",row,A->m-1);
233bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2347eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2357eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
23629bbc08cSBarry Smith       if (in[l] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %d",in[l]);
237590ac198SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[l],A->n-1);
238bfeeae90SHong Zhang       col = in[l] ;
2397eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2407eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2417eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2427eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2437eb43aa7SLois Curfman McInnes         else             low  = t;
2447eb43aa7SLois Curfman McInnes       }
2457eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2467eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2477eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
248b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2497eb43aa7SLois Curfman McInnes           goto finished;
2507eb43aa7SLois Curfman McInnes         }
2517eb43aa7SLois Curfman McInnes       }
252b49de8d1SLois Curfman McInnes       *v++ = zero;
2537eb43aa7SLois Curfman McInnes       finished:;
2547eb43aa7SLois Curfman McInnes     }
2557eb43aa7SLois Curfman McInnes   }
2563a40ed3dSBarry Smith   PetscFunctionReturn(0);
2577eb43aa7SLois Curfman McInnes }
2587eb43aa7SLois Curfman McInnes 
25917ab2063SBarry Smith 
2604a2ae208SSatish Balay #undef __FUNCT__
2614a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
262b0a32e0cSBarry Smith int MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
26317ab2063SBarry Smith {
264416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
265416022c9SBarry Smith   int        i,fd,*col_lens,ierr;
26617ab2063SBarry Smith 
2673a40ed3dSBarry Smith   PetscFunctionBegin;
268b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
269b0a32e0cSBarry Smith   ierr = PetscMalloc((4+A->m)*sizeof(int),&col_lens);CHKERRQ(ierr);
270552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
271273d9f13SBarry Smith   col_lens[1] = A->m;
272273d9f13SBarry Smith   col_lens[2] = A->n;
273416022c9SBarry Smith   col_lens[3] = a->nz;
274416022c9SBarry Smith 
275416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
276273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
277416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
27817ab2063SBarry Smith   }
279273d9f13SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->m,PETSC_INT,1);CHKERRQ(ierr);
280606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
281416022c9SBarry Smith 
282416022c9SBarry Smith   /* store column indices (zero start index) */
2830752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,0);CHKERRQ(ierr);
284416022c9SBarry Smith 
285416022c9SBarry Smith   /* store nonzero values */
2860752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,0);CHKERRQ(ierr);
2873a40ed3dSBarry Smith   PetscFunctionReturn(0);
28817ab2063SBarry Smith }
289416022c9SBarry Smith 
290cd155464SBarry Smith extern int MatMPIAIJFactorInfo_SuperLu(Mat,PetscViewer);
291e7420845SHong Zhang extern int MatSeqAIJFactorInfo_UMFPACK(Mat,PetscViewer);
2924ffef66eSHong Zhang extern int MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
2931dfdf1d9SHong Zhang extern int MatFactorInfo_MUMPS(Mat,PetscViewer);
294cd155464SBarry Smith 
2954a2ae208SSatish Balay #undef __FUNCT__
2964a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
297b0a32e0cSBarry Smith int MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
298416022c9SBarry Smith {
299416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
300bfeeae90SHong Zhang   int               ierr,i,j,m = A->m,shift=0;
301fb9695e5SSatish Balay   char              *name;
302f3ef73ceSBarry Smith   PetscViewerFormat format;
30317ab2063SBarry Smith 
3043a40ed3dSBarry Smith   PetscFunctionBegin;
305435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
306b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
307456192e2SBarry Smith   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL || format == PETSC_VIEWER_ASCII_INFO) {
3086831982aSBarry Smith     if (a->inode.size) {
309b0a32e0cSBarry 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);
3106831982aSBarry Smith     } else {
311b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"not using I-node routines\n");CHKERRQ(ierr);
3126831982aSBarry Smith     }
313fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
314d00d2cf4SBarry Smith     int nofinalvalue = 0;
315273d9f13SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->n-!shift)) {
316d00d2cf4SBarry Smith       nofinalvalue = 1;
317d00d2cf4SBarry Smith     }
318b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
319b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %d %d \n",m,A->n);CHKERRQ(ierr);
320b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %d \n",a->nz);CHKERRQ(ierr);
321b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%d,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
322b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
32317ab2063SBarry Smith 
32417ab2063SBarry Smith     for (i=0; i<m; i++) {
325416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
326aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
327b0a32e0cSBarry 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);
32817ab2063SBarry Smith #else
329b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
33017ab2063SBarry Smith #endif
33117ab2063SBarry Smith       }
33217ab2063SBarry Smith     }
333d00d2cf4SBarry Smith     if (nofinalvalue) {
334b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",m,A->n,0.0);CHKERRQ(ierr);
335d00d2cf4SBarry Smith     }
336fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
337b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
338cd155464SBarry Smith   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
3396ea74821SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST) && !defined(PETSC_USE_SINGLE)
340cd155464SBarry Smith      ierr = MatMPIAIJFactorInfo_SuperLu(A,viewer);CHKERRQ(ierr);
341cd155464SBarry Smith #endif
342e7420845SHong Zhang #if defined(PETSC_HAVE_UMFPACK) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_COMPLEX)
343e7420845SHong Zhang      ierr = MatSeqAIJFactorInfo_UMFPACK(A,viewer);CHKERRQ(ierr);
344e7420845SHong Zhang #endif
3454ffef66eSHong Zhang #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_COMPLEX)
3464ffef66eSHong Zhang      ierr = MatSeqAIJFactorInfo_Matlab(A,viewer);CHKERRQ(ierr);
3474ffef66eSHong Zhang #endif
3481dfdf1d9SHong Zhang #if defined(PETSC_HAVE_MUMPS) && !defined(PETSC_USE_SINGLE)
3491dfdf1d9SHong Zhang      ierr = MatFactorInfo_MUMPS(A,viewer);CHKERRQ(ierr);
3501dfdf1d9SHong Zhang #endif
351cd155464SBarry Smith      PetscFunctionReturn(0);
352fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
353b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
35444cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
355b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr);
35644cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
357aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
35836db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
35962b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36036db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
36162b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36236db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
36362b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3646831982aSBarry Smith         }
36544cd7ae7SLois Curfman McInnes #else
36662b941d6SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
36744cd7ae7SLois Curfman McInnes #endif
36844cd7ae7SLois Curfman McInnes       }
369b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
37044cd7ae7SLois Curfman McInnes     }
371b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
372fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
373496be53dSLois Curfman McInnes     int nzd=0,fshift=1,*sptr;
374b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
375b0a32e0cSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(int),&sptr);CHKERRQ(ierr);
376496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
377496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
378496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
379496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
380aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
38136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
382496be53dSLois Curfman McInnes #else
383496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
384496be53dSLois Curfman McInnes #endif
385496be53dSLois Curfman McInnes         }
386496be53dSLois Curfman McInnes       }
387496be53dSLois Curfman McInnes     }
3882e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
389b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %d %d\n\n",m,nzd);CHKERRQ(ierr);
3902e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
391b0a32e0cSBarry 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);}
392b0a32e0cSBarry 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);}
393b0a32e0cSBarry 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);}
394b0a32e0cSBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
395b0a32e0cSBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %d %d\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
396b0a32e0cSBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %d\n",sptr[i]);CHKERRQ(ierr);}
397496be53dSLois Curfman McInnes     }
398b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
399606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
400496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
401496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
402b0a32e0cSBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %d ",a->j[j]+fshift);CHKERRQ(ierr);}
403496be53dSLois Curfman McInnes       }
404b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
405496be53dSLois Curfman McInnes     }
406b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
407496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
408496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
409496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
410aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
41136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
412b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4136831982aSBarry Smith           }
414496be53dSLois Curfman McInnes #else
415b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
416496be53dSLois Curfman McInnes #endif
417496be53dSLois Curfman McInnes         }
418496be53dSLois Curfman McInnes       }
419b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
420496be53dSLois Curfman McInnes     }
421b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
422fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
42302594712SBarry Smith     int         cnt = 0,jcnt;
42487828ca2SBarry Smith     PetscScalar value;
42502594712SBarry Smith 
426b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
42702594712SBarry Smith     for (i=0; i<m; i++) {
42802594712SBarry Smith       jcnt = 0;
429273d9f13SBarry Smith       for (j=0; j<A->n; j++) {
430e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
43102594712SBarry Smith           value = a->a[cnt++];
432e24b481bSBarry Smith           jcnt++;
43302594712SBarry Smith         } else {
43402594712SBarry Smith           value = 0.0;
43502594712SBarry Smith         }
436aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
437b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
43802594712SBarry Smith #else
439b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
44002594712SBarry Smith #endif
44102594712SBarry Smith       }
442b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
44302594712SBarry Smith     }
444b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4453a40ed3dSBarry Smith   } else {
446b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
44717ab2063SBarry Smith     for (i=0; i<m; i++) {
448b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr);
449416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
450aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
45136db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
45262b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
45336db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
45462b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4553a40ed3dSBarry Smith         } else {
45662b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
45717ab2063SBarry Smith         }
45817ab2063SBarry Smith #else
45962b941d6SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
46017ab2063SBarry Smith #endif
46117ab2063SBarry Smith       }
462b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
46317ab2063SBarry Smith     }
464b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
46517ab2063SBarry Smith   }
466b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4673a40ed3dSBarry Smith   PetscFunctionReturn(0);
468416022c9SBarry Smith }
469416022c9SBarry Smith 
4704a2ae208SSatish Balay #undef __FUNCT__
4714a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
472b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
473416022c9SBarry Smith {
474480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
475416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
476bfeeae90SHong Zhang   int               ierr,i,j,m = A->m,color;
47736db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
478b0a32e0cSBarry Smith   PetscViewer       viewer;
479f3ef73ceSBarry Smith   PetscViewerFormat format;
480cddf8d76SBarry Smith 
4813a40ed3dSBarry Smith   PetscFunctionBegin;
482480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
483b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
48419bcc07fSBarry Smith 
485b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
486416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4870513a670SBarry Smith 
488fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
4890513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
490b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
491416022c9SBarry Smith     for (i=0; i<m; i++) {
492cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
493bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
494bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
495aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
49636db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
497cddf8d76SBarry Smith #else
498cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
499cddf8d76SBarry Smith #endif
500b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
501cddf8d76SBarry Smith       }
502cddf8d76SBarry Smith     }
503b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
504cddf8d76SBarry Smith     for (i=0; i<m; i++) {
505cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
506bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
507bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
508cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
509b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
510cddf8d76SBarry Smith       }
511cddf8d76SBarry Smith     }
512b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
513cddf8d76SBarry Smith     for (i=0; i<m; i++) {
514cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
515bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
516bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
517aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
51836db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
519cddf8d76SBarry Smith #else
520cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
521cddf8d76SBarry Smith #endif
522b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
523416022c9SBarry Smith       }
524416022c9SBarry Smith     }
5250513a670SBarry Smith   } else {
5260513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5270513a670SBarry Smith     /* first determine max of all nonzero values */
5280513a670SBarry Smith     int    nz = a->nz,count;
529b0a32e0cSBarry Smith     PetscDraw   popup;
53036db0b34SBarry Smith     PetscReal scale;
5310513a670SBarry Smith 
5320513a670SBarry Smith     for (i=0; i<nz; i++) {
5330513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5340513a670SBarry Smith     }
535b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
536b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
537b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5380513a670SBarry Smith     count = 0;
5390513a670SBarry Smith     for (i=0; i<m; i++) {
5400513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
541bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
542bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
543b0a32e0cSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(a->a[count]));
544b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5450513a670SBarry Smith         count++;
5460513a670SBarry Smith       }
5470513a670SBarry Smith     }
5480513a670SBarry Smith   }
549480ef9eaSBarry Smith   PetscFunctionReturn(0);
550480ef9eaSBarry Smith }
551cddf8d76SBarry Smith 
5524a2ae208SSatish Balay #undef __FUNCT__
5534a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
554b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
555480ef9eaSBarry Smith {
556480ef9eaSBarry Smith   int        ierr;
557b0a32e0cSBarry Smith   PetscDraw  draw;
55836db0b34SBarry Smith   PetscReal  xr,yr,xl,yl,h,w;
559480ef9eaSBarry Smith   PetscTruth isnull;
560480ef9eaSBarry Smith 
561480ef9eaSBarry Smith   PetscFunctionBegin;
562b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
563b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
564480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
565480ef9eaSBarry Smith 
566480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
567273d9f13SBarry Smith   xr  = A->n; yr = A->m; h = yr/10.0; w = xr/10.0;
568480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
569b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
570b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
571480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5723a40ed3dSBarry Smith   PetscFunctionReturn(0);
573416022c9SBarry Smith }
574416022c9SBarry Smith 
5754a2ae208SSatish Balay #undef __FUNCT__
5764a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
577b0a32e0cSBarry Smith int MatView_SeqAIJ(Mat A,PetscViewer viewer)
578416022c9SBarry Smith {
579416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
580bcd2baecSBarry Smith   int         ierr;
5816831982aSBarry Smith   PetscTruth  issocket,isascii,isbinary,isdraw;
582416022c9SBarry Smith 
5833a40ed3dSBarry Smith   PetscFunctionBegin;
584b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
585b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
586fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
587fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
5880f5bd95cSBarry Smith   if (issocket) {
589b0a32e0cSBarry Smith     ierr = PetscViewerSocketPutSparse_Private(viewer,A->m,A->n,a->nz,a->a,a->i,a->j);CHKERRQ(ierr);
5900f5bd95cSBarry Smith   } else if (isascii) {
5913a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
5920f5bd95cSBarry Smith   } else if (isbinary) {
5933a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
5940f5bd95cSBarry Smith   } else if (isdraw) {
5953a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
5965cd90555SBarry Smith   } else {
59729bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
59817ab2063SBarry Smith   }
5993a40ed3dSBarry Smith   PetscFunctionReturn(0);
60017ab2063SBarry Smith }
60119bcc07fSBarry Smith 
6023a7fca6bSBarry Smith EXTERN int Mat_AIJ_CheckInode(Mat,PetscTruth);
6034a2ae208SSatish Balay #undef __FUNCT__
6044a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
6058f6be9afSLois Curfman McInnes int MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
60617ab2063SBarry Smith {
607416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
60841c01911SSatish Balay   int          fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax,ierr;
609bfeeae90SHong Zhang   int          m = A->m,*ip,N,*ailen = a->ilen,rmax = 0;
61087828ca2SBarry Smith   PetscScalar  *aa = a->a,*ap;
611b3a1e11cSKris Buschelman #if defined(PETSC_HAVE_SUPERLUDIST) || defined(PETSC_HAVE_UMFPACK) || defined(PETSC_HAVE_MUMPS)
6129b9367d5SHong Zhang   PetscTruth   flag;
6139e070d67SMatthew Knepley #endif
61417ab2063SBarry Smith 
6153a40ed3dSBarry Smith   PetscFunctionBegin;
6163a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
61717ab2063SBarry Smith 
61843ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
61917ab2063SBarry Smith   for (i=1; i<m; i++) {
620416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
62117ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
62294a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
62317ab2063SBarry Smith     if (fshift) {
624bfeeae90SHong Zhang       ip = aj + ai[i] ;
625bfeeae90SHong Zhang       ap = aa + ai[i] ;
62617ab2063SBarry Smith       N  = ailen[i];
62717ab2063SBarry Smith       for (j=0; j<N; j++) {
62817ab2063SBarry Smith         ip[j-fshift] = ip[j];
62917ab2063SBarry Smith         ap[j-fshift] = ap[j];
63017ab2063SBarry Smith       }
63117ab2063SBarry Smith     }
63217ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
63317ab2063SBarry Smith   }
63417ab2063SBarry Smith   if (m) {
63517ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
63617ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
63717ab2063SBarry Smith   }
63817ab2063SBarry Smith   /* reset ilen and imax for each row */
63917ab2063SBarry Smith   for (i=0; i<m; i++) {
64017ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
64117ab2063SBarry Smith   }
642bfeeae90SHong Zhang   a->nz = ai[m];
64317ab2063SBarry Smith 
64417ab2063SBarry Smith   /* diagonals may have moved, so kill the diagonal pointers */
645416022c9SBarry Smith   if (fshift && a->diag) {
646606d414cSSatish Balay     ierr = PetscFree(a->diag);CHKERRQ(ierr);
647b0a32e0cSBarry Smith     PetscLogObjectMemory(A,-(m+1)*sizeof(int));
648416022c9SBarry Smith     a->diag = 0;
64917ab2063SBarry Smith   }
650b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded,%d used\n",m,A->n,fshift,a->nz);
651b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %d\n",a->reallocs);
652b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Most nonzeros in any row is %d\n",rmax);
653dd5f02e7SSatish Balay   a->reallocs          = 0;
6544e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
65536db0b34SBarry Smith   a->rmax              = rmax;
6564e220ebcSLois Curfman McInnes 
65776dd722bSSatish Balay   /* check out for identical nodes. If found, use inode functions */
6583a7fca6bSBarry Smith   ierr = Mat_AIJ_CheckInode(A,(PetscTruth)(!fshift));CHKERRQ(ierr);
659cfef3cccSHong Zhang 
6609b9367d5SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST)
661e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(A->prefix,"-mat_aij_superlu_dist",&flag);CHKERRQ(ierr);
6629b9367d5SHong Zhang   if (flag) { ierr = MatUseSuperLU_DIST_MPIAIJ(A);CHKERRQ(ierr); }
6639b9367d5SHong Zhang #endif
6649b9367d5SHong Zhang 
665564e58d6SHong Zhang #if defined(PETSC_HAVE_UMFPACK)
666e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(A->prefix,"-mat_aij_umfpack",&flag);CHKERRQ(ierr);
667564e58d6SHong Zhang   if (flag) { ierr = MatUseUMFPACK_SeqAIJ(A);CHKERRQ(ierr); }
668564e58d6SHong Zhang #endif
669564e58d6SHong Zhang 
670eee76cafSHong Zhang #if defined(PETSC_HAVE_MUMPS)
671eee76cafSHong Zhang   ierr = PetscOptionsHasName(A->prefix,"-mat_aij_mumps",&flag);CHKERRQ(ierr);
672eee76cafSHong Zhang   if (flag) { ierr = MatUseMUMPS_MPIAIJ(A);CHKERRQ(ierr); }
673eee76cafSHong Zhang #endif
674eee76cafSHong Zhang 
6753a40ed3dSBarry Smith   PetscFunctionReturn(0);
67617ab2063SBarry Smith }
67717ab2063SBarry Smith 
6784a2ae208SSatish Balay #undef __FUNCT__
6794a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
6808f6be9afSLois Curfman McInnes int MatZeroEntries_SeqAIJ(Mat A)
68117ab2063SBarry Smith {
682416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
683549d3d68SSatish Balay   int        ierr;
6843a40ed3dSBarry Smith 
6853a40ed3dSBarry Smith   PetscFunctionBegin;
686bfeeae90SHong Zhang   ierr = PetscMemzero(a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
6873a40ed3dSBarry Smith   PetscFunctionReturn(0);
68817ab2063SBarry Smith }
689416022c9SBarry Smith 
6904a2ae208SSatish Balay #undef __FUNCT__
6914a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
692e1311b90SBarry Smith int MatDestroy_SeqAIJ(Mat A)
69317ab2063SBarry Smith {
694416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
69582bf6240SBarry Smith   int        ierr;
696d5d45c9bSBarry Smith 
6973a40ed3dSBarry Smith   PetscFunctionBegin;
698aa482453SBarry Smith #if defined(PETSC_USE_LOG)
699b0a32e0cSBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%d, Cols=%d, NZ=%d",A->m,A->n,a->nz);
70017ab2063SBarry Smith #endif
70136db0b34SBarry Smith   if (a->freedata) {
702606d414cSSatish Balay     ierr = PetscFree(a->a);CHKERRQ(ierr);
703606d414cSSatish Balay     if (!a->singlemalloc) {
704606d414cSSatish Balay       ierr = PetscFree(a->i);CHKERRQ(ierr);
705606d414cSSatish Balay       ierr = PetscFree(a->j);CHKERRQ(ierr);
706606d414cSSatish Balay     }
70736db0b34SBarry Smith   }
708c38d4ed2SBarry Smith   if (a->row) {
709c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
710c38d4ed2SBarry Smith   }
711c38d4ed2SBarry Smith   if (a->col) {
712c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
713c38d4ed2SBarry Smith   }
714606d414cSSatish Balay   if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
715606d414cSSatish Balay   if (a->ilen) {ierr = PetscFree(a->ilen);CHKERRQ(ierr);}
716606d414cSSatish Balay   if (a->imax) {ierr = PetscFree(a->imax);CHKERRQ(ierr);}
717273d9f13SBarry Smith   if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);}
718606d414cSSatish Balay   if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);}
719606d414cSSatish Balay   if (a->inode.size) {ierr = PetscFree(a->inode.size);CHKERRQ(ierr);}
72082bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
721606d414cSSatish Balay   if (a->saved_values) {ierr = PetscFree(a->saved_values);CHKERRQ(ierr);}
722cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
723a30b2313SHong Zhang   if (a->xtoy) {ierr = PetscFree(a->xtoy);CHKERRQ(ierr);}
724a30b2313SHong Zhang 
725606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
7263a40ed3dSBarry Smith   PetscFunctionReturn(0);
72717ab2063SBarry Smith }
72817ab2063SBarry Smith 
7294a2ae208SSatish Balay #undef __FUNCT__
7304a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
7318f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A)
73217ab2063SBarry Smith {
7333a40ed3dSBarry Smith   PetscFunctionBegin;
7343a40ed3dSBarry Smith   PetscFunctionReturn(0);
73517ab2063SBarry Smith }
73617ab2063SBarry Smith 
7374a2ae208SSatish Balay #undef __FUNCT__
7384a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
7398f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op)
74017ab2063SBarry Smith {
741416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7423a40ed3dSBarry Smith 
7433a40ed3dSBarry Smith   PetscFunctionBegin;
744a65d3064SKris Buschelman   switch (op) {
745a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
746a65d3064SKris Buschelman       a->roworiented       = PETSC_TRUE;
747a65d3064SKris Buschelman       break;
748a65d3064SKris Buschelman     case MAT_KEEP_ZEROED_ROWS:
749a65d3064SKris Buschelman       a->keepzeroedrows    = PETSC_TRUE;
750a65d3064SKris Buschelman       break;
751a65d3064SKris Buschelman     case MAT_COLUMN_ORIENTED:
752a65d3064SKris Buschelman       a->roworiented       = PETSC_FALSE;
753a65d3064SKris Buschelman       break;
754a65d3064SKris Buschelman     case MAT_COLUMNS_SORTED:
755a65d3064SKris Buschelman       a->sorted            = PETSC_TRUE;
756a65d3064SKris Buschelman       break;
757a65d3064SKris Buschelman     case MAT_COLUMNS_UNSORTED:
758a65d3064SKris Buschelman       a->sorted            = PETSC_FALSE;
759a65d3064SKris Buschelman       break;
760a65d3064SKris Buschelman     case MAT_NO_NEW_NONZERO_LOCATIONS:
761a65d3064SKris Buschelman       a->nonew             = 1;
762a65d3064SKris Buschelman       break;
763a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
764a65d3064SKris Buschelman       a->nonew             = -1;
765a65d3064SKris Buschelman       break;
766a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
767a65d3064SKris Buschelman       a->nonew             = -2;
768a65d3064SKris Buschelman       break;
769a65d3064SKris Buschelman     case MAT_YES_NEW_NONZERO_LOCATIONS:
770a65d3064SKris Buschelman       a->nonew             = 0;
771a65d3064SKris Buschelman       break;
772a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
773a65d3064SKris Buschelman       a->ignorezeroentries = PETSC_TRUE;
774a65d3064SKris Buschelman       break;
775a65d3064SKris Buschelman     case MAT_USE_INODES:
776a65d3064SKris Buschelman       a->inode.use         = PETSC_TRUE;
777a65d3064SKris Buschelman       break;
778a65d3064SKris Buschelman     case MAT_DO_NOT_USE_INODES:
779a65d3064SKris Buschelman       a->inode.use         = PETSC_FALSE;
780a65d3064SKris Buschelman       break;
781a65d3064SKris Buschelman     case MAT_ROWS_SORTED:
782a65d3064SKris Buschelman     case MAT_ROWS_UNSORTED:
783a65d3064SKris Buschelman     case MAT_YES_NEW_DIAGONALS:
784a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
785a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
786b0a32e0cSBarry Smith       PetscLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n");
787a65d3064SKris Buschelman       break;
788a65d3064SKris Buschelman     case MAT_NO_NEW_DIAGONALS:
78929bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
790a65d3064SKris Buschelman     case MAT_INODE_LIMIT_1:
791a65d3064SKris Buschelman       a->inode.limit  = 1;
792a65d3064SKris Buschelman       break;
793a65d3064SKris Buschelman     case MAT_INODE_LIMIT_2:
794a65d3064SKris Buschelman       a->inode.limit  = 2;
795a65d3064SKris Buschelman       break;
796a65d3064SKris Buschelman     case MAT_INODE_LIMIT_3:
797a65d3064SKris Buschelman       a->inode.limit  = 3;
798a65d3064SKris Buschelman       break;
799a65d3064SKris Buschelman     case MAT_INODE_LIMIT_4:
800a65d3064SKris Buschelman       a->inode.limit  = 4;
801a65d3064SKris Buschelman       break;
802a65d3064SKris Buschelman     case MAT_INODE_LIMIT_5:
803a65d3064SKris Buschelman       a->inode.limit  = 5;
804a65d3064SKris Buschelman       break;
805a65d3064SKris Buschelman     default:
806a65d3064SKris Buschelman       SETERRQ(PETSC_ERR_SUP,"unknown option");
807a65d3064SKris Buschelman   }
8083a40ed3dSBarry Smith   PetscFunctionReturn(0);
80917ab2063SBarry Smith }
81017ab2063SBarry Smith 
8114a2ae208SSatish Balay #undef __FUNCT__
8124a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
8138f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v)
81417ab2063SBarry Smith {
815416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
816bfeeae90SHong Zhang   int          i,j,n,ierr;
81787828ca2SBarry Smith   PetscScalar  *x,zero = 0.0;
81817ab2063SBarry Smith 
8193a40ed3dSBarry Smith   PetscFunctionBegin;
8203a40ed3dSBarry Smith   ierr = VecSet(&zero,v);CHKERRQ(ierr);
821ed480e8bSBarry Smith   ierr = VecGetArrayFast(v,&x);CHKERRQ(ierr);
82236db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
823273d9f13SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
824273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
825bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
826bfeeae90SHong Zhang       if (a->j[j] == i) {
827416022c9SBarry Smith         x[i] = a->a[j];
82817ab2063SBarry Smith         break;
82917ab2063SBarry Smith       }
83017ab2063SBarry Smith     }
83117ab2063SBarry Smith   }
832ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(v,&x);CHKERRQ(ierr);
8333a40ed3dSBarry Smith   PetscFunctionReturn(0);
83417ab2063SBarry Smith }
83517ab2063SBarry Smith 
836d5d45c9bSBarry Smith 
8374a2ae208SSatish Balay #undef __FUNCT__
8384a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
8397c922b88SBarry Smith int MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
84017ab2063SBarry Smith {
841416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
8425c897100SBarry Smith   PetscScalar  *x,*y;
843bfeeae90SHong Zhang   int          ierr,m = A->m;
8445c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
8455c897100SBarry Smith   PetscScalar  *v,alpha;
8465c897100SBarry Smith   int          n,i,*idx;
8475c897100SBarry Smith #endif
84817ab2063SBarry Smith 
8493a40ed3dSBarry Smith   PetscFunctionBegin;
8502e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
851ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
852ed480e8bSBarry Smith   ierr = VecGetArrayFast(yy,&y);CHKERRQ(ierr);
8535c897100SBarry Smith 
8545c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
855bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
8565c897100SBarry Smith #else
85717ab2063SBarry Smith   for (i=0; i<m; i++) {
858bfeeae90SHong Zhang     idx   = a->j + a->i[i] ;
859bfeeae90SHong Zhang     v     = a->a + a->i[i] ;
860416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
86117ab2063SBarry Smith     alpha = x[i];
86217ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
86317ab2063SBarry Smith   }
8645c897100SBarry Smith #endif
865b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
866ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
867ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(yy,&y);CHKERRQ(ierr);
8683a40ed3dSBarry Smith   PetscFunctionReturn(0);
86917ab2063SBarry Smith }
87017ab2063SBarry Smith 
8714a2ae208SSatish Balay #undef __FUNCT__
8725c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
8735c897100SBarry Smith int MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
8745c897100SBarry Smith {
8758d5b0100SBarry Smith   PetscScalar  zero = 0.0;
8765c897100SBarry Smith   int          ierr;
8775c897100SBarry Smith 
8785c897100SBarry Smith   PetscFunctionBegin;
8795c897100SBarry Smith   ierr = VecSet(&zero,yy);CHKERRQ(ierr);
8805c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
8815c897100SBarry Smith   PetscFunctionReturn(0);
8825c897100SBarry Smith }
8835c897100SBarry Smith 
8845c897100SBarry Smith 
8855c897100SBarry Smith #undef __FUNCT__
8864a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
88744cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
88817ab2063SBarry Smith {
889416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
890362ced78SSatish Balay   PetscScalar  *x,*y,*v;
891bfeeae90SHong Zhang   int          ierr,m = A->m,*idx,*ii;
892aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
893e36a17ebSSatish Balay   int          n,i,jrow,j;
894362ced78SSatish Balay   PetscScalar  sum;
895e36a17ebSSatish Balay #endif
89617ab2063SBarry Smith 
897b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
898fee21e36SBarry Smith #pragma disjoint(*x,*y,*v)
899fee21e36SBarry Smith #endif
900fee21e36SBarry Smith 
9013a40ed3dSBarry Smith   PetscFunctionBegin;
902ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
903ed480e8bSBarry Smith   ierr = VecGetArrayFast(yy,&y);CHKERRQ(ierr);
904416022c9SBarry Smith   idx  = a->j;
905d64ed03dSBarry Smith   v    = a->a;
906416022c9SBarry Smith   ii   = a->i;
907aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
908bfeeae90SHong Zhang   fortranmultaij_(&m,x,ii,idx,v,y);
9098d195f9aSBarry Smith #else
91017ab2063SBarry Smith   for (i=0; i<m; i++) {
9119ea0dfa2SSatish Balay     jrow = ii[i];
9129ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
91317ab2063SBarry Smith     sum  = 0.0;
9149ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
9159ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
9169ea0dfa2SSatish Balay      }
91717ab2063SBarry Smith     y[i] = sum;
91817ab2063SBarry Smith   }
9198d195f9aSBarry Smith #endif
920b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz - m);
921ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
922ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(yy,&y);CHKERRQ(ierr);
9233a40ed3dSBarry Smith   PetscFunctionReturn(0);
92417ab2063SBarry Smith }
92517ab2063SBarry Smith 
9264a2ae208SSatish Balay #undef __FUNCT__
9274a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
92844cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
92917ab2063SBarry Smith {
930416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
931362ced78SSatish Balay   PetscScalar  *x,*y,*z,*v;
932bfeeae90SHong Zhang   int          ierr,m = A->m,*idx,*ii;
933aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
934e36a17ebSSatish Balay   int          n,i,jrow,j;
935362ced78SSatish Balay PetscScalar    sum;
936e36a17ebSSatish Balay #endif
9379ea0dfa2SSatish Balay 
9383a40ed3dSBarry Smith   PetscFunctionBegin;
939ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
940ed480e8bSBarry Smith   ierr = VecGetArrayFast(yy,&y);CHKERRQ(ierr);
9412e8a6d31SBarry Smith   if (zz != yy) {
942ed480e8bSBarry Smith     ierr = VecGetArrayFast(zz,&z);CHKERRQ(ierr);
9432e8a6d31SBarry Smith   } else {
9442e8a6d31SBarry Smith     z = y;
9452e8a6d31SBarry Smith   }
946bfeeae90SHong Zhang 
947cddf8d76SBarry Smith   idx  = a->j;
948d64ed03dSBarry Smith   v    = a->a;
949cddf8d76SBarry Smith   ii   = a->i;
950aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
951bfeeae90SHong Zhang   fortranmultaddaij_(&m,x,ii,idx,v,y,z);
95202ab625aSSatish Balay #else
95317ab2063SBarry Smith   for (i=0; i<m; i++) {
9549ea0dfa2SSatish Balay     jrow = ii[i];
9559ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
95617ab2063SBarry Smith     sum  = y[i];
9579ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
9589ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
9599ea0dfa2SSatish Balay      }
96017ab2063SBarry Smith     z[i] = sum;
96117ab2063SBarry Smith   }
96202ab625aSSatish Balay #endif
963b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
964ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
965ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(yy,&y);CHKERRQ(ierr);
9662e8a6d31SBarry Smith   if (zz != yy) {
967ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(zz,&z);CHKERRQ(ierr);
9682e8a6d31SBarry Smith   }
9693a40ed3dSBarry Smith   PetscFunctionReturn(0);
97017ab2063SBarry Smith }
97117ab2063SBarry Smith 
97217ab2063SBarry Smith /*
97317ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
97417ab2063SBarry Smith */
9754a2ae208SSatish Balay #undef __FUNCT__
9764a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
977f1e2ffcdSBarry Smith int MatMarkDiagonal_SeqAIJ(Mat A)
97817ab2063SBarry Smith {
979416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
980bfeeae90SHong Zhang   int        i,j,*diag,m = A->m,ierr;
98117ab2063SBarry Smith 
9823a40ed3dSBarry Smith   PetscFunctionBegin;
983f1e2ffcdSBarry Smith   if (a->diag) PetscFunctionReturn(0);
984f1e2ffcdSBarry Smith 
985b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&diag);CHKERRQ(ierr);
986b0a32e0cSBarry Smith   PetscLogObjectMemory(A,(m+1)*sizeof(int));
987273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
98835b0346bSBarry Smith     diag[i] = a->i[i+1];
989bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
990bfeeae90SHong Zhang       if (a->j[j] == i) {
991bfeeae90SHong Zhang         diag[i] = j;
99217ab2063SBarry Smith         break;
99317ab2063SBarry Smith       }
99417ab2063SBarry Smith     }
99517ab2063SBarry Smith   }
996416022c9SBarry Smith   a->diag = diag;
9973a40ed3dSBarry Smith   PetscFunctionReturn(0);
99817ab2063SBarry Smith }
99917ab2063SBarry Smith 
1000be5855fcSBarry Smith /*
1001be5855fcSBarry Smith      Checks for missing diagonals
1002be5855fcSBarry Smith */
10034a2ae208SSatish Balay #undef __FUNCT__
10044a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1005f1e2ffcdSBarry Smith int MatMissingDiagonal_SeqAIJ(Mat A)
1006be5855fcSBarry Smith {
1007be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1008bfeeae90SHong Zhang   int        *diag,*jj = a->j,i,ierr;
1009be5855fcSBarry Smith 
1010be5855fcSBarry Smith   PetscFunctionBegin;
1011f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1012f1e2ffcdSBarry Smith   diag = a->diag;
1013273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
1014bfeeae90SHong Zhang     if (jj[diag[i]] != i) {
101529bbc08cSBarry Smith       SETERRQ1(1,"Matrix is missing diagonal number %d",i);
1016be5855fcSBarry Smith     }
1017be5855fcSBarry Smith   }
1018be5855fcSBarry Smith   PetscFunctionReturn(0);
1019be5855fcSBarry Smith }
1020be5855fcSBarry Smith 
10214a2ae208SSatish Balay #undef __FUNCT__
10224a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
1023c14dc6b6SHong Zhang int MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx)
102417ab2063SBarry Smith {
1025416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1026*beeb8507SBarry Smith   PetscScalar        *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag;
1027*beeb8507SBarry Smith   const PetscScalar  *v = a->a, *b, *bs,*xb, *ts;
1028*beeb8507SBarry Smith   int                ierr,n = A->n,m = A->m,i;
1029*beeb8507SBarry Smith   const int          *idx,*diag;
103017ab2063SBarry Smith 
10313a40ed3dSBarry Smith   PetscFunctionBegin;
1032b965ef7fSBarry Smith   its = its*lits;
103391723122SBarry Smith   if (its <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits);
103491723122SBarry Smith 
1035ed480e8bSBarry Smith   if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);}
1036ed480e8bSBarry Smith   diag = a->diag;
1037ed480e8bSBarry Smith   if (!a->idiag) {
1038ed480e8bSBarry Smith     ierr     = PetscMalloc(3*m*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr);
1039ed480e8bSBarry Smith     a->ssor  = a->idiag + m;
1040ed480e8bSBarry Smith     mdiag    = a->ssor + m;
1041ed480e8bSBarry Smith 
1042ed480e8bSBarry Smith     v        = a->a;
1043ed480e8bSBarry Smith 
1044ed480e8bSBarry Smith     /* this is wrong when fshift omega changes each iteration */
1045ed480e8bSBarry Smith     if (omega == 1.0 && fshift == 0.0) {
1046ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1047ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1048ed480e8bSBarry Smith         a->idiag[i] = 1.0/v[diag[i]];
1049ed480e8bSBarry Smith       }
1050*beeb8507SBarry Smith       PetscLogFlops(m);
1051ed480e8bSBarry Smith     } else {
1052ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1053ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1054*beeb8507SBarry Smith         a->idiag[i] = omega/(fshift + v[diag[i]]);
1055ed480e8bSBarry Smith       }
1056*beeb8507SBarry Smith       PetscLogFlops(2*m);
1057*beeb8507SBarry Smith     }
1058ed480e8bSBarry Smith   }
1059ed480e8bSBarry Smith   t     = a->ssor;
1060ed480e8bSBarry Smith   idiag = a->idiag;
1061ed480e8bSBarry Smith   mdiag = a->idiag + 2*m;
1062ed480e8bSBarry Smith 
1063ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
1064fb2e594dSBarry Smith   if (xx != bb) {
1065*beeb8507SBarry Smith     ierr = VecGetArrayFast(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1066fb2e594dSBarry Smith   } else {
1067fb2e594dSBarry Smith     b = x;
1068fb2e594dSBarry Smith   }
1069fb2e594dSBarry Smith 
1070ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1071ed480e8bSBarry Smith   xs   = x;
107217ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
107317ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1074ed480e8bSBarry Smith     bs = b;
107517ab2063SBarry Smith     for (i=0; i<m; i++) {
1076ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1077416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1078ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1079ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
108017ab2063SBarry Smith         sum  = b[i]*d/omega;
108117ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
108217ab2063SBarry Smith         x[i] = sum;
108317ab2063SBarry Smith     }
1084ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
1085*beeb8507SBarry Smith     if (bb != xx) {ierr = VecRestoreArrayFast(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1086ed480e8bSBarry Smith     PetscLogFlops(a->nz);
10873a40ed3dSBarry Smith     PetscFunctionReturn(0);
108817ab2063SBarry Smith   }
1089c783ea89SBarry Smith 
1090ed480e8bSBarry Smith 
1091fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1092fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1093fc3d8934SBarry Smith 
1094fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1095fc3d8934SBarry Smith 
1096fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1097fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
109848af12d7SBarry Smith     is the relaxation factor.
1099fc3d8934SBarry Smith     */
1100fc3d8934SBarry Smith 
110148af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
110229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
11033a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
110417ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
110517ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
110617ab2063SBarry Smith 
110717ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
110817ab2063SBarry Smith 
110917ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
111017ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
111117ab2063SBarry Smith     is the relaxation factor.
111217ab2063SBarry Smith     */
111317ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
111417ab2063SBarry Smith 
111517ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
111617ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1117416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1118ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1119ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
112017ab2063SBarry Smith       sum  = b[i];
112117ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1122ed480e8bSBarry Smith       x[i] = sum*idiag[i];
112317ab2063SBarry Smith     }
112417ab2063SBarry Smith 
112517ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1126416022c9SBarry Smith     v = a->a;
1127ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
112817ab2063SBarry Smith 
112917ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1130ed480e8bSBarry Smith     ts = t;
1131416022c9SBarry Smith     diag = a->diag;
113217ab2063SBarry Smith     for (i=0; i<m; i++) {
1133416022c9SBarry Smith       n    = diag[i] - a->i[i];
1134ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1135ed480e8bSBarry Smith       v    = a->a + a->i[i];
113617ab2063SBarry Smith       sum  = t[i];
113717ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
1138ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1139733d66baSBarry Smith       /*  x = x + t */
1140733d66baSBarry Smith       x[i] += t[i];
114117ab2063SBarry Smith     }
114217ab2063SBarry Smith 
1143b0a32e0cSBarry Smith     PetscLogFlops(6*m-1 + 2*a->nz);
1144ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
1145*beeb8507SBarry Smith     if (bb != xx) {ierr = VecRestoreArrayFast(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
11463a40ed3dSBarry Smith     PetscFunctionReturn(0);
114717ab2063SBarry Smith   }
114817ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
114917ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
115077d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
1151*beeb8507SBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,idiag,a->a,(void*)b);
115277d8c4bbSBarry Smith #else
115317ab2063SBarry Smith       for (i=0; i<m; i++) {
1154416022c9SBarry Smith         n    = diag[i] - a->i[i];
1155ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1156ed480e8bSBarry Smith         v    = a->a + a->i[i];
115717ab2063SBarry Smith         sum  = b[i];
115817ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1159ed480e8bSBarry Smith         x[i] = sum*idiag[i];
116017ab2063SBarry Smith       }
116177d8c4bbSBarry Smith #endif
116217ab2063SBarry Smith       xb = x;
1163ed480e8bSBarry Smith       PetscLogFlops(a->nz);
11643a40ed3dSBarry Smith     } else xb = b;
116517ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
116617ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
116717ab2063SBarry Smith       for (i=0; i<m; i++) {
1168ed480e8bSBarry Smith         x[i] *= mdiag[i];
116917ab2063SBarry Smith       }
1170b0a32e0cSBarry Smith       PetscLogFlops(m);
117117ab2063SBarry Smith     }
117217ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
117377d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
1174*beeb8507SBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,idiag,a->a,(void*)xb);
117577d8c4bbSBarry Smith #else
117617ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1177416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1178ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1179ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
118017ab2063SBarry Smith         sum  = xb[i];
118117ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1182ed480e8bSBarry Smith         x[i] = sum*idiag[i];
118317ab2063SBarry Smith       }
118477d8c4bbSBarry Smith #endif
1185ed480e8bSBarry Smith       PetscLogFlops(a->nz);
118617ab2063SBarry Smith     }
118717ab2063SBarry Smith     its--;
118817ab2063SBarry Smith   }
118917ab2063SBarry Smith   while (its--) {
119017ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
119177d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
1192*beeb8507SBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,diag,a->a,(void*)b);
119377d8c4bbSBarry Smith #else
119417ab2063SBarry Smith       for (i=0; i<m; i++) {
1195ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1196416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1197ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1198ed480e8bSBarry Smith         v    = a->a + a->i[i];
119917ab2063SBarry Smith         sum  = b[i];
120017ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1201ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
120217ab2063SBarry Smith       }
120377d8c4bbSBarry Smith #endif
1204ed480e8bSBarry Smith       PetscLogFlops(a->nz);
120517ab2063SBarry Smith     }
120617ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
120777d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
1208*beeb8507SBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,diag,a->a,(void*)b);
120977d8c4bbSBarry Smith #else
121017ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1211ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1212416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1213ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1214ed480e8bSBarry Smith         v    = a->a + a->i[i];
121517ab2063SBarry Smith         sum  = b[i];
121617ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1217ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
121817ab2063SBarry Smith       }
121977d8c4bbSBarry Smith #endif
1220ed480e8bSBarry Smith       PetscLogFlops(a->nz);
122117ab2063SBarry Smith     }
122217ab2063SBarry Smith   }
1223ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
1224*beeb8507SBarry Smith   if (bb != xx) {ierr = VecRestoreArrayFast(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12253a40ed3dSBarry Smith   PetscFunctionReturn(0);
122617ab2063SBarry Smith }
122717ab2063SBarry Smith 
12284a2ae208SSatish Balay #undef __FUNCT__
12294a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
12308f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
123117ab2063SBarry Smith {
1232416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12334e220ebcSLois Curfman McInnes 
12343a40ed3dSBarry Smith   PetscFunctionBegin;
1235273d9f13SBarry Smith   info->rows_global    = (double)A->m;
1236273d9f13SBarry Smith   info->columns_global = (double)A->n;
1237273d9f13SBarry Smith   info->rows_local     = (double)A->m;
1238273d9f13SBarry Smith   info->columns_local  = (double)A->n;
12394e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
12404e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
12414e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
12424e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
12434e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
12444e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
12454e220ebcSLois Curfman McInnes   info->memory         = A->mem;
12464e220ebcSLois Curfman McInnes   if (A->factor) {
12474e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
12484e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
12494e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
12504e220ebcSLois Curfman McInnes   } else {
12514e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12524e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12534e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12544e220ebcSLois Curfman McInnes   }
12553a40ed3dSBarry Smith   PetscFunctionReturn(0);
125617ab2063SBarry Smith }
125717ab2063SBarry Smith 
1258b380c88cSHong Zhang EXTERN int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,MatFactorInfo*,Mat*);
1259ca44d042SBarry Smith EXTERN int MatLUFactorNumeric_SeqAIJ(Mat,Mat*);
1260b380c88cSHong Zhang EXTERN int MatLUFactor_SeqAIJ(Mat,IS,IS,MatFactorInfo*);
1261ca44d042SBarry Smith EXTERN int MatSolve_SeqAIJ(Mat,Vec,Vec);
1262ca44d042SBarry Smith EXTERN int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec);
1263ca44d042SBarry Smith EXTERN int MatSolveTranspose_SeqAIJ(Mat,Vec,Vec);
1264ca44d042SBarry Smith EXTERN int MatSolveTransposeAdd_SeqAIJ(Mat,Vec,Vec,Vec);
126517ab2063SBarry Smith 
12664a2ae208SSatish Balay #undef __FUNCT__
12674a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1268268466fbSBarry Smith int MatZeroRows_SeqAIJ(Mat A,IS is,const PetscScalar *diag)
126917ab2063SBarry Smith {
1270416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1271bfeeae90SHong Zhang   int         i,ierr,N,*rows,m = A->m - 1;
127217ab2063SBarry Smith 
12733a40ed3dSBarry Smith   PetscFunctionBegin;
1274b9b97703SBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
127517ab2063SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
1276f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1277f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
127829bbc08cSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
1279bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1280f1e2ffcdSBarry Smith     }
1281f1e2ffcdSBarry Smith     if (diag) {
1282f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1283f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1284f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1285f1e2ffcdSBarry Smith         a->a[a->diag[rows[i]]] = *diag;
1286f1e2ffcdSBarry Smith       }
1287f1e2ffcdSBarry Smith     }
1288f1e2ffcdSBarry Smith   } else {
128917ab2063SBarry Smith     if (diag) {
129017ab2063SBarry Smith       for (i=0; i<N; i++) {
129129bbc08cSBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
12927ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1293416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1294bfeeae90SHong Zhang           a->a[a->i[rows[i]]] = *diag;
1295bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
12967ae801bdSBarry Smith         } else { /* in case row was completely empty */
1297d64ed03dSBarry Smith           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr);
129817ab2063SBarry Smith         }
129917ab2063SBarry Smith       }
13003a40ed3dSBarry Smith     } else {
130117ab2063SBarry Smith       for (i=0; i<N; i++) {
130229bbc08cSBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range");
1303416022c9SBarry Smith         a->ilen[rows[i]] = 0;
130417ab2063SBarry Smith       }
130517ab2063SBarry Smith     }
1306f1e2ffcdSBarry Smith   }
13077ae801bdSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
130843a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13093a40ed3dSBarry Smith   PetscFunctionReturn(0);
131017ab2063SBarry Smith }
131117ab2063SBarry Smith 
13124a2ae208SSatish Balay #undef __FUNCT__
13134a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
131487828ca2SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,PetscScalar **v)
131517ab2063SBarry Smith {
1316416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1317b3fb0a6cSMatthew Knepley   int        *itmp;
131817ab2063SBarry Smith 
13193a40ed3dSBarry Smith   PetscFunctionBegin;
1320273d9f13SBarry Smith   if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %d out of range",row);
132117ab2063SBarry Smith 
1322416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1323bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
132417ab2063SBarry Smith   if (idx) {
1325bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1326bfeeae90SHong Zhang     if (*nz) {
13274e093b46SBarry Smith       *idx = itmp;
132817ab2063SBarry Smith     }
132917ab2063SBarry Smith     else *idx = 0;
133017ab2063SBarry Smith   }
13313a40ed3dSBarry Smith   PetscFunctionReturn(0);
133217ab2063SBarry Smith }
133317ab2063SBarry Smith 
1334bfeeae90SHong Zhang /* remove this function? */
13354a2ae208SSatish Balay #undef __FUNCT__
13364a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
133787828ca2SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,PetscScalar **v)
133817ab2063SBarry Smith {
1339bfeeae90SHong Zhang   /* Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1340bfeeae90SHong Zhang      int ierr; */
13413a40ed3dSBarry Smith 
13423a40ed3dSBarry Smith   PetscFunctionBegin;
1343bfeeae90SHong Zhang   /* if (idx) {if (*idx && a->indexshift) {ierr = PetscFree(*idx);CHKERRQ(ierr);}} */
13443a40ed3dSBarry Smith   PetscFunctionReturn(0);
134517ab2063SBarry Smith }
134617ab2063SBarry Smith 
13474a2ae208SSatish Balay #undef __FUNCT__
13484a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1349064f8208SBarry Smith int MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
135017ab2063SBarry Smith {
1351416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
135287828ca2SBarry Smith   PetscScalar  *v = a->a;
135336db0b34SBarry Smith   PetscReal    sum = 0.0;
1354bfeeae90SHong Zhang   int          i,j,ierr;
135517ab2063SBarry Smith 
13563a40ed3dSBarry Smith   PetscFunctionBegin;
135717ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1358416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1359aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
136036db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
136117ab2063SBarry Smith #else
136217ab2063SBarry Smith       sum += (*v)*(*v); v++;
136317ab2063SBarry Smith #endif
136417ab2063SBarry Smith     }
1365064f8208SBarry Smith     *nrm = sqrt(sum);
13663a40ed3dSBarry Smith   } else if (type == NORM_1) {
136736db0b34SBarry Smith     PetscReal *tmp;
1368416022c9SBarry Smith     int    *jj = a->j;
1369b0a32e0cSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1370273d9f13SBarry Smith     ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr);
1371064f8208SBarry Smith     *nrm = 0.0;
1372416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1373bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
137417ab2063SBarry Smith     }
1375273d9f13SBarry Smith     for (j=0; j<A->n; j++) {
1376064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
137717ab2063SBarry Smith     }
1378606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
13793a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1380064f8208SBarry Smith     *nrm = 0.0;
1381273d9f13SBarry Smith     for (j=0; j<A->m; j++) {
1382bfeeae90SHong Zhang       v = a->a + a->i[j];
138317ab2063SBarry Smith       sum = 0.0;
1384416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1385cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
138617ab2063SBarry Smith       }
1387064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
138817ab2063SBarry Smith     }
13893a40ed3dSBarry Smith   } else {
139029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
139117ab2063SBarry Smith   }
13923a40ed3dSBarry Smith   PetscFunctionReturn(0);
139317ab2063SBarry Smith }
139417ab2063SBarry Smith 
13954a2ae208SSatish Balay #undef __FUNCT__
13964a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
13978f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B)
139817ab2063SBarry Smith {
1399416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
1400416022c9SBarry Smith   Mat          C;
1401273d9f13SBarry Smith   int          i,ierr,*aj = a->j,*ai = a->i,m = A->m,len,*col;
140287828ca2SBarry Smith   PetscScalar  *array = a->a;
140317ab2063SBarry Smith 
14043a40ed3dSBarry Smith   PetscFunctionBegin;
1405273d9f13SBarry Smith   if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1406b0a32e0cSBarry Smith   ierr = PetscMalloc((1+A->n)*sizeof(int),&col);CHKERRQ(ierr);
1407273d9f13SBarry Smith   ierr = PetscMemzero(col,(1+A->n)*sizeof(int));CHKERRQ(ierr);
1408bfeeae90SHong Zhang 
1409bfeeae90SHong Zhang   for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
1410273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,A->n,m,0,col,&C);CHKERRQ(ierr);
1411606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
141217ab2063SBarry Smith   for (i=0; i<m; i++) {
141317ab2063SBarry Smith     len    = ai[i+1]-ai[i];
1414416022c9SBarry Smith     ierr   = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1415b9b97703SBarry Smith     array += len;
1416b9b97703SBarry Smith     aj    += len;
141717ab2063SBarry Smith   }
141817ab2063SBarry Smith 
14196d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14206d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
142117ab2063SBarry Smith 
1422f1e2ffcdSBarry Smith   if (B) {
1423416022c9SBarry Smith     *B = C;
142417ab2063SBarry Smith   } else {
1425273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
142617ab2063SBarry Smith   }
14273a40ed3dSBarry Smith   PetscFunctionReturn(0);
142817ab2063SBarry Smith }
142917ab2063SBarry Smith 
1430cd0d46ebSvictorle EXTERN_C_BEGIN
1431cd0d46ebSvictorle #undef __FUNCT__
1432cd0d46ebSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1433cd0d46ebSvictorle int MatIsSymmetric_SeqAIJ(Mat A,Mat B,PetscTruth *f)
1434cd0d46ebSvictorle {
1435cd0d46ebSvictorle   Mat_SeqAIJ *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
1436cd0d46ebSvictorle   int *adx,*bdx,*aii,*bii,*aptr,*bptr; PetscScalar *va,*vb;
1437cd0d46ebSvictorle   int ma,na,mb,nb, i,ierr;
1438cd0d46ebSvictorle 
1439cd0d46ebSvictorle   PetscFunctionBegin;
1440cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1441cd0d46ebSvictorle 
1442cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na); CHKERRQ(ierr);
1443cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb); CHKERRQ(ierr);
1444cd0d46ebSvictorle   if (ma!=nb || na!=mb)
1445cd0d46ebSvictorle     SETERRQ(1,"Incompatible A/B sizes for symmetry test");
1446cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1447cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1448cd0d46ebSvictorle   va = aij->a; vb = bij->a;
1449cd0d46ebSvictorle   ierr = PetscMalloc(ma*sizeof(int),&aptr); CHKERRQ(ierr);
1450cd0d46ebSvictorle   ierr = PetscMalloc(mb*sizeof(int),&bptr); CHKERRQ(ierr);
1451cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1452cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1453cd0d46ebSvictorle 
1454cd0d46ebSvictorle   *f = PETSC_TRUE;
1455cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1456cd0d46ebSvictorle     /*printf("row %d spans %d--%d; we start @ %d\n",
1457cd0d46ebSvictorle       i,idx[ii[i]],idx[ii[i+1]-1],idx[aptr[i]]);*/
1458cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
1459cd0d46ebSvictorle       int idc,idr; PetscScalar vc,vr;
1460cd0d46ebSvictorle       /* column/row index/value */
1461cd0d46ebSvictorle       idc = adx[aptr[i]]; idr = bdx[bptr[idc]];
1462cd0d46ebSvictorle       vc = va[aptr[i]]; vr = vb[bptr[idc]];
1463cd0d46ebSvictorle       /*printf("comparing %d: (%d,%d)=%e to (%d,%d)=%e\n",
1464cd0d46ebSvictorle 	aptr[i],i,idc,vc,idc,idr,vr);*/
1465cd0d46ebSvictorle       if (i!=idr || vc!=vr) {
1466cd0d46ebSvictorle 	*f = PETSC_FALSE; goto done;
1467cd0d46ebSvictorle       } else {
14683aeef889SHong Zhang 	aptr[i]++; if (B || i!=idc) bptr[idc]++;
1469cd0d46ebSvictorle       }
1470cd0d46ebSvictorle     }
1471cd0d46ebSvictorle   }
1472cd0d46ebSvictorle  done:
1473cd0d46ebSvictorle   ierr = PetscFree(aptr); CHKERRQ(ierr);
14743aeef889SHong Zhang   if (B) {
14753aeef889SHong Zhang     ierr = PetscFree(bptr); CHKERRQ(ierr);
14763aeef889SHong Zhang   }
1477cd0d46ebSvictorle 
1478cd0d46ebSvictorle   PetscFunctionReturn(0);
1479cd0d46ebSvictorle }
1480cd0d46ebSvictorle EXTERN_C_END
1481cd0d46ebSvictorle 
14824a2ae208SSatish Balay #undef __FUNCT__
14834a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
14848f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
148517ab2063SBarry Smith {
1486416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
148787828ca2SBarry Smith   PetscScalar  *l,*r,x,*v;
1488bfeeae90SHong Zhang   int          ierr,i,j,m = A->m,n = A->n,M,nz = a->nz,*jj;
148917ab2063SBarry Smith 
14903a40ed3dSBarry Smith   PetscFunctionBegin;
149117ab2063SBarry Smith   if (ll) {
14923ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
14933ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1494e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1495273d9f13SBarry Smith     if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
1496ed480e8bSBarry Smith     ierr = VecGetArrayFast(ll,&l);CHKERRQ(ierr);
1497416022c9SBarry Smith     v = a->a;
149817ab2063SBarry Smith     for (i=0; i<m; i++) {
149917ab2063SBarry Smith       x = l[i];
1500416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
150117ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
150217ab2063SBarry Smith     }
1503ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(ll,&l);CHKERRQ(ierr);
1504b0a32e0cSBarry Smith     PetscLogFlops(nz);
150517ab2063SBarry Smith   }
150617ab2063SBarry Smith   if (rr) {
1507e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1508273d9f13SBarry Smith     if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
1509ed480e8bSBarry Smith     ierr = VecGetArrayFast(rr,&r);CHKERRQ(ierr);
1510416022c9SBarry Smith     v = a->a; jj = a->j;
151117ab2063SBarry Smith     for (i=0; i<nz; i++) {
1512bfeeae90SHong Zhang       (*v++) *= r[*jj++];
151317ab2063SBarry Smith     }
1514ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(rr,&r);CHKERRQ(ierr);
1515b0a32e0cSBarry Smith     PetscLogFlops(nz);
151617ab2063SBarry Smith   }
15173a40ed3dSBarry Smith   PetscFunctionReturn(0);
151817ab2063SBarry Smith }
151917ab2063SBarry Smith 
15204a2ae208SSatish Balay #undef __FUNCT__
15214a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
15227b2a1423SBarry Smith int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,int csize,MatReuse scall,Mat *B)
152317ab2063SBarry Smith {
1524db02288aSLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data,*c;
1525273d9f13SBarry Smith   int          *smap,i,k,kstart,kend,ierr,oldcols = A->n,*lens;
152636db0b34SBarry Smith   int          row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
1527bfeeae90SHong Zhang   int          *irow,*icol,nrows,ncols;
152899141d43SSatish Balay   int          *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
152987828ca2SBarry Smith   PetscScalar  *a_new,*mat_a;
1530416022c9SBarry Smith   Mat          C;
1531fee21e36SBarry Smith   PetscTruth   stride;
153217ab2063SBarry Smith 
15333a40ed3dSBarry Smith   PetscFunctionBegin;
1534d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
153529bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1536d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
153729bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
153899141d43SSatish Balay 
153917ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1540b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1541b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
154217ab2063SBarry Smith 
1543fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1544fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1545fee21e36SBarry Smith   if (stride && step == 1) {
154602834360SBarry Smith     /* special case of contiguous rows */
154731ebf83bSSatish Balay     ierr   = PetscMalloc((2*nrows+1)*sizeof(int),&lens);CHKERRQ(ierr);
154831ebf83bSSatish Balay     starts = lens + nrows;
154902834360SBarry Smith     /* loop over new rows determining lens and starting points */
155002834360SBarry Smith     for (i=0; i<nrows; i++) {
1551bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1552a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
155302834360SBarry Smith       for (k=kstart; k<kend; k++) {
1554bfeeae90SHong Zhang         if (aj[k] >= first) {
155502834360SBarry Smith           starts[i] = k;
155602834360SBarry Smith           break;
155702834360SBarry Smith 	}
155802834360SBarry Smith       }
1559a2744918SBarry Smith       sum = 0;
156002834360SBarry Smith       while (k < kend) {
1561bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1562a2744918SBarry Smith         sum++;
156302834360SBarry Smith       }
1564a2744918SBarry Smith       lens[i] = sum;
156502834360SBarry Smith     }
156602834360SBarry Smith     /* create submatrix */
1567cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
156808480c60SBarry Smith       int n_cols,n_rows;
156908480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
157029bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1571d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
157208480c60SBarry Smith       C = *B;
15733a40ed3dSBarry Smith     } else {
157402834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
157508480c60SBarry Smith     }
1576db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1577db02288aSLois Curfman McInnes 
157802834360SBarry Smith     /* loop over rows inserting into submatrix */
1579db02288aSLois Curfman McInnes     a_new    = c->a;
1580db02288aSLois Curfman McInnes     j_new    = c->j;
1581db02288aSLois Curfman McInnes     i_new    = c->i;
1582bfeeae90SHong Zhang 
158302834360SBarry Smith     for (i=0; i<nrows; i++) {
1584a2744918SBarry Smith       ii    = starts[i];
1585a2744918SBarry Smith       lensi = lens[i];
1586a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1587a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
158802834360SBarry Smith       }
158987828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1590a2744918SBarry Smith       a_new      += lensi;
1591a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1592a2744918SBarry Smith       c->ilen[i]  = lensi;
159302834360SBarry Smith     }
1594606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
15953a40ed3dSBarry Smith   } else {
159602834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
1597b0a32e0cSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(int),&smap);CHKERRQ(ierr);
1598bfeeae90SHong Zhang 
1599b0a32e0cSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(int),&lens);CHKERRQ(ierr);
1600549d3d68SSatish Balay     ierr  = PetscMemzero(smap,oldcols*sizeof(int));CHKERRQ(ierr);
160117ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
160202834360SBarry Smith     /* determine lens of each row */
160302834360SBarry Smith     for (i=0; i<nrows; i++) {
1604bfeeae90SHong Zhang       kstart  = ai[irow[i]];
160502834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
160602834360SBarry Smith       lens[i] = 0;
160702834360SBarry Smith       for (k=kstart; k<kend; k++) {
1608bfeeae90SHong Zhang         if (smap[aj[k]]) {
160902834360SBarry Smith           lens[i]++;
161002834360SBarry Smith         }
161102834360SBarry Smith       }
161202834360SBarry Smith     }
161317ab2063SBarry Smith     /* Create and fill new matrix */
1614a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
16150f5bd95cSBarry Smith       PetscTruth equal;
16160f5bd95cSBarry Smith 
161799141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1618273d9f13SBarry Smith       if ((*B)->m  != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1619273d9f13SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(int),&equal);CHKERRQ(ierr);
16200f5bd95cSBarry Smith       if (!equal) {
162129bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
162299141d43SSatish Balay       }
1623273d9f13SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(int));CHKERRQ(ierr);
162408480c60SBarry Smith       C = *B;
16253a40ed3dSBarry Smith     } else {
162602834360SBarry Smith       ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr);
162708480c60SBarry Smith     }
162899141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
162917ab2063SBarry Smith     for (i=0; i<nrows; i++) {
163099141d43SSatish Balay       row    = irow[i];
1631bfeeae90SHong Zhang       kstart = ai[row];
163299141d43SSatish Balay       kend   = kstart + a->ilen[row];
1633bfeeae90SHong Zhang       mat_i  = c->i[i];
163499141d43SSatish Balay       mat_j  = c->j + mat_i;
163599141d43SSatish Balay       mat_a  = c->a + mat_i;
163699141d43SSatish Balay       mat_ilen = c->ilen + i;
163717ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1638bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1639ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
164099141d43SSatish Balay           *mat_a++ = a->a[k];
164199141d43SSatish Balay           (*mat_ilen)++;
164299141d43SSatish Balay 
164317ab2063SBarry Smith         }
164417ab2063SBarry Smith       }
164517ab2063SBarry Smith     }
164602834360SBarry Smith     /* Free work space */
164702834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1648606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1649606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
165002834360SBarry Smith   }
16516d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16526d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
165317ab2063SBarry Smith 
165417ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1655416022c9SBarry Smith   *B = C;
16563a40ed3dSBarry Smith   PetscFunctionReturn(0);
165717ab2063SBarry Smith }
165817ab2063SBarry Smith 
1659a871dcd8SBarry Smith /*
1660a871dcd8SBarry Smith */
16614a2ae208SSatish Balay #undef __FUNCT__
16624a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
1663b380c88cSHong Zhang int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info)
1664a871dcd8SBarry Smith {
166563b91edcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
166608480c60SBarry Smith   int        ierr;
166763b91edcSBarry Smith   Mat        outA;
1668b8a78c4aSBarry Smith   PetscTruth row_identity,col_identity;
166963b91edcSBarry Smith 
16703a40ed3dSBarry Smith   PetscFunctionBegin;
1671d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1672b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1673b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1674b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
167529bbc08cSBarry Smith     SETERRQ(1,"Row and column permutations must be identity for in-place ILU");
1676b8a78c4aSBarry Smith   }
1677a871dcd8SBarry Smith 
167863b91edcSBarry Smith   outA          = inA;
167963b91edcSBarry Smith   inA->factor   = FACTOR_LU;
168063b91edcSBarry Smith   a->row        = row;
168163b91edcSBarry Smith   a->col        = col;
1682c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1683c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
168463b91edcSBarry Smith 
168536db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1686b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
16874c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
1688b0a32e0cSBarry Smith   PetscLogObjectParent(inA,a->icol);
1689f0ec6fceSSatish Balay 
169094a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
169187828ca2SBarry Smith      ierr = PetscMalloc((inA->m+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
169294a9d846SBarry Smith   }
169363b91edcSBarry Smith 
169408480c60SBarry Smith   if (!a->diag) {
1695f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
169663b91edcSBarry Smith   }
169763b91edcSBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA);CHKERRQ(ierr);
16983a40ed3dSBarry Smith   PetscFunctionReturn(0);
1699a871dcd8SBarry Smith }
1700a871dcd8SBarry Smith 
1701d9eff348SSatish Balay #include "petscblaslapack.h"
17024a2ae208SSatish Balay #undef __FUNCT__
17034a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1704268466fbSBarry Smith int MatScale_SeqAIJ(const PetscScalar *alpha,Mat inA)
1705f0b747eeSBarry Smith {
1706f0b747eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
1707f0b747eeSBarry Smith   int        one = 1;
17083a40ed3dSBarry Smith 
17093a40ed3dSBarry Smith   PetscFunctionBegin;
1710268466fbSBarry Smith   BLscal_(&a->nz,(PetscScalar*)alpha,a->a,&one);
1711b0a32e0cSBarry Smith   PetscLogFlops(a->nz);
17123a40ed3dSBarry Smith   PetscFunctionReturn(0);
1713f0b747eeSBarry Smith }
1714f0b747eeSBarry Smith 
17154a2ae208SSatish Balay #undef __FUNCT__
17164a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
1717268466fbSBarry Smith int MatGetSubMatrices_SeqAIJ(Mat A,int n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1718cddf8d76SBarry Smith {
1719cddf8d76SBarry Smith   int ierr,i;
1720cddf8d76SBarry Smith 
17213a40ed3dSBarry Smith   PetscFunctionBegin;
1722cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1723b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1724cddf8d76SBarry Smith   }
1725cddf8d76SBarry Smith 
1726cddf8d76SBarry Smith   for (i=0; i<n; i++) {
17276a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1728cddf8d76SBarry Smith   }
17293a40ed3dSBarry Smith   PetscFunctionReturn(0);
1730cddf8d76SBarry Smith }
1731cddf8d76SBarry Smith 
17324a2ae208SSatish Balay #undef __FUNCT__
17334a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_SeqAIJ"
17348f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A,int *bs)
17355a838052SSatish Balay {
1736f830108cSBarry Smith   PetscFunctionBegin;
17375a838052SSatish Balay   *bs = 1;
17383a40ed3dSBarry Smith   PetscFunctionReturn(0);
17395a838052SSatish Balay }
17405a838052SSatish Balay 
17414a2ae208SSatish Balay #undef __FUNCT__
17424a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
1743268466fbSBarry Smith int MatIncreaseOverlap_SeqAIJ(Mat A,int is_max,IS is[],int ov)
17444dcbc457SBarry Smith {
1745e4d965acSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1746efb16452SHong Zhang   int        row,i,j,k,l,m,n,*idx,ierr,*nidx,isz,val;
17478a047759SSatish Balay   int        start,end,*ai,*aj;
1748f1af5d2fSBarry Smith   PetscBT    table;
1749bbd702dbSSatish Balay 
17503a40ed3dSBarry Smith   PetscFunctionBegin;
1751273d9f13SBarry Smith   m     = A->m;
1752e4d965acSSatish Balay   ai    = a->i;
1753bfeeae90SHong Zhang   aj    = a->j;
17548a047759SSatish Balay 
175529bbc08cSBarry Smith   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal overlap value used");
175606763907SSatish Balay 
1757b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&nidx);CHKERRQ(ierr);
17586831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
175906763907SSatish Balay 
1760e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1761b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1762e4d965acSSatish Balay     isz  = 0;
17636831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1764e4d965acSSatish Balay 
1765e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
17664dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1767b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1768e4d965acSSatish Balay 
1769dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1770e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1771f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
17724dcbc457SBarry Smith     }
177306763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
177406763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1775e4d965acSSatish Balay 
177604a348a9SBarry Smith     k = 0;
177704a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
177804a348a9SBarry Smith       n = isz;
177906763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1780e4d965acSSatish Balay         row   = nidx[k];
1781e4d965acSSatish Balay         start = ai[row];
1782e4d965acSSatish Balay         end   = ai[row+1];
178304a348a9SBarry Smith         for (l = start; l<end ; l++){
1784efb16452SHong Zhang           val = aj[l] ;
1785f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1786e4d965acSSatish Balay         }
1787e4d965acSSatish Balay       }
1788e4d965acSSatish Balay     }
1789029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1790e4d965acSSatish Balay   }
17916831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1792606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
17933a40ed3dSBarry Smith   PetscFunctionReturn(0);
17944dcbc457SBarry Smith }
179517ab2063SBarry Smith 
17960513a670SBarry Smith /* -------------------------------------------------------------- */
17974a2ae208SSatish Balay #undef __FUNCT__
17984a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
17990513a670SBarry Smith int MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
18000513a670SBarry Smith {
18010513a670SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
180287828ca2SBarry Smith   PetscScalar  *vwork;
1803273d9f13SBarry Smith   int          i,ierr,nz,m = A->m,n = A->n,*cwork;
18040513a670SBarry Smith   int          *row,*col,*cnew,j,*lens;
180556cd22aeSBarry Smith   IS           icolp,irowp;
18060513a670SBarry Smith 
18073a40ed3dSBarry Smith   PetscFunctionBegin;
18084c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
180956cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
18104c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
181156cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
18120513a670SBarry Smith 
18130513a670SBarry Smith   /* determine lengths of permuted rows */
1814b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&lens);CHKERRQ(ierr);
18150513a670SBarry Smith   for (i=0; i<m; i++) {
18160513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
18170513a670SBarry Smith   }
18180513a670SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr);
1819606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
18200513a670SBarry Smith 
1821b0a32e0cSBarry Smith   ierr = PetscMalloc(n*sizeof(int),&cnew);CHKERRQ(ierr);
18220513a670SBarry Smith   for (i=0; i<m; i++) {
18230513a670SBarry Smith     ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18240513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
18250513a670SBarry Smith     ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
18260513a670SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18270513a670SBarry Smith   }
1828606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
18290513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18300513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
183156cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
183256cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
183356cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
183456cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
18353a40ed3dSBarry Smith   PetscFunctionReturn(0);
18360513a670SBarry Smith }
18370513a670SBarry Smith 
18384a2ae208SSatish Balay #undef __FUNCT__
18394a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ"
1840682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A)
1841682d7d0cSBarry Smith {
1842c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1843682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1844d132466eSBarry Smith   int               ierr;
1845682d7d0cSBarry Smith 
18463a40ed3dSBarry Smith   PetscFunctionBegin;
1847c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1848d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1849d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1850d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
1851d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_no_inode: Do not use inodes\n");CHKERRQ(ierr);
1852d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n");CHKERRQ(ierr);
1853aa482453SBarry Smith #if defined(PETSC_HAVE_ESSL)
1854d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_essl: Use IBM sparse LU factorization and solve.\n");CHKERRQ(ierr);
1855682d7d0cSBarry Smith #endif
1856cd5578b5SBarry Smith #if defined(PETSC_HAVE_LUSOL)
1857cd5578b5SBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_lusol: Use the Stanford LUSOL sparse factorization and solve.\n");CHKERRQ(ierr);
1858cd5578b5SBarry Smith #endif
185987828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1860ca44d042SBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_matlab: Use Matlab engine sparse LU factorization and solve.\n");CHKERRQ(ierr);
1861ca44d042SBarry Smith #endif
18623a40ed3dSBarry Smith   PetscFunctionReturn(0);
1863682d7d0cSBarry Smith }
1864ca44d042SBarry Smith EXTERN int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg);
1865ca44d042SBarry Smith EXTERN int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
1866b380c88cSHong Zhang EXTERN int MatILUDTFactor_SeqAIJ(Mat,MatFactorInfo*,IS,IS,Mat*);
18674a2ae208SSatish Balay #undef __FUNCT__
18684a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1869cb5b572fSBarry Smith int MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1870cb5b572fSBarry Smith {
1871be6bf707SBarry Smith   int        ierr;
1872cb5b572fSBarry Smith 
1873cb5b572fSBarry Smith   PetscFunctionBegin;
187433f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
187533f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
1876be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1877be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1878be6bf707SBarry Smith 
1879bfeeae90SHong Zhang     if (a->i[A->m] != b->i[B->m]) {
188029bbc08cSBarry Smith       SETERRQ(1,"Number of nonzeros in two matrices are different");
1881cb5b572fSBarry Smith     }
1882bfeeae90SHong Zhang     ierr = PetscMemcpy(b->a,a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
1883cb5b572fSBarry Smith   } else {
1884cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1885cb5b572fSBarry Smith   }
1886cb5b572fSBarry Smith   PetscFunctionReturn(0);
1887cb5b572fSBarry Smith }
1888cb5b572fSBarry Smith 
18894a2ae208SSatish Balay #undef __FUNCT__
18904a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
1891273d9f13SBarry Smith int MatSetUpPreallocation_SeqAIJ(Mat A)
1892273d9f13SBarry Smith {
1893273d9f13SBarry Smith   int        ierr;
1894273d9f13SBarry Smith 
1895273d9f13SBarry Smith   PetscFunctionBegin;
1896273d9f13SBarry Smith   ierr =  MatSeqAIJSetPreallocation(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
1897273d9f13SBarry Smith   PetscFunctionReturn(0);
1898273d9f13SBarry Smith }
1899273d9f13SBarry Smith 
19004a2ae208SSatish Balay #undef __FUNCT__
19014a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
19024e7234bfSBarry Smith int MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
19036c0721eeSBarry Smith {
19046c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
19056c0721eeSBarry Smith   PetscFunctionBegin;
19066c0721eeSBarry Smith   *array = a->a;
19076c0721eeSBarry Smith   PetscFunctionReturn(0);
19086c0721eeSBarry Smith }
19096c0721eeSBarry Smith 
19104a2ae208SSatish Balay #undef __FUNCT__
19114a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
19124e7234bfSBarry Smith int MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
19136c0721eeSBarry Smith {
19146c0721eeSBarry Smith   PetscFunctionBegin;
19156c0721eeSBarry Smith   PetscFunctionReturn(0);
19166c0721eeSBarry Smith }
1917273d9f13SBarry Smith 
1918ee4f033dSBarry Smith #undef __FUNCT__
1919ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
1920ee4f033dSBarry Smith int MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
1921ee4f033dSBarry Smith {
1922ee4f033dSBarry Smith   int           (*f)(void *,Vec,Vec,void*) = (int (*)(void *,Vec,Vec,void *))coloring->f;
1923ee4f033dSBarry Smith   int           k,ierr,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
192487828ca2SBarry Smith   PetscScalar   dx,mone = -1.0,*y,*xx,*w3_array;
192587828ca2SBarry Smith   PetscScalar   *vscale_array;
1926ee4f033dSBarry Smith   PetscReal     epsilon = coloring->error_rel,umin = coloring->umin;
1927ee4f033dSBarry Smith   Vec           w1,w2,w3;
1928ee4f033dSBarry Smith   void          *fctx = coloring->fctx;
1929ee4f033dSBarry Smith   PetscTruth    flg;
1930ee4f033dSBarry Smith 
1931ee4f033dSBarry Smith   PetscFunctionBegin;
1932ee4f033dSBarry Smith   if (!coloring->w1) {
1933ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
1934ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w1);
1935ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
1936ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w2);
1937ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
1938ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w3);
1939ee4f033dSBarry Smith   }
1940ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
1941ee4f033dSBarry Smith 
1942ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
1943e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(coloring->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
1944ee4f033dSBarry Smith   if (flg) {
1945ee4f033dSBarry Smith     PetscLogInfo(coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n");
1946ee4f033dSBarry Smith   } else {
1947ee4f033dSBarry Smith     ierr = MatZeroEntries(J);CHKERRQ(ierr);
1948ee4f033dSBarry Smith   }
1949ee4f033dSBarry Smith 
1950ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
1951ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
1952ee4f033dSBarry Smith 
1953ee4f033dSBarry Smith   /*
1954ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
1955ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
1956ee4f033dSBarry Smith   */
1957ee4f033dSBarry Smith   if (coloring->F) {
1958ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
1959ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
1960ee4f033dSBarry Smith     if (m1 != m2) {
1961ee4f033dSBarry Smith       coloring->F = 0;
1962ee4f033dSBarry Smith     }
1963ee4f033dSBarry Smith   }
1964ee4f033dSBarry Smith 
1965ee4f033dSBarry Smith   if (coloring->F) {
1966ee4f033dSBarry Smith     w1          = coloring->F;
1967ee4f033dSBarry Smith     coloring->F = 0;
1968ee4f033dSBarry Smith   } else {
196966f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1970ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
197166f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1972ee4f033dSBarry Smith   }
1973ee4f033dSBarry Smith 
1974ee4f033dSBarry Smith   /*
1975ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
1976ee4f033dSBarry Smith   */
1977ed480e8bSBarry Smith   ierr = VecGetArrayFast(x1,&xx);CHKERRQ(ierr);xx = xx - start;
1978ed480e8bSBarry Smith   ierr = VecGetArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
1979ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
1980ee4f033dSBarry Smith     /*
1981ee4f033dSBarry Smith        Loop over each column associated with color adding the
1982ee4f033dSBarry Smith        perturbation to the vector w3.
1983ee4f033dSBarry Smith     */
1984ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
1985ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
1986ee4f033dSBarry Smith       dx  = xx[col];
1987ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
1988ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
1989ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
1990ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
1991ee4f033dSBarry Smith #else
1992ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
1993ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
1994ee4f033dSBarry Smith #endif
1995ee4f033dSBarry Smith       dx                *= epsilon;
1996ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
1997ee4f033dSBarry Smith     }
1998ee4f033dSBarry Smith   }
1999ed480e8bSBarry Smith   vscale_array = vscale_array + start;ierr = VecRestoreArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2000ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2001ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2002ee4f033dSBarry Smith 
2003ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2004ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2005ee4f033dSBarry Smith 
2006ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2007ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2008ee4f033dSBarry Smith 
2009ed480e8bSBarry Smith   ierr = VecGetArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2010ee4f033dSBarry Smith   /*
2011ee4f033dSBarry Smith       Loop over each color
2012ee4f033dSBarry Smith   */
2013ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
201449b058dcSBarry Smith     coloring->currentcolor = k;
2015ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
2016ed480e8bSBarry Smith     ierr = VecGetArrayFast(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2017ee4f033dSBarry Smith     /*
2018ee4f033dSBarry Smith        Loop over each column associated with color adding the
2019ee4f033dSBarry Smith        perturbation to the vector w3.
2020ee4f033dSBarry Smith     */
2021ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2022ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2023ee4f033dSBarry Smith       dx  = xx[col];
2024ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2025ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2026ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2027ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2028ee4f033dSBarry Smith #else
2029ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2030ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2031ee4f033dSBarry Smith #endif
2032ee4f033dSBarry Smith       dx            *= epsilon;
2033ee4f033dSBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(1,"Computed 0 differencing parameter");
2034ee4f033dSBarry Smith       w3_array[col] += dx;
2035ee4f033dSBarry Smith     }
2036ed480e8bSBarry Smith     w3_array = w3_array + start; ierr = VecRestoreArrayFast(w3,&w3_array);CHKERRQ(ierr);
2037ee4f033dSBarry Smith 
2038ee4f033dSBarry Smith     /*
2039ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2040ee4f033dSBarry Smith     */
2041ee4f033dSBarry Smith 
204266f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2043ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
204466f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2045ee4f033dSBarry Smith     ierr = VecAXPY(&mone,w1,w2);CHKERRQ(ierr);
2046ee4f033dSBarry Smith 
2047ee4f033dSBarry Smith     /*
2048ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2049ee4f033dSBarry Smith     */
2050ed480e8bSBarry Smith     ierr = VecGetArrayFast(w2,&y);CHKERRQ(ierr);
2051ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2052ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2053ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2054ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2055ee4f033dSBarry Smith       srow   = row + start;
2056ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2057ee4f033dSBarry Smith     }
2058ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(w2,&y);CHKERRQ(ierr);
2059ee4f033dSBarry Smith   }
206049b058dcSBarry Smith   coloring->currentcolor = k;
2061ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2062ed480e8bSBarry Smith   xx = xx + start; ierr  = VecRestoreArrayFast(x1,&xx);CHKERRQ(ierr);
2063ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2064ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2065ee4f033dSBarry Smith   PetscFunctionReturn(0);
2066ee4f033dSBarry Smith }
2067ee4f033dSBarry Smith 
2068ac90fabeSBarry Smith #include "petscblaslapack.h"
2069ac90fabeSBarry Smith #undef __FUNCT__
2070ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2071268466fbSBarry Smith int MatAXPY_SeqAIJ(const PetscScalar *a,Mat X,Mat Y,MatStructure str)
2072ac90fabeSBarry Smith {
2073c537a176SHong Zhang   int        ierr,one=1,i;
2074ac90fabeSBarry Smith   Mat_SeqAIJ *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
2075ac90fabeSBarry Smith 
2076ac90fabeSBarry Smith   PetscFunctionBegin;
2077ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2078268466fbSBarry Smith     BLaxpy_(&x->nz,(PetscScalar*)a,x->a,&one,y->a,&one);
2079c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2080a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2081a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2082a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2083a30b2313SHong Zhang     }
2084a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
208524f910e3SHong Zhang       ierr = MatAXPYGetxtoy_Private(X->m,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2086a30b2313SHong Zhang       y->XtoY = X;
2087c537a176SHong Zhang     }
2088a30b2313SHong Zhang     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += (*a)*(x->a[i]);
2089e2dd4fc4Svictorle     PetscLogInfo(0,"MatAXPY_SeqAIJ: ratio of nnz(X)/nnz(Y): %d/%d = %g\n",x->nz,y->nz,(PetscReal)(x->nz)/y->nz);
2090ac90fabeSBarry Smith   } else {
2091ac90fabeSBarry Smith     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
2092ac90fabeSBarry Smith   }
2093ac90fabeSBarry Smith   PetscFunctionReturn(0);
2094ac90fabeSBarry Smith }
2095ac90fabeSBarry Smith 
2096682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
20970a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2098cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2099cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2100cb5b572fSBarry Smith        MatMult_SeqAIJ,
2101cb5b572fSBarry Smith        MatMultAdd_SeqAIJ,
21027c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
21037c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2104cb5b572fSBarry Smith        MatSolve_SeqAIJ,
2105cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
21067c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
21077c922b88SBarry Smith        MatSolveTransposeAdd_SeqAIJ,
2108cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2109cb5b572fSBarry Smith        0,
211017ab2063SBarry Smith        MatRelax_SeqAIJ,
211117ab2063SBarry Smith        MatTranspose_SeqAIJ,
2112cb5b572fSBarry Smith        MatGetInfo_SeqAIJ,
2113cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2114cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2115cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2116cb5b572fSBarry Smith        MatNorm_SeqAIJ,
2117cb5b572fSBarry Smith        0,
2118cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
211917ab2063SBarry Smith        MatCompress_SeqAIJ,
2120cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2121cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2122cb5b572fSBarry Smith        MatZeroRows_SeqAIJ,
2123cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2124cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2125f76d2b81SHong Zhang        MatCholeskyFactorSymbolic_SeqAIJ,
2126a6175056SHong Zhang        MatCholeskyFactorNumeric_SeqAIJ,
2127273d9f13SBarry Smith        MatSetUpPreallocation_SeqAIJ,
2128cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2129861ba921SHong Zhang        MatICCFactorSymbolic_SeqAIJ,
21306c0721eeSBarry Smith        MatGetArray_SeqAIJ,
21316c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2132be6bf707SBarry Smith        MatDuplicate_SeqAIJ,
2133cb5b572fSBarry Smith        0,
2134cb5b572fSBarry Smith        0,
2135cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2136cb5b572fSBarry Smith        0,
2137ac90fabeSBarry Smith        MatAXPY_SeqAIJ,
2138cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2139cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2140cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2141cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2142f0b747eeSBarry Smith        MatPrintHelp_SeqAIJ,
2143cb5b572fSBarry Smith        MatScale_SeqAIJ,
2144cb5b572fSBarry Smith        0,
2145cb5b572fSBarry Smith        0,
21466945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
21476945ee14SBarry Smith        MatGetBlockSize_SeqAIJ,
21483b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
21493b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
21503b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2151a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2152a93ec695SBarry Smith        MatFDColoringCreate_SeqAIJ,
2153b9617806SBarry Smith        0,
21540513a670SBarry Smith        0,
2155cda55fadSBarry Smith        MatPermute_SeqAIJ,
2156cda55fadSBarry Smith        0,
2157cda55fadSBarry Smith        0,
2158b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2159b9b97703SBarry Smith        MatView_SeqAIJ,
21608a124369SBarry Smith        MatGetPetscMaps_Petsc,
2161ee4f033dSBarry Smith        0,
2162ee4f033dSBarry Smith        0,
2163ee4f033dSBarry Smith        0,
2164ee4f033dSBarry Smith        0,
2165ee4f033dSBarry Smith        0,
2166ee4f033dSBarry Smith        0,
2167ee4f033dSBarry Smith        0,
2168ee4f033dSBarry Smith        0,
2169ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2170ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2171ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
2172ee4f033dSBarry Smith        MatFDColoringApply_SeqAIJ};
217317ab2063SBarry Smith 
2174fb2e594dSBarry Smith EXTERN_C_BEGIN
21754a2ae208SSatish Balay #undef __FUNCT__
21764a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
217715091d37SBarry Smith 
2178bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,int *indices)
2179bef8e0ddSBarry Smith {
2180bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2181bef8e0ddSBarry Smith   int        i,nz,n;
2182bef8e0ddSBarry Smith 
2183bef8e0ddSBarry Smith   PetscFunctionBegin;
2184bef8e0ddSBarry Smith 
2185bef8e0ddSBarry Smith   nz = aij->maxnz;
2186273d9f13SBarry Smith   n  = mat->n;
2187bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2188bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2189bef8e0ddSBarry Smith   }
2190bef8e0ddSBarry Smith   aij->nz = nz;
2191bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2192bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2193bef8e0ddSBarry Smith   }
2194bef8e0ddSBarry Smith 
2195bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2196bef8e0ddSBarry Smith }
2197fb2e594dSBarry Smith EXTERN_C_END
2198bef8e0ddSBarry Smith 
21994a2ae208SSatish Balay #undef __FUNCT__
22004a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2201bef8e0ddSBarry Smith /*@
2202bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2203bef8e0ddSBarry Smith        in the matrix.
2204bef8e0ddSBarry Smith 
2205bef8e0ddSBarry Smith   Input Parameters:
2206bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2207bef8e0ddSBarry Smith -  indices - the column indices
2208bef8e0ddSBarry Smith 
220915091d37SBarry Smith   Level: advanced
221015091d37SBarry Smith 
2211bef8e0ddSBarry Smith   Notes:
2212bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2213bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2214bef8e0ddSBarry Smith   of the MatSetValues() operation.
2215bef8e0ddSBarry Smith 
2216bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2217bef8e0ddSBarry Smith   MatCreateSeqAIJ().
2218bef8e0ddSBarry Smith 
2219bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2220bef8e0ddSBarry Smith 
2221b9617806SBarry Smith     The indices should start with zero, not one.
2222b9617806SBarry Smith 
2223bef8e0ddSBarry Smith @*/
2224bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices(Mat mat,int *indices)
2225bef8e0ddSBarry Smith {
2226bef8e0ddSBarry Smith   int ierr,(*f)(Mat,int *);
2227bef8e0ddSBarry Smith 
2228bef8e0ddSBarry Smith   PetscFunctionBegin;
2229bef8e0ddSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2230c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2231bef8e0ddSBarry Smith   if (f) {
2232bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2233bef8e0ddSBarry Smith   } else {
223429bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to set column indices");
2235bef8e0ddSBarry Smith   }
2236bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2237bef8e0ddSBarry Smith }
2238bef8e0ddSBarry Smith 
2239be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2240be6bf707SBarry Smith 
2241fb2e594dSBarry Smith EXTERN_C_BEGIN
22424a2ae208SSatish Balay #undef __FUNCT__
22434a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2244be6bf707SBarry Smith int MatStoreValues_SeqAIJ(Mat mat)
2245be6bf707SBarry Smith {
2246be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2247bfeeae90SHong Zhang   size_t       nz = aij->i[mat->m],ierr;
2248be6bf707SBarry Smith 
2249be6bf707SBarry Smith   PetscFunctionBegin;
2250be6bf707SBarry Smith   if (aij->nonew != 1) {
225129bbc08cSBarry Smith     SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2252be6bf707SBarry Smith   }
2253be6bf707SBarry Smith 
2254be6bf707SBarry Smith   /* allocate space for values if not already there */
2255be6bf707SBarry Smith   if (!aij->saved_values) {
225687828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
2257be6bf707SBarry Smith   }
2258be6bf707SBarry Smith 
2259be6bf707SBarry Smith   /* copy values over */
226087828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2261be6bf707SBarry Smith   PetscFunctionReturn(0);
2262be6bf707SBarry Smith }
2263fb2e594dSBarry Smith EXTERN_C_END
2264be6bf707SBarry Smith 
22654a2ae208SSatish Balay #undef __FUNCT__
2266b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2267be6bf707SBarry Smith /*@
2268be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2269be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2270be6bf707SBarry Smith        nonlinear portion.
2271be6bf707SBarry Smith 
2272be6bf707SBarry Smith    Collect on Mat
2273be6bf707SBarry Smith 
2274be6bf707SBarry Smith   Input Parameters:
2275be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2276be6bf707SBarry Smith 
227715091d37SBarry Smith   Level: advanced
227815091d37SBarry Smith 
2279be6bf707SBarry Smith   Common Usage, with SNESSolve():
2280be6bf707SBarry Smith $    Create Jacobian matrix
2281be6bf707SBarry Smith $    Set linear terms into matrix
2282be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2283be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2284be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2285be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2286be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2287be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2288be6bf707SBarry Smith $    In your Jacobian routine
2289be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2290be6bf707SBarry Smith $      Set nonlinear terms in matrix
2291be6bf707SBarry Smith 
2292be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2293be6bf707SBarry Smith $    // build linear portion of Jacobian
2294be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2295be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2296be6bf707SBarry Smith $    loop over nonlinear iterations
2297be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2298be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2299be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2300be6bf707SBarry Smith $       Solve linear system with Jacobian
2301be6bf707SBarry Smith $    endloop
2302be6bf707SBarry Smith 
2303be6bf707SBarry Smith   Notes:
2304be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2305be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2306be6bf707SBarry Smith     calling this routine.
2307be6bf707SBarry Smith 
2308be6bf707SBarry Smith .seealso: MatRetrieveValues()
2309be6bf707SBarry Smith 
2310be6bf707SBarry Smith @*/
2311be6bf707SBarry Smith int MatStoreValues(Mat mat)
2312be6bf707SBarry Smith {
2313be6bf707SBarry Smith   int ierr,(*f)(Mat);
2314be6bf707SBarry Smith 
2315be6bf707SBarry Smith   PetscFunctionBegin;
2316be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
231729bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
231829bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2319be6bf707SBarry Smith 
2320c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2321be6bf707SBarry Smith   if (f) {
2322be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2323be6bf707SBarry Smith   } else {
232429bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to store values");
2325be6bf707SBarry Smith   }
2326be6bf707SBarry Smith   PetscFunctionReturn(0);
2327be6bf707SBarry Smith }
2328be6bf707SBarry Smith 
2329fb2e594dSBarry Smith EXTERN_C_BEGIN
23304a2ae208SSatish Balay #undef __FUNCT__
23314a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2332be6bf707SBarry Smith int MatRetrieveValues_SeqAIJ(Mat mat)
2333be6bf707SBarry Smith {
2334be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2335bfeeae90SHong Zhang   int        nz = aij->i[mat->m],ierr;
2336be6bf707SBarry Smith 
2337be6bf707SBarry Smith   PetscFunctionBegin;
2338be6bf707SBarry Smith   if (aij->nonew != 1) {
233929bbc08cSBarry Smith     SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2340be6bf707SBarry Smith   }
2341be6bf707SBarry Smith   if (!aij->saved_values) {
234229bbc08cSBarry Smith     SETERRQ(1,"Must call MatStoreValues(A);first");
2343be6bf707SBarry Smith   }
2344be6bf707SBarry Smith 
2345be6bf707SBarry Smith   /* copy values over */
234687828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2347be6bf707SBarry Smith   PetscFunctionReturn(0);
2348be6bf707SBarry Smith }
2349fb2e594dSBarry Smith EXTERN_C_END
2350be6bf707SBarry Smith 
23514a2ae208SSatish Balay #undef __FUNCT__
23524a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2353be6bf707SBarry Smith /*@
2354be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2355be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2356be6bf707SBarry Smith        nonlinear portion.
2357be6bf707SBarry Smith 
2358be6bf707SBarry Smith    Collect on Mat
2359be6bf707SBarry Smith 
2360be6bf707SBarry Smith   Input Parameters:
2361be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2362be6bf707SBarry Smith 
236315091d37SBarry Smith   Level: advanced
236415091d37SBarry Smith 
2365be6bf707SBarry Smith .seealso: MatStoreValues()
2366be6bf707SBarry Smith 
2367be6bf707SBarry Smith @*/
2368be6bf707SBarry Smith int MatRetrieveValues(Mat mat)
2369be6bf707SBarry Smith {
2370be6bf707SBarry Smith   int ierr,(*f)(Mat);
2371be6bf707SBarry Smith 
2372be6bf707SBarry Smith   PetscFunctionBegin;
2373be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
237429bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
237529bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2376be6bf707SBarry Smith 
2377c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2378be6bf707SBarry Smith   if (f) {
2379be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2380be6bf707SBarry Smith   } else {
238129bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to retrieve values");
2382be6bf707SBarry Smith   }
2383be6bf707SBarry Smith   PetscFunctionReturn(0);
2384be6bf707SBarry Smith }
2385be6bf707SBarry Smith 
2386f83d6046SBarry Smith /*
2387f83d6046SBarry Smith    This allows SeqAIJ matrices to be passed to the matlab engine
2388f83d6046SBarry Smith */
238987828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
2390f83d6046SBarry Smith #include "engine.h"   /* Matlab include file */
2391f83d6046SBarry Smith #include "mex.h"      /* Matlab include file */
2392f83d6046SBarry Smith EXTERN_C_BEGIN
23934a2ae208SSatish Balay #undef __FUNCT__
23944a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEnginePut_SeqAIJ"
239562aa7f4eSBarry Smith int MatMatlabEnginePut_SeqAIJ(PetscObject obj,void *mengine)
2396f83d6046SBarry Smith {
23974a4478b9SSatish Balay   int         ierr,i,*ai,*aj;
2398f83d6046SBarry Smith   Mat         B = (Mat)obj;
2399f83d6046SBarry Smith   mxArray     *mat;
2400d79a51d8SBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)B->data;
2401d79a51d8SBarry Smith 
2402f83d6046SBarry Smith   PetscFunctionBegin;
240301820c62SBarry Smith   mat  = mxCreateSparse(B->n,B->m,aij->nz,mxREAL);
240487828ca2SBarry Smith   ierr = PetscMemcpy(mxGetPr(mat),aij->a,aij->nz*sizeof(PetscScalar));CHKERRQ(ierr);
2405d79a51d8SBarry Smith   /* Matlab stores by column, not row so we pass in the transpose of the matrix */
240666826eb6SBarry Smith   ierr = PetscMemcpy(mxGetIr(mat),aij->j,aij->nz*sizeof(int));CHKERRQ(ierr);
240766826eb6SBarry Smith   ierr = PetscMemcpy(mxGetJc(mat),aij->i,(B->m+1)*sizeof(int));CHKERRQ(ierr);
2408f83d6046SBarry Smith 
240901820c62SBarry Smith   /* Matlab indices start at 0 for sparse (what a surprise) */
2410bfeeae90SHong Zhang 
2411ca44d042SBarry Smith   ierr = PetscObjectName(obj);CHKERRQ(ierr);
2412f83d6046SBarry Smith   mxSetName(mat,obj->name);
241362aa7f4eSBarry Smith   engPutArray((Engine *)mengine,mat);
2414f83d6046SBarry Smith   PetscFunctionReturn(0);
2415f83d6046SBarry Smith }
2416f83d6046SBarry Smith EXTERN_C_END
241766826eb6SBarry Smith 
241866826eb6SBarry Smith EXTERN_C_BEGIN
24194a2ae208SSatish Balay #undef __FUNCT__
24204a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEngineGet_SeqAIJ"
242162aa7f4eSBarry Smith int MatMatlabEngineGet_SeqAIJ(PetscObject obj,void *mengine)
242266826eb6SBarry Smith {
242366826eb6SBarry Smith   int        ierr,ii;
242466826eb6SBarry Smith   Mat        mat = (Mat)obj;
242566826eb6SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
242666826eb6SBarry Smith   mxArray    *mmat;
242766826eb6SBarry Smith 
24286c0721eeSBarry Smith   PetscFunctionBegin;
242966826eb6SBarry Smith   ierr = PetscFree(aij->a);CHKERRQ(ierr);
243066826eb6SBarry Smith 
243162aa7f4eSBarry Smith   mmat = engGetArray((Engine *)mengine,obj->name);
243266826eb6SBarry Smith 
2433a65776f3SBarry Smith   aij->nz           = (mxGetJc(mmat))[mat->m];
2434a7ed9263SMatthew Knepley   ierr              = PetscMalloc(((size_t) aij->nz)*(sizeof(int)+sizeof(PetscScalar))+(mat->m+1)*sizeof(int),&aij->a);CHKERRQ(ierr);
243566826eb6SBarry Smith   aij->j            = (int*)(aij->a + aij->nz);
243666826eb6SBarry Smith   aij->i            = aij->j + aij->nz;
243766826eb6SBarry Smith   aij->singlemalloc = PETSC_TRUE;
243866826eb6SBarry Smith   aij->freedata     = PETSC_TRUE;
243966826eb6SBarry Smith 
244087828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,mxGetPr(mmat),aij->nz*sizeof(PetscScalar));CHKERRQ(ierr);
244166826eb6SBarry Smith   /* Matlab stores by column, not row so we pass in the transpose of the matrix */
244266826eb6SBarry Smith   ierr = PetscMemcpy(aij->j,mxGetIr(mmat),aij->nz*sizeof(int));CHKERRQ(ierr);
244366826eb6SBarry Smith   ierr = PetscMemcpy(aij->i,mxGetJc(mmat),(mat->m+1)*sizeof(int));CHKERRQ(ierr);
244466826eb6SBarry Smith 
244566826eb6SBarry Smith   for (ii=0; ii<mat->m; ii++) {
244666826eb6SBarry Smith     aij->ilen[ii] = aij->imax[ii] = aij->i[ii+1] - aij->i[ii];
244766826eb6SBarry Smith   }
244866826eb6SBarry Smith 
244966826eb6SBarry Smith   ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
245066826eb6SBarry Smith   ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
245166826eb6SBarry Smith 
245266826eb6SBarry Smith   PetscFunctionReturn(0);
245366826eb6SBarry Smith }
245466826eb6SBarry Smith EXTERN_C_END
2455f83d6046SBarry Smith #endif
2456f83d6046SBarry Smith 
2457be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
24584a2ae208SSatish Balay #undef __FUNCT__
24594a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
246017ab2063SBarry Smith /*@C
2461682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
24620d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
24636e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
246451c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
24652bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
246617ab2063SBarry Smith 
2467db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2468db81eaa0SLois Curfman McInnes 
246917ab2063SBarry Smith    Input Parameters:
2470db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
247117ab2063SBarry Smith .  m - number of rows
247217ab2063SBarry Smith .  n - number of columns
247317ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
247451c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
24752bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
247617ab2063SBarry Smith 
247717ab2063SBarry Smith    Output Parameter:
2478416022c9SBarry Smith .  A - the matrix
247917ab2063SBarry Smith 
2480b259b22eSLois Curfman McInnes    Notes:
248117ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
248217ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
24830002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
248444cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
248517ab2063SBarry Smith 
248617ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2487a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
24883d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
24896da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
249017ab2063SBarry Smith 
2491682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
24924fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2493682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
24946c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
24956c7ebb05SLois Curfman McInnes 
24966c7ebb05SLois Curfman McInnes    Options Database Keys:
2497db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
2498db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2499db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2500db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2501db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
250217ab2063SBarry Smith 
2503027ccd11SLois Curfman McInnes    Level: intermediate
2504027ccd11SLois Curfman McInnes 
250536db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
250636db0b34SBarry Smith 
250717ab2063SBarry Smith @*/
2508ca01db9bSBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,const int nnz[],Mat *A)
250917ab2063SBarry Smith {
2510273d9f13SBarry Smith   int ierr;
25116945ee14SBarry Smith 
25123a40ed3dSBarry Smith   PetscFunctionBegin;
2513273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,m,n,A);CHKERRQ(ierr);
2514273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2515273d9f13SBarry Smith   ierr = MatSeqAIJSetPreallocation(*A,nz,nnz);CHKERRQ(ierr);
2516273d9f13SBarry Smith   PetscFunctionReturn(0);
2517273d9f13SBarry Smith }
2518273d9f13SBarry Smith 
25195da197adSKris Buschelman #define SKIP_ALLOCATION -4
25205da197adSKris Buschelman 
25214a2ae208SSatish Balay #undef __FUNCT__
25224a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2523273d9f13SBarry Smith /*@C
2524273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2525273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2526273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2527273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2528273d9f13SBarry Smith 
2529273d9f13SBarry Smith    Collective on MPI_Comm
2530273d9f13SBarry Smith 
2531273d9f13SBarry Smith    Input Parameters:
2532273d9f13SBarry Smith +  comm - MPI communicator, set to PETSC_COMM_SELF
2533273d9f13SBarry Smith .  m - number of rows
2534273d9f13SBarry Smith .  n - number of columns
2535273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2536273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2537273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2538273d9f13SBarry Smith 
2539273d9f13SBarry Smith    Output Parameter:
2540273d9f13SBarry Smith .  A - the matrix
2541273d9f13SBarry Smith 
2542273d9f13SBarry Smith    Notes:
2543273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2544273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2545273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2546273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2547273d9f13SBarry Smith 
2548273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2549273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2550273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2551273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2552273d9f13SBarry Smith 
2553273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2554273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2555273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2556273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2557273d9f13SBarry Smith 
2558273d9f13SBarry Smith    Options Database Keys:
2559273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2560273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2561273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2562273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2563273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2564273d9f13SBarry Smith 
2565273d9f13SBarry Smith    Level: intermediate
2566273d9f13SBarry Smith 
2567273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
2568273d9f13SBarry Smith 
2569273d9f13SBarry Smith @*/
2570ca01db9bSBarry Smith int MatSeqAIJSetPreallocation(Mat B,int nz,const int nnz[])
2571273d9f13SBarry Smith {
2572ca01db9bSBarry Smith   int ierr,(*f)(Mat,int,const int[]);
2573a23d5eceSKris Buschelman 
2574a23d5eceSKris Buschelman   PetscFunctionBegin;
2575a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2576a23d5eceSKris Buschelman   if (f) {
2577a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2578a23d5eceSKris Buschelman   }
2579a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2580a23d5eceSKris Buschelman }
2581a23d5eceSKris Buschelman 
2582a23d5eceSKris Buschelman EXTERN_C_BEGIN
2583a23d5eceSKris Buschelman #undef __FUNCT__
2584a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2585a23d5eceSKris Buschelman int MatSeqAIJSetPreallocation_SeqAIJ(Mat B,int nz,int *nnz)
2586a23d5eceSKris Buschelman {
2587273d9f13SBarry Smith   Mat_SeqAIJ *b;
2588a7ed9263SMatthew Knepley   size_t     len = 0;
2589a43ee2ecSKris Buschelman   PetscTruth skipallocation = PETSC_FALSE;
2590a7ed9263SMatthew Knepley   int        i,ierr;
2591273d9f13SBarry Smith 
2592273d9f13SBarry Smith   PetscFunctionBegin;
2593d5d45c9bSBarry Smith 
2594c461c341SBarry Smith   if (nz == SKIP_ALLOCATION) {
2595c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2596c461c341SBarry Smith     nz             = 0;
2597c461c341SBarry Smith   }
2598c461c341SBarry Smith 
2599435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2600435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2601b73539f3SBarry Smith   if (nnz) {
2602273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
260329bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
26043a7fca6bSBarry 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);
2605b73539f3SBarry Smith     }
2606b73539f3SBarry Smith   }
2607b73539f3SBarry Smith 
2608273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2609273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2610273d9f13SBarry Smith 
2611b0a32e0cSBarry Smith   ierr = PetscMalloc((B->m+1)*sizeof(int),&b->imax);CHKERRQ(ierr);
2612273d9f13SBarry Smith   if (!nnz) {
2613435da068SBarry Smith     if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2614273d9f13SBarry Smith     else if (nz <= 0)        nz = 1;
2615273d9f13SBarry Smith     for (i=0; i<B->m; i++) b->imax[i] = nz;
2616273d9f13SBarry Smith     nz = nz*B->m;
2617273d9f13SBarry Smith   } else {
2618273d9f13SBarry Smith     nz = 0;
2619273d9f13SBarry Smith     for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2620273d9f13SBarry Smith   }
2621273d9f13SBarry Smith 
2622c461c341SBarry Smith   if (!skipallocation) {
2623273d9f13SBarry Smith     /* allocate the matrix space */
2624a7ed9263SMatthew Knepley     len             = ((size_t) nz)*(sizeof(int) + sizeof(PetscScalar)) + (B->m+1)*sizeof(int);
2625b0a32e0cSBarry Smith     ierr            = PetscMalloc(len,&b->a);CHKERRQ(ierr);
2626273d9f13SBarry Smith     b->j            = (int*)(b->a + nz);
2627273d9f13SBarry Smith     ierr            = PetscMemzero(b->j,nz*sizeof(int));CHKERRQ(ierr);
2628273d9f13SBarry Smith     b->i            = b->j + nz;
2629bfeeae90SHong Zhang     b->i[0] = 0;
26305da197adSKris Buschelman     for (i=1; i<B->m+1; i++) {
26315da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
26325da197adSKris Buschelman     }
2633273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2634273d9f13SBarry Smith     b->freedata     = PETSC_TRUE;
2635c461c341SBarry Smith   } else {
2636c461c341SBarry Smith     b->freedata     = PETSC_FALSE;
2637c461c341SBarry Smith   }
2638273d9f13SBarry Smith 
2639273d9f13SBarry Smith   /* b->ilen will count nonzeros in each row so far. */
2640b0a32e0cSBarry Smith   ierr = PetscMalloc((B->m+1)*sizeof(int),&b->ilen);CHKERRQ(ierr);
2641b0a32e0cSBarry Smith   PetscLogObjectMemory(B,len+2*(B->m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2642273d9f13SBarry Smith   for (i=0; i<B->m; i++) { b->ilen[i] = 0;}
2643273d9f13SBarry Smith 
2644273d9f13SBarry Smith   b->nz                = 0;
2645273d9f13SBarry Smith   b->maxnz             = nz;
2646273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2647273d9f13SBarry Smith   PetscFunctionReturn(0);
2648273d9f13SBarry Smith }
2649a23d5eceSKris Buschelman EXTERN_C_END
2650273d9f13SBarry Smith 
2651eb9c0419SKris Buschelman EXTERN int RegisterApplyPtAPRoutines_Private(Mat);
2652eb9c0419SKris Buschelman 
2653273d9f13SBarry Smith EXTERN_C_BEGIN
265405b94e36SKris Buschelman EXTERN int MatConvert_SeqAIJ_SeqSBAIJ(Mat,MatType,Mat*);
265505b94e36SKris Buschelman EXTERN int MatConvert_SeqAIJ_SeqBAIJ(Mat,MatType,Mat*);
265605b94e36SKris Buschelman EXTERN int MatReorderForNonzeroDiagonal_SeqAIJ(Mat,PetscReal,IS,IS);
26570a99d0a2SKris Buschelman EXTERN int MatAdjustForInodes_SeqAIJ(Mat,IS*,IS*);
2658d758967fSKris Buschelman EXTERN int MatSeqAIJGetInodeSizes_SeqAIJ(Mat,int*,int*[],int*);
2659a6175056SHong Zhang EXTERN_C_END
2660a6175056SHong Zhang 
2661a6175056SHong Zhang EXTERN_C_BEGIN
26624a2ae208SSatish Balay #undef __FUNCT__
26634a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
2664273d9f13SBarry Smith int MatCreate_SeqAIJ(Mat B)
2665273d9f13SBarry Smith {
2666273d9f13SBarry Smith   Mat_SeqAIJ *b;
2667273d9f13SBarry Smith   int        ierr,size;
2668273d9f13SBarry Smith   PetscTruth flg;
2669273d9f13SBarry Smith 
2670273d9f13SBarry Smith   PetscFunctionBegin;
2671273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
2672273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
2673273d9f13SBarry Smith 
2674273d9f13SBarry Smith   B->m = B->M = PetscMax(B->m,B->M);
2675273d9f13SBarry Smith   B->n = B->N = PetscMax(B->n,B->N);
2676273d9f13SBarry Smith 
2677b0a32e0cSBarry Smith   ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr);
2678b0a32e0cSBarry Smith   B->data             = (void*)b;
2679549d3d68SSatish Balay   ierr = PetscMemzero(b,sizeof(Mat_SeqAIJ));CHKERRQ(ierr);
2680549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2681416022c9SBarry Smith   B->factor           = 0;
2682416022c9SBarry Smith   B->lupivotthreshold = 1.0;
268390f02eecSBarry Smith   B->mapping          = 0;
2684e82a3eeeSBarry Smith   ierr = PetscOptionsGetReal(B->prefix,"-mat_lu_pivotthreshold",&B->lupivotthreshold,PETSC_NULL);CHKERRQ(ierr);
2685e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-pc_ilu_preserve_row_sums",&b->ilu_preserve_row_sums);CHKERRQ(ierr);
2686416022c9SBarry Smith   b->row              = 0;
2687416022c9SBarry Smith   b->col              = 0;
268882bf6240SBarry Smith   b->icol             = 0;
2689b810aeb4SBarry Smith   b->reallocs         = 0;
269017ab2063SBarry Smith 
26918a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr);
26928a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr);
2693a5ae1ecdSBarry Smith 
2694f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
269536db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2696f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2697416022c9SBarry Smith   b->nonew             = 0;
2698416022c9SBarry Smith   b->diag              = 0;
2699416022c9SBarry Smith   b->solve_work        = 0;
27002a1b7f2aSHong Zhang   B->spptr             = 0;
2701b65db4caSBarry Smith   b->inode.use         = PETSC_TRUE;
2702754ec7b1SSatish Balay   b->inode.node_count  = 0;
2703754ec7b1SSatish Balay   b->inode.size        = 0;
27046c7ebb05SLois Curfman McInnes   b->inode.limit       = 5;
27056c7ebb05SLois Curfman McInnes   b->inode.max_limit   = 5;
2706be6bf707SBarry Smith   b->saved_values      = 0;
2707d7f994e1SBarry Smith   b->idiag             = 0;
2708d7f994e1SBarry Smith   b->ssor              = 0;
2709f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
2710a30b2313SHong Zhang   b->xtoy              = 0;
2711a30b2313SHong Zhang   b->XtoY              = 0;
271217ab2063SBarry Smith 
271335d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
271435d8aa7fSBarry Smith 
2715e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_essl",&flg);CHKERRQ(ierr);
271669957df2SSatish Balay   if (flg) { ierr = MatUseEssl_SeqAIJ(B);CHKERRQ(ierr); }
2717e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_lusol",&flg);CHKERRQ(ierr);
2718cd5578b5SBarry Smith   if (flg) { ierr = MatUseLUSOL_SeqAIJ(B);CHKERRQ(ierr); }
2719e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_matlab",&flg);CHKERRQ(ierr);
2720ca44d042SBarry Smith   if (flg) {ierr = MatUseMatlab_SeqAIJ(B);CHKERRQ(ierr);}
2721e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_dxml",&flg);CHKERRQ(ierr);
272269957df2SSatish Balay   if (flg){
2723416022c9SBarry Smith     ierr = MatUseDXML_SeqAIJ(B);CHKERRQ(ierr);
272417ab2063SBarry Smith   }
2725f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2726bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2727bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2728f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2729be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2730bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2731f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2732be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2733bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
2734a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
2735a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
2736a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
273785fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
273885fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
273985fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
2740cd0d46ebSvictorle   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsSymmetric_C",
2741cd0d46ebSvictorle                                      "MatIsSymmetric_SeqAIJ",
2742cd0d46ebSvictorle                                       MatIsSymmetric_SeqAIJ);CHKERRQ(ierr);
2743a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
2744a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
2745a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
274605b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
274705b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
274805b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
27490a99d0a2SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatAdjustForInodes_C",
27500a99d0a2SKris Buschelman                                      "MatAdjustForInodes_SeqAIJ",
27510a99d0a2SKris Buschelman                                       MatAdjustForInodes_SeqAIJ);CHKERRQ(ierr);
2752d758967fSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJGetInodeSizes_C",
2753d758967fSKris Buschelman                                      "MatSeqAIJGetInodeSizes_SeqAIJ",
2754d758967fSKris Buschelman                                       MatSeqAIJGetInodeSizes_SeqAIJ);CHKERRQ(ierr);
275587828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
2756f83d6046SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatMatlabEnginePut_SeqAIJ",MatMatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
275766826eb6SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatMatlabEngineGet_SeqAIJ",MatMatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
2758f83d6046SBarry Smith #endif
2759eb9c0419SKris Buschelman   ierr = RegisterApplyPtAPRoutines_Private(B);CHKERRQ(ierr);
27603a40ed3dSBarry Smith   PetscFunctionReturn(0);
276117ab2063SBarry Smith }
2762273d9f13SBarry Smith EXTERN_C_END
276317ab2063SBarry Smith 
27644a2ae208SSatish Balay #undef __FUNCT__
27654a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ"
2766be6bf707SBarry Smith int MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
276717ab2063SBarry Smith {
2768416022c9SBarry Smith   Mat        C;
2769416022c9SBarry Smith   Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data;
2770bfeeae90SHong Zhang   int        i,m = A->m,ierr;
2771a7ed9263SMatthew Knepley   size_t     len;
277217ab2063SBarry Smith 
27733a40ed3dSBarry Smith   PetscFunctionBegin;
27744043dd9cSLois Curfman McInnes   *B = 0;
2775273d9f13SBarry Smith   ierr = MatCreate(A->comm,A->m,A->n,A->m,A->n,&C);CHKERRQ(ierr);
2776273d9f13SBarry Smith   ierr = MatSetType(C,MATSEQAIJ);CHKERRQ(ierr);
2777273d9f13SBarry Smith   c    = (Mat_SeqAIJ*)C->data;
2778273d9f13SBarry Smith 
2779416022c9SBarry Smith   C->factor         = A->factor;
2780416022c9SBarry Smith   c->row            = 0;
2781416022c9SBarry Smith   c->col            = 0;
278282bf6240SBarry Smith   c->icol           = 0;
2783f1e2ffcdSBarry Smith   c->keepzeroedrows = a->keepzeroedrows;
2784c456f294SBarry Smith   C->assembled      = PETSC_TRUE;
278517ab2063SBarry Smith 
2786273d9f13SBarry Smith   C->M          = A->m;
2787273d9f13SBarry Smith   C->N          = A->n;
278817ab2063SBarry Smith 
2789b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&c->imax);CHKERRQ(ierr);
2790b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&c->ilen);CHKERRQ(ierr);
279117ab2063SBarry Smith   for (i=0; i<m; i++) {
2792416022c9SBarry Smith     c->imax[i] = a->imax[i];
2793416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
279417ab2063SBarry Smith   }
279517ab2063SBarry Smith 
279617ab2063SBarry Smith   /* allocate the matrix space */
2797f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
2798a7ed9263SMatthew Knepley   len   = ((size_t) (m+1))*sizeof(int)+(a->i[m])*(sizeof(PetscScalar)+sizeof(int));
2799b0a32e0cSBarry Smith   ierr  = PetscMalloc(len,&c->a);CHKERRQ(ierr);
2800bfeeae90SHong Zhang   c->j  = (int*)(c->a + a->i[m] );
2801bfeeae90SHong Zhang   c->i  = c->j + a->i[m];
2802549d3d68SSatish Balay   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(int));CHKERRQ(ierr);
280317ab2063SBarry Smith   if (m > 0) {
2804bfeeae90SHong Zhang     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(int));CHKERRQ(ierr);
2805be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2806bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
2807be6bf707SBarry Smith     } else {
2808bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
280917ab2063SBarry Smith     }
281008480c60SBarry Smith   }
281117ab2063SBarry Smith 
2812b0a32e0cSBarry Smith   PetscLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2813416022c9SBarry Smith   c->sorted      = a->sorted;
2814416022c9SBarry Smith   c->roworiented = a->roworiented;
2815416022c9SBarry Smith   c->nonew       = a->nonew;
28167a743949SBarry Smith   c->ilu_preserve_row_sums = a->ilu_preserve_row_sums;
2817be6bf707SBarry Smith   c->saved_values = 0;
2818d7f994e1SBarry Smith   c->idiag        = 0;
2819d7f994e1SBarry Smith   c->ssor         = 0;
282036db0b34SBarry Smith   c->ignorezeroentries = a->ignorezeroentries;
282136db0b34SBarry Smith   c->freedata     = PETSC_TRUE;
282217ab2063SBarry Smith 
2823416022c9SBarry Smith   if (a->diag) {
2824b0a32e0cSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(int),&c->diag);CHKERRQ(ierr);
2825b0a32e0cSBarry Smith     PetscLogObjectMemory(C,(m+1)*sizeof(int));
282617ab2063SBarry Smith     for (i=0; i<m; i++) {
2827416022c9SBarry Smith       c->diag[i] = a->diag[i];
282817ab2063SBarry Smith     }
28293a40ed3dSBarry Smith   } else c->diag        = 0;
2830b65db4caSBarry Smith   c->inode.use          = a->inode.use;
28316c7ebb05SLois Curfman McInnes   c->inode.limit        = a->inode.limit;
28326c7ebb05SLois Curfman McInnes   c->inode.max_limit    = a->inode.max_limit;
2833754ec7b1SSatish Balay   if (a->inode.size){
2834b0a32e0cSBarry Smith     ierr                = PetscMalloc((m+1)*sizeof(int),&c->inode.size);CHKERRQ(ierr);
2835754ec7b1SSatish Balay     c->inode.node_count = a->inode.node_count;
2836549d3d68SSatish Balay     ierr                = PetscMemcpy(c->inode.size,a->inode.size,(m+1)*sizeof(int));CHKERRQ(ierr);
2837754ec7b1SSatish Balay   } else {
2838754ec7b1SSatish Balay     c->inode.size       = 0;
2839754ec7b1SSatish Balay     c->inode.node_count = 0;
2840754ec7b1SSatish Balay   }
2841416022c9SBarry Smith   c->nz                 = a->nz;
2842416022c9SBarry Smith   c->maxnz              = a->maxnz;
2843416022c9SBarry Smith   c->solve_work         = 0;
28442a1b7f2aSHong Zhang   C->spptr              = 0;      /* Dangerous -I'm throwing away a->spptr */
2845273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2846754ec7b1SSatish Balay 
2847416022c9SBarry Smith   *B = C;
2848b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
28493a40ed3dSBarry Smith   PetscFunctionReturn(0);
285017ab2063SBarry Smith }
285117ab2063SBarry Smith 
2852273d9f13SBarry Smith EXTERN_C_BEGIN
28534a2ae208SSatish Balay #undef __FUNCT__
28544a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
2855b0a32e0cSBarry Smith int MatLoad_SeqAIJ(PetscViewer viewer,MatType type,Mat *A)
285617ab2063SBarry Smith {
2857416022c9SBarry Smith   Mat_SeqAIJ   *a;
2858416022c9SBarry Smith   Mat          B;
2859efb16452SHong Zhang   int          i,nz,ierr,fd,header[4],size,*rowlengths = 0,M,N;
2860bcd2baecSBarry Smith   MPI_Comm     comm;
286114b396bbSKris Buschelman #if defined(PETSC_HAVE_SUPERLUDIST) || defined(PETSC_HAVE_UMFPACK) || defined(PETSC_HAVE_MUMPS)
286227fa2452SHong Zhang   PetscTruth   flag;
286327fa2452SHong Zhang #endif
286417ab2063SBarry Smith 
28653a40ed3dSBarry Smith   PetscFunctionBegin;
2866e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
2867d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
286829bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
2869b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
28700752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
2871552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
287217ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
287317ab2063SBarry Smith 
2874d64ed03dSBarry Smith   if (nz < 0) {
287529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
2876d64ed03dSBarry Smith   }
2877d64ed03dSBarry Smith 
287817ab2063SBarry Smith   /* read in row lengths */
2879b0a32e0cSBarry Smith   ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr);
28800752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
288117ab2063SBarry Smith 
288217ab2063SBarry Smith   /* create our matrix */
2883b3a1e11cSKris Buschelman   ierr = MatCreate(comm,PETSC_DECIDE,PETSC_DECIDE,M,N,&B);CHKERRQ(ierr);
2884b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
2885b3a1e11cSKris Buschelman   ierr = MatSeqAIJSetPreallocation(B,0,rowlengths);CHKERRQ(ierr);
2886416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
288717ab2063SBarry Smith 
288817ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
28890752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
289017ab2063SBarry Smith 
289117ab2063SBarry Smith   /* read in nonzero values */
28920752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
289317ab2063SBarry Smith 
289417ab2063SBarry Smith   /* set matrix "i" values */
2895efb16452SHong Zhang   a->i[0] = 0;
289617ab2063SBarry Smith   for (i=1; i<= M; i++) {
2897416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2898416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
289917ab2063SBarry Smith   }
2900606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
290117ab2063SBarry Smith 
29026d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
29036d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
290427fa2452SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST)
290527fa2452SHong Zhang   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_superlu_dist",&flag);CHKERRQ(ierr);
290627fa2452SHong Zhang   if (flag) { ierr = MatUseSuperLU_DIST_MPIAIJ(B);CHKERRQ(ierr); }
290727fa2452SHong Zhang #endif
290827fa2452SHong Zhang #if defined(PETSC_HAVE_UMFPACK)
290927fa2452SHong Zhang   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_umfpack",&flag);CHKERRQ(ierr);
291027fa2452SHong Zhang   if (flag) { ierr = MatUseUMFPACK_SeqAIJ(B);CHKERRQ(ierr); }
291127fa2452SHong Zhang #endif
2912eee76cafSHong Zhang #if defined(PETSC_HAVE_MUMPS)
2913eee76cafSHong Zhang   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_mumps",&flag);CHKERRQ(ierr);
2914eee76cafSHong Zhang   if (flag) { ierr = MatUseMUMPS_MPIAIJ(B);CHKERRQ(ierr); }
2915eee76cafSHong Zhang #endif
2916b3a1e11cSKris Buschelman   *A = B;
29173a40ed3dSBarry Smith   PetscFunctionReturn(0);
291817ab2063SBarry Smith }
2919273d9f13SBarry Smith EXTERN_C_END
292017ab2063SBarry Smith 
29214a2ae208SSatish Balay #undef __FUNCT__
2922b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
29238f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
29247264ac53SSatish Balay {
29257264ac53SSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
29260f5bd95cSBarry Smith   int        ierr;
29277264ac53SSatish Balay 
29283a40ed3dSBarry Smith   PetscFunctionBegin;
2929bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
2930bfeeae90SHong Zhang   if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)) {
2931ca44d042SBarry Smith     *flg = PETSC_FALSE;
2932ca44d042SBarry Smith     PetscFunctionReturn(0);
2933bcd2baecSBarry Smith   }
29347264ac53SSatish Balay 
29357264ac53SSatish Balay   /* if the a->i are the same */
2936273d9f13SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(int),flg);CHKERRQ(ierr);
29370f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
29387264ac53SSatish Balay 
29397264ac53SSatish Balay   /* if a->j are the same */
29400f5bd95cSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(int),flg);CHKERRQ(ierr);
29410f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
2942bcd2baecSBarry Smith 
2943bcd2baecSBarry Smith   /* if a->a are the same */
294487828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
29450f5bd95cSBarry Smith 
29463a40ed3dSBarry Smith   PetscFunctionReturn(0);
29477264ac53SSatish Balay 
29487264ac53SSatish Balay }
294936db0b34SBarry Smith 
29504a2ae208SSatish Balay #undef __FUNCT__
29514a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
295236db0b34SBarry Smith /*@C
295336db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
295436db0b34SBarry Smith               provided by the user.
295536db0b34SBarry Smith 
295636db0b34SBarry Smith       Coolective on MPI_Comm
295736db0b34SBarry Smith 
295836db0b34SBarry Smith    Input Parameters:
295936db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
296036db0b34SBarry Smith .   m - number of rows
296136db0b34SBarry Smith .   n - number of columns
296236db0b34SBarry Smith .   i - row indices
296336db0b34SBarry Smith .   j - column indices
296436db0b34SBarry Smith -   a - matrix values
296536db0b34SBarry Smith 
296636db0b34SBarry Smith    Output Parameter:
296736db0b34SBarry Smith .   mat - the matrix
296836db0b34SBarry Smith 
296936db0b34SBarry Smith    Level: intermediate
297036db0b34SBarry Smith 
297136db0b34SBarry Smith    Notes:
29720551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
297336db0b34SBarry Smith     once the matrix is destroyed
297436db0b34SBarry Smith 
297536db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
297636db0b34SBarry Smith 
2977bfeeae90SHong Zhang        The i and j indices are 0 based
297836db0b34SBarry Smith 
297936db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
298036db0b34SBarry Smith 
298136db0b34SBarry Smith @*/
298287828ca2SBarry Smith int MatCreateSeqAIJWithArrays(MPI_Comm comm,int m,int n,int* i,int*j,PetscScalar *a,Mat *mat)
298336db0b34SBarry Smith {
298436db0b34SBarry Smith   int        ierr,ii;
298536db0b34SBarry Smith   Mat_SeqAIJ *aij;
298636db0b34SBarry Smith 
298736db0b34SBarry Smith   PetscFunctionBegin;
2988c461c341SBarry Smith   ierr = MatCreateSeqAIJ(comm,m,n,SKIP_ALLOCATION,0,mat);CHKERRQ(ierr);
298936db0b34SBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
299036db0b34SBarry Smith 
2991bfeeae90SHong Zhang   if (i[0] != 0) {
2992bfeeae90SHong Zhang     SETERRQ(1,"i (row indices) must start with 0");
299336db0b34SBarry Smith   }
299436db0b34SBarry Smith   aij->i = i;
299536db0b34SBarry Smith   aij->j = j;
299636db0b34SBarry Smith   aij->a = a;
299736db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
299836db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
299936db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
300036db0b34SBarry Smith 
300136db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
300236db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
30039860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g)
300429bbc08cSBarry 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]);
300536db0b34SBarry Smith #endif
300636db0b34SBarry Smith   }
30079860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g)
300836db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
3009bfeeae90SHong Zhang     if (j[ii] < 0) SETERRQ2(1,"Negative column index at location = %d index = %d",ii,j[ii]);
3010bfeeae90SHong Zhang     if (j[ii] > n - 1) SETERRQ2(1,"Column index to large at location = %d index = %d",ii,j[ii]);
301136db0b34SBarry Smith   }
301236db0b34SBarry Smith #endif
301336db0b34SBarry Smith 
3014b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3015b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
301636db0b34SBarry Smith   PetscFunctionReturn(0);
301736db0b34SBarry Smith }
301836db0b34SBarry Smith 
3019cc8ba8e1SBarry Smith #undef __FUNCT__
3020ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3021ee4f033dSBarry Smith int MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3022cc8ba8e1SBarry Smith {
3023cc8ba8e1SBarry Smith   int        ierr;
3024cc8ba8e1SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
302536db0b34SBarry Smith 
3026cc8ba8e1SBarry Smith   PetscFunctionBegin;
302712c595b3SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
3028cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3029cc8ba8e1SBarry Smith     a->coloring = coloring;
303012c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
303108b6dcc0SBarry Smith     int             i,*larray;
303212c595b3SBarry Smith     ISColoring      ocoloring;
303308b6dcc0SBarry Smith     ISColoringValue *colors;
303412c595b3SBarry Smith 
303512c595b3SBarry Smith     /* set coloring for diagonal portion */
303612c595b3SBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
303712c595b3SBarry Smith     for (i=0; i<A->n; i++) {
303812c595b3SBarry Smith       larray[i] = i;
303912c595b3SBarry Smith     }
304012c595b3SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
304108b6dcc0SBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
304212c595b3SBarry Smith     for (i=0; i<A->n; i++) {
304312c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
304412c595b3SBarry Smith     }
304512c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
304612c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,A->n,colors,&ocoloring);CHKERRQ(ierr);
304712c595b3SBarry Smith     a->coloring = ocoloring;
304812c595b3SBarry Smith   }
3049cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3050cc8ba8e1SBarry Smith }
3051cc8ba8e1SBarry Smith 
305287828ca2SBarry Smith #if defined(PETSC_HAVE_ADIC) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
3053ee4f033dSBarry Smith EXTERN_C_BEGIN
305429c1e371SBarry Smith #include "adic/ad_utils.h"
3055ee4f033dSBarry Smith EXTERN_C_END
3056cc8ba8e1SBarry Smith 
3057cc8ba8e1SBarry Smith #undef __FUNCT__
3058ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3059ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3060cc8ba8e1SBarry Smith {
3061cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
306208b6dcc0SBarry Smith   int             m = A->m,*ii = a->i,*jj = a->j,nz,i,j,nlen;
30634440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
306408b6dcc0SBarry Smith   ISColoringValue *color;
3065cc8ba8e1SBarry Smith 
3066cc8ba8e1SBarry Smith   PetscFunctionBegin;
3067cc8ba8e1SBarry Smith   if (!a->coloring) SETERRQ(1,"Coloring not set for matrix");
30684440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3069cc8ba8e1SBarry Smith   color = a->coloring->colors;
3070cc8ba8e1SBarry Smith   /* loop over rows */
3071cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3072cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3073cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3074cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3075cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3076cc8ba8e1SBarry Smith     }
30774440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3078ee4f033dSBarry Smith   }
3079ee4f033dSBarry Smith   PetscFunctionReturn(0);
3080ee4f033dSBarry Smith }
3081ee4f033dSBarry Smith 
3082ee4f033dSBarry Smith #else
3083ee4f033dSBarry Smith 
3084ee4f033dSBarry Smith #undef __FUNCT__
3085ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3086ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3087ee4f033dSBarry Smith {
3088ee4f033dSBarry Smith   PetscFunctionBegin;
3089ee4f033dSBarry Smith   SETERRQ(1,"PETSc installed without ADIC");
3090ee4f033dSBarry Smith }
3091ee4f033dSBarry Smith 
3092ee4f033dSBarry Smith #endif
3093ee4f033dSBarry Smith 
3094ee4f033dSBarry Smith #undef __FUNCT__
3095ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
3096ee4f033dSBarry Smith int MatSetValuesAdifor_SeqAIJ(Mat A,int nl,void *advalues)
3097ee4f033dSBarry Smith {
3098ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
309908b6dcc0SBarry Smith   int             m = A->m,*ii = a->i,*jj = a->j,nz,i,j;
310087828ca2SBarry Smith   PetscScalar     *v = a->a,*values = (PetscScalar *)advalues;
310108b6dcc0SBarry Smith   ISColoringValue *color;
3102ee4f033dSBarry Smith 
3103ee4f033dSBarry Smith   PetscFunctionBegin;
3104ee4f033dSBarry Smith   if (!a->coloring) SETERRQ(1,"Coloring not set for matrix");
3105ee4f033dSBarry Smith   color = a->coloring->colors;
3106ee4f033dSBarry Smith   /* loop over rows */
3107ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3108ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3109ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3110ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3111ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3112ee4f033dSBarry Smith     }
3113ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3114cc8ba8e1SBarry Smith   }
3115cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3116cc8ba8e1SBarry Smith }
311736db0b34SBarry Smith 
3118