xref: /petsc/src/mat/impls/aij/seq/aij.c (revision be5d1d56a128fdbca06f8d9818f1d611ccde2ba2)
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/inline/spops.h"
98d195f9aSBarry Smith #include "src/inline/dot.h"
100a835dfdSSatish Balay #include "petscbt.h"
1117ab2063SBarry Smith 
124a2ae208SSatish Balay #undef __FUNCT__
134a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
144e7234bfSBarry Smith int MatGetRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *m,int *ia[],int *ja[],PetscTruth *done)
1517ab2063SBarry Smith {
16416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
176945ee14SBarry Smith   int        ierr,i,ishift;
1817ab2063SBarry Smith 
193a40ed3dSBarry Smith   PetscFunctionBegin;
2031625ec5SSatish Balay   *m     = A->m;
213a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
22bfeeae90SHong Zhang   ishift = 0;
2353e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
24273d9f13SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
25bfeeae90SHong Zhang   } else if (oshift == 1) {
26435da068SBarry Smith     int nz = a->i[A->m];
273b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
281bc30dd5SBarry Smith     ierr = PetscMalloc((A->m+1)*sizeof(int),ia);CHKERRQ(ierr);
29b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),ja);CHKERRQ(ierr);
303b2fbd54SBarry Smith     for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
31273d9f13SBarry Smith     for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] + 1;
326945ee14SBarry Smith   } else {
336945ee14SBarry Smith     *ia = a->i; *ja = a->j;
34a2ce50c7SBarry Smith   }
353a40ed3dSBarry Smith   PetscFunctionReturn(0);
36a2744918SBarry Smith }
37a2744918SBarry Smith 
384a2ae208SSatish Balay #undef __FUNCT__
394a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
404e7234bfSBarry Smith int MatRestoreRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int *ia[],int *ja[],PetscTruth *done)
416945ee14SBarry Smith {
42bfeeae90SHong Zhang   int ierr;
436945ee14SBarry Smith 
443a40ed3dSBarry Smith   PetscFunctionBegin;
453a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
46bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
47606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
48606d414cSSatish Balay     ierr = PetscFree(*ja);CHKERRQ(ierr);
49bcd2baecSBarry Smith   }
503a40ed3dSBarry Smith   PetscFunctionReturn(0);
5117ab2063SBarry Smith }
5217ab2063SBarry Smith 
534a2ae208SSatish Balay #undef __FUNCT__
544a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
554e7234bfSBarry Smith int MatGetColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *nn,int *ia[],int *ja[],PetscTruth *done)
563b2fbd54SBarry Smith {
573b2fbd54SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
58bfeeae90SHong Zhang   int        ierr,i,*collengths,*cia,*cja,n = A->n,m = A->m;
59bfeeae90SHong Zhang   int        nz = a->i[m],row,*jj,mr,col;
603b2fbd54SBarry Smith 
613a40ed3dSBarry Smith   PetscFunctionBegin;
623b2fbd54SBarry Smith   *nn     = A->n;
633a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
643b2fbd54SBarry Smith   if (symmetric) {
65bfeeae90SHong Zhang     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
663b2fbd54SBarry Smith   } else {
67b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(int),&collengths);CHKERRQ(ierr);
68549d3d68SSatish Balay     ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr);
69b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(int),&cia);CHKERRQ(ierr);
70b0a32e0cSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(int),&cja);CHKERRQ(ierr);
713b2fbd54SBarry Smith     jj = a->j;
723b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
73bfeeae90SHong Zhang       collengths[jj[i]]++;
743b2fbd54SBarry Smith     }
753b2fbd54SBarry Smith     cia[0] = oshift;
763b2fbd54SBarry Smith     for (i=0; i<n; i++) {
773b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
783b2fbd54SBarry Smith     }
79549d3d68SSatish Balay     ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr);
803b2fbd54SBarry Smith     jj   = a->j;
81a93ec695SBarry Smith     for (row=0; row<m; row++) {
82a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
83a93ec695SBarry Smith       for (i=0; i<mr; i++) {
84bfeeae90SHong Zhang         col = *jj++;
853b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
863b2fbd54SBarry Smith       }
873b2fbd54SBarry Smith     }
88606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
893b2fbd54SBarry Smith     *ia = cia; *ja = cja;
903b2fbd54SBarry Smith   }
913a40ed3dSBarry Smith   PetscFunctionReturn(0);
923b2fbd54SBarry Smith }
933b2fbd54SBarry Smith 
944a2ae208SSatish Balay #undef __FUNCT__
954a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
964e7234bfSBarry Smith int MatRestoreColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int *ia[],int *ja[],PetscTruth *done)
973b2fbd54SBarry Smith {
98606d414cSSatish Balay   int ierr;
99606d414cSSatish Balay 
1003a40ed3dSBarry Smith   PetscFunctionBegin;
1013a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1023b2fbd54SBarry Smith 
103606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
104606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1053b2fbd54SBarry Smith 
1063a40ed3dSBarry Smith   PetscFunctionReturn(0);
1073b2fbd54SBarry Smith }
1083b2fbd54SBarry Smith 
109227d817aSBarry Smith #define CHUNKSIZE   15
11017ab2063SBarry Smith 
1114a2ae208SSatish Balay #undef __FUNCT__
1124a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
113f15d580aSBarry Smith int MatSetValues_SeqAIJ(Mat A,int m,const int im[],int n,const int in[],const PetscScalar v[],InsertMode is)
11417ab2063SBarry Smith {
115416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
116416022c9SBarry Smith   int         *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,sorted = a->sorted;
117273d9f13SBarry Smith   int         *imax = a->imax,*ai = a->i,*ailen = a->ilen;
118bfeeae90SHong Zhang   int         *aj = a->j,nonew = a->nonew,ierr;
11987828ca2SBarry Smith   PetscScalar *ap,value,*aa = a->a;
12036db0b34SBarry Smith   PetscTruth  ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE);
121273d9f13SBarry Smith   PetscTruth  roworiented = a->roworiented;
12217ab2063SBarry Smith 
1233a40ed3dSBarry Smith   PetscFunctionBegin;
12417ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
125416022c9SBarry Smith     row  = im[k];
1265ef9f2a5SBarry Smith     if (row < 0) continue;
127aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
128590ac198SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",row,A->m-1);
1293b2fbd54SBarry Smith #endif
130bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
13117ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
132416022c9SBarry Smith     low = 0;
13317ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1345ef9f2a5SBarry Smith       if (in[l] < 0) continue;
135aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g)
136590ac198SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[l],A->n-1);
1373b2fbd54SBarry Smith #endif
138bfeeae90SHong Zhang       col = in[l];
1394b0e389bSBarry Smith       if (roworiented) {
1405ef9f2a5SBarry Smith         value = v[l + k*n];
141bef8e0ddSBarry Smith       } else {
1424b0e389bSBarry Smith         value = v[k + l*m];
1434b0e389bSBarry Smith       }
14436db0b34SBarry Smith       if (value == 0.0 && ignorezeroentries) continue;
14536db0b34SBarry Smith 
146416022c9SBarry Smith       if (!sorted) low = 0; high = nrow;
147416022c9SBarry Smith       while (high-low > 5) {
148416022c9SBarry Smith         t = (low+high)/2;
149416022c9SBarry Smith         if (rp[t] > col) high = t;
150416022c9SBarry Smith         else             low  = t;
15117ab2063SBarry Smith       }
152416022c9SBarry Smith       for (i=low; i<high; i++) {
15317ab2063SBarry Smith         if (rp[i] > col) break;
15417ab2063SBarry Smith         if (rp[i] == col) {
155416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
15617ab2063SBarry Smith           else                  ap[i] = value;
15717ab2063SBarry Smith           goto noinsert;
15817ab2063SBarry Smith         }
15917ab2063SBarry Smith       }
160c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
16129bbc08cSBarry Smith       else if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix",row,col);
16217ab2063SBarry Smith       if (nrow >= rmax) {
16317ab2063SBarry Smith         /* there is no extra room in row, therefore enlarge */
164a7ed9263SMatthew Knepley         int         new_nz = ai[A->m] + CHUNKSIZE,*new_i,*new_j;
165a7ed9263SMatthew Knepley         size_t      len;
16687828ca2SBarry Smith         PetscScalar *new_a;
16717ab2063SBarry Smith 
16829bbc08cSBarry Smith         if (nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix requiring new malloc()",row,col);
16996854ed6SLois Curfman McInnes 
17017ab2063SBarry Smith         /* malloc new storage space */
171a7ed9263SMatthew Knepley         len     = ((size_t) new_nz)*(sizeof(int)+sizeof(PetscScalar))+(A->m+1)*sizeof(int);
172b0a32e0cSBarry Smith 	ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr);
17317ab2063SBarry Smith         new_j   = (int*)(new_a + new_nz);
17417ab2063SBarry Smith         new_i   = new_j + new_nz;
17517ab2063SBarry Smith 
17617ab2063SBarry Smith         /* copy over old data into new slots */
17717ab2063SBarry Smith         for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];}
178273d9f13SBarry Smith         for (ii=row+1; ii<A->m+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;}
179bfeeae90SHong Zhang         ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow)*sizeof(int));CHKERRQ(ierr);
180bfeeae90SHong Zhang         len  = (((size_t) new_nz) - CHUNKSIZE - ai[row] - nrow );
181bfeeae90SHong Zhang         ierr = PetscMemcpy(new_j+ai[row]+nrow+CHUNKSIZE,aj+ai[row]+nrow,len*sizeof(int));CHKERRQ(ierr);
182bfeeae90SHong Zhang         ierr = PetscMemcpy(new_a,aa,(((size_t) ai[row])+nrow)*sizeof(PetscScalar));CHKERRQ(ierr);
183bfeeae90SHong Zhang         ierr = PetscMemcpy(new_a+ai[row]+nrow+CHUNKSIZE,aa+ai[row]+nrow,len*sizeof(PetscScalar));CHKERRQ(ierr);
18417ab2063SBarry Smith         /* free up old matrix storage */
185606d414cSSatish Balay         ierr = PetscFree(a->a);CHKERRQ(ierr);
186606d414cSSatish Balay         if (!a->singlemalloc) {
187606d414cSSatish Balay           ierr = PetscFree(a->i);CHKERRQ(ierr);
188606d414cSSatish Balay           ierr = PetscFree(a->j);CHKERRQ(ierr);
189606d414cSSatish Balay         }
190416022c9SBarry Smith         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;
191f1e2ffcdSBarry Smith         a->singlemalloc = PETSC_TRUE;
19217ab2063SBarry Smith 
193bfeeae90SHong Zhang         rp   = aj + ai[row]; ap = aa + ai[row] ;
194416022c9SBarry Smith         rmax = imax[row] = imax[row] + CHUNKSIZE;
19587828ca2SBarry Smith         PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar)));
196416022c9SBarry Smith         a->maxnz += CHUNKSIZE;
197b810aeb4SBarry Smith         a->reallocs++;
19817ab2063SBarry Smith       }
199416022c9SBarry Smith       N = nrow++ - 1; a->nz++;
200416022c9SBarry Smith       /* shift up all the later entries in this row */
201416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
20217ab2063SBarry Smith         rp[ii+1] = rp[ii];
20317ab2063SBarry Smith         ap[ii+1] = ap[ii];
20417ab2063SBarry Smith       }
20517ab2063SBarry Smith       rp[i] = col;
20617ab2063SBarry Smith       ap[i] = value;
20717ab2063SBarry Smith       noinsert:;
208416022c9SBarry Smith       low = i + 1;
20917ab2063SBarry Smith     }
21017ab2063SBarry Smith     ailen[row] = nrow;
21117ab2063SBarry Smith   }
2123a40ed3dSBarry Smith   PetscFunctionReturn(0);
21317ab2063SBarry Smith }
21417ab2063SBarry Smith 
2154a2ae208SSatish Balay #undef __FUNCT__
2164a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
217f15d580aSBarry Smith int MatGetValues_SeqAIJ(Mat A,int m,const int im[],int n,const int in[],PetscScalar v[])
2187eb43aa7SLois Curfman McInnes {
2197eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
220b49de8d1SLois Curfman McInnes   int          *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
221bfeeae90SHong Zhang   int          *ai = a->i,*ailen = a->ilen;
22287828ca2SBarry Smith   PetscScalar  *ap,*aa = a->a,zero = 0.0;
2237eb43aa7SLois Curfman McInnes 
2243a40ed3dSBarry Smith   PetscFunctionBegin;
2257eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2267eb43aa7SLois Curfman McInnes     row  = im[k];
22729bbc08cSBarry Smith     if (row < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %d",row);
228590ac198SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",row,A->m-1);
229bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2307eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2317eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
23229bbc08cSBarry Smith       if (in[l] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %d",in[l]);
233590ac198SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[l],A->n-1);
234bfeeae90SHong Zhang       col = in[l] ;
2357eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2367eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2377eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2387eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2397eb43aa7SLois Curfman McInnes         else             low  = t;
2407eb43aa7SLois Curfman McInnes       }
2417eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2427eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2437eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
244b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2457eb43aa7SLois Curfman McInnes           goto finished;
2467eb43aa7SLois Curfman McInnes         }
2477eb43aa7SLois Curfman McInnes       }
248b49de8d1SLois Curfman McInnes       *v++ = zero;
2497eb43aa7SLois Curfman McInnes       finished:;
2507eb43aa7SLois Curfman McInnes     }
2517eb43aa7SLois Curfman McInnes   }
2523a40ed3dSBarry Smith   PetscFunctionReturn(0);
2537eb43aa7SLois Curfman McInnes }
2547eb43aa7SLois Curfman McInnes 
25517ab2063SBarry Smith 
2564a2ae208SSatish Balay #undef __FUNCT__
2574a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
258b0a32e0cSBarry Smith int MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
25917ab2063SBarry Smith {
260416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
261416022c9SBarry Smith   int        i,fd,*col_lens,ierr;
26217ab2063SBarry Smith 
2633a40ed3dSBarry Smith   PetscFunctionBegin;
264b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
265b0a32e0cSBarry Smith   ierr = PetscMalloc((4+A->m)*sizeof(int),&col_lens);CHKERRQ(ierr);
266552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
267273d9f13SBarry Smith   col_lens[1] = A->m;
268273d9f13SBarry Smith   col_lens[2] = A->n;
269416022c9SBarry Smith   col_lens[3] = a->nz;
270416022c9SBarry Smith 
271416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
272273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
273416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
27417ab2063SBarry Smith   }
275273d9f13SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->m,PETSC_INT,1);CHKERRQ(ierr);
276606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
277416022c9SBarry Smith 
278416022c9SBarry Smith   /* store column indices (zero start index) */
2790752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,0);CHKERRQ(ierr);
280416022c9SBarry Smith 
281416022c9SBarry Smith   /* store nonzero values */
2820752156aSBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,0);CHKERRQ(ierr);
2833a40ed3dSBarry Smith   PetscFunctionReturn(0);
28417ab2063SBarry Smith }
285416022c9SBarry Smith 
2864ffef66eSHong Zhang extern int MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
287cd155464SBarry Smith 
2884a2ae208SSatish Balay #undef __FUNCT__
2894a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
290b0a32e0cSBarry Smith int MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
291416022c9SBarry Smith {
292416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
293bfeeae90SHong Zhang   int               ierr,i,j,m = A->m,shift=0;
294fb9695e5SSatish Balay   char              *name;
295f3ef73ceSBarry Smith   PetscViewerFormat format;
29617ab2063SBarry Smith 
2973a40ed3dSBarry Smith   PetscFunctionBegin;
298435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
299b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
300456192e2SBarry Smith   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL || format == PETSC_VIEWER_ASCII_INFO) {
3016831982aSBarry Smith     if (a->inode.size) {
302b0a32e0cSBarry 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);
3036831982aSBarry Smith     } else {
304b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"not using I-node routines\n");CHKERRQ(ierr);
3056831982aSBarry Smith     }
306fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
307d00d2cf4SBarry Smith     int nofinalvalue = 0;
308273d9f13SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->n-!shift)) {
309d00d2cf4SBarry Smith       nofinalvalue = 1;
310d00d2cf4SBarry Smith     }
311b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
312b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %d %d \n",m,A->n);CHKERRQ(ierr);
313b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %d \n",a->nz);CHKERRQ(ierr);
314b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%d,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
315b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
31617ab2063SBarry Smith 
31717ab2063SBarry Smith     for (i=0; i<m; i++) {
318416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
319aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
320b0a32e0cSBarry 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);
32117ab2063SBarry Smith #else
322b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
32317ab2063SBarry Smith #endif
32417ab2063SBarry Smith       }
32517ab2063SBarry Smith     }
326d00d2cf4SBarry Smith     if (nofinalvalue) {
327b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%d %d  %18.16e\n",m,A->n,0.0);CHKERRQ(ierr);
328d00d2cf4SBarry Smith     }
329fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
330b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
331cd155464SBarry Smith   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
33216ce804cSBarry Smith #if defined(PETSC_HAVE_MATLAB) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_COMPLEX)
3334ffef66eSHong Zhang      ierr = MatSeqAIJFactorInfo_Matlab(A,viewer);CHKERRQ(ierr);
3344ffef66eSHong Zhang #endif
335cd155464SBarry Smith      PetscFunctionReturn(0);
336fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
337b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
33844cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
339b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr);
34044cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
341aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
34236db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
34362b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
34436db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
34562b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
34636db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
34762b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3486831982aSBarry Smith         }
34944cd7ae7SLois Curfman McInnes #else
35062b941d6SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
35144cd7ae7SLois Curfman McInnes #endif
35244cd7ae7SLois Curfman McInnes       }
353b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
35444cd7ae7SLois Curfman McInnes     }
355b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
356fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
357496be53dSLois Curfman McInnes     int nzd=0,fshift=1,*sptr;
358b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
359b0a32e0cSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(int),&sptr);CHKERRQ(ierr);
360496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
361496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
362496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
363496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
364aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
36536db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
366496be53dSLois Curfman McInnes #else
367496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
368496be53dSLois Curfman McInnes #endif
369496be53dSLois Curfman McInnes         }
370496be53dSLois Curfman McInnes       }
371496be53dSLois Curfman McInnes     }
3722e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
373b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %d %d\n\n",m,nzd);CHKERRQ(ierr);
3742e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
375b0a32e0cSBarry 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);}
376b0a32e0cSBarry 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);}
377b0a32e0cSBarry 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);}
378b0a32e0cSBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
379b0a32e0cSBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %d %d\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
380b0a32e0cSBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %d\n",sptr[i]);CHKERRQ(ierr);}
381496be53dSLois Curfman McInnes     }
382b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
383606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
384496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
385496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
386b0a32e0cSBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %d ",a->j[j]+fshift);CHKERRQ(ierr);}
387496be53dSLois Curfman McInnes       }
388b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
389496be53dSLois Curfman McInnes     }
390b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
391496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
392496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
393496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
394aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
39536db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
396b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
3976831982aSBarry Smith           }
398496be53dSLois Curfman McInnes #else
399b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
400496be53dSLois Curfman McInnes #endif
401496be53dSLois Curfman McInnes         }
402496be53dSLois Curfman McInnes       }
403b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
404496be53dSLois Curfman McInnes     }
405b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
406fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
40702594712SBarry Smith     int         cnt = 0,jcnt;
40887828ca2SBarry Smith     PetscScalar value;
40902594712SBarry Smith 
410b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
41102594712SBarry Smith     for (i=0; i<m; i++) {
41202594712SBarry Smith       jcnt = 0;
413273d9f13SBarry Smith       for (j=0; j<A->n; j++) {
414e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
41502594712SBarry Smith           value = a->a[cnt++];
416e24b481bSBarry Smith           jcnt++;
41702594712SBarry Smith         } else {
41802594712SBarry Smith           value = 0.0;
41902594712SBarry Smith         }
420aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
421b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
42202594712SBarry Smith #else
423b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
42402594712SBarry Smith #endif
42502594712SBarry Smith       }
426b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
42702594712SBarry Smith     }
428b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4293a40ed3dSBarry Smith   } else {
430b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
43117ab2063SBarry Smith     for (i=0; i<m; i++) {
432b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr);
433416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
434aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
43536db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
43662b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
43736db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
43862b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4393a40ed3dSBarry Smith         } else {
44062b941d6SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
44117ab2063SBarry Smith         }
44217ab2063SBarry Smith #else
44362b941d6SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
44417ab2063SBarry Smith #endif
44517ab2063SBarry Smith       }
446b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
44717ab2063SBarry Smith     }
448b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
44917ab2063SBarry Smith   }
450b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4513a40ed3dSBarry Smith   PetscFunctionReturn(0);
452416022c9SBarry Smith }
453416022c9SBarry Smith 
4544a2ae208SSatish Balay #undef __FUNCT__
4554a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
456b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
457416022c9SBarry Smith {
458480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
459416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
460bfeeae90SHong Zhang   int               ierr,i,j,m = A->m,color;
46136db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
462b0a32e0cSBarry Smith   PetscViewer       viewer;
463f3ef73ceSBarry Smith   PetscViewerFormat format;
464cddf8d76SBarry Smith 
4653a40ed3dSBarry Smith   PetscFunctionBegin;
466480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
467b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
46819bcc07fSBarry Smith 
469b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
470416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4710513a670SBarry Smith 
472fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
4730513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
474b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
475416022c9SBarry Smith     for (i=0; i<m; i++) {
476cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
477bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
478bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
479aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
48036db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
481cddf8d76SBarry Smith #else
482cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
483cddf8d76SBarry Smith #endif
484b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
485cddf8d76SBarry Smith       }
486cddf8d76SBarry Smith     }
487b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
488cddf8d76SBarry Smith     for (i=0; i<m; i++) {
489cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
490bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
491bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
492cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
493b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
494cddf8d76SBarry Smith       }
495cddf8d76SBarry Smith     }
496b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
497cddf8d76SBarry Smith     for (i=0; i<m; i++) {
498cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
499bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
500bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
501aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
50236db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
503cddf8d76SBarry Smith #else
504cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
505cddf8d76SBarry Smith #endif
506b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
507416022c9SBarry Smith       }
508416022c9SBarry Smith     }
5090513a670SBarry Smith   } else {
5100513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5110513a670SBarry Smith     /* first determine max of all nonzero values */
5120513a670SBarry Smith     int    nz = a->nz,count;
513b0a32e0cSBarry Smith     PetscDraw   popup;
51436db0b34SBarry Smith     PetscReal scale;
5150513a670SBarry Smith 
5160513a670SBarry Smith     for (i=0; i<nz; i++) {
5170513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5180513a670SBarry Smith     }
519b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
520b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
521b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5220513a670SBarry Smith     count = 0;
5230513a670SBarry Smith     for (i=0; i<m; i++) {
5240513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
525bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
526bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
527b0a32e0cSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(a->a[count]));
528b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5290513a670SBarry Smith         count++;
5300513a670SBarry Smith       }
5310513a670SBarry Smith     }
5320513a670SBarry Smith   }
533480ef9eaSBarry Smith   PetscFunctionReturn(0);
534480ef9eaSBarry Smith }
535cddf8d76SBarry Smith 
5364a2ae208SSatish Balay #undef __FUNCT__
5374a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
538b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
539480ef9eaSBarry Smith {
540480ef9eaSBarry Smith   int        ierr;
541b0a32e0cSBarry Smith   PetscDraw  draw;
54236db0b34SBarry Smith   PetscReal  xr,yr,xl,yl,h,w;
543480ef9eaSBarry Smith   PetscTruth isnull;
544480ef9eaSBarry Smith 
545480ef9eaSBarry Smith   PetscFunctionBegin;
546b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
547b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
548480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
549480ef9eaSBarry Smith 
550480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
551273d9f13SBarry Smith   xr  = A->n; yr = A->m; h = yr/10.0; w = xr/10.0;
552480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
553b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
554b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
555480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5563a40ed3dSBarry Smith   PetscFunctionReturn(0);
557416022c9SBarry Smith }
558416022c9SBarry Smith 
5594a2ae208SSatish Balay #undef __FUNCT__
5604a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
561b0a32e0cSBarry Smith int MatView_SeqAIJ(Mat A,PetscViewer viewer)
562416022c9SBarry Smith {
563416022c9SBarry Smith   Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
564bcd2baecSBarry Smith   int         ierr;
5656831982aSBarry Smith   PetscTruth  issocket,isascii,isbinary,isdraw;
566416022c9SBarry Smith 
5673a40ed3dSBarry Smith   PetscFunctionBegin;
568b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
569b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
570fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
571fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
5720f5bd95cSBarry Smith   if (issocket) {
573b0a32e0cSBarry Smith     ierr = PetscViewerSocketPutSparse_Private(viewer,A->m,A->n,a->nz,a->a,a->i,a->j);CHKERRQ(ierr);
5740f5bd95cSBarry Smith   } else if (isascii) {
5753a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
5760f5bd95cSBarry Smith   } else if (isbinary) {
5773a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
5780f5bd95cSBarry Smith   } else if (isdraw) {
5793a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
5805cd90555SBarry Smith   } else {
58129bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
58217ab2063SBarry Smith   }
5833a40ed3dSBarry Smith   PetscFunctionReturn(0);
58417ab2063SBarry Smith }
58519bcc07fSBarry Smith 
5864a2ae208SSatish Balay #undef __FUNCT__
5874a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
5888f6be9afSLois Curfman McInnes int MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
58917ab2063SBarry Smith {
590416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
59141c01911SSatish Balay   int          fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax,ierr;
592bfeeae90SHong Zhang   int          m = A->m,*ip,N,*ailen = a->ilen,rmax = 0;
59387828ca2SBarry Smith   PetscScalar  *aa = a->a,*ap;
59417ab2063SBarry Smith 
5953a40ed3dSBarry Smith   PetscFunctionBegin;
5963a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
59717ab2063SBarry Smith 
59843ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
59917ab2063SBarry Smith   for (i=1; i<m; i++) {
600416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
60117ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
60294a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
60317ab2063SBarry Smith     if (fshift) {
604bfeeae90SHong Zhang       ip = aj + ai[i] ;
605bfeeae90SHong Zhang       ap = aa + ai[i] ;
60617ab2063SBarry Smith       N  = ailen[i];
60717ab2063SBarry Smith       for (j=0; j<N; j++) {
60817ab2063SBarry Smith         ip[j-fshift] = ip[j];
60917ab2063SBarry Smith         ap[j-fshift] = ap[j];
61017ab2063SBarry Smith       }
61117ab2063SBarry Smith     }
61217ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
61317ab2063SBarry Smith   }
61417ab2063SBarry Smith   if (m) {
61517ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
61617ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
61717ab2063SBarry Smith   }
61817ab2063SBarry Smith   /* reset ilen and imax for each row */
61917ab2063SBarry Smith   for (i=0; i<m; i++) {
62017ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
62117ab2063SBarry Smith   }
622bfeeae90SHong Zhang   a->nz = ai[m];
62317ab2063SBarry Smith 
62417ab2063SBarry Smith   /* diagonals may have moved, so kill the diagonal pointers */
625416022c9SBarry Smith   if (fshift && a->diag) {
626606d414cSSatish Balay     ierr = PetscFree(a->diag);CHKERRQ(ierr);
627b0a32e0cSBarry Smith     PetscLogObjectMemory(A,-(m+1)*sizeof(int));
628416022c9SBarry Smith     a->diag = 0;
62917ab2063SBarry Smith   }
630b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded,%d used\n",m,A->n,fshift,a->nz);
631b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %d\n",a->reallocs);
632b0a32e0cSBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Most nonzeros in any row is %d\n",rmax);
633dd5f02e7SSatish Balay   a->reallocs          = 0;
6344e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
63536db0b34SBarry Smith   a->rmax              = rmax;
6364e220ebcSLois Curfman McInnes 
63776dd722bSSatish Balay   /* check out for identical nodes. If found, use inode functions */
6383a7fca6bSBarry Smith   ierr = Mat_AIJ_CheckInode(A,(PetscTruth)(!fshift));CHKERRQ(ierr);
639cfef3cccSHong Zhang 
6403a40ed3dSBarry Smith   PetscFunctionReturn(0);
64117ab2063SBarry Smith }
64217ab2063SBarry Smith 
6434a2ae208SSatish Balay #undef __FUNCT__
6444a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
6458f6be9afSLois Curfman McInnes int MatZeroEntries_SeqAIJ(Mat A)
64617ab2063SBarry Smith {
647416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
648549d3d68SSatish Balay   int        ierr;
6493a40ed3dSBarry Smith 
6503a40ed3dSBarry Smith   PetscFunctionBegin;
651bfeeae90SHong Zhang   ierr = PetscMemzero(a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
6523a40ed3dSBarry Smith   PetscFunctionReturn(0);
65317ab2063SBarry Smith }
654416022c9SBarry Smith 
6554a2ae208SSatish Balay #undef __FUNCT__
6564a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
657e1311b90SBarry Smith int MatDestroy_SeqAIJ(Mat A)
65817ab2063SBarry Smith {
659416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
66082bf6240SBarry Smith   int        ierr;
661d5d45c9bSBarry Smith 
6623a40ed3dSBarry Smith   PetscFunctionBegin;
663aa482453SBarry Smith #if defined(PETSC_USE_LOG)
664b0a32e0cSBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%d, Cols=%d, NZ=%d",A->m,A->n,a->nz);
66517ab2063SBarry Smith #endif
66636db0b34SBarry Smith   if (a->freedata) {
667606d414cSSatish Balay     ierr = PetscFree(a->a);CHKERRQ(ierr);
668606d414cSSatish Balay     if (!a->singlemalloc) {
669606d414cSSatish Balay       ierr = PetscFree(a->i);CHKERRQ(ierr);
670606d414cSSatish Balay       ierr = PetscFree(a->j);CHKERRQ(ierr);
671606d414cSSatish Balay     }
67236db0b34SBarry Smith   }
673c38d4ed2SBarry Smith   if (a->row) {
674c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
675c38d4ed2SBarry Smith   }
676c38d4ed2SBarry Smith   if (a->col) {
677c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
678c38d4ed2SBarry Smith   }
679606d414cSSatish Balay   if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
680606d414cSSatish Balay   if (a->ilen) {ierr = PetscFree(a->ilen);CHKERRQ(ierr);}
681606d414cSSatish Balay   if (a->imax) {ierr = PetscFree(a->imax);CHKERRQ(ierr);}
682273d9f13SBarry Smith   if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);}
683606d414cSSatish Balay   if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);}
684606d414cSSatish Balay   if (a->inode.size) {ierr = PetscFree(a->inode.size);CHKERRQ(ierr);}
68582bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
686606d414cSSatish Balay   if (a->saved_values) {ierr = PetscFree(a->saved_values);CHKERRQ(ierr);}
687cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
688a30b2313SHong Zhang   if (a->xtoy) {ierr = PetscFree(a->xtoy);CHKERRQ(ierr);}
689a30b2313SHong Zhang 
690606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
6913a40ed3dSBarry Smith   PetscFunctionReturn(0);
69217ab2063SBarry Smith }
69317ab2063SBarry Smith 
6944a2ae208SSatish Balay #undef __FUNCT__
6954a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
6968f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A)
69717ab2063SBarry Smith {
6983a40ed3dSBarry Smith   PetscFunctionBegin;
6993a40ed3dSBarry Smith   PetscFunctionReturn(0);
70017ab2063SBarry Smith }
70117ab2063SBarry Smith 
7024a2ae208SSatish Balay #undef __FUNCT__
7034a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
7048f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op)
70517ab2063SBarry Smith {
706416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7073a40ed3dSBarry Smith 
7083a40ed3dSBarry Smith   PetscFunctionBegin;
709a65d3064SKris Buschelman   switch (op) {
710a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
711a65d3064SKris Buschelman       a->roworiented       = PETSC_TRUE;
712a65d3064SKris Buschelman       break;
713a65d3064SKris Buschelman     case MAT_KEEP_ZEROED_ROWS:
714a65d3064SKris Buschelman       a->keepzeroedrows    = PETSC_TRUE;
715a65d3064SKris Buschelman       break;
716a65d3064SKris Buschelman     case MAT_COLUMN_ORIENTED:
717a65d3064SKris Buschelman       a->roworiented       = PETSC_FALSE;
718a65d3064SKris Buschelman       break;
719a65d3064SKris Buschelman     case MAT_COLUMNS_SORTED:
720a65d3064SKris Buschelman       a->sorted            = PETSC_TRUE;
721a65d3064SKris Buschelman       break;
722a65d3064SKris Buschelman     case MAT_COLUMNS_UNSORTED:
723a65d3064SKris Buschelman       a->sorted            = PETSC_FALSE;
724a65d3064SKris Buschelman       break;
725a65d3064SKris Buschelman     case MAT_NO_NEW_NONZERO_LOCATIONS:
726a65d3064SKris Buschelman       a->nonew             = 1;
727a65d3064SKris Buschelman       break;
728a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
729a65d3064SKris Buschelman       a->nonew             = -1;
730a65d3064SKris Buschelman       break;
731a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
732a65d3064SKris Buschelman       a->nonew             = -2;
733a65d3064SKris Buschelman       break;
734a65d3064SKris Buschelman     case MAT_YES_NEW_NONZERO_LOCATIONS:
735a65d3064SKris Buschelman       a->nonew             = 0;
736a65d3064SKris Buschelman       break;
737a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
738a65d3064SKris Buschelman       a->ignorezeroentries = PETSC_TRUE;
739a65d3064SKris Buschelman       break;
740a65d3064SKris Buschelman     case MAT_USE_INODES:
741a65d3064SKris Buschelman       a->inode.use         = PETSC_TRUE;
742a65d3064SKris Buschelman       break;
743a65d3064SKris Buschelman     case MAT_DO_NOT_USE_INODES:
744a65d3064SKris Buschelman       a->inode.use         = PETSC_FALSE;
745a65d3064SKris Buschelman       break;
746a65d3064SKris Buschelman     case MAT_ROWS_SORTED:
747a65d3064SKris Buschelman     case MAT_ROWS_UNSORTED:
748a65d3064SKris Buschelman     case MAT_YES_NEW_DIAGONALS:
749a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
750a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
751b0a32e0cSBarry Smith       PetscLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n");
752a65d3064SKris Buschelman       break;
753a65d3064SKris Buschelman     case MAT_NO_NEW_DIAGONALS:
75429bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
755a65d3064SKris Buschelman     case MAT_INODE_LIMIT_1:
756a65d3064SKris Buschelman       a->inode.limit  = 1;
757a65d3064SKris Buschelman       break;
758a65d3064SKris Buschelman     case MAT_INODE_LIMIT_2:
759a65d3064SKris Buschelman       a->inode.limit  = 2;
760a65d3064SKris Buschelman       break;
761a65d3064SKris Buschelman     case MAT_INODE_LIMIT_3:
762a65d3064SKris Buschelman       a->inode.limit  = 3;
763a65d3064SKris Buschelman       break;
764a65d3064SKris Buschelman     case MAT_INODE_LIMIT_4:
765a65d3064SKris Buschelman       a->inode.limit  = 4;
766a65d3064SKris Buschelman       break;
767a65d3064SKris Buschelman     case MAT_INODE_LIMIT_5:
768a65d3064SKris Buschelman       a->inode.limit  = 5;
769a65d3064SKris Buschelman       break;
77077e54ba9SKris Buschelman     case MAT_SYMMETRIC:
77177e54ba9SKris Buschelman     case MAT_STRUCTURALLY_SYMMETRIC:
7729a4540c5SBarry Smith     case MAT_NOT_SYMMETRIC:
7739a4540c5SBarry Smith     case MAT_NOT_STRUCTURALLY_SYMMETRIC:
7749a4540c5SBarry Smith     case MAT_HERMITIAN:
7759a4540c5SBarry Smith     case MAT_NOT_HERMITIAN:
7769a4540c5SBarry Smith     case MAT_SYMMETRY_ETERNAL:
7779a4540c5SBarry Smith     case MAT_NOT_SYMMETRY_ETERNAL:
77877e54ba9SKris Buschelman       break;
779a65d3064SKris Buschelman     default:
780a65d3064SKris Buschelman       SETERRQ(PETSC_ERR_SUP,"unknown option");
781a65d3064SKris Buschelman   }
7823a40ed3dSBarry Smith   PetscFunctionReturn(0);
78317ab2063SBarry Smith }
78417ab2063SBarry Smith 
7854a2ae208SSatish Balay #undef __FUNCT__
7864a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
7878f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v)
78817ab2063SBarry Smith {
789416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
790bfeeae90SHong Zhang   int          i,j,n,ierr;
79187828ca2SBarry Smith   PetscScalar  *x,zero = 0.0;
79217ab2063SBarry Smith 
7933a40ed3dSBarry Smith   PetscFunctionBegin;
7943a40ed3dSBarry Smith   ierr = VecSet(&zero,v);CHKERRQ(ierr);
795ed480e8bSBarry Smith   ierr = VecGetArrayFast(v,&x);CHKERRQ(ierr);
79636db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
797273d9f13SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
798273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
799bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
800bfeeae90SHong Zhang       if (a->j[j] == i) {
801416022c9SBarry Smith         x[i] = a->a[j];
80217ab2063SBarry Smith         break;
80317ab2063SBarry Smith       }
80417ab2063SBarry Smith     }
80517ab2063SBarry Smith   }
806ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(v,&x);CHKERRQ(ierr);
8073a40ed3dSBarry Smith   PetscFunctionReturn(0);
80817ab2063SBarry Smith }
80917ab2063SBarry Smith 
810d5d45c9bSBarry Smith 
8114a2ae208SSatish Balay #undef __FUNCT__
8124a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
8137c922b88SBarry Smith int MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
81417ab2063SBarry Smith {
815416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
8165c897100SBarry Smith   PetscScalar  *x,*y;
817bfeeae90SHong Zhang   int          ierr,m = A->m;
8185c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
8195c897100SBarry Smith   PetscScalar  *v,alpha;
8205c897100SBarry Smith   int          n,i,*idx;
8215c897100SBarry Smith #endif
82217ab2063SBarry Smith 
8233a40ed3dSBarry Smith   PetscFunctionBegin;
8242e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
825ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
826ed480e8bSBarry Smith   ierr = VecGetArrayFast(yy,&y);CHKERRQ(ierr);
8275c897100SBarry Smith 
8285c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
829bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
8305c897100SBarry Smith #else
83117ab2063SBarry Smith   for (i=0; i<m; i++) {
832bfeeae90SHong Zhang     idx   = a->j + a->i[i] ;
833bfeeae90SHong Zhang     v     = a->a + a->i[i] ;
834416022c9SBarry Smith     n     = a->i[i+1] - a->i[i];
83517ab2063SBarry Smith     alpha = x[i];
83617ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
83717ab2063SBarry Smith   }
8385c897100SBarry Smith #endif
839b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
840ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
841ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(yy,&y);CHKERRQ(ierr);
8423a40ed3dSBarry Smith   PetscFunctionReturn(0);
84317ab2063SBarry Smith }
84417ab2063SBarry Smith 
8454a2ae208SSatish Balay #undef __FUNCT__
8465c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
8475c897100SBarry Smith int MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
8485c897100SBarry Smith {
8498d5b0100SBarry Smith   PetscScalar  zero = 0.0;
8505c897100SBarry Smith   int          ierr;
8515c897100SBarry Smith 
8525c897100SBarry Smith   PetscFunctionBegin;
8535c897100SBarry Smith   ierr = VecSet(&zero,yy);CHKERRQ(ierr);
8545c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
8555c897100SBarry Smith   PetscFunctionReturn(0);
8565c897100SBarry Smith }
8575c897100SBarry Smith 
8585c897100SBarry Smith 
8595c897100SBarry Smith #undef __FUNCT__
8604a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
86144cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
86217ab2063SBarry Smith {
863416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
864362ced78SSatish Balay   PetscScalar  *x,*y,*v;
865bfeeae90SHong Zhang   int          ierr,m = A->m,*idx,*ii;
866aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
867e36a17ebSSatish Balay   int          n,i,jrow,j;
868362ced78SSatish Balay   PetscScalar  sum;
869e36a17ebSSatish Balay #endif
87017ab2063SBarry Smith 
871b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
872fee21e36SBarry Smith #pragma disjoint(*x,*y,*v)
873fee21e36SBarry Smith #endif
874fee21e36SBarry Smith 
8753a40ed3dSBarry Smith   PetscFunctionBegin;
876ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
877ed480e8bSBarry Smith   ierr = VecGetArrayFast(yy,&y);CHKERRQ(ierr);
878416022c9SBarry Smith   idx  = a->j;
879d64ed03dSBarry Smith   v    = a->a;
880416022c9SBarry Smith   ii   = a->i;
881aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
882bfeeae90SHong Zhang   fortranmultaij_(&m,x,ii,idx,v,y);
8838d195f9aSBarry Smith #else
88417ab2063SBarry Smith   for (i=0; i<m; i++) {
8859ea0dfa2SSatish Balay     jrow = ii[i];
8869ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
88717ab2063SBarry Smith     sum  = 0.0;
8889ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
8899ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
8909ea0dfa2SSatish Balay      }
89117ab2063SBarry Smith     y[i] = sum;
89217ab2063SBarry Smith   }
8938d195f9aSBarry Smith #endif
894b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz - m);
895ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
896ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(yy,&y);CHKERRQ(ierr);
8973a40ed3dSBarry Smith   PetscFunctionReturn(0);
89817ab2063SBarry Smith }
89917ab2063SBarry Smith 
9004a2ae208SSatish Balay #undef __FUNCT__
9014a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
90244cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
90317ab2063SBarry Smith {
904416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
905362ced78SSatish Balay   PetscScalar  *x,*y,*z,*v;
906bfeeae90SHong Zhang   int          ierr,m = A->m,*idx,*ii;
907aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
908e36a17ebSSatish Balay   int          n,i,jrow,j;
909362ced78SSatish Balay PetscScalar    sum;
910e36a17ebSSatish Balay #endif
9119ea0dfa2SSatish Balay 
9123a40ed3dSBarry Smith   PetscFunctionBegin;
913ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
914ed480e8bSBarry Smith   ierr = VecGetArrayFast(yy,&y);CHKERRQ(ierr);
9152e8a6d31SBarry Smith   if (zz != yy) {
916ed480e8bSBarry Smith     ierr = VecGetArrayFast(zz,&z);CHKERRQ(ierr);
9172e8a6d31SBarry Smith   } else {
9182e8a6d31SBarry Smith     z = y;
9192e8a6d31SBarry Smith   }
920bfeeae90SHong Zhang 
921cddf8d76SBarry Smith   idx  = a->j;
922d64ed03dSBarry Smith   v    = a->a;
923cddf8d76SBarry Smith   ii   = a->i;
924aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
925bfeeae90SHong Zhang   fortranmultaddaij_(&m,x,ii,idx,v,y,z);
92602ab625aSSatish Balay #else
92717ab2063SBarry Smith   for (i=0; i<m; i++) {
9289ea0dfa2SSatish Balay     jrow = ii[i];
9299ea0dfa2SSatish Balay     n    = ii[i+1] - jrow;
93017ab2063SBarry Smith     sum  = y[i];
9319ea0dfa2SSatish Balay     for (j=0; j<n; j++) {
9329ea0dfa2SSatish Balay       sum += v[jrow]*x[idx[jrow]]; jrow++;
9339ea0dfa2SSatish Balay      }
93417ab2063SBarry Smith     z[i] = sum;
93517ab2063SBarry Smith   }
93602ab625aSSatish Balay #endif
937b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
938ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
939ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(yy,&y);CHKERRQ(ierr);
9402e8a6d31SBarry Smith   if (zz != yy) {
941ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(zz,&z);CHKERRQ(ierr);
9422e8a6d31SBarry Smith   }
9433a40ed3dSBarry Smith   PetscFunctionReturn(0);
94417ab2063SBarry Smith }
94517ab2063SBarry Smith 
94617ab2063SBarry Smith /*
94717ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
94817ab2063SBarry Smith */
9494a2ae208SSatish Balay #undef __FUNCT__
9504a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
951f1e2ffcdSBarry Smith int MatMarkDiagonal_SeqAIJ(Mat A)
95217ab2063SBarry Smith {
953416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
954bfeeae90SHong Zhang   int        i,j,*diag,m = A->m,ierr;
95517ab2063SBarry Smith 
9563a40ed3dSBarry Smith   PetscFunctionBegin;
957f1e2ffcdSBarry Smith   if (a->diag) PetscFunctionReturn(0);
958f1e2ffcdSBarry Smith 
959b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&diag);CHKERRQ(ierr);
960b0a32e0cSBarry Smith   PetscLogObjectMemory(A,(m+1)*sizeof(int));
961273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
96235b0346bSBarry Smith     diag[i] = a->i[i+1];
963bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
964bfeeae90SHong Zhang       if (a->j[j] == i) {
965bfeeae90SHong Zhang         diag[i] = j;
96617ab2063SBarry Smith         break;
96717ab2063SBarry Smith       }
96817ab2063SBarry Smith     }
96917ab2063SBarry Smith   }
970416022c9SBarry Smith   a->diag = diag;
9713a40ed3dSBarry Smith   PetscFunctionReturn(0);
97217ab2063SBarry Smith }
97317ab2063SBarry Smith 
974be5855fcSBarry Smith /*
975be5855fcSBarry Smith      Checks for missing diagonals
976be5855fcSBarry Smith */
9774a2ae208SSatish Balay #undef __FUNCT__
9784a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
979f1e2ffcdSBarry Smith int MatMissingDiagonal_SeqAIJ(Mat A)
980be5855fcSBarry Smith {
981be5855fcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
982bfeeae90SHong Zhang   int        *diag,*jj = a->j,i,ierr;
983be5855fcSBarry Smith 
984be5855fcSBarry Smith   PetscFunctionBegin;
985f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
986f1e2ffcdSBarry Smith   diag = a->diag;
987273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
988bfeeae90SHong Zhang     if (jj[diag[i]] != i) {
98929bbc08cSBarry Smith       SETERRQ1(1,"Matrix is missing diagonal number %d",i);
990be5855fcSBarry Smith     }
991be5855fcSBarry Smith   }
992be5855fcSBarry Smith   PetscFunctionReturn(0);
993be5855fcSBarry Smith }
994be5855fcSBarry Smith 
9954a2ae208SSatish Balay #undef __FUNCT__
9964a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
997c14dc6b6SHong Zhang int MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx)
99817ab2063SBarry Smith {
999416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1000beeb8507SBarry Smith   PetscScalar        *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag;
1001beeb8507SBarry Smith   const PetscScalar  *v = a->a, *b, *bs,*xb, *ts;
1002beeb8507SBarry Smith   int                ierr,n = A->n,m = A->m,i;
1003beeb8507SBarry Smith   const int          *idx,*diag;
100417ab2063SBarry Smith 
10053a40ed3dSBarry Smith   PetscFunctionBegin;
1006b965ef7fSBarry Smith   its = its*lits;
100791723122SBarry Smith   if (its <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits);
100891723122SBarry Smith 
1009ed480e8bSBarry Smith   if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);}
1010ed480e8bSBarry Smith   diag = a->diag;
1011ed480e8bSBarry Smith   if (!a->idiag) {
1012ed480e8bSBarry Smith     ierr     = PetscMalloc(3*m*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr);
1013ed480e8bSBarry Smith     a->ssor  = a->idiag + m;
1014ed480e8bSBarry Smith     mdiag    = a->ssor + m;
1015ed480e8bSBarry Smith 
1016ed480e8bSBarry Smith     v        = a->a;
1017ed480e8bSBarry Smith 
1018ed480e8bSBarry Smith     /* this is wrong when fshift omega changes each iteration */
1019ed480e8bSBarry Smith     if (omega == 1.0 && fshift == 0.0) {
1020ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1021ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1022ed480e8bSBarry Smith         a->idiag[i] = 1.0/v[diag[i]];
1023ed480e8bSBarry Smith       }
1024beeb8507SBarry Smith       PetscLogFlops(m);
1025ed480e8bSBarry Smith     } else {
1026ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1027ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1028beeb8507SBarry Smith         a->idiag[i] = omega/(fshift + v[diag[i]]);
1029ed480e8bSBarry Smith       }
1030beeb8507SBarry Smith       PetscLogFlops(2*m);
1031beeb8507SBarry Smith     }
1032ed480e8bSBarry Smith   }
1033ed480e8bSBarry Smith   t     = a->ssor;
1034ed480e8bSBarry Smith   idiag = a->idiag;
1035ed480e8bSBarry Smith   mdiag = a->idiag + 2*m;
1036ed480e8bSBarry Smith 
1037ed480e8bSBarry Smith   ierr = VecGetArrayFast(xx,&x);CHKERRQ(ierr);
1038fb2e594dSBarry Smith   if (xx != bb) {
1039beeb8507SBarry Smith     ierr = VecGetArrayFast(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1040fb2e594dSBarry Smith   } else {
1041fb2e594dSBarry Smith     b = x;
1042fb2e594dSBarry Smith   }
1043fb2e594dSBarry Smith 
1044ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1045ed480e8bSBarry Smith   xs   = x;
104617ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
104717ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1048ed480e8bSBarry Smith     bs = b;
104917ab2063SBarry Smith     for (i=0; i<m; i++) {
1050ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1051416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1052ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1053ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
105417ab2063SBarry Smith         sum  = b[i]*d/omega;
105517ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
105617ab2063SBarry Smith         x[i] = sum;
105717ab2063SBarry Smith     }
1058ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
1059beeb8507SBarry Smith     if (bb != xx) {ierr = VecRestoreArrayFast(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1060ed480e8bSBarry Smith     PetscLogFlops(a->nz);
10613a40ed3dSBarry Smith     PetscFunctionReturn(0);
106217ab2063SBarry Smith   }
1063c783ea89SBarry Smith 
1064ed480e8bSBarry Smith 
1065fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1066fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1067fc3d8934SBarry Smith 
1068fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1069fc3d8934SBarry Smith 
1070fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1071fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
107248af12d7SBarry Smith     is the relaxation factor.
1073fc3d8934SBarry Smith     */
1074fc3d8934SBarry Smith 
107548af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
107629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
10773a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
107817ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
107917ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
108017ab2063SBarry Smith 
108117ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
108217ab2063SBarry Smith 
108317ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
108417ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
108517ab2063SBarry Smith     is the relaxation factor.
108617ab2063SBarry Smith     */
108717ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
108817ab2063SBarry Smith 
108917ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
109017ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1091416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1092ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1093ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
109417ab2063SBarry Smith       sum  = b[i];
109517ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1096ed480e8bSBarry Smith       x[i] = sum*idiag[i];
109717ab2063SBarry Smith     }
109817ab2063SBarry Smith 
109917ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1100416022c9SBarry Smith     v = a->a;
1101ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
110217ab2063SBarry Smith 
110317ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1104ed480e8bSBarry Smith     ts = t;
1105416022c9SBarry Smith     diag = a->diag;
110617ab2063SBarry Smith     for (i=0; i<m; i++) {
1107416022c9SBarry Smith       n    = diag[i] - a->i[i];
1108ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1109ed480e8bSBarry Smith       v    = a->a + a->i[i];
111017ab2063SBarry Smith       sum  = t[i];
111117ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
1112ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1113733d66baSBarry Smith       /*  x = x + t */
1114733d66baSBarry Smith       x[i] += t[i];
111517ab2063SBarry Smith     }
111617ab2063SBarry Smith 
1117b0a32e0cSBarry Smith     PetscLogFlops(6*m-1 + 2*a->nz);
1118ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
1119beeb8507SBarry Smith     if (bb != xx) {ierr = VecRestoreArrayFast(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
11203a40ed3dSBarry Smith     PetscFunctionReturn(0);
112117ab2063SBarry Smith   }
112217ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
112317ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
112477d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
11250c559628SSatish Balay       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(int *)diag,idiag,a->a,(void*)b);
112677d8c4bbSBarry Smith #else
112717ab2063SBarry Smith       for (i=0; i<m; i++) {
1128416022c9SBarry Smith         n    = diag[i] - a->i[i];
1129ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1130ed480e8bSBarry Smith         v    = a->a + a->i[i];
113117ab2063SBarry Smith         sum  = b[i];
113217ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1133ed480e8bSBarry Smith         x[i] = sum*idiag[i];
113417ab2063SBarry Smith       }
113577d8c4bbSBarry Smith #endif
113617ab2063SBarry Smith       xb = x;
1137ed480e8bSBarry Smith       PetscLogFlops(a->nz);
11383a40ed3dSBarry Smith     } else xb = b;
113917ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
114017ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
114117ab2063SBarry Smith       for (i=0; i<m; i++) {
1142ed480e8bSBarry Smith         x[i] *= mdiag[i];
114317ab2063SBarry Smith       }
1144b0a32e0cSBarry Smith       PetscLogFlops(m);
114517ab2063SBarry Smith     }
114617ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
114777d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
11480c559628SSatish Balay       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(int*)diag,idiag,a->a,(void*)xb);
114977d8c4bbSBarry Smith #else
115017ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1151416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1152ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1153ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
115417ab2063SBarry Smith         sum  = xb[i];
115517ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1156ed480e8bSBarry Smith         x[i] = sum*idiag[i];
115717ab2063SBarry Smith       }
115877d8c4bbSBarry Smith #endif
1159ed480e8bSBarry Smith       PetscLogFlops(a->nz);
116017ab2063SBarry Smith     }
116117ab2063SBarry Smith     its--;
116217ab2063SBarry Smith   }
116317ab2063SBarry Smith   while (its--) {
116417ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
116577d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
11660c559628SSatish Balay       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(int*)diag,a->a,(void*)b);
116777d8c4bbSBarry Smith #else
116817ab2063SBarry Smith       for (i=0; i<m; i++) {
1169ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1170416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1171ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1172ed480e8bSBarry Smith         v    = a->a + a->i[i];
117317ab2063SBarry Smith         sum  = b[i];
117417ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1175ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
117617ab2063SBarry Smith       }
117777d8c4bbSBarry Smith #endif
1178ed480e8bSBarry Smith       PetscLogFlops(a->nz);
117917ab2063SBarry Smith     }
118017ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
118177d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
11820c559628SSatish Balay       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(int*)diag,a->a,(void*)b);
118377d8c4bbSBarry Smith #else
118417ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1185ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1186416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1187ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1188ed480e8bSBarry Smith         v    = a->a + a->i[i];
118917ab2063SBarry Smith         sum  = b[i];
119017ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1191ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
119217ab2063SBarry Smith       }
119377d8c4bbSBarry Smith #endif
1194ed480e8bSBarry Smith       PetscLogFlops(a->nz);
119517ab2063SBarry Smith     }
119617ab2063SBarry Smith   }
1197ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(xx,&x);CHKERRQ(ierr);
1198beeb8507SBarry Smith   if (bb != xx) {ierr = VecRestoreArrayFast(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
11993a40ed3dSBarry Smith   PetscFunctionReturn(0);
120017ab2063SBarry Smith }
120117ab2063SBarry Smith 
12024a2ae208SSatish Balay #undef __FUNCT__
12034a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
12048f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
120517ab2063SBarry Smith {
1206416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12074e220ebcSLois Curfman McInnes 
12083a40ed3dSBarry Smith   PetscFunctionBegin;
1209273d9f13SBarry Smith   info->rows_global    = (double)A->m;
1210273d9f13SBarry Smith   info->columns_global = (double)A->n;
1211273d9f13SBarry Smith   info->rows_local     = (double)A->m;
1212273d9f13SBarry Smith   info->columns_local  = (double)A->n;
12134e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
12144e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
12154e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
12164e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
12174e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
12184e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
12194e220ebcSLois Curfman McInnes   info->memory         = A->mem;
12204e220ebcSLois Curfman McInnes   if (A->factor) {
12214e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
12224e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
12234e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
12244e220ebcSLois Curfman McInnes   } else {
12254e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12264e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12274e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12284e220ebcSLois Curfman McInnes   }
12293a40ed3dSBarry Smith   PetscFunctionReturn(0);
123017ab2063SBarry Smith }
123117ab2063SBarry Smith 
12324a2ae208SSatish Balay #undef __FUNCT__
12334a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1234268466fbSBarry Smith int MatZeroRows_SeqAIJ(Mat A,IS is,const PetscScalar *diag)
123517ab2063SBarry Smith {
1236416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1237bfeeae90SHong Zhang   int         i,ierr,N,*rows,m = A->m - 1;
123817ab2063SBarry Smith 
12393a40ed3dSBarry Smith   PetscFunctionBegin;
1240b9b97703SBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
124117ab2063SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
1242f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1243f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1244a45adfd6SMatthew Knepley       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %d out of range", rows[i]);
1245bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1246f1e2ffcdSBarry Smith     }
1247f1e2ffcdSBarry Smith     if (diag) {
1248f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1249f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1250f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1251f1e2ffcdSBarry Smith         a->a[a->diag[rows[i]]] = *diag;
1252f1e2ffcdSBarry Smith       }
1253f1e2ffcdSBarry Smith     }
1254f1e2ffcdSBarry Smith   } else {
125517ab2063SBarry Smith     if (diag) {
125617ab2063SBarry Smith       for (i=0; i<N; i++) {
1257a45adfd6SMatthew Knepley         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %d out of range", rows[i]);
12587ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1259416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1260bfeeae90SHong Zhang           a->a[a->i[rows[i]]] = *diag;
1261bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
12627ae801bdSBarry Smith         } else { /* in case row was completely empty */
1263d64ed03dSBarry Smith           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr);
126417ab2063SBarry Smith         }
126517ab2063SBarry Smith       }
12663a40ed3dSBarry Smith     } else {
126717ab2063SBarry Smith       for (i=0; i<N; i++) {
1268a45adfd6SMatthew Knepley         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %d out of range", rows[i]);
1269416022c9SBarry Smith         a->ilen[rows[i]] = 0;
127017ab2063SBarry Smith       }
127117ab2063SBarry Smith     }
1272f1e2ffcdSBarry Smith   }
12737ae801bdSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
127443a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
12753a40ed3dSBarry Smith   PetscFunctionReturn(0);
127617ab2063SBarry Smith }
127717ab2063SBarry Smith 
12784a2ae208SSatish Balay #undef __FUNCT__
12794a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
128087828ca2SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,PetscScalar **v)
128117ab2063SBarry Smith {
1282416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1283b3fb0a6cSMatthew Knepley   int        *itmp;
128417ab2063SBarry Smith 
12853a40ed3dSBarry Smith   PetscFunctionBegin;
1286273d9f13SBarry Smith   if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %d out of range",row);
128717ab2063SBarry Smith 
1288416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1289bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
129017ab2063SBarry Smith   if (idx) {
1291bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1292bfeeae90SHong Zhang     if (*nz) {
12934e093b46SBarry Smith       *idx = itmp;
129417ab2063SBarry Smith     }
129517ab2063SBarry Smith     else *idx = 0;
129617ab2063SBarry Smith   }
12973a40ed3dSBarry Smith   PetscFunctionReturn(0);
129817ab2063SBarry Smith }
129917ab2063SBarry Smith 
1300bfeeae90SHong Zhang /* remove this function? */
13014a2ae208SSatish Balay #undef __FUNCT__
13024a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
130387828ca2SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,PetscScalar **v)
130417ab2063SBarry Smith {
1305bfeeae90SHong Zhang   /* Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1306bfeeae90SHong Zhang      int ierr; */
13073a40ed3dSBarry Smith 
13083a40ed3dSBarry Smith   PetscFunctionBegin;
1309bfeeae90SHong Zhang   /* if (idx) {if (*idx && a->indexshift) {ierr = PetscFree(*idx);CHKERRQ(ierr);}} */
13103a40ed3dSBarry Smith   PetscFunctionReturn(0);
131117ab2063SBarry Smith }
131217ab2063SBarry Smith 
13134a2ae208SSatish Balay #undef __FUNCT__
13144a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1315064f8208SBarry Smith int MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
131617ab2063SBarry Smith {
1317416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
131887828ca2SBarry Smith   PetscScalar  *v = a->a;
131936db0b34SBarry Smith   PetscReal    sum = 0.0;
1320bfeeae90SHong Zhang   int          i,j,ierr;
132117ab2063SBarry Smith 
13223a40ed3dSBarry Smith   PetscFunctionBegin;
132317ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1324416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1325aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
132636db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
132717ab2063SBarry Smith #else
132817ab2063SBarry Smith       sum += (*v)*(*v); v++;
132917ab2063SBarry Smith #endif
133017ab2063SBarry Smith     }
1331064f8208SBarry Smith     *nrm = sqrt(sum);
13323a40ed3dSBarry Smith   } else if (type == NORM_1) {
133336db0b34SBarry Smith     PetscReal *tmp;
1334416022c9SBarry Smith     int    *jj = a->j;
1335b0a32e0cSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1336273d9f13SBarry Smith     ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr);
1337064f8208SBarry Smith     *nrm = 0.0;
1338416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1339bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
134017ab2063SBarry Smith     }
1341273d9f13SBarry Smith     for (j=0; j<A->n; j++) {
1342064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
134317ab2063SBarry Smith     }
1344606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
13453a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1346064f8208SBarry Smith     *nrm = 0.0;
1347273d9f13SBarry Smith     for (j=0; j<A->m; j++) {
1348bfeeae90SHong Zhang       v = a->a + a->i[j];
134917ab2063SBarry Smith       sum = 0.0;
1350416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1351cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
135217ab2063SBarry Smith       }
1353064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
135417ab2063SBarry Smith     }
13553a40ed3dSBarry Smith   } else {
135629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
135717ab2063SBarry Smith   }
13583a40ed3dSBarry Smith   PetscFunctionReturn(0);
135917ab2063SBarry Smith }
136017ab2063SBarry Smith 
13614a2ae208SSatish Balay #undef __FUNCT__
13624a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
13638f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B)
136417ab2063SBarry Smith {
1365416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
1366416022c9SBarry Smith   Mat          C;
1367273d9f13SBarry Smith   int          i,ierr,*aj = a->j,*ai = a->i,m = A->m,len,*col;
136887828ca2SBarry Smith   PetscScalar  *array = a->a;
136917ab2063SBarry Smith 
13703a40ed3dSBarry Smith   PetscFunctionBegin;
1371273d9f13SBarry Smith   if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1372b0a32e0cSBarry Smith   ierr = PetscMalloc((1+A->n)*sizeof(int),&col);CHKERRQ(ierr);
1373273d9f13SBarry Smith   ierr = PetscMemzero(col,(1+A->n)*sizeof(int));CHKERRQ(ierr);
1374bfeeae90SHong Zhang 
1375bfeeae90SHong Zhang   for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
1376273d9f13SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,A->n,m,0,col,&C);CHKERRQ(ierr);
1377606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
137817ab2063SBarry Smith   for (i=0; i<m; i++) {
137917ab2063SBarry Smith     len    = ai[i+1]-ai[i];
1380416022c9SBarry Smith     ierr   = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1381b9b97703SBarry Smith     array += len;
1382b9b97703SBarry Smith     aj    += len;
138317ab2063SBarry Smith   }
138417ab2063SBarry Smith 
13856d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13866d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
138717ab2063SBarry Smith 
1388f1e2ffcdSBarry Smith   if (B) {
1389416022c9SBarry Smith     *B = C;
139017ab2063SBarry Smith   } else {
1391273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
139217ab2063SBarry Smith   }
13933a40ed3dSBarry Smith   PetscFunctionReturn(0);
139417ab2063SBarry Smith }
139517ab2063SBarry Smith 
1396cd0d46ebSvictorle EXTERN_C_BEGIN
1397cd0d46ebSvictorle #undef __FUNCT__
13985fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
13995fbd3699SBarry Smith int MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscTruth *f)
1400cd0d46ebSvictorle {
1401cd0d46ebSvictorle   Mat_SeqAIJ *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
1402cd0d46ebSvictorle   int *adx,*bdx,*aii,*bii,*aptr,*bptr; PetscScalar *va,*vb;
1403cd0d46ebSvictorle   int ma,na,mb,nb, i,ierr;
1404cd0d46ebSvictorle 
1405cd0d46ebSvictorle   PetscFunctionBegin;
1406cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1407cd0d46ebSvictorle 
1408cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na); CHKERRQ(ierr);
1409cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb); CHKERRQ(ierr);
1410cd0d46ebSvictorle   if (ma!=nb || na!=mb)
1411cd0d46ebSvictorle     SETERRQ(1,"Incompatible A/B sizes for symmetry test");
1412cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1413cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1414cd0d46ebSvictorle   va = aij->a; vb = bij->a;
1415cd0d46ebSvictorle   ierr = PetscMalloc(ma*sizeof(int),&aptr); CHKERRQ(ierr);
1416cd0d46ebSvictorle   ierr = PetscMalloc(mb*sizeof(int),&bptr); CHKERRQ(ierr);
1417cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1418cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1419cd0d46ebSvictorle 
1420cd0d46ebSvictorle   *f = PETSC_TRUE;
1421cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1422cd0d46ebSvictorle     /*printf("row %d spans %d--%d; we start @ %d\n",
1423cd0d46ebSvictorle       i,idx[ii[i]],idx[ii[i+1]-1],idx[aptr[i]]);*/
1424cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
1425cd0d46ebSvictorle       int idc,idr; PetscScalar vc,vr;
1426cd0d46ebSvictorle       /* column/row index/value */
1427cd0d46ebSvictorle       idc = adx[aptr[i]]; idr = bdx[bptr[idc]];
1428cd0d46ebSvictorle       vc = va[aptr[i]]; vr = vb[bptr[idc]];
1429cd0d46ebSvictorle       /*printf("comparing %d: (%d,%d)=%e to (%d,%d)=%e\n",
1430cd0d46ebSvictorle 	aptr[i],i,idc,vc,idc,idr,vr);*/
1431cd0d46ebSvictorle       if (i!=idr || vc!=vr) {
1432cd0d46ebSvictorle 	*f = PETSC_FALSE; goto done;
1433cd0d46ebSvictorle       } else {
14343aeef889SHong Zhang 	aptr[i]++; if (B || i!=idc) bptr[idc]++;
1435cd0d46ebSvictorle       }
1436cd0d46ebSvictorle     }
1437cd0d46ebSvictorle   }
1438cd0d46ebSvictorle  done:
1439cd0d46ebSvictorle   ierr = PetscFree(aptr); CHKERRQ(ierr);
14403aeef889SHong Zhang   if (B) {
14413aeef889SHong Zhang     ierr = PetscFree(bptr); CHKERRQ(ierr);
14423aeef889SHong Zhang   }
1443cd0d46ebSvictorle 
1444cd0d46ebSvictorle   PetscFunctionReturn(0);
1445cd0d46ebSvictorle }
1446cd0d46ebSvictorle EXTERN_C_END
1447cd0d46ebSvictorle 
14489e29f15eSvictorle EXTERN_C_BEGIN
14499e29f15eSvictorle #undef __FUNCT__
14509e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
14519e29f15eSvictorle int MatIsSymmetric_SeqAIJ(Mat A,PetscTruth *f)
14529e29f15eSvictorle {
1453dee28dbaSBarry Smith   int ierr;
14549e29f15eSvictorle   PetscFunctionBegin;
14559e29f15eSvictorle   ierr = MatIsTranspose_SeqAIJ(A,A,f); CHKERRQ(ierr);
14569e29f15eSvictorle   PetscFunctionReturn(0);
14579e29f15eSvictorle }
14589e29f15eSvictorle EXTERN_C_END
14599e29f15eSvictorle 
14604a2ae208SSatish Balay #undef __FUNCT__
14614a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
14628f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
146317ab2063SBarry Smith {
1464416022c9SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
146587828ca2SBarry Smith   PetscScalar  *l,*r,x,*v;
1466bfeeae90SHong Zhang   int          ierr,i,j,m = A->m,n = A->n,M,nz = a->nz,*jj;
146717ab2063SBarry Smith 
14683a40ed3dSBarry Smith   PetscFunctionBegin;
146917ab2063SBarry Smith   if (ll) {
14703ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
14713ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1472e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1473273d9f13SBarry Smith     if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
1474ed480e8bSBarry Smith     ierr = VecGetArrayFast(ll,&l);CHKERRQ(ierr);
1475416022c9SBarry Smith     v = a->a;
147617ab2063SBarry Smith     for (i=0; i<m; i++) {
147717ab2063SBarry Smith       x = l[i];
1478416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
147917ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
148017ab2063SBarry Smith     }
1481ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(ll,&l);CHKERRQ(ierr);
1482b0a32e0cSBarry Smith     PetscLogFlops(nz);
148317ab2063SBarry Smith   }
148417ab2063SBarry Smith   if (rr) {
1485e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1486273d9f13SBarry Smith     if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
1487ed480e8bSBarry Smith     ierr = VecGetArrayFast(rr,&r);CHKERRQ(ierr);
1488416022c9SBarry Smith     v = a->a; jj = a->j;
148917ab2063SBarry Smith     for (i=0; i<nz; i++) {
1490bfeeae90SHong Zhang       (*v++) *= r[*jj++];
149117ab2063SBarry Smith     }
1492ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(rr,&r);CHKERRQ(ierr);
1493b0a32e0cSBarry Smith     PetscLogFlops(nz);
149417ab2063SBarry Smith   }
14953a40ed3dSBarry Smith   PetscFunctionReturn(0);
149617ab2063SBarry Smith }
149717ab2063SBarry Smith 
14984a2ae208SSatish Balay #undef __FUNCT__
14994a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
15007b2a1423SBarry Smith int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,int csize,MatReuse scall,Mat *B)
150117ab2063SBarry Smith {
1502db02288aSLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data,*c;
1503273d9f13SBarry Smith   int          *smap,i,k,kstart,kend,ierr,oldcols = A->n,*lens;
150436db0b34SBarry Smith   int          row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
1505bfeeae90SHong Zhang   int          *irow,*icol,nrows,ncols;
150699141d43SSatish Balay   int          *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
150787828ca2SBarry Smith   PetscScalar  *a_new,*mat_a;
1508416022c9SBarry Smith   Mat          C;
1509fee21e36SBarry Smith   PetscTruth   stride;
151017ab2063SBarry Smith 
15113a40ed3dSBarry Smith   PetscFunctionBegin;
1512d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
151329bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1514d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
151529bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
151699141d43SSatish Balay 
151717ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1518b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1519b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
152017ab2063SBarry Smith 
1521fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1522fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1523fee21e36SBarry Smith   if (stride && step == 1) {
152402834360SBarry Smith     /* special case of contiguous rows */
152531ebf83bSSatish Balay     ierr   = PetscMalloc((2*nrows+1)*sizeof(int),&lens);CHKERRQ(ierr);
152631ebf83bSSatish Balay     starts = lens + nrows;
152702834360SBarry Smith     /* loop over new rows determining lens and starting points */
152802834360SBarry Smith     for (i=0; i<nrows; i++) {
1529bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1530a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
153102834360SBarry Smith       for (k=kstart; k<kend; k++) {
1532bfeeae90SHong Zhang         if (aj[k] >= first) {
153302834360SBarry Smith           starts[i] = k;
153402834360SBarry Smith           break;
153502834360SBarry Smith 	}
153602834360SBarry Smith       }
1537a2744918SBarry Smith       sum = 0;
153802834360SBarry Smith       while (k < kend) {
1539bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1540a2744918SBarry Smith         sum++;
154102834360SBarry Smith       }
1542a2744918SBarry Smith       lens[i] = sum;
154302834360SBarry Smith     }
154402834360SBarry Smith     /* create submatrix */
1545cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
154608480c60SBarry Smith       int n_cols,n_rows;
154708480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
154829bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1549d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
155008480c60SBarry Smith       C = *B;
15513a40ed3dSBarry Smith     } else {
1552e2d9671bSKris Buschelman       ierr = MatCreate(A->comm,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE,&C);CHKERRQ(ierr);
1553e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1554e2d9671bSKris Buschelman       ierr = MatSeqAIJSetPreallocation(C,0,lens);CHKERRQ(ierr);
155508480c60SBarry Smith     }
1556db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1557db02288aSLois Curfman McInnes 
155802834360SBarry Smith     /* loop over rows inserting into submatrix */
1559db02288aSLois Curfman McInnes     a_new    = c->a;
1560db02288aSLois Curfman McInnes     j_new    = c->j;
1561db02288aSLois Curfman McInnes     i_new    = c->i;
1562bfeeae90SHong Zhang 
156302834360SBarry Smith     for (i=0; i<nrows; i++) {
1564a2744918SBarry Smith       ii    = starts[i];
1565a2744918SBarry Smith       lensi = lens[i];
1566a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1567a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
156802834360SBarry Smith       }
156987828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1570a2744918SBarry Smith       a_new      += lensi;
1571a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1572a2744918SBarry Smith       c->ilen[i]  = lensi;
157302834360SBarry Smith     }
1574606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
15753a40ed3dSBarry Smith   } else {
157602834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
1577b0a32e0cSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(int),&smap);CHKERRQ(ierr);
1578bfeeae90SHong Zhang 
1579b0a32e0cSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(int),&lens);CHKERRQ(ierr);
1580549d3d68SSatish Balay     ierr  = PetscMemzero(smap,oldcols*sizeof(int));CHKERRQ(ierr);
158117ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
158202834360SBarry Smith     /* determine lens of each row */
158302834360SBarry Smith     for (i=0; i<nrows; i++) {
1584bfeeae90SHong Zhang       kstart  = ai[irow[i]];
158502834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
158602834360SBarry Smith       lens[i] = 0;
158702834360SBarry Smith       for (k=kstart; k<kend; k++) {
1588bfeeae90SHong Zhang         if (smap[aj[k]]) {
158902834360SBarry Smith           lens[i]++;
159002834360SBarry Smith         }
159102834360SBarry Smith       }
159202834360SBarry Smith     }
159317ab2063SBarry Smith     /* Create and fill new matrix */
1594a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
15950f5bd95cSBarry Smith       PetscTruth equal;
15960f5bd95cSBarry Smith 
159799141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1598273d9f13SBarry Smith       if ((*B)->m  != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1599273d9f13SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(int),&equal);CHKERRQ(ierr);
16000f5bd95cSBarry Smith       if (!equal) {
160129bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
160299141d43SSatish Balay       }
1603273d9f13SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(int));CHKERRQ(ierr);
160408480c60SBarry Smith       C = *B;
16053a40ed3dSBarry Smith     } else {
1606e2d9671bSKris Buschelman       ierr = MatCreate(A->comm,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE,&C);CHKERRQ(ierr);
1607e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1608e2d9671bSKris Buschelman       ierr = MatSeqAIJSetPreallocation(C,0,lens);CHKERRQ(ierr);
160908480c60SBarry Smith     }
161099141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
161117ab2063SBarry Smith     for (i=0; i<nrows; i++) {
161299141d43SSatish Balay       row    = irow[i];
1613bfeeae90SHong Zhang       kstart = ai[row];
161499141d43SSatish Balay       kend   = kstart + a->ilen[row];
1615bfeeae90SHong Zhang       mat_i  = c->i[i];
161699141d43SSatish Balay       mat_j  = c->j + mat_i;
161799141d43SSatish Balay       mat_a  = c->a + mat_i;
161899141d43SSatish Balay       mat_ilen = c->ilen + i;
161917ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1620bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1621ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
162299141d43SSatish Balay           *mat_a++ = a->a[k];
162399141d43SSatish Balay           (*mat_ilen)++;
162499141d43SSatish Balay 
162517ab2063SBarry Smith         }
162617ab2063SBarry Smith       }
162717ab2063SBarry Smith     }
162802834360SBarry Smith     /* Free work space */
162902834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1630606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1631606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
163202834360SBarry Smith   }
16336d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16346d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
163517ab2063SBarry Smith 
163617ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1637416022c9SBarry Smith   *B = C;
16383a40ed3dSBarry Smith   PetscFunctionReturn(0);
163917ab2063SBarry Smith }
164017ab2063SBarry Smith 
1641a871dcd8SBarry Smith /*
1642a871dcd8SBarry Smith */
16434a2ae208SSatish Balay #undef __FUNCT__
16444a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
1645b380c88cSHong Zhang int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info)
1646a871dcd8SBarry Smith {
164763b91edcSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
164808480c60SBarry Smith   int        ierr;
164963b91edcSBarry Smith   Mat        outA;
1650b8a78c4aSBarry Smith   PetscTruth row_identity,col_identity;
165163b91edcSBarry Smith 
16523a40ed3dSBarry Smith   PetscFunctionBegin;
1653d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1654b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1655b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1656b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
165729bbc08cSBarry Smith     SETERRQ(1,"Row and column permutations must be identity for in-place ILU");
1658b8a78c4aSBarry Smith   }
1659a871dcd8SBarry Smith 
166063b91edcSBarry Smith   outA          = inA;
166163b91edcSBarry Smith   inA->factor   = FACTOR_LU;
166263b91edcSBarry Smith   a->row        = row;
166363b91edcSBarry Smith   a->col        = col;
1664c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1665c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
166663b91edcSBarry Smith 
166736db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1668b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
16694c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
1670b0a32e0cSBarry Smith   PetscLogObjectParent(inA,a->icol);
1671f0ec6fceSSatish Balay 
167294a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
167387828ca2SBarry Smith      ierr = PetscMalloc((inA->m+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
167494a9d846SBarry Smith   }
167563b91edcSBarry Smith 
167608480c60SBarry Smith   if (!a->diag) {
1677f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
167863b91edcSBarry Smith   }
167963b91edcSBarry Smith   ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA);CHKERRQ(ierr);
16803a40ed3dSBarry Smith   PetscFunctionReturn(0);
1681a871dcd8SBarry Smith }
1682a871dcd8SBarry Smith 
1683d9eff348SSatish Balay #include "petscblaslapack.h"
16844a2ae208SSatish Balay #undef __FUNCT__
16854a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1686268466fbSBarry Smith int MatScale_SeqAIJ(const PetscScalar *alpha,Mat inA)
1687f0b747eeSBarry Smith {
1688f0b747eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
1689f0b747eeSBarry Smith   int        one = 1;
16903a40ed3dSBarry Smith 
16913a40ed3dSBarry Smith   PetscFunctionBegin;
1692268466fbSBarry Smith   BLscal_(&a->nz,(PetscScalar*)alpha,a->a,&one);
1693b0a32e0cSBarry Smith   PetscLogFlops(a->nz);
16943a40ed3dSBarry Smith   PetscFunctionReturn(0);
1695f0b747eeSBarry Smith }
1696f0b747eeSBarry Smith 
16974a2ae208SSatish Balay #undef __FUNCT__
16984a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
1699268466fbSBarry Smith int MatGetSubMatrices_SeqAIJ(Mat A,int n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1700cddf8d76SBarry Smith {
1701cddf8d76SBarry Smith   int ierr,i;
1702cddf8d76SBarry Smith 
17033a40ed3dSBarry Smith   PetscFunctionBegin;
1704cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1705b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1706cddf8d76SBarry Smith   }
1707cddf8d76SBarry Smith 
1708cddf8d76SBarry Smith   for (i=0; i<n; i++) {
17096a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1710cddf8d76SBarry Smith   }
17113a40ed3dSBarry Smith   PetscFunctionReturn(0);
1712cddf8d76SBarry Smith }
1713cddf8d76SBarry Smith 
17144a2ae208SSatish Balay #undef __FUNCT__
17154a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_SeqAIJ"
17168f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A,int *bs)
17175a838052SSatish Balay {
1718f830108cSBarry Smith   PetscFunctionBegin;
17195a838052SSatish Balay   *bs = 1;
17203a40ed3dSBarry Smith   PetscFunctionReturn(0);
17215a838052SSatish Balay }
17225a838052SSatish Balay 
17234a2ae208SSatish Balay #undef __FUNCT__
17244a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
1725268466fbSBarry Smith int MatIncreaseOverlap_SeqAIJ(Mat A,int is_max,IS is[],int ov)
17264dcbc457SBarry Smith {
1727e4d965acSSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1728efb16452SHong Zhang   int        row,i,j,k,l,m,n,*idx,ierr,*nidx,isz,val;
17298a047759SSatish Balay   int        start,end,*ai,*aj;
1730f1af5d2fSBarry Smith   PetscBT    table;
1731bbd702dbSSatish Balay 
17323a40ed3dSBarry Smith   PetscFunctionBegin;
1733273d9f13SBarry Smith   m     = A->m;
1734e4d965acSSatish Balay   ai    = a->i;
1735bfeeae90SHong Zhang   aj    = a->j;
17368a047759SSatish Balay 
1737a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
173806763907SSatish Balay 
1739b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&nidx);CHKERRQ(ierr);
17406831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
174106763907SSatish Balay 
1742e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1743b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1744e4d965acSSatish Balay     isz  = 0;
17456831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1746e4d965acSSatish Balay 
1747e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
17484dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1749b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1750e4d965acSSatish Balay 
1751dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1752e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1753f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
17544dcbc457SBarry Smith     }
175506763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
175606763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1757e4d965acSSatish Balay 
175804a348a9SBarry Smith     k = 0;
175904a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
176004a348a9SBarry Smith       n = isz;
176106763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1762e4d965acSSatish Balay         row   = nidx[k];
1763e4d965acSSatish Balay         start = ai[row];
1764e4d965acSSatish Balay         end   = ai[row+1];
176504a348a9SBarry Smith         for (l = start; l<end ; l++){
1766efb16452SHong Zhang           val = aj[l] ;
1767f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1768e4d965acSSatish Balay         }
1769e4d965acSSatish Balay       }
1770e4d965acSSatish Balay     }
1771029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1772e4d965acSSatish Balay   }
17736831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1774606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
17753a40ed3dSBarry Smith   PetscFunctionReturn(0);
17764dcbc457SBarry Smith }
177717ab2063SBarry Smith 
17780513a670SBarry Smith /* -------------------------------------------------------------- */
17794a2ae208SSatish Balay #undef __FUNCT__
17804a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
17810513a670SBarry Smith int MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
17820513a670SBarry Smith {
17830513a670SBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
178487828ca2SBarry Smith   PetscScalar  *vwork;
1785273d9f13SBarry Smith   int          i,ierr,nz,m = A->m,n = A->n,*cwork;
17860513a670SBarry Smith   int          *row,*col,*cnew,j,*lens;
178756cd22aeSBarry Smith   IS           icolp,irowp;
17880513a670SBarry Smith 
17893a40ed3dSBarry Smith   PetscFunctionBegin;
17904c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
179156cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
17924c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
179356cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
17940513a670SBarry Smith 
17950513a670SBarry Smith   /* determine lengths of permuted rows */
1796b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&lens);CHKERRQ(ierr);
17970513a670SBarry Smith   for (i=0; i<m; i++) {
17980513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
17990513a670SBarry Smith   }
18000513a670SBarry Smith   ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr);
1801606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
18020513a670SBarry Smith 
1803b0a32e0cSBarry Smith   ierr = PetscMalloc(n*sizeof(int),&cnew);CHKERRQ(ierr);
18040513a670SBarry Smith   for (i=0; i<m; i++) {
18050513a670SBarry Smith     ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18060513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
18070513a670SBarry Smith     ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
18080513a670SBarry Smith     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18090513a670SBarry Smith   }
1810606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
18110513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18120513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
181356cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
181456cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
181556cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
181656cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
18173a40ed3dSBarry Smith   PetscFunctionReturn(0);
18180513a670SBarry Smith }
18190513a670SBarry Smith 
18204a2ae208SSatish Balay #undef __FUNCT__
18214a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ"
1822682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A)
1823682d7d0cSBarry Smith {
1824c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1825682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1826d132466eSBarry Smith   int               ierr;
1827682d7d0cSBarry Smith 
18283a40ed3dSBarry Smith   PetscFunctionBegin;
1829c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1830d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1831d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1832d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
1833d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_no_inode: Do not use inodes\n");CHKERRQ(ierr);
1834d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n");CHKERRQ(ierr);
183516ce804cSBarry Smith #if defined(PETSC_HAVE_MATLAB) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
1836ca44d042SBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_matlab: Use Matlab engine sparse LU factorization and solve.\n");CHKERRQ(ierr);
1837ca44d042SBarry Smith #endif
18383a40ed3dSBarry Smith   PetscFunctionReturn(0);
1839682d7d0cSBarry Smith }
184097304618SKris Buschelman 
18414a2ae208SSatish Balay #undef __FUNCT__
18424a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1843cb5b572fSBarry Smith int MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1844cb5b572fSBarry Smith {
1845be6bf707SBarry Smith   int        ierr;
1846cb5b572fSBarry Smith 
1847cb5b572fSBarry Smith   PetscFunctionBegin;
184833f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
184933f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
1850be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1851be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1852be6bf707SBarry Smith 
1853bfeeae90SHong Zhang     if (a->i[A->m] != b->i[B->m]) {
185429bbc08cSBarry Smith       SETERRQ(1,"Number of nonzeros in two matrices are different");
1855cb5b572fSBarry Smith     }
1856bfeeae90SHong Zhang     ierr = PetscMemcpy(b->a,a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
1857cb5b572fSBarry Smith   } else {
1858cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1859cb5b572fSBarry Smith   }
1860cb5b572fSBarry Smith   PetscFunctionReturn(0);
1861cb5b572fSBarry Smith }
1862cb5b572fSBarry Smith 
18634a2ae208SSatish Balay #undef __FUNCT__
18644a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
1865273d9f13SBarry Smith int MatSetUpPreallocation_SeqAIJ(Mat A)
1866273d9f13SBarry Smith {
1867273d9f13SBarry Smith   int        ierr;
1868273d9f13SBarry Smith 
1869273d9f13SBarry Smith   PetscFunctionBegin;
1870273d9f13SBarry Smith   ierr =  MatSeqAIJSetPreallocation(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
1871273d9f13SBarry Smith   PetscFunctionReturn(0);
1872273d9f13SBarry Smith }
1873273d9f13SBarry Smith 
18744a2ae208SSatish Balay #undef __FUNCT__
18754a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
18764e7234bfSBarry Smith int MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
18776c0721eeSBarry Smith {
18786c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
18796c0721eeSBarry Smith   PetscFunctionBegin;
18806c0721eeSBarry Smith   *array = a->a;
18816c0721eeSBarry Smith   PetscFunctionReturn(0);
18826c0721eeSBarry Smith }
18836c0721eeSBarry Smith 
18844a2ae208SSatish Balay #undef __FUNCT__
18854a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
18864e7234bfSBarry Smith int MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
18876c0721eeSBarry Smith {
18886c0721eeSBarry Smith   PetscFunctionBegin;
18896c0721eeSBarry Smith   PetscFunctionReturn(0);
18906c0721eeSBarry Smith }
1891273d9f13SBarry Smith 
1892ee4f033dSBarry Smith #undef __FUNCT__
1893ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
1894ee4f033dSBarry Smith int MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
1895ee4f033dSBarry Smith {
1896ee4f033dSBarry Smith   int           (*f)(void *,Vec,Vec,void*) = (int (*)(void *,Vec,Vec,void *))coloring->f;
1897ee4f033dSBarry Smith   int           k,ierr,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
189887828ca2SBarry Smith   PetscScalar   dx,mone = -1.0,*y,*xx,*w3_array;
189987828ca2SBarry Smith   PetscScalar   *vscale_array;
1900ee4f033dSBarry Smith   PetscReal     epsilon = coloring->error_rel,umin = coloring->umin;
1901ee4f033dSBarry Smith   Vec           w1,w2,w3;
1902ee4f033dSBarry Smith   void          *fctx = coloring->fctx;
1903ee4f033dSBarry Smith   PetscTruth    flg;
1904ee4f033dSBarry Smith 
1905ee4f033dSBarry Smith   PetscFunctionBegin;
1906ee4f033dSBarry Smith   if (!coloring->w1) {
1907ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
1908ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w1);
1909ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
1910ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w2);
1911ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
1912ee4f033dSBarry Smith     PetscLogObjectParent(coloring,coloring->w3);
1913ee4f033dSBarry Smith   }
1914ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
1915ee4f033dSBarry Smith 
1916ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
1917e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(coloring->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
1918ee4f033dSBarry Smith   if (flg) {
1919ee4f033dSBarry Smith     PetscLogInfo(coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n");
1920ee4f033dSBarry Smith   } else {
1921ee4f033dSBarry Smith     ierr = MatZeroEntries(J);CHKERRQ(ierr);
1922ee4f033dSBarry Smith   }
1923ee4f033dSBarry Smith 
1924ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
1925ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
1926ee4f033dSBarry Smith 
1927ee4f033dSBarry Smith   /*
1928ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
1929ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
1930ee4f033dSBarry Smith   */
1931ee4f033dSBarry Smith   if (coloring->F) {
1932ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
1933ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
1934ee4f033dSBarry Smith     if (m1 != m2) {
1935ee4f033dSBarry Smith       coloring->F = 0;
1936ee4f033dSBarry Smith     }
1937ee4f033dSBarry Smith   }
1938ee4f033dSBarry Smith 
1939ee4f033dSBarry Smith   if (coloring->F) {
1940ee4f033dSBarry Smith     w1          = coloring->F;
1941ee4f033dSBarry Smith     coloring->F = 0;
1942ee4f033dSBarry Smith   } else {
194366f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1944ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
194566f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1946ee4f033dSBarry Smith   }
1947ee4f033dSBarry Smith 
1948ee4f033dSBarry Smith   /*
1949ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
1950ee4f033dSBarry Smith   */
1951ed480e8bSBarry Smith   ierr = VecGetArrayFast(x1,&xx);CHKERRQ(ierr);xx = xx - start;
1952ed480e8bSBarry Smith   ierr = VecGetArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
1953ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
1954ee4f033dSBarry Smith     /*
1955ee4f033dSBarry Smith        Loop over each column associated with color adding the
1956ee4f033dSBarry Smith        perturbation to the vector w3.
1957ee4f033dSBarry Smith     */
1958ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
1959ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
1960ee4f033dSBarry Smith       dx  = xx[col];
1961ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
1962ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
1963ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
1964ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
1965ee4f033dSBarry Smith #else
1966ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
1967ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
1968ee4f033dSBarry Smith #endif
1969ee4f033dSBarry Smith       dx                *= epsilon;
1970ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
1971ee4f033dSBarry Smith     }
1972ee4f033dSBarry Smith   }
1973ed480e8bSBarry Smith   vscale_array = vscale_array + start;ierr = VecRestoreArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);
1974ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1975ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1976ee4f033dSBarry Smith 
1977ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
1978ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
1979ee4f033dSBarry Smith 
1980ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
1981ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
1982ee4f033dSBarry Smith 
1983ed480e8bSBarry Smith   ierr = VecGetArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);
1984ee4f033dSBarry Smith   /*
1985ee4f033dSBarry Smith       Loop over each color
1986ee4f033dSBarry Smith   */
1987ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
198849b058dcSBarry Smith     coloring->currentcolor = k;
1989ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
1990ed480e8bSBarry Smith     ierr = VecGetArrayFast(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
1991ee4f033dSBarry Smith     /*
1992ee4f033dSBarry Smith        Loop over each column associated with color adding the
1993ee4f033dSBarry Smith        perturbation to the vector w3.
1994ee4f033dSBarry Smith     */
1995ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
1996ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
1997ee4f033dSBarry Smith       dx  = xx[col];
1998ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
1999ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2000ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2001ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2002ee4f033dSBarry Smith #else
2003ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2004ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2005ee4f033dSBarry Smith #endif
2006ee4f033dSBarry Smith       dx            *= epsilon;
2007ee4f033dSBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(1,"Computed 0 differencing parameter");
2008ee4f033dSBarry Smith       w3_array[col] += dx;
2009ee4f033dSBarry Smith     }
2010ed480e8bSBarry Smith     w3_array = w3_array + start; ierr = VecRestoreArrayFast(w3,&w3_array);CHKERRQ(ierr);
2011ee4f033dSBarry Smith 
2012ee4f033dSBarry Smith     /*
2013ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2014ee4f033dSBarry Smith     */
2015ee4f033dSBarry Smith 
201666f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2017ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
201866f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2019ee4f033dSBarry Smith     ierr = VecAXPY(&mone,w1,w2);CHKERRQ(ierr);
2020ee4f033dSBarry Smith 
2021ee4f033dSBarry Smith     /*
2022ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2023ee4f033dSBarry Smith     */
2024ed480e8bSBarry Smith     ierr = VecGetArrayFast(w2,&y);CHKERRQ(ierr);
2025ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2026ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2027ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2028ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2029ee4f033dSBarry Smith       srow   = row + start;
2030ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2031ee4f033dSBarry Smith     }
2032ed480e8bSBarry Smith     ierr = VecRestoreArrayFast(w2,&y);CHKERRQ(ierr);
2033ee4f033dSBarry Smith   }
203449b058dcSBarry Smith   coloring->currentcolor = k;
2035ed480e8bSBarry Smith   ierr = VecRestoreArrayFast(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2036ed480e8bSBarry Smith   xx = xx + start; ierr  = VecRestoreArrayFast(x1,&xx);CHKERRQ(ierr);
2037ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2038ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2039ee4f033dSBarry Smith   PetscFunctionReturn(0);
2040ee4f033dSBarry Smith }
2041ee4f033dSBarry Smith 
2042ac90fabeSBarry Smith #include "petscblaslapack.h"
2043ac90fabeSBarry Smith #undef __FUNCT__
2044ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
20457cff1425SSatish Balay int MatAXPY_SeqAIJ(const PetscScalar a[],Mat X,Mat Y,MatStructure str)
2046ac90fabeSBarry Smith {
2047c537a176SHong Zhang   int        ierr,one=1,i;
2048ac90fabeSBarry Smith   Mat_SeqAIJ *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
2049ac90fabeSBarry Smith 
2050ac90fabeSBarry Smith   PetscFunctionBegin;
2051ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2052268466fbSBarry Smith     BLaxpy_(&x->nz,(PetscScalar*)a,x->a,&one,y->a,&one);
2053c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2054a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2055a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2056a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2057a30b2313SHong Zhang     }
2058a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
205924f910e3SHong Zhang       ierr = MatAXPYGetxtoy_Private(X->m,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2060a30b2313SHong Zhang       y->XtoY = X;
2061c537a176SHong Zhang     }
2062a30b2313SHong Zhang     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += (*a)*(x->a[i]);
2063e2dd4fc4Svictorle     PetscLogInfo(0,"MatAXPY_SeqAIJ: ratio of nnz(X)/nnz(Y): %d/%d = %g\n",x->nz,y->nz,(PetscReal)(x->nz)/y->nz);
2064ac90fabeSBarry Smith   } else {
2065ac90fabeSBarry Smith     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
2066ac90fabeSBarry Smith   }
2067ac90fabeSBarry Smith   PetscFunctionReturn(0);
2068ac90fabeSBarry Smith }
2069ac90fabeSBarry Smith 
2070682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
20710a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2072cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2073cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2074cb5b572fSBarry Smith        MatMult_SeqAIJ,
207597304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
20767c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
20777c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2078cb5b572fSBarry Smith        MatSolve_SeqAIJ,
2079cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
20807c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
208197304618SKris Buschelman /*10*/ MatSolveTransposeAdd_SeqAIJ,
2082cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2083cb5b572fSBarry Smith        0,
208417ab2063SBarry Smith        MatRelax_SeqAIJ,
208517ab2063SBarry Smith        MatTranspose_SeqAIJ,
208697304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2087cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2088cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2089cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2090cb5b572fSBarry Smith        MatNorm_SeqAIJ,
209197304618SKris Buschelman /*20*/ 0,
2092cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
209317ab2063SBarry Smith        MatCompress_SeqAIJ,
2094cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2095cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
209697304618SKris Buschelman /*25*/ MatZeroRows_SeqAIJ,
2097cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2098cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2099f76d2b81SHong Zhang        MatCholeskyFactorSymbolic_SeqAIJ,
2100a6175056SHong Zhang        MatCholeskyFactorNumeric_SeqAIJ,
210197304618SKris Buschelman /*30*/ MatSetUpPreallocation_SeqAIJ,
2102cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2103861ba921SHong Zhang        MatICCFactorSymbolic_SeqAIJ,
21046c0721eeSBarry Smith        MatGetArray_SeqAIJ,
21056c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
210697304618SKris Buschelman /*35*/ MatDuplicate_SeqAIJ,
2107cb5b572fSBarry Smith        0,
2108cb5b572fSBarry Smith        0,
2109cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2110cb5b572fSBarry Smith        0,
211197304618SKris Buschelman /*40*/ MatAXPY_SeqAIJ,
2112cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2113cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2114cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2115cb5b572fSBarry Smith        MatCopy_SeqAIJ,
211697304618SKris Buschelman /*45*/ MatPrintHelp_SeqAIJ,
2117cb5b572fSBarry Smith        MatScale_SeqAIJ,
2118cb5b572fSBarry Smith        0,
2119cb5b572fSBarry Smith        0,
21206945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
212197304618SKris Buschelman /*50*/ MatGetBlockSize_SeqAIJ,
21223b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
21233b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
21243b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2125a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
212697304618SKris Buschelman /*55*/ MatFDColoringCreate_SeqAIJ,
2127b9617806SBarry Smith        0,
21280513a670SBarry Smith        0,
2129cda55fadSBarry Smith        MatPermute_SeqAIJ,
2130cda55fadSBarry Smith        0,
213197304618SKris Buschelman /*60*/ 0,
2132b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2133b9b97703SBarry Smith        MatView_SeqAIJ,
21348a124369SBarry Smith        MatGetPetscMaps_Petsc,
2135ee4f033dSBarry Smith        0,
213697304618SKris Buschelman /*65*/ 0,
2137ee4f033dSBarry Smith        0,
2138ee4f033dSBarry Smith        0,
2139ee4f033dSBarry Smith        0,
2140ee4f033dSBarry Smith        0,
214197304618SKris Buschelman /*70*/ 0,
2142ee4f033dSBarry Smith        0,
2143ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2144ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2145ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
214697304618SKris Buschelman /*75*/ MatFDColoringApply_SeqAIJ,
214797304618SKris Buschelman        0,
214897304618SKris Buschelman        0,
214997304618SKris Buschelman        0,
215097304618SKris Buschelman        0,
215197304618SKris Buschelman /*80*/ 0,
215297304618SKris Buschelman        0,
215397304618SKris Buschelman        0,
215497304618SKris Buschelman        0,
21559e29f15eSvictorle /*85*/ MatLoad_SeqAIJ,
21569e29f15eSvictorle        MatIsSymmetric_SeqAIJ,
21579e29f15eSvictorle };
215817ab2063SBarry Smith 
2159fb2e594dSBarry Smith EXTERN_C_BEGIN
21604a2ae208SSatish Balay #undef __FUNCT__
21614a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
216215091d37SBarry Smith 
2163bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,int *indices)
2164bef8e0ddSBarry Smith {
2165bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2166bef8e0ddSBarry Smith   int        i,nz,n;
2167bef8e0ddSBarry Smith 
2168bef8e0ddSBarry Smith   PetscFunctionBegin;
2169bef8e0ddSBarry Smith 
2170bef8e0ddSBarry Smith   nz = aij->maxnz;
2171273d9f13SBarry Smith   n  = mat->n;
2172bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2173bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2174bef8e0ddSBarry Smith   }
2175bef8e0ddSBarry Smith   aij->nz = nz;
2176bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2177bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2178bef8e0ddSBarry Smith   }
2179bef8e0ddSBarry Smith 
2180bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2181bef8e0ddSBarry Smith }
2182fb2e594dSBarry Smith EXTERN_C_END
2183bef8e0ddSBarry Smith 
21844a2ae208SSatish Balay #undef __FUNCT__
21854a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2186bef8e0ddSBarry Smith /*@
2187bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2188bef8e0ddSBarry Smith        in the matrix.
2189bef8e0ddSBarry Smith 
2190bef8e0ddSBarry Smith   Input Parameters:
2191bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2192bef8e0ddSBarry Smith -  indices - the column indices
2193bef8e0ddSBarry Smith 
219415091d37SBarry Smith   Level: advanced
219515091d37SBarry Smith 
2196bef8e0ddSBarry Smith   Notes:
2197bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2198bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2199bef8e0ddSBarry Smith   of the MatSetValues() operation.
2200bef8e0ddSBarry Smith 
2201bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2202bef8e0ddSBarry Smith   MatCreateSeqAIJ().
2203bef8e0ddSBarry Smith 
2204bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2205bef8e0ddSBarry Smith 
2206b9617806SBarry Smith     The indices should start with zero, not one.
2207b9617806SBarry Smith 
2208bef8e0ddSBarry Smith @*/
2209bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices(Mat mat,int *indices)
2210bef8e0ddSBarry Smith {
2211bef8e0ddSBarry Smith   int ierr,(*f)(Mat,int *);
2212bef8e0ddSBarry Smith 
2213bef8e0ddSBarry Smith   PetscFunctionBegin;
2214bef8e0ddSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2215c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2216bef8e0ddSBarry Smith   if (f) {
2217bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2218bef8e0ddSBarry Smith   } else {
221929bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to set column indices");
2220bef8e0ddSBarry Smith   }
2221bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2222bef8e0ddSBarry Smith }
2223bef8e0ddSBarry Smith 
2224be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2225be6bf707SBarry Smith 
2226fb2e594dSBarry Smith EXTERN_C_BEGIN
22274a2ae208SSatish Balay #undef __FUNCT__
22284a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2229be6bf707SBarry Smith int MatStoreValues_SeqAIJ(Mat mat)
2230be6bf707SBarry Smith {
2231be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2232bfeeae90SHong Zhang   size_t       nz = aij->i[mat->m],ierr;
2233be6bf707SBarry Smith 
2234be6bf707SBarry Smith   PetscFunctionBegin;
2235be6bf707SBarry Smith   if (aij->nonew != 1) {
223629bbc08cSBarry Smith     SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2237be6bf707SBarry Smith   }
2238be6bf707SBarry Smith 
2239be6bf707SBarry Smith   /* allocate space for values if not already there */
2240be6bf707SBarry Smith   if (!aij->saved_values) {
224187828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
2242be6bf707SBarry Smith   }
2243be6bf707SBarry Smith 
2244be6bf707SBarry Smith   /* copy values over */
224587828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2246be6bf707SBarry Smith   PetscFunctionReturn(0);
2247be6bf707SBarry Smith }
2248fb2e594dSBarry Smith EXTERN_C_END
2249be6bf707SBarry Smith 
22504a2ae208SSatish Balay #undef __FUNCT__
2251b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2252be6bf707SBarry Smith /*@
2253be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2254be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2255be6bf707SBarry Smith        nonlinear portion.
2256be6bf707SBarry Smith 
2257be6bf707SBarry Smith    Collect on Mat
2258be6bf707SBarry Smith 
2259be6bf707SBarry Smith   Input Parameters:
2260be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2261be6bf707SBarry Smith 
226215091d37SBarry Smith   Level: advanced
226315091d37SBarry Smith 
2264be6bf707SBarry Smith   Common Usage, with SNESSolve():
2265be6bf707SBarry Smith $    Create Jacobian matrix
2266be6bf707SBarry Smith $    Set linear terms into matrix
2267be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2268be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2269be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2270be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2271be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2272be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2273be6bf707SBarry Smith $    In your Jacobian routine
2274be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2275be6bf707SBarry Smith $      Set nonlinear terms in matrix
2276be6bf707SBarry Smith 
2277be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2278be6bf707SBarry Smith $    // build linear portion of Jacobian
2279be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2280be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2281be6bf707SBarry Smith $    loop over nonlinear iterations
2282be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2283be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2284be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2285be6bf707SBarry Smith $       Solve linear system with Jacobian
2286be6bf707SBarry Smith $    endloop
2287be6bf707SBarry Smith 
2288be6bf707SBarry Smith   Notes:
2289be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2290be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2291be6bf707SBarry Smith     calling this routine.
2292be6bf707SBarry Smith 
2293be6bf707SBarry Smith .seealso: MatRetrieveValues()
2294be6bf707SBarry Smith 
2295be6bf707SBarry Smith @*/
2296be6bf707SBarry Smith int MatStoreValues(Mat mat)
2297be6bf707SBarry Smith {
2298be6bf707SBarry Smith   int ierr,(*f)(Mat);
2299be6bf707SBarry Smith 
2300be6bf707SBarry Smith   PetscFunctionBegin;
2301be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
230229bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
230329bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2304be6bf707SBarry Smith 
2305c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2306be6bf707SBarry Smith   if (f) {
2307be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2308be6bf707SBarry Smith   } else {
230929bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to store values");
2310be6bf707SBarry Smith   }
2311be6bf707SBarry Smith   PetscFunctionReturn(0);
2312be6bf707SBarry Smith }
2313be6bf707SBarry Smith 
2314fb2e594dSBarry Smith EXTERN_C_BEGIN
23154a2ae208SSatish Balay #undef __FUNCT__
23164a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2317be6bf707SBarry Smith int MatRetrieveValues_SeqAIJ(Mat mat)
2318be6bf707SBarry Smith {
2319be6bf707SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
2320bfeeae90SHong Zhang   int        nz = aij->i[mat->m],ierr;
2321be6bf707SBarry Smith 
2322be6bf707SBarry Smith   PetscFunctionBegin;
2323be6bf707SBarry Smith   if (aij->nonew != 1) {
232429bbc08cSBarry Smith     SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2325be6bf707SBarry Smith   }
2326be6bf707SBarry Smith   if (!aij->saved_values) {
232729bbc08cSBarry Smith     SETERRQ(1,"Must call MatStoreValues(A);first");
2328be6bf707SBarry Smith   }
2329be6bf707SBarry Smith 
2330be6bf707SBarry Smith   /* copy values over */
233187828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2332be6bf707SBarry Smith   PetscFunctionReturn(0);
2333be6bf707SBarry Smith }
2334fb2e594dSBarry Smith EXTERN_C_END
2335be6bf707SBarry Smith 
23364a2ae208SSatish Balay #undef __FUNCT__
23374a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2338be6bf707SBarry Smith /*@
2339be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2340be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2341be6bf707SBarry Smith        nonlinear portion.
2342be6bf707SBarry Smith 
2343be6bf707SBarry Smith    Collect on Mat
2344be6bf707SBarry Smith 
2345be6bf707SBarry Smith   Input Parameters:
2346be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2347be6bf707SBarry Smith 
234815091d37SBarry Smith   Level: advanced
234915091d37SBarry Smith 
2350be6bf707SBarry Smith .seealso: MatStoreValues()
2351be6bf707SBarry Smith 
2352be6bf707SBarry Smith @*/
2353be6bf707SBarry Smith int MatRetrieveValues(Mat mat)
2354be6bf707SBarry Smith {
2355be6bf707SBarry Smith   int ierr,(*f)(Mat);
2356be6bf707SBarry Smith 
2357be6bf707SBarry Smith   PetscFunctionBegin;
2358be6bf707SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
235929bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
236029bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2361be6bf707SBarry Smith 
2362c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2363be6bf707SBarry Smith   if (f) {
2364be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2365be6bf707SBarry Smith   } else {
236629bbc08cSBarry Smith     SETERRQ(1,"Wrong type of matrix to retrieve values");
2367be6bf707SBarry Smith   }
2368be6bf707SBarry Smith   PetscFunctionReturn(0);
2369be6bf707SBarry Smith }
2370be6bf707SBarry Smith 
2371f83d6046SBarry Smith /*
2372f83d6046SBarry Smith    This allows SeqAIJ matrices to be passed to the matlab engine
2373f83d6046SBarry Smith */
237416ce804cSBarry Smith #if defined(PETSC_HAVE_MATLAB) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
2375f83d6046SBarry Smith #include "engine.h"   /* Matlab include file */
2376f83d6046SBarry Smith #include "mex.h"      /* Matlab include file */
2377f83d6046SBarry Smith EXTERN_C_BEGIN
23784a2ae208SSatish Balay #undef __FUNCT__
23794a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEnginePut_SeqAIJ"
238062aa7f4eSBarry Smith int MatMatlabEnginePut_SeqAIJ(PetscObject obj,void *mengine)
2381f83d6046SBarry Smith {
238225922d2bSSatish Balay   int         ierr;
2383f83d6046SBarry Smith   Mat         B = (Mat)obj;
2384f83d6046SBarry Smith   mxArray     *mat;
2385d79a51d8SBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)B->data;
2386d79a51d8SBarry Smith 
2387f83d6046SBarry Smith   PetscFunctionBegin;
238801820c62SBarry Smith   mat  = mxCreateSparse(B->n,B->m,aij->nz,mxREAL);
238987828ca2SBarry Smith   ierr = PetscMemcpy(mxGetPr(mat),aij->a,aij->nz*sizeof(PetscScalar));CHKERRQ(ierr);
2390d79a51d8SBarry Smith   /* Matlab stores by column, not row so we pass in the transpose of the matrix */
239166826eb6SBarry Smith   ierr = PetscMemcpy(mxGetIr(mat),aij->j,aij->nz*sizeof(int));CHKERRQ(ierr);
239266826eb6SBarry Smith   ierr = PetscMemcpy(mxGetJc(mat),aij->i,(B->m+1)*sizeof(int));CHKERRQ(ierr);
2393f83d6046SBarry Smith 
239401820c62SBarry Smith   /* Matlab indices start at 0 for sparse (what a surprise) */
2395bfeeae90SHong Zhang 
2396ca44d042SBarry Smith   ierr = PetscObjectName(obj);CHKERRQ(ierr);
239716ce804cSBarry Smith   engPutVariable((Engine *)mengine,obj->name,mat);
2398f83d6046SBarry Smith   PetscFunctionReturn(0);
2399f83d6046SBarry Smith }
2400f83d6046SBarry Smith EXTERN_C_END
240166826eb6SBarry Smith 
240266826eb6SBarry Smith EXTERN_C_BEGIN
24034a2ae208SSatish Balay #undef __FUNCT__
24044a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEngineGet_SeqAIJ"
240562aa7f4eSBarry Smith int MatMatlabEngineGet_SeqAIJ(PetscObject obj,void *mengine)
240666826eb6SBarry Smith {
240766826eb6SBarry Smith   int        ierr,ii;
240866826eb6SBarry Smith   Mat        mat = (Mat)obj;
240966826eb6SBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
241066826eb6SBarry Smith   mxArray    *mmat;
241166826eb6SBarry Smith 
24126c0721eeSBarry Smith   PetscFunctionBegin;
241366826eb6SBarry Smith   ierr = PetscFree(aij->a);CHKERRQ(ierr);
241466826eb6SBarry Smith 
241516ce804cSBarry Smith   mmat = engGetVariable((Engine *)mengine,obj->name);
241666826eb6SBarry Smith 
2417a65776f3SBarry Smith   aij->nz           = (mxGetJc(mmat))[mat->m];
2418a7ed9263SMatthew Knepley   ierr              = PetscMalloc(((size_t) aij->nz)*(sizeof(int)+sizeof(PetscScalar))+(mat->m+1)*sizeof(int),&aij->a);CHKERRQ(ierr);
241966826eb6SBarry Smith   aij->j            = (int*)(aij->a + aij->nz);
242066826eb6SBarry Smith   aij->i            = aij->j + aij->nz;
242166826eb6SBarry Smith   aij->singlemalloc = PETSC_TRUE;
242266826eb6SBarry Smith   aij->freedata     = PETSC_TRUE;
242366826eb6SBarry Smith 
242487828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,mxGetPr(mmat),aij->nz*sizeof(PetscScalar));CHKERRQ(ierr);
242566826eb6SBarry Smith   /* Matlab stores by column, not row so we pass in the transpose of the matrix */
242666826eb6SBarry Smith   ierr = PetscMemcpy(aij->j,mxGetIr(mmat),aij->nz*sizeof(int));CHKERRQ(ierr);
242766826eb6SBarry Smith   ierr = PetscMemcpy(aij->i,mxGetJc(mmat),(mat->m+1)*sizeof(int));CHKERRQ(ierr);
242866826eb6SBarry Smith 
242966826eb6SBarry Smith   for (ii=0; ii<mat->m; ii++) {
243066826eb6SBarry Smith     aij->ilen[ii] = aij->imax[ii] = aij->i[ii+1] - aij->i[ii];
243166826eb6SBarry Smith   }
243266826eb6SBarry Smith 
243366826eb6SBarry Smith   ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
243466826eb6SBarry Smith   ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
243566826eb6SBarry Smith 
243666826eb6SBarry Smith   PetscFunctionReturn(0);
243766826eb6SBarry Smith }
243866826eb6SBarry Smith EXTERN_C_END
2439f83d6046SBarry Smith #endif
2440f83d6046SBarry Smith 
2441be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
24424a2ae208SSatish Balay #undef __FUNCT__
24434a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
244417ab2063SBarry Smith /*@C
2445682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
24460d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
24476e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
244851c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
24492bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
245017ab2063SBarry Smith 
2451db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2452db81eaa0SLois Curfman McInnes 
245317ab2063SBarry Smith    Input Parameters:
2454db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
245517ab2063SBarry Smith .  m - number of rows
245617ab2063SBarry Smith .  n - number of columns
245717ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
245851c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
24592bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
246017ab2063SBarry Smith 
246117ab2063SBarry Smith    Output Parameter:
2462416022c9SBarry Smith .  A - the matrix
246317ab2063SBarry Smith 
2464b259b22eSLois Curfman McInnes    Notes:
246517ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
246617ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
24670002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
246844cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
246917ab2063SBarry Smith 
247017ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2471a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
24723d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
24736da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
247417ab2063SBarry Smith 
2475682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
24764fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2477682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
24786c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
24796c7ebb05SLois Curfman McInnes 
24806c7ebb05SLois Curfman McInnes    Options Database Keys:
2481db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
2482db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2483db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2484db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2485db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
248617ab2063SBarry Smith 
2487027ccd11SLois Curfman McInnes    Level: intermediate
2488027ccd11SLois Curfman McInnes 
248936db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
249036db0b34SBarry Smith 
249117ab2063SBarry Smith @*/
2492ca01db9bSBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,const int nnz[],Mat *A)
249317ab2063SBarry Smith {
2494273d9f13SBarry Smith   int ierr;
24956945ee14SBarry Smith 
24963a40ed3dSBarry Smith   PetscFunctionBegin;
2497273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,m,n,A);CHKERRQ(ierr);
2498273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2499273d9f13SBarry Smith   ierr = MatSeqAIJSetPreallocation(*A,nz,nnz);CHKERRQ(ierr);
2500273d9f13SBarry Smith   PetscFunctionReturn(0);
2501273d9f13SBarry Smith }
2502273d9f13SBarry Smith 
25035da197adSKris Buschelman #define SKIP_ALLOCATION -4
25045da197adSKris Buschelman 
25054a2ae208SSatish Balay #undef __FUNCT__
25064a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2507273d9f13SBarry Smith /*@C
2508273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2509273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2510273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2511273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2512273d9f13SBarry Smith 
2513273d9f13SBarry Smith    Collective on MPI_Comm
2514273d9f13SBarry Smith 
2515273d9f13SBarry Smith    Input Parameters:
2516273d9f13SBarry Smith +  comm - MPI communicator, set to PETSC_COMM_SELF
2517273d9f13SBarry Smith .  m - number of rows
2518273d9f13SBarry Smith .  n - number of columns
2519273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2520273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2521273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2522273d9f13SBarry Smith 
2523273d9f13SBarry Smith    Output Parameter:
2524273d9f13SBarry Smith .  A - the matrix
2525273d9f13SBarry Smith 
2526273d9f13SBarry Smith    Notes:
2527273d9f13SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
2528273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2529273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2530273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2531273d9f13SBarry Smith 
2532273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2533273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2534273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2535273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2536273d9f13SBarry Smith 
2537273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2538273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2539273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2540273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2541273d9f13SBarry Smith 
2542273d9f13SBarry Smith    Options Database Keys:
2543273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2544273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2545273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2546273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2547273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2548273d9f13SBarry Smith 
2549273d9f13SBarry Smith    Level: intermediate
2550273d9f13SBarry Smith 
2551273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
2552273d9f13SBarry Smith 
2553273d9f13SBarry Smith @*/
2554ca01db9bSBarry Smith int MatSeqAIJSetPreallocation(Mat B,int nz,const int nnz[])
2555273d9f13SBarry Smith {
2556ca01db9bSBarry Smith   int ierr,(*f)(Mat,int,const int[]);
2557a23d5eceSKris Buschelman 
2558a23d5eceSKris Buschelman   PetscFunctionBegin;
2559a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2560a23d5eceSKris Buschelman   if (f) {
2561a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2562a23d5eceSKris Buschelman   }
2563a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2564a23d5eceSKris Buschelman }
2565a23d5eceSKris Buschelman 
2566a23d5eceSKris Buschelman EXTERN_C_BEGIN
2567a23d5eceSKris Buschelman #undef __FUNCT__
2568a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2569a23d5eceSKris Buschelman int MatSeqAIJSetPreallocation_SeqAIJ(Mat B,int nz,int *nnz)
2570a23d5eceSKris Buschelman {
2571273d9f13SBarry Smith   Mat_SeqAIJ *b;
2572a7ed9263SMatthew Knepley   size_t     len = 0;
2573a43ee2ecSKris Buschelman   PetscTruth skipallocation = PETSC_FALSE;
2574a7ed9263SMatthew Knepley   int        i,ierr;
2575273d9f13SBarry Smith 
2576273d9f13SBarry Smith   PetscFunctionBegin;
2577d5d45c9bSBarry Smith 
2578c461c341SBarry Smith   if (nz == SKIP_ALLOCATION) {
2579c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2580c461c341SBarry Smith     nz             = 0;
2581c461c341SBarry Smith   }
2582c461c341SBarry Smith 
2583435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2584435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2585b73539f3SBarry Smith   if (nnz) {
2586273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
258729bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
25883a7fca6bSBarry 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);
2589b73539f3SBarry Smith     }
2590b73539f3SBarry Smith   }
2591b73539f3SBarry Smith 
2592273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2593273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2594273d9f13SBarry Smith 
2595b0a32e0cSBarry Smith   ierr = PetscMalloc((B->m+1)*sizeof(int),&b->imax);CHKERRQ(ierr);
2596273d9f13SBarry Smith   if (!nnz) {
2597435da068SBarry Smith     if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2598273d9f13SBarry Smith     else if (nz <= 0)        nz = 1;
2599273d9f13SBarry Smith     for (i=0; i<B->m; i++) b->imax[i] = nz;
2600273d9f13SBarry Smith     nz = nz*B->m;
2601273d9f13SBarry Smith   } else {
2602273d9f13SBarry Smith     nz = 0;
2603273d9f13SBarry Smith     for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2604273d9f13SBarry Smith   }
2605273d9f13SBarry Smith 
2606c461c341SBarry Smith   if (!skipallocation) {
2607273d9f13SBarry Smith     /* allocate the matrix space */
2608a7ed9263SMatthew Knepley     len             = ((size_t) nz)*(sizeof(int) + sizeof(PetscScalar)) + (B->m+1)*sizeof(int);
2609b0a32e0cSBarry Smith     ierr            = PetscMalloc(len,&b->a);CHKERRQ(ierr);
2610273d9f13SBarry Smith     b->j            = (int*)(b->a + nz);
2611273d9f13SBarry Smith     ierr            = PetscMemzero(b->j,nz*sizeof(int));CHKERRQ(ierr);
2612273d9f13SBarry Smith     b->i            = b->j + nz;
2613bfeeae90SHong Zhang     b->i[0] = 0;
26145da197adSKris Buschelman     for (i=1; i<B->m+1; i++) {
26155da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
26165da197adSKris Buschelman     }
2617273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2618273d9f13SBarry Smith     b->freedata     = PETSC_TRUE;
2619c461c341SBarry Smith   } else {
2620c461c341SBarry Smith     b->freedata     = PETSC_FALSE;
2621c461c341SBarry Smith   }
2622273d9f13SBarry Smith 
2623273d9f13SBarry Smith   /* b->ilen will count nonzeros in each row so far. */
2624b0a32e0cSBarry Smith   ierr = PetscMalloc((B->m+1)*sizeof(int),&b->ilen);CHKERRQ(ierr);
2625b0a32e0cSBarry Smith   PetscLogObjectMemory(B,len+2*(B->m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2626273d9f13SBarry Smith   for (i=0; i<B->m; i++) { b->ilen[i] = 0;}
2627273d9f13SBarry Smith 
2628273d9f13SBarry Smith   b->nz                = 0;
2629273d9f13SBarry Smith   b->maxnz             = nz;
2630273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2631273d9f13SBarry Smith   PetscFunctionReturn(0);
2632273d9f13SBarry Smith }
2633a23d5eceSKris Buschelman EXTERN_C_END
2634273d9f13SBarry Smith 
26350bad9183SKris Buschelman /*MC
2636fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
26370bad9183SKris Buschelman    based on compressed sparse row format.
26380bad9183SKris Buschelman 
26390bad9183SKris Buschelman    Options Database Keys:
26400bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
26410bad9183SKris Buschelman 
26420bad9183SKris Buschelman   Level: beginner
26430bad9183SKris Buschelman 
26440bad9183SKris Buschelman .seealso: MatCreateSeqAIJ
26450bad9183SKris Buschelman M*/
26460bad9183SKris Buschelman 
2647a6175056SHong Zhang EXTERN_C_BEGIN
26484a2ae208SSatish Balay #undef __FUNCT__
26494a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
2650273d9f13SBarry Smith int MatCreate_SeqAIJ(Mat B)
2651273d9f13SBarry Smith {
2652273d9f13SBarry Smith   Mat_SeqAIJ *b;
2653273d9f13SBarry Smith   int        ierr,size;
2654273d9f13SBarry Smith   PetscTruth flg;
2655273d9f13SBarry Smith 
2656273d9f13SBarry Smith   PetscFunctionBegin;
2657273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
2658273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
2659273d9f13SBarry Smith 
2660273d9f13SBarry Smith   B->m = B->M = PetscMax(B->m,B->M);
2661273d9f13SBarry Smith   B->n = B->N = PetscMax(B->n,B->N);
2662273d9f13SBarry Smith 
2663b0a32e0cSBarry Smith   ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr);
2664b0a32e0cSBarry Smith   B->data             = (void*)b;
2665549d3d68SSatish Balay   ierr = PetscMemzero(b,sizeof(Mat_SeqAIJ));CHKERRQ(ierr);
2666549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2667416022c9SBarry Smith   B->factor           = 0;
2668416022c9SBarry Smith   B->lupivotthreshold = 1.0;
266990f02eecSBarry Smith   B->mapping          = 0;
2670e82a3eeeSBarry Smith   ierr = PetscOptionsGetReal(B->prefix,"-mat_lu_pivotthreshold",&B->lupivotthreshold,PETSC_NULL);CHKERRQ(ierr);
2671e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-pc_ilu_preserve_row_sums",&b->ilu_preserve_row_sums);CHKERRQ(ierr);
2672416022c9SBarry Smith   b->row              = 0;
2673416022c9SBarry Smith   b->col              = 0;
267482bf6240SBarry Smith   b->icol             = 0;
2675b810aeb4SBarry Smith   b->reallocs         = 0;
267617ab2063SBarry Smith 
26778a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr);
26788a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr);
2679a5ae1ecdSBarry Smith 
2680f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
268136db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2682f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2683416022c9SBarry Smith   b->nonew             = 0;
2684416022c9SBarry Smith   b->diag              = 0;
2685416022c9SBarry Smith   b->solve_work        = 0;
26862a1b7f2aSHong Zhang   B->spptr             = 0;
2687b65db4caSBarry Smith   b->inode.use         = PETSC_TRUE;
2688754ec7b1SSatish Balay   b->inode.node_count  = 0;
2689754ec7b1SSatish Balay   b->inode.size        = 0;
26906c7ebb05SLois Curfman McInnes   b->inode.limit       = 5;
26916c7ebb05SLois Curfman McInnes   b->inode.max_limit   = 5;
2692be6bf707SBarry Smith   b->saved_values      = 0;
2693d7f994e1SBarry Smith   b->idiag             = 0;
2694d7f994e1SBarry Smith   b->ssor              = 0;
2695f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
2696a30b2313SHong Zhang   b->xtoy              = 0;
2697a30b2313SHong Zhang   b->XtoY              = 0;
269817ab2063SBarry Smith 
269935d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
270035d8aa7fSBarry Smith 
2701e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-mat_aij_matlab",&flg);CHKERRQ(ierr);
2702ca44d042SBarry Smith   if (flg) {ierr = MatUseMatlab_SeqAIJ(B);CHKERRQ(ierr);}
2703f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2704bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2705bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2706f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2707be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2708bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2709f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2710be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2711bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
2712a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
2713a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
2714a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
271585fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
271685fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
271785fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
27185fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
27195fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
27205fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
2721a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
2722a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
2723a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
272405b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
272505b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
272605b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
27270a99d0a2SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatAdjustForInodes_C",
27280a99d0a2SKris Buschelman                                      "MatAdjustForInodes_SeqAIJ",
27290a99d0a2SKris Buschelman                                       MatAdjustForInodes_SeqAIJ);CHKERRQ(ierr);
2730d758967fSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJGetInodeSizes_C",
2731d758967fSKris Buschelman                                      "MatSeqAIJGetInodeSizes_SeqAIJ",
2732d758967fSKris Buschelman                                       MatSeqAIJGetInodeSizes_SeqAIJ);CHKERRQ(ierr);
273316ce804cSBarry Smith #if defined(PETSC_HAVE_MATLAB) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
2734f83d6046SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatMatlabEnginePut_SeqAIJ",MatMatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
273566826eb6SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatMatlabEngineGet_SeqAIJ",MatMatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
2736f83d6046SBarry Smith #endif
2737eb9c0419SKris Buschelman   ierr = RegisterApplyPtAPRoutines_Private(B);CHKERRQ(ierr);
27383a40ed3dSBarry Smith   PetscFunctionReturn(0);
273917ab2063SBarry Smith }
2740273d9f13SBarry Smith EXTERN_C_END
274117ab2063SBarry Smith 
27424a2ae208SSatish Balay #undef __FUNCT__
27434a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ"
2744be6bf707SBarry Smith int MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
274517ab2063SBarry Smith {
2746416022c9SBarry Smith   Mat        C;
2747416022c9SBarry Smith   Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data;
2748bfeeae90SHong Zhang   int        i,m = A->m,ierr;
2749a7ed9263SMatthew Knepley   size_t     len;
275017ab2063SBarry Smith 
27513a40ed3dSBarry Smith   PetscFunctionBegin;
27524043dd9cSLois Curfman McInnes   *B = 0;
2753273d9f13SBarry Smith   ierr = MatCreate(A->comm,A->m,A->n,A->m,A->n,&C);CHKERRQ(ierr);
2754*be5d1d56SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
2755273d9f13SBarry Smith   c    = (Mat_SeqAIJ*)C->data;
2756273d9f13SBarry Smith 
2757416022c9SBarry Smith   C->factor         = A->factor;
2758416022c9SBarry Smith   c->row            = 0;
2759416022c9SBarry Smith   c->col            = 0;
276082bf6240SBarry Smith   c->icol           = 0;
2761f1e2ffcdSBarry Smith   c->keepzeroedrows = a->keepzeroedrows;
2762c456f294SBarry Smith   C->assembled      = PETSC_TRUE;
276317ab2063SBarry Smith 
2764273d9f13SBarry Smith   C->M          = A->m;
2765273d9f13SBarry Smith   C->N          = A->n;
276617ab2063SBarry Smith 
2767b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&c->imax);CHKERRQ(ierr);
2768b0a32e0cSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(int),&c->ilen);CHKERRQ(ierr);
276917ab2063SBarry Smith   for (i=0; i<m; i++) {
2770416022c9SBarry Smith     c->imax[i] = a->imax[i];
2771416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
277217ab2063SBarry Smith   }
277317ab2063SBarry Smith 
277417ab2063SBarry Smith   /* allocate the matrix space */
2775f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
2776a7ed9263SMatthew Knepley   len   = ((size_t) (m+1))*sizeof(int)+(a->i[m])*(sizeof(PetscScalar)+sizeof(int));
2777b0a32e0cSBarry Smith   ierr  = PetscMalloc(len,&c->a);CHKERRQ(ierr);
2778bfeeae90SHong Zhang   c->j  = (int*)(c->a + a->i[m] );
2779bfeeae90SHong Zhang   c->i  = c->j + a->i[m];
2780549d3d68SSatish Balay   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(int));CHKERRQ(ierr);
278117ab2063SBarry Smith   if (m > 0) {
2782bfeeae90SHong Zhang     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(int));CHKERRQ(ierr);
2783be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2784bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
2785be6bf707SBarry Smith     } else {
2786bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
278717ab2063SBarry Smith     }
278808480c60SBarry Smith   }
278917ab2063SBarry Smith 
2790b0a32e0cSBarry Smith   PetscLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));
2791416022c9SBarry Smith   c->sorted      = a->sorted;
2792416022c9SBarry Smith   c->roworiented = a->roworiented;
2793416022c9SBarry Smith   c->nonew       = a->nonew;
27947a743949SBarry Smith   c->ilu_preserve_row_sums = a->ilu_preserve_row_sums;
2795be6bf707SBarry Smith   c->saved_values = 0;
2796d7f994e1SBarry Smith   c->idiag        = 0;
2797d7f994e1SBarry Smith   c->ssor         = 0;
279836db0b34SBarry Smith   c->ignorezeroentries = a->ignorezeroentries;
279936db0b34SBarry Smith   c->freedata     = PETSC_TRUE;
280017ab2063SBarry Smith 
2801416022c9SBarry Smith   if (a->diag) {
2802b0a32e0cSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(int),&c->diag);CHKERRQ(ierr);
2803b0a32e0cSBarry Smith     PetscLogObjectMemory(C,(m+1)*sizeof(int));
280417ab2063SBarry Smith     for (i=0; i<m; i++) {
2805416022c9SBarry Smith       c->diag[i] = a->diag[i];
280617ab2063SBarry Smith     }
28073a40ed3dSBarry Smith   } else c->diag        = 0;
2808b65db4caSBarry Smith   c->inode.use          = a->inode.use;
28096c7ebb05SLois Curfman McInnes   c->inode.limit        = a->inode.limit;
28106c7ebb05SLois Curfman McInnes   c->inode.max_limit    = a->inode.max_limit;
2811754ec7b1SSatish Balay   if (a->inode.size){
2812b0a32e0cSBarry Smith     ierr                = PetscMalloc((m+1)*sizeof(int),&c->inode.size);CHKERRQ(ierr);
2813754ec7b1SSatish Balay     c->inode.node_count = a->inode.node_count;
2814549d3d68SSatish Balay     ierr                = PetscMemcpy(c->inode.size,a->inode.size,(m+1)*sizeof(int));CHKERRQ(ierr);
2815754ec7b1SSatish Balay   } else {
2816754ec7b1SSatish Balay     c->inode.size       = 0;
2817754ec7b1SSatish Balay     c->inode.node_count = 0;
2818754ec7b1SSatish Balay   }
2819416022c9SBarry Smith   c->nz                 = a->nz;
2820416022c9SBarry Smith   c->maxnz              = a->maxnz;
2821416022c9SBarry Smith   c->solve_work         = 0;
28222a1b7f2aSHong Zhang   C->spptr              = 0;      /* Dangerous -I'm throwing away a->spptr */
2823273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2824754ec7b1SSatish Balay 
2825416022c9SBarry Smith   *B = C;
2826b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
28273a40ed3dSBarry Smith   PetscFunctionReturn(0);
282817ab2063SBarry Smith }
282917ab2063SBarry Smith 
28304a2ae208SSatish Balay #undef __FUNCT__
28314a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
28328e9aea5cSBarry Smith int MatLoad_SeqAIJ(PetscViewer viewer,const MatType type,Mat *A)
283317ab2063SBarry Smith {
2834416022c9SBarry Smith   Mat_SeqAIJ   *a;
2835416022c9SBarry Smith   Mat          B;
2836efb16452SHong Zhang   int          i,nz,ierr,fd,header[4],size,*rowlengths = 0,M,N;
2837bcd2baecSBarry Smith   MPI_Comm     comm;
283817ab2063SBarry Smith 
28393a40ed3dSBarry Smith   PetscFunctionBegin;
2840e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
2841d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
284229bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
2843b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
28440752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
2845552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
284617ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
284717ab2063SBarry Smith 
2848d64ed03dSBarry Smith   if (nz < 0) {
284929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
2850d64ed03dSBarry Smith   }
2851d64ed03dSBarry Smith 
285217ab2063SBarry Smith   /* read in row lengths */
2853b0a32e0cSBarry Smith   ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr);
28540752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
285517ab2063SBarry Smith 
285617ab2063SBarry Smith   /* create our matrix */
2857b3a1e11cSKris Buschelman   ierr = MatCreate(comm,PETSC_DECIDE,PETSC_DECIDE,M,N,&B);CHKERRQ(ierr);
2858b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
2859b3a1e11cSKris Buschelman   ierr = MatSeqAIJSetPreallocation(B,0,rowlengths);CHKERRQ(ierr);
2860416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
286117ab2063SBarry Smith 
286217ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
28630752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
286417ab2063SBarry Smith 
286517ab2063SBarry Smith   /* read in nonzero values */
28660752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
286717ab2063SBarry Smith 
286817ab2063SBarry Smith   /* set matrix "i" values */
2869efb16452SHong Zhang   a->i[0] = 0;
287017ab2063SBarry Smith   for (i=1; i<= M; i++) {
2871416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2872416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
287317ab2063SBarry Smith   }
2874606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
287517ab2063SBarry Smith 
28766d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
28776d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2878b3a1e11cSKris Buschelman   *A = B;
28793a40ed3dSBarry Smith   PetscFunctionReturn(0);
288017ab2063SBarry Smith }
288117ab2063SBarry Smith 
28824a2ae208SSatish Balay #undef __FUNCT__
2883b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
28848f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
28857264ac53SSatish Balay {
28867264ac53SSatish Balay   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
28870f5bd95cSBarry Smith   int        ierr;
28887264ac53SSatish Balay 
28893a40ed3dSBarry Smith   PetscFunctionBegin;
2890bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
2891bfeeae90SHong Zhang   if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)) {
2892ca44d042SBarry Smith     *flg = PETSC_FALSE;
2893ca44d042SBarry Smith     PetscFunctionReturn(0);
2894bcd2baecSBarry Smith   }
28957264ac53SSatish Balay 
28967264ac53SSatish Balay   /* if the a->i are the same */
2897273d9f13SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(int),flg);CHKERRQ(ierr);
28980f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
28997264ac53SSatish Balay 
29007264ac53SSatish Balay   /* if a->j are the same */
29010f5bd95cSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(int),flg);CHKERRQ(ierr);
29020f5bd95cSBarry Smith   if (*flg == PETSC_FALSE) PetscFunctionReturn(0);
2903bcd2baecSBarry Smith 
2904bcd2baecSBarry Smith   /* if a->a are the same */
290587828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
29060f5bd95cSBarry Smith 
29073a40ed3dSBarry Smith   PetscFunctionReturn(0);
29087264ac53SSatish Balay 
29097264ac53SSatish Balay }
291036db0b34SBarry Smith 
29114a2ae208SSatish Balay #undef __FUNCT__
29124a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
291336db0b34SBarry Smith /*@C
291436db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
291536db0b34SBarry Smith               provided by the user.
291636db0b34SBarry Smith 
291736db0b34SBarry Smith       Coolective on MPI_Comm
291836db0b34SBarry Smith 
291936db0b34SBarry Smith    Input Parameters:
292036db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
292136db0b34SBarry Smith .   m - number of rows
292236db0b34SBarry Smith .   n - number of columns
292336db0b34SBarry Smith .   i - row indices
292436db0b34SBarry Smith .   j - column indices
292536db0b34SBarry Smith -   a - matrix values
292636db0b34SBarry Smith 
292736db0b34SBarry Smith    Output Parameter:
292836db0b34SBarry Smith .   mat - the matrix
292936db0b34SBarry Smith 
293036db0b34SBarry Smith    Level: intermediate
293136db0b34SBarry Smith 
293236db0b34SBarry Smith    Notes:
29330551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
293436db0b34SBarry Smith     once the matrix is destroyed
293536db0b34SBarry Smith 
293636db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
293736db0b34SBarry Smith 
2938bfeeae90SHong Zhang        The i and j indices are 0 based
293936db0b34SBarry Smith 
294036db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
294136db0b34SBarry Smith 
294236db0b34SBarry Smith @*/
294387828ca2SBarry Smith int MatCreateSeqAIJWithArrays(MPI_Comm comm,int m,int n,int* i,int*j,PetscScalar *a,Mat *mat)
294436db0b34SBarry Smith {
294536db0b34SBarry Smith   int        ierr,ii;
294636db0b34SBarry Smith   Mat_SeqAIJ *aij;
294736db0b34SBarry Smith 
294836db0b34SBarry Smith   PetscFunctionBegin;
2949c461c341SBarry Smith   ierr = MatCreateSeqAIJ(comm,m,n,SKIP_ALLOCATION,0,mat);CHKERRQ(ierr);
295036db0b34SBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
295136db0b34SBarry Smith 
2952bfeeae90SHong Zhang   if (i[0] != 0) {
2953bfeeae90SHong Zhang     SETERRQ(1,"i (row indices) must start with 0");
295436db0b34SBarry Smith   }
295536db0b34SBarry Smith   aij->i = i;
295636db0b34SBarry Smith   aij->j = j;
295736db0b34SBarry Smith   aij->a = a;
295836db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
295936db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
296036db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
296136db0b34SBarry Smith 
296236db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
296336db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
29649860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g)
296529bbc08cSBarry 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]);
296636db0b34SBarry Smith #endif
296736db0b34SBarry Smith   }
29689860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g)
296936db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
2970bfeeae90SHong Zhang     if (j[ii] < 0) SETERRQ2(1,"Negative column index at location = %d index = %d",ii,j[ii]);
2971bfeeae90SHong Zhang     if (j[ii] > n - 1) SETERRQ2(1,"Column index to large at location = %d index = %d",ii,j[ii]);
297236db0b34SBarry Smith   }
297336db0b34SBarry Smith #endif
297436db0b34SBarry Smith 
2975b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2976b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
297736db0b34SBarry Smith   PetscFunctionReturn(0);
297836db0b34SBarry Smith }
297936db0b34SBarry Smith 
2980cc8ba8e1SBarry Smith #undef __FUNCT__
2981ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
2982ee4f033dSBarry Smith int MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
2983cc8ba8e1SBarry Smith {
2984cc8ba8e1SBarry Smith   int        ierr;
2985cc8ba8e1SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
298636db0b34SBarry Smith 
2987cc8ba8e1SBarry Smith   PetscFunctionBegin;
298812c595b3SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
2989cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
2990cc8ba8e1SBarry Smith     a->coloring = coloring;
299112c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
299208b6dcc0SBarry Smith     int             i,*larray;
299312c595b3SBarry Smith     ISColoring      ocoloring;
299408b6dcc0SBarry Smith     ISColoringValue *colors;
299512c595b3SBarry Smith 
299612c595b3SBarry Smith     /* set coloring for diagonal portion */
299712c595b3SBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(int),&larray);CHKERRQ(ierr);
299812c595b3SBarry Smith     for (i=0; i<A->n; i++) {
299912c595b3SBarry Smith       larray[i] = i;
300012c595b3SBarry Smith     }
300112c595b3SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
300208b6dcc0SBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
300312c595b3SBarry Smith     for (i=0; i<A->n; i++) {
300412c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
300512c595b3SBarry Smith     }
300612c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
300712c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,A->n,colors,&ocoloring);CHKERRQ(ierr);
300812c595b3SBarry Smith     a->coloring = ocoloring;
300912c595b3SBarry Smith   }
3010cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3011cc8ba8e1SBarry Smith }
3012cc8ba8e1SBarry Smith 
301387828ca2SBarry Smith #if defined(PETSC_HAVE_ADIC) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
3014ee4f033dSBarry Smith EXTERN_C_BEGIN
301529c1e371SBarry Smith #include "adic/ad_utils.h"
3016ee4f033dSBarry Smith EXTERN_C_END
3017cc8ba8e1SBarry Smith 
3018cc8ba8e1SBarry Smith #undef __FUNCT__
3019ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3020ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3021cc8ba8e1SBarry Smith {
3022cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
302308b6dcc0SBarry Smith   int             m = A->m,*ii = a->i,*jj = a->j,nz,i,j,nlen;
30244440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
302508b6dcc0SBarry Smith   ISColoringValue *color;
3026cc8ba8e1SBarry Smith 
3027cc8ba8e1SBarry Smith   PetscFunctionBegin;
3028cc8ba8e1SBarry Smith   if (!a->coloring) SETERRQ(1,"Coloring not set for matrix");
30294440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3030cc8ba8e1SBarry Smith   color = a->coloring->colors;
3031cc8ba8e1SBarry Smith   /* loop over rows */
3032cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3033cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3034cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3035cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3036cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3037cc8ba8e1SBarry Smith     }
30384440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3039ee4f033dSBarry Smith   }
3040ee4f033dSBarry Smith   PetscFunctionReturn(0);
3041ee4f033dSBarry Smith }
3042ee4f033dSBarry Smith 
3043ee4f033dSBarry Smith #else
3044ee4f033dSBarry Smith 
3045ee4f033dSBarry Smith #undef __FUNCT__
3046ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3047ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3048ee4f033dSBarry Smith {
3049ee4f033dSBarry Smith   PetscFunctionBegin;
3050ee4f033dSBarry Smith   SETERRQ(1,"PETSc installed without ADIC");
3051ee4f033dSBarry Smith }
3052ee4f033dSBarry Smith 
3053ee4f033dSBarry Smith #endif
3054ee4f033dSBarry Smith 
3055ee4f033dSBarry Smith #undef __FUNCT__
3056ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
3057ee4f033dSBarry Smith int MatSetValuesAdifor_SeqAIJ(Mat A,int nl,void *advalues)
3058ee4f033dSBarry Smith {
3059ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
306008b6dcc0SBarry Smith   int             m = A->m,*ii = a->i,*jj = a->j,nz,i,j;
306187828ca2SBarry Smith   PetscScalar     *v = a->a,*values = (PetscScalar *)advalues;
306208b6dcc0SBarry Smith   ISColoringValue *color;
3063ee4f033dSBarry Smith 
3064ee4f033dSBarry Smith   PetscFunctionBegin;
3065ee4f033dSBarry Smith   if (!a->coloring) SETERRQ(1,"Coloring not set for matrix");
3066ee4f033dSBarry Smith   color = a->coloring->colors;
3067ee4f033dSBarry Smith   /* loop over rows */
3068ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3069ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3070ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3071ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3072ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3073ee4f033dSBarry Smith     }
3074ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3075cc8ba8e1SBarry Smith   }
3076cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3077cc8ba8e1SBarry Smith }
307836db0b34SBarry Smith 
3079