xref: /petsc/src/mat/impls/baij/seq/baij.c (revision f501eaab198d947dfd3560039ee07ae4722dbc46)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
2be1d678aSKris Buschelman 
32593348eSBarry Smith /*
4b6490206SBarry Smith     Defines the basic matrix operations for the BAIJ (compressed row)
52593348eSBarry Smith   matrix storage format.
62593348eSBarry Smith */
7d382aafbSBarry Smith #include "../src/mat/impls/baij/seq/baij.h"  /*I   "petscmat.h"  I*/
8f3da1532SBarry Smith #include "petscblaslapack.h"
9c60f0209SBarry Smith #include "../src/mat/blockinvert.h"
10b01c7715SBarry Smith 
11b01c7715SBarry Smith #undef __FUNCT__
1243516a2dSKris Buschelman #define __FUNCT__ "MatSeqBAIJInvertBlockDiagonal"
13bc08b0f1SBarry Smith /*@
1443516a2dSKris Buschelman   MatSeqBAIJInvertBlockDiagonal - Inverts the block diagonal entries.
1543516a2dSKris Buschelman 
1643516a2dSKris Buschelman   Collective on Mat
1743516a2dSKris Buschelman 
1843516a2dSKris Buschelman   Input Parameters:
1943516a2dSKris Buschelman . mat - the matrix
2043516a2dSKris Buschelman 
2143516a2dSKris Buschelman   Level: advanced
2243516a2dSKris Buschelman @*/
2346129b97SKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJInvertBlockDiagonal(Mat mat)
2443516a2dSKris Buschelman {
2543516a2dSKris Buschelman   PetscErrorCode ierr,(*f)(Mat);
2643516a2dSKris Buschelman 
2743516a2dSKris Buschelman   PetscFunctionBegin;
280700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
29e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
30e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3143516a2dSKris Buschelman 
3243516a2dSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqBAIJInvertBlockDiagonal_C",(void (**)(void))&f);CHKERRQ(ierr);
3343516a2dSKris Buschelman   if (f) {
3443516a2dSKris Buschelman     ierr = (*f)(mat);CHKERRQ(ierr);
3543516a2dSKris Buschelman   } else {
36e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Currently only implemented for SeqBAIJ.");
3743516a2dSKris Buschelman   }
3843516a2dSKris Buschelman   PetscFunctionReturn(0);
3943516a2dSKris Buschelman }
4043516a2dSKris Buschelman 
4143516a2dSKris Buschelman EXTERN_C_BEGIN
4243516a2dSKris Buschelman #undef __FUNCT__
43b01c7715SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqBAIJ"
4446129b97SKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatInvertBlockDiagonal_SeqBAIJ(Mat A)
45b01c7715SBarry Smith {
46b01c7715SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*) A->data;
476849ba73SBarry Smith   PetscErrorCode ierr;
48c0c53c30SBarry Smith   PetscInt       *diag_offset,i,bs = A->rmap->bs,mbs = a->mbs,ipvt[5];
49c0c53c30SBarry Smith   MatScalar      *v = a->a,*odiag,*diag,*mdiag,work[25];
5062bba022SBarry Smith   PetscReal      shift = 0.0;
51b01c7715SBarry Smith 
52b01c7715SBarry Smith   PetscFunctionBegin;
53b01c7715SBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
54b01c7715SBarry Smith   ierr = MatMarkDiagonal_SeqBAIJ(A);CHKERRQ(ierr);
55b01c7715SBarry Smith   diag_offset = a->diag;
56b01c7715SBarry Smith   if (!a->idiag) {
57b01c7715SBarry Smith     ierr = PetscMalloc(2*bs*bs*mbs*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr);
581784c0f5SBarry Smith     ierr = PetscLogObjectMemory(A,2*bs*bs*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
59b01c7715SBarry Smith   }
60b01c7715SBarry Smith   diag  = a->idiag;
61b01c7715SBarry Smith   mdiag = a->idiag+bs*bs*mbs;
62b01c7715SBarry Smith   /* factor and invert each block */
63521d7252SBarry Smith   switch (bs){
64ab040260SJed Brown     case 1:
65ab040260SJed Brown       for (i=0; i<mbs; i++) {
66ab040260SJed Brown         odiag = v + 1*diag_offset[i];
67ab040260SJed Brown         diag[0]  = odiag[0];
68ab040260SJed Brown         mdiag[0] = odiag[0];
69ab040260SJed Brown         diag[0]  = 1.0 / (diag[0] + shift);
70ab040260SJed Brown         diag    += 1;
71ab040260SJed Brown         mdiag   += 1;
72ab040260SJed Brown       }
73ab040260SJed Brown       break;
74b01c7715SBarry Smith     case 2:
75b01c7715SBarry Smith       for (i=0; i<mbs; i++) {
76b01c7715SBarry Smith         odiag   = v + 4*diag_offset[i];
77b01c7715SBarry Smith         diag[0]  = odiag[0]; diag[1] = odiag[1]; diag[2] = odiag[2]; diag[3] = odiag[3];
78b01c7715SBarry Smith 	mdiag[0] = odiag[0]; mdiag[1] = odiag[1]; mdiag[2] = odiag[2]; mdiag[3] = odiag[3];
7962bba022SBarry Smith 	ierr     = Kernel_A_gets_inverse_A_2(diag,shift);CHKERRQ(ierr);
80b01c7715SBarry Smith 	diag    += 4;
81b01c7715SBarry Smith 	mdiag   += 4;
82b01c7715SBarry Smith       }
83b01c7715SBarry Smith       break;
84b01c7715SBarry Smith     case 3:
85b01c7715SBarry Smith       for (i=0; i<mbs; i++) {
86b01c7715SBarry Smith         odiag    = v + 9*diag_offset[i];
87b01c7715SBarry Smith         diag[0]  = odiag[0]; diag[1] = odiag[1]; diag[2] = odiag[2]; diag[3] = odiag[3];
88b01c7715SBarry Smith         diag[4]  = odiag[4]; diag[5] = odiag[5]; diag[6] = odiag[6]; diag[7] = odiag[7];
89b01c7715SBarry Smith         diag[8]  = odiag[8];
90b01c7715SBarry Smith         mdiag[0] = odiag[0]; mdiag[1] = odiag[1]; mdiag[2] = odiag[2]; mdiag[3] = odiag[3];
91b01c7715SBarry Smith         mdiag[4] = odiag[4]; mdiag[5] = odiag[5]; mdiag[6] = odiag[6]; mdiag[7] = odiag[7];
92b01c7715SBarry Smith         mdiag[8] = odiag[8];
9362bba022SBarry Smith 	ierr     = Kernel_A_gets_inverse_A_3(diag,shift);CHKERRQ(ierr);
94b01c7715SBarry Smith 	diag    += 9;
95b01c7715SBarry Smith 	mdiag   += 9;
96b01c7715SBarry Smith       }
97b01c7715SBarry Smith       break;
98b01c7715SBarry Smith     case 4:
99b01c7715SBarry Smith       for (i=0; i<mbs; i++) {
100b01c7715SBarry Smith         odiag  = v + 16*diag_offset[i];
101b01c7715SBarry Smith         ierr   = PetscMemcpy(diag,odiag,16*sizeof(PetscScalar));CHKERRQ(ierr);
102b01c7715SBarry Smith         ierr   = PetscMemcpy(mdiag,odiag,16*sizeof(PetscScalar));CHKERRQ(ierr);
10362bba022SBarry Smith 	ierr   = Kernel_A_gets_inverse_A_4(diag,shift);CHKERRQ(ierr);
104b01c7715SBarry Smith 	diag  += 16;
105b01c7715SBarry Smith 	mdiag += 16;
106b01c7715SBarry Smith       }
107b01c7715SBarry Smith       break;
108b01c7715SBarry Smith     case 5:
109b01c7715SBarry Smith       for (i=0; i<mbs; i++) {
110b01c7715SBarry Smith         odiag = v + 25*diag_offset[i];
111b01c7715SBarry Smith         ierr   = PetscMemcpy(diag,odiag,25*sizeof(PetscScalar));CHKERRQ(ierr);
112b01c7715SBarry Smith         ierr   = PetscMemcpy(mdiag,odiag,25*sizeof(PetscScalar));CHKERRQ(ierr);
113c0c53c30SBarry Smith 	ierr   = Kernel_A_gets_inverse_A_5(diag,ipvt,work,shift);CHKERRQ(ierr);
114b01c7715SBarry Smith 	diag  += 25;
115b01c7715SBarry Smith 	mdiag += 25;
116b01c7715SBarry Smith       }
117b01c7715SBarry Smith       break;
118d49b2adcSBarry Smith     case 6:
119d49b2adcSBarry Smith       for (i=0; i<mbs; i++) {
120d49b2adcSBarry Smith         odiag = v + 36*diag_offset[i];
121d49b2adcSBarry Smith         ierr   = PetscMemcpy(diag,odiag,36*sizeof(PetscScalar));CHKERRQ(ierr);
122d49b2adcSBarry Smith         ierr   = PetscMemcpy(mdiag,odiag,36*sizeof(PetscScalar));CHKERRQ(ierr);
123d49b2adcSBarry Smith 	ierr   = Kernel_A_gets_inverse_A_6(diag,shift);CHKERRQ(ierr);
124d49b2adcSBarry Smith 	diag  += 36;
125d49b2adcSBarry Smith 	mdiag += 36;
126d49b2adcSBarry Smith       }
127d49b2adcSBarry Smith       break;
128b01c7715SBarry Smith     default:
12965e19b50SBarry Smith       SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"not supported for block size %D",bs);
130b01c7715SBarry Smith   }
131b01c7715SBarry Smith   a->idiagvalid = PETSC_TRUE;
132b01c7715SBarry Smith   PetscFunctionReturn(0);
133b01c7715SBarry Smith }
13443516a2dSKris Buschelman EXTERN_C_END
135b01c7715SBarry Smith 
136b01c7715SBarry Smith #undef __FUNCT__
13741f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqBAIJ_1"
13841f059aeSBarry Smith PetscErrorCode MatSOR_SeqBAIJ_1(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1396d3beeddSMatthew Knepley {
1406d3beeddSMatthew Knepley   Mat_SeqBAIJ        *a = (Mat_SeqBAIJ*)A->data;
1416d3beeddSMatthew Knepley   PetscScalar        *x,x1,s1;
142dd6ea824SBarry Smith   const PetscScalar  *b;
143dd6ea824SBarry Smith   const MatScalar    *aa = a->a, *idiag,*mdiag,*v;
1446d3beeddSMatthew Knepley   PetscErrorCode     ierr;
14504fbf559SBarry Smith   PetscInt           m = a->mbs,i,i2,nz,j;
1466d3beeddSMatthew Knepley   const PetscInt     *diag,*ai = a->i,*aj = a->j,*vi;
1476d3beeddSMatthew Knepley 
1486d3beeddSMatthew Knepley   PetscFunctionBegin;
149e32f2f54SBarry Smith   if (flag & SOR_EISENSTAT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for Eisenstat");
1506d3beeddSMatthew Knepley   its = its*lits;
15165e19b50SBarry Smith   if (its <= 0) SETERRQ2(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
152e32f2f54SBarry Smith   if (fshift) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for diagonal shift");
153e32f2f54SBarry Smith   if (omega != 1.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for non-trivial relaxation factor");
154e32f2f54SBarry Smith   if ((flag & SOR_APPLY_UPPER) || (flag & SOR_APPLY_LOWER)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for applying upper or lower triangular parts");
155e32f2f54SBarry Smith   if (its > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support yet for multiple point block SOR iterations");
1566d3beeddSMatthew Knepley 
1576d3beeddSMatthew Knepley   if (!a->idiagvalid){ierr = MatInvertBlockDiagonal_SeqBAIJ(A);CHKERRQ(ierr);}
1586d3beeddSMatthew Knepley 
1596d3beeddSMatthew Knepley   diag  = a->diag;
1606d3beeddSMatthew Knepley   idiag = a->idiag;
1616d3beeddSMatthew Knepley   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1623649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
1636d3beeddSMatthew Knepley 
1646d3beeddSMatthew Knepley   if (flag & SOR_ZERO_INITIAL_GUESS) {
1656d3beeddSMatthew Knepley     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
1666d3beeddSMatthew Knepley       x[0] = b[0]*idiag[0];
1676d3beeddSMatthew Knepley       i2     = 1;
1686d3beeddSMatthew Knepley       idiag += 1;
1696d3beeddSMatthew Knepley       for (i=1; i<m; i++) {
1706d3beeddSMatthew Knepley         v     = aa + ai[i];
1716d3beeddSMatthew Knepley         vi    = aj + ai[i];
1726d3beeddSMatthew Knepley         nz    = diag[i] - ai[i];
1736d3beeddSMatthew Knepley         s1    = b[i2];
17404fbf559SBarry Smith         for (j=0; j<nz; j++) {
17504fbf559SBarry Smith           s1 -= v[j]*x[vi[j]];
1766d3beeddSMatthew Knepley         }
1776d3beeddSMatthew Knepley         x[i2]   = idiag[0]*s1;
1786d3beeddSMatthew Knepley         idiag   += 1;
1796d3beeddSMatthew Knepley         i2      += 1;
1806d3beeddSMatthew Knepley       }
1816d3beeddSMatthew Knepley       /* for logging purposes assume number of nonzero in lower half is 1/2 of total */
1826d3beeddSMatthew Knepley       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
1836d3beeddSMatthew Knepley     }
1846d3beeddSMatthew Knepley     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
1856d3beeddSMatthew Knepley         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
1866d3beeddSMatthew Knepley       i2    = 0;
1876d3beeddSMatthew Knepley       mdiag = a->idiag+a->mbs;
1886d3beeddSMatthew Knepley       for (i=0; i<m; i++) {
1896d3beeddSMatthew Knepley         x1      = x[i2];
1906d3beeddSMatthew Knepley         x[i2]   = mdiag[0]*x1;
1916d3beeddSMatthew Knepley         mdiag  += 1;
1926d3beeddSMatthew Knepley         i2     += 1;
1936d3beeddSMatthew Knepley       }
1946d3beeddSMatthew Knepley       ierr = PetscLogFlops(m);CHKERRQ(ierr);
1956d3beeddSMatthew Knepley     } else if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
196d0f46423SBarry Smith       ierr = PetscMemcpy(x,b,A->rmap->N*sizeof(PetscScalar));CHKERRQ(ierr);
1976d3beeddSMatthew Knepley     }
1986d3beeddSMatthew Knepley     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
1996d3beeddSMatthew Knepley       idiag   = a->idiag+a->mbs - 1;
2006d3beeddSMatthew Knepley       i2      = m - 1;
2016d3beeddSMatthew Knepley       x1      = x[i2];
2026d3beeddSMatthew Knepley       x[i2]   = idiag[0]*x1;
2036d3beeddSMatthew Knepley       idiag -= 1;
2046d3beeddSMatthew Knepley       i2    -= 1;
2056d3beeddSMatthew Knepley       for (i=m-2; i>=0; i--) {
2066d3beeddSMatthew Knepley         v     = aa + (diag[i]+1);
2076d3beeddSMatthew Knepley         vi    = aj + diag[i] + 1;
2086d3beeddSMatthew Knepley         nz    = ai[i+1] - diag[i] - 1;
2096d3beeddSMatthew Knepley         s1    = x[i2];
21004fbf559SBarry Smith         for (j=0; j<nz; j++) {
21104fbf559SBarry Smith           s1 -= v[j]*x[vi[j]];
2126d3beeddSMatthew Knepley         }
2136d3beeddSMatthew Knepley         x[i2]   = idiag[0]*s1;
214ab040260SJed Brown         idiag   -= 1;
2156d3beeddSMatthew Knepley         i2      -= 1;
2166d3beeddSMatthew Knepley       }
2176d3beeddSMatthew Knepley       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
2186d3beeddSMatthew Knepley     }
2196d3beeddSMatthew Knepley   } else {
220e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supports point block SOR with zero initial guess");
2216d3beeddSMatthew Knepley   }
2226d3beeddSMatthew Knepley   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
2233649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
2246d3beeddSMatthew Knepley   PetscFunctionReturn(0);
2256d3beeddSMatthew Knepley }
2266d3beeddSMatthew Knepley 
2276d3beeddSMatthew Knepley #undef __FUNCT__
22841f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqBAIJ_2"
22941f059aeSBarry Smith PetscErrorCode MatSOR_SeqBAIJ_2(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
230b01c7715SBarry Smith {
231b01c7715SBarry Smith   Mat_SeqBAIJ        *a = (Mat_SeqBAIJ*)A->data;
232b01c7715SBarry Smith   PetscScalar        *x,x1,x2,s1,s2;
233dd6ea824SBarry Smith   const PetscScalar  *b;
234dd6ea824SBarry Smith   const MatScalar    *v,*aa = a->a, *idiag,*mdiag;
235dfbe8321SBarry Smith   PetscErrorCode     ierr;
23604fbf559SBarry Smith   PetscInt           m = a->mbs,i,i2,nz,idx,j,it;
237c1ac3661SBarry Smith   const PetscInt     *diag,*ai = a->i,*aj = a->j,*vi;
238b01c7715SBarry Smith 
239b01c7715SBarry Smith   PetscFunctionBegin;
240e32f2f54SBarry Smith   if (flag & SOR_EISENSTAT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for Eisenstat");
241b01c7715SBarry Smith   its = its*lits;
242e32f2f54SBarry Smith   if (its <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
243e32f2f54SBarry Smith   if (fshift) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for diagonal shift");
244e32f2f54SBarry Smith   if (omega != 1.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for non-trivial relaxation factor");
245e32f2f54SBarry Smith   if ((flag & SOR_APPLY_UPPER) || (flag & SOR_APPLY_LOWER)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for applying upper or lower triangular parts");
246e32f2f54SBarry Smith   if (its > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support yet for multiple point block SOR iterations");
247b01c7715SBarry Smith 
248b01c7715SBarry Smith   if (!a->idiagvalid){ierr = MatInvertBlockDiagonal_SeqBAIJ(A);CHKERRQ(ierr);}
249b01c7715SBarry Smith 
250b01c7715SBarry Smith   diag  = a->diag;
251b01c7715SBarry Smith   idiag = a->idiag;
2521ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
2533649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
254b01c7715SBarry Smith 
255b01c7715SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
256b01c7715SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
257b01c7715SBarry Smith       x[0] = b[0]*idiag[0] + b[1]*idiag[2];
258b01c7715SBarry Smith       x[1] = b[0]*idiag[1] + b[1]*idiag[3];
259b01c7715SBarry Smith       i2     = 2;
260b01c7715SBarry Smith       idiag += 4;
261b01c7715SBarry Smith       for (i=1; i<m; i++) {
262b01c7715SBarry Smith 	v     = aa + 4*ai[i];
263b01c7715SBarry Smith 	vi    = aj + ai[i];
264b01c7715SBarry Smith 	nz    = diag[i] - ai[i];
265b01c7715SBarry Smith 	s1    = b[i2]; s2 = b[i2+1];
26604fbf559SBarry Smith         for (j=0; j<nz; j++) {
26704fbf559SBarry Smith 	  idx  = 2*vi[j];
26804fbf559SBarry Smith           it   = 4*j;
269b01c7715SBarry Smith 	  x1   = x[idx]; x2 = x[1+idx];
27004fbf559SBarry Smith 	  s1  -= v[it]*x1 + v[it+2]*x2;
27104fbf559SBarry Smith 	  s2  -= v[it+1]*x1 + v[it+3]*x2;
272b01c7715SBarry Smith 	}
273b01c7715SBarry Smith 	x[i2]   = idiag[0]*s1 + idiag[2]*s2;
274b01c7715SBarry Smith 	x[i2+1] = idiag[1]*s1 + idiag[3]*s2;
275b01c7715SBarry Smith         idiag   += 4;
276b01c7715SBarry Smith         i2      += 2;
277b01c7715SBarry Smith       }
278b01c7715SBarry Smith       /* for logging purposes assume number of nonzero in lower half is 1/2 of total */
279dc0b31edSSatish Balay       ierr = PetscLogFlops(4.0*(a->nz));CHKERRQ(ierr);
280b01c7715SBarry Smith     }
281b01c7715SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
282b01c7715SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
283b01c7715SBarry Smith       i2    = 0;
284b01c7715SBarry Smith       mdiag = a->idiag+4*a->mbs;
285b01c7715SBarry Smith       for (i=0; i<m; i++) {
286b01c7715SBarry Smith         x1      = x[i2]; x2 = x[i2+1];
287b01c7715SBarry Smith         x[i2]   = mdiag[0]*x1 + mdiag[2]*x2;
288b01c7715SBarry Smith         x[i2+1] = mdiag[1]*x1 + mdiag[3]*x2;
289b01c7715SBarry Smith         mdiag  += 4;
290b01c7715SBarry Smith         i2     += 2;
291b01c7715SBarry Smith       }
292dc0b31edSSatish Balay       ierr = PetscLogFlops(6.0*m);CHKERRQ(ierr);
293b01c7715SBarry Smith     } else if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
294d0f46423SBarry Smith       ierr = PetscMemcpy(x,b,A->rmap->N*sizeof(PetscScalar));CHKERRQ(ierr);
295b01c7715SBarry Smith     }
296b01c7715SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
297b01c7715SBarry Smith       idiag   = a->idiag+4*a->mbs - 4;
298b01c7715SBarry Smith       i2      = 2*m - 2;
299b01c7715SBarry Smith       x1      = x[i2]; x2 = x[i2+1];
300b01c7715SBarry Smith       x[i2]   = idiag[0]*x1 + idiag[2]*x2;
301b01c7715SBarry Smith       x[i2+1] = idiag[1]*x1 + idiag[3]*x2;
302b01c7715SBarry Smith       idiag -= 4;
303b01c7715SBarry Smith       i2    -= 2;
304b01c7715SBarry Smith       for (i=m-2; i>=0; i--) {
305b01c7715SBarry Smith 	v     = aa + 4*(diag[i]+1);
306b01c7715SBarry Smith 	vi    = aj + diag[i] + 1;
307b01c7715SBarry Smith 	nz    = ai[i+1] - diag[i] - 1;
308b01c7715SBarry Smith 	s1    = x[i2]; s2 = x[i2+1];
30904fbf559SBarry Smith         for (j=0; j<nz; j++) {
31004fbf559SBarry Smith  	  idx  = 2*vi[j];
31104fbf559SBarry Smith           it   = 4*j;
312b01c7715SBarry Smith 	  x1   = x[idx]; x2 = x[1+idx];
31304fbf559SBarry Smith 	  s1  -= v[it]*x1 + v[it+2]*x2;
31404fbf559SBarry Smith 	  s2  -= v[it+1]*x1 + v[it+3]*x2;
315b01c7715SBarry Smith 	}
316b01c7715SBarry Smith 	x[i2]   = idiag[0]*s1 + idiag[2]*s2;
317b01c7715SBarry Smith 	x[i2+1] = idiag[1]*s1 + idiag[3]*s2;
318b01c7715SBarry Smith         idiag   -= 4;
319b01c7715SBarry Smith         i2      -= 2;
320b01c7715SBarry Smith       }
321dc0b31edSSatish Balay       ierr = PetscLogFlops(4.0*(a->nz));CHKERRQ(ierr);
322b01c7715SBarry Smith     }
323b01c7715SBarry Smith   } else {
324e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supports point block SOR with zero initial guess");
325b01c7715SBarry Smith   }
3261ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
3273649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
328b01c7715SBarry Smith   PetscFunctionReturn(0);
329b01c7715SBarry Smith }
330b01c7715SBarry Smith 
331b01c7715SBarry Smith #undef __FUNCT__
33241f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqBAIJ_3"
33341f059aeSBarry Smith PetscErrorCode MatSOR_SeqBAIJ_3(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
334b01c7715SBarry Smith {
335b01c7715SBarry Smith   Mat_SeqBAIJ        *a = (Mat_SeqBAIJ*)A->data;
336b01c7715SBarry Smith   PetscScalar        *x,x1,x2,x3,s1,s2,s3;
337dd6ea824SBarry Smith   const MatScalar    *v,*aa = a->a, *idiag,*mdiag;
338dd6ea824SBarry Smith   const PetscScalar  *b;
339dfbe8321SBarry Smith   PetscErrorCode     ierr;
340c1ac3661SBarry Smith   PetscInt           m = a->mbs,i,i2,nz,idx;
341c1ac3661SBarry Smith   const PetscInt     *diag,*ai = a->i,*aj = a->j,*vi;
342b01c7715SBarry Smith 
343b01c7715SBarry Smith   PetscFunctionBegin;
344b01c7715SBarry Smith   its = its*lits;
345e32f2f54SBarry Smith   if (flag & SOR_EISENSTAT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for Eisenstat");
346e32f2f54SBarry Smith   if (its <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
347e32f2f54SBarry Smith   if (fshift) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for diagonal shift");
348e32f2f54SBarry Smith   if (omega != 1.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for non-trivial relaxation factor");
349e32f2f54SBarry Smith   if ((flag & SOR_APPLY_UPPER) || (flag & SOR_APPLY_LOWER)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for applying upper or lower triangular parts");
350e32f2f54SBarry Smith   if (its > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support yet for multiple point block SOR iterations");
351b01c7715SBarry Smith 
352b01c7715SBarry Smith   if (!a->idiagvalid){ierr = MatInvertBlockDiagonal_SeqBAIJ(A);CHKERRQ(ierr);}
353b01c7715SBarry Smith 
354b01c7715SBarry Smith   diag  = a->diag;
355b01c7715SBarry Smith   idiag = a->idiag;
3561ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
3573649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
358b01c7715SBarry Smith 
359b01c7715SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
360b01c7715SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
361b01c7715SBarry Smith       x[0] = b[0]*idiag[0] + b[1]*idiag[3] + b[2]*idiag[6];
362b01c7715SBarry Smith       x[1] = b[0]*idiag[1] + b[1]*idiag[4] + b[2]*idiag[7];
363b01c7715SBarry Smith       x[2] = b[0]*idiag[2] + b[1]*idiag[5] + b[2]*idiag[8];
364b01c7715SBarry Smith       i2     = 3;
365b01c7715SBarry Smith       idiag += 9;
366b01c7715SBarry Smith       for (i=1; i<m; i++) {
367b01c7715SBarry Smith         v     = aa + 9*ai[i];
368b01c7715SBarry Smith         vi    = aj + ai[i];
369b01c7715SBarry Smith         nz    = diag[i] - ai[i];
370b01c7715SBarry Smith         s1    = b[i2]; s2 = b[i2+1]; s3 = b[i2+2];
371b01c7715SBarry Smith         while (nz--) {
372b01c7715SBarry Smith           idx  = 3*(*vi++);
373b01c7715SBarry Smith           x1   = x[idx]; x2 = x[1+idx];x3 = x[2+idx];
374b01c7715SBarry Smith           s1  -= v[0]*x1 + v[3]*x2 + v[6]*x3;
375b01c7715SBarry Smith           s2  -= v[1]*x1 + v[4]*x2 + v[7]*x3;
376b01c7715SBarry Smith           s3  -= v[2]*x1 + v[5]*x2 + v[8]*x3;
377b01c7715SBarry Smith           v   += 9;
378b01c7715SBarry Smith         }
379b01c7715SBarry Smith         x[i2]   = idiag[0]*s1 + idiag[3]*s2 + idiag[6]*s3;
380b01c7715SBarry Smith         x[i2+1] = idiag[1]*s1 + idiag[4]*s2 + idiag[7]*s3;
381b01c7715SBarry Smith         x[i2+2] = idiag[2]*s1 + idiag[5]*s2 + idiag[8]*s3;
382b01c7715SBarry Smith         idiag   += 9;
383b01c7715SBarry Smith         i2      += 3;
384b01c7715SBarry Smith       }
385b01c7715SBarry Smith       /* for logging purposes assume number of nonzero in lower half is 1/2 of total */
386dc0b31edSSatish Balay       ierr = PetscLogFlops(9.0*(a->nz));CHKERRQ(ierr);
387b01c7715SBarry Smith     }
388b01c7715SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
389b01c7715SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
390b01c7715SBarry Smith       i2    = 0;
391b01c7715SBarry Smith       mdiag = a->idiag+9*a->mbs;
392b01c7715SBarry Smith       for (i=0; i<m; i++) {
393b01c7715SBarry Smith         x1      = x[i2]; x2 = x[i2+1]; x3 = x[i2+2];
394b01c7715SBarry Smith         x[i2]   = mdiag[0]*x1 + mdiag[3]*x2 + mdiag[6]*x3;
395b01c7715SBarry Smith         x[i2+1] = mdiag[1]*x1 + mdiag[4]*x2 + mdiag[7]*x3;
396b01c7715SBarry Smith         x[i2+2] = mdiag[2]*x1 + mdiag[5]*x2 + mdiag[8]*x3;
397b01c7715SBarry Smith         mdiag  += 9;
398b01c7715SBarry Smith         i2     += 3;
399b01c7715SBarry Smith       }
400dc0b31edSSatish Balay       ierr = PetscLogFlops(15.0*m);CHKERRQ(ierr);
401b01c7715SBarry Smith     } else if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
402d0f46423SBarry Smith       ierr = PetscMemcpy(x,b,A->rmap->N*sizeof(PetscScalar));CHKERRQ(ierr);
403b01c7715SBarry Smith     }
404b01c7715SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
405b01c7715SBarry Smith       idiag   = a->idiag+9*a->mbs - 9;
406b01c7715SBarry Smith       i2      = 3*m - 3;
407b01c7715SBarry Smith       x1      = x[i2]; x2 = x[i2+1]; x3 = x[i2+2];
408b01c7715SBarry Smith       x[i2]   = idiag[0]*x1 + idiag[3]*x2 + idiag[6]*x3;
409b01c7715SBarry Smith       x[i2+1] = idiag[1]*x1 + idiag[4]*x2 + idiag[7]*x3;
410b01c7715SBarry Smith       x[i2+2] = idiag[2]*x1 + idiag[5]*x2 + idiag[8]*x3;
411b01c7715SBarry Smith       idiag -= 9;
412b01c7715SBarry Smith       i2    -= 3;
413b01c7715SBarry Smith       for (i=m-2; i>=0; i--) {
414b01c7715SBarry Smith         v     = aa + 9*(diag[i]+1);
415b01c7715SBarry Smith         vi    = aj + diag[i] + 1;
416b01c7715SBarry Smith         nz    = ai[i+1] - diag[i] - 1;
417b01c7715SBarry Smith         s1    = x[i2]; s2 = x[i2+1]; s3 = x[i2+2];
418b01c7715SBarry Smith         while (nz--) {
419b01c7715SBarry Smith           idx  = 3*(*vi++);
420b01c7715SBarry Smith           x1   = x[idx]; x2 = x[1+idx]; x3 = x[2+idx];
421b01c7715SBarry Smith           s1  -= v[0]*x1 + v[3]*x2 + v[6]*x3;
422b01c7715SBarry Smith           s2  -= v[1]*x1 + v[4]*x2 + v[7]*x3;
423b01c7715SBarry Smith           s3  -= v[2]*x1 + v[5]*x2 + v[8]*x3;
424b01c7715SBarry Smith           v   += 9;
425b01c7715SBarry Smith         }
426b01c7715SBarry Smith         x[i2]   = idiag[0]*s1 + idiag[3]*s2 + idiag[6]*s3;
427b01c7715SBarry Smith         x[i2+1] = idiag[1]*s1 + idiag[4]*s2 + idiag[7]*s3;
428b01c7715SBarry Smith         x[i2+2] = idiag[2]*s1 + idiag[5]*s2 + idiag[8]*s3;
429b01c7715SBarry Smith         idiag   -= 9;
430b01c7715SBarry Smith         i2      -= 3;
431b01c7715SBarry Smith       }
432dc0b31edSSatish Balay       ierr = PetscLogFlops(9.0*(a->nz));CHKERRQ(ierr);
433b01c7715SBarry Smith     }
434b01c7715SBarry Smith   } else {
435e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supports point block SOR with zero initial guess");
436b01c7715SBarry Smith   }
4371ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
4383649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
439b01c7715SBarry Smith   PetscFunctionReturn(0);
440b01c7715SBarry Smith }
441b01c7715SBarry Smith 
442b01c7715SBarry Smith #undef __FUNCT__
44341f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqBAIJ_4"
44441f059aeSBarry Smith PetscErrorCode MatSOR_SeqBAIJ_4(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
445b01c7715SBarry Smith {
446b01c7715SBarry Smith   Mat_SeqBAIJ        *a = (Mat_SeqBAIJ*)A->data;
447b01c7715SBarry Smith   PetscScalar        *x,x1,x2,x3,x4,s1,s2,s3,s4;
448dd6ea824SBarry Smith   const MatScalar    *v,*aa = a->a, *idiag,*mdiag;
449dd6ea824SBarry Smith   const PetscScalar  *b;
450dfbe8321SBarry Smith   PetscErrorCode     ierr;
451c1ac3661SBarry Smith   PetscInt           m = a->mbs,i,i2,nz,idx;
452c1ac3661SBarry Smith   const PetscInt     *diag,*ai = a->i,*aj = a->j,*vi;
453b01c7715SBarry Smith 
454b01c7715SBarry Smith   PetscFunctionBegin;
455e32f2f54SBarry Smith   if (flag & SOR_EISENSTAT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for Eisenstat");
456b01c7715SBarry Smith   its = its*lits;
457e32f2f54SBarry Smith   if (its <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
458e32f2f54SBarry Smith   if (fshift) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for diagonal shift");
459e32f2f54SBarry Smith   if (omega != 1.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for non-trivial relaxation factor");
460e32f2f54SBarry Smith   if ((flag & SOR_APPLY_UPPER) || (flag & SOR_APPLY_LOWER)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for applying upper or lower triangular parts");
461e32f2f54SBarry Smith   if (its > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support yet for multiple point block SOR iterations");
462b01c7715SBarry Smith 
463b01c7715SBarry Smith   if (!a->idiagvalid){ierr = MatInvertBlockDiagonal_SeqBAIJ(A);CHKERRQ(ierr);}
464b01c7715SBarry Smith 
465b01c7715SBarry Smith   diag  = a->diag;
466b01c7715SBarry Smith   idiag = a->idiag;
4671ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
4683649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
469b01c7715SBarry Smith 
470b01c7715SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
471b01c7715SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
472b01c7715SBarry Smith       x[0] = b[0]*idiag[0] + b[1]*idiag[4] + b[2]*idiag[8]  + b[3]*idiag[12];
473b01c7715SBarry Smith       x[1] = b[0]*idiag[1] + b[1]*idiag[5] + b[2]*idiag[9]  + b[3]*idiag[13];
474b01c7715SBarry Smith       x[2] = b[0]*idiag[2] + b[1]*idiag[6] + b[2]*idiag[10] + b[3]*idiag[14];
475b01c7715SBarry Smith       x[3] = b[0]*idiag[3] + b[1]*idiag[7] + b[2]*idiag[11] + b[3]*idiag[15];
476b01c7715SBarry Smith       i2     = 4;
477b01c7715SBarry Smith       idiag += 16;
478b01c7715SBarry Smith       for (i=1; i<m; i++) {
479b01c7715SBarry Smith 	v     = aa + 16*ai[i];
480b01c7715SBarry Smith 	vi    = aj + ai[i];
481b01c7715SBarry Smith 	nz    = diag[i] - ai[i];
482b01c7715SBarry Smith 	s1    = b[i2]; s2 = b[i2+1]; s3 = b[i2+2]; s4 = b[i2+3];
483b01c7715SBarry Smith 	while (nz--) {
484b01c7715SBarry Smith 	  idx  = 4*(*vi++);
485b01c7715SBarry Smith 	  x1   = x[idx]; x2 = x[1+idx]; x3 = x[2+idx]; x4 = x[3+idx];
486b01c7715SBarry Smith 	  s1  -= v[0]*x1 + v[4]*x2 + v[8]*x3  + v[12]*x4;
487b01c7715SBarry Smith 	  s2  -= v[1]*x1 + v[5]*x2 + v[9]*x3  + v[13]*x4;
488b01c7715SBarry Smith 	  s3  -= v[2]*x1 + v[6]*x2 + v[10]*x3 + v[14]*x4;
489b01c7715SBarry Smith 	  s4  -= v[3]*x1 + v[7]*x2 + v[11]*x3 + v[15]*x4;
490b01c7715SBarry Smith 	  v   += 16;
491b01c7715SBarry Smith 	}
492b01c7715SBarry Smith 	x[i2]   = idiag[0]*s1 + idiag[4]*s2 + idiag[8]*s3  + idiag[12]*s4;
493b01c7715SBarry Smith 	x[i2+1] = idiag[1]*s1 + idiag[5]*s2 + idiag[9]*s3  + idiag[13]*s4;
494b01c7715SBarry Smith 	x[i2+2] = idiag[2]*s1 + idiag[6]*s2 + idiag[10]*s3 + idiag[14]*s4;
495b01c7715SBarry Smith 	x[i2+3] = idiag[3]*s1 + idiag[7]*s2 + idiag[11]*s3 + idiag[15]*s4;
496b01c7715SBarry Smith         idiag   += 16;
497b01c7715SBarry Smith         i2      += 4;
498b01c7715SBarry Smith       }
499b01c7715SBarry Smith       /* for logging purposes assume number of nonzero in lower half is 1/2 of total */
500dc0b31edSSatish Balay       ierr = PetscLogFlops(16.0*(a->nz));CHKERRQ(ierr);
501b01c7715SBarry Smith     }
502b01c7715SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
503b01c7715SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
504b01c7715SBarry Smith       i2    = 0;
505b01c7715SBarry Smith       mdiag = a->idiag+16*a->mbs;
506b01c7715SBarry Smith       for (i=0; i<m; i++) {
507b01c7715SBarry Smith         x1      = x[i2]; x2 = x[i2+1]; x3 = x[i2+2]; x4 = x[i2+3];
508b01c7715SBarry Smith         x[i2]   = mdiag[0]*x1 + mdiag[4]*x2 + mdiag[8]*x3  + mdiag[12]*x4;
509b01c7715SBarry Smith         x[i2+1] = mdiag[1]*x1 + mdiag[5]*x2 + mdiag[9]*x3  + mdiag[13]*x4;
510b01c7715SBarry Smith         x[i2+2] = mdiag[2]*x1 + mdiag[6]*x2 + mdiag[10]*x3 + mdiag[14]*x4;
511b01c7715SBarry Smith         x[i2+3] = mdiag[3]*x1 + mdiag[7]*x2 + mdiag[11]*x3 + mdiag[15]*x4;
512b01c7715SBarry Smith         mdiag  += 16;
513b01c7715SBarry Smith         i2     += 4;
514b01c7715SBarry Smith       }
515dc0b31edSSatish Balay       ierr = PetscLogFlops(28.0*m);CHKERRQ(ierr);
516b01c7715SBarry Smith     } else if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
517d0f46423SBarry Smith       ierr = PetscMemcpy(x,b,A->rmap->N*sizeof(PetscScalar));CHKERRQ(ierr);
518b01c7715SBarry Smith     }
519b01c7715SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
520b01c7715SBarry Smith       idiag   = a->idiag+16*a->mbs - 16;
521b01c7715SBarry Smith       i2      = 4*m - 4;
522b01c7715SBarry Smith       x1      = x[i2]; x2 = x[i2+1]; x3 = x[i2+2]; x4 = x[i2+3];
523b01c7715SBarry Smith       x[i2]   = idiag[0]*x1 + idiag[4]*x2 + idiag[8]*x3  + idiag[12]*x4;
524b01c7715SBarry Smith       x[i2+1] = idiag[1]*x1 + idiag[5]*x2 + idiag[9]*x3  + idiag[13]*x4;
525b01c7715SBarry Smith       x[i2+2] = idiag[2]*x1 + idiag[6]*x2 + idiag[10]*x3 + idiag[14]*x4;
526b01c7715SBarry Smith       x[i2+3] = idiag[3]*x1 + idiag[7]*x2 + idiag[11]*x3 + idiag[15]*x4;
527b01c7715SBarry Smith       idiag -= 16;
528b01c7715SBarry Smith       i2    -= 4;
529b01c7715SBarry Smith       for (i=m-2; i>=0; i--) {
530b01c7715SBarry Smith 	v     = aa + 16*(diag[i]+1);
531b01c7715SBarry Smith 	vi    = aj + diag[i] + 1;
532b01c7715SBarry Smith 	nz    = ai[i+1] - diag[i] - 1;
533b01c7715SBarry Smith 	s1    = x[i2]; s2 = x[i2+1]; s3 = x[i2+2]; s4 = x[i2+3];
534b01c7715SBarry Smith 	while (nz--) {
535b01c7715SBarry Smith 	  idx  = 4*(*vi++);
536b01c7715SBarry Smith 	  x1   = x[idx]; x2 = x[1+idx]; x3 = x[2+idx]; x4 = x[3+idx];
537b01c7715SBarry Smith 	  s1  -= v[0]*x1 + v[4]*x2 + v[8]*x3  + v[12]*x4;
538b01c7715SBarry Smith 	  s2  -= v[1]*x1 + v[5]*x2 + v[9]*x3  + v[13]*x4;
539b01c7715SBarry Smith 	  s3  -= v[2]*x1 + v[6]*x2 + v[10]*x3 + v[14]*x4;
540b01c7715SBarry Smith 	  s4  -= v[3]*x1 + v[7]*x2 + v[11]*x3 + v[15]*x4;
541b01c7715SBarry Smith 	  v   += 16;
542b01c7715SBarry Smith 	}
543b01c7715SBarry Smith 	x[i2]   = idiag[0]*s1 + idiag[4]*s2 + idiag[8]*s3  + idiag[12]*s4;
544b01c7715SBarry Smith 	x[i2+1] = idiag[1]*s1 + idiag[5]*s2 + idiag[9]*s3  + idiag[13]*s4;
545b01c7715SBarry Smith 	x[i2+2] = idiag[2]*s1 + idiag[6]*s2 + idiag[10]*s3 + idiag[14]*s4;
546b01c7715SBarry Smith 	x[i2+3] = idiag[3]*s1 + idiag[7]*s2 + idiag[11]*s3 + idiag[15]*s4;
547b01c7715SBarry Smith         idiag   -= 16;
548b01c7715SBarry Smith         i2      -= 4;
549b01c7715SBarry Smith       }
550dc0b31edSSatish Balay       ierr = PetscLogFlops(16.0*(a->nz));CHKERRQ(ierr);
551b01c7715SBarry Smith     }
552b01c7715SBarry Smith   } else {
553e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supports point block SOR with zero initial guess");
554b01c7715SBarry Smith   }
5551ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
5563649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
557b01c7715SBarry Smith   PetscFunctionReturn(0);
558b01c7715SBarry Smith }
559b01c7715SBarry Smith 
560b01c7715SBarry Smith #undef __FUNCT__
56141f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqBAIJ_5"
56241f059aeSBarry Smith PetscErrorCode MatSOR_SeqBAIJ_5(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
563b01c7715SBarry Smith {
564b01c7715SBarry Smith   Mat_SeqBAIJ        *a = (Mat_SeqBAIJ*)A->data;
565b01c7715SBarry Smith   PetscScalar        *x,x1,x2,x3,x4,x5,s1,s2,s3,s4,s5;
566dd6ea824SBarry Smith   const MatScalar    *v,*aa = a->a, *idiag,*mdiag;
567dd6ea824SBarry Smith   const PetscScalar  *b;
568dfbe8321SBarry Smith   PetscErrorCode     ierr;
569c1ac3661SBarry Smith   PetscInt           m = a->mbs,i,i2,nz,idx;
570c1ac3661SBarry Smith   const PetscInt     *diag,*ai = a->i,*aj = a->j,*vi;
571b01c7715SBarry Smith 
572b01c7715SBarry Smith   PetscFunctionBegin;
573e32f2f54SBarry Smith   if (flag & SOR_EISENSTAT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for Eisenstat");
574b01c7715SBarry Smith   its = its*lits;
575e32f2f54SBarry Smith   if (its <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
576e32f2f54SBarry Smith   if (fshift) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for diagonal shift");
577e32f2f54SBarry Smith   if (omega != 1.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for non-trivial relaxation factor");
578e32f2f54SBarry Smith   if ((flag & SOR_APPLY_UPPER) || (flag & SOR_APPLY_LOWER)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for applying upper or lower triangular parts");
579e32f2f54SBarry Smith   if (its > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support yet for multiple point block SOR iterations");
580b01c7715SBarry Smith 
581b01c7715SBarry Smith   if (!a->idiagvalid){ierr = MatInvertBlockDiagonal_SeqBAIJ(A);CHKERRQ(ierr);}
582b01c7715SBarry Smith 
583b01c7715SBarry Smith   diag  = a->diag;
584b01c7715SBarry Smith   idiag = a->idiag;
5851ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
5863649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
587b01c7715SBarry Smith 
588b01c7715SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
589b01c7715SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
590b01c7715SBarry Smith       x[0] = b[0]*idiag[0] + b[1]*idiag[5] + b[2]*idiag[10] + b[3]*idiag[15] + b[4]*idiag[20];
591b01c7715SBarry Smith       x[1] = b[0]*idiag[1] + b[1]*idiag[6] + b[2]*idiag[11] + b[3]*idiag[16] + b[4]*idiag[21];
592b01c7715SBarry Smith       x[2] = b[0]*idiag[2] + b[1]*idiag[7] + b[2]*idiag[12] + b[3]*idiag[17] + b[4]*idiag[22];
593b01c7715SBarry Smith       x[3] = b[0]*idiag[3] + b[1]*idiag[8] + b[2]*idiag[13] + b[3]*idiag[18] + b[4]*idiag[23];
594b01c7715SBarry Smith       x[4] = b[0]*idiag[4] + b[1]*idiag[9] + b[2]*idiag[14] + b[3]*idiag[19] + b[4]*idiag[24];
595b01c7715SBarry Smith       i2     = 5;
596b01c7715SBarry Smith       idiag += 25;
597b01c7715SBarry Smith       for (i=1; i<m; i++) {
598b01c7715SBarry Smith 	v     = aa + 25*ai[i];
599b01c7715SBarry Smith 	vi    = aj + ai[i];
600b01c7715SBarry Smith 	nz    = diag[i] - ai[i];
601b01c7715SBarry Smith 	s1    = b[i2]; s2 = b[i2+1]; s3 = b[i2+2]; s4 = b[i2+3]; s5 = b[i2+4];
602b01c7715SBarry Smith 	while (nz--) {
603b01c7715SBarry Smith 	  idx  = 5*(*vi++);
604b01c7715SBarry Smith 	  x1   = x[idx]; x2 = x[1+idx]; x3 = x[2+idx]; x4 = x[3+idx]; x5 = x[4+idx];
605b01c7715SBarry Smith 	  s1  -= v[0]*x1 + v[5]*x2 + v[10]*x3 + v[15]*x4 + v[20]*x5;
606b01c7715SBarry Smith 	  s2  -= v[1]*x1 + v[6]*x2 + v[11]*x3 + v[16]*x4 + v[21]*x5;
607b01c7715SBarry Smith 	  s3  -= v[2]*x1 + v[7]*x2 + v[12]*x3 + v[17]*x4 + v[22]*x5;
608b01c7715SBarry Smith 	  s4  -= v[3]*x1 + v[8]*x2 + v[13]*x3 + v[18]*x4 + v[23]*x5;
609b01c7715SBarry Smith 	  s5  -= v[4]*x1 + v[9]*x2 + v[14]*x3 + v[19]*x4 + v[24]*x5;
610b01c7715SBarry Smith 	  v   += 25;
611b01c7715SBarry Smith 	}
612b01c7715SBarry Smith 	x[i2]   = idiag[0]*s1 + idiag[5]*s2 + idiag[10]*s3 + idiag[15]*s4 + idiag[20]*s5;
613b01c7715SBarry Smith 	x[i2+1] = idiag[1]*s1 + idiag[6]*s2 + idiag[11]*s3 + idiag[16]*s4 + idiag[21]*s5;
614b01c7715SBarry Smith 	x[i2+2] = idiag[2]*s1 + idiag[7]*s2 + idiag[12]*s3 + idiag[17]*s4 + idiag[22]*s5;
615b01c7715SBarry Smith 	x[i2+3] = idiag[3]*s1 + idiag[8]*s2 + idiag[13]*s3 + idiag[18]*s4 + idiag[23]*s5;
616b01c7715SBarry Smith 	x[i2+4] = idiag[4]*s1 + idiag[9]*s2 + idiag[14]*s3 + idiag[19]*s4 + idiag[24]*s5;
617b01c7715SBarry Smith         idiag   += 25;
618b01c7715SBarry Smith         i2      += 5;
619b01c7715SBarry Smith       }
620b01c7715SBarry Smith       /* for logging purposes assume number of nonzero in lower half is 1/2 of total */
621dc0b31edSSatish Balay       ierr = PetscLogFlops(25.0*(a->nz));CHKERRQ(ierr);
622b01c7715SBarry Smith     }
623b01c7715SBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
624b01c7715SBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
625b01c7715SBarry Smith       i2    = 0;
626b01c7715SBarry Smith       mdiag = a->idiag+25*a->mbs;
627b01c7715SBarry Smith       for (i=0; i<m; i++) {
628b01c7715SBarry Smith         x1      = x[i2]; x2 = x[i2+1]; x3 = x[i2+2]; x4 = x[i2+3]; x5 = x[i2+4];
629b01c7715SBarry Smith         x[i2]   = mdiag[0]*x1 + mdiag[5]*x2 + mdiag[10]*x3 + mdiag[15]*x4 + mdiag[20]*x5;
630b01c7715SBarry Smith         x[i2+1] = mdiag[1]*x1 + mdiag[6]*x2 + mdiag[11]*x3 + mdiag[16]*x4 + mdiag[21]*x5;
631b01c7715SBarry Smith         x[i2+2] = mdiag[2]*x1 + mdiag[7]*x2 + mdiag[12]*x3 + mdiag[17]*x4 + mdiag[22]*x5;
632b01c7715SBarry Smith         x[i2+3] = mdiag[3]*x1 + mdiag[8]*x2 + mdiag[13]*x3 + mdiag[18]*x4 + mdiag[23]*x5;
633b01c7715SBarry Smith         x[i2+4] = mdiag[4]*x1 + mdiag[9]*x2 + mdiag[14]*x3 + mdiag[19]*x4 + mdiag[24]*x5;
634b01c7715SBarry Smith         mdiag  += 25;
635b01c7715SBarry Smith         i2     += 5;
636b01c7715SBarry Smith       }
637dc0b31edSSatish Balay       ierr = PetscLogFlops(45.0*m);CHKERRQ(ierr);
638b01c7715SBarry Smith     } else if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
639d0f46423SBarry Smith       ierr = PetscMemcpy(x,b,A->rmap->N*sizeof(PetscScalar));CHKERRQ(ierr);
640b01c7715SBarry Smith     }
641b01c7715SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
642b01c7715SBarry Smith       idiag   = a->idiag+25*a->mbs - 25;
643b01c7715SBarry Smith       i2      = 5*m - 5;
644b01c7715SBarry Smith       x1      = x[i2]; x2 = x[i2+1]; x3 = x[i2+2]; x4 = x[i2+3]; x5 = x[i2+4];
645b01c7715SBarry Smith       x[i2]   = idiag[0]*x1 + idiag[5]*x2 + idiag[10]*x3 + idiag[15]*x4 + idiag[20]*x5;
646b01c7715SBarry Smith       x[i2+1] = idiag[1]*x1 + idiag[6]*x2 + idiag[11]*x3 + idiag[16]*x4 + idiag[21]*x5;
647b01c7715SBarry Smith       x[i2+2] = idiag[2]*x1 + idiag[7]*x2 + idiag[12]*x3 + idiag[17]*x4 + idiag[22]*x5;
648b01c7715SBarry Smith       x[i2+3] = idiag[3]*x1 + idiag[8]*x2 + idiag[13]*x3 + idiag[18]*x4 + idiag[23]*x5;
649b01c7715SBarry Smith       x[i2+4] = idiag[4]*x1 + idiag[9]*x2 + idiag[14]*x3 + idiag[19]*x4 + idiag[24]*x5;
650b01c7715SBarry Smith       idiag -= 25;
651b01c7715SBarry Smith       i2    -= 5;
652b01c7715SBarry Smith       for (i=m-2; i>=0; i--) {
653b01c7715SBarry Smith 	v     = aa + 25*(diag[i]+1);
654b01c7715SBarry Smith 	vi    = aj + diag[i] + 1;
655b01c7715SBarry Smith 	nz    = ai[i+1] - diag[i] - 1;
656b01c7715SBarry Smith 	s1    = x[i2]; s2 = x[i2+1]; s3 = x[i2+2]; s4 = x[i2+3]; s5 = x[i2+4];
657b01c7715SBarry Smith 	while (nz--) {
658b01c7715SBarry Smith 	  idx  = 5*(*vi++);
659b01c7715SBarry Smith 	  x1   = x[idx]; x2 = x[1+idx]; x3 = x[2+idx]; x4 = x[3+idx]; x5 = x[4+idx];
660b01c7715SBarry Smith 	  s1  -= v[0]*x1 + v[5]*x2 + v[10]*x3 + v[15]*x4 + v[20]*x5;
661b01c7715SBarry Smith 	  s2  -= v[1]*x1 + v[6]*x2 + v[11]*x3 + v[16]*x4 + v[21]*x5;
662b01c7715SBarry Smith 	  s3  -= v[2]*x1 + v[7]*x2 + v[12]*x3 + v[17]*x4 + v[22]*x5;
663b01c7715SBarry Smith 	  s4  -= v[3]*x1 + v[8]*x2 + v[13]*x3 + v[18]*x4 + v[23]*x5;
664b01c7715SBarry Smith 	  s5  -= v[4]*x1 + v[9]*x2 + v[14]*x3 + v[19]*x4 + v[24]*x5;
665b01c7715SBarry Smith 	  v   += 25;
666b01c7715SBarry Smith 	}
667b01c7715SBarry Smith 	x[i2]   = idiag[0]*s1 + idiag[5]*s2 + idiag[10]*s3 + idiag[15]*s4 + idiag[20]*s5;
668b01c7715SBarry Smith 	x[i2+1] = idiag[1]*s1 + idiag[6]*s2 + idiag[11]*s3 + idiag[16]*s4 + idiag[21]*s5;
669b01c7715SBarry Smith 	x[i2+2] = idiag[2]*s1 + idiag[7]*s2 + idiag[12]*s3 + idiag[17]*s4 + idiag[22]*s5;
670b01c7715SBarry Smith 	x[i2+3] = idiag[3]*s1 + idiag[8]*s2 + idiag[13]*s3 + idiag[18]*s4 + idiag[23]*s5;
671b01c7715SBarry Smith 	x[i2+4] = idiag[4]*s1 + idiag[9]*s2 + idiag[14]*s3 + idiag[19]*s4 + idiag[24]*s5;
672b01c7715SBarry Smith         idiag   -= 25;
673b01c7715SBarry Smith         i2      -= 5;
674b01c7715SBarry Smith       }
675dc0b31edSSatish Balay       ierr = PetscLogFlops(25.0*(a->nz));CHKERRQ(ierr);
676b01c7715SBarry Smith     }
677b01c7715SBarry Smith   } else {
678e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supports point block SOR with zero initial guess");
679b01c7715SBarry Smith   }
6801ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
6813649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
682b01c7715SBarry Smith   PetscFunctionReturn(0);
683b01c7715SBarry Smith }
684b01c7715SBarry Smith 
6856d3beeddSMatthew Knepley #undef __FUNCT__
68641f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqBAIJ_6"
68741f059aeSBarry Smith PetscErrorCode MatSOR_SeqBAIJ_6(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
6886d3beeddSMatthew Knepley {
6896d3beeddSMatthew Knepley   Mat_SeqBAIJ        *a = (Mat_SeqBAIJ*)A->data;
6906d3beeddSMatthew Knepley   PetscScalar        *x,x1,x2,x3,x4,x5,x6,s1,s2,s3,s4,s5,s6;
691dd6ea824SBarry Smith   const MatScalar    *v,*aa = a->a, *idiag,*mdiag;
692dd6ea824SBarry Smith   const PetscScalar  *b;
6936d3beeddSMatthew Knepley   PetscErrorCode     ierr;
6946d3beeddSMatthew Knepley   PetscInt           m = a->mbs,i,i2,nz,idx;
6956d3beeddSMatthew Knepley   const PetscInt     *diag,*ai = a->i,*aj = a->j,*vi;
6966d3beeddSMatthew Knepley 
6976d3beeddSMatthew Knepley   PetscFunctionBegin;
698e32f2f54SBarry Smith   if (flag & SOR_EISENSTAT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for Eisenstat");
6996d3beeddSMatthew Knepley   its = its*lits;
700e32f2f54SBarry Smith   if (its <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
701e32f2f54SBarry Smith   if (fshift) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for diagonal shift");
702e32f2f54SBarry Smith   if (omega != 1.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for non-trivial relaxation factor");
703e32f2f54SBarry Smith   if ((flag & SOR_APPLY_UPPER) || (flag & SOR_APPLY_LOWER)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for applying upper or lower triangular parts");
704e32f2f54SBarry Smith   if (its > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support yet for multiple point block SOR iterations");
7056d3beeddSMatthew Knepley 
7066d3beeddSMatthew Knepley   if (!a->idiagvalid){ierr = MatInvertBlockDiagonal_SeqBAIJ(A);CHKERRQ(ierr);}
7076d3beeddSMatthew Knepley 
7086d3beeddSMatthew Knepley   diag  = a->diag;
7096d3beeddSMatthew Knepley   idiag = a->idiag;
7106d3beeddSMatthew Knepley   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
7113649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
7126d3beeddSMatthew Knepley 
7136d3beeddSMatthew Knepley   if (flag & SOR_ZERO_INITIAL_GUESS) {
7146d3beeddSMatthew Knepley     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
7156d3beeddSMatthew Knepley       x[0] = b[0]*idiag[0] + b[1]*idiag[6]  + b[2]*idiag[12] + b[3]*idiag[18] + b[4]*idiag[24] + b[5]*idiag[30];
7166d3beeddSMatthew Knepley       x[1] = b[0]*idiag[1] + b[1]*idiag[7]  + b[2]*idiag[13] + b[3]*idiag[19] + b[4]*idiag[25] + b[5]*idiag[31];
7176d3beeddSMatthew Knepley       x[2] = b[0]*idiag[2] + b[1]*idiag[8]  + b[2]*idiag[14] + b[3]*idiag[20] + b[4]*idiag[26] + b[5]*idiag[32];
7186d3beeddSMatthew Knepley       x[3] = b[0]*idiag[3] + b[1]*idiag[9]  + b[2]*idiag[15] + b[3]*idiag[21] + b[4]*idiag[27] + b[5]*idiag[33];
7196d3beeddSMatthew Knepley       x[4] = b[0]*idiag[4] + b[1]*idiag[10] + b[2]*idiag[16] + b[3]*idiag[22] + b[4]*idiag[28] + b[5]*idiag[34];
7206d3beeddSMatthew Knepley       x[5] = b[0]*idiag[5] + b[1]*idiag[11] + b[2]*idiag[17] + b[3]*idiag[23] + b[4]*idiag[29] + b[5]*idiag[35];
7216d3beeddSMatthew Knepley       i2     = 6;
7226d3beeddSMatthew Knepley       idiag += 36;
7236d3beeddSMatthew Knepley       for (i=1; i<m; i++) {
7246d3beeddSMatthew Knepley         v     = aa + 36*ai[i];
7256d3beeddSMatthew Knepley         vi    = aj + ai[i];
7266d3beeddSMatthew Knepley         nz    = diag[i] - ai[i];
7276d3beeddSMatthew Knepley         s1    = b[i2]; s2 = b[i2+1]; s3 = b[i2+2]; s4 = b[i2+3]; s5 = b[i2+4]; s6 = b[i2+5];
7286d3beeddSMatthew Knepley         while (nz--) {
7296d3beeddSMatthew Knepley           idx  = 6*(*vi++);
7306d3beeddSMatthew Knepley           x1   = x[idx]; x2 = x[1+idx]; x3 = x[2+idx]; x4 = x[3+idx]; x5 = x[4+idx]; x6 = x[5+idx];
7316d3beeddSMatthew Knepley           s1  -= v[0]*x1 + v[6]*x2  + v[12]*x3 + v[18]*x4 + v[24]*x5 + v[30]*x6;
7326d3beeddSMatthew Knepley           s2  -= v[1]*x1 + v[7]*x2  + v[13]*x3 + v[19]*x4 + v[25]*x5 + v[31]*x6;
7336d3beeddSMatthew Knepley           s3  -= v[2]*x1 + v[8]*x2  + v[14]*x3 + v[20]*x4 + v[26]*x5 + v[32]*x6;
7346d3beeddSMatthew Knepley           s4  -= v[3]*x1 + v[9]*x2  + v[15]*x3 + v[21]*x4 + v[27]*x5 + v[33]*x6;
7356d3beeddSMatthew Knepley           s5  -= v[4]*x1 + v[10]*x2 + v[16]*x3 + v[22]*x4 + v[28]*x5 + v[34]*x6;
7366d3beeddSMatthew Knepley           s6  -= v[5]*x1 + v[11]*x2 + v[17]*x3 + v[23]*x4 + v[29]*x5 + v[35]*x6;
7376d3beeddSMatthew Knepley           v   += 36;
7386d3beeddSMatthew Knepley         }
7396d3beeddSMatthew Knepley         x[i2]   = idiag[0]*s1 + idiag[6]*s2  + idiag[12]*s3 + idiag[18]*s4 + idiag[24]*s5 + idiag[30]*s6;
7406d3beeddSMatthew Knepley         x[i2+1] = idiag[1]*s1 + idiag[7]*s2  + idiag[13]*s3 + idiag[19]*s4 + idiag[25]*s5 + idiag[31]*s6;
7416d3beeddSMatthew Knepley         x[i2+2] = idiag[2]*s1 + idiag[8]*s2  + idiag[14]*s3 + idiag[20]*s4 + idiag[26]*s5 + idiag[32]*s6;
7426d3beeddSMatthew Knepley         x[i2+3] = idiag[3]*s1 + idiag[9]*s2  + idiag[15]*s3 + idiag[21]*s4 + idiag[27]*s5 + idiag[33]*s6;
7436d3beeddSMatthew Knepley         x[i2+4] = idiag[4]*s1 + idiag[10]*s2 + idiag[16]*s3 + idiag[22]*s4 + idiag[28]*s5 + idiag[34]*s6;
7446d3beeddSMatthew Knepley         x[i2+5] = idiag[5]*s1 + idiag[11]*s2 + idiag[17]*s3 + idiag[23]*s4 + idiag[29]*s5 + idiag[35]*s6;
7456d3beeddSMatthew Knepley         idiag   += 36;
7466d3beeddSMatthew Knepley         i2      += 6;
7476d3beeddSMatthew Knepley       }
7486d3beeddSMatthew Knepley       /* for logging purposes assume number of nonzero in lower half is 1/2 of total */
749dc0b31edSSatish Balay       ierr = PetscLogFlops(36.0*(a->nz));CHKERRQ(ierr);
7506d3beeddSMatthew Knepley     }
7516d3beeddSMatthew Knepley     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
7526d3beeddSMatthew Knepley         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
7536d3beeddSMatthew Knepley       i2    = 0;
7546d3beeddSMatthew Knepley       mdiag = a->idiag+36*a->mbs;
7556d3beeddSMatthew Knepley       for (i=0; i<m; i++) {
7566d3beeddSMatthew Knepley         x1      = x[i2]; x2 = x[i2+1]; x3 = x[i2+2]; x4 = x[i2+3]; x5 = x[i2+4]; x6 = x[i2+5];
7576d3beeddSMatthew Knepley         x[i2]   = mdiag[0]*x1 + mdiag[6]*x2  + mdiag[12]*x3 + mdiag[18]*x4 + mdiag[24]*x5 + mdiag[30]*x6;
7586d3beeddSMatthew Knepley         x[i2+1] = mdiag[1]*x1 + mdiag[7]*x2  + mdiag[13]*x3 + mdiag[19]*x4 + mdiag[25]*x5 + mdiag[31]*x6;
7596d3beeddSMatthew Knepley         x[i2+2] = mdiag[2]*x1 + mdiag[8]*x2  + mdiag[14]*x3 + mdiag[20]*x4 + mdiag[26]*x5 + mdiag[32]*x6;
7606d3beeddSMatthew Knepley         x[i2+3] = mdiag[3]*x1 + mdiag[9]*x2  + mdiag[15]*x3 + mdiag[21]*x4 + mdiag[27]*x5 + mdiag[33]*x6;
7616d3beeddSMatthew Knepley         x[i2+4] = mdiag[4]*x1 + mdiag[10]*x2 + mdiag[16]*x3 + mdiag[22]*x4 + mdiag[28]*x5 + mdiag[34]*x6;
7626d3beeddSMatthew Knepley         x[i2+5] = mdiag[5]*x1 + mdiag[11]*x2 + mdiag[17]*x3 + mdiag[23]*x4 + mdiag[29]*x5 + mdiag[35]*x6;
7636d3beeddSMatthew Knepley         mdiag  += 36;
7646d3beeddSMatthew Knepley         i2     += 6;
7656d3beeddSMatthew Knepley       }
766dc0b31edSSatish Balay       ierr = PetscLogFlops(60.0*m);CHKERRQ(ierr);
7676d3beeddSMatthew Knepley     } else if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
768d0f46423SBarry Smith       ierr = PetscMemcpy(x,b,A->rmap->N*sizeof(PetscScalar));CHKERRQ(ierr);
7696d3beeddSMatthew Knepley     }
7706d3beeddSMatthew Knepley     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
7716d3beeddSMatthew Knepley       idiag   = a->idiag+36*a->mbs - 36;
7726d3beeddSMatthew Knepley       i2      = 6*m - 6;
7736d3beeddSMatthew Knepley       x1      = x[i2]; x2 = x[i2+1]; x3 = x[i2+2]; x4 = x[i2+3]; x5 = x[i2+4]; x6 = x[i2+5];
7746d3beeddSMatthew Knepley       x[i2]   = idiag[0]*x1 + idiag[6]*x2  + idiag[12]*x3 + idiag[18]*x4 + idiag[24]*x5 + idiag[30]*x6;
7756d3beeddSMatthew Knepley       x[i2+1] = idiag[1]*x1 + idiag[7]*x2  + idiag[13]*x3 + idiag[19]*x4 + idiag[25]*x5 + idiag[31]*x6;
7766d3beeddSMatthew Knepley       x[i2+2] = idiag[2]*x1 + idiag[8]*x2  + idiag[14]*x3 + idiag[20]*x4 + idiag[26]*x5 + idiag[32]*x6;
7776d3beeddSMatthew Knepley       x[i2+3] = idiag[3]*x1 + idiag[9]*x2  + idiag[15]*x3 + idiag[21]*x4 + idiag[27]*x5 + idiag[33]*x6;
7786d3beeddSMatthew Knepley       x[i2+4] = idiag[4]*x1 + idiag[10]*x2 + idiag[16]*x3 + idiag[22]*x4 + idiag[28]*x5 + idiag[34]*x6;
7796d3beeddSMatthew Knepley       x[i2+5] = idiag[5]*x1 + idiag[11]*x2 + idiag[17]*x3 + idiag[23]*x4 + idiag[29]*x5 + idiag[35]*x6;
7806d3beeddSMatthew Knepley       idiag -= 36;
7816d3beeddSMatthew Knepley       i2    -= 6;
7826d3beeddSMatthew Knepley       for (i=m-2; i>=0; i--) {
7836d3beeddSMatthew Knepley         v     = aa + 36*(diag[i]+1);
7846d3beeddSMatthew Knepley         vi    = aj + diag[i] + 1;
7856d3beeddSMatthew Knepley         nz    = ai[i+1] - diag[i] - 1;
7866d3beeddSMatthew Knepley         s1    = x[i2]; s2 = x[i2+1]; s3 = x[i2+2]; s4 = x[i2+3]; s5 = x[i2+4]; s6 = x[i2+5];
7876d3beeddSMatthew Knepley         while (nz--) {
7886d3beeddSMatthew Knepley           idx  = 6*(*vi++);
7896d3beeddSMatthew Knepley           x1   = x[idx]; x2 = x[1+idx]; x3 = x[2+idx]; x4 = x[3+idx]; x5 = x[4+idx]; x6 = x[5+idx];
7906d3beeddSMatthew Knepley           s1  -= v[0]*x1 + v[6]*x2  + v[12]*x3 + v[18]*x4 + v[24]*x5 + v[30]*x6;
7916d3beeddSMatthew Knepley           s2  -= v[1]*x1 + v[7]*x2  + v[13]*x3 + v[19]*x4 + v[25]*x5 + v[31]*x6;
7926d3beeddSMatthew Knepley           s3  -= v[2]*x1 + v[8]*x2  + v[14]*x3 + v[20]*x4 + v[26]*x5 + v[32]*x6;
7936d3beeddSMatthew Knepley           s4  -= v[3]*x1 + v[9]*x2  + v[15]*x3 + v[21]*x4 + v[27]*x5 + v[33]*x6;
7946d3beeddSMatthew Knepley           s5  -= v[4]*x1 + v[10]*x2 + v[16]*x3 + v[22]*x4 + v[28]*x5 + v[34]*x6;
7956d3beeddSMatthew Knepley           s6  -= v[5]*x1 + v[11]*x2 + v[17]*x3 + v[23]*x4 + v[29]*x5 + v[35]*x6;
7966d3beeddSMatthew Knepley           v   += 36;
7976d3beeddSMatthew Knepley         }
7986d3beeddSMatthew Knepley         x[i2]   = idiag[0]*s1 + idiag[6]*s2  + idiag[12]*s3 + idiag[18]*s4 + idiag[24]*s5 + idiag[30]*s6;
7996d3beeddSMatthew Knepley         x[i2+1] = idiag[1]*s1 + idiag[7]*s2  + idiag[13]*s3 + idiag[19]*s4 + idiag[25]*s5 + idiag[31]*s6;
8006d3beeddSMatthew Knepley         x[i2+2] = idiag[2]*s1 + idiag[8]*s2  + idiag[14]*s3 + idiag[20]*s4 + idiag[26]*s5 + idiag[32]*s6;
8016d3beeddSMatthew Knepley         x[i2+3] = idiag[3]*s1 + idiag[9]*s2  + idiag[15]*s3 + idiag[21]*s4 + idiag[27]*s5 + idiag[33]*s6;
8026d3beeddSMatthew Knepley         x[i2+4] = idiag[4]*s1 + idiag[10]*s2 + idiag[16]*s3 + idiag[22]*s4 + idiag[28]*s5 + idiag[34]*s6;
8036d3beeddSMatthew Knepley         x[i2+5] = idiag[5]*s1 + idiag[11]*s2 + idiag[17]*s3 + idiag[23]*s4 + idiag[29]*s5 + idiag[35]*s6;
8046d3beeddSMatthew Knepley         idiag   -= 36;
8056d3beeddSMatthew Knepley         i2      -= 6;
8066d3beeddSMatthew Knepley       }
807dc0b31edSSatish Balay       ierr = PetscLogFlops(36.0*(a->nz));CHKERRQ(ierr);
8086d3beeddSMatthew Knepley     }
8096d3beeddSMatthew Knepley   } else {
810e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supports point block SOR with zero initial guess");
8116d3beeddSMatthew Knepley   }
8126d3beeddSMatthew Knepley   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8133649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
8146d3beeddSMatthew Knepley   PetscFunctionReturn(0);
8156d3beeddSMatthew Knepley }
8166d3beeddSMatthew Knepley 
8176d3beeddSMatthew Knepley #undef __FUNCT__
81841f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqBAIJ_7"
81941f059aeSBarry Smith PetscErrorCode MatSOR_SeqBAIJ_7(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
8206d3beeddSMatthew Knepley {
8216d3beeddSMatthew Knepley   Mat_SeqBAIJ        *a = (Mat_SeqBAIJ*)A->data;
8226d3beeddSMatthew Knepley   PetscScalar        *x,x1,x2,x3,x4,x5,x6,x7,s1,s2,s3,s4,s5,s6,s7;
823dd6ea824SBarry Smith   const MatScalar    *v,*aa = a->a, *idiag,*mdiag;
824dd6ea824SBarry Smith   const PetscScalar  *b;
8256d3beeddSMatthew Knepley   PetscErrorCode     ierr;
8266d3beeddSMatthew Knepley   PetscInt           m = a->mbs,i,i2,nz,idx;
8276d3beeddSMatthew Knepley   const PetscInt     *diag,*ai = a->i,*aj = a->j,*vi;
8286d3beeddSMatthew Knepley 
8296d3beeddSMatthew Knepley   PetscFunctionBegin;
830e32f2f54SBarry Smith   if (flag & SOR_EISENSTAT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for Eisenstat");
8316d3beeddSMatthew Knepley   its = its*lits;
832e32f2f54SBarry Smith   if (its <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
833e32f2f54SBarry Smith   if (fshift) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for diagonal shift");
834e32f2f54SBarry Smith   if (omega != 1.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for non-trivial relaxation factor");
835e32f2f54SBarry Smith   if ((flag & SOR_APPLY_UPPER) || (flag & SOR_APPLY_LOWER)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support for applying upper or lower triangular parts");
836e32f2f54SBarry Smith   if (its > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sorry, no support yet for multiple point block SOR iterations");
8376d3beeddSMatthew Knepley 
8386d3beeddSMatthew Knepley   if (!a->idiagvalid){ierr = MatInvertBlockDiagonal_SeqBAIJ(A);CHKERRQ(ierr);}
8396d3beeddSMatthew Knepley 
8406d3beeddSMatthew Knepley   diag  = a->diag;
8416d3beeddSMatthew Knepley   idiag = a->idiag;
8426d3beeddSMatthew Knepley   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8433649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
8446d3beeddSMatthew Knepley 
8456d3beeddSMatthew Knepley   if (flag & SOR_ZERO_INITIAL_GUESS) {
8466d3beeddSMatthew Knepley     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
8476d3beeddSMatthew Knepley       x[0] = b[0]*idiag[0] + b[1]*idiag[7]  + b[2]*idiag[14] + b[3]*idiag[21] + b[4]*idiag[28] + b[5]*idiag[35] + b[6]*idiag[42];
8486d3beeddSMatthew Knepley       x[1] = b[0]*idiag[1] + b[1]*idiag[8]  + b[2]*idiag[15] + b[3]*idiag[22] + b[4]*idiag[29] + b[5]*idiag[36] + b[6]*idiag[43];
8496d3beeddSMatthew Knepley       x[2] = b[0]*idiag[2] + b[1]*idiag[9]  + b[2]*idiag[16] + b[3]*idiag[23] + b[4]*idiag[30] + b[5]*idiag[37] + b[6]*idiag[44];
8506d3beeddSMatthew Knepley       x[3] = b[0]*idiag[3] + b[1]*idiag[10] + b[2]*idiag[17] + b[3]*idiag[24] + b[4]*idiag[31] + b[5]*idiag[38] + b[6]*idiag[45];
8516d3beeddSMatthew Knepley       x[4] = b[0]*idiag[4] + b[1]*idiag[11] + b[2]*idiag[18] + b[3]*idiag[25] + b[4]*idiag[32] + b[5]*idiag[39] + b[6]*idiag[46];
8526d3beeddSMatthew Knepley       x[5] = b[0]*idiag[5] + b[1]*idiag[12] + b[2]*idiag[19] + b[3]*idiag[26] + b[4]*idiag[33] + b[5]*idiag[40] + b[6]*idiag[47];
8536d3beeddSMatthew Knepley       x[6] = b[0]*idiag[6] + b[1]*idiag[13] + b[2]*idiag[20] + b[3]*idiag[27] + b[4]*idiag[34] + b[5]*idiag[41] + b[6]*idiag[48];
8546d3beeddSMatthew Knepley       i2     = 7;
8556d3beeddSMatthew Knepley       idiag += 49;
8566d3beeddSMatthew Knepley       for (i=1; i<m; i++) {
8576d3beeddSMatthew Knepley         v     = aa + 49*ai[i];
8586d3beeddSMatthew Knepley         vi    = aj + ai[i];
8596d3beeddSMatthew Knepley         nz    = diag[i] - ai[i];
8606d3beeddSMatthew Knepley         s1    = b[i2]; s2 = b[i2+1]; s3 = b[i2+2]; s4 = b[i2+3]; s5 = b[i2+4]; s6 = b[i2+5]; s7 = b[i2+6];
8616d3beeddSMatthew Knepley         while (nz--) {
8626d3beeddSMatthew Knepley           idx  = 7*(*vi++);
8636d3beeddSMatthew Knepley           x1   = x[idx]; x2 = x[1+idx]; x3 = x[2+idx]; x4 = x[3+idx]; x5 = x[4+idx]; x6 = x[5+idx]; x7 = x[6+idx];
8646d3beeddSMatthew Knepley           s1  -= v[0]*x1 + v[7]*x2  + v[14]*x3 + v[21]*x4 + v[28]*x5 + v[35]*x6 + v[42]*x7;
8656d3beeddSMatthew Knepley           s2  -= v[1]*x1 + v[8]*x2  + v[15]*x3 + v[22]*x4 + v[29]*x5 + v[36]*x6 + v[43]*x7;
8666d3beeddSMatthew Knepley           s3  -= v[2]*x1 + v[9]*x2  + v[16]*x3 + v[23]*x4 + v[30]*x5 + v[37]*x6 + v[44]*x7;
8676d3beeddSMatthew Knepley           s4  -= v[3]*x1 + v[10]*x2 + v[17]*x3 + v[24]*x4 + v[31]*x5 + v[38]*x6 + v[45]*x7;
8686d3beeddSMatthew Knepley           s5  -= v[4]*x1 + v[11]*x2 + v[18]*x3 + v[25]*x4 + v[32]*x5 + v[39]*x6 + v[46]*x7;
8696d3beeddSMatthew Knepley           s6  -= v[5]*x1 + v[12]*x2 + v[19]*x3 + v[26]*x4 + v[33]*x5 + v[40]*x6 + v[47]*x7;
8706d3beeddSMatthew Knepley           s7  -= v[6]*x1 + v[13]*x2 + v[20]*x3 + v[27]*x4 + v[34]*x5 + v[41]*x6 + v[48]*x7;
8716d3beeddSMatthew Knepley           v   += 49;
8726d3beeddSMatthew Knepley         }
8736d3beeddSMatthew Knepley         x[i2]   = idiag[0]*s1 + idiag[7]*s2  + idiag[14]*s3 + idiag[21]*s4 + idiag[28]*s5 + idiag[35]*s6 + idiag[42]*s7;
8746d3beeddSMatthew Knepley         x[i2+1] = idiag[1]*s1 + idiag[8]*s2  + idiag[15]*s3 + idiag[22]*s4 + idiag[29]*s5 + idiag[36]*s6 + idiag[43]*s7;
8756d3beeddSMatthew Knepley         x[i2+2] = idiag[2]*s1 + idiag[9]*s2  + idiag[16]*s3 + idiag[23]*s4 + idiag[30]*s5 + idiag[37]*s6 + idiag[44]*s7;
8766d3beeddSMatthew Knepley         x[i2+3] = idiag[3]*s1 + idiag[10]*s2 + idiag[17]*s3 + idiag[24]*s4 + idiag[31]*s5 + idiag[38]*s6 + idiag[45]*s7;
8776d3beeddSMatthew Knepley         x[i2+4] = idiag[4]*s1 + idiag[11]*s2 + idiag[18]*s3 + idiag[25]*s4 + idiag[32]*s5 + idiag[39]*s6 + idiag[46]*s7;
8786d3beeddSMatthew Knepley         x[i2+5] = idiag[5]*s1 + idiag[12]*s2 + idiag[19]*s3 + idiag[26]*s4 + idiag[33]*s5 + idiag[40]*s6 + idiag[47]*s7;
8796d3beeddSMatthew Knepley         x[i2+6] = idiag[6]*s1 + idiag[13]*s2 + idiag[20]*s3 + idiag[27]*s4 + idiag[34]*s5 + idiag[41]*s6 + idiag[48]*s7;
8806d3beeddSMatthew Knepley         idiag   += 49;
8816d3beeddSMatthew Knepley         i2      += 7;
8826d3beeddSMatthew Knepley       }
8836d3beeddSMatthew Knepley       /* for logging purposes assume number of nonzero in lower half is 1/2 of total */
884dc0b31edSSatish Balay       ierr = PetscLogFlops(49.0*(a->nz));CHKERRQ(ierr);
8856d3beeddSMatthew Knepley     }
8866d3beeddSMatthew Knepley     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
8876d3beeddSMatthew Knepley         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
8886d3beeddSMatthew Knepley       i2    = 0;
8896d3beeddSMatthew Knepley       mdiag = a->idiag+49*a->mbs;
8906d3beeddSMatthew Knepley       for (i=0; i<m; i++) {
8916d3beeddSMatthew Knepley         x1      = x[i2]; x2 = x[i2+1]; x3 = x[i2+2]; x4 = x[i2+3]; x5 = x[i2+4]; x6 = x[i2+5]; x7 = x[i2+6];
8926d3beeddSMatthew Knepley         x[i2]   = mdiag[0]*x1 + mdiag[7]*x2  + mdiag[14]*x3 + mdiag[21]*x4 + mdiag[28]*x5 + mdiag[35]*x6 + mdiag[35]*x7;
8936d3beeddSMatthew Knepley         x[i2+1] = mdiag[1]*x1 + mdiag[8]*x2  + mdiag[15]*x3 + mdiag[22]*x4 + mdiag[29]*x5 + mdiag[36]*x6 + mdiag[36]*x7;
8946d3beeddSMatthew Knepley         x[i2+2] = mdiag[2]*x1 + mdiag[9]*x2  + mdiag[16]*x3 + mdiag[23]*x4 + mdiag[30]*x5 + mdiag[37]*x6 + mdiag[37]*x7;
8956d3beeddSMatthew Knepley         x[i2+3] = mdiag[3]*x1 + mdiag[10]*x2 + mdiag[17]*x3 + mdiag[24]*x4 + mdiag[31]*x5 + mdiag[38]*x6 + mdiag[38]*x7;
8966d3beeddSMatthew Knepley         x[i2+4] = mdiag[4]*x1 + mdiag[11]*x2 + mdiag[18]*x3 + mdiag[25]*x4 + mdiag[32]*x5 + mdiag[39]*x6 + mdiag[39]*x7;
8976d3beeddSMatthew Knepley         x[i2+5] = mdiag[5]*x1 + mdiag[12]*x2 + mdiag[19]*x3 + mdiag[26]*x4 + mdiag[33]*x5 + mdiag[40]*x6 + mdiag[40]*x7;
8986d3beeddSMatthew Knepley         x[i2+6] = mdiag[6]*x1 + mdiag[13]*x2 + mdiag[20]*x3 + mdiag[27]*x4 + mdiag[34]*x5 + mdiag[41]*x6 + mdiag[41]*x7;
8996d3beeddSMatthew Knepley         mdiag  += 36;
9006d3beeddSMatthew Knepley         i2     += 6;
9016d3beeddSMatthew Knepley       }
902dc0b31edSSatish Balay       ierr = PetscLogFlops(93.0*m);CHKERRQ(ierr);
9036d3beeddSMatthew Knepley     } else if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
904d0f46423SBarry Smith       ierr = PetscMemcpy(x,b,A->rmap->N*sizeof(PetscScalar));CHKERRQ(ierr);
9056d3beeddSMatthew Knepley     }
9066d3beeddSMatthew Knepley     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
9076d3beeddSMatthew Knepley       idiag   = a->idiag+49*a->mbs - 49;
9086d3beeddSMatthew Knepley       i2      = 7*m - 7;
9096d3beeddSMatthew Knepley       x1      = x[i2]; x2 = x[i2+1]; x3 = x[i2+2]; x4 = x[i2+3]; x5 = x[i2+4]; x6 = x[i2+5]; x7 = x[i2+6];
9106d3beeddSMatthew Knepley       x[i2]   = idiag[0]*x1 + idiag[7]*x2  + idiag[14]*x3 + idiag[21]*x4 + idiag[28]*x5 + idiag[35]*x6 + idiag[42]*x7;
9116d3beeddSMatthew Knepley       x[i2+1] = idiag[1]*x1 + idiag[8]*x2  + idiag[15]*x3 + idiag[22]*x4 + idiag[29]*x5 + idiag[36]*x6 + idiag[43]*x7;
9126d3beeddSMatthew Knepley       x[i2+2] = idiag[2]*x1 + idiag[9]*x2  + idiag[16]*x3 + idiag[23]*x4 + idiag[30]*x5 + idiag[37]*x6 + idiag[44]*x7;
9136d3beeddSMatthew Knepley       x[i2+3] = idiag[3]*x1 + idiag[10]*x2 + idiag[17]*x3 + idiag[24]*x4 + idiag[31]*x5 + idiag[38]*x6 + idiag[45]*x7;
9146d3beeddSMatthew Knepley       x[i2+4] = idiag[4]*x1 + idiag[11]*x2 + idiag[18]*x3 + idiag[25]*x4 + idiag[32]*x5 + idiag[39]*x6 + idiag[46]*x7;
9156d3beeddSMatthew Knepley       x[i2+5] = idiag[5]*x1 + idiag[12]*x2 + idiag[19]*x3 + idiag[26]*x4 + idiag[33]*x5 + idiag[40]*x6 + idiag[47]*x7;
9166d3beeddSMatthew Knepley       x[i2+6] = idiag[6]*x1 + idiag[13]*x2 + idiag[20]*x3 + idiag[27]*x4 + idiag[34]*x5 + idiag[41]*x6 + idiag[48]*x7;
9176d3beeddSMatthew Knepley       idiag -= 49;
9186d3beeddSMatthew Knepley       i2    -= 7;
9196d3beeddSMatthew Knepley       for (i=m-2; i>=0; i--) {
9206d3beeddSMatthew Knepley         v     = aa + 49*(diag[i]+1);
9216d3beeddSMatthew Knepley         vi    = aj + diag[i] + 1;
9226d3beeddSMatthew Knepley         nz    = ai[i+1] - diag[i] - 1;
9236d3beeddSMatthew Knepley         s1    = x[i2]; s2 = x[i2+1]; s3 = x[i2+2]; s4 = x[i2+3]; s5 = x[i2+4]; s6 = x[i2+5]; s7 = x[i2+6];
9246d3beeddSMatthew Knepley         while (nz--) {
9256d3beeddSMatthew Knepley           idx  = 7*(*vi++);
9266d3beeddSMatthew Knepley           x1   = x[idx]; x2 = x[1+idx]; x3 = x[2+idx]; x4 = x[3+idx]; x5 = x[4+idx]; x6 = x[5+idx]; x7 = x[6+idx];
9276d3beeddSMatthew Knepley           s1  -= v[0]*x1 + v[7]*x2  + v[14]*x3 + v[21]*x4 + v[28]*x5 + v[35]*x6 + v[42]*x7;
9286d3beeddSMatthew Knepley           s2  -= v[1]*x1 + v[8]*x2  + v[15]*x3 + v[22]*x4 + v[29]*x5 + v[36]*x6 + v[43]*x7;
9296d3beeddSMatthew Knepley           s3  -= v[2]*x1 + v[9]*x2  + v[16]*x3 + v[23]*x4 + v[30]*x5 + v[37]*x6 + v[44]*x7;
9306d3beeddSMatthew Knepley           s4  -= v[3]*x1 + v[10]*x2 + v[17]*x3 + v[24]*x4 + v[31]*x5 + v[38]*x6 + v[45]*x7;
9316d3beeddSMatthew Knepley           s5  -= v[4]*x1 + v[11]*x2 + v[18]*x3 + v[25]*x4 + v[32]*x5 + v[39]*x6 + v[46]*x7;
9326d3beeddSMatthew Knepley           s6  -= v[5]*x1 + v[12]*x2 + v[19]*x3 + v[26]*x4 + v[33]*x5 + v[40]*x6 + v[47]*x7;
9336d3beeddSMatthew Knepley           s7  -= v[6]*x1 + v[13]*x2 + v[20]*x3 + v[27]*x4 + v[34]*x5 + v[41]*x6 + v[48]*x7;
9346d3beeddSMatthew Knepley           v   += 49;
9356d3beeddSMatthew Knepley         }
9366d3beeddSMatthew Knepley         x[i2]   = idiag[0]*s1 + idiag[7]*s2  + idiag[14]*s3 + idiag[21]*s4 + idiag[28]*s5 + idiag[35]*s6 + idiag[42]*s7;
9376d3beeddSMatthew Knepley         x[i2+1] = idiag[1]*s1 + idiag[8]*s2  + idiag[15]*s3 + idiag[22]*s4 + idiag[29]*s5 + idiag[36]*s6 + idiag[43]*s7;
9386d3beeddSMatthew Knepley         x[i2+2] = idiag[2]*s1 + idiag[9]*s2  + idiag[16]*s3 + idiag[23]*s4 + idiag[30]*s5 + idiag[37]*s6 + idiag[44]*s7;
9396d3beeddSMatthew Knepley         x[i2+3] = idiag[3]*s1 + idiag[10]*s2 + idiag[17]*s3 + idiag[24]*s4 + idiag[31]*s5 + idiag[38]*s6 + idiag[45]*s7;
9406d3beeddSMatthew Knepley         x[i2+4] = idiag[4]*s1 + idiag[11]*s2 + idiag[18]*s3 + idiag[25]*s4 + idiag[32]*s5 + idiag[39]*s6 + idiag[46]*s7;
9416d3beeddSMatthew Knepley         x[i2+5] = idiag[5]*s1 + idiag[12]*s2 + idiag[19]*s3 + idiag[26]*s4 + idiag[33]*s5 + idiag[40]*s6 + idiag[47]*s7;
9426d3beeddSMatthew Knepley         x[i2+6] = idiag[6]*s1 + idiag[13]*s2 + idiag[20]*s3 + idiag[27]*s4 + idiag[34]*s5 + idiag[41]*s6 + idiag[48]*s7;
9436d3beeddSMatthew Knepley         idiag   -= 49;
9446d3beeddSMatthew Knepley         i2      -= 7;
9456d3beeddSMatthew Knepley       }
946dc0b31edSSatish Balay       ierr = PetscLogFlops(49.0*(a->nz));CHKERRQ(ierr);
9476d3beeddSMatthew Knepley     }
9486d3beeddSMatthew Knepley   } else {
949e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supports point block SOR with zero initial guess");
9506d3beeddSMatthew Knepley   }
9516d3beeddSMatthew Knepley   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
9523649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
9536d3beeddSMatthew Knepley   PetscFunctionReturn(0);
9546d3beeddSMatthew Knepley }
9556d3beeddSMatthew Knepley 
956af674e45SBarry Smith /*
95781824310SBarry Smith     Special version for direct calls from Fortran (Used in PETSc-fun3d)
958af674e45SBarry Smith */
959af674e45SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
960af674e45SBarry Smith #define matsetvaluesblocked4_ MATSETVALUESBLOCKED4
961af674e45SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
962af674e45SBarry Smith #define matsetvaluesblocked4_ matsetvaluesblocked4
963af674e45SBarry Smith #endif
964af674e45SBarry Smith 
9659c8c1041SBarry Smith EXTERN_C_BEGIN
966af674e45SBarry Smith #undef __FUNCT__
967af674e45SBarry Smith #define __FUNCT__ "matsetvaluesblocked4_"
968be1d678aSKris Buschelman void PETSCMAT_DLLEXPORT matsetvaluesblocked4_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[])
969af674e45SBarry Smith {
970af674e45SBarry Smith   Mat               A = *AA;
971af674e45SBarry Smith   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
972c1ac3661SBarry Smith   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,N,m = *mm,n = *nn;
973c1ac3661SBarry Smith   PetscInt          *ai=a->i,*ailen=a->ilen;
97417ec6a02SBarry Smith   PetscInt          *aj=a->j,stepval,lastcol = -1;
975f15d580aSBarry Smith   const PetscScalar *value = v;
9764bb09213Spetsc   MatScalar         *ap,*aa = a->a,*bap;
977af674e45SBarry Smith 
978af674e45SBarry Smith   PetscFunctionBegin;
979d0f46423SBarry Smith   if (A->rmap->bs != 4) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONG,"Can only be called with a block size of 4");
980af674e45SBarry Smith   stepval = (n-1)*4;
981af674e45SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
982af674e45SBarry Smith     row  = im[k];
983af674e45SBarry Smith     rp   = aj + ai[row];
984af674e45SBarry Smith     ap   = aa + 16*ai[row];
985af674e45SBarry Smith     nrow = ailen[row];
986af674e45SBarry Smith     low  = 0;
98717ec6a02SBarry Smith     high = nrow;
988af674e45SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
989af674e45SBarry Smith       col = in[l];
99017ec6a02SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
99117ec6a02SBarry Smith       lastcol = col;
9921e3347e8SBarry Smith       value = v + k*(stepval+4 + l)*4;
993af674e45SBarry Smith       while (high-low > 7) {
994af674e45SBarry Smith         t = (low+high)/2;
995af674e45SBarry Smith         if (rp[t] > col) high = t;
996af674e45SBarry Smith         else             low  = t;
997af674e45SBarry Smith       }
998af674e45SBarry Smith       for (i=low; i<high; i++) {
999af674e45SBarry Smith         if (rp[i] > col) break;
1000af674e45SBarry Smith         if (rp[i] == col) {
1001af674e45SBarry Smith           bap  = ap +  16*i;
1002af674e45SBarry Smith           for (ii=0; ii<4; ii++,value+=stepval) {
1003af674e45SBarry Smith             for (jj=ii; jj<16; jj+=4) {
1004af674e45SBarry Smith               bap[jj] += *value++;
1005af674e45SBarry Smith             }
1006af674e45SBarry Smith           }
1007af674e45SBarry Smith           goto noinsert2;
1008af674e45SBarry Smith         }
1009af674e45SBarry Smith       }
1010af674e45SBarry Smith       N = nrow++ - 1;
101117ec6a02SBarry Smith       high++; /* added new column index thus must search to one higher than before */
1012af674e45SBarry Smith       /* shift up all the later entries in this row */
1013af674e45SBarry Smith       for (ii=N; ii>=i; ii--) {
1014af674e45SBarry Smith         rp[ii+1] = rp[ii];
1015a037b02bSBarry Smith         PetscMemcpy(ap+16*(ii+1),ap+16*(ii),16*sizeof(MatScalar));
1016af674e45SBarry Smith       }
1017af674e45SBarry Smith       if (N >= i) {
1018a037b02bSBarry Smith         PetscMemzero(ap+16*i,16*sizeof(MatScalar));
1019af674e45SBarry Smith       }
1020af674e45SBarry Smith       rp[i] = col;
1021af674e45SBarry Smith       bap   = ap +  16*i;
1022af674e45SBarry Smith       for (ii=0; ii<4; ii++,value+=stepval) {
1023af674e45SBarry Smith         for (jj=ii; jj<16; jj+=4) {
1024af674e45SBarry Smith           bap[jj] = *value++;
1025af674e45SBarry Smith         }
1026af674e45SBarry Smith       }
1027af674e45SBarry Smith       noinsert2:;
1028af674e45SBarry Smith       low = i;
1029af674e45SBarry Smith     }
1030af674e45SBarry Smith     ailen[row] = nrow;
1031af674e45SBarry Smith   }
1032be1d678aSKris Buschelman   PetscFunctionReturnVoid();
1033af674e45SBarry Smith }
10349c8c1041SBarry Smith EXTERN_C_END
1035af674e45SBarry Smith 
1036af674e45SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
1037af674e45SBarry Smith #define matsetvalues4_ MATSETVALUES4
1038af674e45SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
1039af674e45SBarry Smith #define matsetvalues4_ matsetvalues4
1040af674e45SBarry Smith #endif
1041af674e45SBarry Smith 
10429c8c1041SBarry Smith EXTERN_C_BEGIN
1043af674e45SBarry Smith #undef __FUNCT__
1044af674e45SBarry Smith #define __FUNCT__ "MatSetValues4_"
1045be1d678aSKris Buschelman void PETSCMAT_DLLEXPORT matsetvalues4_(Mat *AA,PetscInt *mm,PetscInt *im,PetscInt *nn,PetscInt *in,PetscScalar *v)
1046af674e45SBarry Smith {
1047af674e45SBarry Smith   Mat         A = *AA;
1048af674e45SBarry Smith   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
1049c1ac3661SBarry Smith   PetscInt    *rp,k,low,high,t,ii,row,nrow,i,col,l,N,n = *nn,m = *mm;
1050c1ac3661SBarry Smith   PetscInt    *ai=a->i,*ailen=a->ilen;
1051c1ac3661SBarry Smith   PetscInt    *aj=a->j,brow,bcol;
105217ec6a02SBarry Smith   PetscInt    ridx,cidx,lastcol = -1;
1053af674e45SBarry Smith   MatScalar   *ap,value,*aa=a->a,*bap;
1054af674e45SBarry Smith 
1055af674e45SBarry Smith   PetscFunctionBegin;
1056af674e45SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
1057af674e45SBarry Smith     row  = im[k]; brow = row/4;
1058af674e45SBarry Smith     rp   = aj + ai[brow];
1059af674e45SBarry Smith     ap   = aa + 16*ai[brow];
1060af674e45SBarry Smith     nrow = ailen[brow];
1061af674e45SBarry Smith     low  = 0;
106217ec6a02SBarry Smith     high = nrow;
1063af674e45SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1064af674e45SBarry Smith       col = in[l]; bcol = col/4;
1065af674e45SBarry Smith       ridx = row % 4; cidx = col % 4;
1066af674e45SBarry Smith       value = v[l + k*n];
106717ec6a02SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
106817ec6a02SBarry Smith       lastcol = col;
1069af674e45SBarry Smith       while (high-low > 7) {
1070af674e45SBarry Smith         t = (low+high)/2;
1071af674e45SBarry Smith         if (rp[t] > bcol) high = t;
1072af674e45SBarry Smith         else              low  = t;
1073af674e45SBarry Smith       }
1074af674e45SBarry Smith       for (i=low; i<high; i++) {
1075af674e45SBarry Smith         if (rp[i] > bcol) break;
1076af674e45SBarry Smith         if (rp[i] == bcol) {
1077af674e45SBarry Smith           bap  = ap +  16*i + 4*cidx + ridx;
1078af674e45SBarry Smith           *bap += value;
1079af674e45SBarry Smith           goto noinsert1;
1080af674e45SBarry Smith         }
1081af674e45SBarry Smith       }
1082af674e45SBarry Smith       N = nrow++ - 1;
108317ec6a02SBarry Smith       high++; /* added new column thus must search to one higher than before */
1084af674e45SBarry Smith       /* shift up all the later entries in this row */
1085af674e45SBarry Smith       for (ii=N; ii>=i; ii--) {
1086af674e45SBarry Smith         rp[ii+1] = rp[ii];
1087a037b02bSBarry Smith         PetscMemcpy(ap+16*(ii+1),ap+16*(ii),16*sizeof(MatScalar));
1088af674e45SBarry Smith       }
1089af674e45SBarry Smith       if (N>=i) {
1090a037b02bSBarry Smith         PetscMemzero(ap+16*i,16*sizeof(MatScalar));
1091af674e45SBarry Smith       }
1092af674e45SBarry Smith       rp[i]                    = bcol;
1093af674e45SBarry Smith       ap[16*i + 4*cidx + ridx] = value;
1094af674e45SBarry Smith       noinsert1:;
1095af674e45SBarry Smith       low = i;
1096af674e45SBarry Smith     }
1097af674e45SBarry Smith     ailen[brow] = nrow;
1098af674e45SBarry Smith   }
1099be1d678aSKris Buschelman   PetscFunctionReturnVoid();
1100af674e45SBarry Smith }
11019c8c1041SBarry Smith EXTERN_C_END
1102af674e45SBarry Smith 
1103be5855fcSBarry Smith /*
1104be5855fcSBarry Smith      Checks for missing diagonals
1105be5855fcSBarry Smith */
11064a2ae208SSatish Balay #undef __FUNCT__
11074a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqBAIJ"
11082af78befSBarry Smith PetscErrorCode MatMissingDiagonal_SeqBAIJ(Mat A,PetscTruth *missing,PetscInt *d)
1109be5855fcSBarry Smith {
1110be5855fcSBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
11116849ba73SBarry Smith   PetscErrorCode ierr;
1112c1ac3661SBarry Smith   PetscInt       *diag,*jj = a->j,i;
1113be5855fcSBarry Smith 
1114be5855fcSBarry Smith   PetscFunctionBegin;
1115c4992f7dSBarry Smith   ierr = MatMarkDiagonal_SeqBAIJ(A);CHKERRQ(ierr);
11162af78befSBarry Smith   *missing = PETSC_FALSE;
11172efa7f71SHong Zhang   if (A->rmap->n > 0 && !jj) {
11182efa7f71SHong Zhang     *missing  = PETSC_TRUE;
11192efa7f71SHong Zhang     if (d) *d = 0;
11202efa7f71SHong Zhang     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
11212efa7f71SHong Zhang   } else {
1122883fce79SBarry Smith     diag     = a->diag;
11230e8e8aceSBarry Smith     for (i=0; i<a->mbs; i++) {
1124be5855fcSBarry Smith       if (jj[diag[i]] != i) {
11252af78befSBarry Smith         *missing  = PETSC_TRUE;
11262af78befSBarry Smith         if (d) *d = i;
11272efa7f71SHong Zhang         PetscInfo1(A,"Matrix is missing block diagonal number %D",i);
11282efa7f71SHong Zhang       }
1129be5855fcSBarry Smith     }
1130be5855fcSBarry Smith   }
1131be5855fcSBarry Smith   PetscFunctionReturn(0);
1132be5855fcSBarry Smith }
1133be5855fcSBarry Smith 
11344a2ae208SSatish Balay #undef __FUNCT__
11354a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqBAIJ"
1136dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqBAIJ(Mat A)
1137de6a44a3SBarry Smith {
1138de6a44a3SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
11396849ba73SBarry Smith   PetscErrorCode ierr;
114009f38230SBarry Smith   PetscInt       i,j,m = a->mbs;
1141de6a44a3SBarry Smith 
11423a40ed3dSBarry Smith   PetscFunctionBegin;
114309f38230SBarry Smith   if (!a->diag) {
114409f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
11454fd072dbSBarry Smith     ierr = PetscLogObjectMemory(A,m*sizeof(PetscInt));CHKERRQ(ierr);
11464fd072dbSBarry Smith     a->free_diag = PETSC_TRUE;
114709f38230SBarry Smith   }
11487fc0212eSBarry Smith   for (i=0; i<m; i++) {
114909f38230SBarry Smith     a->diag[i] = a->i[i+1];
1150de6a44a3SBarry Smith     for (j=a->i[i]; j<a->i[i+1]; j++) {
1151de6a44a3SBarry Smith       if (a->j[j] == i) {
115209f38230SBarry Smith         a->diag[i] = j;
1153de6a44a3SBarry Smith         break;
1154de6a44a3SBarry Smith       }
1155de6a44a3SBarry Smith     }
1156de6a44a3SBarry Smith   }
11573a40ed3dSBarry Smith   PetscFunctionReturn(0);
1158de6a44a3SBarry Smith }
11592593348eSBarry Smith 
11602593348eSBarry Smith 
1161690b6cddSBarry Smith EXTERN PetscErrorCode MatToSymmetricIJ_SeqAIJ(PetscInt,PetscInt*,PetscInt*,PetscInt,PetscInt,PetscInt**,PetscInt**);
11623b2fbd54SBarry Smith 
11634a2ae208SSatish Balay #undef __FUNCT__
11644a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqBAIJ"
11658f7157efSSatish Balay static PetscErrorCode MatGetRowIJ_SeqBAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
11663b2fbd54SBarry Smith {
11673b2fbd54SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
1168dfbe8321SBarry Smith   PetscErrorCode ierr;
1169d0f46423SBarry Smith   PetscInt       i,j,n = a->mbs,nz = a->i[n],bs = A->rmap->bs,nbs = 1,k,l,cnt;
11708f7157efSSatish Balay   PetscInt       *tia, *tja;
11713b2fbd54SBarry Smith 
11723a40ed3dSBarry Smith   PetscFunctionBegin;
11733b2fbd54SBarry Smith   *nn = n;
11743a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
11753b2fbd54SBarry Smith   if (symmetric) {
11768f7157efSSatish Balay     ierr = MatToSymmetricIJ_SeqAIJ(n,a->i,a->j,0,0,&tia,&tja);CHKERRQ(ierr);
1177553b3c51SBarry Smith     nz   = tia[n];
11783b2fbd54SBarry Smith   } else {
11798f7157efSSatish Balay     tia = a->i; tja = a->j;
11803b2fbd54SBarry Smith   }
11813b2fbd54SBarry Smith 
1182ecc77c7aSBarry Smith   if (!blockcompressed && bs > 1) {
1183ecc77c7aSBarry Smith     (*nn) *= bs;
1184ecc77c7aSBarry Smith     nbs    = bs;
11858f7157efSSatish Balay     /* malloc & create the natural set of indices */
1186ecc77c7aSBarry Smith     ierr = PetscMalloc((n+1)*bs*sizeof(PetscInt),ia);CHKERRQ(ierr);
11879985e31cSBarry Smith     if (n) {
1188ecc77c7aSBarry Smith       (*ia)[0] = 0;
1189ecc77c7aSBarry Smith       for (j=1; j<bs; j++) {
1190ecc77c7aSBarry Smith         (*ia)[j] = (tia[1]-tia[0])*bs+(*ia)[j-1];
1191ecc77c7aSBarry Smith       }
11929985e31cSBarry Smith     }
1193ecc77c7aSBarry Smith 
1194ecc77c7aSBarry Smith     for (i=1; i<n; i++) {
1195ecc77c7aSBarry Smith       (*ia)[i*bs] = (tia[i]-tia[i-1])*bs + (*ia)[i*bs-1];
1196ecc77c7aSBarry Smith       for (j=1; j<bs; j++) {
1197ecc77c7aSBarry Smith         (*ia)[i*bs+j] = (tia[i+1]-tia[i])*bs + (*ia)[i*bs+j-1];
11988f7157efSSatish Balay       }
11998f7157efSSatish Balay     }
12009985e31cSBarry Smith     if (n) {
1201ecc77c7aSBarry Smith       (*ia)[n*bs] = (tia[n]-tia[n-1])*bs + (*ia)[n*bs-1];
12029985e31cSBarry Smith     }
1203ecc77c7aSBarry Smith 
12049985e31cSBarry Smith     if (ja) {
12059985e31cSBarry Smith       ierr = PetscMalloc(nz*bs*bs*sizeof(PetscInt),ja);CHKERRQ(ierr);
12069985e31cSBarry Smith       cnt = 0;
12079985e31cSBarry Smith       for (i=0; i<n; i++) {
12089985e31cSBarry Smith         for (j=0; j<bs; j++) {
12099985e31cSBarry Smith           for (k=tia[i]; k<tia[i+1]; k++) {
12109985e31cSBarry Smith             for (l=0; l<bs; l++) {
12119985e31cSBarry Smith               (*ja)[cnt++] = bs*tja[k] + l;
12129985e31cSBarry Smith 	    }
12139985e31cSBarry Smith           }
12149985e31cSBarry Smith         }
12159985e31cSBarry Smith       }
12169985e31cSBarry Smith     }
12179985e31cSBarry Smith 
1218ecc77c7aSBarry Smith     n     *= bs;
1219ecc77c7aSBarry Smith     nz *= bs*bs;
12208f7157efSSatish Balay     if (symmetric) { /* deallocate memory allocated in MatToSymmetricIJ_SeqAIJ() */
12218f7157efSSatish Balay       ierr = PetscFree(tia);CHKERRQ(ierr);
12228f7157efSSatish Balay       ierr = PetscFree(tja);CHKERRQ(ierr);
12238f7157efSSatish Balay     }
1224f6d58c54SBarry Smith   } else if (oshift == 1) {
1225715a17b5SBarry Smith     if (symmetric) {
1226715a17b5SBarry Smith       PetscInt nz = tia[A->rmap->n/bs];
1227715a17b5SBarry Smith       /*  add 1 to i and j indices */
1228715a17b5SBarry Smith       for (i=0; i<A->rmap->n/bs+1; i++) tia[i] = tia[i] + 1;
1229715a17b5SBarry Smith       *ia = tia;
1230715a17b5SBarry Smith       if (ja) {
1231715a17b5SBarry Smith 	for (i=0; i<nz; i++) tja[i] = tja[i] + 1;
1232715a17b5SBarry Smith         *ja = tja;
1233715a17b5SBarry Smith       }
1234715a17b5SBarry Smith     } else {
1235f6d58c54SBarry Smith       PetscInt nz = a->i[A->rmap->n/bs];
1236f6d58c54SBarry Smith       /* malloc space and  add 1 to i and j indices */
1237f6d58c54SBarry Smith       ierr = PetscMalloc((A->rmap->n/bs+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
1238f6d58c54SBarry Smith       for (i=0; i<A->rmap->n/bs+1; i++) (*ia)[i] = a->i[i] + 1;
1239f6d58c54SBarry Smith       if (ja) {
1240f6d58c54SBarry Smith 	ierr = PetscMalloc(nz*sizeof(PetscInt),ja);CHKERRQ(ierr);
1241f6d58c54SBarry Smith 	for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
1242f6d58c54SBarry Smith       }
1243715a17b5SBarry Smith     }
12448f7157efSSatish Balay   } else {
12458f7157efSSatish Balay     *ia = tia;
1246ecc77c7aSBarry Smith     if (ja) *ja = tja;
12478f7157efSSatish Balay   }
1248f6d58c54SBarry Smith 
12493a40ed3dSBarry Smith   PetscFunctionReturn(0);
12503b2fbd54SBarry Smith }
12513b2fbd54SBarry Smith 
12524a2ae208SSatish Balay #undef __FUNCT__
12534a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqBAIJ"
12548f7157efSSatish Balay static PetscErrorCode MatRestoreRowIJ_SeqBAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
12553b2fbd54SBarry Smith {
12566849ba73SBarry Smith   PetscErrorCode ierr;
12573b2fbd54SBarry Smith 
12583a40ed3dSBarry Smith   PetscFunctionBegin;
12593a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1260715a17b5SBarry Smith   if ((!blockcompressed && A->rmap->bs > 1) || (symmetric || oshift == 1)) {
1261606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
12629985e31cSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
12633b2fbd54SBarry Smith   }
12643a40ed3dSBarry Smith   PetscFunctionReturn(0);
12653b2fbd54SBarry Smith }
12663b2fbd54SBarry Smith 
12674a2ae208SSatish Balay #undef __FUNCT__
12684a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqBAIJ"
1269dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqBAIJ(Mat A)
12702d61bbb3SSatish Balay {
12712d61bbb3SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
1272dfbe8321SBarry Smith   PetscErrorCode ierr;
12732d61bbb3SSatish Balay 
1274433994e6SBarry Smith   PetscFunctionBegin;
1275aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1276d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->N,A->cmap->n,a->nz);
12772d61bbb3SSatish Balay #endif
1278e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
1279c38d4ed2SBarry Smith   if (a->row) {
1280c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
1281c38d4ed2SBarry Smith   }
1282c38d4ed2SBarry Smith   if (a->col) {
1283c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
1284c38d4ed2SBarry Smith   }
12854fd072dbSBarry Smith   if (a->free_diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
128605b42c5fSBarry Smith   ierr = PetscFree(a->idiag);CHKERRQ(ierr);
12874fd072dbSBarry Smith   if (a->free_imax_ilen) {ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);}
128805b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
128905b42c5fSBarry Smith   ierr = PetscFree(a->mult_work);CHKERRQ(ierr);
1290e51c0b9cSSatish Balay   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
129105b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
129205b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
12930e83c824SBarry Smith   if (a->compressedrow.use){ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);}
1294c4319e64SHong Zhang 
129570e08fbdSBarry Smith   if (a->sbaijMat) {ierr = MatDestroy(a->sbaijMat);CHKERRQ(ierr);}
12964fd072dbSBarry Smith   if (a->parent) {ierr = MatDestroy(a->parent);CHKERRQ(ierr);}
1297606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
1298901853e0SKris Buschelman 
1299dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
130043516a2dSKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJInvertBlockDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
1301901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
1302901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
1303901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
1304901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqbaij_seqaij_C","",PETSC_NULL);CHKERRQ(ierr);
1305901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqbaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
1306901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
1307725b52f3SLisandro Dalcin   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
13082d61bbb3SSatish Balay   PetscFunctionReturn(0);
13092d61bbb3SSatish Balay }
13102d61bbb3SSatish Balay 
13114a2ae208SSatish Balay #undef __FUNCT__
13124a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqBAIJ"
13134e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqBAIJ(Mat A,MatOption op,PetscTruth flg)
13142d61bbb3SSatish Balay {
13152d61bbb3SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
131663ba0a88SBarry Smith   PetscErrorCode ierr;
13172d61bbb3SSatish Balay 
13182d61bbb3SSatish Balay   PetscFunctionBegin;
1319aa275fccSKris Buschelman   switch (op) {
1320aa275fccSKris Buschelman   case MAT_ROW_ORIENTED:
13214e0d8c25SBarry Smith     a->roworiented    = flg;
1322aa275fccSKris Buschelman     break;
1323a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1324a9817697SBarry Smith     a->keepnonzeropattern = flg;
1325aa275fccSKris Buschelman     break;
1326512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1327512a5fc5SBarry Smith     a->nonew          = (flg ? 0 : 1);
1328aa275fccSKris Buschelman     break;
1329aa275fccSKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
13304e0d8c25SBarry Smith     a->nonew          = (flg ? -1 : 0);
1331aa275fccSKris Buschelman     break;
1332aa275fccSKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
13334e0d8c25SBarry Smith     a->nonew          = (flg ? -2 : 0);
1334aa275fccSKris Buschelman     break;
133528b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
133628b2fa4aSMatthew Knepley     a->nounused       = (flg ? -1 : 0);
133728b2fa4aSMatthew Knepley     break;
13384e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1339aa275fccSKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1340aa275fccSKris Buschelman   case MAT_USE_HASH_TABLE:
1341290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1342aa275fccSKris Buschelman     break;
134377e54ba9SKris Buschelman   case MAT_SYMMETRIC:
134477e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
13459a4540c5SBarry Smith   case MAT_HERMITIAN:
13469a4540c5SBarry Smith   case MAT_SYMMETRY_ETERNAL:
1347290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
134877e54ba9SKris Buschelman     break;
1349aa275fccSKris Buschelman   default:
1350e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
13512d61bbb3SSatish Balay   }
13522d61bbb3SSatish Balay   PetscFunctionReturn(0);
13532d61bbb3SSatish Balay }
13542d61bbb3SSatish Balay 
13554a2ae208SSatish Balay #undef __FUNCT__
13564a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqBAIJ"
1357c1ac3661SBarry Smith PetscErrorCode MatGetRow_SeqBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
13582d61bbb3SSatish Balay {
13592d61bbb3SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
13606849ba73SBarry Smith   PetscErrorCode ierr;
1361c1ac3661SBarry Smith   PetscInt       itmp,i,j,k,M,*ai,*aj,bs,bn,bp,*idx_i,bs2;
13623f1db9ecSBarry Smith   MatScalar      *aa,*aa_i;
136387828ca2SBarry Smith   PetscScalar    *v_i;
13642d61bbb3SSatish Balay 
13652d61bbb3SSatish Balay   PetscFunctionBegin;
1366d0f46423SBarry Smith   bs  = A->rmap->bs;
13672d61bbb3SSatish Balay   ai  = a->i;
13682d61bbb3SSatish Balay   aj  = a->j;
13692d61bbb3SSatish Balay   aa  = a->a;
13702d61bbb3SSatish Balay   bs2 = a->bs2;
13712d61bbb3SSatish Balay 
1372e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range", row);
13732d61bbb3SSatish Balay 
13742d61bbb3SSatish Balay   bn  = row/bs;   /* Block number */
13752d61bbb3SSatish Balay   bp  = row % bs; /* Block Position */
13762d61bbb3SSatish Balay   M   = ai[bn+1] - ai[bn];
13772d61bbb3SSatish Balay   *nz = bs*M;
13782d61bbb3SSatish Balay 
13792d61bbb3SSatish Balay   if (v) {
13802d61bbb3SSatish Balay     *v = 0;
13812d61bbb3SSatish Balay     if (*nz) {
138287828ca2SBarry Smith       ierr = PetscMalloc((*nz)*sizeof(PetscScalar),v);CHKERRQ(ierr);
13832d61bbb3SSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
13842d61bbb3SSatish Balay         v_i  = *v + i*bs;
13852d61bbb3SSatish Balay         aa_i = aa + bs2*(ai[bn] + i);
13862d61bbb3SSatish Balay         for (j=bp,k=0; j<bs2; j+=bs,k++) {v_i[k] = aa_i[j];}
13872d61bbb3SSatish Balay       }
13882d61bbb3SSatish Balay     }
13892d61bbb3SSatish Balay   }
13902d61bbb3SSatish Balay 
13912d61bbb3SSatish Balay   if (idx) {
13922d61bbb3SSatish Balay     *idx = 0;
13932d61bbb3SSatish Balay     if (*nz) {
1394c1ac3661SBarry Smith       ierr = PetscMalloc((*nz)*sizeof(PetscInt),idx);CHKERRQ(ierr);
13952d61bbb3SSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
13962d61bbb3SSatish Balay         idx_i = *idx + i*bs;
13972d61bbb3SSatish Balay         itmp  = bs*aj[ai[bn] + i];
13982d61bbb3SSatish Balay         for (j=0; j<bs; j++) {idx_i[j] = itmp++;}
13992d61bbb3SSatish Balay       }
14002d61bbb3SSatish Balay     }
14012d61bbb3SSatish Balay   }
14022d61bbb3SSatish Balay   PetscFunctionReturn(0);
14032d61bbb3SSatish Balay }
14042d61bbb3SSatish Balay 
14054a2ae208SSatish Balay #undef __FUNCT__
14064a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqBAIJ"
1407c1ac3661SBarry Smith PetscErrorCode MatRestoreRow_SeqBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
14082d61bbb3SSatish Balay {
1409dfbe8321SBarry Smith   PetscErrorCode ierr;
1410606d414cSSatish Balay 
14112d61bbb3SSatish Balay   PetscFunctionBegin;
141205b42c5fSBarry Smith   if (idx) {ierr = PetscFree(*idx);CHKERRQ(ierr);}
141305b42c5fSBarry Smith   if (v)   {ierr = PetscFree(*v);CHKERRQ(ierr);}
14142d61bbb3SSatish Balay   PetscFunctionReturn(0);
14152d61bbb3SSatish Balay }
14162d61bbb3SSatish Balay 
1417fca92195SBarry Smith extern PetscErrorCode MatSetValues_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
1418fca92195SBarry Smith 
14194a2ae208SSatish Balay #undef __FUNCT__
14204a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqBAIJ"
1421fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqBAIJ(Mat A,MatReuse reuse,Mat *B)
14222d61bbb3SSatish Balay {
14232d61bbb3SSatish Balay   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
14242d61bbb3SSatish Balay   Mat            C;
14256849ba73SBarry Smith   PetscErrorCode ierr;
1426d0f46423SBarry Smith   PetscInt       i,j,k,*aj=a->j,*ai=a->i,bs=A->rmap->bs,mbs=a->mbs,nbs=a->nbs,len,*col;
1427c1ac3661SBarry Smith   PetscInt       *rows,*cols,bs2=a->bs2;
1428dd6ea824SBarry Smith   MatScalar      *array;
14292d61bbb3SSatish Balay 
14302d61bbb3SSatish Balay   PetscFunctionBegin;
1431e32f2f54SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && mbs != nbs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1432fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || A == *B) {
1433c1ac3661SBarry Smith     ierr = PetscMalloc((1+nbs)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1434c1ac3661SBarry Smith     ierr = PetscMemzero(col,(1+nbs)*sizeof(PetscInt));CHKERRQ(ierr);
14352d61bbb3SSatish Balay 
14362d61bbb3SSatish Balay     for (i=0; i<ai[mbs]; i++) col[aj[i]] += 1;
14377adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1438d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,A->rmap->N,A->cmap->n,A->rmap->N);CHKERRQ(ierr);
14397adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1440ab93d7beSBarry Smith     ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(C,bs,PETSC_NULL,col);CHKERRQ(ierr);
1441606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1442fc4dec0aSBarry Smith   } else {
1443fc4dec0aSBarry Smith     C = *B;
1444fc4dec0aSBarry Smith   }
1445fc4dec0aSBarry Smith 
1446fc4dec0aSBarry Smith   array = a->a;
1447fca92195SBarry Smith   ierr = PetscMalloc2(bs,PetscInt,&rows,bs,PetscInt,&cols);CHKERRQ(ierr);
14482d61bbb3SSatish Balay   for (i=0; i<mbs; i++) {
14492d61bbb3SSatish Balay     cols[0] = i*bs;
14502d61bbb3SSatish Balay     for (k=1; k<bs; k++) cols[k] = cols[k-1] + 1;
14512d61bbb3SSatish Balay     len = ai[i+1] - ai[i];
14522d61bbb3SSatish Balay     for (j=0; j<len; j++) {
14532d61bbb3SSatish Balay       rows[0] = (*aj++)*bs;
14542d61bbb3SSatish Balay       for (k=1; k<bs; k++) rows[k] = rows[k-1] + 1;
1455fca92195SBarry Smith       ierr = MatSetValues_SeqBAIJ(C,bs,rows,bs,cols,array,INSERT_VALUES);CHKERRQ(ierr);
14562d61bbb3SSatish Balay       array += bs2;
14572d61bbb3SSatish Balay     }
14582d61bbb3SSatish Balay   }
1459fca92195SBarry Smith   ierr = PetscFree2(rows,cols);CHKERRQ(ierr);
14602d61bbb3SSatish Balay 
14612d61bbb3SSatish Balay   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14622d61bbb3SSatish Balay   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14632d61bbb3SSatish Balay 
1464815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
14652d61bbb3SSatish Balay     *B = C;
14662d61bbb3SSatish Balay   } else {
1467273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
14682d61bbb3SSatish Balay   }
14692d61bbb3SSatish Balay   PetscFunctionReturn(0);
14702d61bbb3SSatish Balay }
14712d61bbb3SSatish Balay 
14724a2ae208SSatish Balay #undef __FUNCT__
14734a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqBAIJ_Binary"
14746849ba73SBarry Smith static PetscErrorCode MatView_SeqBAIJ_Binary(Mat A,PetscViewer viewer)
14752593348eSBarry Smith {
1476b6490206SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
14776849ba73SBarry Smith   PetscErrorCode ierr;
1478d0f46423SBarry Smith   PetscInt       i,*col_lens,bs = A->rmap->bs,count,*jj,j,k,l,bs2=a->bs2;
1479b24ad042SBarry Smith   int            fd;
148087828ca2SBarry Smith   PetscScalar    *aa;
1481ce6f0cecSBarry Smith   FILE           *file;
14822593348eSBarry Smith 
14833a40ed3dSBarry Smith   PetscFunctionBegin;
1484b0a32e0cSBarry Smith   ierr        = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
1485d0f46423SBarry Smith   ierr        = PetscMalloc((4+A->rmap->N)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
14860700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
14873b2fbd54SBarry Smith 
1488d0f46423SBarry Smith   col_lens[1] = A->rmap->N;
1489d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
14907e67e3f9SSatish Balay   col_lens[3] = a->nz*bs2;
14912593348eSBarry Smith 
14922593348eSBarry Smith   /* store lengths of each row and write (including header) to file */
1493b6490206SBarry Smith   count = 0;
1494b6490206SBarry Smith   for (i=0; i<a->mbs; i++) {
1495b6490206SBarry Smith     for (j=0; j<bs; j++) {
1496b6490206SBarry Smith       col_lens[4+count++] = bs*(a->i[i+1] - a->i[i]);
1497b6490206SBarry Smith     }
14982593348eSBarry Smith   }
1499d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->N,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1500606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
15012593348eSBarry Smith 
15022593348eSBarry Smith   /* store column indices (zero start index) */
1503c1ac3661SBarry Smith   ierr  = PetscMalloc((a->nz+1)*bs2*sizeof(PetscInt),&jj);CHKERRQ(ierr);
1504b6490206SBarry Smith   count = 0;
1505b6490206SBarry Smith   for (i=0; i<a->mbs; i++) {
1506b6490206SBarry Smith     for (j=0; j<bs; j++) {
1507b6490206SBarry Smith       for (k=a->i[i]; k<a->i[i+1]; k++) {
1508b6490206SBarry Smith         for (l=0; l<bs; l++) {
1509b6490206SBarry Smith           jj[count++] = bs*a->j[k] + l;
15102593348eSBarry Smith         }
15112593348eSBarry Smith       }
1512b6490206SBarry Smith     }
1513b6490206SBarry Smith   }
15146f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,jj,bs2*a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
1515606d414cSSatish Balay   ierr = PetscFree(jj);CHKERRQ(ierr);
15162593348eSBarry Smith 
15172593348eSBarry Smith   /* store nonzero values */
151887828ca2SBarry Smith   ierr  = PetscMalloc((a->nz+1)*bs2*sizeof(PetscScalar),&aa);CHKERRQ(ierr);
1519b6490206SBarry Smith   count = 0;
1520b6490206SBarry Smith   for (i=0; i<a->mbs; i++) {
1521b6490206SBarry Smith     for (j=0; j<bs; j++) {
1522b6490206SBarry Smith       for (k=a->i[i]; k<a->i[i+1]; k++) {
1523b6490206SBarry Smith         for (l=0; l<bs; l++) {
15247e67e3f9SSatish Balay           aa[count++] = a->a[bs2*k + l*bs + j];
1525b6490206SBarry Smith         }
1526b6490206SBarry Smith       }
1527b6490206SBarry Smith     }
1528b6490206SBarry Smith   }
15296f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,aa,bs2*a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
1530606d414cSSatish Balay   ierr = PetscFree(aa);CHKERRQ(ierr);
1531ce6f0cecSBarry Smith 
1532b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
1533ce6f0cecSBarry Smith   if (file) {
1534d0f46423SBarry Smith     fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
1535ce6f0cecSBarry Smith   }
15363a40ed3dSBarry Smith   PetscFunctionReturn(0);
15372593348eSBarry Smith }
15382593348eSBarry Smith 
15394a2ae208SSatish Balay #undef __FUNCT__
15404a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqBAIJ_ASCII"
15416849ba73SBarry Smith static PetscErrorCode MatView_SeqBAIJ_ASCII(Mat A,PetscViewer viewer)
15422593348eSBarry Smith {
1543b6490206SBarry Smith   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
1544dfbe8321SBarry Smith   PetscErrorCode    ierr;
1545d0f46423SBarry Smith   PetscInt          i,j,bs = A->rmap->bs,k,l,bs2=a->bs2;
1546f3ef73ceSBarry Smith   PetscViewerFormat format;
15472593348eSBarry Smith 
15483a40ed3dSBarry Smith   PetscFunctionBegin;
1549b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
1550456192e2SBarry Smith   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
155177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);CHKERRQ(ierr);
1552fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
1553bcd9e38bSBarry Smith     Mat aij;
1554ceb03754SKris Buschelman     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&aij);CHKERRQ(ierr);
1555bcd9e38bSBarry Smith     ierr = MatView(aij,viewer);CHKERRQ(ierr);
1556bcd9e38bSBarry Smith     ierr = MatDestroy(aij);CHKERRQ(ierr);
155704929863SHong Zhang   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
155804929863SHong Zhang      PetscFunctionReturn(0);
1559fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
1560b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
156144cd7ae7SLois Curfman McInnes     for (i=0; i<a->mbs; i++) {
156244cd7ae7SLois Curfman McInnes       for (j=0; j<bs; j++) {
156377431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
156444cd7ae7SLois Curfman McInnes         for (k=a->i[i]; k<a->i[i+1]; k++) {
156544cd7ae7SLois Curfman McInnes           for (l=0; l<bs; l++) {
1566aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
15670e6d2581SBarry Smith             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
1568a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %Gi) ",bs*a->j[k]+l,
15690e6d2581SBarry Smith                       PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
15700e6d2581SBarry Smith             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
1571a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %Gi) ",bs*a->j[k]+l,
15720e6d2581SBarry Smith                       PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
15730e6d2581SBarry Smith             } else if (PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
1574a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
15750ef38995SBarry Smith             }
157644cd7ae7SLois Curfman McInnes #else
15770ef38995SBarry Smith             if (a->a[bs2*k + l*bs + j] != 0.0) {
1578a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
15790ef38995SBarry Smith             }
158044cd7ae7SLois Curfman McInnes #endif
158144cd7ae7SLois Curfman McInnes           }
158244cd7ae7SLois Curfman McInnes         }
1583b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
158444cd7ae7SLois Curfman McInnes       }
158544cd7ae7SLois Curfman McInnes     }
1586b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
15870ef38995SBarry Smith   } else {
1588b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
1589b6490206SBarry Smith     for (i=0; i<a->mbs; i++) {
1590b6490206SBarry Smith       for (j=0; j<bs; j++) {
159177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
1592b6490206SBarry Smith         for (k=a->i[i]; k<a->i[i+1]; k++) {
1593b6490206SBarry Smith           for (l=0; l<bs; l++) {
1594aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
15950e6d2581SBarry Smith             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0) {
1596a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k]+l,
15970e6d2581SBarry Smith                 PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
15980e6d2581SBarry Smith             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0) {
1599a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k]+l,
16000e6d2581SBarry Smith                 PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
16010ef38995SBarry Smith             } else {
1602a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
160388685aaeSLois Curfman McInnes             }
160488685aaeSLois Curfman McInnes #else
1605a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
160688685aaeSLois Curfman McInnes #endif
16072593348eSBarry Smith           }
16082593348eSBarry Smith         }
1609b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
16102593348eSBarry Smith       }
16112593348eSBarry Smith     }
1612b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
1613b6490206SBarry Smith   }
1614b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
16153a40ed3dSBarry Smith   PetscFunctionReturn(0);
16162593348eSBarry Smith }
16172593348eSBarry Smith 
16184a2ae208SSatish Balay #undef __FUNCT__
16194a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqBAIJ_Draw_Zoom"
16206849ba73SBarry Smith static PetscErrorCode MatView_SeqBAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
16213270192aSSatish Balay {
162277ed5343SBarry Smith   Mat            A = (Mat) Aa;
16233270192aSSatish Balay   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ*)A->data;
16246849ba73SBarry Smith   PetscErrorCode ierr;
1625d0f46423SBarry Smith   PetscInt       row,i,j,k,l,mbs=a->mbs,color,bs=A->rmap->bs,bs2=a->bs2;
16260e6d2581SBarry Smith   PetscReal      xl,yl,xr,yr,x_l,x_r,y_l,y_r;
16273f1db9ecSBarry Smith   MatScalar      *aa;
1628b0a32e0cSBarry Smith   PetscViewer    viewer;
16293270192aSSatish Balay 
16303a40ed3dSBarry Smith   PetscFunctionBegin;
16313270192aSSatish Balay 
1632b65db4caSBarry Smith   /* still need to add support for contour plot of nonzeros; see MatView_SeqAIJ_Draw_Zoom()*/
163377ed5343SBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
163477ed5343SBarry Smith 
1635b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
163677ed5343SBarry Smith 
16373270192aSSatish Balay   /* loop over matrix elements drawing boxes */
1638b0a32e0cSBarry Smith   color = PETSC_DRAW_BLUE;
16393270192aSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
16403270192aSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
1641d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
16423270192aSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
16433270192aSSatish Balay       aa = a->a + j*bs2;
16443270192aSSatish Balay       for (k=0; k<bs; k++) {
16453270192aSSatish Balay         for (l=0; l<bs; l++) {
16460e6d2581SBarry Smith           if (PetscRealPart(*aa++) >=  0.) continue;
1647b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
16483270192aSSatish Balay         }
16493270192aSSatish Balay       }
16503270192aSSatish Balay     }
16513270192aSSatish Balay   }
1652b0a32e0cSBarry Smith   color = PETSC_DRAW_CYAN;
16533270192aSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
16543270192aSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
1655d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
16563270192aSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
16573270192aSSatish Balay       aa = a->a + j*bs2;
16583270192aSSatish Balay       for (k=0; k<bs; k++) {
16593270192aSSatish Balay         for (l=0; l<bs; l++) {
16600e6d2581SBarry Smith           if (PetscRealPart(*aa++) != 0.) continue;
1661b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
16623270192aSSatish Balay         }
16633270192aSSatish Balay       }
16643270192aSSatish Balay     }
16653270192aSSatish Balay   }
16663270192aSSatish Balay 
1667b0a32e0cSBarry Smith   color = PETSC_DRAW_RED;
16683270192aSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
16693270192aSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
1670d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
16713270192aSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
16723270192aSSatish Balay       aa = a->a + j*bs2;
16733270192aSSatish Balay       for (k=0; k<bs; k++) {
16743270192aSSatish Balay         for (l=0; l<bs; l++) {
16750e6d2581SBarry Smith           if (PetscRealPart(*aa++) <= 0.) continue;
1676b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
16773270192aSSatish Balay         }
16783270192aSSatish Balay       }
16793270192aSSatish Balay     }
16803270192aSSatish Balay   }
168177ed5343SBarry Smith   PetscFunctionReturn(0);
168277ed5343SBarry Smith }
16833270192aSSatish Balay 
16844a2ae208SSatish Balay #undef __FUNCT__
16854a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqBAIJ_Draw"
16866849ba73SBarry Smith static PetscErrorCode MatView_SeqBAIJ_Draw(Mat A,PetscViewer viewer)
168777ed5343SBarry Smith {
1688dfbe8321SBarry Smith   PetscErrorCode ierr;
16890e6d2581SBarry Smith   PetscReal      xl,yl,xr,yr,w,h;
1690b0a32e0cSBarry Smith   PetscDraw      draw;
169177ed5343SBarry Smith   PetscTruth     isnull;
16923270192aSSatish Balay 
169377ed5343SBarry Smith   PetscFunctionBegin;
169477ed5343SBarry Smith 
1695b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
1696b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
169777ed5343SBarry Smith 
169877ed5343SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
1699d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->N; h = yr/10.0; w = xr/10.0;
170077ed5343SBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
1701b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
1702b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqBAIJ_Draw_Zoom,A);CHKERRQ(ierr);
170377ed5343SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
17043a40ed3dSBarry Smith   PetscFunctionReturn(0);
17053270192aSSatish Balay }
17063270192aSSatish Balay 
17074a2ae208SSatish Balay #undef __FUNCT__
17084a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqBAIJ"
1709dfbe8321SBarry Smith PetscErrorCode MatView_SeqBAIJ(Mat A,PetscViewer viewer)
17102593348eSBarry Smith {
1711dfbe8321SBarry Smith   PetscErrorCode ierr;
171232077d6dSBarry Smith   PetscTruth     iascii,isbinary,isdraw;
17132593348eSBarry Smith 
17143a40ed3dSBarry Smith   PetscFunctionBegin;
17152692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
17162692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
17172692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
171832077d6dSBarry Smith   if (iascii){
17193a40ed3dSBarry Smith     ierr = MatView_SeqBAIJ_ASCII(A,viewer);CHKERRQ(ierr);
17200f5bd95cSBarry Smith   } else if (isbinary) {
17213a40ed3dSBarry Smith     ierr = MatView_SeqBAIJ_Binary(A,viewer);CHKERRQ(ierr);
17220f5bd95cSBarry Smith   } else if (isdraw) {
17233a40ed3dSBarry Smith     ierr = MatView_SeqBAIJ_Draw(A,viewer);CHKERRQ(ierr);
17245cd90555SBarry Smith   } else {
1725a5e6ed63SBarry Smith     Mat B;
1726ceb03754SKris Buschelman     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);CHKERRQ(ierr);
1727a5e6ed63SBarry Smith     ierr = MatView(B,viewer);CHKERRQ(ierr);
1728a5e6ed63SBarry Smith     ierr = MatDestroy(B);CHKERRQ(ierr);
17292593348eSBarry Smith   }
17303a40ed3dSBarry Smith   PetscFunctionReturn(0);
17312593348eSBarry Smith }
1732b6490206SBarry Smith 
1733cd0e1443SSatish Balay 
17344a2ae208SSatish Balay #undef __FUNCT__
17354a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqBAIJ"
1736c1ac3661SBarry Smith PetscErrorCode MatGetValues_SeqBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
1737cd0e1443SSatish Balay {
1738cd0e1443SSatish Balay   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
1739c1ac3661SBarry Smith   PetscInt    *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
1740c1ac3661SBarry Smith   PetscInt    *ai = a->i,*ailen = a->ilen;
1741d0f46423SBarry Smith   PetscInt    brow,bcol,ridx,cidx,bs=A->rmap->bs,bs2=a->bs2;
174297e567efSBarry Smith   MatScalar   *ap,*aa = a->a;
1743cd0e1443SSatish Balay 
17443a40ed3dSBarry Smith   PetscFunctionBegin;
17452d61bbb3SSatish Balay   for (k=0; k<m; k++) { /* loop over rows */
1746cd0e1443SSatish Balay     row  = im[k]; brow = row/bs;
1747e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row"); */
1748e32f2f54SBarry Smith     if (row >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D too large", row);
17492d61bbb3SSatish Balay     rp   = aj + ai[brow] ; ap = aa + bs2*ai[brow] ;
17502c3acbe9SBarry Smith     nrow = ailen[brow];
17512d61bbb3SSatish Balay     for (l=0; l<n; l++) { /* loop over columns */
1752e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column"); */
1753e32f2f54SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column %D too large", in[l]);
17542d61bbb3SSatish Balay       col  = in[l] ;
17552d61bbb3SSatish Balay       bcol = col/bs;
17562d61bbb3SSatish Balay       cidx = col%bs;
17572d61bbb3SSatish Balay       ridx = row%bs;
17582d61bbb3SSatish Balay       high = nrow;
17592d61bbb3SSatish Balay       low  = 0; /* assume unsorted */
17602d61bbb3SSatish Balay       while (high-low > 5) {
1761cd0e1443SSatish Balay         t = (low+high)/2;
1762cd0e1443SSatish Balay         if (rp[t] > bcol) high = t;
1763cd0e1443SSatish Balay         else             low  = t;
1764cd0e1443SSatish Balay       }
1765cd0e1443SSatish Balay       for (i=low; i<high; i++) {
1766cd0e1443SSatish Balay         if (rp[i] > bcol) break;
1767cd0e1443SSatish Balay         if (rp[i] == bcol) {
17682d61bbb3SSatish Balay           *v++ = ap[bs2*i+bs*cidx+ridx];
17692d61bbb3SSatish Balay           goto finished;
1770cd0e1443SSatish Balay         }
1771cd0e1443SSatish Balay       }
177297e567efSBarry Smith       *v++ = 0.0;
17732d61bbb3SSatish Balay       finished:;
1774cd0e1443SSatish Balay     }
1775cd0e1443SSatish Balay   }
17763a40ed3dSBarry Smith   PetscFunctionReturn(0);
1777cd0e1443SSatish Balay }
1778cd0e1443SSatish Balay 
17794a2ae208SSatish Balay #undef __FUNCT__
17804a2ae208SSatish Balay #define __FUNCT__ "MatSetValuesBlocked_SeqBAIJ"
1781dd6ea824SBarry Smith PetscErrorCode MatSetValuesBlocked_SeqBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
178292c4ed94SBarry Smith {
178392c4ed94SBarry Smith   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
1784e2ee6c50SBarry Smith   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,rmax,N,lastcol = -1;
1785c1ac3661SBarry Smith   PetscInt          *imax=a->imax,*ai=a->i,*ailen=a->ilen;
17866849ba73SBarry Smith   PetscErrorCode    ierr;
1787d0f46423SBarry Smith   PetscInt          *aj=a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs,stepval;
1788273d9f13SBarry Smith   PetscTruth        roworiented=a->roworiented;
1789dd6ea824SBarry Smith   const PetscScalar *value = v;
1790f15d580aSBarry Smith   MatScalar         *ap,*aa = a->a,*bap;
179192c4ed94SBarry Smith 
17923a40ed3dSBarry Smith   PetscFunctionBegin;
17930e324ae4SSatish Balay   if (roworiented) {
17940e324ae4SSatish Balay     stepval = (n-1)*bs;
17950e324ae4SSatish Balay   } else {
17960e324ae4SSatish Balay     stepval = (m-1)*bs;
17970e324ae4SSatish Balay   }
179892c4ed94SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
179992c4ed94SBarry Smith     row  = im[k];
18005ef9f2a5SBarry Smith     if (row < 0) continue;
18012515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
1802e32f2f54SBarry Smith     if (row >= a->mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,a->mbs-1);
180392c4ed94SBarry Smith #endif
180492c4ed94SBarry Smith     rp   = aj + ai[row];
180592c4ed94SBarry Smith     ap   = aa + bs2*ai[row];
180692c4ed94SBarry Smith     rmax = imax[row];
180792c4ed94SBarry Smith     nrow = ailen[row];
180892c4ed94SBarry Smith     low  = 0;
1809c71e6ed7SBarry Smith     high = nrow;
181092c4ed94SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
18115ef9f2a5SBarry Smith       if (in[l] < 0) continue;
18122515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
1813e32f2f54SBarry Smith       if (in[l] >= a->nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],a->nbs-1);
181492c4ed94SBarry Smith #endif
181592c4ed94SBarry Smith       col = in[l];
181692c4ed94SBarry Smith       if (roworiented) {
18170e324ae4SSatish Balay         value = v + k*(stepval+bs)*bs + l*bs;
18180e324ae4SSatish Balay       } else {
18190e324ae4SSatish Balay         value = v + l*(stepval+bs)*bs + k*bs;
182092c4ed94SBarry Smith       }
18217cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
1822e2ee6c50SBarry Smith       lastcol = col;
182392c4ed94SBarry Smith       while (high-low > 7) {
182492c4ed94SBarry Smith         t = (low+high)/2;
182592c4ed94SBarry Smith         if (rp[t] > col) high = t;
182692c4ed94SBarry Smith         else             low  = t;
182792c4ed94SBarry Smith       }
182892c4ed94SBarry Smith       for (i=low; i<high; i++) {
182992c4ed94SBarry Smith         if (rp[i] > col) break;
183092c4ed94SBarry Smith         if (rp[i] == col) {
18318a84c255SSatish Balay           bap  = ap +  bs2*i;
18320e324ae4SSatish Balay           if (roworiented) {
18338a84c255SSatish Balay             if (is == ADD_VALUES) {
1834dd9472c6SBarry Smith               for (ii=0; ii<bs; ii++,value+=stepval) {
1835dd9472c6SBarry Smith                 for (jj=ii; jj<bs2; jj+=bs) {
18368a84c255SSatish Balay                   bap[jj] += *value++;
1837dd9472c6SBarry Smith                 }
1838dd9472c6SBarry Smith               }
18390e324ae4SSatish Balay             } else {
1840dd9472c6SBarry Smith               for (ii=0; ii<bs; ii++,value+=stepval) {
1841dd9472c6SBarry Smith                 for (jj=ii; jj<bs2; jj+=bs) {
18420e324ae4SSatish Balay                   bap[jj] = *value++;
18438a84c255SSatish Balay                 }
1844dd9472c6SBarry Smith               }
1845dd9472c6SBarry Smith             }
18460e324ae4SSatish Balay           } else {
18470e324ae4SSatish Balay             if (is == ADD_VALUES) {
1848dd9472c6SBarry Smith               for (ii=0; ii<bs; ii++,value+=stepval) {
1849dd9472c6SBarry Smith                 for (jj=0; jj<bs; jj++) {
18500e324ae4SSatish Balay                   *bap++ += *value++;
1851dd9472c6SBarry Smith                 }
1852dd9472c6SBarry Smith               }
18530e324ae4SSatish Balay             } else {
1854dd9472c6SBarry Smith               for (ii=0; ii<bs; ii++,value+=stepval) {
1855dd9472c6SBarry Smith                 for (jj=0; jj<bs; jj++) {
18560e324ae4SSatish Balay                   *bap++  = *value++;
18570e324ae4SSatish Balay                 }
18588a84c255SSatish Balay               }
1859dd9472c6SBarry Smith             }
1860dd9472c6SBarry Smith           }
1861f1241b54SBarry Smith           goto noinsert2;
186292c4ed94SBarry Smith         }
186392c4ed94SBarry Smith       }
186489280ab3SLois Curfman McInnes       if (nonew == 1) goto noinsert2;
1865e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
1866fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
1867c03d1d03SSatish Balay       N = nrow++ - 1; high++;
186892c4ed94SBarry Smith       /* shift up all the later entries in this row */
186992c4ed94SBarry Smith       for (ii=N; ii>=i; ii--) {
187092c4ed94SBarry Smith         rp[ii+1] = rp[ii];
1871549d3d68SSatish Balay         ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
187292c4ed94SBarry Smith       }
1873549d3d68SSatish Balay       if (N >= i) {
1874549d3d68SSatish Balay         ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1875549d3d68SSatish Balay       }
187692c4ed94SBarry Smith       rp[i] = col;
18778a84c255SSatish Balay       bap   = ap +  bs2*i;
18780e324ae4SSatish Balay       if (roworiented) {
1879dd9472c6SBarry Smith         for (ii=0; ii<bs; ii++,value+=stepval) {
1880dd9472c6SBarry Smith           for (jj=ii; jj<bs2; jj+=bs) {
18810e324ae4SSatish Balay             bap[jj] = *value++;
1882dd9472c6SBarry Smith           }
1883dd9472c6SBarry Smith         }
18840e324ae4SSatish Balay       } else {
1885dd9472c6SBarry Smith         for (ii=0; ii<bs; ii++,value+=stepval) {
1886dd9472c6SBarry Smith           for (jj=0; jj<bs; jj++) {
18870e324ae4SSatish Balay             *bap++  = *value++;
18880e324ae4SSatish Balay           }
1889dd9472c6SBarry Smith         }
1890dd9472c6SBarry Smith       }
1891f1241b54SBarry Smith       noinsert2:;
189292c4ed94SBarry Smith       low = i;
189392c4ed94SBarry Smith     }
189492c4ed94SBarry Smith     ailen[row] = nrow;
189592c4ed94SBarry Smith   }
18963a40ed3dSBarry Smith   PetscFunctionReturn(0);
189792c4ed94SBarry Smith }
189826e093fcSHong Zhang 
18994a2ae208SSatish Balay #undef __FUNCT__
19004a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqBAIJ"
1901dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqBAIJ(Mat A,MatAssemblyType mode)
1902584200bdSSatish Balay {
1903584200bdSSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
1904c1ac3661SBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
1905d0f46423SBarry Smith   PetscInt       m = A->rmap->N,*ip,N,*ailen = a->ilen;
19066849ba73SBarry Smith   PetscErrorCode ierr;
1907c1ac3661SBarry Smith   PetscInt       mbs = a->mbs,bs2 = a->bs2,rmax = 0;
19083f1db9ecSBarry Smith   MatScalar      *aa = a->a,*ap;
19093447b6efSHong Zhang   PetscReal      ratio=0.6;
1910584200bdSSatish Balay 
19113a40ed3dSBarry Smith   PetscFunctionBegin;
19123a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
1913584200bdSSatish Balay 
191443ee02c3SBarry Smith   if (m) rmax = ailen[0];
1915584200bdSSatish Balay   for (i=1; i<mbs; i++) {
1916584200bdSSatish Balay     /* move each row back by the amount of empty slots (fshift) before it*/
1917584200bdSSatish Balay     fshift += imax[i-1] - ailen[i-1];
1918d402145bSBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
1919584200bdSSatish Balay     if (fshift) {
1920a7c10996SSatish Balay       ip = aj + ai[i]; ap = aa + bs2*ai[i];
1921584200bdSSatish Balay       N = ailen[i];
1922584200bdSSatish Balay       for (j=0; j<N; j++) {
1923584200bdSSatish Balay         ip[j-fshift] = ip[j];
1924549d3d68SSatish Balay         ierr = PetscMemcpy(ap+(j-fshift)*bs2,ap+j*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1925584200bdSSatish Balay       }
1926584200bdSSatish Balay     }
1927584200bdSSatish Balay     ai[i] = ai[i-1] + ailen[i-1];
1928584200bdSSatish Balay   }
1929584200bdSSatish Balay   if (mbs) {
1930584200bdSSatish Balay     fshift += imax[mbs-1] - ailen[mbs-1];
1931584200bdSSatish Balay     ai[mbs] = ai[mbs-1] + ailen[mbs-1];
1932584200bdSSatish Balay   }
1933584200bdSSatish Balay   /* reset ilen and imax for each row */
1934584200bdSSatish Balay   for (i=0; i<mbs; i++) {
1935584200bdSSatish Balay     ailen[i] = imax[i] = ai[i+1] - ai[i];
1936584200bdSSatish Balay   }
1937a7c10996SSatish Balay   a->nz = ai[mbs];
1938584200bdSSatish Balay 
1939584200bdSSatish Balay   /* diagonals may have moved, so kill the diagonal pointers */
1940b01c7715SBarry Smith   a->idiagvalid = PETSC_FALSE;
1941584200bdSSatish Balay   if (fshift && a->diag) {
1942606d414cSSatish Balay     ierr = PetscFree(a->diag);CHKERRQ(ierr);
194352e6d16bSBarry Smith     ierr = PetscLogObjectMemory(A,-(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
1944584200bdSSatish Balay     a->diag = 0;
1945584200bdSSatish Balay   }
194665e19b50SBarry Smith   if (fshift && a->nounused == -1) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D block size %D, %D unneeded", m, A->cmap->n, A->rmap->bs, fshift*bs2);
1947d0f46423SBarry Smith   ierr = PetscInfo5(A,"Matrix size: %D X %D, block size %D; storage space: %D unneeded, %D used\n",m,A->cmap->n,A->rmap->bs,fshift*bs2,a->nz*bs2);CHKERRQ(ierr);
1948ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues is %D\n",a->reallocs);CHKERRQ(ierr);
1949ae15b995SBarry Smith   ierr = PetscInfo1(A,"Most nonzeros blocks in any row is %D\n",rmax);CHKERRQ(ierr);
19508e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
1951e2f3b5e9SSatish Balay   a->reallocs          = 0;
19520e6d2581SBarry Smith   A->info.nz_unneeded  = (PetscReal)fshift*bs2;
1953cf4441caSHong Zhang 
1954cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
19552f53aa61SHong Zhang   if (a->compressedrow.use){
1956317fbc4cSHong Zhang     ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,mbs,ratio);CHKERRQ(ierr);
195773e7a558SHong Zhang   }
195888e51ccdSHong Zhang 
195988e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
19603a40ed3dSBarry Smith   PetscFunctionReturn(0);
1961584200bdSSatish Balay }
1962584200bdSSatish Balay 
1963bea157c4SSatish Balay /*
1964bea157c4SSatish Balay    This function returns an array of flags which indicate the locations of contiguous
1965bea157c4SSatish Balay    blocks that should be zeroed. for eg: if bs = 3  and is = [0,1,2,3,5,6,7,8,9]
1966bea157c4SSatish Balay    then the resulting sizes = [3,1,1,3,1] correspondig to sets [(0,1,2),(3),(5),(6,7,8),(9)]
1967bea157c4SSatish Balay    Assume: sizes should be long enough to hold all the values.
1968bea157c4SSatish Balay */
19694a2ae208SSatish Balay #undef __FUNCT__
19704a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqBAIJ_Check_Blocks"
1971c1ac3661SBarry Smith static PetscErrorCode MatZeroRows_SeqBAIJ_Check_Blocks(PetscInt idx[],PetscInt n,PetscInt bs,PetscInt sizes[], PetscInt *bs_max)
1972d9b7c43dSSatish Balay {
1973c1ac3661SBarry Smith   PetscInt   i,j,k,row;
1974bea157c4SSatish Balay   PetscTruth flg;
19753a40ed3dSBarry Smith 
1976433994e6SBarry Smith   PetscFunctionBegin;
1977bea157c4SSatish Balay   for (i=0,j=0; i<n; j++) {
1978bea157c4SSatish Balay     row = idx[i];
1979bea157c4SSatish Balay     if (row%bs!=0) { /* Not the begining of a block */
1980bea157c4SSatish Balay       sizes[j] = 1;
1981bea157c4SSatish Balay       i++;
1982e4fda26cSSatish Balay     } else if (i+bs > n) { /* complete block doesn't exist (at idx end) */
1983bea157c4SSatish Balay       sizes[j] = 1;         /* Also makes sure atleast 'bs' values exist for next else */
1984bea157c4SSatish Balay       i++;
1985bea157c4SSatish Balay     } else { /* Begining of the block, so check if the complete block exists */
1986bea157c4SSatish Balay       flg = PETSC_TRUE;
1987bea157c4SSatish Balay       for (k=1; k<bs; k++) {
1988bea157c4SSatish Balay         if (row+k != idx[i+k]) { /* break in the block */
1989bea157c4SSatish Balay           flg = PETSC_FALSE;
1990bea157c4SSatish Balay           break;
1991d9b7c43dSSatish Balay         }
1992bea157c4SSatish Balay       }
1993abc0a331SBarry Smith       if (flg) { /* No break in the bs */
1994bea157c4SSatish Balay         sizes[j] = bs;
1995bea157c4SSatish Balay         i+= bs;
1996bea157c4SSatish Balay       } else {
1997bea157c4SSatish Balay         sizes[j] = 1;
1998bea157c4SSatish Balay         i++;
1999bea157c4SSatish Balay       }
2000bea157c4SSatish Balay     }
2001bea157c4SSatish Balay   }
2002bea157c4SSatish Balay   *bs_max = j;
20033a40ed3dSBarry Smith   PetscFunctionReturn(0);
2004d9b7c43dSSatish Balay }
2005d9b7c43dSSatish Balay 
20064a2ae208SSatish Balay #undef __FUNCT__
20074a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqBAIJ"
2008f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqBAIJ(Mat A,PetscInt is_n,const PetscInt is_idx[],PetscScalar diag)
2009d9b7c43dSSatish Balay {
2010d9b7c43dSSatish Balay   Mat_SeqBAIJ    *baij=(Mat_SeqBAIJ*)A->data;
2011dfbe8321SBarry Smith   PetscErrorCode ierr;
2012f4df32b1SMatthew Knepley   PetscInt       i,j,k,count,*rows;
2013d0f46423SBarry Smith   PetscInt       bs=A->rmap->bs,bs2=baij->bs2,*sizes,row,bs_max;
201487828ca2SBarry Smith   PetscScalar    zero = 0.0;
20153f1db9ecSBarry Smith   MatScalar      *aa;
2016d9b7c43dSSatish Balay 
20173a40ed3dSBarry Smith   PetscFunctionBegin;
2018d9b7c43dSSatish Balay   /* Make a copy of the IS and  sort it */
2019bea157c4SSatish Balay   /* allocate memory for rows,sizes */
2020fca92195SBarry Smith   ierr  = PetscMalloc2(is_n,PetscInt,&rows,2*is_n,PetscInt,&sizes);CHKERRQ(ierr);
2021bea157c4SSatish Balay 
2022563b5814SBarry Smith   /* copy IS values to rows, and sort them */
2023bea157c4SSatish Balay   for (i=0; i<is_n; i++) { rows[i] = is_idx[i]; }
2024bea157c4SSatish Balay   ierr = PetscSortInt(is_n,rows);CHKERRQ(ierr);
2025a9817697SBarry Smith   if (baij->keepnonzeropattern) {
2026dffd3267SBarry Smith     for (i=0; i<is_n; i++) { sizes[i] = 1; }
2027dffd3267SBarry Smith     bs_max = is_n;
202888e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
2029dffd3267SBarry Smith   } else {
2030bea157c4SSatish Balay     ierr = MatZeroRows_SeqBAIJ_Check_Blocks(rows,is_n,bs,sizes,&bs_max);CHKERRQ(ierr);
203188e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
2032dffd3267SBarry Smith   }
2033bea157c4SSatish Balay 
2034bea157c4SSatish Balay   for (i=0,j=0; i<bs_max; j+=sizes[i],i++) {
2035bea157c4SSatish Balay     row   = rows[j];
2036e32f2f54SBarry Smith     if (row < 0 || row > A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range",row);
2037bea157c4SSatish Balay     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
2038b31fbe3bSSatish Balay     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
2039a9817697SBarry Smith     if (sizes[i] == bs && !baij->keepnonzeropattern) {
2040f4df32b1SMatthew Knepley       if (diag != 0.0) {
2041bea157c4SSatish Balay         if (baij->ilen[row/bs] > 0) {
2042bea157c4SSatish Balay           baij->ilen[row/bs]       = 1;
2043bea157c4SSatish Balay           baij->j[baij->i[row/bs]] = row/bs;
2044bea157c4SSatish Balay           ierr = PetscMemzero(aa,count*bs*sizeof(MatScalar));CHKERRQ(ierr);
2045a07cd24cSSatish Balay         }
2046563b5814SBarry Smith         /* Now insert all the diagonal values for this bs */
2047bea157c4SSatish Balay         for (k=0; k<bs; k++) {
2048f4df32b1SMatthew Knepley           ierr = (*A->ops->setvalues)(A,1,rows+j+k,1,rows+j+k,&diag,INSERT_VALUES);CHKERRQ(ierr);
2049bea157c4SSatish Balay         }
2050f4df32b1SMatthew Knepley       } else { /* (diag == 0.0) */
2051bea157c4SSatish Balay         baij->ilen[row/bs] = 0;
2052f4df32b1SMatthew Knepley       } /* end (diag == 0.0) */
2053bea157c4SSatish Balay     } else { /* (sizes[i] != bs) */
2054aa482453SBarry Smith #if defined (PETSC_USE_DEBUG)
2055e32f2f54SBarry Smith       if (sizes[i] != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal Error. Value should be 1");
2056bea157c4SSatish Balay #endif
2057bea157c4SSatish Balay       for (k=0; k<count; k++) {
2058d9b7c43dSSatish Balay         aa[0] =  zero;
2059d9b7c43dSSatish Balay         aa    += bs;
2060d9b7c43dSSatish Balay       }
2061f4df32b1SMatthew Knepley       if (diag != 0.0) {
2062f4df32b1SMatthew Knepley         ierr = (*A->ops->setvalues)(A,1,rows+j,1,rows+j,&diag,INSERT_VALUES);CHKERRQ(ierr);
2063d9b7c43dSSatish Balay       }
2064d9b7c43dSSatish Balay     }
2065bea157c4SSatish Balay   }
2066bea157c4SSatish Balay 
2067fca92195SBarry Smith   ierr = PetscFree2(rows,sizes);CHKERRQ(ierr);
20689a8dea36SBarry Smith   ierr = MatAssemblyEnd_SeqBAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20693a40ed3dSBarry Smith   PetscFunctionReturn(0);
2070d9b7c43dSSatish Balay }
20711c351548SSatish Balay 
20724a2ae208SSatish Balay #undef __FUNCT__
20734a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqBAIJ"
2074c1ac3661SBarry Smith PetscErrorCode MatSetValues_SeqBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
20752d61bbb3SSatish Balay {
20762d61bbb3SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
2077e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
2078c1ac3661SBarry Smith   PetscInt       *imax=a->imax,*ai=a->i,*ailen=a->ilen;
2079d0f46423SBarry Smith   PetscInt       *aj=a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
20806849ba73SBarry Smith   PetscErrorCode ierr;
2081c1ac3661SBarry Smith   PetscInt       ridx,cidx,bs2=a->bs2;
2082273d9f13SBarry Smith   PetscTruth     roworiented=a->roworiented;
20833f1db9ecSBarry Smith   MatScalar      *ap,value,*aa=a->a,*bap;
20842d61bbb3SSatish Balay 
20852d61bbb3SSatish Balay   PetscFunctionBegin;
208671fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
20872d61bbb3SSatish Balay   for (k=0; k<m; k++) { /* loop over added rows */
2088085a36d4SBarry Smith     row  = im[k];
2089085a36d4SBarry Smith     brow = row/bs;
20905ef9f2a5SBarry Smith     if (row < 0) continue;
20912515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
2092e32f2f54SBarry Smith     if (row >= A->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->N-1);
20932d61bbb3SSatish Balay #endif
20942d61bbb3SSatish Balay     rp   = aj + ai[brow];
20952d61bbb3SSatish Balay     ap   = aa + bs2*ai[brow];
20962d61bbb3SSatish Balay     rmax = imax[brow];
20972d61bbb3SSatish Balay     nrow = ailen[brow];
20982d61bbb3SSatish Balay     low  = 0;
2099c71e6ed7SBarry Smith     high = nrow;
21002d61bbb3SSatish Balay     for (l=0; l<n; l++) { /* loop over added columns */
21015ef9f2a5SBarry Smith       if (in[l] < 0) continue;
21022515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
2103e32f2f54SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
21042d61bbb3SSatish Balay #endif
21052d61bbb3SSatish Balay       col = in[l]; bcol = col/bs;
21062d61bbb3SSatish Balay       ridx = row % bs; cidx = col % bs;
21072d61bbb3SSatish Balay       if (roworiented) {
21085ef9f2a5SBarry Smith         value = v[l + k*n];
21092d61bbb3SSatish Balay       } else {
21102d61bbb3SSatish Balay         value = v[k + l*m];
21112d61bbb3SSatish Balay       }
21127cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
2113e2ee6c50SBarry Smith       lastcol = col;
21142d61bbb3SSatish Balay       while (high-low > 7) {
21152d61bbb3SSatish Balay         t = (low+high)/2;
21162d61bbb3SSatish Balay         if (rp[t] > bcol) high = t;
21172d61bbb3SSatish Balay         else              low  = t;
21182d61bbb3SSatish Balay       }
21192d61bbb3SSatish Balay       for (i=low; i<high; i++) {
21202d61bbb3SSatish Balay         if (rp[i] > bcol) break;
21212d61bbb3SSatish Balay         if (rp[i] == bcol) {
21222d61bbb3SSatish Balay           bap  = ap +  bs2*i + bs*cidx + ridx;
21232d61bbb3SSatish Balay           if (is == ADD_VALUES) *bap += value;
21242d61bbb3SSatish Balay           else                  *bap  = value;
21252d61bbb3SSatish Balay           goto noinsert1;
21262d61bbb3SSatish Balay         }
21272d61bbb3SSatish Balay       }
21282d61bbb3SSatish Balay       if (nonew == 1) goto noinsert1;
2129e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
2130fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
2131c03d1d03SSatish Balay       N = nrow++ - 1; high++;
21322d61bbb3SSatish Balay       /* shift up all the later entries in this row */
21332d61bbb3SSatish Balay       for (ii=N; ii>=i; ii--) {
21342d61bbb3SSatish Balay         rp[ii+1] = rp[ii];
2135549d3d68SSatish Balay         ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
21362d61bbb3SSatish Balay       }
2137549d3d68SSatish Balay       if (N>=i) {
2138549d3d68SSatish Balay         ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
2139549d3d68SSatish Balay       }
21402d61bbb3SSatish Balay       rp[i]                      = bcol;
21412d61bbb3SSatish Balay       ap[bs2*i + bs*cidx + ridx] = value;
2142085a36d4SBarry Smith       a->nz++;
21432d61bbb3SSatish Balay       noinsert1:;
21442d61bbb3SSatish Balay       low = i;
21452d61bbb3SSatish Balay     }
21462d61bbb3SSatish Balay     ailen[brow] = nrow;
21472d61bbb3SSatish Balay   }
214888e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
21492d61bbb3SSatish Balay   PetscFunctionReturn(0);
21502d61bbb3SSatish Balay }
21512d61bbb3SSatish Balay 
21524a2ae208SSatish Balay #undef __FUNCT__
21534a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqBAIJ"
21540481f469SBarry Smith PetscErrorCode MatILUFactor_SeqBAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
21552d61bbb3SSatish Balay {
21562d61bbb3SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)inA->data;
21572d61bbb3SSatish Balay   Mat            outA;
2158dfbe8321SBarry Smith   PetscErrorCode ierr;
2159667159a5SBarry Smith   PetscTruth     row_identity,col_identity;
21602d61bbb3SSatish Balay 
21612d61bbb3SSatish Balay   PetscFunctionBegin;
2162e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for in-place ILU");
2163667159a5SBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2164667159a5SBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2165667159a5SBarry Smith   if (!row_identity || !col_identity) {
2166e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for in-place ILU");
2167667159a5SBarry Smith   }
21682d61bbb3SSatish Balay 
21692d61bbb3SSatish Balay   outA            = inA;
2170d5f3da31SBarry Smith   inA->factortype = MAT_FACTOR_LU;
21712d61bbb3SSatish Balay 
2172c4992f7dSBarry Smith   ierr = MatMarkDiagonal_SeqBAIJ(inA);CHKERRQ(ierr);
2173cf242676SKris Buschelman 
2174c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
2175c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr); }
2176c3122656SLisandro Dalcin   a->row = row;
2177c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
2178c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr); }
2179c3122656SLisandro Dalcin   a->col = col;
2180c38d4ed2SBarry Smith 
2181c38d4ed2SBarry Smith   /* Create the invert permutation so that it can be used in MatLUFactorNumeric() */
2182a23ff555SBarry Smith   if (a->icol) {
2183a23ff555SBarry Smith     ierr = ISDestroy(a->icol);CHKERRQ(ierr);
2184a23ff555SBarry Smith   }
21854c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
218652e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2187c38d4ed2SBarry Smith 
21888b1456e3SHong Zhang   ierr = MatSeqBAIJSetNumericFactorization_inplace(inA,(PetscTruth)(row_identity && col_identity));CHKERRQ(ierr);
2189c38d4ed2SBarry Smith   if (!a->solve_work) {
2190d0f46423SBarry Smith     ierr = PetscMalloc((inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2191d0f46423SBarry Smith     ierr = PetscLogObjectMemory(inA,(inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar));CHKERRQ(ierr);
2192c38d4ed2SBarry Smith   }
2193719d5645SBarry Smith   ierr = MatLUFactorNumeric(outA,inA,info);CHKERRQ(ierr);
2194667159a5SBarry Smith 
21952d61bbb3SSatish Balay   PetscFunctionReturn(0);
21962d61bbb3SSatish Balay }
2197d9b7c43dSSatish Balay 
2198fb2e594dSBarry Smith EXTERN_C_BEGIN
21994a2ae208SSatish Balay #undef __FUNCT__
22004a2ae208SSatish Balay #define __FUNCT__ "MatSeqBAIJSetColumnIndices_SeqBAIJ"
2201be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetColumnIndices_SeqBAIJ(Mat mat,PetscInt *indices)
220227a8da17SBarry Smith {
220327a8da17SBarry Smith   Mat_SeqBAIJ *baij = (Mat_SeqBAIJ *)mat->data;
2204bdb1c0e1SJed Brown   PetscInt    i,nz,mbs;
220527a8da17SBarry Smith 
220627a8da17SBarry Smith   PetscFunctionBegin;
220714db4f74SSatish Balay   nz  = baij->maxnz/baij->bs2;
2208bdb1c0e1SJed Brown   mbs = baij->mbs;
220927a8da17SBarry Smith   for (i=0; i<nz; i++) {
221027a8da17SBarry Smith     baij->j[i] = indices[i];
221127a8da17SBarry Smith   }
221227a8da17SBarry Smith   baij->nz = nz;
2213bdb1c0e1SJed Brown   for (i=0; i<mbs; i++) {
221427a8da17SBarry Smith     baij->ilen[i] = baij->imax[i];
221527a8da17SBarry Smith   }
221627a8da17SBarry Smith   PetscFunctionReturn(0);
221727a8da17SBarry Smith }
2218fb2e594dSBarry Smith EXTERN_C_END
221927a8da17SBarry Smith 
22204a2ae208SSatish Balay #undef __FUNCT__
22214a2ae208SSatish Balay #define __FUNCT__ "MatSeqBAIJSetColumnIndices"
222227a8da17SBarry Smith /*@
222327a8da17SBarry Smith     MatSeqBAIJSetColumnIndices - Set the column indices for all the rows
222427a8da17SBarry Smith        in the matrix.
222527a8da17SBarry Smith 
222627a8da17SBarry Smith   Input Parameters:
222727a8da17SBarry Smith +  mat - the SeqBAIJ matrix
222827a8da17SBarry Smith -  indices - the column indices
222927a8da17SBarry Smith 
223015091d37SBarry Smith   Level: advanced
223115091d37SBarry Smith 
223227a8da17SBarry Smith   Notes:
223327a8da17SBarry Smith     This can be called if you have precomputed the nonzero structure of the
223427a8da17SBarry Smith   matrix and want to provide it to the matrix object to improve the performance
223527a8da17SBarry Smith   of the MatSetValues() operation.
223627a8da17SBarry Smith 
223727a8da17SBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2238d1be2dadSMatthew Knepley   MatCreateSeqBAIJ(), and the columns indices MUST be sorted.
223927a8da17SBarry Smith 
224027a8da17SBarry Smith     MUST be called before any calls to MatSetValues();
224127a8da17SBarry Smith 
224227a8da17SBarry Smith @*/
2243be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetColumnIndices(Mat mat,PetscInt *indices)
224427a8da17SBarry Smith {
2245c1ac3661SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
224627a8da17SBarry Smith 
224727a8da17SBarry Smith   PetscFunctionBegin;
22480700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
22494482741eSBarry Smith   PetscValidPointer(indices,2);
2250c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqBAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
225127a8da17SBarry Smith   if (f) {
225227a8da17SBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2253e7e72b3dSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Wrong type of matrix to set column indices");
225427a8da17SBarry Smith   PetscFunctionReturn(0);
225527a8da17SBarry Smith }
225627a8da17SBarry Smith 
22574a2ae208SSatish Balay #undef __FUNCT__
2258985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqBAIJ"
2259985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqBAIJ(Mat A,Vec v,PetscInt idx[])
2260273d9f13SBarry Smith {
2261273d9f13SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
2262dfbe8321SBarry Smith   PetscErrorCode ierr;
2263c1ac3661SBarry Smith   PetscInt       i,j,n,row,bs,*ai,*aj,mbs;
2264273d9f13SBarry Smith   PetscReal      atmp;
226587828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
2266273d9f13SBarry Smith   MatScalar      *aa;
2267c1ac3661SBarry Smith   PetscInt       ncols,brow,krow,kcol;
2268273d9f13SBarry Smith 
2269273d9f13SBarry Smith   PetscFunctionBegin;
2270e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2271d0f46423SBarry Smith   bs   = A->rmap->bs;
2272273d9f13SBarry Smith   aa   = a->a;
2273273d9f13SBarry Smith   ai   = a->i;
2274273d9f13SBarry Smith   aj   = a->j;
2275273d9f13SBarry Smith   mbs  = a->mbs;
2276273d9f13SBarry Smith 
22772dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
22781ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2279273d9f13SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2280e32f2f54SBarry Smith   if (n != A->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2281273d9f13SBarry Smith   for (i=0; i<mbs; i++) {
2282273d9f13SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2283273d9f13SBarry Smith     brow  = bs*i;
2284273d9f13SBarry Smith     for (j=0; j<ncols; j++){
2285273d9f13SBarry Smith       for (kcol=0; kcol<bs; kcol++){
2286273d9f13SBarry Smith         for (krow=0; krow<bs; krow++){
2287273d9f13SBarry Smith           atmp = PetscAbsScalar(*aa);aa++;
2288273d9f13SBarry Smith           row = brow + krow;    /* row index */
2289a83599f4SBarry Smith           /* printf("val[%d,%d]: %G\n",row,bcol+kcol,atmp); */
2290985db425SBarry Smith           if (PetscAbsScalar(x[row]) < atmp) {x[row] = atmp; if (idx) idx[row] = bs*(*aj) + kcol;}
2291273d9f13SBarry Smith         }
2292273d9f13SBarry Smith       }
2293273d9f13SBarry Smith       aj++;
2294273d9f13SBarry Smith     }
2295273d9f13SBarry Smith   }
22961ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2297273d9f13SBarry Smith   PetscFunctionReturn(0);
2298273d9f13SBarry Smith }
2299273d9f13SBarry Smith 
23004a2ae208SSatish Balay #undef __FUNCT__
23013c896bc6SHong Zhang #define __FUNCT__ "MatCopy_SeqBAIJ"
23023c896bc6SHong Zhang PetscErrorCode MatCopy_SeqBAIJ(Mat A,Mat B,MatStructure str)
23033c896bc6SHong Zhang {
23043c896bc6SHong Zhang   PetscErrorCode ierr;
23053c896bc6SHong Zhang 
23063c896bc6SHong Zhang   PetscFunctionBegin;
23073c896bc6SHong Zhang   /* If the two matrices have the same copy implementation, use fast copy. */
23083c896bc6SHong Zhang   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
23093c896bc6SHong Zhang     Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
23103c896bc6SHong Zhang     Mat_SeqBAIJ *b = (Mat_SeqBAIJ*)B->data;
23113c896bc6SHong Zhang 
2312e7e72b3dSBarry Smith     if (a->i[A->rmap->N] != b->i[B->rmap->N]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2313d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->N])*sizeof(PetscScalar));CHKERRQ(ierr);
23143c896bc6SHong Zhang   } else {
23153c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
23163c896bc6SHong Zhang   }
23173c896bc6SHong Zhang   PetscFunctionReturn(0);
23183c896bc6SHong Zhang }
23193c896bc6SHong Zhang 
23203c896bc6SHong Zhang #undef __FUNCT__
23214a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqBAIJ"
2322dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqBAIJ(Mat A)
2323273d9f13SBarry Smith {
2324dfbe8321SBarry Smith   PetscErrorCode ierr;
2325273d9f13SBarry Smith 
2326273d9f13SBarry Smith   PetscFunctionBegin;
2327db4efbfdSBarry Smith   ierr =  MatSeqBAIJSetPreallocation_SeqBAIJ(A,-PetscMax(A->rmap->bs,1),PETSC_DEFAULT,0);CHKERRQ(ierr);
2328273d9f13SBarry Smith   PetscFunctionReturn(0);
2329273d9f13SBarry Smith }
2330273d9f13SBarry Smith 
23314a2ae208SSatish Balay #undef __FUNCT__
23324a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqBAIJ"
2333dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqBAIJ(Mat A,PetscScalar *array[])
2334f2a5309cSSatish Balay {
2335f2a5309cSSatish Balay   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
2336f2a5309cSSatish Balay   PetscFunctionBegin;
2337f2a5309cSSatish Balay   *array = a->a;
2338f2a5309cSSatish Balay   PetscFunctionReturn(0);
2339f2a5309cSSatish Balay }
2340f2a5309cSSatish Balay 
23414a2ae208SSatish Balay #undef __FUNCT__
23424a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqBAIJ"
2343dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqBAIJ(Mat A,PetscScalar *array[])
2344f2a5309cSSatish Balay {
2345f2a5309cSSatish Balay   PetscFunctionBegin;
2346f2a5309cSSatish Balay   PetscFunctionReturn(0);
2347f2a5309cSSatish Balay }
2348f2a5309cSSatish Balay 
234942ee4b1aSHong Zhang #undef __FUNCT__
235042ee4b1aSHong Zhang #define __FUNCT__ "MatAXPY_SeqBAIJ"
2351f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
235242ee4b1aSHong Zhang {
235342ee4b1aSHong Zhang   Mat_SeqBAIJ    *x  = (Mat_SeqBAIJ *)X->data,*y = (Mat_SeqBAIJ *)Y->data;
2354dfbe8321SBarry Smith   PetscErrorCode ierr;
2355d0f46423SBarry Smith   PetscInt       i,bs=Y->rmap->bs,j,bs2;
23560805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
235742ee4b1aSHong Zhang 
235842ee4b1aSHong Zhang   PetscFunctionBegin;
235942ee4b1aSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
2360f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2361f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2362c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2363c4319e64SHong Zhang     if (y->xtoy && y->XtoY != X) {
2364c4319e64SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2365c4319e64SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2366c537a176SHong Zhang     }
2367c4319e64SHong Zhang     if (!y->xtoy) { /* get xtoy */
2368c4319e64SHong Zhang       ierr = MatAXPYGetxtoy_Private(x->mbs,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2369c4319e64SHong Zhang       y->XtoY = X;
2370c009d632SSatish Balay       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2371c537a176SHong Zhang     }
2372c4319e64SHong Zhang     bs2 = bs*bs;
2373c537a176SHong Zhang     for (i=0; i<x->nz; i++) {
2374c4319e64SHong Zhang       j = 0;
2375c4319e64SHong Zhang       while (j < bs2){
2376f4df32b1SMatthew Knepley         y->a[bs2*y->xtoy[i]+j] += a*(x->a[bs2*i+j]);
2377c4319e64SHong Zhang         j++;
2378c537a176SHong Zhang       }
2379c4319e64SHong Zhang     }
23801e2582c4SBarry Smith     ierr = PetscInfo3(Y,"ratio of nnz(X)/nnz(Y): %D/%D = %G\n",bs2*x->nz,bs2*y->nz,(PetscReal)(bs2*x->nz)/(bs2*y->nz));CHKERRQ(ierr);
238142ee4b1aSHong Zhang   } else {
2382f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
238342ee4b1aSHong Zhang   }
238442ee4b1aSHong Zhang   PetscFunctionReturn(0);
238542ee4b1aSHong Zhang }
238642ee4b1aSHong Zhang 
238799cafbc1SBarry Smith #undef __FUNCT__
238841c166b1SJed Brown #define __FUNCT__ "MatSetBlockSize_SeqBAIJ"
238941c166b1SJed Brown PetscErrorCode MatSetBlockSize_SeqBAIJ(Mat A,PetscInt bs)
239041c166b1SJed Brown {
239141c166b1SJed Brown   PetscInt rbs,cbs;
239241c166b1SJed Brown   PetscErrorCode ierr;
239341c166b1SJed Brown 
239441c166b1SJed Brown   PetscFunctionBegin;
239541c166b1SJed Brown   ierr = PetscLayoutGetBlockSize(A->rmap,&rbs);CHKERRQ(ierr);
239641c166b1SJed Brown   ierr = PetscLayoutGetBlockSize(A->cmap,&cbs);CHKERRQ(ierr);
2397e32f2f54SBarry Smith   if (rbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with BAIJ %d",bs,rbs);
2398e32f2f54SBarry Smith   if (cbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with BAIJ %d",bs,cbs);
239941c166b1SJed Brown   PetscFunctionReturn(0);
240041c166b1SJed Brown }
240141c166b1SJed Brown 
240241c166b1SJed Brown #undef __FUNCT__
240399cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqBAIJ"
240499cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqBAIJ(Mat A)
240599cafbc1SBarry Smith {
240699cafbc1SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
240799cafbc1SBarry Smith   PetscInt       i,nz = a->bs2*a->i[a->mbs];
2408dd6ea824SBarry Smith   MatScalar      *aa = a->a;
240999cafbc1SBarry Smith 
241099cafbc1SBarry Smith   PetscFunctionBegin;
241199cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
241299cafbc1SBarry Smith   PetscFunctionReturn(0);
241399cafbc1SBarry Smith }
241499cafbc1SBarry Smith 
241599cafbc1SBarry Smith #undef __FUNCT__
241699cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqBAIJ"
241799cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqBAIJ(Mat A)
241899cafbc1SBarry Smith {
241999cafbc1SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
242099cafbc1SBarry Smith   PetscInt       i,nz = a->bs2*a->i[a->mbs];
2421dd6ea824SBarry Smith   MatScalar      *aa = a->a;
242299cafbc1SBarry Smith 
242399cafbc1SBarry Smith   PetscFunctionBegin;
242499cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
242599cafbc1SBarry Smith   PetscFunctionReturn(0);
242699cafbc1SBarry Smith }
242799cafbc1SBarry Smith 
24283acb8795SBarry Smith extern PetscErrorCode MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
24293acb8795SBarry Smith 
24303acb8795SBarry Smith #undef __FUNCT__
24313acb8795SBarry Smith #define __FUNCT__ "MatGetColumnIJ_SeqBAIJ"
24323acb8795SBarry Smith /*
24333acb8795SBarry Smith     Code almost idential to MatGetColumnIJ_SeqAIJ() should share common code
24343acb8795SBarry Smith */
24353acb8795SBarry Smith PetscErrorCode MatGetColumnIJ_SeqBAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
24363acb8795SBarry Smith {
24373acb8795SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
24383acb8795SBarry Smith   PetscErrorCode ierr;
24393acb8795SBarry Smith   PetscInt       bs = A->rmap->bs,i,*collengths,*cia,*cja,n = A->cmap->n/bs,m = A->rmap->n/bs;
24403acb8795SBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
24413acb8795SBarry Smith 
24423acb8795SBarry Smith   PetscFunctionBegin;
24433acb8795SBarry Smith   *nn = n;
24443acb8795SBarry Smith   if (!ia) PetscFunctionReturn(0);
2445e7e72b3dSBarry Smith   if (symmetric) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not for BAIJ matrices");
2446e7e72b3dSBarry Smith   else {
24473acb8795SBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
24483acb8795SBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
24493acb8795SBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
24503acb8795SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
24513acb8795SBarry Smith     jj = a->j;
24523acb8795SBarry Smith     for (i=0; i<nz; i++) {
24533acb8795SBarry Smith       collengths[jj[i]]++;
24543acb8795SBarry Smith     }
24553acb8795SBarry Smith     cia[0] = oshift;
24563acb8795SBarry Smith     for (i=0; i<n; i++) {
24573acb8795SBarry Smith       cia[i+1] = cia[i] + collengths[i];
24583acb8795SBarry Smith     }
24593acb8795SBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
24603acb8795SBarry Smith     jj   = a->j;
24613acb8795SBarry Smith     for (row=0; row<m; row++) {
24623acb8795SBarry Smith       mr = a->i[row+1] - a->i[row];
24633acb8795SBarry Smith       for (i=0; i<mr; i++) {
24643acb8795SBarry Smith         col = *jj++;
24653acb8795SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
24663acb8795SBarry Smith       }
24673acb8795SBarry Smith     }
24683acb8795SBarry Smith     ierr = PetscFree(collengths);CHKERRQ(ierr);
24693acb8795SBarry Smith     *ia = cia; *ja = cja;
24703acb8795SBarry Smith   }
24713acb8795SBarry Smith   PetscFunctionReturn(0);
24723acb8795SBarry Smith }
24733acb8795SBarry Smith 
24743acb8795SBarry Smith #undef __FUNCT__
24753acb8795SBarry Smith #define __FUNCT__ "MatRestoreColumnIJ_SeqBAIJ"
24763acb8795SBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqBAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
24773acb8795SBarry Smith {
24783acb8795SBarry Smith   PetscErrorCode ierr;
24793acb8795SBarry Smith 
24803acb8795SBarry Smith   PetscFunctionBegin;
24813acb8795SBarry Smith   if (!ia) PetscFunctionReturn(0);
24823acb8795SBarry Smith   ierr = PetscFree(*ia);CHKERRQ(ierr);
24833acb8795SBarry Smith   ierr = PetscFree(*ja);CHKERRQ(ierr);
24843acb8795SBarry Smith   PetscFunctionReturn(0);
24853acb8795SBarry Smith }
24863acb8795SBarry Smith 
2487f6d58c54SBarry Smith #undef __FUNCT__
2488f6d58c54SBarry Smith #define __FUNCT__ "MatFDColoringApply_BAIJ"
2489f6d58c54SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_BAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2490f6d58c54SBarry Smith {
2491f6d58c54SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
2492f6d58c54SBarry Smith   PetscErrorCode ierr;
2493f6d58c54SBarry Smith   PetscInt       bs = J->rmap->bs,i,j,k,start,end,l,row,col,*srows,**vscaleforrow,m1,m2;
2494f6d58c54SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
2495f6d58c54SBarry Smith   PetscScalar    *vscale_array;
2496f6d58c54SBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin,unorm;
2497f6d58c54SBarry Smith   Vec            w1=coloring->w1,w2=coloring->w2,w3;
2498f6d58c54SBarry Smith   void           *fctx = coloring->fctx;
2499f6d58c54SBarry Smith   PetscTruth     flg = PETSC_FALSE;
2500f6d58c54SBarry Smith   PetscInt       ctype=coloring->ctype,N,col_start=0,col_end=0;
2501f6d58c54SBarry Smith   Vec            x1_tmp;
2502f6d58c54SBarry Smith 
2503f6d58c54SBarry Smith   PetscFunctionBegin;
25040700a824SBarry Smith   PetscValidHeaderSpecific(J,MAT_CLASSID,1);
25050700a824SBarry Smith   PetscValidHeaderSpecific(coloring,MAT_FDCOLORING_CLASSID,2);
25060700a824SBarry Smith   PetscValidHeaderSpecific(x1,VEC_CLASSID,3);
2507e32f2f54SBarry Smith   if (!f) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatFDColoringSetFunction()");
2508f6d58c54SBarry Smith 
2509f6d58c54SBarry Smith   ierr = PetscLogEventBegin(MAT_FDColoringApply,coloring,J,x1,0);CHKERRQ(ierr);
2510f6d58c54SBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
2511f6d58c54SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2512f6d58c54SBarry Smith   if (flg) {
2513f6d58c54SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2514f6d58c54SBarry Smith   } else {
2515f6d58c54SBarry Smith     PetscTruth assembled;
2516f6d58c54SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
2517f6d58c54SBarry Smith     if (assembled) {
2518f6d58c54SBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2519f6d58c54SBarry Smith     }
2520f6d58c54SBarry Smith   }
2521f6d58c54SBarry Smith 
2522f6d58c54SBarry Smith   x1_tmp = x1;
2523f6d58c54SBarry Smith   if (!coloring->vscale){
2524f6d58c54SBarry Smith     ierr = VecDuplicate(x1_tmp,&coloring->vscale);CHKERRQ(ierr);
2525f6d58c54SBarry Smith   }
2526f6d58c54SBarry Smith 
2527f6d58c54SBarry Smith   /*
2528f6d58c54SBarry Smith     This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2529f6d58c54SBarry Smith     coloring->F for the coarser grids from the finest
2530f6d58c54SBarry Smith   */
2531f6d58c54SBarry Smith   if (coloring->F) {
2532f6d58c54SBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2533f6d58c54SBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2534f6d58c54SBarry Smith     if (m1 != m2) {
2535f6d58c54SBarry Smith       coloring->F = 0;
2536f6d58c54SBarry Smith       }
2537f6d58c54SBarry Smith     }
2538f6d58c54SBarry Smith 
2539f6d58c54SBarry Smith   if (coloring->htype[0] == 'w') { /* tacky test; need to make systematic if we add other approaches to computing h*/
2540f6d58c54SBarry Smith     ierr = VecNorm(x1_tmp,NORM_2,&unorm);CHKERRQ(ierr);
2541f6d58c54SBarry Smith   }
2542f6d58c54SBarry Smith   ierr = VecGetOwnershipRange(w1,&start,&end);CHKERRQ(ierr); /* OwnershipRange is used by ghosted x! */
2543f6d58c54SBarry Smith 
2544f6d58c54SBarry Smith   /* Set w1 = F(x1) */
2545f6d58c54SBarry Smith   if (coloring->F) {
2546f6d58c54SBarry Smith     w1          = coloring->F; /* use already computed value of function */
2547f6d58c54SBarry Smith     coloring->F = 0;
2548f6d58c54SBarry Smith   } else {
2549f6d58c54SBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2550f6d58c54SBarry Smith     ierr = (*f)(sctx,x1_tmp,w1,fctx);CHKERRQ(ierr);
2551f6d58c54SBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2552f6d58c54SBarry Smith   }
2553f6d58c54SBarry Smith 
2554f6d58c54SBarry Smith   if (!coloring->w3) {
2555f6d58c54SBarry Smith     ierr = VecDuplicate(x1_tmp,&coloring->w3);CHKERRQ(ierr);
2556f6d58c54SBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2557f6d58c54SBarry Smith   }
2558f6d58c54SBarry Smith   w3 = coloring->w3;
2559f6d58c54SBarry Smith 
2560f6d58c54SBarry Smith     CHKMEMQ;
2561f6d58c54SBarry Smith     /* Compute all the local scale factors, including ghost points */
2562f6d58c54SBarry Smith   ierr = VecGetLocalSize(x1_tmp,&N);CHKERRQ(ierr);
2563f6d58c54SBarry Smith   ierr = VecGetArray(x1_tmp,&xx);CHKERRQ(ierr);
2564f6d58c54SBarry Smith   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2565f6d58c54SBarry Smith   if (ctype == IS_COLORING_GHOSTED){
2566f6d58c54SBarry Smith     col_start = 0; col_end = N;
2567f6d58c54SBarry Smith   } else if (ctype == IS_COLORING_GLOBAL){
2568f6d58c54SBarry Smith     xx = xx - start;
2569f6d58c54SBarry Smith     vscale_array = vscale_array - start;
2570f6d58c54SBarry Smith     col_start = start; col_end = N + start;
2571f6d58c54SBarry Smith   }    CHKMEMQ;
2572f6d58c54SBarry Smith   for (col=col_start; col<col_end; col++){
2573f6d58c54SBarry Smith     /* Loop over each local column, vscale[col] = 1./(epsilon*dx[col]) */
2574f6d58c54SBarry Smith     if (coloring->htype[0] == 'w') {
2575f6d58c54SBarry Smith       dx = 1.0 + unorm;
2576f6d58c54SBarry Smith     } else {
2577f6d58c54SBarry Smith       dx  = xx[col];
2578f6d58c54SBarry Smith     }
2579f6d58c54SBarry Smith     if (dx == 0.0) dx = 1.0;
2580f6d58c54SBarry Smith #if !defined(PETSC_USE_COMPLEX)
2581f6d58c54SBarry Smith     if (dx < umin && dx >= 0.0)      dx = umin;
2582f6d58c54SBarry Smith     else if (dx < 0.0 && dx > -umin) dx = -umin;
2583f6d58c54SBarry Smith #else
2584f6d58c54SBarry Smith     if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2585f6d58c54SBarry Smith     else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2586f6d58c54SBarry Smith #endif
2587f6d58c54SBarry Smith     dx               *= epsilon;
2588f6d58c54SBarry Smith     vscale_array[col] = 1.0/dx;
2589f6d58c54SBarry Smith   }     CHKMEMQ;
2590f6d58c54SBarry Smith   if (ctype == IS_COLORING_GLOBAL)  vscale_array = vscale_array + start;
2591f6d58c54SBarry Smith   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2592f6d58c54SBarry Smith   if (ctype == IS_COLORING_GLOBAL){
2593f6d58c54SBarry Smith     ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2594f6d58c54SBarry Smith     ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2595f6d58c54SBarry Smith   }
2596f6d58c54SBarry Smith   CHKMEMQ;
2597f6d58c54SBarry Smith   if (coloring->vscaleforrow) {
2598f6d58c54SBarry Smith     vscaleforrow = coloring->vscaleforrow;
2599e7e72b3dSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Null Object: coloring->vscaleforrow");
2600f6d58c54SBarry Smith 
2601f6d58c54SBarry Smith   ierr = PetscMalloc(bs*sizeof(PetscInt),&srows);CHKERRQ(ierr);
2602f6d58c54SBarry Smith   /*
2603f6d58c54SBarry Smith     Loop over each color
2604f6d58c54SBarry Smith   */
2605f6d58c54SBarry Smith   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2606f6d58c54SBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2607f6d58c54SBarry Smith     coloring->currentcolor = k;
2608f6d58c54SBarry Smith     for (i=0; i<bs; i++) {
2609f6d58c54SBarry Smith       ierr = VecCopy(x1_tmp,w3);CHKERRQ(ierr);
2610f6d58c54SBarry Smith       ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);
2611f6d58c54SBarry Smith       if (ctype == IS_COLORING_GLOBAL) w3_array = w3_array - start;
2612f6d58c54SBarry Smith       /*
2613f6d58c54SBarry Smith 	Loop over each column associated with color
2614f6d58c54SBarry Smith 	adding the perturbation to the vector w3.
2615f6d58c54SBarry Smith       */
2616f6d58c54SBarry Smith       for (l=0; l<coloring->ncolumns[k]; l++) {
2617f6d58c54SBarry Smith 	col = i + bs*coloring->columns[k][l];    /* local column of the matrix we are probing for */
2618f6d58c54SBarry Smith 	if (coloring->htype[0] == 'w') {
2619f6d58c54SBarry Smith 	  dx = 1.0 + unorm;
2620f6d58c54SBarry Smith 	} else {
2621f6d58c54SBarry Smith 	  dx  = xx[col];
2622f6d58c54SBarry Smith 	}
2623f6d58c54SBarry Smith 	if (dx == 0.0) dx = 1.0;
2624f6d58c54SBarry Smith #if !defined(PETSC_USE_COMPLEX)
2625f6d58c54SBarry Smith 	if (dx < umin && dx >= 0.0)      dx = umin;
2626f6d58c54SBarry Smith 	else if (dx < 0.0 && dx > -umin) dx = -umin;
2627f6d58c54SBarry Smith #else
2628f6d58c54SBarry Smith 	if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2629f6d58c54SBarry Smith 	else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2630f6d58c54SBarry Smith #endif
2631f6d58c54SBarry Smith 	dx            *= epsilon;
2632e32f2f54SBarry Smith 	if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2633f6d58c54SBarry Smith 	w3_array[col] += dx;
2634f6d58c54SBarry Smith       }
2635f6d58c54SBarry Smith       if (ctype == IS_COLORING_GLOBAL) w3_array = w3_array + start;
2636f6d58c54SBarry Smith       ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2637f6d58c54SBarry Smith 
2638f6d58c54SBarry Smith       /*
2639f6d58c54SBarry Smith 	Evaluate function at w3 = x1 + dx (here dx is a vector of perturbations)
2640f6d58c54SBarry Smith 	w2 = F(x1 + dx) - F(x1)
2641f6d58c54SBarry Smith       */
2642f6d58c54SBarry Smith       ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2643f6d58c54SBarry Smith       ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
2644f6d58c54SBarry Smith       ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2645f6d58c54SBarry Smith       ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2646f6d58c54SBarry Smith 
2647f6d58c54SBarry Smith       /*
2648f6d58c54SBarry Smith 	Loop over rows of vector, putting results into Jacobian matrix
2649f6d58c54SBarry Smith       */
2650f6d58c54SBarry Smith       ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2651f6d58c54SBarry Smith       for (l=0; l<coloring->nrows[k]; l++) {
2652f6d58c54SBarry Smith 	row    = bs*coloring->rows[k][l];             /* local row index */
2653f6d58c54SBarry Smith 	col    = i + bs*coloring->columnsforrow[k][l];    /* global column index */
2654f6d58c54SBarry Smith         for (j=0; j<bs; j++) {
2655f6d58c54SBarry Smith   	  y[row+j] *= vscale_array[j+bs*vscaleforrow[k][l]];
2656f6d58c54SBarry Smith           srows[j]  = row + start + j;
2657f6d58c54SBarry Smith         }
2658f6d58c54SBarry Smith 	ierr   = MatSetValues(J,bs,srows,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2659f6d58c54SBarry Smith       }
2660f6d58c54SBarry Smith       ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2661f6d58c54SBarry Smith     }
2662f6d58c54SBarry Smith   } /* endof for each color */
2663f6d58c54SBarry Smith   if (ctype == IS_COLORING_GLOBAL) xx = xx + start;
2664f6d58c54SBarry Smith   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2665f6d58c54SBarry Smith   ierr = VecRestoreArray(x1_tmp,&xx);CHKERRQ(ierr);
2666f6d58c54SBarry Smith   ierr = PetscFree(srows);CHKERRQ(ierr);
2667f6d58c54SBarry Smith 
2668f6d58c54SBarry Smith   coloring->currentcolor = -1;
2669f6d58c54SBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2670f6d58c54SBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2671f6d58c54SBarry Smith   ierr = PetscLogEventEnd(MAT_FDColoringApply,coloring,J,x1,0);CHKERRQ(ierr);
2672f6d58c54SBarry Smith   PetscFunctionReturn(0);
2673f6d58c54SBarry Smith }
267499cafbc1SBarry Smith 
26752593348eSBarry Smith /* -------------------------------------------------------------------*/
2676cc2dc46cSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqBAIJ,
2677cc2dc46cSBarry Smith        MatGetRow_SeqBAIJ,
2678cc2dc46cSBarry Smith        MatRestoreRow_SeqBAIJ,
2679cc2dc46cSBarry Smith        MatMult_SeqBAIJ_N,
268097304618SKris Buschelman /* 4*/ MatMultAdd_SeqBAIJ_N,
26817c922b88SBarry Smith        MatMultTranspose_SeqBAIJ,
26827c922b88SBarry Smith        MatMultTransposeAdd_SeqBAIJ,
2683db4efbfdSBarry Smith        0,
2684cc2dc46cSBarry Smith        0,
2685cc2dc46cSBarry Smith        0,
268697304618SKris Buschelman /*10*/ 0,
2687cc2dc46cSBarry Smith        MatLUFactor_SeqBAIJ,
2688cc2dc46cSBarry Smith        0,
2689b6490206SBarry Smith        0,
2690f2501298SSatish Balay        MatTranspose_SeqBAIJ,
269197304618SKris Buschelman /*15*/ MatGetInfo_SeqBAIJ,
2692cc2dc46cSBarry Smith        MatEqual_SeqBAIJ,
2693cc2dc46cSBarry Smith        MatGetDiagonal_SeqBAIJ,
2694cc2dc46cSBarry Smith        MatDiagonalScale_SeqBAIJ,
2695cc2dc46cSBarry Smith        MatNorm_SeqBAIJ,
269697304618SKris Buschelman /*20*/ 0,
2697cc2dc46cSBarry Smith        MatAssemblyEnd_SeqBAIJ,
2698cc2dc46cSBarry Smith        MatSetOption_SeqBAIJ,
2699cc2dc46cSBarry Smith        MatZeroEntries_SeqBAIJ,
2700d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqBAIJ,
2701db4efbfdSBarry Smith        0,
2702db4efbfdSBarry Smith        0,
2703db4efbfdSBarry Smith        0,
2704db4efbfdSBarry Smith        0,
2705d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqBAIJ,
2706db4efbfdSBarry Smith        0,
2707db4efbfdSBarry Smith        0,
2708f2a5309cSSatish Balay        MatGetArray_SeqBAIJ,
2709f2a5309cSSatish Balay        MatRestoreArray_SeqBAIJ,
2710d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqBAIJ,
2711cc2dc46cSBarry Smith        0,
2712cc2dc46cSBarry Smith        0,
2713cc2dc46cSBarry Smith        MatILUFactor_SeqBAIJ,
2714cc2dc46cSBarry Smith        0,
2715d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqBAIJ,
2716cc2dc46cSBarry Smith        MatGetSubMatrices_SeqBAIJ,
2717cc2dc46cSBarry Smith        MatIncreaseOverlap_SeqBAIJ,
2718cc2dc46cSBarry Smith        MatGetValues_SeqBAIJ,
27193c896bc6SHong Zhang        MatCopy_SeqBAIJ,
2720d519adbfSMatthew Knepley /*44*/ 0,
2721cc2dc46cSBarry Smith        MatScale_SeqBAIJ,
2722cc2dc46cSBarry Smith        0,
2723cc2dc46cSBarry Smith        0,
2724fe97e370SBarry Smith        0,
272541c166b1SJed Brown /*49*/ MatSetBlockSize_SeqBAIJ,
27263b2fbd54SBarry Smith        MatGetRowIJ_SeqBAIJ,
272792c4ed94SBarry Smith        MatRestoreRowIJ_SeqBAIJ,
27283acb8795SBarry Smith        MatGetColumnIJ_SeqBAIJ,
27293acb8795SBarry Smith        MatRestoreColumnIJ_SeqBAIJ,
27303acb8795SBarry Smith /*54*/ MatFDColoringCreate_SeqAIJ,
2731cc2dc46cSBarry Smith        0,
2732cc2dc46cSBarry Smith        0,
2733cc2dc46cSBarry Smith        0,
2734d3825aa8SBarry Smith        MatSetValuesBlocked_SeqBAIJ,
2735d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_SeqBAIJ,
2736b9b97703SBarry Smith        MatDestroy_SeqBAIJ,
2737b9b97703SBarry Smith        MatView_SeqBAIJ,
2738357abbc8SBarry Smith        0,
2739273d9f13SBarry Smith        0,
2740d519adbfSMatthew Knepley /*64*/ 0,
2741273d9f13SBarry Smith        0,
2742273d9f13SBarry Smith        0,
2743273d9f13SBarry Smith        0,
2744273d9f13SBarry Smith        0,
2745d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqBAIJ,
2746273d9f13SBarry Smith        0,
2747c87e5d42SMatthew Knepley        MatConvert_Basic,
274897304618SKris Buschelman        0,
274997304618SKris Buschelman        0,
2750d519adbfSMatthew Knepley /*74*/ 0,
2751f6d58c54SBarry Smith        MatFDColoringApply_BAIJ,
275297304618SKris Buschelman        0,
275397304618SKris Buschelman        0,
275497304618SKris Buschelman        0,
2755d519adbfSMatthew Knepley /*79*/ 0,
275697304618SKris Buschelman        0,
275797304618SKris Buschelman        0,
275897304618SKris Buschelman        0,
2759865e5f61SKris Buschelman        MatLoad_SeqBAIJ,
2760d519adbfSMatthew Knepley /*84*/ 0,
2761b01c7715SBarry Smith        0,
2762b01c7715SBarry Smith        0,
2763b01c7715SBarry Smith        0,
2764865e5f61SKris Buschelman        0,
2765d519adbfSMatthew Knepley /*89*/ 0,
2766865e5f61SKris Buschelman        0,
2767865e5f61SKris Buschelman        0,
2768865e5f61SKris Buschelman        0,
2769865e5f61SKris Buschelman        0,
2770d519adbfSMatthew Knepley /*94*/ 0,
2771865e5f61SKris Buschelman        0,
2772865e5f61SKris Buschelman        0,
277399cafbc1SBarry Smith        0,
277499cafbc1SBarry Smith        0,
2775d519adbfSMatthew Knepley /*99*/0,
277699cafbc1SBarry Smith        0,
277799cafbc1SBarry Smith        0,
277899cafbc1SBarry Smith        0,
277999cafbc1SBarry Smith        0,
2780d519adbfSMatthew Knepley /*104*/0,
278199cafbc1SBarry Smith        MatRealPart_SeqBAIJ,
27822af78befSBarry Smith        MatImaginaryPart_SeqBAIJ,
27832af78befSBarry Smith        0,
27842af78befSBarry Smith        0,
2785d519adbfSMatthew Knepley /*109*/0,
27862af78befSBarry Smith        0,
27872af78befSBarry Smith        0,
27882af78befSBarry Smith        0,
2789547795f9SHong Zhang        MatMissingDiagonal_SeqBAIJ,
2790547795f9SHong Zhang /*114*/0,
2791547795f9SHong Zhang        0,
2792547795f9SHong Zhang        0,
2793547795f9SHong Zhang        0,
2794547795f9SHong Zhang        0,
2795547795f9SHong Zhang /*119*/0,
2796547795f9SHong Zhang        0,
2797547795f9SHong Zhang        MatMultHermitianTranspose_SeqBAIJ,
2798*f501eaabSShri Abhyankar        MatMultHermitianTransposeAdd_SeqBAIJ,
2799*f501eaabSShri Abhyankar        MatLoadnew_SeqBAIJ
280099cafbc1SBarry Smith };
28012593348eSBarry Smith 
28023e90b805SBarry Smith EXTERN_C_BEGIN
28034a2ae208SSatish Balay #undef __FUNCT__
28044a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqBAIJ"
2805be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqBAIJ(Mat mat)
28063e90b805SBarry Smith {
28073e90b805SBarry Smith   Mat_SeqBAIJ    *aij = (Mat_SeqBAIJ *)mat->data;
2808d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
2809dfbe8321SBarry Smith   PetscErrorCode ierr;
28103e90b805SBarry Smith 
28113e90b805SBarry Smith   PetscFunctionBegin;
2812e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
28133e90b805SBarry Smith 
28143e90b805SBarry Smith   /* allocate space for values if not already there */
28153e90b805SBarry Smith   if (!aij->saved_values) {
281687828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
28171784c0f5SBarry Smith     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
28183e90b805SBarry Smith   }
28193e90b805SBarry Smith 
28203e90b805SBarry Smith   /* copy values over */
282187828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
28223e90b805SBarry Smith   PetscFunctionReturn(0);
28233e90b805SBarry Smith }
28243e90b805SBarry Smith EXTERN_C_END
28253e90b805SBarry Smith 
28263e90b805SBarry Smith EXTERN_C_BEGIN
28274a2ae208SSatish Balay #undef __FUNCT__
28284a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqBAIJ"
2829be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqBAIJ(Mat mat)
28303e90b805SBarry Smith {
28313e90b805SBarry Smith   Mat_SeqBAIJ    *aij = (Mat_SeqBAIJ *)mat->data;
28326849ba73SBarry Smith   PetscErrorCode ierr;
2833d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
28343e90b805SBarry Smith 
28353e90b805SBarry Smith   PetscFunctionBegin;
2836e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2837e7e72b3dSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
28383e90b805SBarry Smith 
28393e90b805SBarry Smith   /* copy values over */
284087828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
28413e90b805SBarry Smith   PetscFunctionReturn(0);
28423e90b805SBarry Smith }
28433e90b805SBarry Smith EXTERN_C_END
28443e90b805SBarry Smith 
2845273d9f13SBarry Smith EXTERN_C_BEGIN
2846f69a0ea3SMatthew Knepley extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
2847f69a0ea3SMatthew Knepley extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqBAIJ_SeqSBAIJ(Mat, MatType,MatReuse,Mat*);
2848273d9f13SBarry Smith EXTERN_C_END
2849273d9f13SBarry Smith 
2850273d9f13SBarry Smith EXTERN_C_BEGIN
28514a2ae208SSatish Balay #undef __FUNCT__
2852a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqBAIJSetPreallocation_SeqBAIJ"
2853be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetPreallocation_SeqBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
2854a23d5eceSKris Buschelman {
2855a23d5eceSKris Buschelman   Mat_SeqBAIJ    *b;
28566849ba73SBarry Smith   PetscErrorCode ierr;
2857db4efbfdSBarry Smith   PetscInt       i,mbs,nbs,bs2,newbs = PetscAbs(bs);
2858ab93d7beSBarry Smith   PetscTruth     flg,skipallocation = PETSC_FALSE;
2859a23d5eceSKris Buschelman 
2860a23d5eceSKris Buschelman   PetscFunctionBegin;
2861a23d5eceSKris Buschelman 
2862ab93d7beSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2863ab93d7beSBarry Smith     skipallocation = PETSC_TRUE;
2864ab93d7beSBarry Smith     nz             = 0;
2865ab93d7beSBarry Smith   }
28668c07d4e3SBarry Smith 
2867db4efbfdSBarry Smith   if (bs < 0) {
28687adad957SLisandro Dalcin     ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Block options for SEQBAIJ matrix 1","Mat");CHKERRQ(ierr);
2869db4efbfdSBarry Smith       ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatSeqBAIJSetPreallocation",newbs,&newbs,PETSC_NULL);CHKERRQ(ierr);
28708c07d4e3SBarry Smith     ierr = PetscOptionsEnd();CHKERRQ(ierr);
2871db4efbfdSBarry Smith     bs   = PetscAbs(bs);
2872db4efbfdSBarry Smith   }
2873e7e72b3dSBarry Smith   if (nnz && newbs != bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot change blocksize from command line if setting nnz");
2874a23d5eceSKris Buschelman   bs   = newbs;
2875a23d5eceSKris Buschelman 
287626283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
287726283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
287826283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
287926283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2880899cda47SBarry Smith 
2881899cda47SBarry Smith   B->preallocated = PETSC_TRUE;
2882899cda47SBarry Smith 
2883d0f46423SBarry Smith   mbs  = B->rmap->n/bs;
2884d0f46423SBarry Smith   nbs  = B->cmap->n/bs;
2885a23d5eceSKris Buschelman   bs2  = bs*bs;
2886a23d5eceSKris Buschelman 
288765e19b50SBarry Smith   if (mbs*bs!=B->rmap->n || nbs*bs!=B->cmap->n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number rows %D, cols %D must be divisible by blocksize %D",B->rmap->N,B->cmap->n,bs);
2888a23d5eceSKris Buschelman 
2889a23d5eceSKris Buschelman   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2890e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
2891a23d5eceSKris Buschelman   if (nnz) {
2892a23d5eceSKris Buschelman     for (i=0; i<mbs; i++) {
2893e32f2f54SBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %D value %D",i,nnz[i]);
2894e32f2f54SBarry Smith       if (nnz[i] > nbs) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than block row length: local row %D value %D rowlength %D",i,nnz[i],nbs);
2895a23d5eceSKris Buschelman     }
2896a23d5eceSKris Buschelman   }
2897a23d5eceSKris Buschelman 
2898a23d5eceSKris Buschelman   b       = (Mat_SeqBAIJ*)B->data;
28997adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)B)->comm,PETSC_NULL,"Optimize options for SEQBAIJ matrix 2 ","Mat");CHKERRQ(ierr);
29008c07d4e3SBarry Smith     ierr = PetscOptionsTruth("-mat_no_unroll","Do not optimize for block size (slow)",PETSC_NULL,PETSC_FALSE,&flg,PETSC_NULL);CHKERRQ(ierr);
29018c07d4e3SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
29028c07d4e3SBarry Smith 
2903a23d5eceSKris Buschelman   if (!flg) {
2904a23d5eceSKris Buschelman     switch (bs) {
2905a23d5eceSKris Buschelman     case 1:
2906a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_1;
2907a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_1;
290841f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_1;
2909a23d5eceSKris Buschelman       break;
2910a23d5eceSKris Buschelman     case 2:
2911a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_2;
2912a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_2;
291341f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_2;
2914a23d5eceSKris Buschelman       break;
2915a23d5eceSKris Buschelman     case 3:
2916a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_3;
2917a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_3;
291841f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_3;
2919a23d5eceSKris Buschelman       break;
2920a23d5eceSKris Buschelman     case 4:
2921a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_4;
2922a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_4;
292341f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_4;
2924a23d5eceSKris Buschelman       break;
2925a23d5eceSKris Buschelman     case 5:
2926a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_5;
2927a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_5;
292841f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_5;
2929a23d5eceSKris Buschelman       break;
2930a23d5eceSKris Buschelman     case 6:
2931a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_6;
2932a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_6;
293341f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_6;
2934a23d5eceSKris Buschelman       break;
2935a23d5eceSKris Buschelman     case 7:
2936a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_7;
2937a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_7;
293841f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_7;
2939a23d5eceSKris Buschelman       break;
29408ab949d8SShri Abhyankar     case 15:
2941832cc040SShri Abhyankar       B->ops->mult = MatMult_SeqBAIJ_15_ver1;
29428ab949d8SShri Abhyankar       break;
2943a23d5eceSKris Buschelman     default:
2944a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_N;
2945a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_N;
2946a23d5eceSKris Buschelman       break;
2947a23d5eceSKris Buschelman     }
2948a23d5eceSKris Buschelman   }
2949d0f46423SBarry Smith   B->rmap->bs      = bs;
2950a23d5eceSKris Buschelman   b->mbs     = mbs;
2951a23d5eceSKris Buschelman   b->nbs     = nbs;
2952ab93d7beSBarry Smith   if (!skipallocation) {
29532ee49352SLisandro Dalcin     if (!b->imax) {
2954ab93d7beSBarry Smith       ierr = PetscMalloc2(mbs,PetscInt,&b->imax,mbs,PetscInt,&b->ilen);CHKERRQ(ierr);
29554fd072dbSBarry Smith       ierr = PetscLogObjectMemory(B,2*mbs*sizeof(PetscInt));
29564fd072dbSBarry Smith       b->free_imax_ilen = PETSC_TRUE;
29572ee49352SLisandro Dalcin     }
2958ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each block row so far. */
2959ab93d7beSBarry Smith     for (i=0; i<mbs; i++) { b->ilen[i] = 0;}
2960a23d5eceSKris Buschelman     if (!nnz) {
2961a23d5eceSKris Buschelman       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2962a23d5eceSKris Buschelman       else if (nz <= 0)        nz = 1;
2963a23d5eceSKris Buschelman       for (i=0; i<mbs; i++) b->imax[i] = nz;
2964a23d5eceSKris Buschelman       nz = nz*mbs;
2965a23d5eceSKris Buschelman     } else {
2966a23d5eceSKris Buschelman       nz = 0;
2967a23d5eceSKris Buschelman       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2968a23d5eceSKris Buschelman     }
2969a23d5eceSKris Buschelman 
2970a23d5eceSKris Buschelman     /* allocate the matrix space */
29712ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
2972d0f46423SBarry Smith     ierr = PetscMalloc3(bs2*nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->N+1,PetscInt,&b->i);CHKERRQ(ierr);
2973d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
2974a23d5eceSKris Buschelman     ierr  = PetscMemzero(b->a,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
2975c1ac3661SBarry Smith     ierr  = PetscMemzero(b->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
2976a23d5eceSKris Buschelman     b->singlemalloc = PETSC_TRUE;
2977a23d5eceSKris Buschelman     b->i[0] = 0;
2978a23d5eceSKris Buschelman     for (i=1; i<mbs+1; i++) {
2979a23d5eceSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
2980a23d5eceSKris Buschelman     }
2981e6b907acSBarry Smith     b->free_a     = PETSC_TRUE;
2982e6b907acSBarry Smith     b->free_ij    = PETSC_TRUE;
2983e811da20SHong Zhang   } else {
2984e6b907acSBarry Smith     b->free_a     = PETSC_FALSE;
2985e6b907acSBarry Smith     b->free_ij    = PETSC_FALSE;
2986ab93d7beSBarry Smith   }
2987a23d5eceSKris Buschelman 
2988d0f46423SBarry Smith   B->rmap->bs          = bs;
2989a23d5eceSKris Buschelman   b->bs2              = bs2;
2990a23d5eceSKris Buschelman   b->mbs              = mbs;
2991a23d5eceSKris Buschelman   b->nz               = 0;
2992a23d5eceSKris Buschelman   b->maxnz            = nz*bs2;
2993a23d5eceSKris Buschelman   B->info.nz_unneeded = (PetscReal)b->maxnz;
2994a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2995a23d5eceSKris Buschelman }
2996a23d5eceSKris Buschelman EXTERN_C_END
2997a23d5eceSKris Buschelman 
2998b24902e0SBarry Smith EXTERN_C_BEGIN
2999725b52f3SLisandro Dalcin #undef __FUNCT__
3000725b52f3SLisandro Dalcin #define __FUNCT__ "MatSeqBAIJSetPreallocationCSR_SeqBAIJ"
3001cf12db73SBarry Smith PetscErrorCode MatSeqBAIJSetPreallocationCSR_SeqBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
3002725b52f3SLisandro Dalcin {
3003725b52f3SLisandro Dalcin   PetscInt       i,m,nz,nz_max=0,*nnz;
3004725b52f3SLisandro Dalcin   PetscScalar    *values=0;
3005725b52f3SLisandro Dalcin   PetscErrorCode ierr;
3006725b52f3SLisandro Dalcin 
3007725b52f3SLisandro Dalcin   PetscFunctionBegin;
3008e32f2f54SBarry Smith   if (bs < 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
300926283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
301026283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
301126283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
301226283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3013d0f46423SBarry Smith   m = B->rmap->n/bs;
3014725b52f3SLisandro Dalcin 
3015e32f2f54SBarry Smith   if (ii[0] != 0) { SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "ii[0] must be 0 but it is %D",ii[0]); }
3016725b52f3SLisandro Dalcin   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3017725b52f3SLisandro Dalcin   for(i=0; i<m; i++) {
3018cf12db73SBarry Smith     nz = ii[i+1]- ii[i];
3019e32f2f54SBarry Smith     if (nz < 0) { SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D",i,nz); }
3020725b52f3SLisandro Dalcin     nz_max = PetscMax(nz_max, nz);
3021725b52f3SLisandro Dalcin     nnz[i] = nz;
3022725b52f3SLisandro Dalcin   }
3023725b52f3SLisandro Dalcin   ierr = MatSeqBAIJSetPreallocation(B,bs,0,nnz);CHKERRQ(ierr);
3024725b52f3SLisandro Dalcin   ierr = PetscFree(nnz);CHKERRQ(ierr);
3025725b52f3SLisandro Dalcin 
3026725b52f3SLisandro Dalcin   values = (PetscScalar*)V;
3027725b52f3SLisandro Dalcin   if (!values) {
3028725b52f3SLisandro Dalcin     ierr = PetscMalloc(bs*bs*(nz_max+1)*sizeof(PetscScalar),&values);CHKERRQ(ierr);
3029725b52f3SLisandro Dalcin     ierr = PetscMemzero(values,bs*bs*nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3030725b52f3SLisandro Dalcin   }
3031725b52f3SLisandro Dalcin   for (i=0; i<m; i++) {
3032cf12db73SBarry Smith     PetscInt          ncols  = ii[i+1] - ii[i];
3033cf12db73SBarry Smith     const PetscInt    *icols = jj + ii[i];
3034cf12db73SBarry Smith     const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
3035725b52f3SLisandro Dalcin     ierr = MatSetValuesBlocked_SeqBAIJ(B,1,&i,ncols,icols,svals,INSERT_VALUES);CHKERRQ(ierr);
3036725b52f3SLisandro Dalcin   }
3037725b52f3SLisandro Dalcin   if (!V) { ierr = PetscFree(values);CHKERRQ(ierr); }
3038725b52f3SLisandro Dalcin   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3039725b52f3SLisandro Dalcin   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3040725b52f3SLisandro Dalcin 
3041725b52f3SLisandro Dalcin   PetscFunctionReturn(0);
3042725b52f3SLisandro Dalcin }
3043725b52f3SLisandro Dalcin EXTERN_C_END
3044725b52f3SLisandro Dalcin 
3045725b52f3SLisandro Dalcin 
3046207126cbSBarry Smith EXTERN_C_BEGIN
3047b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqbaij_petsc(Mat,MatFactorType,Mat*);
304867877ebaSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
3049bccb9932SShri Abhyankar extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_baij_mumps(Mat,MatFactorType,Mat*);
305067877ebaSShri Abhyankar #endif
3051db4efbfdSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqbaij_petsc(Mat,MatFactorType,Mat*);
3052b24902e0SBarry Smith EXTERN_C_END
3053b24902e0SBarry Smith 
30540bad9183SKris Buschelman /*MC
3055fafad747SKris Buschelman    MATSEQBAIJ - MATSEQBAIJ = "seqbaij" - A matrix type to be used for sequential block sparse matrices, based on
30560bad9183SKris Buschelman    block sparse compressed row format.
30570bad9183SKris Buschelman 
30580bad9183SKris Buschelman    Options Database Keys:
30590bad9183SKris Buschelman . -mat_type seqbaij - sets the matrix type to "seqbaij" during a call to MatSetFromOptions()
30600bad9183SKris Buschelman 
30610bad9183SKris Buschelman   Level: beginner
30620bad9183SKris Buschelman 
3063f0c06035SSatish Balay .seealso: MatCreateSeqBAIJ()
30640bad9183SKris Buschelman M*/
30650bad9183SKris Buschelman 
3066b24902e0SBarry Smith 
3067a23d5eceSKris Buschelman EXTERN_C_BEGIN
3068a23d5eceSKris Buschelman #undef __FUNCT__
30694a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqBAIJ"
3070be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqBAIJ(Mat B)
30712593348eSBarry Smith {
3072dfbe8321SBarry Smith   PetscErrorCode ierr;
3073c1ac3661SBarry Smith   PetscMPIInt    size;
3074b6490206SBarry Smith   Mat_SeqBAIJ    *b;
30753b2fbd54SBarry Smith 
30763a40ed3dSBarry Smith   PetscFunctionBegin;
30777adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3078e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");
3079b6490206SBarry Smith 
308038f2d2fdSLisandro Dalcin   ierr    = PetscNewLog(B,Mat_SeqBAIJ,&b);CHKERRQ(ierr);
3081b0a32e0cSBarry Smith   B->data = (void*)b;
3082549d3d68SSatish Balay   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
308390f02eecSBarry Smith   B->mapping               = 0;
30842593348eSBarry Smith   b->row                   = 0;
30852593348eSBarry Smith   b->col                   = 0;
3086e51c0b9cSSatish Balay   b->icol                  = 0;
30872593348eSBarry Smith   b->reallocs              = 0;
30883e90b805SBarry Smith   b->saved_values          = 0;
30892593348eSBarry Smith 
3090c4992f7dSBarry Smith   b->roworiented           = PETSC_TRUE;
30912593348eSBarry Smith   b->nonew                 = 0;
30922593348eSBarry Smith   b->diag                  = 0;
30932593348eSBarry Smith   b->solve_work            = 0;
3094de6a44a3SBarry Smith   b->mult_work             = 0;
30952a1b7f2aSHong Zhang   B->spptr                 = 0;
30960e6d2581SBarry Smith   B->info.nz_unneeded      = (PetscReal)b->maxnz;
3097a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3098c4319e64SHong Zhang   b->xtoy                  = 0;
3099c4319e64SHong Zhang   b->XtoY                  = 0;
310073e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
310126e093fcSHong Zhang   b->compressedrow.nrows   = 0;
310273e7a558SHong Zhang   b->compressedrow.i       = PETSC_NULL;
310373e7a558SHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
310473e7a558SHong Zhang   b->compressedrow.checked = PETSC_FALSE;
310588e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
31064e220ebcSLois Curfman McInnes 
3107ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
3108db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqbaij_petsc",
3109db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqbaij_petsc);CHKERRQ(ierr);
3110ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
3111b24902e0SBarry Smith                                      "MatGetFactor_seqbaij_petsc",
3112b24902e0SBarry Smith                                      MatGetFactor_seqbaij_petsc);CHKERRQ(ierr);
311367877ebaSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
3114bccb9932SShri Abhyankar   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", "MatGetFactor_baij_mumps", MatGetFactor_baij_mumps);CHKERRQ(ierr);
311567877ebaSShri Abhyankar #endif
311643516a2dSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqBAIJInvertBlockDiagonal_C",
311743516a2dSKris Buschelman                                      "MatInvertBlockDiagonal_SeqBAIJ",
311843516a2dSKris Buschelman                                       MatInvertBlockDiagonal_SeqBAIJ);CHKERRQ(ierr);
3119f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
31203e90b805SBarry Smith                                      "MatStoreValues_SeqBAIJ",
3121bc4b532fSSatish Balay                                       MatStoreValues_SeqBAIJ);CHKERRQ(ierr);
3122f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
31233e90b805SBarry Smith                                      "MatRetrieveValues_SeqBAIJ",
3124bc4b532fSSatish Balay                                       MatRetrieveValues_SeqBAIJ);CHKERRQ(ierr);
3125f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqBAIJSetColumnIndices_C",
312627a8da17SBarry Smith                                      "MatSeqBAIJSetColumnIndices_SeqBAIJ",
3127bc4b532fSSatish Balay                                       MatSeqBAIJSetColumnIndices_SeqBAIJ);CHKERRQ(ierr);
3128a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqbaij_seqaij_C",
3129273d9f13SBarry Smith                                      "MatConvert_SeqBAIJ_SeqAIJ",
3130273d9f13SBarry Smith                                       MatConvert_SeqBAIJ_SeqAIJ);CHKERRQ(ierr);
3131a0e1a404SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqbaij_seqsbaij_C",
3132a0e1a404SHong Zhang                                      "MatConvert_SeqBAIJ_SeqSBAIJ",
3133a0e1a404SHong Zhang                                       MatConvert_SeqBAIJ_SeqSBAIJ);CHKERRQ(ierr);
3134a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqBAIJSetPreallocation_C",
3135a23d5eceSKris Buschelman                                      "MatSeqBAIJSetPreallocation_SeqBAIJ",
3136a23d5eceSKris Buschelman                                       MatSeqBAIJSetPreallocation_SeqBAIJ);CHKERRQ(ierr);
3137725b52f3SLisandro Dalcin   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqBAIJSetPreallocationCSR_C",
3138725b52f3SLisandro Dalcin                                      "MatSeqBAIJSetPreallocationCSR_SeqBAIJ",
3139725b52f3SLisandro Dalcin                                       MatSeqBAIJSetPreallocationCSR_SeqBAIJ);CHKERRQ(ierr);
314017667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQBAIJ);CHKERRQ(ierr);
31413a40ed3dSBarry Smith   PetscFunctionReturn(0);
31422593348eSBarry Smith }
3143273d9f13SBarry Smith EXTERN_C_END
31442593348eSBarry Smith 
31454a2ae208SSatish Balay #undef __FUNCT__
3146b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqBAIJ"
314716a2bf60SHong Zhang PetscErrorCode MatDuplicateNoCreate_SeqBAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscTruth mallocmatspace)
31482593348eSBarry Smith {
3149b24902e0SBarry Smith   Mat_SeqBAIJ    *c = (Mat_SeqBAIJ*)C->data,*a = (Mat_SeqBAIJ*)A->data;
31506849ba73SBarry Smith   PetscErrorCode ierr;
3151a96a251dSBarry Smith   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 = a->bs2;
3152de6a44a3SBarry Smith 
31533a40ed3dSBarry Smith   PetscFunctionBegin;
3154e32f2f54SBarry Smith   if (a->i[mbs] != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt matrix");
31552593348eSBarry Smith 
31564fd072dbSBarry Smith   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
31574fd072dbSBarry Smith     c->imax = a->imax;
31584fd072dbSBarry Smith     c->ilen = a->ilen;
31594fd072dbSBarry Smith     c->free_imax_ilen = PETSC_FALSE;
31604fd072dbSBarry Smith   } else {
316133b91e9fSSatish Balay     ierr = PetscMalloc2(mbs,PetscInt,&c->imax,mbs,PetscInt,&c->ilen);CHKERRQ(ierr);
31624fd072dbSBarry Smith     ierr = PetscLogObjectMemory(C,2*mbs*sizeof(PetscInt));CHKERRQ(ierr);
3163b6490206SBarry Smith     for (i=0; i<mbs; i++) {
31642593348eSBarry Smith       c->imax[i] = a->imax[i];
31652593348eSBarry Smith       c->ilen[i] = a->ilen[i];
31662593348eSBarry Smith     }
31674fd072dbSBarry Smith     c->free_imax_ilen = PETSC_TRUE;
31684fd072dbSBarry Smith   }
31692593348eSBarry Smith 
31702593348eSBarry Smith   /* allocate the matrix space */
317116a2bf60SHong Zhang   if (mallocmatspace){
31724fd072dbSBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
31734fd072dbSBarry Smith       ierr = PetscMalloc(bs2*nz*sizeof(PetscScalar),&c->a);CHKERRQ(ierr);
31744fd072dbSBarry Smith       ierr = PetscLogObjectMemory(C,a->i[mbs]*bs2*sizeof(PetscScalar));CHKERRQ(ierr);
31754fd072dbSBarry Smith       c->singlemalloc = PETSC_FALSE;
31764fd072dbSBarry Smith       c->free_ij      = PETSC_FALSE;
31774fd072dbSBarry Smith       c->i            = a->i;
31784fd072dbSBarry Smith       c->j            = a->j;
31794fd072dbSBarry Smith       c->parent       = A;
31804fd072dbSBarry Smith       ierr            = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
31814fd072dbSBarry Smith       ierr            = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
31824fd072dbSBarry Smith       ierr            = MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
31834fd072dbSBarry Smith     } else {
3184a96a251dSBarry Smith       ierr = PetscMalloc3(bs2*nz,PetscScalar,&c->a,nz,PetscInt,&c->j,mbs+1,PetscInt,&c->i);CHKERRQ(ierr);
318516a2bf60SHong Zhang       ierr = PetscLogObjectMemory(C,a->i[mbs]*(bs2*sizeof(PetscScalar)+sizeof(PetscInt))+(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
3186c4992f7dSBarry Smith       c->singlemalloc = PETSC_TRUE;
31874fd072dbSBarry Smith       c->free_ij      = PETSC_TRUE;
3188c1ac3661SBarry Smith       ierr = PetscMemcpy(c->i,a->i,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
3189b6490206SBarry Smith       if (mbs > 0) {
3190c1ac3661SBarry Smith 	ierr = PetscMemcpy(c->j,a->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
31912e8a6d31SBarry Smith 	if (cpvalues == MAT_COPY_VALUES) {
3192549d3d68SSatish Balay 	  ierr = PetscMemcpy(c->a,a->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
31932e8a6d31SBarry Smith 	} else {
3194549d3d68SSatish Balay 	  ierr = PetscMemzero(c->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
31952593348eSBarry Smith 	}
31962593348eSBarry Smith       }
319716a2bf60SHong Zhang     }
31984fd072dbSBarry Smith   }
319916a2bf60SHong Zhang 
32002593348eSBarry Smith   c->roworiented = a->roworiented;
32012593348eSBarry Smith   c->nonew       = a->nonew;
320226283091SBarry Smith   ierr = PetscLayoutCopy(A->rmap,&C->rmap);CHKERRQ(ierr);
320326283091SBarry Smith   ierr = PetscLayoutCopy(A->cmap,&C->cmap);CHKERRQ(ierr);
32045c9eb25fSBarry Smith   c->bs2         = a->bs2;
32055c9eb25fSBarry Smith   c->mbs         = a->mbs;
32065c9eb25fSBarry Smith   c->nbs         = a->nbs;
32072593348eSBarry Smith 
32082593348eSBarry Smith   if (a->diag) {
32094fd072dbSBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
32104fd072dbSBarry Smith       c->diag      = a->diag;
32114fd072dbSBarry Smith       c->free_diag = PETSC_FALSE;
32124fd072dbSBarry Smith     } else {
3213c1ac3661SBarry Smith       ierr = PetscMalloc((mbs+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
321452e6d16bSBarry Smith       ierr = PetscLogObjectMemory(C,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
3215b6490206SBarry Smith       for (i=0; i<mbs; i++) {
32162593348eSBarry Smith         c->diag[i] = a->diag[i];
32172593348eSBarry Smith       }
32184fd072dbSBarry Smith       c->free_diag = PETSC_TRUE;
32194fd072dbSBarry Smith     }
322098305bb5SBarry Smith   } else c->diag        = 0;
32212593348eSBarry Smith   c->nz                 = a->nz;
32228e9a0fb8SHong Zhang   c->maxnz              = bs2*a->nz; /* Since we allocate exactly the right amount */
32232593348eSBarry Smith   c->solve_work         = 0;
32247fc0212eSBarry Smith   c->mult_work          = 0;
3225e6b907acSBarry Smith   c->free_a             = PETSC_TRUE;
3226e6b907acSBarry Smith   c->free_ij            = PETSC_TRUE;
3227273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3228273d9f13SBarry Smith   C->assembled          = PETSC_TRUE;
322988e51ccdSHong Zhang 
323088e51ccdSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
323188e51ccdSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
323288e51ccdSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
323388e51ccdSHong Zhang   if (a->compressedrow.checked && a->compressedrow.use){
323488e51ccdSHong Zhang     i = a->compressedrow.nrows;
32350e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i+1,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
32364fd072dbSBarry Smith     ierr = PetscLogObjectMemory(C,(2*i+1)*sizeof(PetscInt));CHKERRQ(ierr);
323788e51ccdSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
323888e51ccdSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
323988e51ccdSHong Zhang   } else {
324088e51ccdSHong Zhang     c->compressedrow.use    = PETSC_FALSE;
324188e51ccdSHong Zhang     c->compressedrow.i      = PETSC_NULL;
324288e51ccdSHong Zhang     c->compressedrow.rindex = PETSC_NULL;
324388e51ccdSHong Zhang   }
324488e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
32457adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
32465d5aaa0eSBarry Smith   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
32473a40ed3dSBarry Smith   PetscFunctionReturn(0);
32482593348eSBarry Smith }
32492593348eSBarry Smith 
32504a2ae208SSatish Balay #undef __FUNCT__
3251b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqBAIJ"
3252b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3253b24902e0SBarry Smith {
3254b24902e0SBarry Smith     PetscErrorCode ierr;
3255b24902e0SBarry Smith 
3256b24902e0SBarry Smith   PetscFunctionBegin;
32575c9eb25fSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
3258d0f46423SBarry Smith   ierr = MatSetSizes(*B,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);CHKERRQ(ierr);
32595c9eb25fSBarry Smith   ierr = MatSetType(*B,MATSEQBAIJ);CHKERRQ(ierr);
326016a2bf60SHong Zhang   ierr = MatDuplicateNoCreate_SeqBAIJ(*B,A,cpvalues,PETSC_TRUE);
3261b24902e0SBarry Smith   PetscFunctionReturn(0);
3262b24902e0SBarry Smith }
3263b24902e0SBarry Smith 
3264b24902e0SBarry Smith #undef __FUNCT__
32654a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqBAIJ"
3266a313700dSBarry Smith PetscErrorCode MatLoad_SeqBAIJ(PetscViewer viewer, const MatType type,Mat *A)
32672593348eSBarry Smith {
3268b6490206SBarry Smith   Mat_SeqBAIJ    *a;
32692593348eSBarry Smith   Mat            B;
32706849ba73SBarry Smith   PetscErrorCode ierr;
3271b24ad042SBarry Smith   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs=1;
3272c1ac3661SBarry Smith   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*browlengths,maskcount;
3273c1ac3661SBarry Smith   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows;
3274c1ac3661SBarry Smith   PetscInt       *masked,nmask,tmp,bs2,ishift;
3275b24ad042SBarry Smith   PetscMPIInt    size;
3276b24ad042SBarry Smith   int            fd;
327787828ca2SBarry Smith   PetscScalar    *aa;
327819bcc07fSBarry Smith   MPI_Comm       comm = ((PetscObject)viewer)->comm;
32792593348eSBarry Smith 
32803a40ed3dSBarry Smith   PetscFunctionBegin;
32818c07d4e3SBarry Smith   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading SEQBAIJ matrix","Mat");CHKERRQ(ierr);
32828c07d4e3SBarry Smith     ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
32838c07d4e3SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3284de6a44a3SBarry Smith   bs2  = bs*bs;
3285b6490206SBarry Smith 
3286d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3287e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
3288b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
32890752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3290e32f2f54SBarry Smith   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
32912593348eSBarry Smith   M = header[1]; N = header[2]; nz = header[3];
32922593348eSBarry Smith 
3293e7e72b3dSBarry Smith   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqBAIJ");
3294e32f2f54SBarry Smith   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
329535aab85fSBarry Smith 
329635aab85fSBarry Smith   /*
329735aab85fSBarry Smith      This code adds extra rows to make sure the number of rows is
329835aab85fSBarry Smith     divisible by the blocksize
329935aab85fSBarry Smith   */
3300b6490206SBarry Smith   mbs        = M/bs;
330135aab85fSBarry Smith   extra_rows = bs - M + bs*(mbs);
330235aab85fSBarry Smith   if (extra_rows == bs) extra_rows = 0;
330335aab85fSBarry Smith   else                  mbs++;
330435aab85fSBarry Smith   if (extra_rows) {
33051e2582c4SBarry Smith     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
330635aab85fSBarry Smith   }
3307b6490206SBarry Smith 
33082593348eSBarry Smith   /* read in row lengths */
3309c1ac3661SBarry Smith   ierr = PetscMalloc((M+extra_rows)*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
33100752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
331135aab85fSBarry Smith   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
33122593348eSBarry Smith 
3313b6490206SBarry Smith   /* read in column indices */
3314c1ac3661SBarry Smith   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscInt),&jj);CHKERRQ(ierr);
33150752156aSBarry Smith   ierr = PetscBinaryRead(fd,jj,nz,PETSC_INT);CHKERRQ(ierr);
331635aab85fSBarry Smith   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;
3317b6490206SBarry Smith 
3318b6490206SBarry Smith   /* loop over row lengths determining block row lengths */
3319c1ac3661SBarry Smith   ierr     = PetscMalloc(mbs*sizeof(PetscInt),&browlengths);CHKERRQ(ierr);
3320c1ac3661SBarry Smith   ierr     = PetscMemzero(browlengths,mbs*sizeof(PetscInt));CHKERRQ(ierr);
3321fca92195SBarry Smith   ierr     = PetscMalloc2(mbs,PetscInt,&mask,mbs,PetscInt,&masked);CHKERRQ(ierr);
3322c1ac3661SBarry Smith   ierr     = PetscMemzero(mask,mbs*sizeof(PetscInt));CHKERRQ(ierr);
3323fca92195SBarry Smith   rowcount = 0;
3324fca92195SBarry Smith   nzcount = 0;
3325b6490206SBarry Smith   for (i=0; i<mbs; i++) {
332635aab85fSBarry Smith     nmask = 0;
3327b6490206SBarry Smith     for (j=0; j<bs; j++) {
3328b6490206SBarry Smith       kmax = rowlengths[rowcount];
3329b6490206SBarry Smith       for (k=0; k<kmax; k++) {
333035aab85fSBarry Smith         tmp = jj[nzcount++]/bs;
333135aab85fSBarry Smith         if (!mask[tmp]) {masked[nmask++] = tmp; mask[tmp] = 1;}
3332b6490206SBarry Smith       }
3333b6490206SBarry Smith       rowcount++;
3334b6490206SBarry Smith     }
333535aab85fSBarry Smith     browlengths[i] += nmask;
333635aab85fSBarry Smith     /* zero out the mask elements we set */
333735aab85fSBarry Smith     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
3338b6490206SBarry Smith   }
3339b6490206SBarry Smith 
33402593348eSBarry Smith   /* create our matrix */
3341f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);
3342f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);
334378ae41b4SKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3344ab93d7beSBarry Smith   ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(B,bs,0,browlengths);CHKERRQ(ierr);
3345b6490206SBarry Smith   a = (Mat_SeqBAIJ*)B->data;
33462593348eSBarry Smith 
33472593348eSBarry Smith   /* set matrix "i" values */
3348de6a44a3SBarry Smith   a->i[0] = 0;
3349b6490206SBarry Smith   for (i=1; i<= mbs; i++) {
3350b6490206SBarry Smith     a->i[i]      = a->i[i-1] + browlengths[i-1];
3351b6490206SBarry Smith     a->ilen[i-1] = browlengths[i-1];
33522593348eSBarry Smith   }
3353b6490206SBarry Smith   a->nz         = 0;
3354b6490206SBarry Smith   for (i=0; i<mbs; i++) a->nz += browlengths[i];
33552593348eSBarry Smith 
3356b6490206SBarry Smith   /* read in nonzero values */
335787828ca2SBarry Smith   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscScalar),&aa);CHKERRQ(ierr);
33580752156aSBarry Smith   ierr = PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);CHKERRQ(ierr);
335935aab85fSBarry Smith   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;
3360b6490206SBarry Smith 
3361b6490206SBarry Smith   /* set "a" and "j" values into matrix */
3362b6490206SBarry Smith   nzcount = 0; jcount = 0;
3363b6490206SBarry Smith   for (i=0; i<mbs; i++) {
3364b6490206SBarry Smith     nzcountb = nzcount;
336535aab85fSBarry Smith     nmask    = 0;
3366b6490206SBarry Smith     for (j=0; j<bs; j++) {
3367b6490206SBarry Smith       kmax = rowlengths[i*bs+j];
3368b6490206SBarry Smith       for (k=0; k<kmax; k++) {
336935aab85fSBarry Smith         tmp = jj[nzcount++]/bs;
337035aab85fSBarry Smith 	if (!mask[tmp]) { masked[nmask++] = tmp; mask[tmp] = 1;}
3371b6490206SBarry Smith       }
3372b6490206SBarry Smith     }
3373de6a44a3SBarry Smith     /* sort the masked values */
3374433994e6SBarry Smith     ierr = PetscSortInt(nmask,masked);CHKERRQ(ierr);
3375de6a44a3SBarry Smith 
3376b6490206SBarry Smith     /* set "j" values into matrix */
3377b6490206SBarry Smith     maskcount = 1;
337835aab85fSBarry Smith     for (j=0; j<nmask; j++) {
337935aab85fSBarry Smith       a->j[jcount++]  = masked[j];
3380de6a44a3SBarry Smith       mask[masked[j]] = maskcount++;
3381b6490206SBarry Smith     }
3382b6490206SBarry Smith     /* set "a" values into matrix */
3383de6a44a3SBarry Smith     ishift = bs2*a->i[i];
3384b6490206SBarry Smith     for (j=0; j<bs; j++) {
3385b6490206SBarry Smith       kmax = rowlengths[i*bs+j];
3386b6490206SBarry Smith       for (k=0; k<kmax; k++) {
3387de6a44a3SBarry Smith         tmp       = jj[nzcountb]/bs ;
3388de6a44a3SBarry Smith         block     = mask[tmp] - 1;
3389de6a44a3SBarry Smith         point     = jj[nzcountb] - bs*tmp;
3390de6a44a3SBarry Smith         idx       = ishift + bs2*block + j + bs*point;
3391375fe846SBarry Smith         a->a[idx] = (MatScalar)aa[nzcountb++];
3392b6490206SBarry Smith       }
3393b6490206SBarry Smith     }
339435aab85fSBarry Smith     /* zero out the mask elements we set */
339535aab85fSBarry Smith     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
3396b6490206SBarry Smith   }
3397e32f2f54SBarry Smith   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");
3398b6490206SBarry Smith 
3399606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3400606d414cSSatish Balay   ierr = PetscFree(browlengths);CHKERRQ(ierr);
3401606d414cSSatish Balay   ierr = PetscFree(aa);CHKERRQ(ierr);
3402606d414cSSatish Balay   ierr = PetscFree(jj);CHKERRQ(ierr);
3403fca92195SBarry Smith   ierr = PetscFree2(mask,masked);CHKERRQ(ierr);
3404b6490206SBarry Smith 
340578ae41b4SKris Buschelman   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
340678ae41b4SKris Buschelman   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
34079c01be13SBarry Smith   ierr = MatView_Private(B);CHKERRQ(ierr);
340878ae41b4SKris Buschelman 
340978ae41b4SKris Buschelman   *A = B;
34103a40ed3dSBarry Smith   PetscFunctionReturn(0);
34112593348eSBarry Smith }
34122593348eSBarry Smith 
34134a2ae208SSatish Balay #undef __FUNCT__
3414*f501eaabSShri Abhyankar #define __FUNCT__ "MatLoadnew_SeqBAIJ"
3415*f501eaabSShri Abhyankar PetscErrorCode MatLoadnew_SeqBAIJ(PetscViewer viewer,Mat newmat)
3416*f501eaabSShri Abhyankar {
3417*f501eaabSShri Abhyankar   Mat_SeqBAIJ    *a;
3418*f501eaabSShri Abhyankar   PetscErrorCode ierr;
3419*f501eaabSShri Abhyankar   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs=1;
3420*f501eaabSShri Abhyankar   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*browlengths,maskcount;
3421*f501eaabSShri Abhyankar   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows,rows,cols;
3422*f501eaabSShri Abhyankar   PetscInt       *masked,nmask,tmp,bs2,ishift;
3423*f501eaabSShri Abhyankar   PetscMPIInt    size;
3424*f501eaabSShri Abhyankar   int            fd;
3425*f501eaabSShri Abhyankar   PetscScalar    *aa;
3426*f501eaabSShri Abhyankar   MPI_Comm       comm = ((PetscObject)viewer)->comm;
3427*f501eaabSShri Abhyankar 
3428*f501eaabSShri Abhyankar   PetscFunctionBegin;
3429*f501eaabSShri Abhyankar   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading SEQBAIJ matrix","Mat");CHKERRQ(ierr);
3430*f501eaabSShri Abhyankar   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
3431*f501eaabSShri Abhyankar   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3432*f501eaabSShri Abhyankar   bs2  = bs*bs;
3433*f501eaabSShri Abhyankar 
3434*f501eaabSShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3435*f501eaabSShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
3436*f501eaabSShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
3437*f501eaabSShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3438*f501eaabSShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
3439*f501eaabSShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
3440*f501eaabSShri Abhyankar 
3441*f501eaabSShri Abhyankar   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqBAIJ");
3442*f501eaabSShri Abhyankar   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
3443*f501eaabSShri Abhyankar 
3444*f501eaabSShri Abhyankar   /*
3445*f501eaabSShri Abhyankar      This code adds extra rows to make sure the number of rows is
3446*f501eaabSShri Abhyankar     divisible by the blocksize
3447*f501eaabSShri Abhyankar   */
3448*f501eaabSShri Abhyankar   mbs        = M/bs;
3449*f501eaabSShri Abhyankar   extra_rows = bs - M + bs*(mbs);
3450*f501eaabSShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
3451*f501eaabSShri Abhyankar   else                  mbs++;
3452*f501eaabSShri Abhyankar   if (extra_rows) {
3453*f501eaabSShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
3454*f501eaabSShri Abhyankar   }
3455*f501eaabSShri Abhyankar 
3456*f501eaabSShri Abhyankar   /* Set global sizes if not already set */
3457*f501eaabSShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
3458*f501eaabSShri Abhyankar     ierr = MatSetSizes(newmat,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
3459*f501eaabSShri Abhyankar   } else { /* Check if the matrix global sizes are correct */
3460*f501eaabSShri Abhyankar     ierr = MatGetSize(newmat,&rows,&cols);CHKERRQ(ierr);
3461*f501eaabSShri Abhyankar     if (M != rows ||  N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix in file of different length (%d, %d) than the input matrix (%d, %d)",M,N,rows,cols);
3462*f501eaabSShri Abhyankar   }
3463*f501eaabSShri Abhyankar 
3464*f501eaabSShri Abhyankar   /* read in row lengths */
3465*f501eaabSShri Abhyankar   ierr = PetscMalloc((M+extra_rows)*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
3466*f501eaabSShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
3467*f501eaabSShri Abhyankar   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
3468*f501eaabSShri Abhyankar 
3469*f501eaabSShri Abhyankar   /* read in column indices */
3470*f501eaabSShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscInt),&jj);CHKERRQ(ierr);
3471*f501eaabSShri Abhyankar   ierr = PetscBinaryRead(fd,jj,nz,PETSC_INT);CHKERRQ(ierr);
3472*f501eaabSShri Abhyankar   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;
3473*f501eaabSShri Abhyankar 
3474*f501eaabSShri Abhyankar   /* loop over row lengths determining block row lengths */
3475*f501eaabSShri Abhyankar   ierr     = PetscMalloc(mbs*sizeof(PetscInt),&browlengths);CHKERRQ(ierr);
3476*f501eaabSShri Abhyankar   ierr     = PetscMemzero(browlengths,mbs*sizeof(PetscInt));CHKERRQ(ierr);
3477*f501eaabSShri Abhyankar   ierr     = PetscMalloc2(mbs,PetscInt,&mask,mbs,PetscInt,&masked);CHKERRQ(ierr);
3478*f501eaabSShri Abhyankar   ierr     = PetscMemzero(mask,mbs*sizeof(PetscInt));CHKERRQ(ierr);
3479*f501eaabSShri Abhyankar   rowcount = 0;
3480*f501eaabSShri Abhyankar   nzcount = 0;
3481*f501eaabSShri Abhyankar   for (i=0; i<mbs; i++) {
3482*f501eaabSShri Abhyankar     nmask = 0;
3483*f501eaabSShri Abhyankar     for (j=0; j<bs; j++) {
3484*f501eaabSShri Abhyankar       kmax = rowlengths[rowcount];
3485*f501eaabSShri Abhyankar       for (k=0; k<kmax; k++) {
3486*f501eaabSShri Abhyankar         tmp = jj[nzcount++]/bs;
3487*f501eaabSShri Abhyankar         if (!mask[tmp]) {masked[nmask++] = tmp; mask[tmp] = 1;}
3488*f501eaabSShri Abhyankar       }
3489*f501eaabSShri Abhyankar       rowcount++;
3490*f501eaabSShri Abhyankar     }
3491*f501eaabSShri Abhyankar     browlengths[i] += nmask;
3492*f501eaabSShri Abhyankar     /* zero out the mask elements we set */
3493*f501eaabSShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
3494*f501eaabSShri Abhyankar   }
3495*f501eaabSShri Abhyankar 
3496*f501eaabSShri Abhyankar   /* create our matrix */
3497*f501eaabSShri Abhyankar   ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(newmat,bs,0,browlengths);CHKERRQ(ierr);
3498*f501eaabSShri Abhyankar   a = (Mat_SeqBAIJ*)newmat->data;
3499*f501eaabSShri Abhyankar 
3500*f501eaabSShri Abhyankar   /* set matrix "i" values */
3501*f501eaabSShri Abhyankar   a->i[0] = 0;
3502*f501eaabSShri Abhyankar   for (i=1; i<= mbs; i++) {
3503*f501eaabSShri Abhyankar     a->i[i]      = a->i[i-1] + browlengths[i-1];
3504*f501eaabSShri Abhyankar     a->ilen[i-1] = browlengths[i-1];
3505*f501eaabSShri Abhyankar   }
3506*f501eaabSShri Abhyankar   a->nz         = 0;
3507*f501eaabSShri Abhyankar   for (i=0; i<mbs; i++) a->nz += browlengths[i];
3508*f501eaabSShri Abhyankar 
3509*f501eaabSShri Abhyankar   /* read in nonzero values */
3510*f501eaabSShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscScalar),&aa);CHKERRQ(ierr);
3511*f501eaabSShri Abhyankar   ierr = PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);CHKERRQ(ierr);
3512*f501eaabSShri Abhyankar   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;
3513*f501eaabSShri Abhyankar 
3514*f501eaabSShri Abhyankar   /* set "a" and "j" values into matrix */
3515*f501eaabSShri Abhyankar   nzcount = 0; jcount = 0;
3516*f501eaabSShri Abhyankar   for (i=0; i<mbs; i++) {
3517*f501eaabSShri Abhyankar     nzcountb = nzcount;
3518*f501eaabSShri Abhyankar     nmask    = 0;
3519*f501eaabSShri Abhyankar     for (j=0; j<bs; j++) {
3520*f501eaabSShri Abhyankar       kmax = rowlengths[i*bs+j];
3521*f501eaabSShri Abhyankar       for (k=0; k<kmax; k++) {
3522*f501eaabSShri Abhyankar         tmp = jj[nzcount++]/bs;
3523*f501eaabSShri Abhyankar 	if (!mask[tmp]) { masked[nmask++] = tmp; mask[tmp] = 1;}
3524*f501eaabSShri Abhyankar       }
3525*f501eaabSShri Abhyankar     }
3526*f501eaabSShri Abhyankar     /* sort the masked values */
3527*f501eaabSShri Abhyankar     ierr = PetscSortInt(nmask,masked);CHKERRQ(ierr);
3528*f501eaabSShri Abhyankar 
3529*f501eaabSShri Abhyankar     /* set "j" values into matrix */
3530*f501eaabSShri Abhyankar     maskcount = 1;
3531*f501eaabSShri Abhyankar     for (j=0; j<nmask; j++) {
3532*f501eaabSShri Abhyankar       a->j[jcount++]  = masked[j];
3533*f501eaabSShri Abhyankar       mask[masked[j]] = maskcount++;
3534*f501eaabSShri Abhyankar     }
3535*f501eaabSShri Abhyankar     /* set "a" values into matrix */
3536*f501eaabSShri Abhyankar     ishift = bs2*a->i[i];
3537*f501eaabSShri Abhyankar     for (j=0; j<bs; j++) {
3538*f501eaabSShri Abhyankar       kmax = rowlengths[i*bs+j];
3539*f501eaabSShri Abhyankar       for (k=0; k<kmax; k++) {
3540*f501eaabSShri Abhyankar         tmp       = jj[nzcountb]/bs ;
3541*f501eaabSShri Abhyankar         block     = mask[tmp] - 1;
3542*f501eaabSShri Abhyankar         point     = jj[nzcountb] - bs*tmp;
3543*f501eaabSShri Abhyankar         idx       = ishift + bs2*block + j + bs*point;
3544*f501eaabSShri Abhyankar         a->a[idx] = (MatScalar)aa[nzcountb++];
3545*f501eaabSShri Abhyankar       }
3546*f501eaabSShri Abhyankar     }
3547*f501eaabSShri Abhyankar     /* zero out the mask elements we set */
3548*f501eaabSShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
3549*f501eaabSShri Abhyankar   }
3550*f501eaabSShri Abhyankar   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");
3551*f501eaabSShri Abhyankar 
3552*f501eaabSShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3553*f501eaabSShri Abhyankar   ierr = PetscFree(browlengths);CHKERRQ(ierr);
3554*f501eaabSShri Abhyankar   ierr = PetscFree(aa);CHKERRQ(ierr);
3555*f501eaabSShri Abhyankar   ierr = PetscFree(jj);CHKERRQ(ierr);
3556*f501eaabSShri Abhyankar   ierr = PetscFree2(mask,masked);CHKERRQ(ierr);
3557*f501eaabSShri Abhyankar 
3558*f501eaabSShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3559*f501eaabSShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3560*f501eaabSShri Abhyankar   ierr = MatView_Private(newmat);CHKERRQ(ierr);
3561*f501eaabSShri Abhyankar   PetscFunctionReturn(0);
3562*f501eaabSShri Abhyankar }
3563*f501eaabSShri Abhyankar 
3564*f501eaabSShri Abhyankar #undef __FUNCT__
35654a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqBAIJ"
3566273d9f13SBarry Smith /*@C
3567273d9f13SBarry Smith    MatCreateSeqBAIJ - Creates a sparse matrix in block AIJ (block
3568273d9f13SBarry Smith    compressed row) format.  For good matrix assembly performance the
3569273d9f13SBarry Smith    user should preallocate the matrix storage by setting the parameter nz
3570273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3571273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
35722593348eSBarry Smith 
3573273d9f13SBarry Smith    Collective on MPI_Comm
3574273d9f13SBarry Smith 
3575273d9f13SBarry Smith    Input Parameters:
3576273d9f13SBarry Smith +  comm - MPI communicator, set to PETSC_COMM_SELF
3577273d9f13SBarry Smith .  bs - size of block
3578273d9f13SBarry Smith .  m - number of rows
3579273d9f13SBarry Smith .  n - number of columns
358035d8aa7fSBarry Smith .  nz - number of nonzero blocks  per block row (same for all rows)
358135d8aa7fSBarry Smith -  nnz - array containing the number of nonzero blocks in the various block rows
3582273d9f13SBarry Smith          (possibly different for each block row) or PETSC_NULL
3583273d9f13SBarry Smith 
3584273d9f13SBarry Smith    Output Parameter:
3585273d9f13SBarry Smith .  A - the matrix
3586273d9f13SBarry Smith 
3587175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3588ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3589175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3590175b88e8SBarry Smith 
3591273d9f13SBarry Smith    Options Database Keys:
3592273d9f13SBarry Smith .   -mat_no_unroll - uses code that does not unroll the loops in the
3593273d9f13SBarry Smith                      block calculations (much slower)
3594273d9f13SBarry Smith .    -mat_block_size - size of the blocks to use
3595273d9f13SBarry Smith 
3596273d9f13SBarry Smith    Level: intermediate
3597273d9f13SBarry Smith 
3598273d9f13SBarry Smith    Notes:
3599d1be2dadSMatthew Knepley    The number of rows and columns must be divisible by blocksize.
3600d1be2dadSMatthew Knepley 
360149a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
360249a6f317SBarry Smith 
360335d8aa7fSBarry Smith    A nonzero block is any block that as 1 or more nonzeros in it
360435d8aa7fSBarry Smith 
3605273d9f13SBarry Smith    The block AIJ format is fully compatible with standard Fortran 77
3606273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3607273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3608273d9f13SBarry Smith 
3609273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3610273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3611273d9f13SBarry Smith    allocation.  For additional details, see the users manual chapter on
3612273d9f13SBarry Smith    matrices.
3613273d9f13SBarry Smith 
3614273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIBAIJ()
3615273d9f13SBarry Smith @*/
3616be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
3617273d9f13SBarry Smith {
3618dfbe8321SBarry Smith   PetscErrorCode ierr;
3619273d9f13SBarry Smith 
3620273d9f13SBarry Smith   PetscFunctionBegin;
3621f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3622f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3623273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQBAIJ);CHKERRQ(ierr);
3624ab93d7beSBarry Smith   ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(*A,bs,nz,(PetscInt*)nnz);CHKERRQ(ierr);
3625273d9f13SBarry Smith   PetscFunctionReturn(0);
3626273d9f13SBarry Smith }
3627273d9f13SBarry Smith 
36284a2ae208SSatish Balay #undef __FUNCT__
36294a2ae208SSatish Balay #define __FUNCT__ "MatSeqBAIJSetPreallocation"
3630273d9f13SBarry Smith /*@C
3631273d9f13SBarry Smith    MatSeqBAIJSetPreallocation - Sets the block size and expected nonzeros
3632273d9f13SBarry Smith    per row in the matrix. For good matrix assembly performance the
3633273d9f13SBarry Smith    user should preallocate the matrix storage by setting the parameter nz
3634273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3635273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3636273d9f13SBarry Smith 
3637273d9f13SBarry Smith    Collective on MPI_Comm
3638273d9f13SBarry Smith 
3639273d9f13SBarry Smith    Input Parameters:
3640273d9f13SBarry Smith +  A - the matrix
3641273d9f13SBarry Smith .  bs - size of block
3642273d9f13SBarry Smith .  nz - number of block nonzeros per block row (same for all rows)
3643273d9f13SBarry Smith -  nnz - array containing the number of block nonzeros in the various block rows
3644273d9f13SBarry Smith          (possibly different for each block row) or PETSC_NULL
3645273d9f13SBarry Smith 
3646273d9f13SBarry Smith    Options Database Keys:
3647273d9f13SBarry Smith .   -mat_no_unroll - uses code that does not unroll the loops in the
3648273d9f13SBarry Smith                      block calculations (much slower)
3649273d9f13SBarry Smith .    -mat_block_size - size of the blocks to use
3650273d9f13SBarry Smith 
3651273d9f13SBarry Smith    Level: intermediate
3652273d9f13SBarry Smith 
3653273d9f13SBarry Smith    Notes:
365449a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
365549a6f317SBarry Smith 
3656aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3657aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3658aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3659aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3660aa95bbe8SBarry Smith 
3661273d9f13SBarry Smith    The block AIJ format is fully compatible with standard Fortran 77
3662273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3663273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3664273d9f13SBarry Smith 
3665273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3666273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3667273d9f13SBarry Smith    allocation.  For additional details, see the users manual chapter on
3668273d9f13SBarry Smith    matrices.
3669273d9f13SBarry Smith 
3670aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIBAIJ(), MatGetInfo()
3671273d9f13SBarry Smith @*/
3672be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
3673273d9f13SBarry Smith {
3674c1ac3661SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,PetscInt,const PetscInt[]);
3675273d9f13SBarry Smith 
3676273d9f13SBarry Smith   PetscFunctionBegin;
3677a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqBAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
3678a23d5eceSKris Buschelman   if (f) {
3679a23d5eceSKris Buschelman     ierr = (*f)(B,bs,nz,nnz);CHKERRQ(ierr);
3680273d9f13SBarry Smith   }
3681273d9f13SBarry Smith   PetscFunctionReturn(0);
3682273d9f13SBarry Smith }
3683a1d92eedSBarry Smith 
3684c75a6043SHong Zhang #undef __FUNCT__
3685725b52f3SLisandro Dalcin #define __FUNCT__ "MatSeqBAIJSetPreallocationCSR"
3686725b52f3SLisandro Dalcin /*@C
3687725b52f3SLisandro Dalcin    MatSeqBAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format
3688725b52f3SLisandro Dalcin    (the default sequential PETSc format).
3689725b52f3SLisandro Dalcin 
3690725b52f3SLisandro Dalcin    Collective on MPI_Comm
3691725b52f3SLisandro Dalcin 
3692725b52f3SLisandro Dalcin    Input Parameters:
3693725b52f3SLisandro Dalcin +  A - the matrix
3694725b52f3SLisandro Dalcin .  i - the indices into j for the start of each local row (starts with zero)
3695725b52f3SLisandro Dalcin .  j - the column indices for each local row (starts with zero) these must be sorted for each row
3696725b52f3SLisandro Dalcin -  v - optional values in the matrix
3697725b52f3SLisandro Dalcin 
3698725b52f3SLisandro Dalcin    Level: developer
3699725b52f3SLisandro Dalcin 
3700725b52f3SLisandro Dalcin .keywords: matrix, aij, compressed row, sparse
3701725b52f3SLisandro Dalcin 
3702725b52f3SLisandro Dalcin .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatSeqBAIJSetPreallocation(), MATSEQBAIJ
3703725b52f3SLisandro Dalcin @*/
3704725b52f3SLisandro Dalcin PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3705725b52f3SLisandro Dalcin {
3706725b52f3SLisandro Dalcin   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]);
3707725b52f3SLisandro Dalcin 
3708725b52f3SLisandro Dalcin   PetscFunctionBegin;
3709725b52f3SLisandro Dalcin   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqBAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
3710725b52f3SLisandro Dalcin   if (f) {
3711725b52f3SLisandro Dalcin     ierr = (*f)(B,bs,i,j,v);CHKERRQ(ierr);
3712725b52f3SLisandro Dalcin   }
3713725b52f3SLisandro Dalcin   PetscFunctionReturn(0);
3714725b52f3SLisandro Dalcin }
3715725b52f3SLisandro Dalcin 
3716725b52f3SLisandro Dalcin 
3717725b52f3SLisandro Dalcin #undef __FUNCT__
3718c75a6043SHong Zhang #define __FUNCT__ "MatCreateSeqBAIJWithArrays"
3719c75a6043SHong Zhang /*@
3720c75a6043SHong Zhang      MatCreateSeqBAIJWithArrays - Creates an sequential BAIJ matrix using matrix elements
3721c75a6043SHong Zhang               (upper triangular entries in CSR format) provided by the user.
3722c75a6043SHong Zhang 
3723c75a6043SHong Zhang      Collective on MPI_Comm
3724c75a6043SHong Zhang 
3725c75a6043SHong Zhang    Input Parameters:
3726c75a6043SHong Zhang +  comm - must be an MPI communicator of size 1
3727c75a6043SHong Zhang .  bs - size of block
3728c75a6043SHong Zhang .  m - number of rows
3729c75a6043SHong Zhang .  n - number of columns
3730c75a6043SHong Zhang .  i - row indices
3731c75a6043SHong Zhang .  j - column indices
3732c75a6043SHong Zhang -  a - matrix values
3733c75a6043SHong Zhang 
3734c75a6043SHong Zhang    Output Parameter:
3735c75a6043SHong Zhang .  mat - the matrix
3736c75a6043SHong Zhang 
3737c75a6043SHong Zhang    Level: intermediate
3738c75a6043SHong Zhang 
3739c75a6043SHong Zhang    Notes:
3740c75a6043SHong Zhang        The i, j, and a arrays are not copied by this routine, the user must free these arrays
3741c75a6043SHong Zhang     once the matrix is destroyed
3742c75a6043SHong Zhang 
3743c75a6043SHong Zhang        You cannot set new nonzero locations into this matrix, that will generate an error.
3744c75a6043SHong Zhang 
3745c75a6043SHong Zhang        The i and j indices are 0 based
3746c75a6043SHong Zhang 
3747c75a6043SHong Zhang .seealso: MatCreate(), MatCreateMPIBAIJ(), MatCreateSeqBAIJ()
3748c75a6043SHong Zhang 
3749c75a6043SHong Zhang @*/
3750c75a6043SHong Zhang PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
3751c75a6043SHong Zhang {
3752c75a6043SHong Zhang   PetscErrorCode ierr;
3753c75a6043SHong Zhang   PetscInt       ii;
3754c75a6043SHong Zhang   Mat_SeqBAIJ    *baij;
3755c75a6043SHong Zhang 
3756c75a6043SHong Zhang   PetscFunctionBegin;
3757e32f2f54SBarry Smith   if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"block size %D > 1 is not supported yet",bs);
3758e32f2f54SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
3759c75a6043SHong Zhang 
3760c75a6043SHong Zhang   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3761c75a6043SHong Zhang   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3762c75a6043SHong Zhang   ierr = MatSetType(*mat,MATSEQBAIJ);CHKERRQ(ierr);
3763c75a6043SHong Zhang   ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(*mat,bs,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3764c75a6043SHong Zhang   baij = (Mat_SeqBAIJ*)(*mat)->data;
3765c75a6043SHong Zhang   ierr = PetscMalloc2(m,PetscInt,&baij->imax,m,PetscInt,&baij->ilen);CHKERRQ(ierr);
37661784c0f5SBarry Smith   ierr = PetscLogObjectMemory(*mat,2*m*sizeof(PetscInt));CHKERRQ(ierr);
3767c75a6043SHong Zhang 
3768c75a6043SHong Zhang   baij->i = i;
3769c75a6043SHong Zhang   baij->j = j;
3770c75a6043SHong Zhang   baij->a = a;
3771c75a6043SHong Zhang   baij->singlemalloc = PETSC_FALSE;
3772c75a6043SHong Zhang   baij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3773e6b907acSBarry Smith   baij->free_a       = PETSC_FALSE;
3774e6b907acSBarry Smith   baij->free_ij       = PETSC_FALSE;
3775c75a6043SHong Zhang 
3776c75a6043SHong Zhang   for (ii=0; ii<m; ii++) {
3777c75a6043SHong Zhang     baij->ilen[ii] = baij->imax[ii] = i[ii+1] - i[ii];
3778c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
3779e32f2f54SBarry Smith     if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
3780c75a6043SHong Zhang #endif
3781c75a6043SHong Zhang   }
3782c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
3783c75a6043SHong Zhang   for (ii=0; ii<baij->i[m]; ii++) {
3784e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
3785e32f2f54SBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
3786c75a6043SHong Zhang   }
3787c75a6043SHong Zhang #endif
3788c75a6043SHong Zhang 
3789c75a6043SHong Zhang   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3790c75a6043SHong Zhang   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3791c75a6043SHong Zhang   PetscFunctionReturn(0);
3792c75a6043SHong Zhang }
3793