xref: /petsc/src/mat/impls/baij/seq/baij.c (revision 5a586d828132823a7f656d0d7f79985f923612fa)
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;
1169*5a586d82SBarry Smith   PetscInt       i,j,n = a->mbs,nz = a->i[n],bs = A->rmap->bs,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;
11848f7157efSSatish Balay     /* malloc & create the natural set of indices */
1185ecc77c7aSBarry Smith     ierr = PetscMalloc((n+1)*bs*sizeof(PetscInt),ia);CHKERRQ(ierr);
11869985e31cSBarry Smith     if (n) {
1187ecc77c7aSBarry Smith       (*ia)[0] = 0;
1188ecc77c7aSBarry Smith       for (j=1; j<bs; j++) {
1189ecc77c7aSBarry Smith         (*ia)[j] = (tia[1]-tia[0])*bs+(*ia)[j-1];
1190ecc77c7aSBarry Smith       }
11919985e31cSBarry Smith     }
1192ecc77c7aSBarry Smith 
1193ecc77c7aSBarry Smith     for (i=1; i<n; i++) {
1194ecc77c7aSBarry Smith       (*ia)[i*bs] = (tia[i]-tia[i-1])*bs + (*ia)[i*bs-1];
1195ecc77c7aSBarry Smith       for (j=1; j<bs; j++) {
1196ecc77c7aSBarry Smith         (*ia)[i*bs+j] = (tia[i+1]-tia[i])*bs + (*ia)[i*bs+j-1];
11978f7157efSSatish Balay       }
11988f7157efSSatish Balay     }
11999985e31cSBarry Smith     if (n) {
1200ecc77c7aSBarry Smith       (*ia)[n*bs] = (tia[n]-tia[n-1])*bs + (*ia)[n*bs-1];
12019985e31cSBarry Smith     }
1202ecc77c7aSBarry Smith 
12039985e31cSBarry Smith     if (ja) {
12049985e31cSBarry Smith       ierr = PetscMalloc(nz*bs*bs*sizeof(PetscInt),ja);CHKERRQ(ierr);
12059985e31cSBarry Smith       cnt = 0;
12069985e31cSBarry Smith       for (i=0; i<n; i++) {
12079985e31cSBarry Smith         for (j=0; j<bs; j++) {
12089985e31cSBarry Smith           for (k=tia[i]; k<tia[i+1]; k++) {
12099985e31cSBarry Smith             for (l=0; l<bs; l++) {
12109985e31cSBarry Smith               (*ja)[cnt++] = bs*tja[k] + l;
12119985e31cSBarry Smith 	    }
12129985e31cSBarry Smith           }
12139985e31cSBarry Smith         }
12149985e31cSBarry Smith       }
12159985e31cSBarry Smith     }
12169985e31cSBarry Smith 
1217ecc77c7aSBarry Smith     n     *= bs;
1218ecc77c7aSBarry Smith     nz *= bs*bs;
12198f7157efSSatish Balay     if (symmetric) { /* deallocate memory allocated in MatToSymmetricIJ_SeqAIJ() */
12208f7157efSSatish Balay       ierr = PetscFree(tia);CHKERRQ(ierr);
12218f7157efSSatish Balay       ierr = PetscFree(tja);CHKERRQ(ierr);
12228f7157efSSatish Balay     }
1223f6d58c54SBarry Smith   } else if (oshift == 1) {
1224715a17b5SBarry Smith     if (symmetric) {
1225715a17b5SBarry Smith       PetscInt nz = tia[A->rmap->n/bs];
1226715a17b5SBarry Smith       /*  add 1 to i and j indices */
1227715a17b5SBarry Smith       for (i=0; i<A->rmap->n/bs+1; i++) tia[i] = tia[i] + 1;
1228715a17b5SBarry Smith       *ia = tia;
1229715a17b5SBarry Smith       if (ja) {
1230715a17b5SBarry Smith 	for (i=0; i<nz; i++) tja[i] = tja[i] + 1;
1231715a17b5SBarry Smith         *ja = tja;
1232715a17b5SBarry Smith       }
1233715a17b5SBarry Smith     } else {
1234f6d58c54SBarry Smith       PetscInt nz = a->i[A->rmap->n/bs];
1235f6d58c54SBarry Smith       /* malloc space and  add 1 to i and j indices */
1236f6d58c54SBarry Smith       ierr = PetscMalloc((A->rmap->n/bs+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
1237f6d58c54SBarry Smith       for (i=0; i<A->rmap->n/bs+1; i++) (*ia)[i] = a->i[i] + 1;
1238f6d58c54SBarry Smith       if (ja) {
1239f6d58c54SBarry Smith 	ierr = PetscMalloc(nz*sizeof(PetscInt),ja);CHKERRQ(ierr);
1240f6d58c54SBarry Smith 	for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
1241f6d58c54SBarry Smith       }
1242715a17b5SBarry Smith     }
12438f7157efSSatish Balay   } else {
12448f7157efSSatish Balay     *ia = tia;
1245ecc77c7aSBarry Smith     if (ja) *ja = tja;
12468f7157efSSatish Balay   }
1247f6d58c54SBarry Smith 
12483a40ed3dSBarry Smith   PetscFunctionReturn(0);
12493b2fbd54SBarry Smith }
12503b2fbd54SBarry Smith 
12514a2ae208SSatish Balay #undef __FUNCT__
12524a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqBAIJ"
12538f7157efSSatish Balay static PetscErrorCode MatRestoreRowIJ_SeqBAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
12543b2fbd54SBarry Smith {
12556849ba73SBarry Smith   PetscErrorCode ierr;
12563b2fbd54SBarry Smith 
12573a40ed3dSBarry Smith   PetscFunctionBegin;
12583a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1259715a17b5SBarry Smith   if ((!blockcompressed && A->rmap->bs > 1) || (symmetric || oshift == 1)) {
1260606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
12619985e31cSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
12623b2fbd54SBarry Smith   }
12633a40ed3dSBarry Smith   PetscFunctionReturn(0);
12643b2fbd54SBarry Smith }
12653b2fbd54SBarry Smith 
12664a2ae208SSatish Balay #undef __FUNCT__
12674a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqBAIJ"
1268dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqBAIJ(Mat A)
12692d61bbb3SSatish Balay {
12702d61bbb3SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
1271dfbe8321SBarry Smith   PetscErrorCode ierr;
12722d61bbb3SSatish Balay 
1273433994e6SBarry Smith   PetscFunctionBegin;
1274aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1275d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->N,A->cmap->n,a->nz);
12762d61bbb3SSatish Balay #endif
1277e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
1278c38d4ed2SBarry Smith   if (a->row) {
1279c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
1280c38d4ed2SBarry Smith   }
1281c38d4ed2SBarry Smith   if (a->col) {
1282c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
1283c38d4ed2SBarry Smith   }
12844fd072dbSBarry Smith   if (a->free_diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
128505b42c5fSBarry Smith   ierr = PetscFree(a->idiag);CHKERRQ(ierr);
12864fd072dbSBarry Smith   if (a->free_imax_ilen) {ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);}
128705b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
128805b42c5fSBarry Smith   ierr = PetscFree(a->mult_work);CHKERRQ(ierr);
1289e51c0b9cSSatish Balay   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
129005b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
129105b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
12920e83c824SBarry Smith   if (a->compressedrow.use){ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);}
1293c4319e64SHong Zhang 
129470e08fbdSBarry Smith   if (a->sbaijMat) {ierr = MatDestroy(a->sbaijMat);CHKERRQ(ierr);}
12954fd072dbSBarry Smith   if (a->parent) {ierr = MatDestroy(a->parent);CHKERRQ(ierr);}
1296606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
1297901853e0SKris Buschelman 
1298dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
129943516a2dSKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJInvertBlockDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
1300901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
1301901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
1302901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
1303901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqbaij_seqaij_C","",PETSC_NULL);CHKERRQ(ierr);
1304901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqbaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
1305901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
1306725b52f3SLisandro Dalcin   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqBAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
13072d61bbb3SSatish Balay   PetscFunctionReturn(0);
13082d61bbb3SSatish Balay }
13092d61bbb3SSatish Balay 
13104a2ae208SSatish Balay #undef __FUNCT__
13114a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqBAIJ"
13124e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqBAIJ(Mat A,MatOption op,PetscTruth flg)
13132d61bbb3SSatish Balay {
13142d61bbb3SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
131563ba0a88SBarry Smith   PetscErrorCode ierr;
13162d61bbb3SSatish Balay 
13172d61bbb3SSatish Balay   PetscFunctionBegin;
1318aa275fccSKris Buschelman   switch (op) {
1319aa275fccSKris Buschelman   case MAT_ROW_ORIENTED:
13204e0d8c25SBarry Smith     a->roworiented    = flg;
1321aa275fccSKris Buschelman     break;
1322a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1323a9817697SBarry Smith     a->keepnonzeropattern = flg;
1324aa275fccSKris Buschelman     break;
1325512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1326512a5fc5SBarry Smith     a->nonew          = (flg ? 0 : 1);
1327aa275fccSKris Buschelman     break;
1328aa275fccSKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
13294e0d8c25SBarry Smith     a->nonew          = (flg ? -1 : 0);
1330aa275fccSKris Buschelman     break;
1331aa275fccSKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
13324e0d8c25SBarry Smith     a->nonew          = (flg ? -2 : 0);
1333aa275fccSKris Buschelman     break;
133428b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
133528b2fa4aSMatthew Knepley     a->nounused       = (flg ? -1 : 0);
133628b2fa4aSMatthew Knepley     break;
13374e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1338aa275fccSKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1339aa275fccSKris Buschelman   case MAT_USE_HASH_TABLE:
1340290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1341aa275fccSKris Buschelman     break;
134277e54ba9SKris Buschelman   case MAT_SYMMETRIC:
134377e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
13449a4540c5SBarry Smith   case MAT_HERMITIAN:
13459a4540c5SBarry Smith   case MAT_SYMMETRY_ETERNAL:
1346290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
134777e54ba9SKris Buschelman     break;
1348aa275fccSKris Buschelman   default:
1349e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
13502d61bbb3SSatish Balay   }
13512d61bbb3SSatish Balay   PetscFunctionReturn(0);
13522d61bbb3SSatish Balay }
13532d61bbb3SSatish Balay 
13544a2ae208SSatish Balay #undef __FUNCT__
13554a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqBAIJ"
1356c1ac3661SBarry Smith PetscErrorCode MatGetRow_SeqBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
13572d61bbb3SSatish Balay {
13582d61bbb3SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
13596849ba73SBarry Smith   PetscErrorCode ierr;
1360c1ac3661SBarry Smith   PetscInt       itmp,i,j,k,M,*ai,*aj,bs,bn,bp,*idx_i,bs2;
13613f1db9ecSBarry Smith   MatScalar      *aa,*aa_i;
136287828ca2SBarry Smith   PetscScalar    *v_i;
13632d61bbb3SSatish Balay 
13642d61bbb3SSatish Balay   PetscFunctionBegin;
1365d0f46423SBarry Smith   bs  = A->rmap->bs;
13662d61bbb3SSatish Balay   ai  = a->i;
13672d61bbb3SSatish Balay   aj  = a->j;
13682d61bbb3SSatish Balay   aa  = a->a;
13692d61bbb3SSatish Balay   bs2 = a->bs2;
13702d61bbb3SSatish Balay 
1371e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range", row);
13722d61bbb3SSatish Balay 
13732d61bbb3SSatish Balay   bn  = row/bs;   /* Block number */
13742d61bbb3SSatish Balay   bp  = row % bs; /* Block Position */
13752d61bbb3SSatish Balay   M   = ai[bn+1] - ai[bn];
13762d61bbb3SSatish Balay   *nz = bs*M;
13772d61bbb3SSatish Balay 
13782d61bbb3SSatish Balay   if (v) {
13792d61bbb3SSatish Balay     *v = 0;
13802d61bbb3SSatish Balay     if (*nz) {
138187828ca2SBarry Smith       ierr = PetscMalloc((*nz)*sizeof(PetscScalar),v);CHKERRQ(ierr);
13822d61bbb3SSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
13832d61bbb3SSatish Balay         v_i  = *v + i*bs;
13842d61bbb3SSatish Balay         aa_i = aa + bs2*(ai[bn] + i);
13852d61bbb3SSatish Balay         for (j=bp,k=0; j<bs2; j+=bs,k++) {v_i[k] = aa_i[j];}
13862d61bbb3SSatish Balay       }
13872d61bbb3SSatish Balay     }
13882d61bbb3SSatish Balay   }
13892d61bbb3SSatish Balay 
13902d61bbb3SSatish Balay   if (idx) {
13912d61bbb3SSatish Balay     *idx = 0;
13922d61bbb3SSatish Balay     if (*nz) {
1393c1ac3661SBarry Smith       ierr = PetscMalloc((*nz)*sizeof(PetscInt),idx);CHKERRQ(ierr);
13942d61bbb3SSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
13952d61bbb3SSatish Balay         idx_i = *idx + i*bs;
13962d61bbb3SSatish Balay         itmp  = bs*aj[ai[bn] + i];
13972d61bbb3SSatish Balay         for (j=0; j<bs; j++) {idx_i[j] = itmp++;}
13982d61bbb3SSatish Balay       }
13992d61bbb3SSatish Balay     }
14002d61bbb3SSatish Balay   }
14012d61bbb3SSatish Balay   PetscFunctionReturn(0);
14022d61bbb3SSatish Balay }
14032d61bbb3SSatish Balay 
14044a2ae208SSatish Balay #undef __FUNCT__
14054a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqBAIJ"
1406c1ac3661SBarry Smith PetscErrorCode MatRestoreRow_SeqBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
14072d61bbb3SSatish Balay {
1408dfbe8321SBarry Smith   PetscErrorCode ierr;
1409606d414cSSatish Balay 
14102d61bbb3SSatish Balay   PetscFunctionBegin;
141105b42c5fSBarry Smith   if (idx) {ierr = PetscFree(*idx);CHKERRQ(ierr);}
141205b42c5fSBarry Smith   if (v)   {ierr = PetscFree(*v);CHKERRQ(ierr);}
14132d61bbb3SSatish Balay   PetscFunctionReturn(0);
14142d61bbb3SSatish Balay }
14152d61bbb3SSatish Balay 
1416fca92195SBarry Smith extern PetscErrorCode MatSetValues_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
1417fca92195SBarry Smith 
14184a2ae208SSatish Balay #undef __FUNCT__
14194a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqBAIJ"
1420fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqBAIJ(Mat A,MatReuse reuse,Mat *B)
14212d61bbb3SSatish Balay {
14222d61bbb3SSatish Balay   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data;
14232d61bbb3SSatish Balay   Mat            C;
14246849ba73SBarry Smith   PetscErrorCode ierr;
1425d0f46423SBarry Smith   PetscInt       i,j,k,*aj=a->j,*ai=a->i,bs=A->rmap->bs,mbs=a->mbs,nbs=a->nbs,len,*col;
1426c1ac3661SBarry Smith   PetscInt       *rows,*cols,bs2=a->bs2;
1427dd6ea824SBarry Smith   MatScalar      *array;
14282d61bbb3SSatish Balay 
14292d61bbb3SSatish Balay   PetscFunctionBegin;
1430e32f2f54SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && mbs != nbs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1431fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || A == *B) {
1432c1ac3661SBarry Smith     ierr = PetscMalloc((1+nbs)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1433c1ac3661SBarry Smith     ierr = PetscMemzero(col,(1+nbs)*sizeof(PetscInt));CHKERRQ(ierr);
14342d61bbb3SSatish Balay 
14352d61bbb3SSatish Balay     for (i=0; i<ai[mbs]; i++) col[aj[i]] += 1;
14367adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1437d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,A->rmap->N,A->cmap->n,A->rmap->N);CHKERRQ(ierr);
14387adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1439ab93d7beSBarry Smith     ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(C,bs,PETSC_NULL,col);CHKERRQ(ierr);
1440606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1441fc4dec0aSBarry Smith   } else {
1442fc4dec0aSBarry Smith     C = *B;
1443fc4dec0aSBarry Smith   }
1444fc4dec0aSBarry Smith 
1445fc4dec0aSBarry Smith   array = a->a;
1446fca92195SBarry Smith   ierr = PetscMalloc2(bs,PetscInt,&rows,bs,PetscInt,&cols);CHKERRQ(ierr);
14472d61bbb3SSatish Balay   for (i=0; i<mbs; i++) {
14482d61bbb3SSatish Balay     cols[0] = i*bs;
14492d61bbb3SSatish Balay     for (k=1; k<bs; k++) cols[k] = cols[k-1] + 1;
14502d61bbb3SSatish Balay     len = ai[i+1] - ai[i];
14512d61bbb3SSatish Balay     for (j=0; j<len; j++) {
14522d61bbb3SSatish Balay       rows[0] = (*aj++)*bs;
14532d61bbb3SSatish Balay       for (k=1; k<bs; k++) rows[k] = rows[k-1] + 1;
1454fca92195SBarry Smith       ierr = MatSetValues_SeqBAIJ(C,bs,rows,bs,cols,array,INSERT_VALUES);CHKERRQ(ierr);
14552d61bbb3SSatish Balay       array += bs2;
14562d61bbb3SSatish Balay     }
14572d61bbb3SSatish Balay   }
1458fca92195SBarry Smith   ierr = PetscFree2(rows,cols);CHKERRQ(ierr);
14592d61bbb3SSatish Balay 
14602d61bbb3SSatish Balay   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14612d61bbb3SSatish Balay   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14622d61bbb3SSatish Balay 
1463815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
14642d61bbb3SSatish Balay     *B = C;
14652d61bbb3SSatish Balay   } else {
1466273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
14672d61bbb3SSatish Balay   }
14682d61bbb3SSatish Balay   PetscFunctionReturn(0);
14692d61bbb3SSatish Balay }
14702d61bbb3SSatish Balay 
14714a2ae208SSatish Balay #undef __FUNCT__
14724a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqBAIJ_Binary"
14736849ba73SBarry Smith static PetscErrorCode MatView_SeqBAIJ_Binary(Mat A,PetscViewer viewer)
14742593348eSBarry Smith {
1475b6490206SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
14766849ba73SBarry Smith   PetscErrorCode ierr;
1477d0f46423SBarry Smith   PetscInt       i,*col_lens,bs = A->rmap->bs,count,*jj,j,k,l,bs2=a->bs2;
1478b24ad042SBarry Smith   int            fd;
147987828ca2SBarry Smith   PetscScalar    *aa;
1480ce6f0cecSBarry Smith   FILE           *file;
14812593348eSBarry Smith 
14823a40ed3dSBarry Smith   PetscFunctionBegin;
1483b0a32e0cSBarry Smith   ierr        = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
1484d0f46423SBarry Smith   ierr        = PetscMalloc((4+A->rmap->N)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
14850700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
14863b2fbd54SBarry Smith 
1487d0f46423SBarry Smith   col_lens[1] = A->rmap->N;
1488d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
14897e67e3f9SSatish Balay   col_lens[3] = a->nz*bs2;
14902593348eSBarry Smith 
14912593348eSBarry Smith   /* store lengths of each row and write (including header) to file */
1492b6490206SBarry Smith   count = 0;
1493b6490206SBarry Smith   for (i=0; i<a->mbs; i++) {
1494b6490206SBarry Smith     for (j=0; j<bs; j++) {
1495b6490206SBarry Smith       col_lens[4+count++] = bs*(a->i[i+1] - a->i[i]);
1496b6490206SBarry Smith     }
14972593348eSBarry Smith   }
1498d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->N,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1499606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
15002593348eSBarry Smith 
15012593348eSBarry Smith   /* store column indices (zero start index) */
1502c1ac3661SBarry Smith   ierr  = PetscMalloc((a->nz+1)*bs2*sizeof(PetscInt),&jj);CHKERRQ(ierr);
1503b6490206SBarry Smith   count = 0;
1504b6490206SBarry Smith   for (i=0; i<a->mbs; i++) {
1505b6490206SBarry Smith     for (j=0; j<bs; j++) {
1506b6490206SBarry Smith       for (k=a->i[i]; k<a->i[i+1]; k++) {
1507b6490206SBarry Smith         for (l=0; l<bs; l++) {
1508b6490206SBarry Smith           jj[count++] = bs*a->j[k] + l;
15092593348eSBarry Smith         }
15102593348eSBarry Smith       }
1511b6490206SBarry Smith     }
1512b6490206SBarry Smith   }
15136f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,jj,bs2*a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
1514606d414cSSatish Balay   ierr = PetscFree(jj);CHKERRQ(ierr);
15152593348eSBarry Smith 
15162593348eSBarry Smith   /* store nonzero values */
151787828ca2SBarry Smith   ierr  = PetscMalloc((a->nz+1)*bs2*sizeof(PetscScalar),&aa);CHKERRQ(ierr);
1518b6490206SBarry Smith   count = 0;
1519b6490206SBarry Smith   for (i=0; i<a->mbs; i++) {
1520b6490206SBarry Smith     for (j=0; j<bs; j++) {
1521b6490206SBarry Smith       for (k=a->i[i]; k<a->i[i+1]; k++) {
1522b6490206SBarry Smith         for (l=0; l<bs; l++) {
15237e67e3f9SSatish Balay           aa[count++] = a->a[bs2*k + l*bs + j];
1524b6490206SBarry Smith         }
1525b6490206SBarry Smith       }
1526b6490206SBarry Smith     }
1527b6490206SBarry Smith   }
15286f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,aa,bs2*a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
1529606d414cSSatish Balay   ierr = PetscFree(aa);CHKERRQ(ierr);
1530ce6f0cecSBarry Smith 
1531b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
1532ce6f0cecSBarry Smith   if (file) {
1533d0f46423SBarry Smith     fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
1534ce6f0cecSBarry Smith   }
15353a40ed3dSBarry Smith   PetscFunctionReturn(0);
15362593348eSBarry Smith }
15372593348eSBarry Smith 
15384a2ae208SSatish Balay #undef __FUNCT__
15394a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqBAIJ_ASCII"
15406849ba73SBarry Smith static PetscErrorCode MatView_SeqBAIJ_ASCII(Mat A,PetscViewer viewer)
15412593348eSBarry Smith {
1542b6490206SBarry Smith   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
1543dfbe8321SBarry Smith   PetscErrorCode    ierr;
1544d0f46423SBarry Smith   PetscInt          i,j,bs = A->rmap->bs,k,l,bs2=a->bs2;
1545f3ef73ceSBarry Smith   PetscViewerFormat format;
15462593348eSBarry Smith 
15473a40ed3dSBarry Smith   PetscFunctionBegin;
1548b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
1549456192e2SBarry Smith   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
155077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);CHKERRQ(ierr);
1551fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
1552bcd9e38bSBarry Smith     Mat aij;
1553ceb03754SKris Buschelman     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&aij);CHKERRQ(ierr);
1554bcd9e38bSBarry Smith     ierr = MatView(aij,viewer);CHKERRQ(ierr);
1555bcd9e38bSBarry Smith     ierr = MatDestroy(aij);CHKERRQ(ierr);
155604929863SHong Zhang   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
155704929863SHong Zhang      PetscFunctionReturn(0);
1558fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
1559b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
156044cd7ae7SLois Curfman McInnes     for (i=0; i<a->mbs; i++) {
156144cd7ae7SLois Curfman McInnes       for (j=0; j<bs; j++) {
156277431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
156344cd7ae7SLois Curfman McInnes         for (k=a->i[i]; k<a->i[i+1]; k++) {
156444cd7ae7SLois Curfman McInnes           for (l=0; l<bs; l++) {
1565aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
15660e6d2581SBarry Smith             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
1567a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %Gi) ",bs*a->j[k]+l,
15680e6d2581SBarry Smith                       PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
15690e6d2581SBarry Smith             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
1570a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %Gi) ",bs*a->j[k]+l,
15710e6d2581SBarry Smith                       PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
15720e6d2581SBarry Smith             } else if (PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
1573a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
15740ef38995SBarry Smith             }
157544cd7ae7SLois Curfman McInnes #else
15760ef38995SBarry Smith             if (a->a[bs2*k + l*bs + j] != 0.0) {
1577a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
15780ef38995SBarry Smith             }
157944cd7ae7SLois Curfman McInnes #endif
158044cd7ae7SLois Curfman McInnes           }
158144cd7ae7SLois Curfman McInnes         }
1582b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
158344cd7ae7SLois Curfman McInnes       }
158444cd7ae7SLois Curfman McInnes     }
1585b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
15860ef38995SBarry Smith   } else {
1587b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
1588b6490206SBarry Smith     for (i=0; i<a->mbs; i++) {
1589b6490206SBarry Smith       for (j=0; j<bs; j++) {
159077431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
1591b6490206SBarry Smith         for (k=a->i[i]; k<a->i[i+1]; k++) {
1592b6490206SBarry Smith           for (l=0; l<bs; l++) {
1593aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
15940e6d2581SBarry Smith             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0) {
1595a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k]+l,
15960e6d2581SBarry Smith                 PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
15970e6d2581SBarry Smith             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0) {
1598a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k]+l,
15990e6d2581SBarry Smith                 PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
16000ef38995SBarry Smith             } else {
1601a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
160288685aaeSLois Curfman McInnes             }
160388685aaeSLois Curfman McInnes #else
1604a83599f4SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
160588685aaeSLois Curfman McInnes #endif
16062593348eSBarry Smith           }
16072593348eSBarry Smith         }
1608b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
16092593348eSBarry Smith       }
16102593348eSBarry Smith     }
1611b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
1612b6490206SBarry Smith   }
1613b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
16143a40ed3dSBarry Smith   PetscFunctionReturn(0);
16152593348eSBarry Smith }
16162593348eSBarry Smith 
16174a2ae208SSatish Balay #undef __FUNCT__
16184a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqBAIJ_Draw_Zoom"
16196849ba73SBarry Smith static PetscErrorCode MatView_SeqBAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
16203270192aSSatish Balay {
162177ed5343SBarry Smith   Mat            A = (Mat) Aa;
16223270192aSSatish Balay   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ*)A->data;
16236849ba73SBarry Smith   PetscErrorCode ierr;
1624d0f46423SBarry Smith   PetscInt       row,i,j,k,l,mbs=a->mbs,color,bs=A->rmap->bs,bs2=a->bs2;
16250e6d2581SBarry Smith   PetscReal      xl,yl,xr,yr,x_l,x_r,y_l,y_r;
16263f1db9ecSBarry Smith   MatScalar      *aa;
1627b0a32e0cSBarry Smith   PetscViewer    viewer;
16283270192aSSatish Balay 
16293a40ed3dSBarry Smith   PetscFunctionBegin;
16303270192aSSatish Balay 
1631b65db4caSBarry Smith   /* still need to add support for contour plot of nonzeros; see MatView_SeqAIJ_Draw_Zoom()*/
163277ed5343SBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
163377ed5343SBarry Smith 
1634b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
163577ed5343SBarry Smith 
16363270192aSSatish Balay   /* loop over matrix elements drawing boxes */
1637b0a32e0cSBarry Smith   color = PETSC_DRAW_BLUE;
16383270192aSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
16393270192aSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
1640d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
16413270192aSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
16423270192aSSatish Balay       aa = a->a + j*bs2;
16433270192aSSatish Balay       for (k=0; k<bs; k++) {
16443270192aSSatish Balay         for (l=0; l<bs; l++) {
16450e6d2581SBarry Smith           if (PetscRealPart(*aa++) >=  0.) continue;
1646b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
16473270192aSSatish Balay         }
16483270192aSSatish Balay       }
16493270192aSSatish Balay     }
16503270192aSSatish Balay   }
1651b0a32e0cSBarry Smith   color = PETSC_DRAW_CYAN;
16523270192aSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
16533270192aSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
1654d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
16553270192aSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
16563270192aSSatish Balay       aa = a->a + j*bs2;
16573270192aSSatish Balay       for (k=0; k<bs; k++) {
16583270192aSSatish Balay         for (l=0; l<bs; l++) {
16590e6d2581SBarry Smith           if (PetscRealPart(*aa++) != 0.) continue;
1660b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
16613270192aSSatish Balay         }
16623270192aSSatish Balay       }
16633270192aSSatish Balay     }
16643270192aSSatish Balay   }
16653270192aSSatish Balay 
1666b0a32e0cSBarry Smith   color = PETSC_DRAW_RED;
16673270192aSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
16683270192aSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
1669d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
16703270192aSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
16713270192aSSatish Balay       aa = a->a + j*bs2;
16723270192aSSatish Balay       for (k=0; k<bs; k++) {
16733270192aSSatish Balay         for (l=0; l<bs; l++) {
16740e6d2581SBarry Smith           if (PetscRealPart(*aa++) <= 0.) continue;
1675b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
16763270192aSSatish Balay         }
16773270192aSSatish Balay       }
16783270192aSSatish Balay     }
16793270192aSSatish Balay   }
168077ed5343SBarry Smith   PetscFunctionReturn(0);
168177ed5343SBarry Smith }
16823270192aSSatish Balay 
16834a2ae208SSatish Balay #undef __FUNCT__
16844a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqBAIJ_Draw"
16856849ba73SBarry Smith static PetscErrorCode MatView_SeqBAIJ_Draw(Mat A,PetscViewer viewer)
168677ed5343SBarry Smith {
1687dfbe8321SBarry Smith   PetscErrorCode ierr;
16880e6d2581SBarry Smith   PetscReal      xl,yl,xr,yr,w,h;
1689b0a32e0cSBarry Smith   PetscDraw      draw;
169077ed5343SBarry Smith   PetscTruth     isnull;
16913270192aSSatish Balay 
169277ed5343SBarry Smith   PetscFunctionBegin;
169377ed5343SBarry Smith 
1694b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
1695b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
169677ed5343SBarry Smith 
169777ed5343SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
1698d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->N; h = yr/10.0; w = xr/10.0;
169977ed5343SBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
1700b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
1701b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqBAIJ_Draw_Zoom,A);CHKERRQ(ierr);
170277ed5343SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
17033a40ed3dSBarry Smith   PetscFunctionReturn(0);
17043270192aSSatish Balay }
17053270192aSSatish Balay 
17064a2ae208SSatish Balay #undef __FUNCT__
17074a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqBAIJ"
1708dfbe8321SBarry Smith PetscErrorCode MatView_SeqBAIJ(Mat A,PetscViewer viewer)
17092593348eSBarry Smith {
1710dfbe8321SBarry Smith   PetscErrorCode ierr;
171132077d6dSBarry Smith   PetscTruth     iascii,isbinary,isdraw;
17122593348eSBarry Smith 
17133a40ed3dSBarry Smith   PetscFunctionBegin;
17142692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
17152692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
17162692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
171732077d6dSBarry Smith   if (iascii){
17183a40ed3dSBarry Smith     ierr = MatView_SeqBAIJ_ASCII(A,viewer);CHKERRQ(ierr);
17190f5bd95cSBarry Smith   } else if (isbinary) {
17203a40ed3dSBarry Smith     ierr = MatView_SeqBAIJ_Binary(A,viewer);CHKERRQ(ierr);
17210f5bd95cSBarry Smith   } else if (isdraw) {
17223a40ed3dSBarry Smith     ierr = MatView_SeqBAIJ_Draw(A,viewer);CHKERRQ(ierr);
17235cd90555SBarry Smith   } else {
1724a5e6ed63SBarry Smith     Mat B;
1725ceb03754SKris Buschelman     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);CHKERRQ(ierr);
1726a5e6ed63SBarry Smith     ierr = MatView(B,viewer);CHKERRQ(ierr);
1727a5e6ed63SBarry Smith     ierr = MatDestroy(B);CHKERRQ(ierr);
17282593348eSBarry Smith   }
17293a40ed3dSBarry Smith   PetscFunctionReturn(0);
17302593348eSBarry Smith }
1731b6490206SBarry Smith 
1732cd0e1443SSatish Balay 
17334a2ae208SSatish Balay #undef __FUNCT__
17344a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqBAIJ"
1735c1ac3661SBarry Smith PetscErrorCode MatGetValues_SeqBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
1736cd0e1443SSatish Balay {
1737cd0e1443SSatish Balay   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
1738c1ac3661SBarry Smith   PetscInt    *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
1739c1ac3661SBarry Smith   PetscInt    *ai = a->i,*ailen = a->ilen;
1740d0f46423SBarry Smith   PetscInt    brow,bcol,ridx,cidx,bs=A->rmap->bs,bs2=a->bs2;
174197e567efSBarry Smith   MatScalar   *ap,*aa = a->a;
1742cd0e1443SSatish Balay 
17433a40ed3dSBarry Smith   PetscFunctionBegin;
17442d61bbb3SSatish Balay   for (k=0; k<m; k++) { /* loop over rows */
1745cd0e1443SSatish Balay     row  = im[k]; brow = row/bs;
1746e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row"); */
1747e32f2f54SBarry Smith     if (row >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D too large", row);
17482d61bbb3SSatish Balay     rp   = aj + ai[brow] ; ap = aa + bs2*ai[brow] ;
17492c3acbe9SBarry Smith     nrow = ailen[brow];
17502d61bbb3SSatish Balay     for (l=0; l<n; l++) { /* loop over columns */
1751e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column"); */
1752e32f2f54SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column %D too large", in[l]);
17532d61bbb3SSatish Balay       col  = in[l] ;
17542d61bbb3SSatish Balay       bcol = col/bs;
17552d61bbb3SSatish Balay       cidx = col%bs;
17562d61bbb3SSatish Balay       ridx = row%bs;
17572d61bbb3SSatish Balay       high = nrow;
17582d61bbb3SSatish Balay       low  = 0; /* assume unsorted */
17592d61bbb3SSatish Balay       while (high-low > 5) {
1760cd0e1443SSatish Balay         t = (low+high)/2;
1761cd0e1443SSatish Balay         if (rp[t] > bcol) high = t;
1762cd0e1443SSatish Balay         else             low  = t;
1763cd0e1443SSatish Balay       }
1764cd0e1443SSatish Balay       for (i=low; i<high; i++) {
1765cd0e1443SSatish Balay         if (rp[i] > bcol) break;
1766cd0e1443SSatish Balay         if (rp[i] == bcol) {
17672d61bbb3SSatish Balay           *v++ = ap[bs2*i+bs*cidx+ridx];
17682d61bbb3SSatish Balay           goto finished;
1769cd0e1443SSatish Balay         }
1770cd0e1443SSatish Balay       }
177197e567efSBarry Smith       *v++ = 0.0;
17722d61bbb3SSatish Balay       finished:;
1773cd0e1443SSatish Balay     }
1774cd0e1443SSatish Balay   }
17753a40ed3dSBarry Smith   PetscFunctionReturn(0);
1776cd0e1443SSatish Balay }
1777cd0e1443SSatish Balay 
17784a2ae208SSatish Balay #undef __FUNCT__
17794a2ae208SSatish Balay #define __FUNCT__ "MatSetValuesBlocked_SeqBAIJ"
1780dd6ea824SBarry Smith PetscErrorCode MatSetValuesBlocked_SeqBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
178192c4ed94SBarry Smith {
178292c4ed94SBarry Smith   Mat_SeqBAIJ       *a = (Mat_SeqBAIJ*)A->data;
1783e2ee6c50SBarry Smith   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,rmax,N,lastcol = -1;
1784c1ac3661SBarry Smith   PetscInt          *imax=a->imax,*ai=a->i,*ailen=a->ilen;
17856849ba73SBarry Smith   PetscErrorCode    ierr;
1786d0f46423SBarry Smith   PetscInt          *aj=a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs,stepval;
1787273d9f13SBarry Smith   PetscTruth        roworiented=a->roworiented;
1788dd6ea824SBarry Smith   const PetscScalar *value = v;
1789f15d580aSBarry Smith   MatScalar         *ap,*aa = a->a,*bap;
179092c4ed94SBarry Smith 
17913a40ed3dSBarry Smith   PetscFunctionBegin;
17920e324ae4SSatish Balay   if (roworiented) {
17930e324ae4SSatish Balay     stepval = (n-1)*bs;
17940e324ae4SSatish Balay   } else {
17950e324ae4SSatish Balay     stepval = (m-1)*bs;
17960e324ae4SSatish Balay   }
179792c4ed94SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
179892c4ed94SBarry Smith     row  = im[k];
17995ef9f2a5SBarry Smith     if (row < 0) continue;
18002515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
1801e32f2f54SBarry Smith     if (row >= a->mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,a->mbs-1);
180292c4ed94SBarry Smith #endif
180392c4ed94SBarry Smith     rp   = aj + ai[row];
180492c4ed94SBarry Smith     ap   = aa + bs2*ai[row];
180592c4ed94SBarry Smith     rmax = imax[row];
180692c4ed94SBarry Smith     nrow = ailen[row];
180792c4ed94SBarry Smith     low  = 0;
1808c71e6ed7SBarry Smith     high = nrow;
180992c4ed94SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
18105ef9f2a5SBarry Smith       if (in[l] < 0) continue;
18112515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
1812e32f2f54SBarry 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);
181392c4ed94SBarry Smith #endif
181492c4ed94SBarry Smith       col = in[l];
181592c4ed94SBarry Smith       if (roworiented) {
18160e324ae4SSatish Balay         value = v + k*(stepval+bs)*bs + l*bs;
18170e324ae4SSatish Balay       } else {
18180e324ae4SSatish Balay         value = v + l*(stepval+bs)*bs + k*bs;
181992c4ed94SBarry Smith       }
18207cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
1821e2ee6c50SBarry Smith       lastcol = col;
182292c4ed94SBarry Smith       while (high-low > 7) {
182392c4ed94SBarry Smith         t = (low+high)/2;
182492c4ed94SBarry Smith         if (rp[t] > col) high = t;
182592c4ed94SBarry Smith         else             low  = t;
182692c4ed94SBarry Smith       }
182792c4ed94SBarry Smith       for (i=low; i<high; i++) {
182892c4ed94SBarry Smith         if (rp[i] > col) break;
182992c4ed94SBarry Smith         if (rp[i] == col) {
18308a84c255SSatish Balay           bap  = ap +  bs2*i;
18310e324ae4SSatish Balay           if (roworiented) {
18328a84c255SSatish Balay             if (is == ADD_VALUES) {
1833dd9472c6SBarry Smith               for (ii=0; ii<bs; ii++,value+=stepval) {
1834dd9472c6SBarry Smith                 for (jj=ii; jj<bs2; jj+=bs) {
18358a84c255SSatish Balay                   bap[jj] += *value++;
1836dd9472c6SBarry Smith                 }
1837dd9472c6SBarry Smith               }
18380e324ae4SSatish Balay             } else {
1839dd9472c6SBarry Smith               for (ii=0; ii<bs; ii++,value+=stepval) {
1840dd9472c6SBarry Smith                 for (jj=ii; jj<bs2; jj+=bs) {
18410e324ae4SSatish Balay                   bap[jj] = *value++;
18428a84c255SSatish Balay                 }
1843dd9472c6SBarry Smith               }
1844dd9472c6SBarry Smith             }
18450e324ae4SSatish Balay           } else {
18460e324ae4SSatish Balay             if (is == ADD_VALUES) {
1847dd9472c6SBarry Smith               for (ii=0; ii<bs; ii++,value+=stepval) {
1848dd9472c6SBarry Smith                 for (jj=0; jj<bs; jj++) {
18490e324ae4SSatish Balay                   *bap++ += *value++;
1850dd9472c6SBarry Smith                 }
1851dd9472c6SBarry Smith               }
18520e324ae4SSatish Balay             } else {
1853dd9472c6SBarry Smith               for (ii=0; ii<bs; ii++,value+=stepval) {
1854dd9472c6SBarry Smith                 for (jj=0; jj<bs; jj++) {
18550e324ae4SSatish Balay                   *bap++  = *value++;
18560e324ae4SSatish Balay                 }
18578a84c255SSatish Balay               }
1858dd9472c6SBarry Smith             }
1859dd9472c6SBarry Smith           }
1860f1241b54SBarry Smith           goto noinsert2;
186192c4ed94SBarry Smith         }
186292c4ed94SBarry Smith       }
186389280ab3SLois Curfman McInnes       if (nonew == 1) goto noinsert2;
1864e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
1865fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
1866c03d1d03SSatish Balay       N = nrow++ - 1; high++;
186792c4ed94SBarry Smith       /* shift up all the later entries in this row */
186892c4ed94SBarry Smith       for (ii=N; ii>=i; ii--) {
186992c4ed94SBarry Smith         rp[ii+1] = rp[ii];
1870549d3d68SSatish Balay         ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
187192c4ed94SBarry Smith       }
1872549d3d68SSatish Balay       if (N >= i) {
1873549d3d68SSatish Balay         ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1874549d3d68SSatish Balay       }
187592c4ed94SBarry Smith       rp[i] = col;
18768a84c255SSatish Balay       bap   = ap +  bs2*i;
18770e324ae4SSatish Balay       if (roworiented) {
1878dd9472c6SBarry Smith         for (ii=0; ii<bs; ii++,value+=stepval) {
1879dd9472c6SBarry Smith           for (jj=ii; jj<bs2; jj+=bs) {
18800e324ae4SSatish Balay             bap[jj] = *value++;
1881dd9472c6SBarry Smith           }
1882dd9472c6SBarry Smith         }
18830e324ae4SSatish Balay       } else {
1884dd9472c6SBarry Smith         for (ii=0; ii<bs; ii++,value+=stepval) {
1885dd9472c6SBarry Smith           for (jj=0; jj<bs; jj++) {
18860e324ae4SSatish Balay             *bap++  = *value++;
18870e324ae4SSatish Balay           }
1888dd9472c6SBarry Smith         }
1889dd9472c6SBarry Smith       }
1890f1241b54SBarry Smith       noinsert2:;
189192c4ed94SBarry Smith       low = i;
189292c4ed94SBarry Smith     }
189392c4ed94SBarry Smith     ailen[row] = nrow;
189492c4ed94SBarry Smith   }
18953a40ed3dSBarry Smith   PetscFunctionReturn(0);
189692c4ed94SBarry Smith }
189726e093fcSHong Zhang 
18984a2ae208SSatish Balay #undef __FUNCT__
18994a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqBAIJ"
1900dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqBAIJ(Mat A,MatAssemblyType mode)
1901584200bdSSatish Balay {
1902584200bdSSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
1903c1ac3661SBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
1904d0f46423SBarry Smith   PetscInt       m = A->rmap->N,*ip,N,*ailen = a->ilen;
19056849ba73SBarry Smith   PetscErrorCode ierr;
1906c1ac3661SBarry Smith   PetscInt       mbs = a->mbs,bs2 = a->bs2,rmax = 0;
19073f1db9ecSBarry Smith   MatScalar      *aa = a->a,*ap;
19083447b6efSHong Zhang   PetscReal      ratio=0.6;
1909584200bdSSatish Balay 
19103a40ed3dSBarry Smith   PetscFunctionBegin;
19113a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
1912584200bdSSatish Balay 
191343ee02c3SBarry Smith   if (m) rmax = ailen[0];
1914584200bdSSatish Balay   for (i=1; i<mbs; i++) {
1915584200bdSSatish Balay     /* move each row back by the amount of empty slots (fshift) before it*/
1916584200bdSSatish Balay     fshift += imax[i-1] - ailen[i-1];
1917d402145bSBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
1918584200bdSSatish Balay     if (fshift) {
1919a7c10996SSatish Balay       ip = aj + ai[i]; ap = aa + bs2*ai[i];
1920584200bdSSatish Balay       N = ailen[i];
1921584200bdSSatish Balay       for (j=0; j<N; j++) {
1922584200bdSSatish Balay         ip[j-fshift] = ip[j];
1923549d3d68SSatish Balay         ierr = PetscMemcpy(ap+(j-fshift)*bs2,ap+j*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1924584200bdSSatish Balay       }
1925584200bdSSatish Balay     }
1926584200bdSSatish Balay     ai[i] = ai[i-1] + ailen[i-1];
1927584200bdSSatish Balay   }
1928584200bdSSatish Balay   if (mbs) {
1929584200bdSSatish Balay     fshift += imax[mbs-1] - ailen[mbs-1];
1930584200bdSSatish Balay     ai[mbs] = ai[mbs-1] + ailen[mbs-1];
1931584200bdSSatish Balay   }
1932584200bdSSatish Balay   /* reset ilen and imax for each row */
1933584200bdSSatish Balay   for (i=0; i<mbs; i++) {
1934584200bdSSatish Balay     ailen[i] = imax[i] = ai[i+1] - ai[i];
1935584200bdSSatish Balay   }
1936a7c10996SSatish Balay   a->nz = ai[mbs];
1937584200bdSSatish Balay 
1938584200bdSSatish Balay   /* diagonals may have moved, so kill the diagonal pointers */
1939b01c7715SBarry Smith   a->idiagvalid = PETSC_FALSE;
1940584200bdSSatish Balay   if (fshift && a->diag) {
1941606d414cSSatish Balay     ierr = PetscFree(a->diag);CHKERRQ(ierr);
194252e6d16bSBarry Smith     ierr = PetscLogObjectMemory(A,-(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
1943584200bdSSatish Balay     a->diag = 0;
1944584200bdSSatish Balay   }
194565e19b50SBarry 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);
1946d0f46423SBarry 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);
1947ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues is %D\n",a->reallocs);CHKERRQ(ierr);
1948ae15b995SBarry Smith   ierr = PetscInfo1(A,"Most nonzeros blocks in any row is %D\n",rmax);CHKERRQ(ierr);
19498e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
1950e2f3b5e9SSatish Balay   a->reallocs          = 0;
19510e6d2581SBarry Smith   A->info.nz_unneeded  = (PetscReal)fshift*bs2;
1952cf4441caSHong Zhang 
1953cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
19542f53aa61SHong Zhang   if (a->compressedrow.use){
1955317fbc4cSHong Zhang     ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,mbs,ratio);CHKERRQ(ierr);
195673e7a558SHong Zhang   }
195788e51ccdSHong Zhang 
195888e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
19593a40ed3dSBarry Smith   PetscFunctionReturn(0);
1960584200bdSSatish Balay }
1961584200bdSSatish Balay 
1962bea157c4SSatish Balay /*
1963bea157c4SSatish Balay    This function returns an array of flags which indicate the locations of contiguous
1964bea157c4SSatish Balay    blocks that should be zeroed. for eg: if bs = 3  and is = [0,1,2,3,5,6,7,8,9]
1965bea157c4SSatish Balay    then the resulting sizes = [3,1,1,3,1] correspondig to sets [(0,1,2),(3),(5),(6,7,8),(9)]
1966bea157c4SSatish Balay    Assume: sizes should be long enough to hold all the values.
1967bea157c4SSatish Balay */
19684a2ae208SSatish Balay #undef __FUNCT__
19694a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqBAIJ_Check_Blocks"
1970c1ac3661SBarry Smith static PetscErrorCode MatZeroRows_SeqBAIJ_Check_Blocks(PetscInt idx[],PetscInt n,PetscInt bs,PetscInt sizes[], PetscInt *bs_max)
1971d9b7c43dSSatish Balay {
1972c1ac3661SBarry Smith   PetscInt   i,j,k,row;
1973bea157c4SSatish Balay   PetscTruth flg;
19743a40ed3dSBarry Smith 
1975433994e6SBarry Smith   PetscFunctionBegin;
1976bea157c4SSatish Balay   for (i=0,j=0; i<n; j++) {
1977bea157c4SSatish Balay     row = idx[i];
1978bea157c4SSatish Balay     if (row%bs!=0) { /* Not the begining of a block */
1979bea157c4SSatish Balay       sizes[j] = 1;
1980bea157c4SSatish Balay       i++;
1981e4fda26cSSatish Balay     } else if (i+bs > n) { /* complete block doesn't exist (at idx end) */
1982bea157c4SSatish Balay       sizes[j] = 1;         /* Also makes sure atleast 'bs' values exist for next else */
1983bea157c4SSatish Balay       i++;
1984bea157c4SSatish Balay     } else { /* Begining of the block, so check if the complete block exists */
1985bea157c4SSatish Balay       flg = PETSC_TRUE;
1986bea157c4SSatish Balay       for (k=1; k<bs; k++) {
1987bea157c4SSatish Balay         if (row+k != idx[i+k]) { /* break in the block */
1988bea157c4SSatish Balay           flg = PETSC_FALSE;
1989bea157c4SSatish Balay           break;
1990d9b7c43dSSatish Balay         }
1991bea157c4SSatish Balay       }
1992abc0a331SBarry Smith       if (flg) { /* No break in the bs */
1993bea157c4SSatish Balay         sizes[j] = bs;
1994bea157c4SSatish Balay         i+= bs;
1995bea157c4SSatish Balay       } else {
1996bea157c4SSatish Balay         sizes[j] = 1;
1997bea157c4SSatish Balay         i++;
1998bea157c4SSatish Balay       }
1999bea157c4SSatish Balay     }
2000bea157c4SSatish Balay   }
2001bea157c4SSatish Balay   *bs_max = j;
20023a40ed3dSBarry Smith   PetscFunctionReturn(0);
2003d9b7c43dSSatish Balay }
2004d9b7c43dSSatish Balay 
20054a2ae208SSatish Balay #undef __FUNCT__
20064a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqBAIJ"
2007f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqBAIJ(Mat A,PetscInt is_n,const PetscInt is_idx[],PetscScalar diag)
2008d9b7c43dSSatish Balay {
2009d9b7c43dSSatish Balay   Mat_SeqBAIJ    *baij=(Mat_SeqBAIJ*)A->data;
2010dfbe8321SBarry Smith   PetscErrorCode ierr;
2011f4df32b1SMatthew Knepley   PetscInt       i,j,k,count,*rows;
2012d0f46423SBarry Smith   PetscInt       bs=A->rmap->bs,bs2=baij->bs2,*sizes,row,bs_max;
201387828ca2SBarry Smith   PetscScalar    zero = 0.0;
20143f1db9ecSBarry Smith   MatScalar      *aa;
2015d9b7c43dSSatish Balay 
20163a40ed3dSBarry Smith   PetscFunctionBegin;
2017d9b7c43dSSatish Balay   /* Make a copy of the IS and  sort it */
2018bea157c4SSatish Balay   /* allocate memory for rows,sizes */
2019fca92195SBarry Smith   ierr  = PetscMalloc2(is_n,PetscInt,&rows,2*is_n,PetscInt,&sizes);CHKERRQ(ierr);
2020bea157c4SSatish Balay 
2021563b5814SBarry Smith   /* copy IS values to rows, and sort them */
2022bea157c4SSatish Balay   for (i=0; i<is_n; i++) { rows[i] = is_idx[i]; }
2023bea157c4SSatish Balay   ierr = PetscSortInt(is_n,rows);CHKERRQ(ierr);
2024a9817697SBarry Smith   if (baij->keepnonzeropattern) {
2025dffd3267SBarry Smith     for (i=0; i<is_n; i++) { sizes[i] = 1; }
2026dffd3267SBarry Smith     bs_max = is_n;
202788e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
2028dffd3267SBarry Smith   } else {
2029bea157c4SSatish Balay     ierr = MatZeroRows_SeqBAIJ_Check_Blocks(rows,is_n,bs,sizes,&bs_max);CHKERRQ(ierr);
203088e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
2031dffd3267SBarry Smith   }
2032bea157c4SSatish Balay 
2033bea157c4SSatish Balay   for (i=0,j=0; i<bs_max; j+=sizes[i],i++) {
2034bea157c4SSatish Balay     row   = rows[j];
2035e32f2f54SBarry Smith     if (row < 0 || row > A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range",row);
2036bea157c4SSatish Balay     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
2037b31fbe3bSSatish Balay     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
2038a9817697SBarry Smith     if (sizes[i] == bs && !baij->keepnonzeropattern) {
2039f4df32b1SMatthew Knepley       if (diag != 0.0) {
2040bea157c4SSatish Balay         if (baij->ilen[row/bs] > 0) {
2041bea157c4SSatish Balay           baij->ilen[row/bs]       = 1;
2042bea157c4SSatish Balay           baij->j[baij->i[row/bs]] = row/bs;
2043bea157c4SSatish Balay           ierr = PetscMemzero(aa,count*bs*sizeof(MatScalar));CHKERRQ(ierr);
2044a07cd24cSSatish Balay         }
2045563b5814SBarry Smith         /* Now insert all the diagonal values for this bs */
2046bea157c4SSatish Balay         for (k=0; k<bs; k++) {
2047f4df32b1SMatthew Knepley           ierr = (*A->ops->setvalues)(A,1,rows+j+k,1,rows+j+k,&diag,INSERT_VALUES);CHKERRQ(ierr);
2048bea157c4SSatish Balay         }
2049f4df32b1SMatthew Knepley       } else { /* (diag == 0.0) */
2050bea157c4SSatish Balay         baij->ilen[row/bs] = 0;
2051f4df32b1SMatthew Knepley       } /* end (diag == 0.0) */
2052bea157c4SSatish Balay     } else { /* (sizes[i] != bs) */
2053aa482453SBarry Smith #if defined (PETSC_USE_DEBUG)
2054e32f2f54SBarry Smith       if (sizes[i] != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal Error. Value should be 1");
2055bea157c4SSatish Balay #endif
2056bea157c4SSatish Balay       for (k=0; k<count; k++) {
2057d9b7c43dSSatish Balay         aa[0] =  zero;
2058d9b7c43dSSatish Balay         aa    += bs;
2059d9b7c43dSSatish Balay       }
2060f4df32b1SMatthew Knepley       if (diag != 0.0) {
2061f4df32b1SMatthew Knepley         ierr = (*A->ops->setvalues)(A,1,rows+j,1,rows+j,&diag,INSERT_VALUES);CHKERRQ(ierr);
2062d9b7c43dSSatish Balay       }
2063d9b7c43dSSatish Balay     }
2064bea157c4SSatish Balay   }
2065bea157c4SSatish Balay 
2066fca92195SBarry Smith   ierr = PetscFree2(rows,sizes);CHKERRQ(ierr);
20679a8dea36SBarry Smith   ierr = MatAssemblyEnd_SeqBAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20683a40ed3dSBarry Smith   PetscFunctionReturn(0);
2069d9b7c43dSSatish Balay }
20701c351548SSatish Balay 
20714a2ae208SSatish Balay #undef __FUNCT__
20724a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqBAIJ"
2073c1ac3661SBarry Smith PetscErrorCode MatSetValues_SeqBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
20742d61bbb3SSatish Balay {
20752d61bbb3SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
2076e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
2077c1ac3661SBarry Smith   PetscInt       *imax=a->imax,*ai=a->i,*ailen=a->ilen;
2078d0f46423SBarry Smith   PetscInt       *aj=a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
20796849ba73SBarry Smith   PetscErrorCode ierr;
2080c1ac3661SBarry Smith   PetscInt       ridx,cidx,bs2=a->bs2;
2081273d9f13SBarry Smith   PetscTruth     roworiented=a->roworiented;
20823f1db9ecSBarry Smith   MatScalar      *ap,value,*aa=a->a,*bap;
20832d61bbb3SSatish Balay 
20842d61bbb3SSatish Balay   PetscFunctionBegin;
208571fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
20862d61bbb3SSatish Balay   for (k=0; k<m; k++) { /* loop over added rows */
2087085a36d4SBarry Smith     row  = im[k];
2088085a36d4SBarry Smith     brow = row/bs;
20895ef9f2a5SBarry Smith     if (row < 0) continue;
20902515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
2091e32f2f54SBarry 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);
20922d61bbb3SSatish Balay #endif
20932d61bbb3SSatish Balay     rp   = aj + ai[brow];
20942d61bbb3SSatish Balay     ap   = aa + bs2*ai[brow];
20952d61bbb3SSatish Balay     rmax = imax[brow];
20962d61bbb3SSatish Balay     nrow = ailen[brow];
20972d61bbb3SSatish Balay     low  = 0;
2098c71e6ed7SBarry Smith     high = nrow;
20992d61bbb3SSatish Balay     for (l=0; l<n; l++) { /* loop over added columns */
21005ef9f2a5SBarry Smith       if (in[l] < 0) continue;
21012515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
2102e32f2f54SBarry 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);
21032d61bbb3SSatish Balay #endif
21042d61bbb3SSatish Balay       col = in[l]; bcol = col/bs;
21052d61bbb3SSatish Balay       ridx = row % bs; cidx = col % bs;
21062d61bbb3SSatish Balay       if (roworiented) {
21075ef9f2a5SBarry Smith         value = v[l + k*n];
21082d61bbb3SSatish Balay       } else {
21092d61bbb3SSatish Balay         value = v[k + l*m];
21102d61bbb3SSatish Balay       }
21117cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
2112e2ee6c50SBarry Smith       lastcol = col;
21132d61bbb3SSatish Balay       while (high-low > 7) {
21142d61bbb3SSatish Balay         t = (low+high)/2;
21152d61bbb3SSatish Balay         if (rp[t] > bcol) high = t;
21162d61bbb3SSatish Balay         else              low  = t;
21172d61bbb3SSatish Balay       }
21182d61bbb3SSatish Balay       for (i=low; i<high; i++) {
21192d61bbb3SSatish Balay         if (rp[i] > bcol) break;
21202d61bbb3SSatish Balay         if (rp[i] == bcol) {
21212d61bbb3SSatish Balay           bap  = ap +  bs2*i + bs*cidx + ridx;
21222d61bbb3SSatish Balay           if (is == ADD_VALUES) *bap += value;
21232d61bbb3SSatish Balay           else                  *bap  = value;
21242d61bbb3SSatish Balay           goto noinsert1;
21252d61bbb3SSatish Balay         }
21262d61bbb3SSatish Balay       }
21272d61bbb3SSatish Balay       if (nonew == 1) goto noinsert1;
2128e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
2129fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
2130c03d1d03SSatish Balay       N = nrow++ - 1; high++;
21312d61bbb3SSatish Balay       /* shift up all the later entries in this row */
21322d61bbb3SSatish Balay       for (ii=N; ii>=i; ii--) {
21332d61bbb3SSatish Balay         rp[ii+1] = rp[ii];
2134549d3d68SSatish Balay         ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
21352d61bbb3SSatish Balay       }
2136549d3d68SSatish Balay       if (N>=i) {
2137549d3d68SSatish Balay         ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
2138549d3d68SSatish Balay       }
21392d61bbb3SSatish Balay       rp[i]                      = bcol;
21402d61bbb3SSatish Balay       ap[bs2*i + bs*cidx + ridx] = value;
2141085a36d4SBarry Smith       a->nz++;
21422d61bbb3SSatish Balay       noinsert1:;
21432d61bbb3SSatish Balay       low = i;
21442d61bbb3SSatish Balay     }
21452d61bbb3SSatish Balay     ailen[brow] = nrow;
21462d61bbb3SSatish Balay   }
214788e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
21482d61bbb3SSatish Balay   PetscFunctionReturn(0);
21492d61bbb3SSatish Balay }
21502d61bbb3SSatish Balay 
21514a2ae208SSatish Balay #undef __FUNCT__
21524a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqBAIJ"
21530481f469SBarry Smith PetscErrorCode MatILUFactor_SeqBAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
21542d61bbb3SSatish Balay {
21552d61bbb3SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)inA->data;
21562d61bbb3SSatish Balay   Mat            outA;
2157dfbe8321SBarry Smith   PetscErrorCode ierr;
2158667159a5SBarry Smith   PetscTruth     row_identity,col_identity;
21592d61bbb3SSatish Balay 
21602d61bbb3SSatish Balay   PetscFunctionBegin;
2161e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for in-place ILU");
2162667159a5SBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2163667159a5SBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2164667159a5SBarry Smith   if (!row_identity || !col_identity) {
2165e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for in-place ILU");
2166667159a5SBarry Smith   }
21672d61bbb3SSatish Balay 
21682d61bbb3SSatish Balay   outA            = inA;
2169d5f3da31SBarry Smith   inA->factortype = MAT_FACTOR_LU;
21702d61bbb3SSatish Balay 
2171c4992f7dSBarry Smith   ierr = MatMarkDiagonal_SeqBAIJ(inA);CHKERRQ(ierr);
2172cf242676SKris Buschelman 
2173c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
2174c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr); }
2175c3122656SLisandro Dalcin   a->row = row;
2176c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
2177c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr); }
2178c3122656SLisandro Dalcin   a->col = col;
2179c38d4ed2SBarry Smith 
2180c38d4ed2SBarry Smith   /* Create the invert permutation so that it can be used in MatLUFactorNumeric() */
2181a23ff555SBarry Smith   if (a->icol) {
2182a23ff555SBarry Smith     ierr = ISDestroy(a->icol);CHKERRQ(ierr);
2183a23ff555SBarry Smith   }
21844c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
218552e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2186c38d4ed2SBarry Smith 
21878b1456e3SHong Zhang   ierr = MatSeqBAIJSetNumericFactorization_inplace(inA,(PetscTruth)(row_identity && col_identity));CHKERRQ(ierr);
2188c38d4ed2SBarry Smith   if (!a->solve_work) {
2189d0f46423SBarry Smith     ierr = PetscMalloc((inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2190d0f46423SBarry Smith     ierr = PetscLogObjectMemory(inA,(inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar));CHKERRQ(ierr);
2191c38d4ed2SBarry Smith   }
2192719d5645SBarry Smith   ierr = MatLUFactorNumeric(outA,inA,info);CHKERRQ(ierr);
2193667159a5SBarry Smith 
21942d61bbb3SSatish Balay   PetscFunctionReturn(0);
21952d61bbb3SSatish Balay }
2196d9b7c43dSSatish Balay 
2197fb2e594dSBarry Smith EXTERN_C_BEGIN
21984a2ae208SSatish Balay #undef __FUNCT__
21994a2ae208SSatish Balay #define __FUNCT__ "MatSeqBAIJSetColumnIndices_SeqBAIJ"
2200be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetColumnIndices_SeqBAIJ(Mat mat,PetscInt *indices)
220127a8da17SBarry Smith {
220227a8da17SBarry Smith   Mat_SeqBAIJ *baij = (Mat_SeqBAIJ *)mat->data;
2203bdb1c0e1SJed Brown   PetscInt    i,nz,mbs;
220427a8da17SBarry Smith 
220527a8da17SBarry Smith   PetscFunctionBegin;
220614db4f74SSatish Balay   nz  = baij->maxnz/baij->bs2;
2207bdb1c0e1SJed Brown   mbs = baij->mbs;
220827a8da17SBarry Smith   for (i=0; i<nz; i++) {
220927a8da17SBarry Smith     baij->j[i] = indices[i];
221027a8da17SBarry Smith   }
221127a8da17SBarry Smith   baij->nz = nz;
2212bdb1c0e1SJed Brown   for (i=0; i<mbs; i++) {
221327a8da17SBarry Smith     baij->ilen[i] = baij->imax[i];
221427a8da17SBarry Smith   }
221527a8da17SBarry Smith   PetscFunctionReturn(0);
221627a8da17SBarry Smith }
2217fb2e594dSBarry Smith EXTERN_C_END
221827a8da17SBarry Smith 
22194a2ae208SSatish Balay #undef __FUNCT__
22204a2ae208SSatish Balay #define __FUNCT__ "MatSeqBAIJSetColumnIndices"
222127a8da17SBarry Smith /*@
222227a8da17SBarry Smith     MatSeqBAIJSetColumnIndices - Set the column indices for all the rows
222327a8da17SBarry Smith        in the matrix.
222427a8da17SBarry Smith 
222527a8da17SBarry Smith   Input Parameters:
222627a8da17SBarry Smith +  mat - the SeqBAIJ matrix
222727a8da17SBarry Smith -  indices - the column indices
222827a8da17SBarry Smith 
222915091d37SBarry Smith   Level: advanced
223015091d37SBarry Smith 
223127a8da17SBarry Smith   Notes:
223227a8da17SBarry Smith     This can be called if you have precomputed the nonzero structure of the
223327a8da17SBarry Smith   matrix and want to provide it to the matrix object to improve the performance
223427a8da17SBarry Smith   of the MatSetValues() operation.
223527a8da17SBarry Smith 
223627a8da17SBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2237d1be2dadSMatthew Knepley   MatCreateSeqBAIJ(), and the columns indices MUST be sorted.
223827a8da17SBarry Smith 
223927a8da17SBarry Smith     MUST be called before any calls to MatSetValues();
224027a8da17SBarry Smith 
224127a8da17SBarry Smith @*/
2242be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetColumnIndices(Mat mat,PetscInt *indices)
224327a8da17SBarry Smith {
2244c1ac3661SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
224527a8da17SBarry Smith 
224627a8da17SBarry Smith   PetscFunctionBegin;
22470700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
22484482741eSBarry Smith   PetscValidPointer(indices,2);
2249c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqBAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
225027a8da17SBarry Smith   if (f) {
225127a8da17SBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2252e7e72b3dSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Wrong type of matrix to set column indices");
225327a8da17SBarry Smith   PetscFunctionReturn(0);
225427a8da17SBarry Smith }
225527a8da17SBarry Smith 
22564a2ae208SSatish Balay #undef __FUNCT__
2257985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqBAIJ"
2258985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqBAIJ(Mat A,Vec v,PetscInt idx[])
2259273d9f13SBarry Smith {
2260273d9f13SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
2261dfbe8321SBarry Smith   PetscErrorCode ierr;
2262c1ac3661SBarry Smith   PetscInt       i,j,n,row,bs,*ai,*aj,mbs;
2263273d9f13SBarry Smith   PetscReal      atmp;
226487828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
2265273d9f13SBarry Smith   MatScalar      *aa;
2266c1ac3661SBarry Smith   PetscInt       ncols,brow,krow,kcol;
2267273d9f13SBarry Smith 
2268273d9f13SBarry Smith   PetscFunctionBegin;
2269e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2270d0f46423SBarry Smith   bs   = A->rmap->bs;
2271273d9f13SBarry Smith   aa   = a->a;
2272273d9f13SBarry Smith   ai   = a->i;
2273273d9f13SBarry Smith   aj   = a->j;
2274273d9f13SBarry Smith   mbs  = a->mbs;
2275273d9f13SBarry Smith 
22762dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
22771ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2278273d9f13SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2279e32f2f54SBarry Smith   if (n != A->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2280273d9f13SBarry Smith   for (i=0; i<mbs; i++) {
2281273d9f13SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2282273d9f13SBarry Smith     brow  = bs*i;
2283273d9f13SBarry Smith     for (j=0; j<ncols; j++){
2284273d9f13SBarry Smith       for (kcol=0; kcol<bs; kcol++){
2285273d9f13SBarry Smith         for (krow=0; krow<bs; krow++){
2286273d9f13SBarry Smith           atmp = PetscAbsScalar(*aa);aa++;
2287273d9f13SBarry Smith           row = brow + krow;    /* row index */
2288a83599f4SBarry Smith           /* printf("val[%d,%d]: %G\n",row,bcol+kcol,atmp); */
2289985db425SBarry Smith           if (PetscAbsScalar(x[row]) < atmp) {x[row] = atmp; if (idx) idx[row] = bs*(*aj) + kcol;}
2290273d9f13SBarry Smith         }
2291273d9f13SBarry Smith       }
2292273d9f13SBarry Smith       aj++;
2293273d9f13SBarry Smith     }
2294273d9f13SBarry Smith   }
22951ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2296273d9f13SBarry Smith   PetscFunctionReturn(0);
2297273d9f13SBarry Smith }
2298273d9f13SBarry Smith 
22994a2ae208SSatish Balay #undef __FUNCT__
23003c896bc6SHong Zhang #define __FUNCT__ "MatCopy_SeqBAIJ"
23013c896bc6SHong Zhang PetscErrorCode MatCopy_SeqBAIJ(Mat A,Mat B,MatStructure str)
23023c896bc6SHong Zhang {
23033c896bc6SHong Zhang   PetscErrorCode ierr;
23043c896bc6SHong Zhang 
23053c896bc6SHong Zhang   PetscFunctionBegin;
23063c896bc6SHong Zhang   /* If the two matrices have the same copy implementation, use fast copy. */
23073c896bc6SHong Zhang   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
23083c896bc6SHong Zhang     Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
23093c896bc6SHong Zhang     Mat_SeqBAIJ *b = (Mat_SeqBAIJ*)B->data;
23103c896bc6SHong Zhang 
2311e7e72b3dSBarry 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");
2312d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->N])*sizeof(PetscScalar));CHKERRQ(ierr);
23133c896bc6SHong Zhang   } else {
23143c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
23153c896bc6SHong Zhang   }
23163c896bc6SHong Zhang   PetscFunctionReturn(0);
23173c896bc6SHong Zhang }
23183c896bc6SHong Zhang 
23193c896bc6SHong Zhang #undef __FUNCT__
23204a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqBAIJ"
2321dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqBAIJ(Mat A)
2322273d9f13SBarry Smith {
2323dfbe8321SBarry Smith   PetscErrorCode ierr;
2324273d9f13SBarry Smith 
2325273d9f13SBarry Smith   PetscFunctionBegin;
2326db4efbfdSBarry Smith   ierr =  MatSeqBAIJSetPreallocation_SeqBAIJ(A,-PetscMax(A->rmap->bs,1),PETSC_DEFAULT,0);CHKERRQ(ierr);
2327273d9f13SBarry Smith   PetscFunctionReturn(0);
2328273d9f13SBarry Smith }
2329273d9f13SBarry Smith 
23304a2ae208SSatish Balay #undef __FUNCT__
23314a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqBAIJ"
2332dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqBAIJ(Mat A,PetscScalar *array[])
2333f2a5309cSSatish Balay {
2334f2a5309cSSatish Balay   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
2335f2a5309cSSatish Balay   PetscFunctionBegin;
2336f2a5309cSSatish Balay   *array = a->a;
2337f2a5309cSSatish Balay   PetscFunctionReturn(0);
2338f2a5309cSSatish Balay }
2339f2a5309cSSatish Balay 
23404a2ae208SSatish Balay #undef __FUNCT__
23414a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqBAIJ"
2342dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqBAIJ(Mat A,PetscScalar *array[])
2343f2a5309cSSatish Balay {
2344f2a5309cSSatish Balay   PetscFunctionBegin;
2345f2a5309cSSatish Balay   PetscFunctionReturn(0);
2346f2a5309cSSatish Balay }
2347f2a5309cSSatish Balay 
234842ee4b1aSHong Zhang #undef __FUNCT__
234942ee4b1aSHong Zhang #define __FUNCT__ "MatAXPY_SeqBAIJ"
2350f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
235142ee4b1aSHong Zhang {
235242ee4b1aSHong Zhang   Mat_SeqBAIJ    *x  = (Mat_SeqBAIJ *)X->data,*y = (Mat_SeqBAIJ *)Y->data;
2353dfbe8321SBarry Smith   PetscErrorCode ierr;
2354d0f46423SBarry Smith   PetscInt       i,bs=Y->rmap->bs,j,bs2;
23550805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
235642ee4b1aSHong Zhang 
235742ee4b1aSHong Zhang   PetscFunctionBegin;
235842ee4b1aSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
2359f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2360f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2361c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2362c4319e64SHong Zhang     if (y->xtoy && y->XtoY != X) {
2363c4319e64SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2364c4319e64SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2365c537a176SHong Zhang     }
2366c4319e64SHong Zhang     if (!y->xtoy) { /* get xtoy */
2367c4319e64SHong Zhang       ierr = MatAXPYGetxtoy_Private(x->mbs,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2368c4319e64SHong Zhang       y->XtoY = X;
2369c009d632SSatish Balay       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2370c537a176SHong Zhang     }
2371c4319e64SHong Zhang     bs2 = bs*bs;
2372c537a176SHong Zhang     for (i=0; i<x->nz; i++) {
2373c4319e64SHong Zhang       j = 0;
2374c4319e64SHong Zhang       while (j < bs2){
2375f4df32b1SMatthew Knepley         y->a[bs2*y->xtoy[i]+j] += a*(x->a[bs2*i+j]);
2376c4319e64SHong Zhang         j++;
2377c537a176SHong Zhang       }
2378c4319e64SHong Zhang     }
23791e2582c4SBarry 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);
238042ee4b1aSHong Zhang   } else {
2381f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
238242ee4b1aSHong Zhang   }
238342ee4b1aSHong Zhang   PetscFunctionReturn(0);
238442ee4b1aSHong Zhang }
238542ee4b1aSHong Zhang 
238699cafbc1SBarry Smith #undef __FUNCT__
238741c166b1SJed Brown #define __FUNCT__ "MatSetBlockSize_SeqBAIJ"
238841c166b1SJed Brown PetscErrorCode MatSetBlockSize_SeqBAIJ(Mat A,PetscInt bs)
238941c166b1SJed Brown {
239041c166b1SJed Brown   PetscInt rbs,cbs;
239141c166b1SJed Brown   PetscErrorCode ierr;
239241c166b1SJed Brown 
239341c166b1SJed Brown   PetscFunctionBegin;
239441c166b1SJed Brown   ierr = PetscLayoutGetBlockSize(A->rmap,&rbs);CHKERRQ(ierr);
239541c166b1SJed Brown   ierr = PetscLayoutGetBlockSize(A->cmap,&cbs);CHKERRQ(ierr);
2396e32f2f54SBarry Smith   if (rbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with BAIJ %d",bs,rbs);
2397e32f2f54SBarry Smith   if (cbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with BAIJ %d",bs,cbs);
239841c166b1SJed Brown   PetscFunctionReturn(0);
239941c166b1SJed Brown }
240041c166b1SJed Brown 
240141c166b1SJed Brown #undef __FUNCT__
240299cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqBAIJ"
240399cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqBAIJ(Mat A)
240499cafbc1SBarry Smith {
240599cafbc1SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
240699cafbc1SBarry Smith   PetscInt       i,nz = a->bs2*a->i[a->mbs];
2407dd6ea824SBarry Smith   MatScalar      *aa = a->a;
240899cafbc1SBarry Smith 
240999cafbc1SBarry Smith   PetscFunctionBegin;
241099cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
241199cafbc1SBarry Smith   PetscFunctionReturn(0);
241299cafbc1SBarry Smith }
241399cafbc1SBarry Smith 
241499cafbc1SBarry Smith #undef __FUNCT__
241599cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqBAIJ"
241699cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqBAIJ(Mat A)
241799cafbc1SBarry Smith {
241899cafbc1SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
241999cafbc1SBarry Smith   PetscInt       i,nz = a->bs2*a->i[a->mbs];
2420dd6ea824SBarry Smith   MatScalar      *aa = a->a;
242199cafbc1SBarry Smith 
242299cafbc1SBarry Smith   PetscFunctionBegin;
242399cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
242499cafbc1SBarry Smith   PetscFunctionReturn(0);
242599cafbc1SBarry Smith }
242699cafbc1SBarry Smith 
24273acb8795SBarry Smith extern PetscErrorCode MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
24283acb8795SBarry Smith 
24293acb8795SBarry Smith #undef __FUNCT__
24303acb8795SBarry Smith #define __FUNCT__ "MatGetColumnIJ_SeqBAIJ"
24313acb8795SBarry Smith /*
24323acb8795SBarry Smith     Code almost idential to MatGetColumnIJ_SeqAIJ() should share common code
24333acb8795SBarry Smith */
24343acb8795SBarry Smith PetscErrorCode MatGetColumnIJ_SeqBAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
24353acb8795SBarry Smith {
24363acb8795SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
24373acb8795SBarry Smith   PetscErrorCode ierr;
24383acb8795SBarry Smith   PetscInt       bs = A->rmap->bs,i,*collengths,*cia,*cja,n = A->cmap->n/bs,m = A->rmap->n/bs;
24393acb8795SBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
24403acb8795SBarry Smith 
24413acb8795SBarry Smith   PetscFunctionBegin;
24423acb8795SBarry Smith   *nn = n;
24433acb8795SBarry Smith   if (!ia) PetscFunctionReturn(0);
2444e7e72b3dSBarry Smith   if (symmetric) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not for BAIJ matrices");
2445e7e72b3dSBarry Smith   else {
24463acb8795SBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
24473acb8795SBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
24483acb8795SBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
24493acb8795SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
24503acb8795SBarry Smith     jj = a->j;
24513acb8795SBarry Smith     for (i=0; i<nz; i++) {
24523acb8795SBarry Smith       collengths[jj[i]]++;
24533acb8795SBarry Smith     }
24543acb8795SBarry Smith     cia[0] = oshift;
24553acb8795SBarry Smith     for (i=0; i<n; i++) {
24563acb8795SBarry Smith       cia[i+1] = cia[i] + collengths[i];
24573acb8795SBarry Smith     }
24583acb8795SBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
24593acb8795SBarry Smith     jj   = a->j;
24603acb8795SBarry Smith     for (row=0; row<m; row++) {
24613acb8795SBarry Smith       mr = a->i[row+1] - a->i[row];
24623acb8795SBarry Smith       for (i=0; i<mr; i++) {
24633acb8795SBarry Smith         col = *jj++;
24643acb8795SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
24653acb8795SBarry Smith       }
24663acb8795SBarry Smith     }
24673acb8795SBarry Smith     ierr = PetscFree(collengths);CHKERRQ(ierr);
24683acb8795SBarry Smith     *ia = cia; *ja = cja;
24693acb8795SBarry Smith   }
24703acb8795SBarry Smith   PetscFunctionReturn(0);
24713acb8795SBarry Smith }
24723acb8795SBarry Smith 
24733acb8795SBarry Smith #undef __FUNCT__
24743acb8795SBarry Smith #define __FUNCT__ "MatRestoreColumnIJ_SeqBAIJ"
24753acb8795SBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqBAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
24763acb8795SBarry Smith {
24773acb8795SBarry Smith   PetscErrorCode ierr;
24783acb8795SBarry Smith 
24793acb8795SBarry Smith   PetscFunctionBegin;
24803acb8795SBarry Smith   if (!ia) PetscFunctionReturn(0);
24813acb8795SBarry Smith   ierr = PetscFree(*ia);CHKERRQ(ierr);
24823acb8795SBarry Smith   ierr = PetscFree(*ja);CHKERRQ(ierr);
24833acb8795SBarry Smith   PetscFunctionReturn(0);
24843acb8795SBarry Smith }
24853acb8795SBarry Smith 
2486f6d58c54SBarry Smith #undef __FUNCT__
2487f6d58c54SBarry Smith #define __FUNCT__ "MatFDColoringApply_BAIJ"
2488f6d58c54SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_BAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2489f6d58c54SBarry Smith {
2490f6d58c54SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
2491f6d58c54SBarry Smith   PetscErrorCode ierr;
2492f6d58c54SBarry Smith   PetscInt       bs = J->rmap->bs,i,j,k,start,end,l,row,col,*srows,**vscaleforrow,m1,m2;
2493f6d58c54SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
2494f6d58c54SBarry Smith   PetscScalar    *vscale_array;
2495f6d58c54SBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin,unorm;
2496f6d58c54SBarry Smith   Vec            w1=coloring->w1,w2=coloring->w2,w3;
2497f6d58c54SBarry Smith   void           *fctx = coloring->fctx;
2498f6d58c54SBarry Smith   PetscTruth     flg = PETSC_FALSE;
2499f6d58c54SBarry Smith   PetscInt       ctype=coloring->ctype,N,col_start=0,col_end=0;
2500f6d58c54SBarry Smith   Vec            x1_tmp;
2501f6d58c54SBarry Smith 
2502f6d58c54SBarry Smith   PetscFunctionBegin;
25030700a824SBarry Smith   PetscValidHeaderSpecific(J,MAT_CLASSID,1);
25040700a824SBarry Smith   PetscValidHeaderSpecific(coloring,MAT_FDCOLORING_CLASSID,2);
25050700a824SBarry Smith   PetscValidHeaderSpecific(x1,VEC_CLASSID,3);
2506e32f2f54SBarry Smith   if (!f) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatFDColoringSetFunction()");
2507f6d58c54SBarry Smith 
2508f6d58c54SBarry Smith   ierr = PetscLogEventBegin(MAT_FDColoringApply,coloring,J,x1,0);CHKERRQ(ierr);
2509f6d58c54SBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
2510f6d58c54SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2511f6d58c54SBarry Smith   if (flg) {
2512f6d58c54SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2513f6d58c54SBarry Smith   } else {
2514f6d58c54SBarry Smith     PetscTruth assembled;
2515f6d58c54SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
2516f6d58c54SBarry Smith     if (assembled) {
2517f6d58c54SBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2518f6d58c54SBarry Smith     }
2519f6d58c54SBarry Smith   }
2520f6d58c54SBarry Smith 
2521f6d58c54SBarry Smith   x1_tmp = x1;
2522f6d58c54SBarry Smith   if (!coloring->vscale){
2523f6d58c54SBarry Smith     ierr = VecDuplicate(x1_tmp,&coloring->vscale);CHKERRQ(ierr);
2524f6d58c54SBarry Smith   }
2525f6d58c54SBarry Smith 
2526f6d58c54SBarry Smith   /*
2527f6d58c54SBarry Smith     This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2528f6d58c54SBarry Smith     coloring->F for the coarser grids from the finest
2529f6d58c54SBarry Smith   */
2530f6d58c54SBarry Smith   if (coloring->F) {
2531f6d58c54SBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2532f6d58c54SBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2533f6d58c54SBarry Smith     if (m1 != m2) {
2534f6d58c54SBarry Smith       coloring->F = 0;
2535f6d58c54SBarry Smith       }
2536f6d58c54SBarry Smith     }
2537f6d58c54SBarry Smith 
2538f6d58c54SBarry Smith   if (coloring->htype[0] == 'w') { /* tacky test; need to make systematic if we add other approaches to computing h*/
2539f6d58c54SBarry Smith     ierr = VecNorm(x1_tmp,NORM_2,&unorm);CHKERRQ(ierr);
2540f6d58c54SBarry Smith   }
2541f6d58c54SBarry Smith   ierr = VecGetOwnershipRange(w1,&start,&end);CHKERRQ(ierr); /* OwnershipRange is used by ghosted x! */
2542f6d58c54SBarry Smith 
2543f6d58c54SBarry Smith   /* Set w1 = F(x1) */
2544f6d58c54SBarry Smith   if (coloring->F) {
2545f6d58c54SBarry Smith     w1          = coloring->F; /* use already computed value of function */
2546f6d58c54SBarry Smith     coloring->F = 0;
2547f6d58c54SBarry Smith   } else {
2548f6d58c54SBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2549f6d58c54SBarry Smith     ierr = (*f)(sctx,x1_tmp,w1,fctx);CHKERRQ(ierr);
2550f6d58c54SBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2551f6d58c54SBarry Smith   }
2552f6d58c54SBarry Smith 
2553f6d58c54SBarry Smith   if (!coloring->w3) {
2554f6d58c54SBarry Smith     ierr = VecDuplicate(x1_tmp,&coloring->w3);CHKERRQ(ierr);
2555f6d58c54SBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2556f6d58c54SBarry Smith   }
2557f6d58c54SBarry Smith   w3 = coloring->w3;
2558f6d58c54SBarry Smith 
2559f6d58c54SBarry Smith     CHKMEMQ;
2560f6d58c54SBarry Smith     /* Compute all the local scale factors, including ghost points */
2561f6d58c54SBarry Smith   ierr = VecGetLocalSize(x1_tmp,&N);CHKERRQ(ierr);
2562f6d58c54SBarry Smith   ierr = VecGetArray(x1_tmp,&xx);CHKERRQ(ierr);
2563f6d58c54SBarry Smith   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2564f6d58c54SBarry Smith   if (ctype == IS_COLORING_GHOSTED){
2565f6d58c54SBarry Smith     col_start = 0; col_end = N;
2566f6d58c54SBarry Smith   } else if (ctype == IS_COLORING_GLOBAL){
2567f6d58c54SBarry Smith     xx = xx - start;
2568f6d58c54SBarry Smith     vscale_array = vscale_array - start;
2569f6d58c54SBarry Smith     col_start = start; col_end = N + start;
2570f6d58c54SBarry Smith   }    CHKMEMQ;
2571f6d58c54SBarry Smith   for (col=col_start; col<col_end; col++){
2572f6d58c54SBarry Smith     /* Loop over each local column, vscale[col] = 1./(epsilon*dx[col]) */
2573f6d58c54SBarry Smith     if (coloring->htype[0] == 'w') {
2574f6d58c54SBarry Smith       dx = 1.0 + unorm;
2575f6d58c54SBarry Smith     } else {
2576f6d58c54SBarry Smith       dx  = xx[col];
2577f6d58c54SBarry Smith     }
2578f6d58c54SBarry Smith     if (dx == 0.0) dx = 1.0;
2579f6d58c54SBarry Smith #if !defined(PETSC_USE_COMPLEX)
2580f6d58c54SBarry Smith     if (dx < umin && dx >= 0.0)      dx = umin;
2581f6d58c54SBarry Smith     else if (dx < 0.0 && dx > -umin) dx = -umin;
2582f6d58c54SBarry Smith #else
2583f6d58c54SBarry Smith     if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2584f6d58c54SBarry Smith     else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2585f6d58c54SBarry Smith #endif
2586f6d58c54SBarry Smith     dx               *= epsilon;
2587f6d58c54SBarry Smith     vscale_array[col] = 1.0/dx;
2588f6d58c54SBarry Smith   }     CHKMEMQ;
2589f6d58c54SBarry Smith   if (ctype == IS_COLORING_GLOBAL)  vscale_array = vscale_array + start;
2590f6d58c54SBarry Smith   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2591f6d58c54SBarry Smith   if (ctype == IS_COLORING_GLOBAL){
2592f6d58c54SBarry Smith     ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2593f6d58c54SBarry Smith     ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2594f6d58c54SBarry Smith   }
2595f6d58c54SBarry Smith   CHKMEMQ;
2596f6d58c54SBarry Smith   if (coloring->vscaleforrow) {
2597f6d58c54SBarry Smith     vscaleforrow = coloring->vscaleforrow;
2598e7e72b3dSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Null Object: coloring->vscaleforrow");
2599f6d58c54SBarry Smith 
2600f6d58c54SBarry Smith   ierr = PetscMalloc(bs*sizeof(PetscInt),&srows);CHKERRQ(ierr);
2601f6d58c54SBarry Smith   /*
2602f6d58c54SBarry Smith     Loop over each color
2603f6d58c54SBarry Smith   */
2604f6d58c54SBarry Smith   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2605f6d58c54SBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2606f6d58c54SBarry Smith     coloring->currentcolor = k;
2607f6d58c54SBarry Smith     for (i=0; i<bs; i++) {
2608f6d58c54SBarry Smith       ierr = VecCopy(x1_tmp,w3);CHKERRQ(ierr);
2609f6d58c54SBarry Smith       ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);
2610f6d58c54SBarry Smith       if (ctype == IS_COLORING_GLOBAL) w3_array = w3_array - start;
2611f6d58c54SBarry Smith       /*
2612f6d58c54SBarry Smith 	Loop over each column associated with color
2613f6d58c54SBarry Smith 	adding the perturbation to the vector w3.
2614f6d58c54SBarry Smith       */
2615f6d58c54SBarry Smith       for (l=0; l<coloring->ncolumns[k]; l++) {
2616f6d58c54SBarry Smith 	col = i + bs*coloring->columns[k][l];    /* local column of the matrix we are probing for */
2617f6d58c54SBarry Smith 	if (coloring->htype[0] == 'w') {
2618f6d58c54SBarry Smith 	  dx = 1.0 + unorm;
2619f6d58c54SBarry Smith 	} else {
2620f6d58c54SBarry Smith 	  dx  = xx[col];
2621f6d58c54SBarry Smith 	}
2622f6d58c54SBarry Smith 	if (dx == 0.0) dx = 1.0;
2623f6d58c54SBarry Smith #if !defined(PETSC_USE_COMPLEX)
2624f6d58c54SBarry Smith 	if (dx < umin && dx >= 0.0)      dx = umin;
2625f6d58c54SBarry Smith 	else if (dx < 0.0 && dx > -umin) dx = -umin;
2626f6d58c54SBarry Smith #else
2627f6d58c54SBarry Smith 	if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2628f6d58c54SBarry Smith 	else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2629f6d58c54SBarry Smith #endif
2630f6d58c54SBarry Smith 	dx            *= epsilon;
2631e32f2f54SBarry Smith 	if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2632f6d58c54SBarry Smith 	w3_array[col] += dx;
2633f6d58c54SBarry Smith       }
2634f6d58c54SBarry Smith       if (ctype == IS_COLORING_GLOBAL) w3_array = w3_array + start;
2635f6d58c54SBarry Smith       ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2636f6d58c54SBarry Smith 
2637f6d58c54SBarry Smith       /*
2638f6d58c54SBarry Smith 	Evaluate function at w3 = x1 + dx (here dx is a vector of perturbations)
2639f6d58c54SBarry Smith 	w2 = F(x1 + dx) - F(x1)
2640f6d58c54SBarry Smith       */
2641f6d58c54SBarry Smith       ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2642f6d58c54SBarry Smith       ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
2643f6d58c54SBarry Smith       ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2644f6d58c54SBarry Smith       ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2645f6d58c54SBarry Smith 
2646f6d58c54SBarry Smith       /*
2647f6d58c54SBarry Smith 	Loop over rows of vector, putting results into Jacobian matrix
2648f6d58c54SBarry Smith       */
2649f6d58c54SBarry Smith       ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2650f6d58c54SBarry Smith       for (l=0; l<coloring->nrows[k]; l++) {
2651f6d58c54SBarry Smith 	row    = bs*coloring->rows[k][l];             /* local row index */
2652f6d58c54SBarry Smith 	col    = i + bs*coloring->columnsforrow[k][l];    /* global column index */
2653f6d58c54SBarry Smith         for (j=0; j<bs; j++) {
2654f6d58c54SBarry Smith   	  y[row+j] *= vscale_array[j+bs*vscaleforrow[k][l]];
2655f6d58c54SBarry Smith           srows[j]  = row + start + j;
2656f6d58c54SBarry Smith         }
2657f6d58c54SBarry Smith 	ierr   = MatSetValues(J,bs,srows,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2658f6d58c54SBarry Smith       }
2659f6d58c54SBarry Smith       ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2660f6d58c54SBarry Smith     }
2661f6d58c54SBarry Smith   } /* endof for each color */
2662f6d58c54SBarry Smith   if (ctype == IS_COLORING_GLOBAL) xx = xx + start;
2663f6d58c54SBarry Smith   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2664f6d58c54SBarry Smith   ierr = VecRestoreArray(x1_tmp,&xx);CHKERRQ(ierr);
2665f6d58c54SBarry Smith   ierr = PetscFree(srows);CHKERRQ(ierr);
2666f6d58c54SBarry Smith 
2667f6d58c54SBarry Smith   coloring->currentcolor = -1;
2668f6d58c54SBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2669f6d58c54SBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2670f6d58c54SBarry Smith   ierr = PetscLogEventEnd(MAT_FDColoringApply,coloring,J,x1,0);CHKERRQ(ierr);
2671f6d58c54SBarry Smith   PetscFunctionReturn(0);
2672f6d58c54SBarry Smith }
267399cafbc1SBarry Smith 
26742593348eSBarry Smith /* -------------------------------------------------------------------*/
2675cc2dc46cSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqBAIJ,
2676cc2dc46cSBarry Smith        MatGetRow_SeqBAIJ,
2677cc2dc46cSBarry Smith        MatRestoreRow_SeqBAIJ,
2678cc2dc46cSBarry Smith        MatMult_SeqBAIJ_N,
267997304618SKris Buschelman /* 4*/ MatMultAdd_SeqBAIJ_N,
26807c922b88SBarry Smith        MatMultTranspose_SeqBAIJ,
26817c922b88SBarry Smith        MatMultTransposeAdd_SeqBAIJ,
2682db4efbfdSBarry Smith        0,
2683cc2dc46cSBarry Smith        0,
2684cc2dc46cSBarry Smith        0,
268597304618SKris Buschelman /*10*/ 0,
2686cc2dc46cSBarry Smith        MatLUFactor_SeqBAIJ,
2687cc2dc46cSBarry Smith        0,
2688b6490206SBarry Smith        0,
2689f2501298SSatish Balay        MatTranspose_SeqBAIJ,
269097304618SKris Buschelman /*15*/ MatGetInfo_SeqBAIJ,
2691cc2dc46cSBarry Smith        MatEqual_SeqBAIJ,
2692cc2dc46cSBarry Smith        MatGetDiagonal_SeqBAIJ,
2693cc2dc46cSBarry Smith        MatDiagonalScale_SeqBAIJ,
2694cc2dc46cSBarry Smith        MatNorm_SeqBAIJ,
269597304618SKris Buschelman /*20*/ 0,
2696cc2dc46cSBarry Smith        MatAssemblyEnd_SeqBAIJ,
2697cc2dc46cSBarry Smith        MatSetOption_SeqBAIJ,
2698cc2dc46cSBarry Smith        MatZeroEntries_SeqBAIJ,
2699d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqBAIJ,
2700db4efbfdSBarry Smith        0,
2701db4efbfdSBarry Smith        0,
2702db4efbfdSBarry Smith        0,
2703db4efbfdSBarry Smith        0,
2704d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqBAIJ,
2705db4efbfdSBarry Smith        0,
2706db4efbfdSBarry Smith        0,
2707f2a5309cSSatish Balay        MatGetArray_SeqBAIJ,
2708f2a5309cSSatish Balay        MatRestoreArray_SeqBAIJ,
2709d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqBAIJ,
2710cc2dc46cSBarry Smith        0,
2711cc2dc46cSBarry Smith        0,
2712cc2dc46cSBarry Smith        MatILUFactor_SeqBAIJ,
2713cc2dc46cSBarry Smith        0,
2714d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqBAIJ,
2715cc2dc46cSBarry Smith        MatGetSubMatrices_SeqBAIJ,
2716cc2dc46cSBarry Smith        MatIncreaseOverlap_SeqBAIJ,
2717cc2dc46cSBarry Smith        MatGetValues_SeqBAIJ,
27183c896bc6SHong Zhang        MatCopy_SeqBAIJ,
2719d519adbfSMatthew Knepley /*44*/ 0,
2720cc2dc46cSBarry Smith        MatScale_SeqBAIJ,
2721cc2dc46cSBarry Smith        0,
2722cc2dc46cSBarry Smith        0,
2723fe97e370SBarry Smith        0,
272441c166b1SJed Brown /*49*/ MatSetBlockSize_SeqBAIJ,
27253b2fbd54SBarry Smith        MatGetRowIJ_SeqBAIJ,
272692c4ed94SBarry Smith        MatRestoreRowIJ_SeqBAIJ,
27273acb8795SBarry Smith        MatGetColumnIJ_SeqBAIJ,
27283acb8795SBarry Smith        MatRestoreColumnIJ_SeqBAIJ,
27293acb8795SBarry Smith /*54*/ MatFDColoringCreate_SeqAIJ,
2730cc2dc46cSBarry Smith        0,
2731cc2dc46cSBarry Smith        0,
2732cc2dc46cSBarry Smith        0,
2733d3825aa8SBarry Smith        MatSetValuesBlocked_SeqBAIJ,
2734d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_SeqBAIJ,
2735b9b97703SBarry Smith        MatDestroy_SeqBAIJ,
2736b9b97703SBarry Smith        MatView_SeqBAIJ,
2737357abbc8SBarry Smith        0,
2738273d9f13SBarry Smith        0,
2739d519adbfSMatthew Knepley /*64*/ 0,
2740273d9f13SBarry Smith        0,
2741273d9f13SBarry Smith        0,
2742273d9f13SBarry Smith        0,
2743273d9f13SBarry Smith        0,
2744d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqBAIJ,
2745273d9f13SBarry Smith        0,
2746c87e5d42SMatthew Knepley        MatConvert_Basic,
274797304618SKris Buschelman        0,
274897304618SKris Buschelman        0,
2749d519adbfSMatthew Knepley /*74*/ 0,
2750f6d58c54SBarry Smith        MatFDColoringApply_BAIJ,
275197304618SKris Buschelman        0,
275297304618SKris Buschelman        0,
275397304618SKris Buschelman        0,
2754d519adbfSMatthew Knepley /*79*/ 0,
275597304618SKris Buschelman        0,
275697304618SKris Buschelman        0,
275797304618SKris Buschelman        0,
2758865e5f61SKris Buschelman        MatLoad_SeqBAIJ,
2759d519adbfSMatthew Knepley /*84*/ 0,
2760b01c7715SBarry Smith        0,
2761b01c7715SBarry Smith        0,
2762b01c7715SBarry Smith        0,
2763865e5f61SKris Buschelman        0,
2764d519adbfSMatthew Knepley /*89*/ 0,
2765865e5f61SKris Buschelman        0,
2766865e5f61SKris Buschelman        0,
2767865e5f61SKris Buschelman        0,
2768865e5f61SKris Buschelman        0,
2769d519adbfSMatthew Knepley /*94*/ 0,
2770865e5f61SKris Buschelman        0,
2771865e5f61SKris Buschelman        0,
277299cafbc1SBarry Smith        0,
277399cafbc1SBarry Smith        0,
2774d519adbfSMatthew Knepley /*99*/0,
277599cafbc1SBarry Smith        0,
277699cafbc1SBarry Smith        0,
277799cafbc1SBarry Smith        0,
277899cafbc1SBarry Smith        0,
2779d519adbfSMatthew Knepley /*104*/0,
278099cafbc1SBarry Smith        MatRealPart_SeqBAIJ,
27812af78befSBarry Smith        MatImaginaryPart_SeqBAIJ,
27822af78befSBarry Smith        0,
27832af78befSBarry Smith        0,
2784d519adbfSMatthew Knepley /*109*/0,
27852af78befSBarry Smith        0,
27862af78befSBarry Smith        0,
27872af78befSBarry Smith        0,
2788547795f9SHong Zhang        MatMissingDiagonal_SeqBAIJ,
2789547795f9SHong Zhang /*114*/0,
2790547795f9SHong Zhang        0,
2791547795f9SHong Zhang        0,
2792547795f9SHong Zhang        0,
2793547795f9SHong Zhang        0,
2794547795f9SHong Zhang /*119*/0,
2795547795f9SHong Zhang        0,
2796547795f9SHong Zhang        MatMultHermitianTranspose_SeqBAIJ,
2797f501eaabSShri Abhyankar        MatMultHermitianTransposeAdd_SeqBAIJ,
2798f501eaabSShri Abhyankar        MatLoadnew_SeqBAIJ
279999cafbc1SBarry Smith };
28002593348eSBarry Smith 
28013e90b805SBarry Smith EXTERN_C_BEGIN
28024a2ae208SSatish Balay #undef __FUNCT__
28034a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqBAIJ"
2804be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqBAIJ(Mat mat)
28053e90b805SBarry Smith {
28063e90b805SBarry Smith   Mat_SeqBAIJ    *aij = (Mat_SeqBAIJ *)mat->data;
2807d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
2808dfbe8321SBarry Smith   PetscErrorCode ierr;
28093e90b805SBarry Smith 
28103e90b805SBarry Smith   PetscFunctionBegin;
2811e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
28123e90b805SBarry Smith 
28133e90b805SBarry Smith   /* allocate space for values if not already there */
28143e90b805SBarry Smith   if (!aij->saved_values) {
281587828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
28161784c0f5SBarry Smith     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
28173e90b805SBarry Smith   }
28183e90b805SBarry Smith 
28193e90b805SBarry Smith   /* copy values over */
282087828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
28213e90b805SBarry Smith   PetscFunctionReturn(0);
28223e90b805SBarry Smith }
28233e90b805SBarry Smith EXTERN_C_END
28243e90b805SBarry Smith 
28253e90b805SBarry Smith EXTERN_C_BEGIN
28264a2ae208SSatish Balay #undef __FUNCT__
28274a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqBAIJ"
2828be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqBAIJ(Mat mat)
28293e90b805SBarry Smith {
28303e90b805SBarry Smith   Mat_SeqBAIJ    *aij = (Mat_SeqBAIJ *)mat->data;
28316849ba73SBarry Smith   PetscErrorCode ierr;
2832d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
28333e90b805SBarry Smith 
28343e90b805SBarry Smith   PetscFunctionBegin;
2835e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2836e7e72b3dSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
28373e90b805SBarry Smith 
28383e90b805SBarry Smith   /* copy values over */
283987828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
28403e90b805SBarry Smith   PetscFunctionReturn(0);
28413e90b805SBarry Smith }
28423e90b805SBarry Smith EXTERN_C_END
28433e90b805SBarry Smith 
2844273d9f13SBarry Smith EXTERN_C_BEGIN
2845f69a0ea3SMatthew Knepley extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
2846f69a0ea3SMatthew Knepley extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqBAIJ_SeqSBAIJ(Mat, MatType,MatReuse,Mat*);
2847273d9f13SBarry Smith EXTERN_C_END
2848273d9f13SBarry Smith 
2849273d9f13SBarry Smith EXTERN_C_BEGIN
28504a2ae208SSatish Balay #undef __FUNCT__
2851a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqBAIJSetPreallocation_SeqBAIJ"
2852be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetPreallocation_SeqBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
2853a23d5eceSKris Buschelman {
2854a23d5eceSKris Buschelman   Mat_SeqBAIJ    *b;
28556849ba73SBarry Smith   PetscErrorCode ierr;
2856db4efbfdSBarry Smith   PetscInt       i,mbs,nbs,bs2,newbs = PetscAbs(bs);
2857ab93d7beSBarry Smith   PetscTruth     flg,skipallocation = PETSC_FALSE;
2858a23d5eceSKris Buschelman 
2859a23d5eceSKris Buschelman   PetscFunctionBegin;
2860a23d5eceSKris Buschelman 
2861ab93d7beSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2862ab93d7beSBarry Smith     skipallocation = PETSC_TRUE;
2863ab93d7beSBarry Smith     nz             = 0;
2864ab93d7beSBarry Smith   }
28658c07d4e3SBarry Smith 
2866db4efbfdSBarry Smith   if (bs < 0) {
28677adad957SLisandro Dalcin     ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Block options for SEQBAIJ matrix 1","Mat");CHKERRQ(ierr);
2868db4efbfdSBarry Smith       ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatSeqBAIJSetPreallocation",newbs,&newbs,PETSC_NULL);CHKERRQ(ierr);
28698c07d4e3SBarry Smith     ierr = PetscOptionsEnd();CHKERRQ(ierr);
2870db4efbfdSBarry Smith     bs   = PetscAbs(bs);
2871db4efbfdSBarry Smith   }
2872e7e72b3dSBarry Smith   if (nnz && newbs != bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot change blocksize from command line if setting nnz");
2873a23d5eceSKris Buschelman   bs   = newbs;
2874a23d5eceSKris Buschelman 
287526283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
287626283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
287726283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
287826283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2879899cda47SBarry Smith 
2880899cda47SBarry Smith   B->preallocated = PETSC_TRUE;
2881899cda47SBarry Smith 
2882d0f46423SBarry Smith   mbs  = B->rmap->n/bs;
2883d0f46423SBarry Smith   nbs  = B->cmap->n/bs;
2884a23d5eceSKris Buschelman   bs2  = bs*bs;
2885a23d5eceSKris Buschelman 
288665e19b50SBarry 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);
2887a23d5eceSKris Buschelman 
2888a23d5eceSKris Buschelman   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2889e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
2890a23d5eceSKris Buschelman   if (nnz) {
2891a23d5eceSKris Buschelman     for (i=0; i<mbs; i++) {
2892e32f2f54SBarry 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]);
2893e32f2f54SBarry 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);
2894a23d5eceSKris Buschelman     }
2895a23d5eceSKris Buschelman   }
2896a23d5eceSKris Buschelman 
2897a23d5eceSKris Buschelman   b       = (Mat_SeqBAIJ*)B->data;
28987adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)B)->comm,PETSC_NULL,"Optimize options for SEQBAIJ matrix 2 ","Mat");CHKERRQ(ierr);
28998c07d4e3SBarry Smith     ierr = PetscOptionsTruth("-mat_no_unroll","Do not optimize for block size (slow)",PETSC_NULL,PETSC_FALSE,&flg,PETSC_NULL);CHKERRQ(ierr);
29008c07d4e3SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
29018c07d4e3SBarry Smith 
2902a23d5eceSKris Buschelman   if (!flg) {
2903a23d5eceSKris Buschelman     switch (bs) {
2904a23d5eceSKris Buschelman     case 1:
2905a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_1;
2906a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_1;
290741f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_1;
2908a23d5eceSKris Buschelman       break;
2909a23d5eceSKris Buschelman     case 2:
2910a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_2;
2911a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_2;
291241f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_2;
2913a23d5eceSKris Buschelman       break;
2914a23d5eceSKris Buschelman     case 3:
2915a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_3;
2916a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_3;
291741f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_3;
2918a23d5eceSKris Buschelman       break;
2919a23d5eceSKris Buschelman     case 4:
2920a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_4;
2921a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_4;
292241f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_4;
2923a23d5eceSKris Buschelman       break;
2924a23d5eceSKris Buschelman     case 5:
2925a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_5;
2926a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_5;
292741f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_5;
2928a23d5eceSKris Buschelman       break;
2929a23d5eceSKris Buschelman     case 6:
2930a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_6;
2931a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_6;
293241f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_6;
2933a23d5eceSKris Buschelman       break;
2934a23d5eceSKris Buschelman     case 7:
2935a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_7;
2936a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_7;
293741f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_7;
2938a23d5eceSKris Buschelman       break;
29398ab949d8SShri Abhyankar     case 15:
2940832cc040SShri Abhyankar       B->ops->mult = MatMult_SeqBAIJ_15_ver1;
29418ab949d8SShri Abhyankar       break;
2942a23d5eceSKris Buschelman     default:
2943a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_N;
2944a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_N;
2945a23d5eceSKris Buschelman       break;
2946a23d5eceSKris Buschelman     }
2947a23d5eceSKris Buschelman   }
2948d0f46423SBarry Smith   B->rmap->bs      = bs;
2949a23d5eceSKris Buschelman   b->mbs     = mbs;
2950a23d5eceSKris Buschelman   b->nbs     = nbs;
2951ab93d7beSBarry Smith   if (!skipallocation) {
29522ee49352SLisandro Dalcin     if (!b->imax) {
2953ab93d7beSBarry Smith       ierr = PetscMalloc2(mbs,PetscInt,&b->imax,mbs,PetscInt,&b->ilen);CHKERRQ(ierr);
29544fd072dbSBarry Smith       ierr = PetscLogObjectMemory(B,2*mbs*sizeof(PetscInt));
29554fd072dbSBarry Smith       b->free_imax_ilen = PETSC_TRUE;
29562ee49352SLisandro Dalcin     }
2957ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each block row so far. */
2958ab93d7beSBarry Smith     for (i=0; i<mbs; i++) { b->ilen[i] = 0;}
2959a23d5eceSKris Buschelman     if (!nnz) {
2960a23d5eceSKris Buschelman       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2961a23d5eceSKris Buschelman       else if (nz <= 0)        nz = 1;
2962a23d5eceSKris Buschelman       for (i=0; i<mbs; i++) b->imax[i] = nz;
2963a23d5eceSKris Buschelman       nz = nz*mbs;
2964a23d5eceSKris Buschelman     } else {
2965a23d5eceSKris Buschelman       nz = 0;
2966a23d5eceSKris Buschelman       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2967a23d5eceSKris Buschelman     }
2968a23d5eceSKris Buschelman 
2969a23d5eceSKris Buschelman     /* allocate the matrix space */
29702ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
2971d0f46423SBarry Smith     ierr = PetscMalloc3(bs2*nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->N+1,PetscInt,&b->i);CHKERRQ(ierr);
2972d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
2973a23d5eceSKris Buschelman     ierr  = PetscMemzero(b->a,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
2974c1ac3661SBarry Smith     ierr  = PetscMemzero(b->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
2975a23d5eceSKris Buschelman     b->singlemalloc = PETSC_TRUE;
2976a23d5eceSKris Buschelman     b->i[0] = 0;
2977a23d5eceSKris Buschelman     for (i=1; i<mbs+1; i++) {
2978a23d5eceSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
2979a23d5eceSKris Buschelman     }
2980e6b907acSBarry Smith     b->free_a     = PETSC_TRUE;
2981e6b907acSBarry Smith     b->free_ij    = PETSC_TRUE;
2982e811da20SHong Zhang   } else {
2983e6b907acSBarry Smith     b->free_a     = PETSC_FALSE;
2984e6b907acSBarry Smith     b->free_ij    = PETSC_FALSE;
2985ab93d7beSBarry Smith   }
2986a23d5eceSKris Buschelman 
2987d0f46423SBarry Smith   B->rmap->bs          = bs;
2988a23d5eceSKris Buschelman   b->bs2              = bs2;
2989a23d5eceSKris Buschelman   b->mbs              = mbs;
2990a23d5eceSKris Buschelman   b->nz               = 0;
2991a23d5eceSKris Buschelman   b->maxnz            = nz*bs2;
2992a23d5eceSKris Buschelman   B->info.nz_unneeded = (PetscReal)b->maxnz;
2993a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2994a23d5eceSKris Buschelman }
2995a23d5eceSKris Buschelman EXTERN_C_END
2996a23d5eceSKris Buschelman 
2997b24902e0SBarry Smith EXTERN_C_BEGIN
2998725b52f3SLisandro Dalcin #undef __FUNCT__
2999725b52f3SLisandro Dalcin #define __FUNCT__ "MatSeqBAIJSetPreallocationCSR_SeqBAIJ"
3000cf12db73SBarry Smith PetscErrorCode MatSeqBAIJSetPreallocationCSR_SeqBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
3001725b52f3SLisandro Dalcin {
3002725b52f3SLisandro Dalcin   PetscInt       i,m,nz,nz_max=0,*nnz;
3003725b52f3SLisandro Dalcin   PetscScalar    *values=0;
3004725b52f3SLisandro Dalcin   PetscErrorCode ierr;
3005725b52f3SLisandro Dalcin 
3006725b52f3SLisandro Dalcin   PetscFunctionBegin;
3007e32f2f54SBarry Smith   if (bs < 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
300826283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
300926283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
301026283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
301126283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3012d0f46423SBarry Smith   m = B->rmap->n/bs;
3013725b52f3SLisandro Dalcin 
3014e32f2f54SBarry Smith   if (ii[0] != 0) { SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "ii[0] must be 0 but it is %D",ii[0]); }
3015725b52f3SLisandro Dalcin   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3016725b52f3SLisandro Dalcin   for(i=0; i<m; i++) {
3017cf12db73SBarry Smith     nz = ii[i+1]- ii[i];
3018e32f2f54SBarry Smith     if (nz < 0) { SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D",i,nz); }
3019725b52f3SLisandro Dalcin     nz_max = PetscMax(nz_max, nz);
3020725b52f3SLisandro Dalcin     nnz[i] = nz;
3021725b52f3SLisandro Dalcin   }
3022725b52f3SLisandro Dalcin   ierr = MatSeqBAIJSetPreallocation(B,bs,0,nnz);CHKERRQ(ierr);
3023725b52f3SLisandro Dalcin   ierr = PetscFree(nnz);CHKERRQ(ierr);
3024725b52f3SLisandro Dalcin 
3025725b52f3SLisandro Dalcin   values = (PetscScalar*)V;
3026725b52f3SLisandro Dalcin   if (!values) {
3027725b52f3SLisandro Dalcin     ierr = PetscMalloc(bs*bs*(nz_max+1)*sizeof(PetscScalar),&values);CHKERRQ(ierr);
3028725b52f3SLisandro Dalcin     ierr = PetscMemzero(values,bs*bs*nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3029725b52f3SLisandro Dalcin   }
3030725b52f3SLisandro Dalcin   for (i=0; i<m; i++) {
3031cf12db73SBarry Smith     PetscInt          ncols  = ii[i+1] - ii[i];
3032cf12db73SBarry Smith     const PetscInt    *icols = jj + ii[i];
3033cf12db73SBarry Smith     const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
3034725b52f3SLisandro Dalcin     ierr = MatSetValuesBlocked_SeqBAIJ(B,1,&i,ncols,icols,svals,INSERT_VALUES);CHKERRQ(ierr);
3035725b52f3SLisandro Dalcin   }
3036725b52f3SLisandro Dalcin   if (!V) { ierr = PetscFree(values);CHKERRQ(ierr); }
3037725b52f3SLisandro Dalcin   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3038725b52f3SLisandro Dalcin   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3039725b52f3SLisandro Dalcin 
3040725b52f3SLisandro Dalcin   PetscFunctionReturn(0);
3041725b52f3SLisandro Dalcin }
3042725b52f3SLisandro Dalcin EXTERN_C_END
3043725b52f3SLisandro Dalcin 
3044725b52f3SLisandro Dalcin 
3045207126cbSBarry Smith EXTERN_C_BEGIN
3046b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqbaij_petsc(Mat,MatFactorType,Mat*);
304767877ebaSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
3048bccb9932SShri Abhyankar extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_baij_mumps(Mat,MatFactorType,Mat*);
304967877ebaSShri Abhyankar #endif
3050db4efbfdSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqbaij_petsc(Mat,MatFactorType,Mat*);
3051b24902e0SBarry Smith EXTERN_C_END
3052b24902e0SBarry Smith 
30530bad9183SKris Buschelman /*MC
3054fafad747SKris Buschelman    MATSEQBAIJ - MATSEQBAIJ = "seqbaij" - A matrix type to be used for sequential block sparse matrices, based on
30550bad9183SKris Buschelman    block sparse compressed row format.
30560bad9183SKris Buschelman 
30570bad9183SKris Buschelman    Options Database Keys:
30580bad9183SKris Buschelman . -mat_type seqbaij - sets the matrix type to "seqbaij" during a call to MatSetFromOptions()
30590bad9183SKris Buschelman 
30600bad9183SKris Buschelman   Level: beginner
30610bad9183SKris Buschelman 
3062f0c06035SSatish Balay .seealso: MatCreateSeqBAIJ()
30630bad9183SKris Buschelman M*/
30640bad9183SKris Buschelman 
3065b24902e0SBarry Smith 
3066a23d5eceSKris Buschelman EXTERN_C_BEGIN
3067a23d5eceSKris Buschelman #undef __FUNCT__
30684a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqBAIJ"
3069be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqBAIJ(Mat B)
30702593348eSBarry Smith {
3071dfbe8321SBarry Smith   PetscErrorCode ierr;
3072c1ac3661SBarry Smith   PetscMPIInt    size;
3073b6490206SBarry Smith   Mat_SeqBAIJ    *b;
30743b2fbd54SBarry Smith 
30753a40ed3dSBarry Smith   PetscFunctionBegin;
30767adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3077e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");
3078b6490206SBarry Smith 
307938f2d2fdSLisandro Dalcin   ierr    = PetscNewLog(B,Mat_SeqBAIJ,&b);CHKERRQ(ierr);
3080b0a32e0cSBarry Smith   B->data = (void*)b;
3081549d3d68SSatish Balay   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
308290f02eecSBarry Smith   B->mapping               = 0;
30832593348eSBarry Smith   b->row                   = 0;
30842593348eSBarry Smith   b->col                   = 0;
3085e51c0b9cSSatish Balay   b->icol                  = 0;
30862593348eSBarry Smith   b->reallocs              = 0;
30873e90b805SBarry Smith   b->saved_values          = 0;
30882593348eSBarry Smith 
3089c4992f7dSBarry Smith   b->roworiented           = PETSC_TRUE;
30902593348eSBarry Smith   b->nonew                 = 0;
30912593348eSBarry Smith   b->diag                  = 0;
30922593348eSBarry Smith   b->solve_work            = 0;
3093de6a44a3SBarry Smith   b->mult_work             = 0;
30942a1b7f2aSHong Zhang   B->spptr                 = 0;
30950e6d2581SBarry Smith   B->info.nz_unneeded      = (PetscReal)b->maxnz;
3096a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3097c4319e64SHong Zhang   b->xtoy                  = 0;
3098c4319e64SHong Zhang   b->XtoY                  = 0;
309973e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
310026e093fcSHong Zhang   b->compressedrow.nrows   = 0;
310173e7a558SHong Zhang   b->compressedrow.i       = PETSC_NULL;
310273e7a558SHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
310373e7a558SHong Zhang   b->compressedrow.checked = PETSC_FALSE;
310488e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
31054e220ebcSLois Curfman McInnes 
3106ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
3107db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqbaij_petsc",
3108db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqbaij_petsc);CHKERRQ(ierr);
3109ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
3110b24902e0SBarry Smith                                      "MatGetFactor_seqbaij_petsc",
3111b24902e0SBarry Smith                                      MatGetFactor_seqbaij_petsc);CHKERRQ(ierr);
311267877ebaSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
3113bccb9932SShri Abhyankar   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", "MatGetFactor_baij_mumps", MatGetFactor_baij_mumps);CHKERRQ(ierr);
311467877ebaSShri Abhyankar #endif
311543516a2dSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqBAIJInvertBlockDiagonal_C",
311643516a2dSKris Buschelman                                      "MatInvertBlockDiagonal_SeqBAIJ",
311743516a2dSKris Buschelman                                       MatInvertBlockDiagonal_SeqBAIJ);CHKERRQ(ierr);
3118f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
31193e90b805SBarry Smith                                      "MatStoreValues_SeqBAIJ",
3120bc4b532fSSatish Balay                                       MatStoreValues_SeqBAIJ);CHKERRQ(ierr);
3121f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
31223e90b805SBarry Smith                                      "MatRetrieveValues_SeqBAIJ",
3123bc4b532fSSatish Balay                                       MatRetrieveValues_SeqBAIJ);CHKERRQ(ierr);
3124f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqBAIJSetColumnIndices_C",
312527a8da17SBarry Smith                                      "MatSeqBAIJSetColumnIndices_SeqBAIJ",
3126bc4b532fSSatish Balay                                       MatSeqBAIJSetColumnIndices_SeqBAIJ);CHKERRQ(ierr);
3127a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqbaij_seqaij_C",
3128273d9f13SBarry Smith                                      "MatConvert_SeqBAIJ_SeqAIJ",
3129273d9f13SBarry Smith                                       MatConvert_SeqBAIJ_SeqAIJ);CHKERRQ(ierr);
3130a0e1a404SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqbaij_seqsbaij_C",
3131a0e1a404SHong Zhang                                      "MatConvert_SeqBAIJ_SeqSBAIJ",
3132a0e1a404SHong Zhang                                       MatConvert_SeqBAIJ_SeqSBAIJ);CHKERRQ(ierr);
3133a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqBAIJSetPreallocation_C",
3134a23d5eceSKris Buschelman                                      "MatSeqBAIJSetPreallocation_SeqBAIJ",
3135a23d5eceSKris Buschelman                                       MatSeqBAIJSetPreallocation_SeqBAIJ);CHKERRQ(ierr);
3136725b52f3SLisandro Dalcin   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqBAIJSetPreallocationCSR_C",
3137725b52f3SLisandro Dalcin                                      "MatSeqBAIJSetPreallocationCSR_SeqBAIJ",
3138725b52f3SLisandro Dalcin                                       MatSeqBAIJSetPreallocationCSR_SeqBAIJ);CHKERRQ(ierr);
313917667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQBAIJ);CHKERRQ(ierr);
31403a40ed3dSBarry Smith   PetscFunctionReturn(0);
31412593348eSBarry Smith }
3142273d9f13SBarry Smith EXTERN_C_END
31432593348eSBarry Smith 
31444a2ae208SSatish Balay #undef __FUNCT__
3145b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqBAIJ"
314616a2bf60SHong Zhang PetscErrorCode MatDuplicateNoCreate_SeqBAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscTruth mallocmatspace)
31472593348eSBarry Smith {
3148b24902e0SBarry Smith   Mat_SeqBAIJ    *c = (Mat_SeqBAIJ*)C->data,*a = (Mat_SeqBAIJ*)A->data;
31496849ba73SBarry Smith   PetscErrorCode ierr;
3150a96a251dSBarry Smith   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 = a->bs2;
3151de6a44a3SBarry Smith 
31523a40ed3dSBarry Smith   PetscFunctionBegin;
3153e32f2f54SBarry Smith   if (a->i[mbs] != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt matrix");
31542593348eSBarry Smith 
31554fd072dbSBarry Smith   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
31564fd072dbSBarry Smith     c->imax = a->imax;
31574fd072dbSBarry Smith     c->ilen = a->ilen;
31584fd072dbSBarry Smith     c->free_imax_ilen = PETSC_FALSE;
31594fd072dbSBarry Smith   } else {
316033b91e9fSSatish Balay     ierr = PetscMalloc2(mbs,PetscInt,&c->imax,mbs,PetscInt,&c->ilen);CHKERRQ(ierr);
31614fd072dbSBarry Smith     ierr = PetscLogObjectMemory(C,2*mbs*sizeof(PetscInt));CHKERRQ(ierr);
3162b6490206SBarry Smith     for (i=0; i<mbs; i++) {
31632593348eSBarry Smith       c->imax[i] = a->imax[i];
31642593348eSBarry Smith       c->ilen[i] = a->ilen[i];
31652593348eSBarry Smith     }
31664fd072dbSBarry Smith     c->free_imax_ilen = PETSC_TRUE;
31674fd072dbSBarry Smith   }
31682593348eSBarry Smith 
31692593348eSBarry Smith   /* allocate the matrix space */
317016a2bf60SHong Zhang   if (mallocmatspace){
31714fd072dbSBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
31724fd072dbSBarry Smith       ierr = PetscMalloc(bs2*nz*sizeof(PetscScalar),&c->a);CHKERRQ(ierr);
31734fd072dbSBarry Smith       ierr = PetscLogObjectMemory(C,a->i[mbs]*bs2*sizeof(PetscScalar));CHKERRQ(ierr);
31744fd072dbSBarry Smith       c->singlemalloc = PETSC_FALSE;
31754fd072dbSBarry Smith       c->free_ij      = PETSC_FALSE;
31764fd072dbSBarry Smith       c->i            = a->i;
31774fd072dbSBarry Smith       c->j            = a->j;
31784fd072dbSBarry Smith       c->parent       = A;
31794fd072dbSBarry Smith       ierr            = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
31804fd072dbSBarry Smith       ierr            = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
31814fd072dbSBarry Smith       ierr            = MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
31824fd072dbSBarry Smith     } else {
3183a96a251dSBarry Smith       ierr = PetscMalloc3(bs2*nz,PetscScalar,&c->a,nz,PetscInt,&c->j,mbs+1,PetscInt,&c->i);CHKERRQ(ierr);
318416a2bf60SHong Zhang       ierr = PetscLogObjectMemory(C,a->i[mbs]*(bs2*sizeof(PetscScalar)+sizeof(PetscInt))+(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
3185c4992f7dSBarry Smith       c->singlemalloc = PETSC_TRUE;
31864fd072dbSBarry Smith       c->free_ij      = PETSC_TRUE;
3187c1ac3661SBarry Smith       ierr = PetscMemcpy(c->i,a->i,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
3188b6490206SBarry Smith       if (mbs > 0) {
3189c1ac3661SBarry Smith 	ierr = PetscMemcpy(c->j,a->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
31902e8a6d31SBarry Smith 	if (cpvalues == MAT_COPY_VALUES) {
3191549d3d68SSatish Balay 	  ierr = PetscMemcpy(c->a,a->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
31922e8a6d31SBarry Smith 	} else {
3193549d3d68SSatish Balay 	  ierr = PetscMemzero(c->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
31942593348eSBarry Smith 	}
31952593348eSBarry Smith       }
319616a2bf60SHong Zhang     }
31974fd072dbSBarry Smith   }
319816a2bf60SHong Zhang 
31992593348eSBarry Smith   c->roworiented = a->roworiented;
32002593348eSBarry Smith   c->nonew       = a->nonew;
320126283091SBarry Smith   ierr = PetscLayoutCopy(A->rmap,&C->rmap);CHKERRQ(ierr);
320226283091SBarry Smith   ierr = PetscLayoutCopy(A->cmap,&C->cmap);CHKERRQ(ierr);
32035c9eb25fSBarry Smith   c->bs2         = a->bs2;
32045c9eb25fSBarry Smith   c->mbs         = a->mbs;
32055c9eb25fSBarry Smith   c->nbs         = a->nbs;
32062593348eSBarry Smith 
32072593348eSBarry Smith   if (a->diag) {
32084fd072dbSBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
32094fd072dbSBarry Smith       c->diag      = a->diag;
32104fd072dbSBarry Smith       c->free_diag = PETSC_FALSE;
32114fd072dbSBarry Smith     } else {
3212c1ac3661SBarry Smith       ierr = PetscMalloc((mbs+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
321352e6d16bSBarry Smith       ierr = PetscLogObjectMemory(C,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
3214b6490206SBarry Smith       for (i=0; i<mbs; i++) {
32152593348eSBarry Smith         c->diag[i] = a->diag[i];
32162593348eSBarry Smith       }
32174fd072dbSBarry Smith       c->free_diag = PETSC_TRUE;
32184fd072dbSBarry Smith     }
321998305bb5SBarry Smith   } else c->diag        = 0;
32202593348eSBarry Smith   c->nz                 = a->nz;
32218e9a0fb8SHong Zhang   c->maxnz              = bs2*a->nz; /* Since we allocate exactly the right amount */
32222593348eSBarry Smith   c->solve_work         = 0;
32237fc0212eSBarry Smith   c->mult_work          = 0;
3224e6b907acSBarry Smith   c->free_a             = PETSC_TRUE;
3225e6b907acSBarry Smith   c->free_ij            = PETSC_TRUE;
3226273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3227273d9f13SBarry Smith   C->assembled          = PETSC_TRUE;
322888e51ccdSHong Zhang 
322988e51ccdSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
323088e51ccdSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
323188e51ccdSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
323288e51ccdSHong Zhang   if (a->compressedrow.checked && a->compressedrow.use){
323388e51ccdSHong Zhang     i = a->compressedrow.nrows;
32340e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i+1,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
32354fd072dbSBarry Smith     ierr = PetscLogObjectMemory(C,(2*i+1)*sizeof(PetscInt));CHKERRQ(ierr);
323688e51ccdSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
323788e51ccdSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
323888e51ccdSHong Zhang   } else {
323988e51ccdSHong Zhang     c->compressedrow.use    = PETSC_FALSE;
324088e51ccdSHong Zhang     c->compressedrow.i      = PETSC_NULL;
324188e51ccdSHong Zhang     c->compressedrow.rindex = PETSC_NULL;
324288e51ccdSHong Zhang   }
324388e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
32447adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
32455d5aaa0eSBarry Smith   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
32463a40ed3dSBarry Smith   PetscFunctionReturn(0);
32472593348eSBarry Smith }
32482593348eSBarry Smith 
32494a2ae208SSatish Balay #undef __FUNCT__
3250b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqBAIJ"
3251b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3252b24902e0SBarry Smith {
3253b24902e0SBarry Smith     PetscErrorCode ierr;
3254b24902e0SBarry Smith 
3255b24902e0SBarry Smith   PetscFunctionBegin;
32565c9eb25fSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
3257d0f46423SBarry Smith   ierr = MatSetSizes(*B,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);CHKERRQ(ierr);
32585c9eb25fSBarry Smith   ierr = MatSetType(*B,MATSEQBAIJ);CHKERRQ(ierr);
325916a2bf60SHong Zhang   ierr = MatDuplicateNoCreate_SeqBAIJ(*B,A,cpvalues,PETSC_TRUE);
3260b24902e0SBarry Smith   PetscFunctionReturn(0);
3261b24902e0SBarry Smith }
3262b24902e0SBarry Smith 
3263b24902e0SBarry Smith #undef __FUNCT__
32644a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqBAIJ"
3265a313700dSBarry Smith PetscErrorCode MatLoad_SeqBAIJ(PetscViewer viewer, const MatType type,Mat *A)
32662593348eSBarry Smith {
3267b6490206SBarry Smith   Mat_SeqBAIJ    *a;
32682593348eSBarry Smith   Mat            B;
32696849ba73SBarry Smith   PetscErrorCode ierr;
3270b24ad042SBarry Smith   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs=1;
3271c1ac3661SBarry Smith   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*browlengths,maskcount;
3272c1ac3661SBarry Smith   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows;
3273c1ac3661SBarry Smith   PetscInt       *masked,nmask,tmp,bs2,ishift;
3274b24ad042SBarry Smith   PetscMPIInt    size;
3275b24ad042SBarry Smith   int            fd;
327687828ca2SBarry Smith   PetscScalar    *aa;
327719bcc07fSBarry Smith   MPI_Comm       comm = ((PetscObject)viewer)->comm;
32782593348eSBarry Smith 
32793a40ed3dSBarry Smith   PetscFunctionBegin;
32808c07d4e3SBarry Smith   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading SEQBAIJ matrix","Mat");CHKERRQ(ierr);
32818c07d4e3SBarry Smith     ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
32828c07d4e3SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3283de6a44a3SBarry Smith   bs2  = bs*bs;
3284b6490206SBarry Smith 
3285d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3286e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
3287b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
32880752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3289e32f2f54SBarry Smith   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
32902593348eSBarry Smith   M = header[1]; N = header[2]; nz = header[3];
32912593348eSBarry Smith 
3292e7e72b3dSBarry Smith   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqBAIJ");
3293e32f2f54SBarry Smith   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
329435aab85fSBarry Smith 
329535aab85fSBarry Smith   /*
329635aab85fSBarry Smith      This code adds extra rows to make sure the number of rows is
329735aab85fSBarry Smith     divisible by the blocksize
329835aab85fSBarry Smith   */
3299b6490206SBarry Smith   mbs        = M/bs;
330035aab85fSBarry Smith   extra_rows = bs - M + bs*(mbs);
330135aab85fSBarry Smith   if (extra_rows == bs) extra_rows = 0;
330235aab85fSBarry Smith   else                  mbs++;
330335aab85fSBarry Smith   if (extra_rows) {
33041e2582c4SBarry Smith     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
330535aab85fSBarry Smith   }
3306b6490206SBarry Smith 
33072593348eSBarry Smith   /* read in row lengths */
3308c1ac3661SBarry Smith   ierr = PetscMalloc((M+extra_rows)*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
33090752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
331035aab85fSBarry Smith   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
33112593348eSBarry Smith 
3312b6490206SBarry Smith   /* read in column indices */
3313c1ac3661SBarry Smith   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscInt),&jj);CHKERRQ(ierr);
33140752156aSBarry Smith   ierr = PetscBinaryRead(fd,jj,nz,PETSC_INT);CHKERRQ(ierr);
331535aab85fSBarry Smith   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;
3316b6490206SBarry Smith 
3317b6490206SBarry Smith   /* loop over row lengths determining block row lengths */
3318c1ac3661SBarry Smith   ierr     = PetscMalloc(mbs*sizeof(PetscInt),&browlengths);CHKERRQ(ierr);
3319c1ac3661SBarry Smith   ierr     = PetscMemzero(browlengths,mbs*sizeof(PetscInt));CHKERRQ(ierr);
3320fca92195SBarry Smith   ierr     = PetscMalloc2(mbs,PetscInt,&mask,mbs,PetscInt,&masked);CHKERRQ(ierr);
3321c1ac3661SBarry Smith   ierr     = PetscMemzero(mask,mbs*sizeof(PetscInt));CHKERRQ(ierr);
3322fca92195SBarry Smith   rowcount = 0;
3323fca92195SBarry Smith   nzcount = 0;
3324b6490206SBarry Smith   for (i=0; i<mbs; i++) {
332535aab85fSBarry Smith     nmask = 0;
3326b6490206SBarry Smith     for (j=0; j<bs; j++) {
3327b6490206SBarry Smith       kmax = rowlengths[rowcount];
3328b6490206SBarry Smith       for (k=0; k<kmax; k++) {
332935aab85fSBarry Smith         tmp = jj[nzcount++]/bs;
333035aab85fSBarry Smith         if (!mask[tmp]) {masked[nmask++] = tmp; mask[tmp] = 1;}
3331b6490206SBarry Smith       }
3332b6490206SBarry Smith       rowcount++;
3333b6490206SBarry Smith     }
333435aab85fSBarry Smith     browlengths[i] += nmask;
333535aab85fSBarry Smith     /* zero out the mask elements we set */
333635aab85fSBarry Smith     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
3337b6490206SBarry Smith   }
3338b6490206SBarry Smith 
33392593348eSBarry Smith   /* create our matrix */
3340f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);
3341f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);
334278ae41b4SKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3343ab93d7beSBarry Smith   ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(B,bs,0,browlengths);CHKERRQ(ierr);
3344b6490206SBarry Smith   a = (Mat_SeqBAIJ*)B->data;
33452593348eSBarry Smith 
33462593348eSBarry Smith   /* set matrix "i" values */
3347de6a44a3SBarry Smith   a->i[0] = 0;
3348b6490206SBarry Smith   for (i=1; i<= mbs; i++) {
3349b6490206SBarry Smith     a->i[i]      = a->i[i-1] + browlengths[i-1];
3350b6490206SBarry Smith     a->ilen[i-1] = browlengths[i-1];
33512593348eSBarry Smith   }
3352b6490206SBarry Smith   a->nz         = 0;
3353b6490206SBarry Smith   for (i=0; i<mbs; i++) a->nz += browlengths[i];
33542593348eSBarry Smith 
3355b6490206SBarry Smith   /* read in nonzero values */
335687828ca2SBarry Smith   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscScalar),&aa);CHKERRQ(ierr);
33570752156aSBarry Smith   ierr = PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);CHKERRQ(ierr);
335835aab85fSBarry Smith   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;
3359b6490206SBarry Smith 
3360b6490206SBarry Smith   /* set "a" and "j" values into matrix */
3361b6490206SBarry Smith   nzcount = 0; jcount = 0;
3362b6490206SBarry Smith   for (i=0; i<mbs; i++) {
3363b6490206SBarry Smith     nzcountb = nzcount;
336435aab85fSBarry Smith     nmask    = 0;
3365b6490206SBarry Smith     for (j=0; j<bs; j++) {
3366b6490206SBarry Smith       kmax = rowlengths[i*bs+j];
3367b6490206SBarry Smith       for (k=0; k<kmax; k++) {
336835aab85fSBarry Smith         tmp = jj[nzcount++]/bs;
336935aab85fSBarry Smith 	if (!mask[tmp]) { masked[nmask++] = tmp; mask[tmp] = 1;}
3370b6490206SBarry Smith       }
3371b6490206SBarry Smith     }
3372de6a44a3SBarry Smith     /* sort the masked values */
3373433994e6SBarry Smith     ierr = PetscSortInt(nmask,masked);CHKERRQ(ierr);
3374de6a44a3SBarry Smith 
3375b6490206SBarry Smith     /* set "j" values into matrix */
3376b6490206SBarry Smith     maskcount = 1;
337735aab85fSBarry Smith     for (j=0; j<nmask; j++) {
337835aab85fSBarry Smith       a->j[jcount++]  = masked[j];
3379de6a44a3SBarry Smith       mask[masked[j]] = maskcount++;
3380b6490206SBarry Smith     }
3381b6490206SBarry Smith     /* set "a" values into matrix */
3382de6a44a3SBarry Smith     ishift = bs2*a->i[i];
3383b6490206SBarry Smith     for (j=0; j<bs; j++) {
3384b6490206SBarry Smith       kmax = rowlengths[i*bs+j];
3385b6490206SBarry Smith       for (k=0; k<kmax; k++) {
3386de6a44a3SBarry Smith         tmp       = jj[nzcountb]/bs ;
3387de6a44a3SBarry Smith         block     = mask[tmp] - 1;
3388de6a44a3SBarry Smith         point     = jj[nzcountb] - bs*tmp;
3389de6a44a3SBarry Smith         idx       = ishift + bs2*block + j + bs*point;
3390375fe846SBarry Smith         a->a[idx] = (MatScalar)aa[nzcountb++];
3391b6490206SBarry Smith       }
3392b6490206SBarry Smith     }
339335aab85fSBarry Smith     /* zero out the mask elements we set */
339435aab85fSBarry Smith     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
3395b6490206SBarry Smith   }
3396e32f2f54SBarry Smith   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");
3397b6490206SBarry Smith 
3398606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3399606d414cSSatish Balay   ierr = PetscFree(browlengths);CHKERRQ(ierr);
3400606d414cSSatish Balay   ierr = PetscFree(aa);CHKERRQ(ierr);
3401606d414cSSatish Balay   ierr = PetscFree(jj);CHKERRQ(ierr);
3402fca92195SBarry Smith   ierr = PetscFree2(mask,masked);CHKERRQ(ierr);
3403b6490206SBarry Smith 
340478ae41b4SKris Buschelman   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
340578ae41b4SKris Buschelman   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
34069c01be13SBarry Smith   ierr = MatView_Private(B);CHKERRQ(ierr);
340778ae41b4SKris Buschelman 
340878ae41b4SKris Buschelman   *A = B;
34093a40ed3dSBarry Smith   PetscFunctionReturn(0);
34102593348eSBarry Smith }
34112593348eSBarry Smith 
34124a2ae208SSatish Balay #undef __FUNCT__
3413f501eaabSShri Abhyankar #define __FUNCT__ "MatLoadnew_SeqBAIJ"
3414f501eaabSShri Abhyankar PetscErrorCode MatLoadnew_SeqBAIJ(PetscViewer viewer,Mat newmat)
3415f501eaabSShri Abhyankar {
3416f501eaabSShri Abhyankar   Mat_SeqBAIJ    *a;
3417f501eaabSShri Abhyankar   PetscErrorCode ierr;
3418f501eaabSShri Abhyankar   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs=1;
3419f501eaabSShri Abhyankar   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*browlengths,maskcount;
3420f501eaabSShri Abhyankar   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows,rows,cols;
3421f501eaabSShri Abhyankar   PetscInt       *masked,nmask,tmp,bs2,ishift;
3422f501eaabSShri Abhyankar   PetscMPIInt    size;
3423f501eaabSShri Abhyankar   int            fd;
3424f501eaabSShri Abhyankar   PetscScalar    *aa;
3425f501eaabSShri Abhyankar   MPI_Comm       comm = ((PetscObject)viewer)->comm;
3426f501eaabSShri Abhyankar 
3427f501eaabSShri Abhyankar   PetscFunctionBegin;
3428f501eaabSShri Abhyankar   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading SEQBAIJ matrix","Mat");CHKERRQ(ierr);
3429f501eaabSShri Abhyankar   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
3430f501eaabSShri Abhyankar   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3431f501eaabSShri Abhyankar   bs2  = bs*bs;
3432f501eaabSShri Abhyankar 
3433f501eaabSShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3434f501eaabSShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
3435f501eaabSShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
3436f501eaabSShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3437f501eaabSShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
3438f501eaabSShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
3439f501eaabSShri Abhyankar 
3440f501eaabSShri Abhyankar   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqBAIJ");
3441f501eaabSShri Abhyankar   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
3442f501eaabSShri Abhyankar 
3443f501eaabSShri Abhyankar   /*
3444f501eaabSShri Abhyankar      This code adds extra rows to make sure the number of rows is
3445f501eaabSShri Abhyankar     divisible by the blocksize
3446f501eaabSShri Abhyankar   */
3447f501eaabSShri Abhyankar   mbs        = M/bs;
3448f501eaabSShri Abhyankar   extra_rows = bs - M + bs*(mbs);
3449f501eaabSShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
3450f501eaabSShri Abhyankar   else                  mbs++;
3451f501eaabSShri Abhyankar   if (extra_rows) {
3452f501eaabSShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
3453f501eaabSShri Abhyankar   }
3454f501eaabSShri Abhyankar 
3455f501eaabSShri Abhyankar   /* Set global sizes if not already set */
3456f501eaabSShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
3457f501eaabSShri Abhyankar     ierr = MatSetSizes(newmat,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
3458f501eaabSShri Abhyankar   } else { /* Check if the matrix global sizes are correct */
3459f501eaabSShri Abhyankar     ierr = MatGetSize(newmat,&rows,&cols);CHKERRQ(ierr);
3460f501eaabSShri 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);
3461f501eaabSShri Abhyankar   }
3462f501eaabSShri Abhyankar 
3463f501eaabSShri Abhyankar   /* read in row lengths */
3464f501eaabSShri Abhyankar   ierr = PetscMalloc((M+extra_rows)*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
3465f501eaabSShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
3466f501eaabSShri Abhyankar   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
3467f501eaabSShri Abhyankar 
3468f501eaabSShri Abhyankar   /* read in column indices */
3469f501eaabSShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscInt),&jj);CHKERRQ(ierr);
3470f501eaabSShri Abhyankar   ierr = PetscBinaryRead(fd,jj,nz,PETSC_INT);CHKERRQ(ierr);
3471f501eaabSShri Abhyankar   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;
3472f501eaabSShri Abhyankar 
3473f501eaabSShri Abhyankar   /* loop over row lengths determining block row lengths */
3474f501eaabSShri Abhyankar   ierr     = PetscMalloc(mbs*sizeof(PetscInt),&browlengths);CHKERRQ(ierr);
3475f501eaabSShri Abhyankar   ierr     = PetscMemzero(browlengths,mbs*sizeof(PetscInt));CHKERRQ(ierr);
3476f501eaabSShri Abhyankar   ierr     = PetscMalloc2(mbs,PetscInt,&mask,mbs,PetscInt,&masked);CHKERRQ(ierr);
3477f501eaabSShri Abhyankar   ierr     = PetscMemzero(mask,mbs*sizeof(PetscInt));CHKERRQ(ierr);
3478f501eaabSShri Abhyankar   rowcount = 0;
3479f501eaabSShri Abhyankar   nzcount = 0;
3480f501eaabSShri Abhyankar   for (i=0; i<mbs; i++) {
3481f501eaabSShri Abhyankar     nmask = 0;
3482f501eaabSShri Abhyankar     for (j=0; j<bs; j++) {
3483f501eaabSShri Abhyankar       kmax = rowlengths[rowcount];
3484f501eaabSShri Abhyankar       for (k=0; k<kmax; k++) {
3485f501eaabSShri Abhyankar         tmp = jj[nzcount++]/bs;
3486f501eaabSShri Abhyankar         if (!mask[tmp]) {masked[nmask++] = tmp; mask[tmp] = 1;}
3487f501eaabSShri Abhyankar       }
3488f501eaabSShri Abhyankar       rowcount++;
3489f501eaabSShri Abhyankar     }
3490f501eaabSShri Abhyankar     browlengths[i] += nmask;
3491f501eaabSShri Abhyankar     /* zero out the mask elements we set */
3492f501eaabSShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
3493f501eaabSShri Abhyankar   }
3494f501eaabSShri Abhyankar 
34952f480046SShri Abhyankar   /* Do preallocation  */
3496f501eaabSShri Abhyankar   ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(newmat,bs,0,browlengths);CHKERRQ(ierr);
3497f501eaabSShri Abhyankar   a = (Mat_SeqBAIJ*)newmat->data;
3498f501eaabSShri Abhyankar 
3499f501eaabSShri Abhyankar   /* set matrix "i" values */
3500f501eaabSShri Abhyankar   a->i[0] = 0;
3501f501eaabSShri Abhyankar   for (i=1; i<= mbs; i++) {
3502f501eaabSShri Abhyankar     a->i[i]      = a->i[i-1] + browlengths[i-1];
3503f501eaabSShri Abhyankar     a->ilen[i-1] = browlengths[i-1];
3504f501eaabSShri Abhyankar   }
3505f501eaabSShri Abhyankar   a->nz         = 0;
3506f501eaabSShri Abhyankar   for (i=0; i<mbs; i++) a->nz += browlengths[i];
3507f501eaabSShri Abhyankar 
3508f501eaabSShri Abhyankar   /* read in nonzero values */
3509f501eaabSShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscScalar),&aa);CHKERRQ(ierr);
3510f501eaabSShri Abhyankar   ierr = PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);CHKERRQ(ierr);
3511f501eaabSShri Abhyankar   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;
3512f501eaabSShri Abhyankar 
3513f501eaabSShri Abhyankar   /* set "a" and "j" values into matrix */
3514f501eaabSShri Abhyankar   nzcount = 0; jcount = 0;
3515f501eaabSShri Abhyankar   for (i=0; i<mbs; i++) {
3516f501eaabSShri Abhyankar     nzcountb = nzcount;
3517f501eaabSShri Abhyankar     nmask    = 0;
3518f501eaabSShri Abhyankar     for (j=0; j<bs; j++) {
3519f501eaabSShri Abhyankar       kmax = rowlengths[i*bs+j];
3520f501eaabSShri Abhyankar       for (k=0; k<kmax; k++) {
3521f501eaabSShri Abhyankar         tmp = jj[nzcount++]/bs;
3522f501eaabSShri Abhyankar 	if (!mask[tmp]) { masked[nmask++] = tmp; mask[tmp] = 1;}
3523f501eaabSShri Abhyankar       }
3524f501eaabSShri Abhyankar     }
3525f501eaabSShri Abhyankar     /* sort the masked values */
3526f501eaabSShri Abhyankar     ierr = PetscSortInt(nmask,masked);CHKERRQ(ierr);
3527f501eaabSShri Abhyankar 
3528f501eaabSShri Abhyankar     /* set "j" values into matrix */
3529f501eaabSShri Abhyankar     maskcount = 1;
3530f501eaabSShri Abhyankar     for (j=0; j<nmask; j++) {
3531f501eaabSShri Abhyankar       a->j[jcount++]  = masked[j];
3532f501eaabSShri Abhyankar       mask[masked[j]] = maskcount++;
3533f501eaabSShri Abhyankar     }
3534f501eaabSShri Abhyankar     /* set "a" values into matrix */
3535f501eaabSShri Abhyankar     ishift = bs2*a->i[i];
3536f501eaabSShri Abhyankar     for (j=0; j<bs; j++) {
3537f501eaabSShri Abhyankar       kmax = rowlengths[i*bs+j];
3538f501eaabSShri Abhyankar       for (k=0; k<kmax; k++) {
3539f501eaabSShri Abhyankar         tmp       = jj[nzcountb]/bs ;
3540f501eaabSShri Abhyankar         block     = mask[tmp] - 1;
3541f501eaabSShri Abhyankar         point     = jj[nzcountb] - bs*tmp;
3542f501eaabSShri Abhyankar         idx       = ishift + bs2*block + j + bs*point;
3543f501eaabSShri Abhyankar         a->a[idx] = (MatScalar)aa[nzcountb++];
3544f501eaabSShri Abhyankar       }
3545f501eaabSShri Abhyankar     }
3546f501eaabSShri Abhyankar     /* zero out the mask elements we set */
3547f501eaabSShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
3548f501eaabSShri Abhyankar   }
3549f501eaabSShri Abhyankar   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");
3550f501eaabSShri Abhyankar 
3551f501eaabSShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3552f501eaabSShri Abhyankar   ierr = PetscFree(browlengths);CHKERRQ(ierr);
3553f501eaabSShri Abhyankar   ierr = PetscFree(aa);CHKERRQ(ierr);
3554f501eaabSShri Abhyankar   ierr = PetscFree(jj);CHKERRQ(ierr);
3555f501eaabSShri Abhyankar   ierr = PetscFree2(mask,masked);CHKERRQ(ierr);
3556f501eaabSShri Abhyankar 
3557f501eaabSShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3558f501eaabSShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3559f501eaabSShri Abhyankar   ierr = MatView_Private(newmat);CHKERRQ(ierr);
3560f501eaabSShri Abhyankar   PetscFunctionReturn(0);
3561f501eaabSShri Abhyankar }
3562f501eaabSShri Abhyankar 
3563f501eaabSShri Abhyankar #undef __FUNCT__
35644a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqBAIJ"
3565273d9f13SBarry Smith /*@C
3566273d9f13SBarry Smith    MatCreateSeqBAIJ - Creates a sparse matrix in block AIJ (block
3567273d9f13SBarry Smith    compressed row) format.  For good matrix assembly performance the
3568273d9f13SBarry Smith    user should preallocate the matrix storage by setting the parameter nz
3569273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3570273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
35712593348eSBarry Smith 
3572273d9f13SBarry Smith    Collective on MPI_Comm
3573273d9f13SBarry Smith 
3574273d9f13SBarry Smith    Input Parameters:
3575273d9f13SBarry Smith +  comm - MPI communicator, set to PETSC_COMM_SELF
3576273d9f13SBarry Smith .  bs - size of block
3577273d9f13SBarry Smith .  m - number of rows
3578273d9f13SBarry Smith .  n - number of columns
357935d8aa7fSBarry Smith .  nz - number of nonzero blocks  per block row (same for all rows)
358035d8aa7fSBarry Smith -  nnz - array containing the number of nonzero blocks in the various block rows
3581273d9f13SBarry Smith          (possibly different for each block row) or PETSC_NULL
3582273d9f13SBarry Smith 
3583273d9f13SBarry Smith    Output Parameter:
3584273d9f13SBarry Smith .  A - the matrix
3585273d9f13SBarry Smith 
3586175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3587ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3588175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3589175b88e8SBarry Smith 
3590273d9f13SBarry Smith    Options Database Keys:
3591273d9f13SBarry Smith .   -mat_no_unroll - uses code that does not unroll the loops in the
3592273d9f13SBarry Smith                      block calculations (much slower)
3593273d9f13SBarry Smith .    -mat_block_size - size of the blocks to use
3594273d9f13SBarry Smith 
3595273d9f13SBarry Smith    Level: intermediate
3596273d9f13SBarry Smith 
3597273d9f13SBarry Smith    Notes:
3598d1be2dadSMatthew Knepley    The number of rows and columns must be divisible by blocksize.
3599d1be2dadSMatthew Knepley 
360049a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
360149a6f317SBarry Smith 
360235d8aa7fSBarry Smith    A nonzero block is any block that as 1 or more nonzeros in it
360335d8aa7fSBarry Smith 
3604273d9f13SBarry Smith    The block AIJ format is fully compatible with standard Fortran 77
3605273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3606273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3607273d9f13SBarry Smith 
3608273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3609273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3610273d9f13SBarry Smith    allocation.  For additional details, see the users manual chapter on
3611273d9f13SBarry Smith    matrices.
3612273d9f13SBarry Smith 
3613273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIBAIJ()
3614273d9f13SBarry Smith @*/
3615be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
3616273d9f13SBarry Smith {
3617dfbe8321SBarry Smith   PetscErrorCode ierr;
3618273d9f13SBarry Smith 
3619273d9f13SBarry Smith   PetscFunctionBegin;
3620f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3621f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3622273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQBAIJ);CHKERRQ(ierr);
3623ab93d7beSBarry Smith   ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(*A,bs,nz,(PetscInt*)nnz);CHKERRQ(ierr);
3624273d9f13SBarry Smith   PetscFunctionReturn(0);
3625273d9f13SBarry Smith }
3626273d9f13SBarry Smith 
36274a2ae208SSatish Balay #undef __FUNCT__
36284a2ae208SSatish Balay #define __FUNCT__ "MatSeqBAIJSetPreallocation"
3629273d9f13SBarry Smith /*@C
3630273d9f13SBarry Smith    MatSeqBAIJSetPreallocation - Sets the block size and expected nonzeros
3631273d9f13SBarry Smith    per row in the matrix. For good matrix assembly performance the
3632273d9f13SBarry Smith    user should preallocate the matrix storage by setting the parameter nz
3633273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3634273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3635273d9f13SBarry Smith 
3636273d9f13SBarry Smith    Collective on MPI_Comm
3637273d9f13SBarry Smith 
3638273d9f13SBarry Smith    Input Parameters:
3639273d9f13SBarry Smith +  A - the matrix
3640273d9f13SBarry Smith .  bs - size of block
3641273d9f13SBarry Smith .  nz - number of block nonzeros per block row (same for all rows)
3642273d9f13SBarry Smith -  nnz - array containing the number of block nonzeros in the various block rows
3643273d9f13SBarry Smith          (possibly different for each block row) or PETSC_NULL
3644273d9f13SBarry Smith 
3645273d9f13SBarry Smith    Options Database Keys:
3646273d9f13SBarry Smith .   -mat_no_unroll - uses code that does not unroll the loops in the
3647273d9f13SBarry Smith                      block calculations (much slower)
3648273d9f13SBarry Smith .    -mat_block_size - size of the blocks to use
3649273d9f13SBarry Smith 
3650273d9f13SBarry Smith    Level: intermediate
3651273d9f13SBarry Smith 
3652273d9f13SBarry Smith    Notes:
365349a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
365449a6f317SBarry Smith 
3655aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3656aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3657aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3658aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3659aa95bbe8SBarry Smith 
3660273d9f13SBarry Smith    The block AIJ format is fully compatible with standard Fortran 77
3661273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3662273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3663273d9f13SBarry Smith 
3664273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3665273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3666273d9f13SBarry Smith    allocation.  For additional details, see the users manual chapter on
3667273d9f13SBarry Smith    matrices.
3668273d9f13SBarry Smith 
3669aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIBAIJ(), MatGetInfo()
3670273d9f13SBarry Smith @*/
3671be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
3672273d9f13SBarry Smith {
3673c1ac3661SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,PetscInt,const PetscInt[]);
3674273d9f13SBarry Smith 
3675273d9f13SBarry Smith   PetscFunctionBegin;
3676a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqBAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
3677a23d5eceSKris Buschelman   if (f) {
3678a23d5eceSKris Buschelman     ierr = (*f)(B,bs,nz,nnz);CHKERRQ(ierr);
3679273d9f13SBarry Smith   }
3680273d9f13SBarry Smith   PetscFunctionReturn(0);
3681273d9f13SBarry Smith }
3682a1d92eedSBarry Smith 
3683c75a6043SHong Zhang #undef __FUNCT__
3684725b52f3SLisandro Dalcin #define __FUNCT__ "MatSeqBAIJSetPreallocationCSR"
3685725b52f3SLisandro Dalcin /*@C
3686725b52f3SLisandro Dalcin    MatSeqBAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format
3687725b52f3SLisandro Dalcin    (the default sequential PETSc format).
3688725b52f3SLisandro Dalcin 
3689725b52f3SLisandro Dalcin    Collective on MPI_Comm
3690725b52f3SLisandro Dalcin 
3691725b52f3SLisandro Dalcin    Input Parameters:
3692725b52f3SLisandro Dalcin +  A - the matrix
3693725b52f3SLisandro Dalcin .  i - the indices into j for the start of each local row (starts with zero)
3694725b52f3SLisandro Dalcin .  j - the column indices for each local row (starts with zero) these must be sorted for each row
3695725b52f3SLisandro Dalcin -  v - optional values in the matrix
3696725b52f3SLisandro Dalcin 
3697725b52f3SLisandro Dalcin    Level: developer
3698725b52f3SLisandro Dalcin 
3699725b52f3SLisandro Dalcin .keywords: matrix, aij, compressed row, sparse
3700725b52f3SLisandro Dalcin 
3701725b52f3SLisandro Dalcin .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatSeqBAIJSetPreallocation(), MATSEQBAIJ
3702725b52f3SLisandro Dalcin @*/
3703725b52f3SLisandro Dalcin PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3704725b52f3SLisandro Dalcin {
3705725b52f3SLisandro Dalcin   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]);
3706725b52f3SLisandro Dalcin 
3707725b52f3SLisandro Dalcin   PetscFunctionBegin;
3708725b52f3SLisandro Dalcin   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqBAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
3709725b52f3SLisandro Dalcin   if (f) {
3710725b52f3SLisandro Dalcin     ierr = (*f)(B,bs,i,j,v);CHKERRQ(ierr);
3711725b52f3SLisandro Dalcin   }
3712725b52f3SLisandro Dalcin   PetscFunctionReturn(0);
3713725b52f3SLisandro Dalcin }
3714725b52f3SLisandro Dalcin 
3715725b52f3SLisandro Dalcin 
3716725b52f3SLisandro Dalcin #undef __FUNCT__
3717c75a6043SHong Zhang #define __FUNCT__ "MatCreateSeqBAIJWithArrays"
3718c75a6043SHong Zhang /*@
3719c75a6043SHong Zhang      MatCreateSeqBAIJWithArrays - Creates an sequential BAIJ matrix using matrix elements
3720c75a6043SHong Zhang               (upper triangular entries in CSR format) provided by the user.
3721c75a6043SHong Zhang 
3722c75a6043SHong Zhang      Collective on MPI_Comm
3723c75a6043SHong Zhang 
3724c75a6043SHong Zhang    Input Parameters:
3725c75a6043SHong Zhang +  comm - must be an MPI communicator of size 1
3726c75a6043SHong Zhang .  bs - size of block
3727c75a6043SHong Zhang .  m - number of rows
3728c75a6043SHong Zhang .  n - number of columns
3729c75a6043SHong Zhang .  i - row indices
3730c75a6043SHong Zhang .  j - column indices
3731c75a6043SHong Zhang -  a - matrix values
3732c75a6043SHong Zhang 
3733c75a6043SHong Zhang    Output Parameter:
3734c75a6043SHong Zhang .  mat - the matrix
3735c75a6043SHong Zhang 
3736c75a6043SHong Zhang    Level: intermediate
3737c75a6043SHong Zhang 
3738c75a6043SHong Zhang    Notes:
3739c75a6043SHong Zhang        The i, j, and a arrays are not copied by this routine, the user must free these arrays
3740c75a6043SHong Zhang     once the matrix is destroyed
3741c75a6043SHong Zhang 
3742c75a6043SHong Zhang        You cannot set new nonzero locations into this matrix, that will generate an error.
3743c75a6043SHong Zhang 
3744c75a6043SHong Zhang        The i and j indices are 0 based
3745c75a6043SHong Zhang 
3746c75a6043SHong Zhang .seealso: MatCreate(), MatCreateMPIBAIJ(), MatCreateSeqBAIJ()
3747c75a6043SHong Zhang 
3748c75a6043SHong Zhang @*/
3749c75a6043SHong Zhang PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
3750c75a6043SHong Zhang {
3751c75a6043SHong Zhang   PetscErrorCode ierr;
3752c75a6043SHong Zhang   PetscInt       ii;
3753c75a6043SHong Zhang   Mat_SeqBAIJ    *baij;
3754c75a6043SHong Zhang 
3755c75a6043SHong Zhang   PetscFunctionBegin;
3756e32f2f54SBarry Smith   if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"block size %D > 1 is not supported yet",bs);
3757e32f2f54SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
3758c75a6043SHong Zhang 
3759c75a6043SHong Zhang   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3760c75a6043SHong Zhang   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3761c75a6043SHong Zhang   ierr = MatSetType(*mat,MATSEQBAIJ);CHKERRQ(ierr);
3762c75a6043SHong Zhang   ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(*mat,bs,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3763c75a6043SHong Zhang   baij = (Mat_SeqBAIJ*)(*mat)->data;
3764c75a6043SHong Zhang   ierr = PetscMalloc2(m,PetscInt,&baij->imax,m,PetscInt,&baij->ilen);CHKERRQ(ierr);
37651784c0f5SBarry Smith   ierr = PetscLogObjectMemory(*mat,2*m*sizeof(PetscInt));CHKERRQ(ierr);
3766c75a6043SHong Zhang 
3767c75a6043SHong Zhang   baij->i = i;
3768c75a6043SHong Zhang   baij->j = j;
3769c75a6043SHong Zhang   baij->a = a;
3770c75a6043SHong Zhang   baij->singlemalloc = PETSC_FALSE;
3771c75a6043SHong Zhang   baij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3772e6b907acSBarry Smith   baij->free_a       = PETSC_FALSE;
3773e6b907acSBarry Smith   baij->free_ij       = PETSC_FALSE;
3774c75a6043SHong Zhang 
3775c75a6043SHong Zhang   for (ii=0; ii<m; ii++) {
3776c75a6043SHong Zhang     baij->ilen[ii] = baij->imax[ii] = i[ii+1] - i[ii];
3777c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
3778e32f2f54SBarry 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]);
3779c75a6043SHong Zhang #endif
3780c75a6043SHong Zhang   }
3781c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
3782c75a6043SHong Zhang   for (ii=0; ii<baij->i[m]; ii++) {
3783e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
3784e32f2f54SBarry 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]);
3785c75a6043SHong Zhang   }
3786c75a6043SHong Zhang #endif
3787c75a6043SHong Zhang 
3788c75a6043SHong Zhang   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3789c75a6043SHong Zhang   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3790c75a6043SHong Zhang   PetscFunctionReturn(0);
3791c75a6043SHong Zhang }
3792