xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 71c2f376a8ea5b67e6a5bb4815321c85a96b4c48)
1b3cc6726SBarry Smith 
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"
1497f1f81fSBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1517ab2063SBarry Smith {
16416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
17dfbe8321SBarry Smith   PetscErrorCode ierr;
1897f1f81fSBarry Smith   PetscInt       i,ishift;
1917ab2063SBarry Smith 
203a40ed3dSBarry Smith   PetscFunctionBegin;
2131625ec5SSatish Balay   *m     = A->m;
223a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
23bfeeae90SHong Zhang   ishift = 0;
2453e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
25273d9f13SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
26bfeeae90SHong Zhang   } else if (oshift == 1) {
2797f1f81fSBarry Smith     PetscInt nz = a->i[A->m];
283b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
2997f1f81fSBarry Smith     ierr = PetscMalloc((A->m+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
3097f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
313b2fbd54SBarry Smith     for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
32273d9f13SBarry Smith     for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] + 1;
336945ee14SBarry Smith   } else {
346945ee14SBarry Smith     *ia = a->i; *ja = a->j;
35a2ce50c7SBarry Smith   }
363a40ed3dSBarry Smith   PetscFunctionReturn(0);
37a2744918SBarry Smith }
38a2744918SBarry Smith 
394a2ae208SSatish Balay #undef __FUNCT__
404a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
4197f1f81fSBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
426945ee14SBarry Smith {
43dfbe8321SBarry Smith   PetscErrorCode ierr;
446945ee14SBarry Smith 
453a40ed3dSBarry Smith   PetscFunctionBegin;
463a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
47bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
48606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
49606d414cSSatish Balay     ierr = PetscFree(*ja);CHKERRQ(ierr);
50bcd2baecSBarry Smith   }
513a40ed3dSBarry Smith   PetscFunctionReturn(0);
5217ab2063SBarry Smith }
5317ab2063SBarry Smith 
544a2ae208SSatish Balay #undef __FUNCT__
554a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
5697f1f81fSBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
573b2fbd54SBarry Smith {
583b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
59dfbe8321SBarry Smith   PetscErrorCode ierr;
6097f1f81fSBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->n,m = A->m;
6197f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
623b2fbd54SBarry Smith 
633a40ed3dSBarry Smith   PetscFunctionBegin;
643b2fbd54SBarry Smith   *nn     = A->n;
653a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
663b2fbd54SBarry Smith   if (symmetric) {
67bfeeae90SHong Zhang     ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
683b2fbd54SBarry Smith   } else {
6997f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
7097f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
7197f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
7297f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
733b2fbd54SBarry Smith     jj = a->j;
743b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
75bfeeae90SHong Zhang       collengths[jj[i]]++;
763b2fbd54SBarry Smith     }
773b2fbd54SBarry Smith     cia[0] = oshift;
783b2fbd54SBarry Smith     for (i=0; i<n; i++) {
793b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
803b2fbd54SBarry Smith     }
8197f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
823b2fbd54SBarry Smith     jj   = a->j;
83a93ec695SBarry Smith     for (row=0; row<m; row++) {
84a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
85a93ec695SBarry Smith       for (i=0; i<mr; i++) {
86bfeeae90SHong Zhang         col = *jj++;
873b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
883b2fbd54SBarry Smith       }
893b2fbd54SBarry Smith     }
90606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
913b2fbd54SBarry Smith     *ia = cia; *ja = cja;
923b2fbd54SBarry Smith   }
933a40ed3dSBarry Smith   PetscFunctionReturn(0);
943b2fbd54SBarry Smith }
953b2fbd54SBarry Smith 
964a2ae208SSatish Balay #undef __FUNCT__
974a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
9897f1f81fSBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
993b2fbd54SBarry Smith {
100dfbe8321SBarry Smith   PetscErrorCode ierr;
101606d414cSSatish Balay 
1023a40ed3dSBarry Smith   PetscFunctionBegin;
1033a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1043b2fbd54SBarry Smith 
105606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
106606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1073b2fbd54SBarry Smith 
1083a40ed3dSBarry Smith   PetscFunctionReturn(0);
1093b2fbd54SBarry Smith }
1103b2fbd54SBarry Smith 
111227d817aSBarry Smith #define CHUNKSIZE   15
11217ab2063SBarry Smith 
1134a2ae208SSatish Balay #undef __FUNCT__
1144a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
11597f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
11617ab2063SBarry Smith {
117416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
118e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
11997f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
1206849ba73SBarry Smith   PetscErrorCode ierr;
121e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
12287828ca2SBarry Smith   PetscScalar    *ap,value,*aa = a->a;
12336db0b34SBarry Smith   PetscTruth     ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE);
124273d9f13SBarry Smith   PetscTruth     roworiented = a->roworiented;
12517ab2063SBarry Smith 
1263a40ed3dSBarry Smith   PetscFunctionBegin;
12717ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
128416022c9SBarry Smith     row  = im[k];
1295ef9f2a5SBarry Smith     if (row < 0) continue;
1302515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
13177431f27SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->m-1);
1323b2fbd54SBarry Smith #endif
133bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
13417ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
135416022c9SBarry Smith     low  = 0;
136c71e6ed7SBarry Smith     high = nrow;
13717ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1385ef9f2a5SBarry Smith       if (in[l] < 0) continue;
1392515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
14077431f27SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->n-1);
1413b2fbd54SBarry Smith #endif
142bfeeae90SHong Zhang       col = in[l];
1434b0e389bSBarry Smith       if (roworiented) {
1445ef9f2a5SBarry Smith         value = v[l + k*n];
145bef8e0ddSBarry Smith       } else {
1464b0e389bSBarry Smith         value = v[k + l*m];
1474b0e389bSBarry Smith       }
148abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
14936db0b34SBarry Smith 
150c71e6ed7SBarry Smith       if (col < lastcol) low = 0; else high = nrow;
151e2ee6c50SBarry Smith       lastcol = col;
152416022c9SBarry Smith       while (high-low > 5) {
153416022c9SBarry Smith         t = (low+high)/2;
154416022c9SBarry Smith         if (rp[t] > col) high = t;
155416022c9SBarry Smith         else             low  = t;
15617ab2063SBarry Smith       }
157416022c9SBarry Smith       for (i=low; i<high; i++) {
15817ab2063SBarry Smith         if (rp[i] > col) break;
15917ab2063SBarry Smith         if (rp[i] == col) {
160416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
16117ab2063SBarry Smith           else                  ap[i] = value;
16217ab2063SBarry Smith           goto noinsert;
16317ab2063SBarry Smith         }
16417ab2063SBarry Smith       }
165abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
166c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
16777431f27SBarry Smith       else if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
16817ab2063SBarry Smith       if (nrow >= rmax) {
16917ab2063SBarry Smith         /* there is no extra room in row, therefore enlarge */
17097f1f81fSBarry Smith         PetscInt         new_nz = ai[A->m] + CHUNKSIZE,*new_i,*new_j;
171a7ed9263SMatthew Knepley         size_t      len;
17287828ca2SBarry Smith         PetscScalar *new_a;
17317ab2063SBarry Smith 
17477431f27SBarry Smith         if (nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix requiring new malloc()",row,col);
17596854ed6SLois Curfman McInnes 
17617ab2063SBarry Smith         /* malloc new storage space */
17797f1f81fSBarry Smith         len     = ((size_t) new_nz)*(sizeof(PetscInt)+sizeof(PetscScalar))+(A->m+1)*sizeof(PetscInt);
178b0a32e0cSBarry Smith 	ierr    = PetscMalloc(len,&new_a);CHKERRQ(ierr);
17997f1f81fSBarry Smith         new_j   = (PetscInt*)(new_a + new_nz);
18017ab2063SBarry Smith         new_i   = new_j + new_nz;
18117ab2063SBarry Smith 
18217ab2063SBarry Smith         /* copy over old data into new slots */
18317ab2063SBarry Smith         for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];}
184273d9f13SBarry Smith         for (ii=row+1; ii<A->m+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;}
18597f1f81fSBarry Smith         ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow)*sizeof(PetscInt));CHKERRQ(ierr);
186bfeeae90SHong Zhang         len  = (((size_t) new_nz) - CHUNKSIZE - ai[row] - nrow );
18797f1f81fSBarry Smith         ierr = PetscMemcpy(new_j+ai[row]+nrow+CHUNKSIZE,aj+ai[row]+nrow,len*sizeof(PetscInt));CHKERRQ(ierr);
188bfeeae90SHong Zhang         ierr = PetscMemcpy(new_a,aa,(((size_t) ai[row])+nrow)*sizeof(PetscScalar));CHKERRQ(ierr);
189bfeeae90SHong Zhang         ierr = PetscMemcpy(new_a+ai[row]+nrow+CHUNKSIZE,aa+ai[row]+nrow,len*sizeof(PetscScalar));CHKERRQ(ierr);
19017ab2063SBarry Smith         /* free up old matrix storage */
191606d414cSSatish Balay         ierr = PetscFree(a->a);CHKERRQ(ierr);
192606d414cSSatish Balay         if (!a->singlemalloc) {
193606d414cSSatish Balay           ierr = PetscFree(a->i);CHKERRQ(ierr);
194606d414cSSatish Balay           ierr = PetscFree(a->j);CHKERRQ(ierr);
195606d414cSSatish Balay         }
196416022c9SBarry Smith         aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;
197f1e2ffcdSBarry Smith         a->singlemalloc = PETSC_TRUE;
19817ab2063SBarry Smith 
199bfeeae90SHong Zhang         rp   = aj + ai[row]; ap = aa + ai[row] ;
200416022c9SBarry Smith         rmax = imax[row] = imax[row] + CHUNKSIZE;
20152e6d16bSBarry Smith         ierr = PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(PetscInt) + sizeof(PetscScalar)));CHKERRQ(ierr);
202416022c9SBarry Smith         a->maxnz += CHUNKSIZE;
203b810aeb4SBarry Smith         a->reallocs++;
20417ab2063SBarry Smith       }
205416022c9SBarry Smith       N = nrow++ - 1; a->nz++;
206416022c9SBarry Smith       /* shift up all the later entries in this row */
207416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
20817ab2063SBarry Smith         rp[ii+1] = rp[ii];
20917ab2063SBarry Smith         ap[ii+1] = ap[ii];
21017ab2063SBarry Smith       }
21117ab2063SBarry Smith       rp[i] = col;
21217ab2063SBarry Smith       ap[i] = value;
21317ab2063SBarry Smith       noinsert:;
214416022c9SBarry Smith       low = i + 1;
21517ab2063SBarry Smith     }
21617ab2063SBarry Smith     ailen[row] = nrow;
21717ab2063SBarry Smith   }
21888e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
2193a40ed3dSBarry Smith   PetscFunctionReturn(0);
22017ab2063SBarry Smith }
22117ab2063SBarry Smith 
2224a2ae208SSatish Balay #undef __FUNCT__
2234a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
22497f1f81fSBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
2257eb43aa7SLois Curfman McInnes {
2267eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
22797f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
22897f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
22987828ca2SBarry Smith   PetscScalar  *ap,*aa = a->a,zero = 0.0;
2307eb43aa7SLois Curfman McInnes 
2313a40ed3dSBarry Smith   PetscFunctionBegin;
2327eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2337eb43aa7SLois Curfman McInnes     row  = im[k];
23477431f27SBarry Smith     if (row < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row);
23577431f27SBarry Smith     if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->m-1);
236bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2377eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2387eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
23977431f27SBarry Smith       if (in[l] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]);
24077431f27SBarry Smith       if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->n-1);
241bfeeae90SHong Zhang       col = in[l] ;
2427eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2437eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2447eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2457eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2467eb43aa7SLois Curfman McInnes         else             low  = t;
2477eb43aa7SLois Curfman McInnes       }
2487eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2497eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2507eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
251b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2527eb43aa7SLois Curfman McInnes           goto finished;
2537eb43aa7SLois Curfman McInnes         }
2547eb43aa7SLois Curfman McInnes       }
255b49de8d1SLois Curfman McInnes       *v++ = zero;
2567eb43aa7SLois Curfman McInnes       finished:;
2577eb43aa7SLois Curfman McInnes     }
2587eb43aa7SLois Curfman McInnes   }
2593a40ed3dSBarry Smith   PetscFunctionReturn(0);
2607eb43aa7SLois Curfman McInnes }
2617eb43aa7SLois Curfman McInnes 
26217ab2063SBarry Smith 
2634a2ae208SSatish Balay #undef __FUNCT__
2644a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
265dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
26617ab2063SBarry Smith {
267416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2686849ba73SBarry Smith   PetscErrorCode ierr;
2696f69ff64SBarry Smith   PetscInt       i,*col_lens;
2706f69ff64SBarry Smith   int            fd;
27117ab2063SBarry Smith 
2723a40ed3dSBarry Smith   PetscFunctionBegin;
273b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
27497f1f81fSBarry Smith   ierr = PetscMalloc((4+A->m)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
275552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
276273d9f13SBarry Smith   col_lens[1] = A->m;
277273d9f13SBarry Smith   col_lens[2] = A->n;
278416022c9SBarry Smith   col_lens[3] = a->nz;
279416022c9SBarry Smith 
280416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
281273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
282416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
28317ab2063SBarry Smith   }
2846f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->m,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
285606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
286416022c9SBarry Smith 
287416022c9SBarry Smith   /* store column indices (zero start index) */
2886f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
289416022c9SBarry Smith 
290416022c9SBarry Smith   /* store nonzero values */
2916f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
2923a40ed3dSBarry Smith   PetscFunctionReturn(0);
29317ab2063SBarry Smith }
294416022c9SBarry Smith 
295dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
296cd155464SBarry Smith 
2974a2ae208SSatish Balay #undef __FUNCT__
2984a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
299dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
300416022c9SBarry Smith {
301416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
302dfbe8321SBarry Smith   PetscErrorCode    ierr;
30397f1f81fSBarry Smith   PetscInt          i,j,m = A->m,shift=0;
304fb9695e5SSatish Balay   char              *name;
305f3ef73ceSBarry Smith   PetscViewerFormat format;
30617ab2063SBarry Smith 
3073a40ed3dSBarry Smith   PetscFunctionBegin;
308435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
309b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
310*71c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
31197f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
312273d9f13SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->n-!shift)) {
313d00d2cf4SBarry Smith       nofinalvalue = 1;
314d00d2cf4SBarry Smith     }
315b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
31677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->n);CHKERRQ(ierr);
31777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
31877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
319b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
32017ab2063SBarry Smith 
32117ab2063SBarry Smith     for (i=0; i<m; i++) {
322416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
323aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
32477431f27SBarry 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);
32517ab2063SBarry Smith #else
32677431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
32717ab2063SBarry Smith #endif
32817ab2063SBarry Smith       }
32917ab2063SBarry Smith     }
330d00d2cf4SBarry Smith     if (nofinalvalue) {
33177431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->n,0.0);CHKERRQ(ierr);
332d00d2cf4SBarry Smith     }
333fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
334b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
335cd155464SBarry Smith   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
336cd155464SBarry Smith      PetscFunctionReturn(0);
337fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
338b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
33944cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
34077431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
34144cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
342aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
34336db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
34477431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
34536db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
34677431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
34736db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
34877431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3496831982aSBarry Smith         }
35044cd7ae7SLois Curfman McInnes #else
35177431f27SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
35244cd7ae7SLois Curfman McInnes #endif
35344cd7ae7SLois Curfman McInnes       }
354b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
35544cd7ae7SLois Curfman McInnes     }
356b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
357fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
35897f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
359b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
36097f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
361496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
362496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
363496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
364496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
365aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
36636db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
367496be53dSLois Curfman McInnes #else
368496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
369496be53dSLois Curfman McInnes #endif
370496be53dSLois Curfman McInnes         }
371496be53dSLois Curfman McInnes       }
372496be53dSLois Curfman McInnes     }
3732e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
37477431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
3752e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
37677431f27SBarry 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);}
37777431f27SBarry 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);}
37877431f27SBarry 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);}
37977431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
38077431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
38177431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
382496be53dSLois Curfman McInnes     }
383b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
384606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
385496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
386496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
38777431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
388496be53dSLois Curfman McInnes       }
389b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
390496be53dSLois Curfman McInnes     }
391b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
392496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
393496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
394496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
395aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
39636db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
397b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
3986831982aSBarry Smith           }
399496be53dSLois Curfman McInnes #else
400b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
401496be53dSLois Curfman McInnes #endif
402496be53dSLois Curfman McInnes         }
403496be53dSLois Curfman McInnes       }
404b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
405496be53dSLois Curfman McInnes     }
406b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
407fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
40897f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
40987828ca2SBarry Smith     PetscScalar value;
41002594712SBarry Smith 
411b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
41202594712SBarry Smith     for (i=0; i<m; i++) {
41302594712SBarry Smith       jcnt = 0;
414273d9f13SBarry Smith       for (j=0; j<A->n; j++) {
415e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
41602594712SBarry Smith           value = a->a[cnt++];
417e24b481bSBarry Smith           jcnt++;
41802594712SBarry Smith         } else {
41902594712SBarry Smith           value = 0.0;
42002594712SBarry Smith         }
421aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
422b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
42302594712SBarry Smith #else
424b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
42502594712SBarry Smith #endif
42602594712SBarry Smith       }
427b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
42802594712SBarry Smith     }
429b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4303a40ed3dSBarry Smith   } else {
431b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
43217ab2063SBarry Smith     for (i=0; i<m; i++) {
43377431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
434416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
435aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
43636db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
43777431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
43836db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
43977431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4403a40ed3dSBarry Smith         } else {
44177431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
44217ab2063SBarry Smith         }
44317ab2063SBarry Smith #else
44477431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
44517ab2063SBarry Smith #endif
44617ab2063SBarry Smith       }
447b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
44817ab2063SBarry Smith     }
449b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
45017ab2063SBarry Smith   }
451b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4523a40ed3dSBarry Smith   PetscFunctionReturn(0);
453416022c9SBarry Smith }
454416022c9SBarry Smith 
4554a2ae208SSatish Balay #undef __FUNCT__
4564a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
457dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
458416022c9SBarry Smith {
459480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
460416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
461dfbe8321SBarry Smith   PetscErrorCode    ierr;
46297f1f81fSBarry Smith   PetscInt          i,j,m = A->m,color;
46336db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
464b0a32e0cSBarry Smith   PetscViewer       viewer;
465f3ef73ceSBarry Smith   PetscViewerFormat format;
466cddf8d76SBarry Smith 
4673a40ed3dSBarry Smith   PetscFunctionBegin;
468480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
469b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
47019bcc07fSBarry Smith 
471b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
472416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
4730513a670SBarry Smith 
474fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
4750513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
476b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
477416022c9SBarry Smith     for (i=0; i<m; i++) {
478cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
479bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
480bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
481aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
48236db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
483cddf8d76SBarry Smith #else
484cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
485cddf8d76SBarry Smith #endif
486b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
487cddf8d76SBarry Smith       }
488cddf8d76SBarry Smith     }
489b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
490cddf8d76SBarry Smith     for (i=0; i<m; i++) {
491cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
492bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
493bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
494cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
495b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
496cddf8d76SBarry Smith       }
497cddf8d76SBarry Smith     }
498b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
499cddf8d76SBarry Smith     for (i=0; i<m; i++) {
500cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
501bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
502bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
503aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
50436db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
505cddf8d76SBarry Smith #else
506cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
507cddf8d76SBarry Smith #endif
508b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
509416022c9SBarry Smith       }
510416022c9SBarry Smith     }
5110513a670SBarry Smith   } else {
5120513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5130513a670SBarry Smith     /* first determine max of all nonzero values */
51497f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
515b0a32e0cSBarry Smith     PetscDraw   popup;
51636db0b34SBarry Smith     PetscReal scale;
5170513a670SBarry Smith 
5180513a670SBarry Smith     for (i=0; i<nz; i++) {
5190513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5200513a670SBarry Smith     }
521b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
522b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
523b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5240513a670SBarry Smith     count = 0;
5250513a670SBarry Smith     for (i=0; i<m; i++) {
5260513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
527bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
528bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
52997f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
530b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5310513a670SBarry Smith         count++;
5320513a670SBarry Smith       }
5330513a670SBarry Smith     }
5340513a670SBarry Smith   }
535480ef9eaSBarry Smith   PetscFunctionReturn(0);
536480ef9eaSBarry Smith }
537cddf8d76SBarry Smith 
5384a2ae208SSatish Balay #undef __FUNCT__
5394a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
540dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
541480ef9eaSBarry Smith {
542dfbe8321SBarry Smith   PetscErrorCode ierr;
543b0a32e0cSBarry Smith   PetscDraw      draw;
54436db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
545480ef9eaSBarry Smith   PetscTruth     isnull;
546480ef9eaSBarry Smith 
547480ef9eaSBarry Smith   PetscFunctionBegin;
548b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
549b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
550480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
551480ef9eaSBarry Smith 
552480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
553273d9f13SBarry Smith   xr  = A->n; yr = A->m; h = yr/10.0; w = xr/10.0;
554480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
555b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
556b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
557480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
5583a40ed3dSBarry Smith   PetscFunctionReturn(0);
559416022c9SBarry Smith }
560416022c9SBarry Smith 
5614a2ae208SSatish Balay #undef __FUNCT__
5624a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
563dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
564416022c9SBarry Smith {
565416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
566dfbe8321SBarry Smith   PetscErrorCode ierr;
56732077d6dSBarry Smith   PetscTruth     issocket,iascii,isbinary,isdraw;
568416022c9SBarry Smith 
5693a40ed3dSBarry Smith   PetscFunctionBegin;
570b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
57132077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
572fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
573fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
5740f5bd95cSBarry Smith   if (issocket) {
575b0a32e0cSBarry Smith     ierr = PetscViewerSocketPutSparse_Private(viewer,A->m,A->n,a->nz,a->a,a->i,a->j);CHKERRQ(ierr);
57632077d6dSBarry Smith   } else if (iascii) {
5773a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
5780f5bd95cSBarry Smith   } else if (isbinary) {
5793a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
5800f5bd95cSBarry Smith   } else if (isdraw) {
5813a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
5825cd90555SBarry Smith   } else {
583958c9bccSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
58417ab2063SBarry Smith   }
585*71c2f376SKris Buschelman   /* Call parent MatView here */
5863a40ed3dSBarry Smith   PetscFunctionReturn(0);
58717ab2063SBarry Smith }
58819bcc07fSBarry Smith 
5894a2ae208SSatish Balay #undef __FUNCT__
5904a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
591dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
59217ab2063SBarry Smith {
593416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
5946849ba73SBarry Smith   PetscErrorCode ierr;
59597f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
59697f1f81fSBarry Smith   PetscInt       m = A->m,*ip,N,*ailen = a->ilen,rmax = 0;
59787828ca2SBarry Smith   PetscScalar    *aa = a->a,*ap;
5983447b6efSHong Zhang   PetscReal      ratio=0.6;
59917ab2063SBarry Smith 
6003a40ed3dSBarry Smith   PetscFunctionBegin;
6013a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
60217ab2063SBarry Smith 
60343ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
60417ab2063SBarry Smith   for (i=1; i<m; i++) {
605416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
60617ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
60794a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
60817ab2063SBarry Smith     if (fshift) {
609bfeeae90SHong Zhang       ip = aj + ai[i] ;
610bfeeae90SHong Zhang       ap = aa + ai[i] ;
61117ab2063SBarry Smith       N  = ailen[i];
61217ab2063SBarry Smith       for (j=0; j<N; j++) {
61317ab2063SBarry Smith         ip[j-fshift] = ip[j];
61417ab2063SBarry Smith         ap[j-fshift] = ap[j];
61517ab2063SBarry Smith       }
61617ab2063SBarry Smith     }
61717ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
61817ab2063SBarry Smith   }
61917ab2063SBarry Smith   if (m) {
62017ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
62117ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
62217ab2063SBarry Smith   }
62317ab2063SBarry Smith   /* reset ilen and imax for each row */
62417ab2063SBarry Smith   for (i=0; i<m; i++) {
62517ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
62617ab2063SBarry Smith   }
627bfeeae90SHong Zhang   a->nz = ai[m];
62817ab2063SBarry Smith 
62917ab2063SBarry Smith   /* diagonals may have moved, so kill the diagonal pointers */
630416022c9SBarry Smith   if (fshift && a->diag) {
631606d414cSSatish Balay     ierr = PetscFree(a->diag);CHKERRQ(ierr);
63252e6d16bSBarry Smith     ierr = PetscLogObjectMemory(A,-(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
633416022c9SBarry Smith     a->diag = 0;
63417ab2063SBarry Smith   }
63577431f27SBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->n,fshift,a->nz);
63677431f27SBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %D\n",a->reallocs);
6371466f1e1SBarry Smith   PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Maximum nonzeros in any row is %D\n",rmax);
638dd5f02e7SSatish Balay   a->reallocs          = 0;
6394e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
64036db0b34SBarry Smith   a->rmax              = rmax;
6414e220ebcSLois Curfman McInnes 
642cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
643317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
64488e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
645*71c2f376SKris Buschelman 
646*71c2f376SKris Buschelman   /* Call parent MatAssemblyEnd here */
6473a40ed3dSBarry Smith   PetscFunctionReturn(0);
64817ab2063SBarry Smith }
64917ab2063SBarry Smith 
6504a2ae208SSatish Balay #undef __FUNCT__
6514a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
652dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
65317ab2063SBarry Smith {
654416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
655dfbe8321SBarry Smith   PetscErrorCode ierr;
6563a40ed3dSBarry Smith 
6573a40ed3dSBarry Smith   PetscFunctionBegin;
658bfeeae90SHong Zhang   ierr = PetscMemzero(a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
6593a40ed3dSBarry Smith   PetscFunctionReturn(0);
66017ab2063SBarry Smith }
661416022c9SBarry Smith 
6624a2ae208SSatish Balay #undef __FUNCT__
6634a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
664dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
66517ab2063SBarry Smith {
666416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
667dfbe8321SBarry Smith   PetscErrorCode ierr;
668d5d45c9bSBarry Smith 
6693a40ed3dSBarry Smith   PetscFunctionBegin;
670aa482453SBarry Smith #if defined(PETSC_USE_LOG)
67177431f27SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->m,A->n,a->nz);
67217ab2063SBarry Smith #endif
67336db0b34SBarry Smith   if (a->freedata) {
674606d414cSSatish Balay     ierr = PetscFree(a->a);CHKERRQ(ierr);
675606d414cSSatish Balay     if (!a->singlemalloc) {
676606d414cSSatish Balay       ierr = PetscFree(a->i);CHKERRQ(ierr);
677606d414cSSatish Balay       ierr = PetscFree(a->j);CHKERRQ(ierr);
678606d414cSSatish Balay     }
67936db0b34SBarry Smith   }
680c38d4ed2SBarry Smith   if (a->row) {
681c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
682c38d4ed2SBarry Smith   }
683c38d4ed2SBarry Smith   if (a->col) {
684c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
685c38d4ed2SBarry Smith   }
686606d414cSSatish Balay   if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
687606d414cSSatish Balay   if (a->ilen) {ierr = PetscFree(a->ilen);CHKERRQ(ierr);}
688606d414cSSatish Balay   if (a->imax) {ierr = PetscFree(a->imax);CHKERRQ(ierr);}
689273d9f13SBarry Smith   if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);}
690606d414cSSatish Balay   if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);}
69182bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
692606d414cSSatish Balay   if (a->saved_values) {ierr = PetscFree(a->saved_values);CHKERRQ(ierr);}
693cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
694a30b2313SHong Zhang   if (a->xtoy) {ierr = PetscFree(a->xtoy);CHKERRQ(ierr);}
695d487561eSHong Zhang   if (a->compressedrow.use){ierr = PetscFree(a->compressedrow.i);}
696a30b2313SHong Zhang 
697606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
698901853e0SKris Buschelman 
699901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
700901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
701901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
702901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
703901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
704901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
705901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
706901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
707*71c2f376SKris Buschelman 
708*71c2f376SKris Buschelman   /* Call parent destroy anywhere in this routine */
7093a40ed3dSBarry Smith   PetscFunctionReturn(0);
71017ab2063SBarry Smith }
71117ab2063SBarry Smith 
7124a2ae208SSatish Balay #undef __FUNCT__
7134a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ"
714dfbe8321SBarry Smith PetscErrorCode MatCompress_SeqAIJ(Mat A)
71517ab2063SBarry Smith {
7163a40ed3dSBarry Smith   PetscFunctionBegin;
7173a40ed3dSBarry Smith   PetscFunctionReturn(0);
71817ab2063SBarry Smith }
71917ab2063SBarry Smith 
7204a2ae208SSatish Balay #undef __FUNCT__
7214a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
722dfbe8321SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op)
72317ab2063SBarry Smith {
724416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
7253a40ed3dSBarry Smith 
7263a40ed3dSBarry Smith   PetscFunctionBegin;
727a65d3064SKris Buschelman   switch (op) {
728a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
729a65d3064SKris Buschelman       a->roworiented       = PETSC_TRUE;
730a65d3064SKris Buschelman       break;
731a65d3064SKris Buschelman     case MAT_KEEP_ZEROED_ROWS:
732a65d3064SKris Buschelman       a->keepzeroedrows    = PETSC_TRUE;
733a65d3064SKris Buschelman       break;
734a65d3064SKris Buschelman     case MAT_COLUMN_ORIENTED:
735a65d3064SKris Buschelman       a->roworiented       = PETSC_FALSE;
736a65d3064SKris Buschelman       break;
737a65d3064SKris Buschelman     case MAT_COLUMNS_SORTED:
738a65d3064SKris Buschelman       a->sorted            = PETSC_TRUE;
739a65d3064SKris Buschelman       break;
740a65d3064SKris Buschelman     case MAT_COLUMNS_UNSORTED:
741a65d3064SKris Buschelman       a->sorted            = PETSC_FALSE;
742a65d3064SKris Buschelman       break;
743a65d3064SKris Buschelman     case MAT_NO_NEW_NONZERO_LOCATIONS:
744a65d3064SKris Buschelman       a->nonew             = 1;
745a65d3064SKris Buschelman       break;
746a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
747a65d3064SKris Buschelman       a->nonew             = -1;
748a65d3064SKris Buschelman       break;
749a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
750a65d3064SKris Buschelman       a->nonew             = -2;
751a65d3064SKris Buschelman       break;
752a65d3064SKris Buschelman     case MAT_YES_NEW_NONZERO_LOCATIONS:
753a65d3064SKris Buschelman       a->nonew             = 0;
754a65d3064SKris Buschelman       break;
755a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
756a65d3064SKris Buschelman       a->ignorezeroentries = PETSC_TRUE;
757a65d3064SKris Buschelman       break;
758d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
759d487561eSHong Zhang       a->compressedrow.use = PETSC_TRUE;
760d487561eSHong Zhang       break;
761d487561eSHong Zhang     case MAT_DO_NOT_USE_COMPRESSEDROW:
762d487561eSHong Zhang       a->compressedrow.use = PETSC_FALSE;
763d487561eSHong Zhang       break;
764a65d3064SKris Buschelman     case MAT_ROWS_SORTED:
765a65d3064SKris Buschelman     case MAT_ROWS_UNSORTED:
766a65d3064SKris Buschelman     case MAT_YES_NEW_DIAGONALS:
767a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
768a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
769b0a32e0cSBarry Smith       PetscLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n");
770a65d3064SKris Buschelman       break;
771a65d3064SKris Buschelman     case MAT_NO_NEW_DIAGONALS:
77229bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
773a65d3064SKris Buschelman     default:
774*71c2f376SKris Buschelman       break;
775a65d3064SKris Buschelman   }
776*71c2f376SKris Buschelman   /* Call parent MatSetOption here */
7773a40ed3dSBarry Smith   PetscFunctionReturn(0);
77817ab2063SBarry Smith }
77917ab2063SBarry Smith 
7804a2ae208SSatish Balay #undef __FUNCT__
7814a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
782dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
78317ab2063SBarry Smith {
784416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7856849ba73SBarry Smith   PetscErrorCode ierr;
78697f1f81fSBarry Smith   PetscInt       i,j,n;
78787828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
78817ab2063SBarry Smith 
7893a40ed3dSBarry Smith   PetscFunctionBegin;
7903a40ed3dSBarry Smith   ierr = VecSet(&zero,v);CHKERRQ(ierr);
7911ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
79236db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
793273d9f13SBarry Smith   if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
794273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
795bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
796bfeeae90SHong Zhang       if (a->j[j] == i) {
797416022c9SBarry Smith         x[i] = a->a[j];
79817ab2063SBarry Smith         break;
79917ab2063SBarry Smith       }
80017ab2063SBarry Smith     }
80117ab2063SBarry Smith   }
8021ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
8033a40ed3dSBarry Smith   PetscFunctionReturn(0);
80417ab2063SBarry Smith }
80517ab2063SBarry Smith 
8064a2ae208SSatish Balay #undef __FUNCT__
8074a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
808dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
80917ab2063SBarry Smith {
810416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
8115c897100SBarry Smith   PetscScalar       *x,*y;
812dfbe8321SBarry Smith   PetscErrorCode    ierr;
81397f1f81fSBarry Smith   PetscInt          m = A->m;
8145c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
8155c897100SBarry Smith   PetscScalar       *v,alpha;
8167b2bb3b9SHong Zhang   PetscInt          n,i,*idx,*ii,*ridx=PETSC_NULL;
8173447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
8184eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
8195c897100SBarry Smith #endif
82017ab2063SBarry Smith 
8213a40ed3dSBarry Smith   PetscFunctionBegin;
8222e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
8231ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8241ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
8255c897100SBarry Smith 
8265c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
827bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
8285c897100SBarry Smith #else
8293447b6efSHong Zhang   if (usecprow){
8303447b6efSHong Zhang     m    = cprow.nrows;
8313447b6efSHong Zhang     ii   = cprow.i;
8327b2bb3b9SHong Zhang     ridx = cprow.rindex;
8333447b6efSHong Zhang   } else {
8343447b6efSHong Zhang     ii = a->i;
8353447b6efSHong Zhang   }
83617ab2063SBarry Smith   for (i=0; i<m; i++) {
8373447b6efSHong Zhang     idx   = a->j + ii[i] ;
8383447b6efSHong Zhang     v     = a->a + ii[i] ;
8393447b6efSHong Zhang     n     = ii[i+1] - ii[i];
8403447b6efSHong Zhang     if (usecprow){
8417b2bb3b9SHong Zhang       alpha = x[ridx[i]];
8423447b6efSHong Zhang     } else {
84317ab2063SBarry Smith       alpha = x[i];
8443447b6efSHong Zhang     }
84517ab2063SBarry Smith     while (n-->0) {y[*idx++] += alpha * *v++;}
84617ab2063SBarry Smith   }
8475c897100SBarry Smith #endif
848b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
8491ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8501ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8513a40ed3dSBarry Smith   PetscFunctionReturn(0);
85217ab2063SBarry Smith }
85317ab2063SBarry Smith 
8544a2ae208SSatish Balay #undef __FUNCT__
8555c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
856dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
8575c897100SBarry Smith {
8588d5b0100SBarry Smith   PetscScalar    zero = 0.0;
859dfbe8321SBarry Smith   PetscErrorCode ierr;
8605c897100SBarry Smith 
8615c897100SBarry Smith   PetscFunctionBegin;
8625c897100SBarry Smith   ierr = VecSet(&zero,yy);CHKERRQ(ierr);
8635c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
8645c897100SBarry Smith   PetscFunctionReturn(0);
8655c897100SBarry Smith }
8665c897100SBarry Smith 
8675c897100SBarry Smith 
8685c897100SBarry Smith #undef __FUNCT__
8694a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
870dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
87117ab2063SBarry Smith {
872416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
87397952fefSHong Zhang   PetscScalar    *x,*y,*aa;
874dfbe8321SBarry Smith   PetscErrorCode ierr;
87597952fefSHong Zhang   PetscInt       m=A->m,*aj,*ii;
876aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
87797952fefSHong Zhang   PetscInt       n,i,jrow,j,*ridx=PETSC_NULL;
878362ced78SSatish Balay   PetscScalar    sum;
87997952fefSHong Zhang   PetscTruth     usecprow=a->compressedrow.use;
880e36a17ebSSatish Balay #endif
88117ab2063SBarry Smith 
882b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
88397952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
884fee21e36SBarry Smith #endif
885fee21e36SBarry Smith 
8863a40ed3dSBarry Smith   PetscFunctionBegin;
8871ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8881ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
88997952fefSHong Zhang   aj  = a->j;
89097952fefSHong Zhang   aa    = a->a;
891416022c9SBarry Smith   ii   = a->i;
892aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
89397952fefSHong Zhang   fortranmultaij_(&m,x,ii,aj,aa,y);
8948d195f9aSBarry Smith #else
8954eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
89697952fefSHong Zhang     m    = a->compressedrow.nrows;
89797952fefSHong Zhang     ii   = a->compressedrow.i;
89897952fefSHong Zhang     ridx = a->compressedrow.rindex;
89997952fefSHong Zhang     for (i=0; i<m; i++){
90097952fefSHong Zhang       n   = ii[i+1] - ii[i];
90197952fefSHong Zhang       aj  = a->j + ii[i];
90297952fefSHong Zhang       aa  = a->a + ii[i];
90397952fefSHong Zhang       sum = 0.0;
90497952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
90597952fefSHong Zhang       y[*ridx++] = sum;
90697952fefSHong Zhang     }
90797952fefSHong Zhang   } else { /* do not use compressed row format */
90817ab2063SBarry Smith     for (i=0; i<m; i++) {
9099ea0dfa2SSatish Balay       jrow = ii[i];
9109ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
91117ab2063SBarry Smith       sum  = 0.0;
9129ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
91397952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
9149ea0dfa2SSatish Balay       }
91517ab2063SBarry Smith       y[i] = sum;
91617ab2063SBarry Smith     }
91797952fefSHong Zhang   }
9188d195f9aSBarry Smith #endif
919b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz - m);
9201ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
9211ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9223a40ed3dSBarry Smith   PetscFunctionReturn(0);
92317ab2063SBarry Smith }
92417ab2063SBarry Smith 
9254a2ae208SSatish Balay #undef __FUNCT__
9264a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
927dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
92817ab2063SBarry Smith {
929416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
93097952fefSHong Zhang   PetscScalar    *x,*y,*z,*aa;
931dfbe8321SBarry Smith   PetscErrorCode ierr;
93297952fefSHong Zhang   PetscInt       m = A->m,*aj,*ii;
933aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
93497952fefSHong Zhang   PetscInt       n,i,jrow,j,*ridx=PETSC_NULL;
935362ced78SSatish Balay   PetscScalar    sum;
93697952fefSHong Zhang   PetscTruth     usecprow=a->compressedrow.use;
937e36a17ebSSatish Balay #endif
9389ea0dfa2SSatish Balay 
9393a40ed3dSBarry Smith   PetscFunctionBegin;
9401ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9411ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9422e8a6d31SBarry Smith   if (zz != yy) {
9431ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9442e8a6d31SBarry Smith   } else {
9452e8a6d31SBarry Smith     z = y;
9462e8a6d31SBarry Smith   }
947bfeeae90SHong Zhang 
94897952fefSHong Zhang   aj  = a->j;
94997952fefSHong Zhang   aa  = a->a;
950cddf8d76SBarry Smith   ii  = a->i;
951aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
95297952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
95302ab625aSSatish Balay #else
9544eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
9554eb6d288SHong Zhang     if (zz != yy){
9564eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
9574eb6d288SHong Zhang     }
95897952fefSHong Zhang     m    = a->compressedrow.nrows;
95997952fefSHong Zhang     ii   = a->compressedrow.i;
96097952fefSHong Zhang     ridx = a->compressedrow.rindex;
96197952fefSHong Zhang     for (i=0; i<m; i++){
96297952fefSHong Zhang       n  = ii[i+1] - ii[i];
96397952fefSHong Zhang       aj  = a->j + ii[i];
96497952fefSHong Zhang       aa  = a->a + ii[i];
96597952fefSHong Zhang       sum = y[*ridx];
96697952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
96797952fefSHong Zhang       z[*ridx++] = sum;
96897952fefSHong Zhang     }
96997952fefSHong Zhang   } else { /* do not use compressed row format */
97017ab2063SBarry Smith     for (i=0; i<m; i++) {
9719ea0dfa2SSatish Balay       jrow = ii[i];
9729ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
97317ab2063SBarry Smith       sum  = y[i];
9749ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
97597952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
9769ea0dfa2SSatish Balay       }
97717ab2063SBarry Smith       z[i] = sum;
97817ab2063SBarry Smith     }
97997952fefSHong Zhang   }
98002ab625aSSatish Balay #endif
981b0a32e0cSBarry Smith   PetscLogFlops(2*a->nz);
9821ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
9831ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9842e8a6d31SBarry Smith   if (zz != yy) {
9851ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
9862e8a6d31SBarry Smith   }
9873a40ed3dSBarry Smith   PetscFunctionReturn(0);
98817ab2063SBarry Smith }
98917ab2063SBarry Smith 
99017ab2063SBarry Smith /*
99117ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
99217ab2063SBarry Smith */
9934a2ae208SSatish Balay #undef __FUNCT__
9944a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
995dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
99617ab2063SBarry Smith {
997416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9986849ba73SBarry Smith   PetscErrorCode ierr;
99997f1f81fSBarry Smith   PetscInt       i,j,*diag,m = A->m;
100017ab2063SBarry Smith 
10013a40ed3dSBarry Smith   PetscFunctionBegin;
1002f1e2ffcdSBarry Smith   if (a->diag) PetscFunctionReturn(0);
1003f1e2ffcdSBarry Smith 
100497f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&diag);CHKERRQ(ierr);
100552e6d16bSBarry Smith   ierr = PetscLogObjectMemory(A,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
1006273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
100735b0346bSBarry Smith     diag[i] = a->i[i+1];
1008bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1009bfeeae90SHong Zhang       if (a->j[j] == i) {
1010bfeeae90SHong Zhang         diag[i] = j;
101117ab2063SBarry Smith         break;
101217ab2063SBarry Smith       }
101317ab2063SBarry Smith     }
101417ab2063SBarry Smith   }
1015416022c9SBarry Smith   a->diag = diag;
10163a40ed3dSBarry Smith   PetscFunctionReturn(0);
101717ab2063SBarry Smith }
101817ab2063SBarry Smith 
1019be5855fcSBarry Smith /*
1020be5855fcSBarry Smith      Checks for missing diagonals
1021be5855fcSBarry Smith */
10224a2ae208SSatish Balay #undef __FUNCT__
10234a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1024dfbe8321SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A)
1025be5855fcSBarry Smith {
1026be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10276849ba73SBarry Smith   PetscErrorCode ierr;
102897f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1029be5855fcSBarry Smith 
1030be5855fcSBarry Smith   PetscFunctionBegin;
1031f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1032f1e2ffcdSBarry Smith   diag = a->diag;
1033273d9f13SBarry Smith   for (i=0; i<A->m; i++) {
1034bfeeae90SHong Zhang     if (jj[diag[i]] != i) {
103577431f27SBarry Smith       SETERRQ1(PETSC_ERR_PLIB,"Matrix is missing diagonal number %D",i);
1036be5855fcSBarry Smith     }
1037be5855fcSBarry Smith   }
1038be5855fcSBarry Smith   PetscFunctionReturn(0);
1039be5855fcSBarry Smith }
1040be5855fcSBarry Smith 
10414a2ae208SSatish Balay #undef __FUNCT__
10424a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ"
104397f1f81fSBarry Smith PetscErrorCode MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
104417ab2063SBarry Smith {
1045416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1046beeb8507SBarry Smith   PetscScalar        *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag;
1047beeb8507SBarry Smith   const PetscScalar  *v = a->a, *b, *bs,*xb, *ts;
1048dfbe8321SBarry Smith   PetscErrorCode     ierr;
104997f1f81fSBarry Smith   PetscInt           n = A->n,m = A->m,i;
105097f1f81fSBarry Smith   const PetscInt     *idx,*diag;
105117ab2063SBarry Smith 
10523a40ed3dSBarry Smith   PetscFunctionBegin;
1053b965ef7fSBarry Smith   its = its*lits;
105477431f27SBarry Smith   if (its <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
105591723122SBarry Smith 
1056ed480e8bSBarry Smith   if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);}
1057ed480e8bSBarry Smith   diag = a->diag;
1058ed480e8bSBarry Smith   if (!a->idiag) {
1059ed480e8bSBarry Smith     ierr     = PetscMalloc(3*m*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr);
1060ed480e8bSBarry Smith     a->ssor  = a->idiag + m;
1061ed480e8bSBarry Smith     mdiag    = a->ssor + m;
1062ed480e8bSBarry Smith 
1063ed480e8bSBarry Smith     v        = a->a;
1064ed480e8bSBarry Smith 
1065ed480e8bSBarry Smith     /* this is wrong when fshift omega changes each iteration */
1066958c9bccSBarry Smith     if (omega == 1.0 && !fshift) {
1067ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1068ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1069ed480e8bSBarry Smith         a->idiag[i] = 1.0/v[diag[i]];
1070ed480e8bSBarry Smith       }
1071beeb8507SBarry Smith       PetscLogFlops(m);
1072ed480e8bSBarry Smith     } else {
1073ed480e8bSBarry Smith       for (i=0; i<m; i++) {
1074ed480e8bSBarry Smith         mdiag[i]    = v[diag[i]];
1075beeb8507SBarry Smith         a->idiag[i] = omega/(fshift + v[diag[i]]);
1076ed480e8bSBarry Smith       }
1077beeb8507SBarry Smith       PetscLogFlops(2*m);
1078beeb8507SBarry Smith     }
1079ed480e8bSBarry Smith   }
1080ed480e8bSBarry Smith   t     = a->ssor;
1081ed480e8bSBarry Smith   idiag = a->idiag;
1082ed480e8bSBarry Smith   mdiag = a->idiag + 2*m;
1083ed480e8bSBarry Smith 
10841ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1085fb2e594dSBarry Smith   if (xx != bb) {
10861ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1087fb2e594dSBarry Smith   } else {
1088fb2e594dSBarry Smith     b = x;
1089fb2e594dSBarry Smith   }
1090fb2e594dSBarry Smith 
1091ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1092ed480e8bSBarry Smith   xs   = x;
109317ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
109417ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1095ed480e8bSBarry Smith     bs = b;
109617ab2063SBarry Smith     for (i=0; i<m; i++) {
1097ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1098416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1099ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1100ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
110117ab2063SBarry Smith         sum  = b[i]*d/omega;
110217ab2063SBarry Smith         SPARSEDENSEDOT(sum,bs,v,idx,n);
110317ab2063SBarry Smith         x[i] = sum;
110417ab2063SBarry Smith     }
11051ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11061ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1107ed480e8bSBarry Smith     PetscLogFlops(a->nz);
11083a40ed3dSBarry Smith     PetscFunctionReturn(0);
110917ab2063SBarry Smith   }
1110c783ea89SBarry Smith 
1111ed480e8bSBarry Smith 
1112fc3d8934SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1113fc3d8934SBarry Smith     U is upper triangular, E is diagonal; This routine applies
1114fc3d8934SBarry Smith 
1115fc3d8934SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
1116fc3d8934SBarry Smith 
1117fc3d8934SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
1118fc3d8934SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
111948af12d7SBarry Smith     is the relaxation factor.
1120fc3d8934SBarry Smith     */
1121fc3d8934SBarry Smith 
112248af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
112329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
11243a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
112517ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
112617ab2063SBarry Smith     U is upper triangular, E is diagonal; This routine applies
112717ab2063SBarry Smith 
112817ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
112917ab2063SBarry Smith 
113017ab2063SBarry Smith     to a vector efficiently using Eisenstat's trick. This is for
113117ab2063SBarry Smith     the case of SSOR preconditioner, so E is D/omega where omega
113217ab2063SBarry Smith     is the relaxation factor.
113317ab2063SBarry Smith     */
113417ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
113517ab2063SBarry Smith 
113617ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
113717ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1138416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1139ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1140ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
114117ab2063SBarry Smith       sum  = b[i];
114217ab2063SBarry Smith       SPARSEDENSEMDOT(sum,xs,v,idx,n);
1143ed480e8bSBarry Smith       x[i] = sum*idiag[i];
114417ab2063SBarry Smith     }
114517ab2063SBarry Smith 
114617ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1147416022c9SBarry Smith     v = a->a;
1148ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
114917ab2063SBarry Smith 
115017ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1151ed480e8bSBarry Smith     ts = t;
1152416022c9SBarry Smith     diag = a->diag;
115317ab2063SBarry Smith     for (i=0; i<m; i++) {
1154416022c9SBarry Smith       n    = diag[i] - a->i[i];
1155ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1156ed480e8bSBarry Smith       v    = a->a + a->i[i];
115717ab2063SBarry Smith       sum  = t[i];
115817ab2063SBarry Smith       SPARSEDENSEMDOT(sum,ts,v,idx,n);
1159ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1160733d66baSBarry Smith       /*  x = x + t */
1161733d66baSBarry Smith       x[i] += t[i];
116217ab2063SBarry Smith     }
116317ab2063SBarry Smith 
1164b0a32e0cSBarry Smith     PetscLogFlops(6*m-1 + 2*a->nz);
11651ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11661ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
11673a40ed3dSBarry Smith     PetscFunctionReturn(0);
116817ab2063SBarry Smith   }
116917ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
117017ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
117177d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
117297f1f81fSBarry Smith       fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)b);
117377d8c4bbSBarry Smith #else
117417ab2063SBarry Smith       for (i=0; i<m; i++) {
1175416022c9SBarry Smith         n    = diag[i] - a->i[i];
1176ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1177ed480e8bSBarry Smith         v    = a->a + a->i[i];
117817ab2063SBarry Smith         sum  = b[i];
117917ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1180ed480e8bSBarry Smith         x[i] = sum*idiag[i];
118117ab2063SBarry Smith       }
118277d8c4bbSBarry Smith #endif
118317ab2063SBarry Smith       xb = x;
1184ed480e8bSBarry Smith       PetscLogFlops(a->nz);
11853a40ed3dSBarry Smith     } else xb = b;
118617ab2063SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
118717ab2063SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
118817ab2063SBarry Smith       for (i=0; i<m; i++) {
1189ed480e8bSBarry Smith         x[i] *= mdiag[i];
119017ab2063SBarry Smith       }
1191b0a32e0cSBarry Smith       PetscLogFlops(m);
119217ab2063SBarry Smith     }
119317ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
119477d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
119597f1f81fSBarry Smith       fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)xb);
119677d8c4bbSBarry Smith #else
119717ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1198416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1199ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1200ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
120117ab2063SBarry Smith         sum  = xb[i];
120217ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1203ed480e8bSBarry Smith         x[i] = sum*idiag[i];
120417ab2063SBarry Smith       }
120577d8c4bbSBarry Smith #endif
1206ed480e8bSBarry Smith       PetscLogFlops(a->nz);
120717ab2063SBarry Smith     }
120817ab2063SBarry Smith     its--;
120917ab2063SBarry Smith   }
121017ab2063SBarry Smith   while (its--) {
121117ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
121277d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
121397f1f81fSBarry Smith       fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
121477d8c4bbSBarry Smith #else
121517ab2063SBarry Smith       for (i=0; i<m; i++) {
1216ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1217416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1218ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1219ed480e8bSBarry Smith         v    = a->a + a->i[i];
122017ab2063SBarry Smith         sum  = b[i];
122117ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1222ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
122317ab2063SBarry Smith       }
122477d8c4bbSBarry Smith #endif
1225ed480e8bSBarry Smith       PetscLogFlops(a->nz);
122617ab2063SBarry Smith     }
122717ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
122877d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
122997f1f81fSBarry Smith       fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b);
123077d8c4bbSBarry Smith #else
123117ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1232ed480e8bSBarry Smith         d    = fshift + a->a[diag[i]];
1233416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1234ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1235ed480e8bSBarry Smith         v    = a->a + a->i[i];
123617ab2063SBarry Smith         sum  = b[i];
123717ab2063SBarry Smith         SPARSEDENSEMDOT(sum,xs,v,idx,n);
1238ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
123917ab2063SBarry Smith       }
124077d8c4bbSBarry Smith #endif
1241ed480e8bSBarry Smith       PetscLogFlops(a->nz);
124217ab2063SBarry Smith     }
124317ab2063SBarry Smith   }
12441ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12451ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12463a40ed3dSBarry Smith   PetscFunctionReturn(0);
124717ab2063SBarry Smith }
124817ab2063SBarry Smith 
12494a2ae208SSatish Balay #undef __FUNCT__
12504a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1251dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
125217ab2063SBarry Smith {
1253416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
12544e220ebcSLois Curfman McInnes 
12553a40ed3dSBarry Smith   PetscFunctionBegin;
1256273d9f13SBarry Smith   info->rows_global    = (double)A->m;
1257273d9f13SBarry Smith   info->columns_global = (double)A->n;
1258273d9f13SBarry Smith   info->rows_local     = (double)A->m;
1259273d9f13SBarry Smith   info->columns_local  = (double)A->n;
12604e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
12614e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
12624e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
12634e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
12644e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
12654e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
12664e220ebcSLois Curfman McInnes   info->memory         = A->mem;
12674e220ebcSLois Curfman McInnes   if (A->factor) {
12684e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
12694e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
12704e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
12714e220ebcSLois Curfman McInnes   } else {
12724e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
12734e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
12744e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
12754e220ebcSLois Curfman McInnes   }
12763a40ed3dSBarry Smith   PetscFunctionReturn(0);
127717ab2063SBarry Smith }
127817ab2063SBarry Smith 
12794a2ae208SSatish Balay #undef __FUNCT__
12804a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1281dfbe8321SBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,IS is,const PetscScalar *diag)
128217ab2063SBarry Smith {
1283416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
12846849ba73SBarry Smith   PetscErrorCode ierr;
128597f1f81fSBarry Smith   PetscInt       i,N,*rows,m = A->m - 1;
128617ab2063SBarry Smith 
12873a40ed3dSBarry Smith   PetscFunctionBegin;
1288b9b97703SBarry Smith   ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr);
128917ab2063SBarry Smith   ierr = ISGetIndices(is,&rows);CHKERRQ(ierr);
1290f1e2ffcdSBarry Smith   if (a->keepzeroedrows) {
1291f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
129277431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1293bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1294f1e2ffcdSBarry Smith     }
1295f1e2ffcdSBarry Smith     if (diag) {
1296f1e2ffcdSBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1297f1e2ffcdSBarry Smith       ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
1298f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1299f1e2ffcdSBarry Smith         a->a[a->diag[rows[i]]] = *diag;
1300f1e2ffcdSBarry Smith       }
1301f1e2ffcdSBarry Smith     }
130288e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1303f1e2ffcdSBarry Smith   } else {
130417ab2063SBarry Smith     if (diag) {
130517ab2063SBarry Smith       for (i=0; i<N; i++) {
130677431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
13077ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1308416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1309bfeeae90SHong Zhang           a->a[a->i[rows[i]]] = *diag;
1310bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
13117ae801bdSBarry Smith         } else { /* in case row was completely empty */
1312d64ed03dSBarry Smith           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr);
131317ab2063SBarry Smith         }
131417ab2063SBarry Smith       }
13153a40ed3dSBarry Smith     } else {
131617ab2063SBarry Smith       for (i=0; i<N; i++) {
131777431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1318416022c9SBarry Smith         a->ilen[rows[i]] = 0;
131917ab2063SBarry Smith       }
132017ab2063SBarry Smith     }
132188e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1322f1e2ffcdSBarry Smith   }
13237ae801bdSBarry Smith   ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr);
132443a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13253a40ed3dSBarry Smith   PetscFunctionReturn(0);
132617ab2063SBarry Smith }
132717ab2063SBarry Smith 
13284a2ae208SSatish Balay #undef __FUNCT__
13294a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
133097f1f81fSBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
133117ab2063SBarry Smith {
1332416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
133397f1f81fSBarry Smith   PetscInt   *itmp;
133417ab2063SBarry Smith 
13353a40ed3dSBarry Smith   PetscFunctionBegin;
133677431f27SBarry Smith   if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
133717ab2063SBarry Smith 
1338416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1339bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
134017ab2063SBarry Smith   if (idx) {
1341bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1342bfeeae90SHong Zhang     if (*nz) {
13434e093b46SBarry Smith       *idx = itmp;
134417ab2063SBarry Smith     }
134517ab2063SBarry Smith     else *idx = 0;
134617ab2063SBarry Smith   }
13473a40ed3dSBarry Smith   PetscFunctionReturn(0);
134817ab2063SBarry Smith }
134917ab2063SBarry Smith 
1350bfeeae90SHong Zhang /* remove this function? */
13514a2ae208SSatish Balay #undef __FUNCT__
13524a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
135397f1f81fSBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
135417ab2063SBarry Smith {
13553a40ed3dSBarry Smith   PetscFunctionBegin;
13563a40ed3dSBarry Smith   PetscFunctionReturn(0);
135717ab2063SBarry Smith }
135817ab2063SBarry Smith 
13594a2ae208SSatish Balay #undef __FUNCT__
13604a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1361dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
136217ab2063SBarry Smith {
1363416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
136487828ca2SBarry Smith   PetscScalar    *v = a->a;
136536db0b34SBarry Smith   PetscReal      sum = 0.0;
13666849ba73SBarry Smith   PetscErrorCode ierr;
136797f1f81fSBarry Smith   PetscInt       i,j;
136817ab2063SBarry Smith 
13693a40ed3dSBarry Smith   PetscFunctionBegin;
137017ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1371416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1372aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
137336db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
137417ab2063SBarry Smith #else
137517ab2063SBarry Smith       sum += (*v)*(*v); v++;
137617ab2063SBarry Smith #endif
137717ab2063SBarry Smith     }
1378064f8208SBarry Smith     *nrm = sqrt(sum);
13793a40ed3dSBarry Smith   } else if (type == NORM_1) {
138036db0b34SBarry Smith     PetscReal *tmp;
138197f1f81fSBarry Smith     PetscInt    *jj = a->j;
1382b0a32e0cSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1383273d9f13SBarry Smith     ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr);
1384064f8208SBarry Smith     *nrm = 0.0;
1385416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1386bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
138717ab2063SBarry Smith     }
1388273d9f13SBarry Smith     for (j=0; j<A->n; j++) {
1389064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
139017ab2063SBarry Smith     }
1391606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
13923a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1393064f8208SBarry Smith     *nrm = 0.0;
1394273d9f13SBarry Smith     for (j=0; j<A->m; j++) {
1395bfeeae90SHong Zhang       v = a->a + a->i[j];
139617ab2063SBarry Smith       sum = 0.0;
1397416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1398cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
139917ab2063SBarry Smith       }
1400064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
140117ab2063SBarry Smith     }
14023a40ed3dSBarry Smith   } else {
140329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
140417ab2063SBarry Smith   }
14053a40ed3dSBarry Smith   PetscFunctionReturn(0);
140617ab2063SBarry Smith }
140717ab2063SBarry Smith 
14084a2ae208SSatish Balay #undef __FUNCT__
14094a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1410dfbe8321SBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,Mat *B)
141117ab2063SBarry Smith {
1412416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1413416022c9SBarry Smith   Mat            C;
14146849ba73SBarry Smith   PetscErrorCode ierr;
141597f1f81fSBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->m,len,*col;
141687828ca2SBarry Smith   PetscScalar    *array = a->a;
141717ab2063SBarry Smith 
14183a40ed3dSBarry Smith   PetscFunctionBegin;
1419273d9f13SBarry Smith   if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
142097f1f81fSBarry Smith   ierr = PetscMalloc((1+A->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
142197f1f81fSBarry Smith   ierr = PetscMemzero(col,(1+A->n)*sizeof(PetscInt));CHKERRQ(ierr);
1422bfeeae90SHong Zhang 
1423bfeeae90SHong Zhang   for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
1424f204ca49SKris Buschelman   ierr = MatCreate(A->comm,A->n,m,A->n,m,&C);CHKERRQ(ierr);
1425f204ca49SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1426f204ca49SKris Buschelman   ierr = MatSeqAIJSetPreallocation(C,0,col);CHKERRQ(ierr);
1427606d414cSSatish Balay   ierr = PetscFree(col);CHKERRQ(ierr);
142817ab2063SBarry Smith   for (i=0; i<m; i++) {
142917ab2063SBarry Smith     len    = ai[i+1]-ai[i];
1430416022c9SBarry Smith     ierr   = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1431b9b97703SBarry Smith     array += len;
1432b9b97703SBarry Smith     aj    += len;
143317ab2063SBarry Smith   }
143417ab2063SBarry Smith 
14356d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14366d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
143717ab2063SBarry Smith 
1438f1e2ffcdSBarry Smith   if (B) {
1439416022c9SBarry Smith     *B = C;
144017ab2063SBarry Smith   } else {
1441273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
144217ab2063SBarry Smith   }
14433a40ed3dSBarry Smith   PetscFunctionReturn(0);
144417ab2063SBarry Smith }
144517ab2063SBarry Smith 
1446cd0d46ebSvictorle EXTERN_C_BEGIN
1447cd0d46ebSvictorle #undef __FUNCT__
14485fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1449dfbe8321SBarry Smith PetscErrorCode MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1450cd0d46ebSvictorle {
1451cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
145297f1f81fSBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr; PetscScalar *va,*vb;
14536849ba73SBarry Smith   PetscErrorCode ierr;
145497f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1455cd0d46ebSvictorle 
1456cd0d46ebSvictorle   PetscFunctionBegin;
1457cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1458cd0d46ebSvictorle 
1459cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1460cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
14615485867bSBarry Smith   if (ma!=nb || na!=mb){
14625485867bSBarry Smith     *f = PETSC_FALSE;
14635485867bSBarry Smith     PetscFunctionReturn(0);
14645485867bSBarry Smith   }
1465cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1466cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1467cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
146897f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
146997f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1470cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1471cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1472cd0d46ebSvictorle 
1473cd0d46ebSvictorle   *f = PETSC_TRUE;
1474cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1475cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
147697f1f81fSBarry Smith       PetscInt         idc,idr;
14775485867bSBarry Smith       PetscScalar vc,vr;
1478cd0d46ebSvictorle       /* column/row index/value */
14795485867bSBarry Smith       idc = adx[aptr[i]];
14805485867bSBarry Smith       idr = bdx[bptr[idc]];
14815485867bSBarry Smith       vc  = va[aptr[i]];
14825485867bSBarry Smith       vr  = vb[bptr[idc]];
14835485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
14845485867bSBarry Smith 	*f = PETSC_FALSE;
14855485867bSBarry Smith         goto done;
1486cd0d46ebSvictorle       } else {
14875485867bSBarry Smith 	aptr[i]++;
14885485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1489cd0d46ebSvictorle       }
1490cd0d46ebSvictorle     }
1491cd0d46ebSvictorle   }
1492cd0d46ebSvictorle  done:
1493cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
14943aeef889SHong Zhang   if (B) {
14953aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
14963aeef889SHong Zhang   }
1497cd0d46ebSvictorle   PetscFunctionReturn(0);
1498cd0d46ebSvictorle }
1499cd0d46ebSvictorle EXTERN_C_END
1500cd0d46ebSvictorle 
15019e29f15eSvictorle #undef __FUNCT__
15029e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1503dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
15049e29f15eSvictorle {
1505dfbe8321SBarry Smith   PetscErrorCode ierr;
15069e29f15eSvictorle   PetscFunctionBegin;
15075485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
15089e29f15eSvictorle   PetscFunctionReturn(0);
15099e29f15eSvictorle }
15109e29f15eSvictorle 
15114a2ae208SSatish Balay #undef __FUNCT__
15124a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1513dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
151417ab2063SBarry Smith {
1515416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
151687828ca2SBarry Smith   PetscScalar    *l,*r,x,*v;
1517dfbe8321SBarry Smith   PetscErrorCode ierr;
151897f1f81fSBarry Smith   PetscInt       i,j,m = A->m,n = A->n,M,nz = a->nz,*jj;
151917ab2063SBarry Smith 
15203a40ed3dSBarry Smith   PetscFunctionBegin;
152117ab2063SBarry Smith   if (ll) {
15223ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
15233ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1524e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1525273d9f13SBarry Smith     if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
15261ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1527416022c9SBarry Smith     v = a->a;
152817ab2063SBarry Smith     for (i=0; i<m; i++) {
152917ab2063SBarry Smith       x = l[i];
1530416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
153117ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
153217ab2063SBarry Smith     }
15331ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1534b0a32e0cSBarry Smith     PetscLogFlops(nz);
153517ab2063SBarry Smith   }
153617ab2063SBarry Smith   if (rr) {
1537e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1538273d9f13SBarry Smith     if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
15391ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1540416022c9SBarry Smith     v = a->a; jj = a->j;
154117ab2063SBarry Smith     for (i=0; i<nz; i++) {
1542bfeeae90SHong Zhang       (*v++) *= r[*jj++];
154317ab2063SBarry Smith     }
15441ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1545b0a32e0cSBarry Smith     PetscLogFlops(nz);
154617ab2063SBarry Smith   }
15473a40ed3dSBarry Smith   PetscFunctionReturn(0);
154817ab2063SBarry Smith }
154917ab2063SBarry Smith 
15504a2ae208SSatish Balay #undef __FUNCT__
15514a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
155297f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
155317ab2063SBarry Smith {
1554db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
15556849ba73SBarry Smith   PetscErrorCode ierr;
155697f1f81fSBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->n,*lens;
155797f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
155897f1f81fSBarry Smith   PetscInt       *irow,*icol,nrows,ncols;
155997f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
156087828ca2SBarry Smith   PetscScalar    *a_new,*mat_a;
1561416022c9SBarry Smith   Mat            C;
1562fee21e36SBarry Smith   PetscTruth     stride;
156317ab2063SBarry Smith 
15643a40ed3dSBarry Smith   PetscFunctionBegin;
1565d64ed03dSBarry Smith   ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr);
156629bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
1567d64ed03dSBarry Smith   ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr);
156829bbc08cSBarry Smith   if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
156999141d43SSatish Balay 
157017ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1571b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1572b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
157317ab2063SBarry Smith 
1574fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1575fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1576fee21e36SBarry Smith   if (stride && step == 1) {
157702834360SBarry Smith     /* special case of contiguous rows */
157897f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
157931ebf83bSSatish Balay     starts = lens + nrows;
158002834360SBarry Smith     /* loop over new rows determining lens and starting points */
158102834360SBarry Smith     for (i=0; i<nrows; i++) {
1582bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1583a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
158402834360SBarry Smith       for (k=kstart; k<kend; k++) {
1585bfeeae90SHong Zhang         if (aj[k] >= first) {
158602834360SBarry Smith           starts[i] = k;
158702834360SBarry Smith           break;
158802834360SBarry Smith 	}
158902834360SBarry Smith       }
1590a2744918SBarry Smith       sum = 0;
159102834360SBarry Smith       while (k < kend) {
1592bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1593a2744918SBarry Smith         sum++;
159402834360SBarry Smith       }
1595a2744918SBarry Smith       lens[i] = sum;
159602834360SBarry Smith     }
159702834360SBarry Smith     /* create submatrix */
1598cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
159997f1f81fSBarry Smith       PetscInt n_cols,n_rows;
160008480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
160129bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1602d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
160308480c60SBarry Smith       C = *B;
16043a40ed3dSBarry Smith     } else {
1605e2d9671bSKris Buschelman       ierr = MatCreate(A->comm,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE,&C);CHKERRQ(ierr);
1606e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1607e2d9671bSKris Buschelman       ierr = MatSeqAIJSetPreallocation(C,0,lens);CHKERRQ(ierr);
160808480c60SBarry Smith     }
1609db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1610db02288aSLois Curfman McInnes 
161102834360SBarry Smith     /* loop over rows inserting into submatrix */
1612db02288aSLois Curfman McInnes     a_new    = c->a;
1613db02288aSLois Curfman McInnes     j_new    = c->j;
1614db02288aSLois Curfman McInnes     i_new    = c->i;
1615bfeeae90SHong Zhang 
161602834360SBarry Smith     for (i=0; i<nrows; i++) {
1617a2744918SBarry Smith       ii    = starts[i];
1618a2744918SBarry Smith       lensi = lens[i];
1619a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1620a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
162102834360SBarry Smith       }
162287828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1623a2744918SBarry Smith       a_new      += lensi;
1624a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1625a2744918SBarry Smith       c->ilen[i]  = lensi;
162602834360SBarry Smith     }
1627606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
16283a40ed3dSBarry Smith   } else {
162902834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
163097f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1631bfeeae90SHong Zhang 
163297f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
163397f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
163417ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
163502834360SBarry Smith     /* determine lens of each row */
163602834360SBarry Smith     for (i=0; i<nrows; i++) {
1637bfeeae90SHong Zhang       kstart  = ai[irow[i]];
163802834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
163902834360SBarry Smith       lens[i] = 0;
164002834360SBarry Smith       for (k=kstart; k<kend; k++) {
1641bfeeae90SHong Zhang         if (smap[aj[k]]) {
164202834360SBarry Smith           lens[i]++;
164302834360SBarry Smith         }
164402834360SBarry Smith       }
164502834360SBarry Smith     }
164617ab2063SBarry Smith     /* Create and fill new matrix */
1647a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
16480f5bd95cSBarry Smith       PetscTruth equal;
16490f5bd95cSBarry Smith 
165099141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1651273d9f13SBarry Smith       if ((*B)->m  != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
165297f1f81fSBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(PetscInt),&equal);CHKERRQ(ierr);
16530f5bd95cSBarry Smith       if (!equal) {
165429bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
165599141d43SSatish Balay       }
165697f1f81fSBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(PetscInt));CHKERRQ(ierr);
165708480c60SBarry Smith       C = *B;
16583a40ed3dSBarry Smith     } else {
1659e2d9671bSKris Buschelman       ierr = MatCreate(A->comm,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE,&C);CHKERRQ(ierr);
1660e2d9671bSKris Buschelman       ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
1661e2d9671bSKris Buschelman       ierr = MatSeqAIJSetPreallocation(C,0,lens);CHKERRQ(ierr);
166208480c60SBarry Smith     }
166399141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
166417ab2063SBarry Smith     for (i=0; i<nrows; i++) {
166599141d43SSatish Balay       row    = irow[i];
1666bfeeae90SHong Zhang       kstart = ai[row];
166799141d43SSatish Balay       kend   = kstart + a->ilen[row];
1668bfeeae90SHong Zhang       mat_i  = c->i[i];
166999141d43SSatish Balay       mat_j  = c->j + mat_i;
167099141d43SSatish Balay       mat_a  = c->a + mat_i;
167199141d43SSatish Balay       mat_ilen = c->ilen + i;
167217ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1673bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1674ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
167599141d43SSatish Balay           *mat_a++ = a->a[k];
167699141d43SSatish Balay           (*mat_ilen)++;
167799141d43SSatish Balay 
167817ab2063SBarry Smith         }
167917ab2063SBarry Smith       }
168017ab2063SBarry Smith     }
168102834360SBarry Smith     /* Free work space */
168202834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1683606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1684606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
168502834360SBarry Smith   }
16866d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16876d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
168817ab2063SBarry Smith 
168917ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1690416022c9SBarry Smith   *B = C;
16913a40ed3dSBarry Smith   PetscFunctionReturn(0);
169217ab2063SBarry Smith }
169317ab2063SBarry Smith 
1694a871dcd8SBarry Smith /*
1695a871dcd8SBarry Smith */
16964a2ae208SSatish Balay #undef __FUNCT__
16974a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
1698dfbe8321SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info)
1699a871dcd8SBarry Smith {
170063b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1701dfbe8321SBarry Smith   PetscErrorCode ierr;
170263b91edcSBarry Smith   Mat            outA;
1703b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
170463b91edcSBarry Smith 
17053a40ed3dSBarry Smith   PetscFunctionBegin;
1706d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1707b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1708b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1709b8a78c4aSBarry Smith   if (!row_identity || !col_identity) {
1710634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for in-place ILU");
1711b8a78c4aSBarry Smith   }
1712a871dcd8SBarry Smith 
171363b91edcSBarry Smith   outA          = inA;
171463b91edcSBarry Smith   inA->factor   = FACTOR_LU;
171563b91edcSBarry Smith   a->row        = row;
171663b91edcSBarry Smith   a->col        = col;
1717c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1718c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
171963b91edcSBarry Smith 
172036db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1721b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
17224c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
172352e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1724f0ec6fceSSatish Balay 
172594a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
172687828ca2SBarry Smith      ierr = PetscMalloc((inA->m+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
172794a9d846SBarry Smith   }
172863b91edcSBarry Smith 
172908480c60SBarry Smith   if (!a->diag) {
1730f1e2ffcdSBarry Smith     ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
173163b91edcSBarry Smith   }
1732af281ebdSHong Zhang   ierr = MatLUFactorNumeric_SeqAIJ(inA,info,&outA);CHKERRQ(ierr);
17333a40ed3dSBarry Smith   PetscFunctionReturn(0);
1734a871dcd8SBarry Smith }
1735a871dcd8SBarry Smith 
1736d9eff348SSatish Balay #include "petscblaslapack.h"
17374a2ae208SSatish Balay #undef __FUNCT__
17384a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1739dfbe8321SBarry Smith PetscErrorCode MatScale_SeqAIJ(const PetscScalar *alpha,Mat inA)
1740f0b747eeSBarry Smith {
1741f0b747eeSBarry Smith   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)inA->data;
17424ce68768SBarry Smith   PetscBLASInt bnz = (PetscBLASInt)a->nz,one = 1;
17433a40ed3dSBarry Smith 
17443a40ed3dSBarry Smith   PetscFunctionBegin;
174571044d3cSBarry Smith   BLASscal_(&bnz,(PetscScalar*)alpha,a->a,&one);
1746b0a32e0cSBarry Smith   PetscLogFlops(a->nz);
17473a40ed3dSBarry Smith   PetscFunctionReturn(0);
1748f0b747eeSBarry Smith }
1749f0b747eeSBarry Smith 
17504a2ae208SSatish Balay #undef __FUNCT__
17514a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
175297f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1753cddf8d76SBarry Smith {
1754dfbe8321SBarry Smith   PetscErrorCode ierr;
175597f1f81fSBarry Smith   PetscInt       i;
1756cddf8d76SBarry Smith 
17573a40ed3dSBarry Smith   PetscFunctionBegin;
1758cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1759b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1760cddf8d76SBarry Smith   }
1761cddf8d76SBarry Smith 
1762cddf8d76SBarry Smith   for (i=0; i<n; i++) {
17636a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1764cddf8d76SBarry Smith   }
17653a40ed3dSBarry Smith   PetscFunctionReturn(0);
1766cddf8d76SBarry Smith }
1767cddf8d76SBarry Smith 
17684a2ae208SSatish Balay #undef __FUNCT__
17694a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
177097f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
17714dcbc457SBarry Smith {
1772e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
17736849ba73SBarry Smith   PetscErrorCode ierr;
177497f1f81fSBarry Smith   PetscInt       row,i,j,k,l,m,n,*idx,*nidx,isz,val;
177597f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1776f1af5d2fSBarry Smith   PetscBT        table;
1777bbd702dbSSatish Balay 
17783a40ed3dSBarry Smith   PetscFunctionBegin;
1779273d9f13SBarry Smith   m     = A->m;
1780e4d965acSSatish Balay   ai    = a->i;
1781bfeeae90SHong Zhang   aj    = a->j;
17828a047759SSatish Balay 
1783a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
178406763907SSatish Balay 
178597f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
17866831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
178706763907SSatish Balay 
1788e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1789b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1790e4d965acSSatish Balay     isz  = 0;
17916831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1792e4d965acSSatish Balay 
1793e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
17944dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1795b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1796e4d965acSSatish Balay 
1797dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1798e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1799f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
18004dcbc457SBarry Smith     }
180106763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
180206763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1803e4d965acSSatish Balay 
180404a348a9SBarry Smith     k = 0;
180504a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
180604a348a9SBarry Smith       n = isz;
180706763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1808e4d965acSSatish Balay         row   = nidx[k];
1809e4d965acSSatish Balay         start = ai[row];
1810e4d965acSSatish Balay         end   = ai[row+1];
181104a348a9SBarry Smith         for (l = start; l<end ; l++){
1812efb16452SHong Zhang           val = aj[l] ;
1813f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1814e4d965acSSatish Balay         }
1815e4d965acSSatish Balay       }
1816e4d965acSSatish Balay     }
1817029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1818e4d965acSSatish Balay   }
18196831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1820606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
18213a40ed3dSBarry Smith   PetscFunctionReturn(0);
18224dcbc457SBarry Smith }
182317ab2063SBarry Smith 
18240513a670SBarry Smith /* -------------------------------------------------------------- */
18254a2ae208SSatish Balay #undef __FUNCT__
18264a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1827dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
18280513a670SBarry Smith {
18290513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
18306849ba73SBarry Smith   PetscErrorCode ierr;
183197f1f81fSBarry Smith   PetscInt       i,nz,m = A->m,n = A->n,*col;
183297f1f81fSBarry Smith   PetscInt       *row,*cnew,j,*lens;
183356cd22aeSBarry Smith   IS             icolp,irowp;
183497f1f81fSBarry Smith   PetscInt       *cwork;
183532ec9ce4SBarry Smith   PetscScalar    *vwork;
18360513a670SBarry Smith 
18373a40ed3dSBarry Smith   PetscFunctionBegin;
18384c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
183956cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
18404c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
184156cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
18420513a670SBarry Smith 
18430513a670SBarry Smith   /* determine lengths of permuted rows */
184497f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
18450513a670SBarry Smith   for (i=0; i<m; i++) {
18460513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
18470513a670SBarry Smith   }
1848f204ca49SKris Buschelman   ierr = MatCreate(A->comm,m,n,m,n,B);CHKERRQ(ierr);
1849f204ca49SKris Buschelman   ierr = MatSetType(*B,A->type_name);CHKERRQ(ierr);
1850f204ca49SKris Buschelman   ierr = MatSeqAIJSetPreallocation(*B,0,lens);CHKERRQ(ierr);
1851606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
18520513a670SBarry Smith 
185397f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
18540513a670SBarry Smith   for (i=0; i<m; i++) {
185532ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18560513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
1857cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
185832ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
18590513a670SBarry Smith   }
1860606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
18613c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
18620513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18630513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
186456cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
186556cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
186656cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
186756cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
18683a40ed3dSBarry Smith   PetscFunctionReturn(0);
18690513a670SBarry Smith }
18700513a670SBarry Smith 
18714a2ae208SSatish Balay #undef __FUNCT__
18724a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ"
1873dfbe8321SBarry Smith PetscErrorCode MatPrintHelp_SeqAIJ(Mat A)
1874682d7d0cSBarry Smith {
1875c38d4ed2SBarry Smith   static PetscTruth called = PETSC_FALSE;
1876682d7d0cSBarry Smith   MPI_Comm          comm = A->comm;
1877dfbe8321SBarry Smith   PetscErrorCode    ierr;
1878682d7d0cSBarry Smith 
18793a40ed3dSBarry Smith   PetscFunctionBegin;
1880c38d4ed2SBarry Smith   if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE;
1881d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr);
1882d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr);
1883d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(comm,"  -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr);
188473e7a558SHong Zhang   ierr = (*PetscHelpPrintf)(comm,"  -mat_no_compressedrow: Do not use compressedrow\n");CHKERRQ(ierr);
1885*71c2f376SKris Buschelman   /* Call parent MatPrintHelp here */
18863a40ed3dSBarry Smith   PetscFunctionReturn(0);
1887682d7d0cSBarry Smith }
188897304618SKris Buschelman 
18894a2ae208SSatish Balay #undef __FUNCT__
18904a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
1891dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
1892cb5b572fSBarry Smith {
1893dfbe8321SBarry Smith   PetscErrorCode ierr;
1894cb5b572fSBarry Smith 
1895cb5b572fSBarry Smith   PetscFunctionBegin;
189633f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
189733f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
1898be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1899be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
1900be6bf707SBarry Smith 
1901bfeeae90SHong Zhang     if (a->i[A->m] != b->i[B->m]) {
1902634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
1903cb5b572fSBarry Smith     }
1904bfeeae90SHong Zhang     ierr = PetscMemcpy(b->a,a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr);
1905cb5b572fSBarry Smith   } else {
1906cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1907cb5b572fSBarry Smith   }
1908cb5b572fSBarry Smith   PetscFunctionReturn(0);
1909cb5b572fSBarry Smith }
1910cb5b572fSBarry Smith 
19114a2ae208SSatish Balay #undef __FUNCT__
19124a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
1913dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
1914273d9f13SBarry Smith {
1915dfbe8321SBarry Smith   PetscErrorCode ierr;
1916273d9f13SBarry Smith 
1917273d9f13SBarry Smith   PetscFunctionBegin;
1918273d9f13SBarry Smith   ierr =  MatSeqAIJSetPreallocation(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
1919273d9f13SBarry Smith   PetscFunctionReturn(0);
1920273d9f13SBarry Smith }
1921273d9f13SBarry Smith 
19224a2ae208SSatish Balay #undef __FUNCT__
19234a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
1924dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
19256c0721eeSBarry Smith {
19266c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
19276c0721eeSBarry Smith   PetscFunctionBegin;
19286c0721eeSBarry Smith   *array = a->a;
19296c0721eeSBarry Smith   PetscFunctionReturn(0);
19306c0721eeSBarry Smith }
19316c0721eeSBarry Smith 
19324a2ae208SSatish Balay #undef __FUNCT__
19334a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
1934dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
19356c0721eeSBarry Smith {
19366c0721eeSBarry Smith   PetscFunctionBegin;
19376c0721eeSBarry Smith   PetscFunctionReturn(0);
19386c0721eeSBarry Smith }
1939273d9f13SBarry Smith 
1940ee4f033dSBarry Smith #undef __FUNCT__
1941ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
1942dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
1943ee4f033dSBarry Smith {
19446849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
19456849ba73SBarry Smith   PetscErrorCode ierr;
194697f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
194787828ca2SBarry Smith   PetscScalar    dx,mone = -1.0,*y,*xx,*w3_array;
194887828ca2SBarry Smith   PetscScalar    *vscale_array;
1949ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
1950ee4f033dSBarry Smith   Vec            w1,w2,w3;
1951ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
1952ee4f033dSBarry Smith   PetscTruth     flg;
1953ee4f033dSBarry Smith 
1954ee4f033dSBarry Smith   PetscFunctionBegin;
1955ee4f033dSBarry Smith   if (!coloring->w1) {
1956ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
195752e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
1958ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
195952e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
1960ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
196152e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
1962ee4f033dSBarry Smith   }
1963ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
1964ee4f033dSBarry Smith 
1965ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
1966e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(coloring->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr);
1967ee4f033dSBarry Smith   if (flg) {
1968ee4f033dSBarry Smith     PetscLogInfo(coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n");
1969ee4f033dSBarry Smith   } else {
19700b9b6f31SBarry Smith     PetscTruth assembled;
19710b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
19720b9b6f31SBarry Smith     if (assembled) {
1973ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
1974ee4f033dSBarry Smith     }
19750b9b6f31SBarry Smith   }
1976ee4f033dSBarry Smith 
1977ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
1978ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
1979ee4f033dSBarry Smith 
1980ee4f033dSBarry Smith   /*
1981ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
1982ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
1983ee4f033dSBarry Smith   */
1984ee4f033dSBarry Smith   if (coloring->F) {
1985ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
1986ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
1987ee4f033dSBarry Smith     if (m1 != m2) {
1988ee4f033dSBarry Smith       coloring->F = 0;
1989ee4f033dSBarry Smith     }
1990ee4f033dSBarry Smith   }
1991ee4f033dSBarry Smith 
1992ee4f033dSBarry Smith   if (coloring->F) {
1993ee4f033dSBarry Smith     w1          = coloring->F;
1994ee4f033dSBarry Smith     coloring->F = 0;
1995ee4f033dSBarry Smith   } else {
199666f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1997ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
199866f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
1999ee4f033dSBarry Smith   }
2000ee4f033dSBarry Smith 
2001ee4f033dSBarry Smith   /*
2002ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2003ee4f033dSBarry Smith   */
20041ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
20051ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2006ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2007ee4f033dSBarry Smith     /*
2008ee4f033dSBarry Smith        Loop over each column associated with color adding the
2009ee4f033dSBarry Smith        perturbation to the vector w3.
2010ee4f033dSBarry Smith     */
2011ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2012ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2013ee4f033dSBarry Smith       dx  = xx[col];
2014ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2015ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2016ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2017ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2018ee4f033dSBarry Smith #else
2019ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2020ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2021ee4f033dSBarry Smith #endif
2022ee4f033dSBarry Smith       dx                *= epsilon;
2023ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2024ee4f033dSBarry Smith     }
2025ee4f033dSBarry Smith   }
20261ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2027ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2028ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2029ee4f033dSBarry Smith 
2030ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2031ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2032ee4f033dSBarry Smith 
2033ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2034ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2035ee4f033dSBarry Smith 
20361ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2037ee4f033dSBarry Smith   /*
2038ee4f033dSBarry Smith       Loop over each color
2039ee4f033dSBarry Smith   */
2040ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
204149b058dcSBarry Smith     coloring->currentcolor = k;
2042ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
20431ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2044ee4f033dSBarry Smith     /*
2045ee4f033dSBarry Smith        Loop over each column associated with color adding the
2046ee4f033dSBarry Smith        perturbation to the vector w3.
2047ee4f033dSBarry Smith     */
2048ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2049ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2050ee4f033dSBarry Smith       dx  = xx[col];
20515b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2052ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2053ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2054ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2055ee4f033dSBarry Smith #else
2056ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2057ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2058ee4f033dSBarry Smith #endif
2059ee4f033dSBarry Smith       dx            *= epsilon;
2060634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2061ee4f033dSBarry Smith       w3_array[col] += dx;
2062ee4f033dSBarry Smith     }
20631ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2064ee4f033dSBarry Smith 
2065ee4f033dSBarry Smith     /*
2066ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2067ee4f033dSBarry Smith     */
2068ee4f033dSBarry Smith 
206966f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2070ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
207166f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2072ee4f033dSBarry Smith     ierr = VecAXPY(&mone,w1,w2);CHKERRQ(ierr);
2073ee4f033dSBarry Smith 
2074ee4f033dSBarry Smith     /*
2075ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2076ee4f033dSBarry Smith     */
20771ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2078ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2079ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2080ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2081ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2082ee4f033dSBarry Smith       srow   = row + start;
2083ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2084ee4f033dSBarry Smith     }
20851ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2086ee4f033dSBarry Smith   }
208749b058dcSBarry Smith   coloring->currentcolor = k;
20881ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
20891ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2090ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2091ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2092ee4f033dSBarry Smith   PetscFunctionReturn(0);
2093ee4f033dSBarry Smith }
2094ee4f033dSBarry Smith 
2095ac90fabeSBarry Smith #include "petscblaslapack.h"
2096ac90fabeSBarry Smith #undef __FUNCT__
2097ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2098dfbe8321SBarry Smith PetscErrorCode MatAXPY_SeqAIJ(const PetscScalar a[],Mat X,Mat Y,MatStructure str)
2099ac90fabeSBarry Smith {
2100dfbe8321SBarry Smith   PetscErrorCode ierr;
210197f1f81fSBarry Smith   PetscInt       i;
2102ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
21034ce68768SBarry Smith   PetscBLASInt   one=1,bnz = (PetscBLASInt)x->nz;
2104ac90fabeSBarry Smith 
2105ac90fabeSBarry Smith   PetscFunctionBegin;
2106ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
210771044d3cSBarry Smith     BLASaxpy_(&bnz,(PetscScalar*)a,x->a,&one,y->a,&one);
2108c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2109a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2110a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2111a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2112a30b2313SHong Zhang     }
2113a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
211424f910e3SHong Zhang       ierr = MatAXPYGetxtoy_Private(X->m,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2115a30b2313SHong Zhang       y->XtoY = X;
2116c537a176SHong Zhang     }
2117a30b2313SHong Zhang     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += (*a)*(x->a[i]);
2118e2dd4fc4Svictorle     PetscLogInfo(0,"MatAXPY_SeqAIJ: ratio of nnz(X)/nnz(Y): %d/%d = %g\n",x->nz,y->nz,(PetscReal)(x->nz)/y->nz);
2119ac90fabeSBarry Smith   } else {
2120ac90fabeSBarry Smith     ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr);
2121ac90fabeSBarry Smith   }
2122ac90fabeSBarry Smith   PetscFunctionReturn(0);
2123ac90fabeSBarry Smith }
2124ac90fabeSBarry Smith 
2125521d7252SBarry Smith #undef __FUNCT__
2126521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2127521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2128521d7252SBarry Smith {
2129521d7252SBarry Smith   PetscFunctionBegin;
2130521d7252SBarry Smith   PetscFunctionReturn(0);
2131521d7252SBarry Smith }
2132521d7252SBarry Smith 
2133682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
21340a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2135cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2136cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2137cb5b572fSBarry Smith        MatMult_SeqAIJ,
213897304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
21397c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
21407c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2141cb5b572fSBarry Smith        MatSolve_SeqAIJ,
2142cb5b572fSBarry Smith        MatSolveAdd_SeqAIJ,
21437c922b88SBarry Smith        MatSolveTranspose_SeqAIJ,
214497304618SKris Buschelman /*10*/ MatSolveTransposeAdd_SeqAIJ,
2145cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2146cb5b572fSBarry Smith        0,
214717ab2063SBarry Smith        MatRelax_SeqAIJ,
214817ab2063SBarry Smith        MatTranspose_SeqAIJ,
214997304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2150cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2151cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2152cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2153cb5b572fSBarry Smith        MatNorm_SeqAIJ,
215497304618SKris Buschelman /*20*/ 0,
2155cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
215617ab2063SBarry Smith        MatCompress_SeqAIJ,
2157cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2158cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
215997304618SKris Buschelman /*25*/ MatZeroRows_SeqAIJ,
2160cb5b572fSBarry Smith        MatLUFactorSymbolic_SeqAIJ,
2161cb5b572fSBarry Smith        MatLUFactorNumeric_SeqAIJ,
2162f76d2b81SHong Zhang        MatCholeskyFactorSymbolic_SeqAIJ,
2163a6175056SHong Zhang        MatCholeskyFactorNumeric_SeqAIJ,
216497304618SKris Buschelman /*30*/ MatSetUpPreallocation_SeqAIJ,
2165cb5b572fSBarry Smith        MatILUFactorSymbolic_SeqAIJ,
2166861ba921SHong Zhang        MatICCFactorSymbolic_SeqAIJ,
21676c0721eeSBarry Smith        MatGetArray_SeqAIJ,
21686c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
216997304618SKris Buschelman /*35*/ MatDuplicate_SeqAIJ,
2170cb5b572fSBarry Smith        0,
2171cb5b572fSBarry Smith        0,
2172cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2173cb5b572fSBarry Smith        0,
217497304618SKris Buschelman /*40*/ MatAXPY_SeqAIJ,
2175cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2176cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2177cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2178cb5b572fSBarry Smith        MatCopy_SeqAIJ,
217997304618SKris Buschelman /*45*/ MatPrintHelp_SeqAIJ,
2180cb5b572fSBarry Smith        MatScale_SeqAIJ,
2181cb5b572fSBarry Smith        0,
2182cb5b572fSBarry Smith        0,
21836945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2184521d7252SBarry Smith /*50*/ MatSetBlockSize_SeqAIJ,
21853b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
21863b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
21873b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2188a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
218997304618SKris Buschelman /*55*/ MatFDColoringCreate_SeqAIJ,
2190b9617806SBarry Smith        0,
21910513a670SBarry Smith        0,
2192cda55fadSBarry Smith        MatPermute_SeqAIJ,
2193cda55fadSBarry Smith        0,
219497304618SKris Buschelman /*60*/ 0,
2195b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2196b9b97703SBarry Smith        MatView_SeqAIJ,
21978a124369SBarry Smith        MatGetPetscMaps_Petsc,
2198ee4f033dSBarry Smith        0,
219997304618SKris Buschelman /*65*/ 0,
2200ee4f033dSBarry Smith        0,
2201ee4f033dSBarry Smith        0,
2202ee4f033dSBarry Smith        0,
2203ee4f033dSBarry Smith        0,
220497304618SKris Buschelman /*70*/ 0,
2205ee4f033dSBarry Smith        0,
2206ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2207dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2208ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2209dcf5cc72SBarry Smith #else
2210dcf5cc72SBarry Smith        0,
2211dcf5cc72SBarry Smith #endif
2212ee4f033dSBarry Smith        MatSetValuesAdifor_SeqAIJ,
221397304618SKris Buschelman /*75*/ MatFDColoringApply_SeqAIJ,
221497304618SKris Buschelman        0,
221597304618SKris Buschelman        0,
221697304618SKris Buschelman        0,
221797304618SKris Buschelman        0,
221897304618SKris Buschelman /*80*/ 0,
221997304618SKris Buschelman        0,
222097304618SKris Buschelman        0,
222197304618SKris Buschelman        0,
2222bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2223bc011b1eSHong Zhang /*85*/ MatIsSymmetric_SeqAIJ,
22246284ec50SHong Zhang        0,
22256284ec50SHong Zhang        0,
22266284ec50SHong Zhang        0,
2227bc011b1eSHong Zhang        0,
2228bc011b1eSHong Zhang /*90*/ MatMatMult_SeqAIJ_SeqAIJ,
222926be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
223026be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2231d439da42SKris Buschelman        MatPtAP_Basic,
22327ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
22337ba1a0bfSKris Buschelman /*95*/ MatPtAPNumeric_SeqAIJ,
2234bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2235bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2236bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
22377ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
22387ba1a0bfSKris Buschelman /*100*/MatPtAPNumeric_SeqAIJ_SeqAIJ,
2239609c6c4dSKris Buschelman        0,
2240609c6c4dSKris Buschelman        0,
22419e29f15eSvictorle };
224217ab2063SBarry Smith 
2243fb2e594dSBarry Smith EXTERN_C_BEGIN
22444a2ae208SSatish Balay #undef __FUNCT__
22454a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
224697f1f81fSBarry Smith PetscErrorCode MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2247bef8e0ddSBarry Smith {
2248bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
224997f1f81fSBarry Smith   PetscInt   i,nz,n;
2250bef8e0ddSBarry Smith 
2251bef8e0ddSBarry Smith   PetscFunctionBegin;
2252bef8e0ddSBarry Smith 
2253bef8e0ddSBarry Smith   nz = aij->maxnz;
2254273d9f13SBarry Smith   n  = mat->n;
2255bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2256bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2257bef8e0ddSBarry Smith   }
2258bef8e0ddSBarry Smith   aij->nz = nz;
2259bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2260bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2261bef8e0ddSBarry Smith   }
2262bef8e0ddSBarry Smith 
2263bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2264bef8e0ddSBarry Smith }
2265fb2e594dSBarry Smith EXTERN_C_END
2266bef8e0ddSBarry Smith 
22674a2ae208SSatish Balay #undef __FUNCT__
22684a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2269bef8e0ddSBarry Smith /*@
2270bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2271bef8e0ddSBarry Smith        in the matrix.
2272bef8e0ddSBarry Smith 
2273bef8e0ddSBarry Smith   Input Parameters:
2274bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2275bef8e0ddSBarry Smith -  indices - the column indices
2276bef8e0ddSBarry Smith 
227715091d37SBarry Smith   Level: advanced
227815091d37SBarry Smith 
2279bef8e0ddSBarry Smith   Notes:
2280bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2281bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2282bef8e0ddSBarry Smith   of the MatSetValues() operation.
2283bef8e0ddSBarry Smith 
2284bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2285d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2286bef8e0ddSBarry Smith 
2287bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2288bef8e0ddSBarry Smith 
2289b9617806SBarry Smith     The indices should start with zero, not one.
2290b9617806SBarry Smith 
2291bef8e0ddSBarry Smith @*/
229297f1f81fSBarry Smith PetscErrorCode MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2293bef8e0ddSBarry Smith {
229497f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2295bef8e0ddSBarry Smith 
2296bef8e0ddSBarry Smith   PetscFunctionBegin;
22974482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
22984482741eSBarry Smith   PetscValidPointer(indices,2);
2299c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2300bef8e0ddSBarry Smith   if (f) {
2301bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2302bef8e0ddSBarry Smith   } else {
2303634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2304bef8e0ddSBarry Smith   }
2305bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2306bef8e0ddSBarry Smith }
2307bef8e0ddSBarry Smith 
2308be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2309be6bf707SBarry Smith 
2310fb2e594dSBarry Smith EXTERN_C_BEGIN
23114a2ae208SSatish Balay #undef __FUNCT__
23124a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2313dfbe8321SBarry Smith PetscErrorCode MatStoreValues_SeqAIJ(Mat mat)
2314be6bf707SBarry Smith {
2315be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
23166849ba73SBarry Smith   PetscErrorCode ierr;
23176849ba73SBarry Smith   size_t         nz = aij->i[mat->m];
2318be6bf707SBarry Smith 
2319be6bf707SBarry Smith   PetscFunctionBegin;
2320be6bf707SBarry Smith   if (aij->nonew != 1) {
2321634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2322be6bf707SBarry Smith   }
2323be6bf707SBarry Smith 
2324be6bf707SBarry Smith   /* allocate space for values if not already there */
2325be6bf707SBarry Smith   if (!aij->saved_values) {
232687828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
2327be6bf707SBarry Smith   }
2328be6bf707SBarry Smith 
2329be6bf707SBarry Smith   /* copy values over */
233087828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2331be6bf707SBarry Smith   PetscFunctionReturn(0);
2332be6bf707SBarry Smith }
2333fb2e594dSBarry Smith EXTERN_C_END
2334be6bf707SBarry Smith 
23354a2ae208SSatish Balay #undef __FUNCT__
2336b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2337be6bf707SBarry Smith /*@
2338be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2339be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2340be6bf707SBarry Smith        nonlinear portion.
2341be6bf707SBarry Smith 
2342be6bf707SBarry Smith    Collect on Mat
2343be6bf707SBarry Smith 
2344be6bf707SBarry Smith   Input Parameters:
23450e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2346be6bf707SBarry Smith 
234715091d37SBarry Smith   Level: advanced
234815091d37SBarry Smith 
2349be6bf707SBarry Smith   Common Usage, with SNESSolve():
2350be6bf707SBarry Smith $    Create Jacobian matrix
2351be6bf707SBarry Smith $    Set linear terms into matrix
2352be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2353be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2354be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2355be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2356be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2357be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2358be6bf707SBarry Smith $    In your Jacobian routine
2359be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2360be6bf707SBarry Smith $      Set nonlinear terms in matrix
2361be6bf707SBarry Smith 
2362be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2363be6bf707SBarry Smith $    // build linear portion of Jacobian
2364be6bf707SBarry Smith $    ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS);
2365be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2366be6bf707SBarry Smith $    loop over nonlinear iterations
2367be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2368be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2369be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2370be6bf707SBarry Smith $       Solve linear system with Jacobian
2371be6bf707SBarry Smith $    endloop
2372be6bf707SBarry Smith 
2373be6bf707SBarry Smith   Notes:
2374be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2375be6bf707SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before
2376be6bf707SBarry Smith     calling this routine.
2377be6bf707SBarry Smith 
2378be6bf707SBarry Smith .seealso: MatRetrieveValues()
2379be6bf707SBarry Smith 
2380be6bf707SBarry Smith @*/
2381dfbe8321SBarry Smith PetscErrorCode MatStoreValues(Mat mat)
2382be6bf707SBarry Smith {
2383dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2384be6bf707SBarry Smith 
2385be6bf707SBarry Smith   PetscFunctionBegin;
23864482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
238729bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
238829bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2389be6bf707SBarry Smith 
2390c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2391be6bf707SBarry Smith   if (f) {
2392be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2393be6bf707SBarry Smith   } else {
2394634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2395be6bf707SBarry Smith   }
2396be6bf707SBarry Smith   PetscFunctionReturn(0);
2397be6bf707SBarry Smith }
2398be6bf707SBarry Smith 
2399fb2e594dSBarry Smith EXTERN_C_BEGIN
24004a2ae208SSatish Balay #undef __FUNCT__
24014a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2402dfbe8321SBarry Smith PetscErrorCode MatRetrieveValues_SeqAIJ(Mat mat)
2403be6bf707SBarry Smith {
2404be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
24056849ba73SBarry Smith   PetscErrorCode ierr;
240697f1f81fSBarry Smith   PetscInt       nz = aij->i[mat->m];
2407be6bf707SBarry Smith 
2408be6bf707SBarry Smith   PetscFunctionBegin;
2409be6bf707SBarry Smith   if (aij->nonew != 1) {
2410634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first");
2411be6bf707SBarry Smith   }
2412be6bf707SBarry Smith   if (!aij->saved_values) {
2413634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2414be6bf707SBarry Smith   }
2415be6bf707SBarry Smith   /* copy values over */
241687828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2417be6bf707SBarry Smith   PetscFunctionReturn(0);
2418be6bf707SBarry Smith }
2419fb2e594dSBarry Smith EXTERN_C_END
2420be6bf707SBarry Smith 
24214a2ae208SSatish Balay #undef __FUNCT__
24224a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2423be6bf707SBarry Smith /*@
2424be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2425be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2426be6bf707SBarry Smith        nonlinear portion.
2427be6bf707SBarry Smith 
2428be6bf707SBarry Smith    Collect on Mat
2429be6bf707SBarry Smith 
2430be6bf707SBarry Smith   Input Parameters:
2431be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2432be6bf707SBarry Smith 
243315091d37SBarry Smith   Level: advanced
243415091d37SBarry Smith 
2435be6bf707SBarry Smith .seealso: MatStoreValues()
2436be6bf707SBarry Smith 
2437be6bf707SBarry Smith @*/
2438dfbe8321SBarry Smith PetscErrorCode MatRetrieveValues(Mat mat)
2439be6bf707SBarry Smith {
2440dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2441be6bf707SBarry Smith 
2442be6bf707SBarry Smith   PetscFunctionBegin;
24434482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
244429bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
244529bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2446be6bf707SBarry Smith 
2447c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2448be6bf707SBarry Smith   if (f) {
2449be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2450be6bf707SBarry Smith   } else {
2451634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2452be6bf707SBarry Smith   }
2453be6bf707SBarry Smith   PetscFunctionReturn(0);
2454be6bf707SBarry Smith }
2455be6bf707SBarry Smith 
2456f83d6046SBarry Smith 
2457be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
24584a2ae208SSatish Balay #undef __FUNCT__
24594a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
246017ab2063SBarry Smith /*@C
2461682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
24620d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
24636e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
246451c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
24652bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
246617ab2063SBarry Smith 
2467db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2468db81eaa0SLois Curfman McInnes 
246917ab2063SBarry Smith    Input Parameters:
2470db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
247117ab2063SBarry Smith .  m - number of rows
247217ab2063SBarry Smith .  n - number of columns
247317ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
247451c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
24752bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
247617ab2063SBarry Smith 
247717ab2063SBarry Smith    Output Parameter:
2478416022c9SBarry Smith .  A - the matrix
247917ab2063SBarry Smith 
2480b259b22eSLois Curfman McInnes    Notes:
248149a6f317SBarry Smith    If nnz is given then nz is ignored
248249a6f317SBarry Smith 
248317ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
248417ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
24850002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
248644cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
248717ab2063SBarry Smith 
248817ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2489a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
24903d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
24916da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
249217ab2063SBarry Smith 
2493682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
24944fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2495682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
24966c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
24976c7ebb05SLois Curfman McInnes 
24986c7ebb05SLois Curfman McInnes    Options Database Keys:
2499db81eaa0SLois Curfman McInnes +  -mat_aij_no_inode  - Do not use inodes
2500db81eaa0SLois Curfman McInnes .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2501db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2502db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2503db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
250417ab2063SBarry Smith 
2505027ccd11SLois Curfman McInnes    Level: intermediate
2506027ccd11SLois Curfman McInnes 
250736db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
250836db0b34SBarry Smith 
250917ab2063SBarry Smith @*/
251097f1f81fSBarry Smith PetscErrorCode MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
251117ab2063SBarry Smith {
2512dfbe8321SBarry Smith   PetscErrorCode ierr;
25136945ee14SBarry Smith 
25143a40ed3dSBarry Smith   PetscFunctionBegin;
2515273d9f13SBarry Smith   ierr = MatCreate(comm,m,n,m,n,A);CHKERRQ(ierr);
2516273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2517273d9f13SBarry Smith   ierr = MatSeqAIJSetPreallocation(*A,nz,nnz);CHKERRQ(ierr);
2518273d9f13SBarry Smith   PetscFunctionReturn(0);
2519273d9f13SBarry Smith }
2520273d9f13SBarry Smith 
25215da197adSKris Buschelman #define SKIP_ALLOCATION -4
25225da197adSKris Buschelman 
25234a2ae208SSatish Balay #undef __FUNCT__
25244a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2525273d9f13SBarry Smith /*@C
2526273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2527273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2528273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2529273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2530273d9f13SBarry Smith 
2531273d9f13SBarry Smith    Collective on MPI_Comm
2532273d9f13SBarry Smith 
2533273d9f13SBarry Smith    Input Parameters:
2534273d9f13SBarry Smith +  comm - MPI communicator, set to PETSC_COMM_SELF
2535273d9f13SBarry Smith .  m - number of rows
2536273d9f13SBarry Smith .  n - number of columns
2537273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2538273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2539273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2540273d9f13SBarry Smith 
2541273d9f13SBarry Smith    Output Parameter:
2542273d9f13SBarry Smith .  A - the matrix
2543273d9f13SBarry Smith 
2544273d9f13SBarry Smith    Notes:
254549a6f317SBarry Smith      If nnz is given then nz is ignored
254649a6f317SBarry Smith 
2547273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2548273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2549273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2550273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2551273d9f13SBarry Smith 
2552273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2553273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2554273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2555273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2556273d9f13SBarry Smith 
2557273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2558273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2559273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2560273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2561273d9f13SBarry Smith 
2562273d9f13SBarry Smith    Options Database Keys:
2563273d9f13SBarry Smith +  -mat_aij_no_inode  - Do not use inodes
2564273d9f13SBarry Smith .  -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5)
2565273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2566273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2567273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2568273d9f13SBarry Smith 
2569273d9f13SBarry Smith    Level: intermediate
2570273d9f13SBarry Smith 
2571273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
2572273d9f13SBarry Smith 
2573273d9f13SBarry Smith @*/
257497f1f81fSBarry Smith PetscErrorCode MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2575273d9f13SBarry Smith {
257697f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2577a23d5eceSKris Buschelman 
2578a23d5eceSKris Buschelman   PetscFunctionBegin;
2579a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2580a23d5eceSKris Buschelman   if (f) {
2581a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2582a23d5eceSKris Buschelman   }
2583a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2584a23d5eceSKris Buschelman }
2585a23d5eceSKris Buschelman 
2586a23d5eceSKris Buschelman EXTERN_C_BEGIN
2587a23d5eceSKris Buschelman #undef __FUNCT__
2588a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
258997f1f81fSBarry Smith PetscErrorCode MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2590a23d5eceSKris Buschelman {
2591273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2592a7ed9263SMatthew Knepley   size_t         len = 0;
2593a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
25946849ba73SBarry Smith   PetscErrorCode ierr;
259597f1f81fSBarry Smith   PetscInt       i;
2596273d9f13SBarry Smith 
2597273d9f13SBarry Smith   PetscFunctionBegin;
2598d5d45c9bSBarry Smith 
2599c461c341SBarry Smith   if (nz == SKIP_ALLOCATION) {
2600c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2601c461c341SBarry Smith     nz             = 0;
2602c461c341SBarry Smith   }
2603c461c341SBarry Smith 
2604435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2605435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2606b73539f3SBarry Smith   if (nnz) {
2607273d9f13SBarry Smith     for (i=0; i<B->m; i++) {
260829bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
26093a7fca6bSBarry 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);
2610b73539f3SBarry Smith     }
2611b73539f3SBarry Smith   }
2612b73539f3SBarry Smith 
2613273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2614273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2615273d9f13SBarry Smith 
261697f1f81fSBarry Smith   ierr = PetscMalloc((B->m+1)*sizeof(PetscInt),&b->imax);CHKERRQ(ierr);
2617273d9f13SBarry Smith   if (!nnz) {
2618435da068SBarry Smith     if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2619273d9f13SBarry Smith     else if (nz <= 0)        nz = 1;
2620273d9f13SBarry Smith     for (i=0; i<B->m; i++) b->imax[i] = nz;
2621273d9f13SBarry Smith     nz = nz*B->m;
2622273d9f13SBarry Smith   } else {
2623273d9f13SBarry Smith     nz = 0;
2624273d9f13SBarry Smith     for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2625273d9f13SBarry Smith   }
2626273d9f13SBarry Smith 
2627c461c341SBarry Smith   if (!skipallocation) {
2628273d9f13SBarry Smith     /* allocate the matrix space */
262997f1f81fSBarry Smith     len             = ((size_t) nz)*(sizeof(PetscInt) + sizeof(PetscScalar)) + (B->m+1)*sizeof(PetscInt);
2630b0a32e0cSBarry Smith     ierr            = PetscMalloc(len,&b->a);CHKERRQ(ierr);
263197f1f81fSBarry Smith     b->j            = (PetscInt*)(b->a + nz);
263297f1f81fSBarry Smith     ierr            = PetscMemzero(b->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
2633273d9f13SBarry Smith     b->i            = b->j + nz;
2634bfeeae90SHong Zhang     b->i[0] = 0;
26355da197adSKris Buschelman     for (i=1; i<B->m+1; i++) {
26365da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
26375da197adSKris Buschelman     }
2638273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2639273d9f13SBarry Smith     b->freedata     = PETSC_TRUE;
2640c461c341SBarry Smith   } else {
2641c461c341SBarry Smith     b->freedata     = PETSC_FALSE;
2642c461c341SBarry Smith   }
2643273d9f13SBarry Smith 
2644273d9f13SBarry Smith   /* b->ilen will count nonzeros in each row so far. */
264597f1f81fSBarry Smith   ierr = PetscMalloc((B->m+1)*sizeof(PetscInt),&b->ilen);CHKERRQ(ierr);
264652e6d16bSBarry Smith   ierr = PetscLogObjectMemory(B,len+2*(B->m+1)*sizeof(PetscInt)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));CHKERRQ(ierr);
2647273d9f13SBarry Smith   for (i=0; i<B->m; i++) { b->ilen[i] = 0;}
2648273d9f13SBarry Smith 
2649273d9f13SBarry Smith   b->nz                = 0;
2650273d9f13SBarry Smith   b->maxnz             = nz;
2651273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
2652273d9f13SBarry Smith   PetscFunctionReturn(0);
2653273d9f13SBarry Smith }
2654a23d5eceSKris Buschelman EXTERN_C_END
2655273d9f13SBarry Smith 
26560bad9183SKris Buschelman /*MC
2657fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
26580bad9183SKris Buschelman    based on compressed sparse row format.
26590bad9183SKris Buschelman 
26600bad9183SKris Buschelman    Options Database Keys:
26610bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
26620bad9183SKris Buschelman 
26630bad9183SKris Buschelman   Level: beginner
26640bad9183SKris Buschelman 
2665f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
26660bad9183SKris Buschelman M*/
26670bad9183SKris Buschelman 
2668a6175056SHong Zhang EXTERN_C_BEGIN
26694a2ae208SSatish Balay #undef __FUNCT__
26704a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
2671dfbe8321SBarry Smith PetscErrorCode MatCreate_SeqAIJ(Mat B)
2672273d9f13SBarry Smith {
2673273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2674dfbe8321SBarry Smith   PetscErrorCode ierr;
267538baddfdSBarry Smith   PetscMPIInt    size;
2676273d9f13SBarry Smith 
2677273d9f13SBarry Smith   PetscFunctionBegin;
2678273d9f13SBarry Smith   ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr);
2679273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
2680273d9f13SBarry Smith 
2681*71c2f376SKris Buschelman   /* Call parent MatCreate -- i.e., ChangeTypeName(SeqAIJ)&MatSetType(parent) */
2682273d9f13SBarry Smith   B->m = B->M = PetscMax(B->m,B->M);
2683273d9f13SBarry Smith   B->n = B->N = PetscMax(B->n,B->N);
2684273d9f13SBarry Smith 
2685b0a32e0cSBarry Smith   ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr);
2686b0a32e0cSBarry Smith   B->data             = (void*)b;
2687549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2688416022c9SBarry Smith   B->factor           = 0;
2689416022c9SBarry Smith   B->lupivotthreshold = 1.0;
269090f02eecSBarry Smith   B->mapping          = 0;
2691e82a3eeeSBarry Smith   ierr = PetscOptionsGetReal(B->prefix,"-mat_lu_pivotthreshold",&B->lupivotthreshold,PETSC_NULL);CHKERRQ(ierr);
2692e82a3eeeSBarry Smith   ierr = PetscOptionsHasName(B->prefix,"-pc_ilu_preserve_row_sums",&b->ilu_preserve_row_sums);CHKERRQ(ierr);
2693416022c9SBarry Smith   b->row              = 0;
2694416022c9SBarry Smith   b->col              = 0;
269582bf6240SBarry Smith   b->icol             = 0;
2696b810aeb4SBarry Smith   b->reallocs         = 0;
2697922d95a3SBarry Smith   b->lu_shift         = PETSC_FALSE;
269817ab2063SBarry Smith 
26998a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr);
27008a124369SBarry Smith   ierr = PetscMapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr);
2701a5ae1ecdSBarry Smith 
2702f1e2ffcdSBarry Smith   b->sorted            = PETSC_FALSE;
270336db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
2704f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
2705416022c9SBarry Smith   b->nonew             = 0;
2706416022c9SBarry Smith   b->diag              = 0;
2707416022c9SBarry Smith   b->solve_work        = 0;
27082a1b7f2aSHong Zhang   B->spptr             = 0;
2709be6bf707SBarry Smith   b->saved_values      = 0;
2710d7f994e1SBarry Smith   b->idiag             = 0;
2711d7f994e1SBarry Smith   b->ssor              = 0;
2712f1e2ffcdSBarry Smith   b->keepzeroedrows    = PETSC_FALSE;
2713a30b2313SHong Zhang   b->xtoy              = 0;
2714a30b2313SHong Zhang   b->XtoY              = 0;
271573e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
2716d487561eSHong Zhang   b->compressedrow.nrows   = B->m;
2717d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
2718d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
2719d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
272088e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
272117ab2063SBarry Smith 
272235d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
272335d8aa7fSBarry Smith 
2724f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
2725bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
2726bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
2727f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
2728be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
2729bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
2730f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
2731be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
2732bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
2733b24ad042SBarry Smith #if !defined(PETSC_USE_64BIT_INT)
2734a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
2735a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
2736a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
2737b24ad042SBarry Smith #endif
273885fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
273985fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
274085fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
27415fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
27425fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
27435fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
2744a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
2745a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
2746a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
274705b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
274805b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
274905b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
27503a40ed3dSBarry Smith   PetscFunctionReturn(0);
275117ab2063SBarry Smith }
2752273d9f13SBarry Smith EXTERN_C_END
275317ab2063SBarry Smith 
27544a2ae208SSatish Balay #undef __FUNCT__
27554a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ"
2756dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
275717ab2063SBarry Smith {
2758*71c2f376SKris Buschelman   /* Not sure where to call parent MatDuplicate ... end I guess */
2759416022c9SBarry Smith   Mat            C;
2760416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
27616849ba73SBarry Smith   PetscErrorCode ierr;
276297f1f81fSBarry Smith   PetscInt       i,m = A->m;
2763a7ed9263SMatthew Knepley   size_t         len;
276417ab2063SBarry Smith 
27653a40ed3dSBarry Smith   PetscFunctionBegin;
27664043dd9cSLois Curfman McInnes   *B = 0;
2767273d9f13SBarry Smith   ierr = MatCreate(A->comm,A->m,A->n,A->m,A->n,&C);CHKERRQ(ierr);
2768be5d1d56SKris Buschelman   ierr = MatSetType(C,A->type_name);CHKERRQ(ierr);
27691d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
27701d5dac46SHong Zhang 
2771273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
2772273d9f13SBarry Smith 
2773416022c9SBarry Smith   C->factor           = A->factor;
27746ad4291fSHong Zhang   C->lupivotthreshold = A->lupivotthreshold;
27756ad4291fSHong Zhang 
2776416022c9SBarry Smith   c->row            = 0;
2777416022c9SBarry Smith   c->col            = 0;
277882bf6240SBarry Smith   c->icol           = 0;
27796ad4291fSHong Zhang   c->reallocs       = 0;
27806ad4291fSHong Zhang   c->lu_shift       = PETSC_FALSE;
278117ab2063SBarry Smith 
27826ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
278317ab2063SBarry Smith 
278497f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->imax);CHKERRQ(ierr);
278597f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->ilen);CHKERRQ(ierr);
278617ab2063SBarry Smith   for (i=0; i<m; i++) {
2787416022c9SBarry Smith     c->imax[i] = a->imax[i];
2788416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
278917ab2063SBarry Smith   }
279017ab2063SBarry Smith 
279117ab2063SBarry Smith   /* allocate the matrix space */
2792f1e2ffcdSBarry Smith   c->singlemalloc = PETSC_TRUE;
279397f1f81fSBarry Smith   len   = ((size_t) (m+1))*sizeof(PetscInt)+(a->i[m])*(sizeof(PetscScalar)+sizeof(PetscInt));
2794b0a32e0cSBarry Smith   ierr  = PetscMalloc(len,&c->a);CHKERRQ(ierr);
279597f1f81fSBarry Smith   c->j  = (PetscInt*)(c->a + a->i[m] );
2796bfeeae90SHong Zhang   c->i  = c->j + a->i[m];
279797f1f81fSBarry Smith   ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
279817ab2063SBarry Smith   if (m > 0) {
279997f1f81fSBarry Smith     ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
2800be6bf707SBarry Smith     if (cpvalues == MAT_COPY_VALUES) {
2801bfeeae90SHong Zhang       ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
2802be6bf707SBarry Smith     } else {
2803bfeeae90SHong Zhang       ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
280417ab2063SBarry Smith     }
280508480c60SBarry Smith   }
280617ab2063SBarry Smith 
280752e6d16bSBarry Smith   ierr = PetscLogObjectMemory(C,len+2*(m+1)*sizeof(PetscInt)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ));CHKERRQ(ierr);
2808416022c9SBarry Smith   c->sorted            = a->sorted;
28096ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
2810416022c9SBarry Smith   c->roworiented       = a->roworiented;
2811416022c9SBarry Smith   c->nonew             = a->nonew;
2812416022c9SBarry Smith   if (a->diag) {
281397f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
281452e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
281517ab2063SBarry Smith     for (i=0; i<m; i++) {
2816416022c9SBarry Smith       c->diag[i] = a->diag[i];
281717ab2063SBarry Smith     }
28183a40ed3dSBarry Smith   } else c->diag        = 0;
28196ad4291fSHong Zhang   c->solve_work         = 0;
28206ad4291fSHong Zhang   c->saved_values          = 0;
28216ad4291fSHong Zhang   c->idiag                 = 0;
28226ad4291fSHong Zhang   c->ilu_preserve_row_sums = a->ilu_preserve_row_sums;
28236ad4291fSHong Zhang   c->ssor                  = 0;
28246ad4291fSHong Zhang   c->keepzeroedrows        = a->keepzeroedrows;
28256ad4291fSHong Zhang   c->freedata              = PETSC_TRUE;
28266ad4291fSHong Zhang   c->xtoy                  = 0;
28276ad4291fSHong Zhang   c->XtoY                  = 0;
28286ad4291fSHong Zhang 
2829416022c9SBarry Smith   c->nz                 = a->nz;
2830416022c9SBarry Smith   c->maxnz              = a->maxnz;
2831273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2832754ec7b1SSatish Balay 
28336ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
28346ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
28356ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
28366ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
28376ad4291fSHong Zhang     i = a->compressedrow.nrows;
28386ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
28396ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
28406ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
28416ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
284227ea64f8SHong Zhang   } else {
284327ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
284427ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
284527ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
28466ad4291fSHong Zhang   }
284788e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
28486ad4291fSHong Zhang 
2849416022c9SBarry Smith   *B = C;
2850b0a32e0cSBarry Smith   ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr);
28513a40ed3dSBarry Smith   PetscFunctionReturn(0);
285217ab2063SBarry Smith }
285317ab2063SBarry Smith 
28544a2ae208SSatish Balay #undef __FUNCT__
28554a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
2856dfbe8321SBarry Smith PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer,const MatType type,Mat *A)
285717ab2063SBarry Smith {
2858416022c9SBarry Smith   Mat_SeqAIJ     *a;
2859416022c9SBarry Smith   Mat            B;
28606849ba73SBarry Smith   PetscErrorCode ierr;
28613c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
286238baddfdSBarry Smith   int            fd;
286338baddfdSBarry Smith   PetscMPIInt    size;
2864bcd2baecSBarry Smith   MPI_Comm       comm;
286517ab2063SBarry Smith 
28663a40ed3dSBarry Smith   PetscFunctionBegin;
2867e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
2868d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
286929bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
2870b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
28710752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
2872552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
287317ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
287417ab2063SBarry Smith 
2875d64ed03dSBarry Smith   if (nz < 0) {
287629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
2877d64ed03dSBarry Smith   }
2878d64ed03dSBarry Smith 
287917ab2063SBarry Smith   /* read in row lengths */
288097f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
28810752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
288217ab2063SBarry Smith 
28833c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
28843c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
28853c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
28863c601197SSatish Balay 
288717ab2063SBarry Smith   /* create our matrix */
2888b3a1e11cSKris Buschelman   ierr = MatCreate(comm,PETSC_DECIDE,PETSC_DECIDE,M,N,&B);CHKERRQ(ierr);
2889b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
2890b3a1e11cSKris Buschelman   ierr = MatSeqAIJSetPreallocation(B,0,rowlengths);CHKERRQ(ierr);
2891416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
289217ab2063SBarry Smith 
289317ab2063SBarry Smith   /* read in column indices and adjust for Fortran indexing*/
28940752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
289517ab2063SBarry Smith 
289617ab2063SBarry Smith   /* read in nonzero values */
28970752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
289817ab2063SBarry Smith 
289917ab2063SBarry Smith   /* set matrix "i" values */
2900efb16452SHong Zhang   a->i[0] = 0;
290117ab2063SBarry Smith   for (i=1; i<= M; i++) {
2902416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
2903416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
290417ab2063SBarry Smith   }
2905606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
290617ab2063SBarry Smith 
29076d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
29086d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2909b3a1e11cSKris Buschelman   *A = B;
29103a40ed3dSBarry Smith   PetscFunctionReturn(0);
291117ab2063SBarry Smith }
291217ab2063SBarry Smith 
29134a2ae208SSatish Balay #undef __FUNCT__
2914b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
2915dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
29167264ac53SSatish Balay {
29177264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
2918dfbe8321SBarry Smith   PetscErrorCode ierr;
29197264ac53SSatish Balay 
29203a40ed3dSBarry Smith   PetscFunctionBegin;
2921bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
2922bfeeae90SHong Zhang   if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)) {
2923ca44d042SBarry Smith     *flg = PETSC_FALSE;
2924ca44d042SBarry Smith     PetscFunctionReturn(0);
2925bcd2baecSBarry Smith   }
29267264ac53SSatish Balay 
29277264ac53SSatish Balay   /* if the a->i are the same */
292897f1f81fSBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
2929abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
29307264ac53SSatish Balay 
29317264ac53SSatish Balay   /* if a->j are the same */
293297f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
2933abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
2934bcd2baecSBarry Smith 
2935bcd2baecSBarry Smith   /* if a->a are the same */
293687828ca2SBarry Smith   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
29370f5bd95cSBarry Smith 
29383a40ed3dSBarry Smith   PetscFunctionReturn(0);
29397264ac53SSatish Balay 
29407264ac53SSatish Balay }
294136db0b34SBarry Smith 
29424a2ae208SSatish Balay #undef __FUNCT__
29434a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
294436db0b34SBarry Smith /*@C
294536db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
294636db0b34SBarry Smith               provided by the user.
294736db0b34SBarry Smith 
294836db0b34SBarry Smith       Coolective on MPI_Comm
294936db0b34SBarry Smith 
295036db0b34SBarry Smith    Input Parameters:
295136db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
295236db0b34SBarry Smith .   m - number of rows
295336db0b34SBarry Smith .   n - number of columns
295436db0b34SBarry Smith .   i - row indices
295536db0b34SBarry Smith .   j - column indices
295636db0b34SBarry Smith -   a - matrix values
295736db0b34SBarry Smith 
295836db0b34SBarry Smith    Output Parameter:
295936db0b34SBarry Smith .   mat - the matrix
296036db0b34SBarry Smith 
296136db0b34SBarry Smith    Level: intermediate
296236db0b34SBarry Smith 
296336db0b34SBarry Smith    Notes:
29640551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
296536db0b34SBarry Smith     once the matrix is destroyed
296636db0b34SBarry Smith 
296736db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
296836db0b34SBarry Smith 
2969bfeeae90SHong Zhang        The i and j indices are 0 based
297036db0b34SBarry Smith 
297136db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ()
297236db0b34SBarry Smith 
297336db0b34SBarry Smith @*/
297497f1f81fSBarry Smith PetscErrorCode MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
297536db0b34SBarry Smith {
2976dfbe8321SBarry Smith   PetscErrorCode ierr;
297797f1f81fSBarry Smith   PetscInt       ii;
297836db0b34SBarry Smith   Mat_SeqAIJ     *aij;
297936db0b34SBarry Smith 
298036db0b34SBarry Smith   PetscFunctionBegin;
2981f204ca49SKris Buschelman   ierr = MatCreate(comm,m,n,m,n,mat);CHKERRQ(ierr);
2982f204ca49SKris Buschelman   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
2983f204ca49SKris Buschelman   ierr = MatSeqAIJSetPreallocation(*mat,SKIP_ALLOCATION,0);CHKERRQ(ierr);
298436db0b34SBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
298536db0b34SBarry Smith 
2986bfeeae90SHong Zhang   if (i[0] != 0) {
2987634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
298836db0b34SBarry Smith   }
298936db0b34SBarry Smith   aij->i = i;
299036db0b34SBarry Smith   aij->j = j;
299136db0b34SBarry Smith   aij->a = a;
299236db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
299336db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
299436db0b34SBarry Smith   aij->freedata     = PETSC_FALSE;
299536db0b34SBarry Smith 
299636db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
299736db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
29982515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
299979a5c55eSBarry Smith     if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
300036db0b34SBarry Smith #endif
300136db0b34SBarry Smith   }
30022515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
300336db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
300479a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
300579a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
300636db0b34SBarry Smith   }
300736db0b34SBarry Smith #endif
300836db0b34SBarry Smith 
3009b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3010b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
301136db0b34SBarry Smith   PetscFunctionReturn(0);
301236db0b34SBarry Smith }
301336db0b34SBarry Smith 
3014cc8ba8e1SBarry Smith #undef __FUNCT__
3015ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3016dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3017cc8ba8e1SBarry Smith {
3018dfbe8321SBarry Smith   PetscErrorCode ierr;
3019cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
302036db0b34SBarry Smith 
3021cc8ba8e1SBarry Smith   PetscFunctionBegin;
302212c595b3SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
3023cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3024cc8ba8e1SBarry Smith     a->coloring = coloring;
302512c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
302697f1f81fSBarry Smith     PetscInt             i,*larray;
302712c595b3SBarry Smith     ISColoring      ocoloring;
302808b6dcc0SBarry Smith     ISColoringValue *colors;
302912c595b3SBarry Smith 
303012c595b3SBarry Smith     /* set coloring for diagonal portion */
303197f1f81fSBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
303212c595b3SBarry Smith     for (i=0; i<A->n; i++) {
303312c595b3SBarry Smith       larray[i] = i;
303412c595b3SBarry Smith     }
303512c595b3SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
303608b6dcc0SBarry Smith     ierr = PetscMalloc((A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
303712c595b3SBarry Smith     for (i=0; i<A->n; i++) {
303812c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
303912c595b3SBarry Smith     }
304012c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
304112c595b3SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,A->n,colors,&ocoloring);CHKERRQ(ierr);
304212c595b3SBarry Smith     a->coloring = ocoloring;
304312c595b3SBarry Smith   }
3044cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3045cc8ba8e1SBarry Smith }
3046cc8ba8e1SBarry Smith 
3047dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3048ee4f033dSBarry Smith EXTERN_C_BEGIN
304929c1e371SBarry Smith #include "adic/ad_utils.h"
3050ee4f033dSBarry Smith EXTERN_C_END
3051cc8ba8e1SBarry Smith 
3052cc8ba8e1SBarry Smith #undef __FUNCT__
3053ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3054dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3055cc8ba8e1SBarry Smith {
3056cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
305797f1f81fSBarry Smith   PetscInt        m = A->m,*ii = a->i,*jj = a->j,nz,i,j,nlen;
30584440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
305908b6dcc0SBarry Smith   ISColoringValue *color;
3060cc8ba8e1SBarry Smith 
3061cc8ba8e1SBarry Smith   PetscFunctionBegin;
3062e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
30634440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3064cc8ba8e1SBarry Smith   color = a->coloring->colors;
3065cc8ba8e1SBarry Smith   /* loop over rows */
3066cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3067cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3068cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3069cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3070cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3071cc8ba8e1SBarry Smith     }
30724440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3073ee4f033dSBarry Smith   }
3074ee4f033dSBarry Smith   PetscFunctionReturn(0);
3075ee4f033dSBarry Smith }
3076ee4f033dSBarry Smith #endif
3077ee4f033dSBarry Smith 
3078ee4f033dSBarry Smith #undef __FUNCT__
3079ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
308097f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3081ee4f033dSBarry Smith {
3082ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
308397f1f81fSBarry Smith   PetscInt             m = A->m,*ii = a->i,*jj = a->j,nz,i,j;
308487828ca2SBarry Smith   PetscScalar     *v = a->a,*values = (PetscScalar *)advalues;
308508b6dcc0SBarry Smith   ISColoringValue *color;
3086ee4f033dSBarry Smith 
3087ee4f033dSBarry Smith   PetscFunctionBegin;
3088e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3089ee4f033dSBarry Smith   color = a->coloring->colors;
3090ee4f033dSBarry Smith   /* loop over rows */
3091ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3092ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3093ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3094ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3095ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3096ee4f033dSBarry Smith     }
3097ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3098cc8ba8e1SBarry Smith   }
3099cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3100cc8ba8e1SBarry Smith }
310136db0b34SBarry Smith 
3102