xref: /petsc/src/mat/impls/baij/seq/baij.c (revision 112444f445afd2ff3b13d8a223b16527a1e55859)
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;
11695a586d82SBarry 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) {
181653ef36baSBarry Smith         value = v + (k*(stepval+bs) + l)*bs;
18170e324ae4SSatish Balay       } else {
181853ef36baSBarry Smith         value = v + (l*(stepval+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) {
184753ef36baSBarry Smith               for (ii=0; ii<bs; ii++,value+=bs+stepval) {
1848dd9472c6SBarry Smith                 for (jj=0; jj<bs; jj++) {
184953ef36baSBarry Smith                   bap[jj] += value[jj];
1850dd9472c6SBarry Smith                 }
185153ef36baSBarry Smith                 bap += bs;
1852dd9472c6SBarry Smith               }
18530e324ae4SSatish Balay             } else {
185453ef36baSBarry Smith               for (ii=0; ii<bs; ii++,value+=bs+stepval) {
1855dd9472c6SBarry Smith                 for (jj=0; jj<bs; jj++) {
185653ef36baSBarry Smith                   bap[jj]  = value[jj];
18570e324ae4SSatish Balay                 }
185853ef36baSBarry Smith                 bap += bs;
18598a84c255SSatish Balay               }
1860dd9472c6SBarry Smith             }
1861dd9472c6SBarry Smith           }
1862f1241b54SBarry Smith           goto noinsert2;
186392c4ed94SBarry Smith         }
186492c4ed94SBarry Smith       }
186589280ab3SLois Curfman McInnes       if (nonew == 1) goto noinsert2;
1866e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
1867fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
1868c03d1d03SSatish Balay       N = nrow++ - 1; high++;
186992c4ed94SBarry Smith       /* shift up all the later entries in this row */
187092c4ed94SBarry Smith       for (ii=N; ii>=i; ii--) {
187192c4ed94SBarry Smith         rp[ii+1] = rp[ii];
1872549d3d68SSatish Balay         ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
187392c4ed94SBarry Smith       }
1874549d3d68SSatish Balay       if (N >= i) {
1875549d3d68SSatish Balay         ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1876549d3d68SSatish Balay       }
187792c4ed94SBarry Smith       rp[i] = col;
18788a84c255SSatish Balay       bap   = ap +  bs2*i;
18790e324ae4SSatish Balay       if (roworiented) {
1880dd9472c6SBarry Smith         for (ii=0; ii<bs; ii++,value+=stepval) {
1881dd9472c6SBarry Smith           for (jj=ii; jj<bs2; jj+=bs) {
18820e324ae4SSatish Balay             bap[jj] = *value++;
1883dd9472c6SBarry Smith           }
1884dd9472c6SBarry Smith         }
18850e324ae4SSatish Balay       } else {
1886dd9472c6SBarry Smith         for (ii=0; ii<bs; ii++,value+=stepval) {
1887dd9472c6SBarry Smith           for (jj=0; jj<bs; jj++) {
18880e324ae4SSatish Balay             *bap++  = *value++;
18890e324ae4SSatish Balay           }
1890dd9472c6SBarry Smith         }
1891dd9472c6SBarry Smith       }
1892f1241b54SBarry Smith       noinsert2:;
189392c4ed94SBarry Smith       low = i;
189492c4ed94SBarry Smith     }
189592c4ed94SBarry Smith     ailen[row] = nrow;
189692c4ed94SBarry Smith   }
18973a40ed3dSBarry Smith   PetscFunctionReturn(0);
189892c4ed94SBarry Smith }
189926e093fcSHong Zhang 
19004a2ae208SSatish Balay #undef __FUNCT__
19014a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqBAIJ"
1902dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqBAIJ(Mat A,MatAssemblyType mode)
1903584200bdSSatish Balay {
1904584200bdSSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
1905c1ac3661SBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
1906d0f46423SBarry Smith   PetscInt       m = A->rmap->N,*ip,N,*ailen = a->ilen;
19076849ba73SBarry Smith   PetscErrorCode ierr;
1908c1ac3661SBarry Smith   PetscInt       mbs = a->mbs,bs2 = a->bs2,rmax = 0;
19093f1db9ecSBarry Smith   MatScalar      *aa = a->a,*ap;
19103447b6efSHong Zhang   PetscReal      ratio=0.6;
1911584200bdSSatish Balay 
19123a40ed3dSBarry Smith   PetscFunctionBegin;
19133a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
1914584200bdSSatish Balay 
191543ee02c3SBarry Smith   if (m) rmax = ailen[0];
1916584200bdSSatish Balay   for (i=1; i<mbs; i++) {
1917584200bdSSatish Balay     /* move each row back by the amount of empty slots (fshift) before it*/
1918584200bdSSatish Balay     fshift += imax[i-1] - ailen[i-1];
1919d402145bSBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
1920584200bdSSatish Balay     if (fshift) {
1921a7c10996SSatish Balay       ip = aj + ai[i]; ap = aa + bs2*ai[i];
1922584200bdSSatish Balay       N = ailen[i];
1923584200bdSSatish Balay       for (j=0; j<N; j++) {
1924584200bdSSatish Balay         ip[j-fshift] = ip[j];
1925549d3d68SSatish Balay         ierr = PetscMemcpy(ap+(j-fshift)*bs2,ap+j*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
1926584200bdSSatish Balay       }
1927584200bdSSatish Balay     }
1928584200bdSSatish Balay     ai[i] = ai[i-1] + ailen[i-1];
1929584200bdSSatish Balay   }
1930584200bdSSatish Balay   if (mbs) {
1931584200bdSSatish Balay     fshift += imax[mbs-1] - ailen[mbs-1];
1932584200bdSSatish Balay     ai[mbs] = ai[mbs-1] + ailen[mbs-1];
1933584200bdSSatish Balay   }
1934584200bdSSatish Balay   /* reset ilen and imax for each row */
1935584200bdSSatish Balay   for (i=0; i<mbs; i++) {
1936584200bdSSatish Balay     ailen[i] = imax[i] = ai[i+1] - ai[i];
1937584200bdSSatish Balay   }
1938a7c10996SSatish Balay   a->nz = ai[mbs];
1939584200bdSSatish Balay 
1940584200bdSSatish Balay   /* diagonals may have moved, so kill the diagonal pointers */
1941b01c7715SBarry Smith   a->idiagvalid = PETSC_FALSE;
1942584200bdSSatish Balay   if (fshift && a->diag) {
1943606d414cSSatish Balay     ierr = PetscFree(a->diag);CHKERRQ(ierr);
194452e6d16bSBarry Smith     ierr = PetscLogObjectMemory(A,-(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
1945584200bdSSatish Balay     a->diag = 0;
1946584200bdSSatish Balay   }
194765e19b50SBarry 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);
1948d0f46423SBarry 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);
1949ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues is %D\n",a->reallocs);CHKERRQ(ierr);
1950ae15b995SBarry Smith   ierr = PetscInfo1(A,"Most nonzeros blocks in any row is %D\n",rmax);CHKERRQ(ierr);
19518e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
1952e2f3b5e9SSatish Balay   a->reallocs          = 0;
19530e6d2581SBarry Smith   A->info.nz_unneeded  = (PetscReal)fshift*bs2;
1954cf4441caSHong Zhang 
1955cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
19562f53aa61SHong Zhang   if (a->compressedrow.use){
1957317fbc4cSHong Zhang     ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,mbs,ratio);CHKERRQ(ierr);
195873e7a558SHong Zhang   }
195988e51ccdSHong Zhang 
196088e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
19613a40ed3dSBarry Smith   PetscFunctionReturn(0);
1962584200bdSSatish Balay }
1963584200bdSSatish Balay 
1964bea157c4SSatish Balay /*
1965bea157c4SSatish Balay    This function returns an array of flags which indicate the locations of contiguous
1966bea157c4SSatish Balay    blocks that should be zeroed. for eg: if bs = 3  and is = [0,1,2,3,5,6,7,8,9]
1967bea157c4SSatish Balay    then the resulting sizes = [3,1,1,3,1] correspondig to sets [(0,1,2),(3),(5),(6,7,8),(9)]
1968bea157c4SSatish Balay    Assume: sizes should be long enough to hold all the values.
1969bea157c4SSatish Balay */
19704a2ae208SSatish Balay #undef __FUNCT__
19714a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqBAIJ_Check_Blocks"
1972c1ac3661SBarry Smith static PetscErrorCode MatZeroRows_SeqBAIJ_Check_Blocks(PetscInt idx[],PetscInt n,PetscInt bs,PetscInt sizes[], PetscInt *bs_max)
1973d9b7c43dSSatish Balay {
1974c1ac3661SBarry Smith   PetscInt   i,j,k,row;
1975bea157c4SSatish Balay   PetscTruth flg;
19763a40ed3dSBarry Smith 
1977433994e6SBarry Smith   PetscFunctionBegin;
1978bea157c4SSatish Balay   for (i=0,j=0; i<n; j++) {
1979bea157c4SSatish Balay     row = idx[i];
1980bea157c4SSatish Balay     if (row%bs!=0) { /* Not the begining of a block */
1981bea157c4SSatish Balay       sizes[j] = 1;
1982bea157c4SSatish Balay       i++;
1983e4fda26cSSatish Balay     } else if (i+bs > n) { /* complete block doesn't exist (at idx end) */
1984bea157c4SSatish Balay       sizes[j] = 1;         /* Also makes sure atleast 'bs' values exist for next else */
1985bea157c4SSatish Balay       i++;
1986bea157c4SSatish Balay     } else { /* Begining of the block, so check if the complete block exists */
1987bea157c4SSatish Balay       flg = PETSC_TRUE;
1988bea157c4SSatish Balay       for (k=1; k<bs; k++) {
1989bea157c4SSatish Balay         if (row+k != idx[i+k]) { /* break in the block */
1990bea157c4SSatish Balay           flg = PETSC_FALSE;
1991bea157c4SSatish Balay           break;
1992d9b7c43dSSatish Balay         }
1993bea157c4SSatish Balay       }
1994abc0a331SBarry Smith       if (flg) { /* No break in the bs */
1995bea157c4SSatish Balay         sizes[j] = bs;
1996bea157c4SSatish Balay         i+= bs;
1997bea157c4SSatish Balay       } else {
1998bea157c4SSatish Balay         sizes[j] = 1;
1999bea157c4SSatish Balay         i++;
2000bea157c4SSatish Balay       }
2001bea157c4SSatish Balay     }
2002bea157c4SSatish Balay   }
2003bea157c4SSatish Balay   *bs_max = j;
20043a40ed3dSBarry Smith   PetscFunctionReturn(0);
2005d9b7c43dSSatish Balay }
2006d9b7c43dSSatish Balay 
20074a2ae208SSatish Balay #undef __FUNCT__
20084a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqBAIJ"
2009f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqBAIJ(Mat A,PetscInt is_n,const PetscInt is_idx[],PetscScalar diag)
2010d9b7c43dSSatish Balay {
2011d9b7c43dSSatish Balay   Mat_SeqBAIJ    *baij=(Mat_SeqBAIJ*)A->data;
2012dfbe8321SBarry Smith   PetscErrorCode ierr;
2013f4df32b1SMatthew Knepley   PetscInt       i,j,k,count,*rows;
2014d0f46423SBarry Smith   PetscInt       bs=A->rmap->bs,bs2=baij->bs2,*sizes,row,bs_max;
201587828ca2SBarry Smith   PetscScalar    zero = 0.0;
20163f1db9ecSBarry Smith   MatScalar      *aa;
2017d9b7c43dSSatish Balay 
20183a40ed3dSBarry Smith   PetscFunctionBegin;
2019d9b7c43dSSatish Balay   /* Make a copy of the IS and  sort it */
2020bea157c4SSatish Balay   /* allocate memory for rows,sizes */
2021fca92195SBarry Smith   ierr  = PetscMalloc2(is_n,PetscInt,&rows,2*is_n,PetscInt,&sizes);CHKERRQ(ierr);
2022bea157c4SSatish Balay 
2023563b5814SBarry Smith   /* copy IS values to rows, and sort them */
2024bea157c4SSatish Balay   for (i=0; i<is_n; i++) { rows[i] = is_idx[i]; }
2025bea157c4SSatish Balay   ierr = PetscSortInt(is_n,rows);CHKERRQ(ierr);
2026a9817697SBarry Smith   if (baij->keepnonzeropattern) {
2027dffd3267SBarry Smith     for (i=0; i<is_n; i++) { sizes[i] = 1; }
2028dffd3267SBarry Smith     bs_max = is_n;
202988e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
2030dffd3267SBarry Smith   } else {
2031bea157c4SSatish Balay     ierr = MatZeroRows_SeqBAIJ_Check_Blocks(rows,is_n,bs,sizes,&bs_max);CHKERRQ(ierr);
203288e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
2033dffd3267SBarry Smith   }
2034bea157c4SSatish Balay 
2035bea157c4SSatish Balay   for (i=0,j=0; i<bs_max; j+=sizes[i],i++) {
2036bea157c4SSatish Balay     row   = rows[j];
2037e32f2f54SBarry Smith     if (row < 0 || row > A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range",row);
2038bea157c4SSatish Balay     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
2039b31fbe3bSSatish Balay     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
2040a9817697SBarry Smith     if (sizes[i] == bs && !baij->keepnonzeropattern) {
2041f4df32b1SMatthew Knepley       if (diag != 0.0) {
2042bea157c4SSatish Balay         if (baij->ilen[row/bs] > 0) {
2043bea157c4SSatish Balay           baij->ilen[row/bs]       = 1;
2044bea157c4SSatish Balay           baij->j[baij->i[row/bs]] = row/bs;
2045bea157c4SSatish Balay           ierr = PetscMemzero(aa,count*bs*sizeof(MatScalar));CHKERRQ(ierr);
2046a07cd24cSSatish Balay         }
2047563b5814SBarry Smith         /* Now insert all the diagonal values for this bs */
2048bea157c4SSatish Balay         for (k=0; k<bs; k++) {
2049f4df32b1SMatthew Knepley           ierr = (*A->ops->setvalues)(A,1,rows+j+k,1,rows+j+k,&diag,INSERT_VALUES);CHKERRQ(ierr);
2050bea157c4SSatish Balay         }
2051f4df32b1SMatthew Knepley       } else { /* (diag == 0.0) */
2052bea157c4SSatish Balay         baij->ilen[row/bs] = 0;
2053f4df32b1SMatthew Knepley       } /* end (diag == 0.0) */
2054bea157c4SSatish Balay     } else { /* (sizes[i] != bs) */
2055aa482453SBarry Smith #if defined (PETSC_USE_DEBUG)
2056e32f2f54SBarry Smith       if (sizes[i] != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal Error. Value should be 1");
2057bea157c4SSatish Balay #endif
2058bea157c4SSatish Balay       for (k=0; k<count; k++) {
2059d9b7c43dSSatish Balay         aa[0] =  zero;
2060d9b7c43dSSatish Balay         aa    += bs;
2061d9b7c43dSSatish Balay       }
2062f4df32b1SMatthew Knepley       if (diag != 0.0) {
2063f4df32b1SMatthew Knepley         ierr = (*A->ops->setvalues)(A,1,rows+j,1,rows+j,&diag,INSERT_VALUES);CHKERRQ(ierr);
2064d9b7c43dSSatish Balay       }
2065d9b7c43dSSatish Balay     }
2066bea157c4SSatish Balay   }
2067bea157c4SSatish Balay 
2068fca92195SBarry Smith   ierr = PetscFree2(rows,sizes);CHKERRQ(ierr);
20699a8dea36SBarry Smith   ierr = MatAssemblyEnd_SeqBAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20703a40ed3dSBarry Smith   PetscFunctionReturn(0);
2071d9b7c43dSSatish Balay }
20721c351548SSatish Balay 
20734a2ae208SSatish Balay #undef __FUNCT__
20744a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqBAIJ"
2075c1ac3661SBarry Smith PetscErrorCode MatSetValues_SeqBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
20762d61bbb3SSatish Balay {
20772d61bbb3SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
2078e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
2079c1ac3661SBarry Smith   PetscInt       *imax=a->imax,*ai=a->i,*ailen=a->ilen;
2080d0f46423SBarry Smith   PetscInt       *aj=a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
20816849ba73SBarry Smith   PetscErrorCode ierr;
2082c1ac3661SBarry Smith   PetscInt       ridx,cidx,bs2=a->bs2;
2083273d9f13SBarry Smith   PetscTruth     roworiented=a->roworiented;
20843f1db9ecSBarry Smith   MatScalar      *ap,value,*aa=a->a,*bap;
20852d61bbb3SSatish Balay 
20862d61bbb3SSatish Balay   PetscFunctionBegin;
208771fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
20882d61bbb3SSatish Balay   for (k=0; k<m; k++) { /* loop over added rows */
2089085a36d4SBarry Smith     row  = im[k];
2090085a36d4SBarry Smith     brow = row/bs;
20915ef9f2a5SBarry Smith     if (row < 0) continue;
20922515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
2093e32f2f54SBarry 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);
20942d61bbb3SSatish Balay #endif
20952d61bbb3SSatish Balay     rp   = aj + ai[brow];
20962d61bbb3SSatish Balay     ap   = aa + bs2*ai[brow];
20972d61bbb3SSatish Balay     rmax = imax[brow];
20982d61bbb3SSatish Balay     nrow = ailen[brow];
20992d61bbb3SSatish Balay     low  = 0;
2100c71e6ed7SBarry Smith     high = nrow;
21012d61bbb3SSatish Balay     for (l=0; l<n; l++) { /* loop over added columns */
21025ef9f2a5SBarry Smith       if (in[l] < 0) continue;
21032515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
2104e32f2f54SBarry 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);
21052d61bbb3SSatish Balay #endif
21062d61bbb3SSatish Balay       col = in[l]; bcol = col/bs;
21072d61bbb3SSatish Balay       ridx = row % bs; cidx = col % bs;
21082d61bbb3SSatish Balay       if (roworiented) {
21095ef9f2a5SBarry Smith         value = v[l + k*n];
21102d61bbb3SSatish Balay       } else {
21112d61bbb3SSatish Balay         value = v[k + l*m];
21122d61bbb3SSatish Balay       }
21137cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
2114e2ee6c50SBarry Smith       lastcol = col;
21152d61bbb3SSatish Balay       while (high-low > 7) {
21162d61bbb3SSatish Balay         t = (low+high)/2;
21172d61bbb3SSatish Balay         if (rp[t] > bcol) high = t;
21182d61bbb3SSatish Balay         else              low  = t;
21192d61bbb3SSatish Balay       }
21202d61bbb3SSatish Balay       for (i=low; i<high; i++) {
21212d61bbb3SSatish Balay         if (rp[i] > bcol) break;
21222d61bbb3SSatish Balay         if (rp[i] == bcol) {
21232d61bbb3SSatish Balay           bap  = ap +  bs2*i + bs*cidx + ridx;
21242d61bbb3SSatish Balay           if (is == ADD_VALUES) *bap += value;
21252d61bbb3SSatish Balay           else                  *bap  = value;
21262d61bbb3SSatish Balay           goto noinsert1;
21272d61bbb3SSatish Balay         }
21282d61bbb3SSatish Balay       }
21292d61bbb3SSatish Balay       if (nonew == 1) goto noinsert1;
2130e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
2131fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
2132c03d1d03SSatish Balay       N = nrow++ - 1; high++;
21332d61bbb3SSatish Balay       /* shift up all the later entries in this row */
21342d61bbb3SSatish Balay       for (ii=N; ii>=i; ii--) {
21352d61bbb3SSatish Balay         rp[ii+1] = rp[ii];
2136549d3d68SSatish Balay         ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
21372d61bbb3SSatish Balay       }
2138549d3d68SSatish Balay       if (N>=i) {
2139549d3d68SSatish Balay         ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
2140549d3d68SSatish Balay       }
21412d61bbb3SSatish Balay       rp[i]                      = bcol;
21422d61bbb3SSatish Balay       ap[bs2*i + bs*cidx + ridx] = value;
2143085a36d4SBarry Smith       a->nz++;
21442d61bbb3SSatish Balay       noinsert1:;
21452d61bbb3SSatish Balay       low = i;
21462d61bbb3SSatish Balay     }
21472d61bbb3SSatish Balay     ailen[brow] = nrow;
21482d61bbb3SSatish Balay   }
214988e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
21502d61bbb3SSatish Balay   PetscFunctionReturn(0);
21512d61bbb3SSatish Balay }
21522d61bbb3SSatish Balay 
21534a2ae208SSatish Balay #undef __FUNCT__
21544a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqBAIJ"
21550481f469SBarry Smith PetscErrorCode MatILUFactor_SeqBAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
21562d61bbb3SSatish Balay {
21572d61bbb3SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)inA->data;
21582d61bbb3SSatish Balay   Mat            outA;
2159dfbe8321SBarry Smith   PetscErrorCode ierr;
2160667159a5SBarry Smith   PetscTruth     row_identity,col_identity;
21612d61bbb3SSatish Balay 
21622d61bbb3SSatish Balay   PetscFunctionBegin;
2163e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for in-place ILU");
2164667159a5SBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2165667159a5SBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2166667159a5SBarry Smith   if (!row_identity || !col_identity) {
2167e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for in-place ILU");
2168667159a5SBarry Smith   }
21692d61bbb3SSatish Balay 
21702d61bbb3SSatish Balay   outA            = inA;
2171d5f3da31SBarry Smith   inA->factortype = MAT_FACTOR_LU;
21722d61bbb3SSatish Balay 
2173c4992f7dSBarry Smith   ierr = MatMarkDiagonal_SeqBAIJ(inA);CHKERRQ(ierr);
2174cf242676SKris Buschelman 
2175c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
2176c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr); }
2177c3122656SLisandro Dalcin   a->row = row;
2178c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
2179c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr); }
2180c3122656SLisandro Dalcin   a->col = col;
2181c38d4ed2SBarry Smith 
2182c38d4ed2SBarry Smith   /* Create the invert permutation so that it can be used in MatLUFactorNumeric() */
2183a23ff555SBarry Smith   if (a->icol) {
2184a23ff555SBarry Smith     ierr = ISDestroy(a->icol);CHKERRQ(ierr);
2185a23ff555SBarry Smith   }
21864c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
218752e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2188c38d4ed2SBarry Smith 
21898b1456e3SHong Zhang   ierr = MatSeqBAIJSetNumericFactorization_inplace(inA,(PetscTruth)(row_identity && col_identity));CHKERRQ(ierr);
2190c38d4ed2SBarry Smith   if (!a->solve_work) {
2191d0f46423SBarry Smith     ierr = PetscMalloc((inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2192d0f46423SBarry Smith     ierr = PetscLogObjectMemory(inA,(inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar));CHKERRQ(ierr);
2193c38d4ed2SBarry Smith   }
2194719d5645SBarry Smith   ierr = MatLUFactorNumeric(outA,inA,info);CHKERRQ(ierr);
2195667159a5SBarry Smith 
21962d61bbb3SSatish Balay   PetscFunctionReturn(0);
21972d61bbb3SSatish Balay }
2198d9b7c43dSSatish Balay 
2199fb2e594dSBarry Smith EXTERN_C_BEGIN
22004a2ae208SSatish Balay #undef __FUNCT__
22014a2ae208SSatish Balay #define __FUNCT__ "MatSeqBAIJSetColumnIndices_SeqBAIJ"
2202be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetColumnIndices_SeqBAIJ(Mat mat,PetscInt *indices)
220327a8da17SBarry Smith {
220427a8da17SBarry Smith   Mat_SeqBAIJ *baij = (Mat_SeqBAIJ *)mat->data;
2205bdb1c0e1SJed Brown   PetscInt    i,nz,mbs;
220627a8da17SBarry Smith 
220727a8da17SBarry Smith   PetscFunctionBegin;
220814db4f74SSatish Balay   nz  = baij->maxnz/baij->bs2;
2209bdb1c0e1SJed Brown   mbs = baij->mbs;
221027a8da17SBarry Smith   for (i=0; i<nz; i++) {
221127a8da17SBarry Smith     baij->j[i] = indices[i];
221227a8da17SBarry Smith   }
221327a8da17SBarry Smith   baij->nz = nz;
2214bdb1c0e1SJed Brown   for (i=0; i<mbs; i++) {
221527a8da17SBarry Smith     baij->ilen[i] = baij->imax[i];
221627a8da17SBarry Smith   }
221727a8da17SBarry Smith   PetscFunctionReturn(0);
221827a8da17SBarry Smith }
2219fb2e594dSBarry Smith EXTERN_C_END
222027a8da17SBarry Smith 
22214a2ae208SSatish Balay #undef __FUNCT__
22224a2ae208SSatish Balay #define __FUNCT__ "MatSeqBAIJSetColumnIndices"
222327a8da17SBarry Smith /*@
222427a8da17SBarry Smith     MatSeqBAIJSetColumnIndices - Set the column indices for all the rows
222527a8da17SBarry Smith        in the matrix.
222627a8da17SBarry Smith 
222727a8da17SBarry Smith   Input Parameters:
222827a8da17SBarry Smith +  mat - the SeqBAIJ matrix
222927a8da17SBarry Smith -  indices - the column indices
223027a8da17SBarry Smith 
223115091d37SBarry Smith   Level: advanced
223215091d37SBarry Smith 
223327a8da17SBarry Smith   Notes:
223427a8da17SBarry Smith     This can be called if you have precomputed the nonzero structure of the
223527a8da17SBarry Smith   matrix and want to provide it to the matrix object to improve the performance
223627a8da17SBarry Smith   of the MatSetValues() operation.
223727a8da17SBarry Smith 
223827a8da17SBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2239d1be2dadSMatthew Knepley   MatCreateSeqBAIJ(), and the columns indices MUST be sorted.
224027a8da17SBarry Smith 
224127a8da17SBarry Smith     MUST be called before any calls to MatSetValues();
224227a8da17SBarry Smith 
224327a8da17SBarry Smith @*/
2244be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetColumnIndices(Mat mat,PetscInt *indices)
224527a8da17SBarry Smith {
2246c1ac3661SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
224727a8da17SBarry Smith 
224827a8da17SBarry Smith   PetscFunctionBegin;
22490700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
22504482741eSBarry Smith   PetscValidPointer(indices,2);
2251c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqBAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
225227a8da17SBarry Smith   if (f) {
225327a8da17SBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2254e7e72b3dSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Wrong type of matrix to set column indices");
225527a8da17SBarry Smith   PetscFunctionReturn(0);
225627a8da17SBarry Smith }
225727a8da17SBarry Smith 
22584a2ae208SSatish Balay #undef __FUNCT__
2259985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqBAIJ"
2260985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqBAIJ(Mat A,Vec v,PetscInt idx[])
2261273d9f13SBarry Smith {
2262273d9f13SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
2263dfbe8321SBarry Smith   PetscErrorCode ierr;
2264c1ac3661SBarry Smith   PetscInt       i,j,n,row,bs,*ai,*aj,mbs;
2265273d9f13SBarry Smith   PetscReal      atmp;
226687828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
2267273d9f13SBarry Smith   MatScalar      *aa;
2268c1ac3661SBarry Smith   PetscInt       ncols,brow,krow,kcol;
2269273d9f13SBarry Smith 
2270273d9f13SBarry Smith   PetscFunctionBegin;
2271e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2272d0f46423SBarry Smith   bs   = A->rmap->bs;
2273273d9f13SBarry Smith   aa   = a->a;
2274273d9f13SBarry Smith   ai   = a->i;
2275273d9f13SBarry Smith   aj   = a->j;
2276273d9f13SBarry Smith   mbs  = a->mbs;
2277273d9f13SBarry Smith 
22782dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
22791ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2280273d9f13SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2281e32f2f54SBarry Smith   if (n != A->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2282273d9f13SBarry Smith   for (i=0; i<mbs; i++) {
2283273d9f13SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2284273d9f13SBarry Smith     brow  = bs*i;
2285273d9f13SBarry Smith     for (j=0; j<ncols; j++){
2286273d9f13SBarry Smith       for (kcol=0; kcol<bs; kcol++){
2287273d9f13SBarry Smith         for (krow=0; krow<bs; krow++){
2288273d9f13SBarry Smith           atmp = PetscAbsScalar(*aa);aa++;
2289273d9f13SBarry Smith           row = brow + krow;    /* row index */
2290a83599f4SBarry Smith           /* printf("val[%d,%d]: %G\n",row,bcol+kcol,atmp); */
2291985db425SBarry Smith           if (PetscAbsScalar(x[row]) < atmp) {x[row] = atmp; if (idx) idx[row] = bs*(*aj) + kcol;}
2292273d9f13SBarry Smith         }
2293273d9f13SBarry Smith       }
2294273d9f13SBarry Smith       aj++;
2295273d9f13SBarry Smith     }
2296273d9f13SBarry Smith   }
22971ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2298273d9f13SBarry Smith   PetscFunctionReturn(0);
2299273d9f13SBarry Smith }
2300273d9f13SBarry Smith 
23014a2ae208SSatish Balay #undef __FUNCT__
23023c896bc6SHong Zhang #define __FUNCT__ "MatCopy_SeqBAIJ"
23033c896bc6SHong Zhang PetscErrorCode MatCopy_SeqBAIJ(Mat A,Mat B,MatStructure str)
23043c896bc6SHong Zhang {
23053c896bc6SHong Zhang   PetscErrorCode ierr;
23063c896bc6SHong Zhang 
23073c896bc6SHong Zhang   PetscFunctionBegin;
23083c896bc6SHong Zhang   /* If the two matrices have the same copy implementation, use fast copy. */
23093c896bc6SHong Zhang   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
23103c896bc6SHong Zhang     Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
23113c896bc6SHong Zhang     Mat_SeqBAIJ *b = (Mat_SeqBAIJ*)B->data;
23123c896bc6SHong Zhang 
2313e7e72b3dSBarry 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");
2314d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->N])*sizeof(PetscScalar));CHKERRQ(ierr);
23153c896bc6SHong Zhang   } else {
23163c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
23173c896bc6SHong Zhang   }
23183c896bc6SHong Zhang   PetscFunctionReturn(0);
23193c896bc6SHong Zhang }
23203c896bc6SHong Zhang 
23213c896bc6SHong Zhang #undef __FUNCT__
23224a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqBAIJ"
2323dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqBAIJ(Mat A)
2324273d9f13SBarry Smith {
2325dfbe8321SBarry Smith   PetscErrorCode ierr;
2326273d9f13SBarry Smith 
2327273d9f13SBarry Smith   PetscFunctionBegin;
2328db4efbfdSBarry Smith   ierr =  MatSeqBAIJSetPreallocation_SeqBAIJ(A,-PetscMax(A->rmap->bs,1),PETSC_DEFAULT,0);CHKERRQ(ierr);
2329273d9f13SBarry Smith   PetscFunctionReturn(0);
2330273d9f13SBarry Smith }
2331273d9f13SBarry Smith 
23324a2ae208SSatish Balay #undef __FUNCT__
23334a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqBAIJ"
2334dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqBAIJ(Mat A,PetscScalar *array[])
2335f2a5309cSSatish Balay {
2336f2a5309cSSatish Balay   Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
2337f2a5309cSSatish Balay   PetscFunctionBegin;
2338f2a5309cSSatish Balay   *array = a->a;
2339f2a5309cSSatish Balay   PetscFunctionReturn(0);
2340f2a5309cSSatish Balay }
2341f2a5309cSSatish Balay 
23424a2ae208SSatish Balay #undef __FUNCT__
23434a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqBAIJ"
2344dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqBAIJ(Mat A,PetscScalar *array[])
2345f2a5309cSSatish Balay {
2346f2a5309cSSatish Balay   PetscFunctionBegin;
2347f2a5309cSSatish Balay   PetscFunctionReturn(0);
2348f2a5309cSSatish Balay }
2349f2a5309cSSatish Balay 
235042ee4b1aSHong Zhang #undef __FUNCT__
235142ee4b1aSHong Zhang #define __FUNCT__ "MatAXPY_SeqBAIJ"
2352f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
235342ee4b1aSHong Zhang {
235442ee4b1aSHong Zhang   Mat_SeqBAIJ    *x  = (Mat_SeqBAIJ *)X->data,*y = (Mat_SeqBAIJ *)Y->data;
2355dfbe8321SBarry Smith   PetscErrorCode ierr;
2356d0f46423SBarry Smith   PetscInt       i,bs=Y->rmap->bs,j,bs2;
23570805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
235842ee4b1aSHong Zhang 
235942ee4b1aSHong Zhang   PetscFunctionBegin;
236042ee4b1aSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
2361f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2362f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2363c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2364c4319e64SHong Zhang     if (y->xtoy && y->XtoY != X) {
2365c4319e64SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2366c4319e64SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2367c537a176SHong Zhang     }
2368c4319e64SHong Zhang     if (!y->xtoy) { /* get xtoy */
2369c4319e64SHong Zhang       ierr = MatAXPYGetxtoy_Private(x->mbs,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2370c4319e64SHong Zhang       y->XtoY = X;
2371c009d632SSatish Balay       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2372c537a176SHong Zhang     }
2373c4319e64SHong Zhang     bs2 = bs*bs;
2374c537a176SHong Zhang     for (i=0; i<x->nz; i++) {
2375c4319e64SHong Zhang       j = 0;
2376c4319e64SHong Zhang       while (j < bs2){
2377f4df32b1SMatthew Knepley         y->a[bs2*y->xtoy[i]+j] += a*(x->a[bs2*i+j]);
2378c4319e64SHong Zhang         j++;
2379c537a176SHong Zhang       }
2380c4319e64SHong Zhang     }
23811e2582c4SBarry 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);
238242ee4b1aSHong Zhang   } else {
2383f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
238442ee4b1aSHong Zhang   }
238542ee4b1aSHong Zhang   PetscFunctionReturn(0);
238642ee4b1aSHong Zhang }
238742ee4b1aSHong Zhang 
238899cafbc1SBarry Smith #undef __FUNCT__
238941c166b1SJed Brown #define __FUNCT__ "MatSetBlockSize_SeqBAIJ"
239041c166b1SJed Brown PetscErrorCode MatSetBlockSize_SeqBAIJ(Mat A,PetscInt bs)
239141c166b1SJed Brown {
239241c166b1SJed Brown   PetscInt rbs,cbs;
239341c166b1SJed Brown   PetscErrorCode ierr;
239441c166b1SJed Brown 
239541c166b1SJed Brown   PetscFunctionBegin;
239641c166b1SJed Brown   ierr = PetscLayoutGetBlockSize(A->rmap,&rbs);CHKERRQ(ierr);
239741c166b1SJed Brown   ierr = PetscLayoutGetBlockSize(A->cmap,&cbs);CHKERRQ(ierr);
2398e32f2f54SBarry Smith   if (rbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with BAIJ %d",bs,rbs);
2399e32f2f54SBarry Smith   if (cbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with BAIJ %d",bs,cbs);
240041c166b1SJed Brown   PetscFunctionReturn(0);
240141c166b1SJed Brown }
240241c166b1SJed Brown 
240341c166b1SJed Brown #undef __FUNCT__
240499cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqBAIJ"
240599cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqBAIJ(Mat A)
240699cafbc1SBarry Smith {
240799cafbc1SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
240899cafbc1SBarry Smith   PetscInt       i,nz = a->bs2*a->i[a->mbs];
2409dd6ea824SBarry Smith   MatScalar      *aa = a->a;
241099cafbc1SBarry Smith 
241199cafbc1SBarry Smith   PetscFunctionBegin;
241299cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
241399cafbc1SBarry Smith   PetscFunctionReturn(0);
241499cafbc1SBarry Smith }
241599cafbc1SBarry Smith 
241699cafbc1SBarry Smith #undef __FUNCT__
241799cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqBAIJ"
241899cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqBAIJ(Mat A)
241999cafbc1SBarry Smith {
242099cafbc1SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
242199cafbc1SBarry Smith   PetscInt       i,nz = a->bs2*a->i[a->mbs];
2422dd6ea824SBarry Smith   MatScalar      *aa = a->a;
242399cafbc1SBarry Smith 
242499cafbc1SBarry Smith   PetscFunctionBegin;
242599cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
242699cafbc1SBarry Smith   PetscFunctionReturn(0);
242799cafbc1SBarry Smith }
242899cafbc1SBarry Smith 
24293acb8795SBarry Smith extern PetscErrorCode MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
24303acb8795SBarry Smith 
24313acb8795SBarry Smith #undef __FUNCT__
24323acb8795SBarry Smith #define __FUNCT__ "MatGetColumnIJ_SeqBAIJ"
24333acb8795SBarry Smith /*
24343acb8795SBarry Smith     Code almost idential to MatGetColumnIJ_SeqAIJ() should share common code
24353acb8795SBarry Smith */
24363acb8795SBarry Smith PetscErrorCode MatGetColumnIJ_SeqBAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
24373acb8795SBarry Smith {
24383acb8795SBarry Smith   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)A->data;
24393acb8795SBarry Smith   PetscErrorCode ierr;
24403acb8795SBarry Smith   PetscInt       bs = A->rmap->bs,i,*collengths,*cia,*cja,n = A->cmap->n/bs,m = A->rmap->n/bs;
24413acb8795SBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
24423acb8795SBarry Smith 
24433acb8795SBarry Smith   PetscFunctionBegin;
24443acb8795SBarry Smith   *nn = n;
24453acb8795SBarry Smith   if (!ia) PetscFunctionReturn(0);
2446e7e72b3dSBarry Smith   if (symmetric) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not for BAIJ matrices");
2447e7e72b3dSBarry Smith   else {
24483acb8795SBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
24493acb8795SBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
24503acb8795SBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
24513acb8795SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
24523acb8795SBarry Smith     jj = a->j;
24533acb8795SBarry Smith     for (i=0; i<nz; i++) {
24543acb8795SBarry Smith       collengths[jj[i]]++;
24553acb8795SBarry Smith     }
24563acb8795SBarry Smith     cia[0] = oshift;
24573acb8795SBarry Smith     for (i=0; i<n; i++) {
24583acb8795SBarry Smith       cia[i+1] = cia[i] + collengths[i];
24593acb8795SBarry Smith     }
24603acb8795SBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
24613acb8795SBarry Smith     jj   = a->j;
24623acb8795SBarry Smith     for (row=0; row<m; row++) {
24633acb8795SBarry Smith       mr = a->i[row+1] - a->i[row];
24643acb8795SBarry Smith       for (i=0; i<mr; i++) {
24653acb8795SBarry Smith         col = *jj++;
24663acb8795SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
24673acb8795SBarry Smith       }
24683acb8795SBarry Smith     }
24693acb8795SBarry Smith     ierr = PetscFree(collengths);CHKERRQ(ierr);
24703acb8795SBarry Smith     *ia = cia; *ja = cja;
24713acb8795SBarry Smith   }
24723acb8795SBarry Smith   PetscFunctionReturn(0);
24733acb8795SBarry Smith }
24743acb8795SBarry Smith 
24753acb8795SBarry Smith #undef __FUNCT__
24763acb8795SBarry Smith #define __FUNCT__ "MatRestoreColumnIJ_SeqBAIJ"
24773acb8795SBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqBAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
24783acb8795SBarry Smith {
24793acb8795SBarry Smith   PetscErrorCode ierr;
24803acb8795SBarry Smith 
24813acb8795SBarry Smith   PetscFunctionBegin;
24823acb8795SBarry Smith   if (!ia) PetscFunctionReturn(0);
24833acb8795SBarry Smith   ierr = PetscFree(*ia);CHKERRQ(ierr);
24843acb8795SBarry Smith   ierr = PetscFree(*ja);CHKERRQ(ierr);
24853acb8795SBarry Smith   PetscFunctionReturn(0);
24863acb8795SBarry Smith }
24873acb8795SBarry Smith 
2488f6d58c54SBarry Smith #undef __FUNCT__
2489f6d58c54SBarry Smith #define __FUNCT__ "MatFDColoringApply_BAIJ"
2490f6d58c54SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_BAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2491f6d58c54SBarry Smith {
2492f6d58c54SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
2493f6d58c54SBarry Smith   PetscErrorCode ierr;
2494f6d58c54SBarry Smith   PetscInt       bs = J->rmap->bs,i,j,k,start,end,l,row,col,*srows,**vscaleforrow,m1,m2;
2495f6d58c54SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
2496f6d58c54SBarry Smith   PetscScalar    *vscale_array;
2497f6d58c54SBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin,unorm;
2498f6d58c54SBarry Smith   Vec            w1=coloring->w1,w2=coloring->w2,w3;
2499f6d58c54SBarry Smith   void           *fctx = coloring->fctx;
2500f6d58c54SBarry Smith   PetscTruth     flg = PETSC_FALSE;
2501f6d58c54SBarry Smith   PetscInt       ctype=coloring->ctype,N,col_start=0,col_end=0;
2502f6d58c54SBarry Smith   Vec            x1_tmp;
2503f6d58c54SBarry Smith 
2504f6d58c54SBarry Smith   PetscFunctionBegin;
25050700a824SBarry Smith   PetscValidHeaderSpecific(J,MAT_CLASSID,1);
25060700a824SBarry Smith   PetscValidHeaderSpecific(coloring,MAT_FDCOLORING_CLASSID,2);
25070700a824SBarry Smith   PetscValidHeaderSpecific(x1,VEC_CLASSID,3);
2508e32f2f54SBarry Smith   if (!f) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatFDColoringSetFunction()");
2509f6d58c54SBarry Smith 
2510f6d58c54SBarry Smith   ierr = PetscLogEventBegin(MAT_FDColoringApply,coloring,J,x1,0);CHKERRQ(ierr);
2511f6d58c54SBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
2512f6d58c54SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2513f6d58c54SBarry Smith   if (flg) {
2514f6d58c54SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2515f6d58c54SBarry Smith   } else {
2516f6d58c54SBarry Smith     PetscTruth assembled;
2517f6d58c54SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
2518f6d58c54SBarry Smith     if (assembled) {
2519f6d58c54SBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2520f6d58c54SBarry Smith     }
2521f6d58c54SBarry Smith   }
2522f6d58c54SBarry Smith 
2523f6d58c54SBarry Smith   x1_tmp = x1;
2524f6d58c54SBarry Smith   if (!coloring->vscale){
2525f6d58c54SBarry Smith     ierr = VecDuplicate(x1_tmp,&coloring->vscale);CHKERRQ(ierr);
2526f6d58c54SBarry Smith   }
2527f6d58c54SBarry Smith 
2528f6d58c54SBarry Smith   /*
2529f6d58c54SBarry Smith     This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2530f6d58c54SBarry Smith     coloring->F for the coarser grids from the finest
2531f6d58c54SBarry Smith   */
2532f6d58c54SBarry Smith   if (coloring->F) {
2533f6d58c54SBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2534f6d58c54SBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2535f6d58c54SBarry Smith     if (m1 != m2) {
2536f6d58c54SBarry Smith       coloring->F = 0;
2537f6d58c54SBarry Smith       }
2538f6d58c54SBarry Smith     }
2539f6d58c54SBarry Smith 
2540f6d58c54SBarry Smith   if (coloring->htype[0] == 'w') { /* tacky test; need to make systematic if we add other approaches to computing h*/
2541f6d58c54SBarry Smith     ierr = VecNorm(x1_tmp,NORM_2,&unorm);CHKERRQ(ierr);
2542f6d58c54SBarry Smith   }
2543f6d58c54SBarry Smith   ierr = VecGetOwnershipRange(w1,&start,&end);CHKERRQ(ierr); /* OwnershipRange is used by ghosted x! */
2544f6d58c54SBarry Smith 
2545f6d58c54SBarry Smith   /* Set w1 = F(x1) */
2546f6d58c54SBarry Smith   if (coloring->F) {
2547f6d58c54SBarry Smith     w1          = coloring->F; /* use already computed value of function */
2548f6d58c54SBarry Smith     coloring->F = 0;
2549f6d58c54SBarry Smith   } else {
2550f6d58c54SBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2551f6d58c54SBarry Smith     ierr = (*f)(sctx,x1_tmp,w1,fctx);CHKERRQ(ierr);
2552f6d58c54SBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2553f6d58c54SBarry Smith   }
2554f6d58c54SBarry Smith 
2555f6d58c54SBarry Smith   if (!coloring->w3) {
2556f6d58c54SBarry Smith     ierr = VecDuplicate(x1_tmp,&coloring->w3);CHKERRQ(ierr);
2557f6d58c54SBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2558f6d58c54SBarry Smith   }
2559f6d58c54SBarry Smith   w3 = coloring->w3;
2560f6d58c54SBarry Smith 
2561f6d58c54SBarry Smith     CHKMEMQ;
2562f6d58c54SBarry Smith     /* Compute all the local scale factors, including ghost points */
2563f6d58c54SBarry Smith   ierr = VecGetLocalSize(x1_tmp,&N);CHKERRQ(ierr);
2564f6d58c54SBarry Smith   ierr = VecGetArray(x1_tmp,&xx);CHKERRQ(ierr);
2565f6d58c54SBarry Smith   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2566f6d58c54SBarry Smith   if (ctype == IS_COLORING_GHOSTED){
2567f6d58c54SBarry Smith     col_start = 0; col_end = N;
2568f6d58c54SBarry Smith   } else if (ctype == IS_COLORING_GLOBAL){
2569f6d58c54SBarry Smith     xx = xx - start;
2570f6d58c54SBarry Smith     vscale_array = vscale_array - start;
2571f6d58c54SBarry Smith     col_start = start; col_end = N + start;
2572f6d58c54SBarry Smith   }    CHKMEMQ;
2573f6d58c54SBarry Smith   for (col=col_start; col<col_end; col++){
2574f6d58c54SBarry Smith     /* Loop over each local column, vscale[col] = 1./(epsilon*dx[col]) */
2575f6d58c54SBarry Smith     if (coloring->htype[0] == 'w') {
2576f6d58c54SBarry Smith       dx = 1.0 + unorm;
2577f6d58c54SBarry Smith     } else {
2578f6d58c54SBarry Smith       dx  = xx[col];
2579f6d58c54SBarry Smith     }
2580f6d58c54SBarry Smith     if (dx == 0.0) dx = 1.0;
2581f6d58c54SBarry Smith #if !defined(PETSC_USE_COMPLEX)
2582f6d58c54SBarry Smith     if (dx < umin && dx >= 0.0)      dx = umin;
2583f6d58c54SBarry Smith     else if (dx < 0.0 && dx > -umin) dx = -umin;
2584f6d58c54SBarry Smith #else
2585f6d58c54SBarry Smith     if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2586f6d58c54SBarry Smith     else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2587f6d58c54SBarry Smith #endif
2588f6d58c54SBarry Smith     dx               *= epsilon;
2589f6d58c54SBarry Smith     vscale_array[col] = 1.0/dx;
2590f6d58c54SBarry Smith   }     CHKMEMQ;
2591f6d58c54SBarry Smith   if (ctype == IS_COLORING_GLOBAL)  vscale_array = vscale_array + start;
2592f6d58c54SBarry Smith   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2593f6d58c54SBarry Smith   if (ctype == IS_COLORING_GLOBAL){
2594f6d58c54SBarry Smith     ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2595f6d58c54SBarry Smith     ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2596f6d58c54SBarry Smith   }
2597f6d58c54SBarry Smith   CHKMEMQ;
2598f6d58c54SBarry Smith   if (coloring->vscaleforrow) {
2599f6d58c54SBarry Smith     vscaleforrow = coloring->vscaleforrow;
2600e7e72b3dSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Null Object: coloring->vscaleforrow");
2601f6d58c54SBarry Smith 
2602f6d58c54SBarry Smith   ierr = PetscMalloc(bs*sizeof(PetscInt),&srows);CHKERRQ(ierr);
2603f6d58c54SBarry Smith   /*
2604f6d58c54SBarry Smith     Loop over each color
2605f6d58c54SBarry Smith   */
2606f6d58c54SBarry Smith   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2607f6d58c54SBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2608f6d58c54SBarry Smith     coloring->currentcolor = k;
2609f6d58c54SBarry Smith     for (i=0; i<bs; i++) {
2610f6d58c54SBarry Smith       ierr = VecCopy(x1_tmp,w3);CHKERRQ(ierr);
2611f6d58c54SBarry Smith       ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);
2612f6d58c54SBarry Smith       if (ctype == IS_COLORING_GLOBAL) w3_array = w3_array - start;
2613f6d58c54SBarry Smith       /*
2614f6d58c54SBarry Smith 	Loop over each column associated with color
2615f6d58c54SBarry Smith 	adding the perturbation to the vector w3.
2616f6d58c54SBarry Smith       */
2617f6d58c54SBarry Smith       for (l=0; l<coloring->ncolumns[k]; l++) {
2618f6d58c54SBarry Smith 	col = i + bs*coloring->columns[k][l];    /* local column of the matrix we are probing for */
2619f6d58c54SBarry Smith 	if (coloring->htype[0] == 'w') {
2620f6d58c54SBarry Smith 	  dx = 1.0 + unorm;
2621f6d58c54SBarry Smith 	} else {
2622f6d58c54SBarry Smith 	  dx  = xx[col];
2623f6d58c54SBarry Smith 	}
2624f6d58c54SBarry Smith 	if (dx == 0.0) dx = 1.0;
2625f6d58c54SBarry Smith #if !defined(PETSC_USE_COMPLEX)
2626f6d58c54SBarry Smith 	if (dx < umin && dx >= 0.0)      dx = umin;
2627f6d58c54SBarry Smith 	else if (dx < 0.0 && dx > -umin) dx = -umin;
2628f6d58c54SBarry Smith #else
2629f6d58c54SBarry Smith 	if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2630f6d58c54SBarry Smith 	else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2631f6d58c54SBarry Smith #endif
2632f6d58c54SBarry Smith 	dx            *= epsilon;
2633e32f2f54SBarry Smith 	if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2634f6d58c54SBarry Smith 	w3_array[col] += dx;
2635f6d58c54SBarry Smith       }
2636f6d58c54SBarry Smith       if (ctype == IS_COLORING_GLOBAL) w3_array = w3_array + start;
2637f6d58c54SBarry Smith       ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2638f6d58c54SBarry Smith 
2639f6d58c54SBarry Smith       /*
2640f6d58c54SBarry Smith 	Evaluate function at w3 = x1 + dx (here dx is a vector of perturbations)
2641f6d58c54SBarry Smith 	w2 = F(x1 + dx) - F(x1)
2642f6d58c54SBarry Smith       */
2643f6d58c54SBarry Smith       ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2644f6d58c54SBarry Smith       ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
2645f6d58c54SBarry Smith       ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2646f6d58c54SBarry Smith       ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2647f6d58c54SBarry Smith 
2648f6d58c54SBarry Smith       /*
2649f6d58c54SBarry Smith 	Loop over rows of vector, putting results into Jacobian matrix
2650f6d58c54SBarry Smith       */
2651f6d58c54SBarry Smith       ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2652f6d58c54SBarry Smith       for (l=0; l<coloring->nrows[k]; l++) {
2653f6d58c54SBarry Smith 	row    = bs*coloring->rows[k][l];             /* local row index */
2654f6d58c54SBarry Smith 	col    = i + bs*coloring->columnsforrow[k][l];    /* global column index */
2655f6d58c54SBarry Smith         for (j=0; j<bs; j++) {
2656f6d58c54SBarry Smith   	  y[row+j] *= vscale_array[j+bs*vscaleforrow[k][l]];
2657f6d58c54SBarry Smith           srows[j]  = row + start + j;
2658f6d58c54SBarry Smith         }
2659f6d58c54SBarry Smith 	ierr   = MatSetValues(J,bs,srows,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2660f6d58c54SBarry Smith       }
2661f6d58c54SBarry Smith       ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2662f6d58c54SBarry Smith     }
2663f6d58c54SBarry Smith   } /* endof for each color */
2664f6d58c54SBarry Smith   if (ctype == IS_COLORING_GLOBAL) xx = xx + start;
2665f6d58c54SBarry Smith   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2666f6d58c54SBarry Smith   ierr = VecRestoreArray(x1_tmp,&xx);CHKERRQ(ierr);
2667f6d58c54SBarry Smith   ierr = PetscFree(srows);CHKERRQ(ierr);
2668f6d58c54SBarry Smith 
2669f6d58c54SBarry Smith   coloring->currentcolor = -1;
2670f6d58c54SBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2671f6d58c54SBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2672f6d58c54SBarry Smith   ierr = PetscLogEventEnd(MAT_FDColoringApply,coloring,J,x1,0);CHKERRQ(ierr);
2673f6d58c54SBarry Smith   PetscFunctionReturn(0);
2674f6d58c54SBarry Smith }
267599cafbc1SBarry Smith 
26762593348eSBarry Smith /* -------------------------------------------------------------------*/
2677cc2dc46cSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqBAIJ,
2678cc2dc46cSBarry Smith        MatGetRow_SeqBAIJ,
2679cc2dc46cSBarry Smith        MatRestoreRow_SeqBAIJ,
2680cc2dc46cSBarry Smith        MatMult_SeqBAIJ_N,
268197304618SKris Buschelman /* 4*/ MatMultAdd_SeqBAIJ_N,
26827c922b88SBarry Smith        MatMultTranspose_SeqBAIJ,
26837c922b88SBarry Smith        MatMultTransposeAdd_SeqBAIJ,
2684db4efbfdSBarry Smith        0,
2685cc2dc46cSBarry Smith        0,
2686cc2dc46cSBarry Smith        0,
268797304618SKris Buschelman /*10*/ 0,
2688cc2dc46cSBarry Smith        MatLUFactor_SeqBAIJ,
2689cc2dc46cSBarry Smith        0,
2690b6490206SBarry Smith        0,
2691f2501298SSatish Balay        MatTranspose_SeqBAIJ,
269297304618SKris Buschelman /*15*/ MatGetInfo_SeqBAIJ,
2693cc2dc46cSBarry Smith        MatEqual_SeqBAIJ,
2694cc2dc46cSBarry Smith        MatGetDiagonal_SeqBAIJ,
2695cc2dc46cSBarry Smith        MatDiagonalScale_SeqBAIJ,
2696cc2dc46cSBarry Smith        MatNorm_SeqBAIJ,
269797304618SKris Buschelman /*20*/ 0,
2698cc2dc46cSBarry Smith        MatAssemblyEnd_SeqBAIJ,
2699cc2dc46cSBarry Smith        MatSetOption_SeqBAIJ,
2700cc2dc46cSBarry Smith        MatZeroEntries_SeqBAIJ,
2701d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqBAIJ,
2702db4efbfdSBarry Smith        0,
2703db4efbfdSBarry Smith        0,
2704db4efbfdSBarry Smith        0,
2705db4efbfdSBarry Smith        0,
2706d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqBAIJ,
2707db4efbfdSBarry Smith        0,
2708db4efbfdSBarry Smith        0,
2709f2a5309cSSatish Balay        MatGetArray_SeqBAIJ,
2710f2a5309cSSatish Balay        MatRestoreArray_SeqBAIJ,
2711d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqBAIJ,
2712cc2dc46cSBarry Smith        0,
2713cc2dc46cSBarry Smith        0,
2714cc2dc46cSBarry Smith        MatILUFactor_SeqBAIJ,
2715cc2dc46cSBarry Smith        0,
2716d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqBAIJ,
2717cc2dc46cSBarry Smith        MatGetSubMatrices_SeqBAIJ,
2718cc2dc46cSBarry Smith        MatIncreaseOverlap_SeqBAIJ,
2719cc2dc46cSBarry Smith        MatGetValues_SeqBAIJ,
27203c896bc6SHong Zhang        MatCopy_SeqBAIJ,
2721d519adbfSMatthew Knepley /*44*/ 0,
2722cc2dc46cSBarry Smith        MatScale_SeqBAIJ,
2723cc2dc46cSBarry Smith        0,
2724cc2dc46cSBarry Smith        0,
2725fe97e370SBarry Smith        0,
272641c166b1SJed Brown /*49*/ MatSetBlockSize_SeqBAIJ,
27273b2fbd54SBarry Smith        MatGetRowIJ_SeqBAIJ,
272892c4ed94SBarry Smith        MatRestoreRowIJ_SeqBAIJ,
27293acb8795SBarry Smith        MatGetColumnIJ_SeqBAIJ,
27303acb8795SBarry Smith        MatRestoreColumnIJ_SeqBAIJ,
27313acb8795SBarry Smith /*54*/ MatFDColoringCreate_SeqAIJ,
2732cc2dc46cSBarry Smith        0,
2733cc2dc46cSBarry Smith        0,
2734cc2dc46cSBarry Smith        0,
2735d3825aa8SBarry Smith        MatSetValuesBlocked_SeqBAIJ,
2736d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_SeqBAIJ,
2737b9b97703SBarry Smith        MatDestroy_SeqBAIJ,
2738b9b97703SBarry Smith        MatView_SeqBAIJ,
2739357abbc8SBarry Smith        0,
2740273d9f13SBarry Smith        0,
2741d519adbfSMatthew Knepley /*64*/ 0,
2742273d9f13SBarry Smith        0,
2743273d9f13SBarry Smith        0,
2744273d9f13SBarry Smith        0,
2745273d9f13SBarry Smith        0,
2746d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqBAIJ,
2747273d9f13SBarry Smith        0,
2748c87e5d42SMatthew Knepley        MatConvert_Basic,
274997304618SKris Buschelman        0,
275097304618SKris Buschelman        0,
2751d519adbfSMatthew Knepley /*74*/ 0,
2752f6d58c54SBarry Smith        MatFDColoringApply_BAIJ,
275397304618SKris Buschelman        0,
275497304618SKris Buschelman        0,
275597304618SKris Buschelman        0,
2756d519adbfSMatthew Knepley /*79*/ 0,
275797304618SKris Buschelman        0,
275897304618SKris Buschelman        0,
275997304618SKris Buschelman        0,
27605bba2384SShri Abhyankar        MatLoad_SeqBAIJ,
2761d519adbfSMatthew Knepley /*84*/ 0,
2762b01c7715SBarry Smith        0,
2763b01c7715SBarry Smith        0,
2764b01c7715SBarry Smith        0,
2765865e5f61SKris Buschelman        0,
2766d519adbfSMatthew Knepley /*89*/ 0,
2767865e5f61SKris Buschelman        0,
2768865e5f61SKris Buschelman        0,
2769865e5f61SKris Buschelman        0,
2770865e5f61SKris Buschelman        0,
2771d519adbfSMatthew Knepley /*94*/ 0,
2772865e5f61SKris Buschelman        0,
2773865e5f61SKris Buschelman        0,
277499cafbc1SBarry Smith        0,
277599cafbc1SBarry Smith        0,
2776d519adbfSMatthew Knepley /*99*/0,
277799cafbc1SBarry Smith        0,
277899cafbc1SBarry Smith        0,
277999cafbc1SBarry Smith        0,
278099cafbc1SBarry Smith        0,
2781d519adbfSMatthew Knepley /*104*/0,
278299cafbc1SBarry Smith        MatRealPart_SeqBAIJ,
27832af78befSBarry Smith        MatImaginaryPart_SeqBAIJ,
27842af78befSBarry Smith        0,
27852af78befSBarry Smith        0,
2786d519adbfSMatthew Knepley /*109*/0,
27872af78befSBarry Smith        0,
27882af78befSBarry Smith        0,
27892af78befSBarry Smith        0,
2790547795f9SHong Zhang        MatMissingDiagonal_SeqBAIJ,
2791547795f9SHong Zhang /*114*/0,
2792547795f9SHong Zhang        0,
2793547795f9SHong Zhang        0,
2794547795f9SHong Zhang        0,
2795547795f9SHong Zhang        0,
2796547795f9SHong Zhang /*119*/0,
2797547795f9SHong Zhang        0,
2798547795f9SHong Zhang        MatMultHermitianTranspose_SeqBAIJ,
27995bba2384SShri Abhyankar        MatMultHermitianTransposeAdd_SeqBAIJ
280099cafbc1SBarry Smith };
28012593348eSBarry Smith 
28023e90b805SBarry Smith EXTERN_C_BEGIN
28034a2ae208SSatish Balay #undef __FUNCT__
28044a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqBAIJ"
2805be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqBAIJ(Mat mat)
28063e90b805SBarry Smith {
28073e90b805SBarry Smith   Mat_SeqBAIJ    *aij = (Mat_SeqBAIJ *)mat->data;
2808d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
2809dfbe8321SBarry Smith   PetscErrorCode ierr;
28103e90b805SBarry Smith 
28113e90b805SBarry Smith   PetscFunctionBegin;
2812e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
28133e90b805SBarry Smith 
28143e90b805SBarry Smith   /* allocate space for values if not already there */
28153e90b805SBarry Smith   if (!aij->saved_values) {
281687828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
28171784c0f5SBarry Smith     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
28183e90b805SBarry Smith   }
28193e90b805SBarry Smith 
28203e90b805SBarry Smith   /* copy values over */
282187828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
28223e90b805SBarry Smith   PetscFunctionReturn(0);
28233e90b805SBarry Smith }
28243e90b805SBarry Smith EXTERN_C_END
28253e90b805SBarry Smith 
28263e90b805SBarry Smith EXTERN_C_BEGIN
28274a2ae208SSatish Balay #undef __FUNCT__
28284a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqBAIJ"
2829be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqBAIJ(Mat mat)
28303e90b805SBarry Smith {
28313e90b805SBarry Smith   Mat_SeqBAIJ    *aij = (Mat_SeqBAIJ *)mat->data;
28326849ba73SBarry Smith   PetscErrorCode ierr;
2833d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
28343e90b805SBarry Smith 
28353e90b805SBarry Smith   PetscFunctionBegin;
2836e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2837e7e72b3dSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
28383e90b805SBarry Smith 
28393e90b805SBarry Smith   /* copy values over */
284087828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
28413e90b805SBarry Smith   PetscFunctionReturn(0);
28423e90b805SBarry Smith }
28433e90b805SBarry Smith EXTERN_C_END
28443e90b805SBarry Smith 
2845273d9f13SBarry Smith EXTERN_C_BEGIN
2846f69a0ea3SMatthew Knepley extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
2847f69a0ea3SMatthew Knepley extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqBAIJ_SeqSBAIJ(Mat, MatType,MatReuse,Mat*);
2848273d9f13SBarry Smith EXTERN_C_END
2849273d9f13SBarry Smith 
2850273d9f13SBarry Smith EXTERN_C_BEGIN
28514a2ae208SSatish Balay #undef __FUNCT__
2852a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqBAIJSetPreallocation_SeqBAIJ"
2853be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetPreallocation_SeqBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
2854a23d5eceSKris Buschelman {
2855a23d5eceSKris Buschelman   Mat_SeqBAIJ    *b;
28566849ba73SBarry Smith   PetscErrorCode ierr;
2857db4efbfdSBarry Smith   PetscInt       i,mbs,nbs,bs2,newbs = PetscAbs(bs);
2858ab93d7beSBarry Smith   PetscTruth     flg,skipallocation = PETSC_FALSE;
2859a23d5eceSKris Buschelman 
2860a23d5eceSKris Buschelman   PetscFunctionBegin;
2861a23d5eceSKris Buschelman 
2862ab93d7beSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2863ab93d7beSBarry Smith     skipallocation = PETSC_TRUE;
2864ab93d7beSBarry Smith     nz             = 0;
2865ab93d7beSBarry Smith   }
28668c07d4e3SBarry Smith 
2867db4efbfdSBarry Smith   if (bs < 0) {
28687adad957SLisandro Dalcin     ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Block options for SEQBAIJ matrix 1","Mat");CHKERRQ(ierr);
2869db4efbfdSBarry Smith       ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatSeqBAIJSetPreallocation",newbs,&newbs,PETSC_NULL);CHKERRQ(ierr);
28708c07d4e3SBarry Smith     ierr = PetscOptionsEnd();CHKERRQ(ierr);
2871db4efbfdSBarry Smith     bs   = PetscAbs(bs);
2872db4efbfdSBarry Smith   }
2873e7e72b3dSBarry Smith   if (nnz && newbs != bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot change blocksize from command line if setting nnz");
2874a23d5eceSKris Buschelman   bs   = newbs;
2875a23d5eceSKris Buschelman 
287626283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
287726283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
287826283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
287926283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2880899cda47SBarry Smith 
2881899cda47SBarry Smith   B->preallocated = PETSC_TRUE;
2882899cda47SBarry Smith 
2883d0f46423SBarry Smith   mbs  = B->rmap->n/bs;
2884d0f46423SBarry Smith   nbs  = B->cmap->n/bs;
2885a23d5eceSKris Buschelman   bs2  = bs*bs;
2886a23d5eceSKris Buschelman 
288765e19b50SBarry Smith   if (mbs*bs!=B->rmap->n || nbs*bs!=B->cmap->n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number rows %D, cols %D must be divisible by blocksize %D",B->rmap->N,B->cmap->n,bs);
2888a23d5eceSKris Buschelman 
2889a23d5eceSKris Buschelman   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2890e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
2891a23d5eceSKris Buschelman   if (nnz) {
2892a23d5eceSKris Buschelman     for (i=0; i<mbs; i++) {
2893e32f2f54SBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %D value %D",i,nnz[i]);
2894e32f2f54SBarry Smith       if (nnz[i] > nbs) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than block row length: local row %D value %D rowlength %D",i,nnz[i],nbs);
2895a23d5eceSKris Buschelman     }
2896a23d5eceSKris Buschelman   }
2897a23d5eceSKris Buschelman 
2898a23d5eceSKris Buschelman   b       = (Mat_SeqBAIJ*)B->data;
28997adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)B)->comm,PETSC_NULL,"Optimize options for SEQBAIJ matrix 2 ","Mat");CHKERRQ(ierr);
29008c07d4e3SBarry Smith     ierr = PetscOptionsTruth("-mat_no_unroll","Do not optimize for block size (slow)",PETSC_NULL,PETSC_FALSE,&flg,PETSC_NULL);CHKERRQ(ierr);
29018c07d4e3SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
29028c07d4e3SBarry Smith 
2903a23d5eceSKris Buschelman   if (!flg) {
2904a23d5eceSKris Buschelman     switch (bs) {
2905a23d5eceSKris Buschelman     case 1:
2906a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_1;
2907a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_1;
290841f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_1;
2909a23d5eceSKris Buschelman       break;
2910a23d5eceSKris Buschelman     case 2:
2911a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_2;
2912a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_2;
291341f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_2;
2914a23d5eceSKris Buschelman       break;
2915a23d5eceSKris Buschelman     case 3:
2916a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_3;
2917a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_3;
291841f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_3;
2919a23d5eceSKris Buschelman       break;
2920a23d5eceSKris Buschelman     case 4:
2921a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_4;
2922a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_4;
292341f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_4;
2924a23d5eceSKris Buschelman       break;
2925a23d5eceSKris Buschelman     case 5:
2926a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_5;
2927a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_5;
292841f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_5;
2929a23d5eceSKris Buschelman       break;
2930a23d5eceSKris Buschelman     case 6:
2931a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_6;
2932a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_6;
293341f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_6;
2934a23d5eceSKris Buschelman       break;
2935a23d5eceSKris Buschelman     case 7:
2936a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_7;
2937a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_7;
293841f059aeSBarry Smith       B->ops->sor             = MatSOR_SeqBAIJ_7;
2939a23d5eceSKris Buschelman       break;
29408ab949d8SShri Abhyankar     case 15:
2941832cc040SShri Abhyankar       B->ops->mult = MatMult_SeqBAIJ_15_ver1;
29428ab949d8SShri Abhyankar       break;
2943a23d5eceSKris Buschelman     default:
2944a23d5eceSKris Buschelman       B->ops->mult            = MatMult_SeqBAIJ_N;
2945a23d5eceSKris Buschelman       B->ops->multadd         = MatMultAdd_SeqBAIJ_N;
2946a23d5eceSKris Buschelman       break;
2947a23d5eceSKris Buschelman     }
2948a23d5eceSKris Buschelman   }
2949d0f46423SBarry Smith   B->rmap->bs      = bs;
2950a23d5eceSKris Buschelman   b->mbs     = mbs;
2951a23d5eceSKris Buschelman   b->nbs     = nbs;
2952ab93d7beSBarry Smith   if (!skipallocation) {
29532ee49352SLisandro Dalcin     if (!b->imax) {
2954ab93d7beSBarry Smith       ierr = PetscMalloc2(mbs,PetscInt,&b->imax,mbs,PetscInt,&b->ilen);CHKERRQ(ierr);
29554fd072dbSBarry Smith       ierr = PetscLogObjectMemory(B,2*mbs*sizeof(PetscInt));
29564fd072dbSBarry Smith       b->free_imax_ilen = PETSC_TRUE;
29572ee49352SLisandro Dalcin     }
2958ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each block row so far. */
2959ab93d7beSBarry Smith     for (i=0; i<mbs; i++) { b->ilen[i] = 0;}
2960a23d5eceSKris Buschelman     if (!nnz) {
2961a23d5eceSKris Buschelman       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2962a23d5eceSKris Buschelman       else if (nz <= 0)        nz = 1;
2963a23d5eceSKris Buschelman       for (i=0; i<mbs; i++) b->imax[i] = nz;
2964a23d5eceSKris Buschelman       nz = nz*mbs;
2965a23d5eceSKris Buschelman     } else {
2966a23d5eceSKris Buschelman       nz = 0;
2967a23d5eceSKris Buschelman       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2968a23d5eceSKris Buschelman     }
2969a23d5eceSKris Buschelman 
2970a23d5eceSKris Buschelman     /* allocate the matrix space */
29712ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
2972d0f46423SBarry Smith     ierr = PetscMalloc3(bs2*nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->N+1,PetscInt,&b->i);CHKERRQ(ierr);
2973d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
2974a23d5eceSKris Buschelman     ierr  = PetscMemzero(b->a,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
2975c1ac3661SBarry Smith     ierr  = PetscMemzero(b->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
2976a23d5eceSKris Buschelman     b->singlemalloc = PETSC_TRUE;
2977a23d5eceSKris Buschelman     b->i[0] = 0;
2978a23d5eceSKris Buschelman     for (i=1; i<mbs+1; i++) {
2979a23d5eceSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
2980a23d5eceSKris Buschelman     }
2981e6b907acSBarry Smith     b->free_a     = PETSC_TRUE;
2982e6b907acSBarry Smith     b->free_ij    = PETSC_TRUE;
2983e811da20SHong Zhang   } else {
2984e6b907acSBarry Smith     b->free_a     = PETSC_FALSE;
2985e6b907acSBarry Smith     b->free_ij    = PETSC_FALSE;
2986ab93d7beSBarry Smith   }
2987a23d5eceSKris Buschelman 
2988d0f46423SBarry Smith   B->rmap->bs          = bs;
2989a23d5eceSKris Buschelman   b->bs2              = bs2;
2990a23d5eceSKris Buschelman   b->mbs              = mbs;
2991a23d5eceSKris Buschelman   b->nz               = 0;
2992a23d5eceSKris Buschelman   b->maxnz            = nz*bs2;
2993a23d5eceSKris Buschelman   B->info.nz_unneeded = (PetscReal)b->maxnz;
2994a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2995a23d5eceSKris Buschelman }
2996a23d5eceSKris Buschelman EXTERN_C_END
2997a23d5eceSKris Buschelman 
2998b24902e0SBarry Smith EXTERN_C_BEGIN
2999725b52f3SLisandro Dalcin #undef __FUNCT__
3000725b52f3SLisandro Dalcin #define __FUNCT__ "MatSeqBAIJSetPreallocationCSR_SeqBAIJ"
3001cf12db73SBarry Smith PetscErrorCode MatSeqBAIJSetPreallocationCSR_SeqBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
3002725b52f3SLisandro Dalcin {
3003725b52f3SLisandro Dalcin   PetscInt       i,m,nz,nz_max=0,*nnz;
3004725b52f3SLisandro Dalcin   PetscScalar    *values=0;
3005725b52f3SLisandro Dalcin   PetscErrorCode ierr;
3006725b52f3SLisandro Dalcin 
3007725b52f3SLisandro Dalcin   PetscFunctionBegin;
3008e32f2f54SBarry Smith   if (bs < 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
300926283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
301026283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
301126283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
301226283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3013d0f46423SBarry Smith   m = B->rmap->n/bs;
3014725b52f3SLisandro Dalcin 
3015e32f2f54SBarry Smith   if (ii[0] != 0) { SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "ii[0] must be 0 but it is %D",ii[0]); }
3016725b52f3SLisandro Dalcin   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3017725b52f3SLisandro Dalcin   for(i=0; i<m; i++) {
3018cf12db73SBarry Smith     nz = ii[i+1]- ii[i];
3019e32f2f54SBarry Smith     if (nz < 0) { SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D",i,nz); }
3020725b52f3SLisandro Dalcin     nz_max = PetscMax(nz_max, nz);
3021725b52f3SLisandro Dalcin     nnz[i] = nz;
3022725b52f3SLisandro Dalcin   }
3023725b52f3SLisandro Dalcin   ierr = MatSeqBAIJSetPreallocation(B,bs,0,nnz);CHKERRQ(ierr);
3024725b52f3SLisandro Dalcin   ierr = PetscFree(nnz);CHKERRQ(ierr);
3025725b52f3SLisandro Dalcin 
3026725b52f3SLisandro Dalcin   values = (PetscScalar*)V;
3027725b52f3SLisandro Dalcin   if (!values) {
3028725b52f3SLisandro Dalcin     ierr = PetscMalloc(bs*bs*(nz_max+1)*sizeof(PetscScalar),&values);CHKERRQ(ierr);
3029725b52f3SLisandro Dalcin     ierr = PetscMemzero(values,bs*bs*nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3030725b52f3SLisandro Dalcin   }
3031725b52f3SLisandro Dalcin   for (i=0; i<m; i++) {
3032cf12db73SBarry Smith     PetscInt          ncols  = ii[i+1] - ii[i];
3033cf12db73SBarry Smith     const PetscInt    *icols = jj + ii[i];
3034cf12db73SBarry Smith     const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
3035725b52f3SLisandro Dalcin     ierr = MatSetValuesBlocked_SeqBAIJ(B,1,&i,ncols,icols,svals,INSERT_VALUES);CHKERRQ(ierr);
3036725b52f3SLisandro Dalcin   }
3037725b52f3SLisandro Dalcin   if (!V) { ierr = PetscFree(values);CHKERRQ(ierr); }
3038725b52f3SLisandro Dalcin   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3039725b52f3SLisandro Dalcin   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3040725b52f3SLisandro Dalcin 
3041725b52f3SLisandro Dalcin   PetscFunctionReturn(0);
3042725b52f3SLisandro Dalcin }
3043725b52f3SLisandro Dalcin EXTERN_C_END
3044725b52f3SLisandro Dalcin 
3045725b52f3SLisandro Dalcin 
3046207126cbSBarry Smith EXTERN_C_BEGIN
3047b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqbaij_petsc(Mat,MatFactorType,Mat*);
304867877ebaSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
3049bccb9932SShri Abhyankar extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_baij_mumps(Mat,MatFactorType,Mat*);
305067877ebaSShri Abhyankar #endif
3051db4efbfdSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqbaij_petsc(Mat,MatFactorType,Mat*);
3052b24902e0SBarry Smith EXTERN_C_END
3053b24902e0SBarry Smith 
30540bad9183SKris Buschelman /*MC
3055fafad747SKris Buschelman    MATSEQBAIJ - MATSEQBAIJ = "seqbaij" - A matrix type to be used for sequential block sparse matrices, based on
30560bad9183SKris Buschelman    block sparse compressed row format.
30570bad9183SKris Buschelman 
30580bad9183SKris Buschelman    Options Database Keys:
30590bad9183SKris Buschelman . -mat_type seqbaij - sets the matrix type to "seqbaij" during a call to MatSetFromOptions()
30600bad9183SKris Buschelman 
30610bad9183SKris Buschelman   Level: beginner
30620bad9183SKris Buschelman 
3063f0c06035SSatish Balay .seealso: MatCreateSeqBAIJ()
30640bad9183SKris Buschelman M*/
30650bad9183SKris Buschelman 
3066b24902e0SBarry Smith 
3067a23d5eceSKris Buschelman EXTERN_C_BEGIN
3068a23d5eceSKris Buschelman #undef __FUNCT__
30694a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqBAIJ"
3070be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqBAIJ(Mat B)
30712593348eSBarry Smith {
3072dfbe8321SBarry Smith   PetscErrorCode ierr;
3073c1ac3661SBarry Smith   PetscMPIInt    size;
3074b6490206SBarry Smith   Mat_SeqBAIJ    *b;
30753b2fbd54SBarry Smith 
30763a40ed3dSBarry Smith   PetscFunctionBegin;
30777adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3078e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");
3079b6490206SBarry Smith 
308038f2d2fdSLisandro Dalcin   ierr    = PetscNewLog(B,Mat_SeqBAIJ,&b);CHKERRQ(ierr);
3081b0a32e0cSBarry Smith   B->data = (void*)b;
3082549d3d68SSatish Balay   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
308390f02eecSBarry Smith   B->mapping               = 0;
30842593348eSBarry Smith   b->row                   = 0;
30852593348eSBarry Smith   b->col                   = 0;
3086e51c0b9cSSatish Balay   b->icol                  = 0;
30872593348eSBarry Smith   b->reallocs              = 0;
30883e90b805SBarry Smith   b->saved_values          = 0;
30892593348eSBarry Smith 
3090c4992f7dSBarry Smith   b->roworiented           = PETSC_TRUE;
30912593348eSBarry Smith   b->nonew                 = 0;
30922593348eSBarry Smith   b->diag                  = 0;
30932593348eSBarry Smith   b->solve_work            = 0;
3094de6a44a3SBarry Smith   b->mult_work             = 0;
30952a1b7f2aSHong Zhang   B->spptr                 = 0;
30960e6d2581SBarry Smith   B->info.nz_unneeded      = (PetscReal)b->maxnz;
3097a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3098c4319e64SHong Zhang   b->xtoy                  = 0;
3099c4319e64SHong Zhang   b->XtoY                  = 0;
310073e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
310126e093fcSHong Zhang   b->compressedrow.nrows   = 0;
310273e7a558SHong Zhang   b->compressedrow.i       = PETSC_NULL;
310373e7a558SHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
310473e7a558SHong Zhang   b->compressedrow.checked = PETSC_FALSE;
310588e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
31064e220ebcSLois Curfman McInnes 
3107ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
3108db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqbaij_petsc",
3109db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqbaij_petsc);CHKERRQ(ierr);
3110ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
3111b24902e0SBarry Smith                                      "MatGetFactor_seqbaij_petsc",
3112b24902e0SBarry Smith                                      MatGetFactor_seqbaij_petsc);CHKERRQ(ierr);
311367877ebaSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
3114bccb9932SShri Abhyankar   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", "MatGetFactor_baij_mumps", MatGetFactor_baij_mumps);CHKERRQ(ierr);
311567877ebaSShri Abhyankar #endif
311643516a2dSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqBAIJInvertBlockDiagonal_C",
311743516a2dSKris Buschelman                                      "MatInvertBlockDiagonal_SeqBAIJ",
311843516a2dSKris Buschelman                                       MatInvertBlockDiagonal_SeqBAIJ);CHKERRQ(ierr);
3119f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
31203e90b805SBarry Smith                                      "MatStoreValues_SeqBAIJ",
3121bc4b532fSSatish Balay                                       MatStoreValues_SeqBAIJ);CHKERRQ(ierr);
3122f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
31233e90b805SBarry Smith                                      "MatRetrieveValues_SeqBAIJ",
3124bc4b532fSSatish Balay                                       MatRetrieveValues_SeqBAIJ);CHKERRQ(ierr);
3125f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqBAIJSetColumnIndices_C",
312627a8da17SBarry Smith                                      "MatSeqBAIJSetColumnIndices_SeqBAIJ",
3127bc4b532fSSatish Balay                                       MatSeqBAIJSetColumnIndices_SeqBAIJ);CHKERRQ(ierr);
3128a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqbaij_seqaij_C",
3129273d9f13SBarry Smith                                      "MatConvert_SeqBAIJ_SeqAIJ",
3130273d9f13SBarry Smith                                       MatConvert_SeqBAIJ_SeqAIJ);CHKERRQ(ierr);
3131a0e1a404SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqbaij_seqsbaij_C",
3132a0e1a404SHong Zhang                                      "MatConvert_SeqBAIJ_SeqSBAIJ",
3133a0e1a404SHong Zhang                                       MatConvert_SeqBAIJ_SeqSBAIJ);CHKERRQ(ierr);
3134a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqBAIJSetPreallocation_C",
3135a23d5eceSKris Buschelman                                      "MatSeqBAIJSetPreallocation_SeqBAIJ",
3136a23d5eceSKris Buschelman                                       MatSeqBAIJSetPreallocation_SeqBAIJ);CHKERRQ(ierr);
3137725b52f3SLisandro Dalcin   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqBAIJSetPreallocationCSR_C",
3138725b52f3SLisandro Dalcin                                      "MatSeqBAIJSetPreallocationCSR_SeqBAIJ",
3139725b52f3SLisandro Dalcin                                       MatSeqBAIJSetPreallocationCSR_SeqBAIJ);CHKERRQ(ierr);
314017667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQBAIJ);CHKERRQ(ierr);
31413a40ed3dSBarry Smith   PetscFunctionReturn(0);
31422593348eSBarry Smith }
3143273d9f13SBarry Smith EXTERN_C_END
31442593348eSBarry Smith 
31454a2ae208SSatish Balay #undef __FUNCT__
3146b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqBAIJ"
314716a2bf60SHong Zhang PetscErrorCode MatDuplicateNoCreate_SeqBAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscTruth mallocmatspace)
31482593348eSBarry Smith {
3149b24902e0SBarry Smith   Mat_SeqBAIJ    *c = (Mat_SeqBAIJ*)C->data,*a = (Mat_SeqBAIJ*)A->data;
31506849ba73SBarry Smith   PetscErrorCode ierr;
3151a96a251dSBarry Smith   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 = a->bs2;
3152de6a44a3SBarry Smith 
31533a40ed3dSBarry Smith   PetscFunctionBegin;
3154e32f2f54SBarry Smith   if (a->i[mbs] != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt matrix");
31552593348eSBarry Smith 
31564fd072dbSBarry Smith   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
31574fd072dbSBarry Smith     c->imax = a->imax;
31584fd072dbSBarry Smith     c->ilen = a->ilen;
31594fd072dbSBarry Smith     c->free_imax_ilen = PETSC_FALSE;
31604fd072dbSBarry Smith   } else {
316133b91e9fSSatish Balay     ierr = PetscMalloc2(mbs,PetscInt,&c->imax,mbs,PetscInt,&c->ilen);CHKERRQ(ierr);
31624fd072dbSBarry Smith     ierr = PetscLogObjectMemory(C,2*mbs*sizeof(PetscInt));CHKERRQ(ierr);
3163b6490206SBarry Smith     for (i=0; i<mbs; i++) {
31642593348eSBarry Smith       c->imax[i] = a->imax[i];
31652593348eSBarry Smith       c->ilen[i] = a->ilen[i];
31662593348eSBarry Smith     }
31674fd072dbSBarry Smith     c->free_imax_ilen = PETSC_TRUE;
31684fd072dbSBarry Smith   }
31692593348eSBarry Smith 
31702593348eSBarry Smith   /* allocate the matrix space */
317116a2bf60SHong Zhang   if (mallocmatspace){
31724fd072dbSBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
31734fd072dbSBarry Smith       ierr = PetscMalloc(bs2*nz*sizeof(PetscScalar),&c->a);CHKERRQ(ierr);
31744fd072dbSBarry Smith       ierr = PetscLogObjectMemory(C,a->i[mbs]*bs2*sizeof(PetscScalar));CHKERRQ(ierr);
31754fd072dbSBarry Smith       c->singlemalloc = PETSC_FALSE;
31764fd072dbSBarry Smith       c->free_ij      = PETSC_FALSE;
31774fd072dbSBarry Smith       c->i            = a->i;
31784fd072dbSBarry Smith       c->j            = a->j;
31794fd072dbSBarry Smith       c->parent       = A;
31804fd072dbSBarry Smith       ierr            = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
31814fd072dbSBarry Smith       ierr            = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
31824fd072dbSBarry Smith       ierr            = MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
31834fd072dbSBarry Smith     } else {
3184a96a251dSBarry Smith       ierr = PetscMalloc3(bs2*nz,PetscScalar,&c->a,nz,PetscInt,&c->j,mbs+1,PetscInt,&c->i);CHKERRQ(ierr);
318516a2bf60SHong Zhang       ierr = PetscLogObjectMemory(C,a->i[mbs]*(bs2*sizeof(PetscScalar)+sizeof(PetscInt))+(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
3186c4992f7dSBarry Smith       c->singlemalloc = PETSC_TRUE;
31874fd072dbSBarry Smith       c->free_ij      = PETSC_TRUE;
3188c1ac3661SBarry Smith       ierr = PetscMemcpy(c->i,a->i,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
3189b6490206SBarry Smith       if (mbs > 0) {
3190c1ac3661SBarry Smith 	ierr = PetscMemcpy(c->j,a->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
31912e8a6d31SBarry Smith 	if (cpvalues == MAT_COPY_VALUES) {
3192549d3d68SSatish Balay 	  ierr = PetscMemcpy(c->a,a->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
31932e8a6d31SBarry Smith 	} else {
3194549d3d68SSatish Balay 	  ierr = PetscMemzero(c->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
31952593348eSBarry Smith 	}
31962593348eSBarry Smith       }
319716a2bf60SHong Zhang     }
31984fd072dbSBarry Smith   }
319916a2bf60SHong Zhang 
32002593348eSBarry Smith   c->roworiented = a->roworiented;
32012593348eSBarry Smith   c->nonew       = a->nonew;
320226283091SBarry Smith   ierr = PetscLayoutCopy(A->rmap,&C->rmap);CHKERRQ(ierr);
320326283091SBarry Smith   ierr = PetscLayoutCopy(A->cmap,&C->cmap);CHKERRQ(ierr);
32045c9eb25fSBarry Smith   c->bs2         = a->bs2;
32055c9eb25fSBarry Smith   c->mbs         = a->mbs;
32065c9eb25fSBarry Smith   c->nbs         = a->nbs;
32072593348eSBarry Smith 
32082593348eSBarry Smith   if (a->diag) {
32094fd072dbSBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
32104fd072dbSBarry Smith       c->diag      = a->diag;
32114fd072dbSBarry Smith       c->free_diag = PETSC_FALSE;
32124fd072dbSBarry Smith     } else {
3213c1ac3661SBarry Smith       ierr = PetscMalloc((mbs+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
321452e6d16bSBarry Smith       ierr = PetscLogObjectMemory(C,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
3215b6490206SBarry Smith       for (i=0; i<mbs; i++) {
32162593348eSBarry Smith         c->diag[i] = a->diag[i];
32172593348eSBarry Smith       }
32184fd072dbSBarry Smith       c->free_diag = PETSC_TRUE;
32194fd072dbSBarry Smith     }
322098305bb5SBarry Smith   } else c->diag        = 0;
32212593348eSBarry Smith   c->nz                 = a->nz;
32228e9a0fb8SHong Zhang   c->maxnz              = bs2*a->nz; /* Since we allocate exactly the right amount */
32232593348eSBarry Smith   c->solve_work         = 0;
32247fc0212eSBarry Smith   c->mult_work          = 0;
3225e6b907acSBarry Smith   c->free_a             = PETSC_TRUE;
3226e6b907acSBarry Smith   c->free_ij            = PETSC_TRUE;
3227273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3228273d9f13SBarry Smith   C->assembled          = PETSC_TRUE;
322988e51ccdSHong Zhang 
323088e51ccdSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
323188e51ccdSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
323288e51ccdSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
323388e51ccdSHong Zhang   if (a->compressedrow.checked && a->compressedrow.use){
323488e51ccdSHong Zhang     i = a->compressedrow.nrows;
32350e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i+1,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
32364fd072dbSBarry Smith     ierr = PetscLogObjectMemory(C,(2*i+1)*sizeof(PetscInt));CHKERRQ(ierr);
323788e51ccdSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
323888e51ccdSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
323988e51ccdSHong Zhang   } else {
324088e51ccdSHong Zhang     c->compressedrow.use    = PETSC_FALSE;
324188e51ccdSHong Zhang     c->compressedrow.i      = PETSC_NULL;
324288e51ccdSHong Zhang     c->compressedrow.rindex = PETSC_NULL;
324388e51ccdSHong Zhang   }
324488e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
32457adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
32465d5aaa0eSBarry Smith   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
32473a40ed3dSBarry Smith   PetscFunctionReturn(0);
32482593348eSBarry Smith }
32492593348eSBarry Smith 
32504a2ae208SSatish Balay #undef __FUNCT__
3251b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqBAIJ"
3252b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3253b24902e0SBarry Smith {
3254b24902e0SBarry Smith     PetscErrorCode ierr;
3255b24902e0SBarry Smith 
3256b24902e0SBarry Smith   PetscFunctionBegin;
32575c9eb25fSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
3258d0f46423SBarry Smith   ierr = MatSetSizes(*B,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);CHKERRQ(ierr);
32595c9eb25fSBarry Smith   ierr = MatSetType(*B,MATSEQBAIJ);CHKERRQ(ierr);
326016a2bf60SHong Zhang   ierr = MatDuplicateNoCreate_SeqBAIJ(*B,A,cpvalues,PETSC_TRUE);
3261b24902e0SBarry Smith   PetscFunctionReturn(0);
3262b24902e0SBarry Smith }
3263b24902e0SBarry Smith 
3264b24902e0SBarry Smith #undef __FUNCT__
32655bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_SeqBAIJ"
3266*112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqBAIJ(Mat newmat,PetscViewer viewer)
3267f501eaabSShri Abhyankar {
3268f501eaabSShri Abhyankar   Mat_SeqBAIJ    *a;
3269f501eaabSShri Abhyankar   PetscErrorCode ierr;
3270f501eaabSShri Abhyankar   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs=1;
3271f501eaabSShri Abhyankar   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*browlengths,maskcount;
3272f501eaabSShri Abhyankar   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows,rows,cols;
3273f501eaabSShri Abhyankar   PetscInt       *masked,nmask,tmp,bs2,ishift;
3274f501eaabSShri Abhyankar   PetscMPIInt    size;
3275f501eaabSShri Abhyankar   int            fd;
3276f501eaabSShri Abhyankar   PetscScalar    *aa;
3277f501eaabSShri Abhyankar   MPI_Comm       comm = ((PetscObject)viewer)->comm;
3278f501eaabSShri Abhyankar 
3279f501eaabSShri Abhyankar   PetscFunctionBegin;
3280f501eaabSShri Abhyankar   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading SEQBAIJ matrix","Mat");CHKERRQ(ierr);
3281f501eaabSShri Abhyankar   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
3282f501eaabSShri Abhyankar   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3283f501eaabSShri Abhyankar   bs2  = bs*bs;
3284f501eaabSShri Abhyankar 
3285f501eaabSShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3286f501eaabSShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
3287f501eaabSShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
3288f501eaabSShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3289f501eaabSShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
3290f501eaabSShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
3291f501eaabSShri Abhyankar 
3292f501eaabSShri Abhyankar   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqBAIJ");
3293f501eaabSShri Abhyankar   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
3294f501eaabSShri Abhyankar 
3295f501eaabSShri Abhyankar   /*
3296f501eaabSShri Abhyankar      This code adds extra rows to make sure the number of rows is
3297f501eaabSShri Abhyankar     divisible by the blocksize
3298f501eaabSShri Abhyankar   */
3299f501eaabSShri Abhyankar   mbs        = M/bs;
3300f501eaabSShri Abhyankar   extra_rows = bs - M + bs*(mbs);
3301f501eaabSShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
3302f501eaabSShri Abhyankar   else                  mbs++;
3303f501eaabSShri Abhyankar   if (extra_rows) {
3304f501eaabSShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
3305f501eaabSShri Abhyankar   }
3306f501eaabSShri Abhyankar 
3307f501eaabSShri Abhyankar   /* Set global sizes if not already set */
3308f501eaabSShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
3309f501eaabSShri Abhyankar     ierr = MatSetSizes(newmat,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
3310f501eaabSShri Abhyankar   } else { /* Check if the matrix global sizes are correct */
3311f501eaabSShri Abhyankar     ierr = MatGetSize(newmat,&rows,&cols);CHKERRQ(ierr);
3312f501eaabSShri 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);
3313f501eaabSShri Abhyankar   }
3314f501eaabSShri Abhyankar 
3315f501eaabSShri Abhyankar   /* read in row lengths */
3316f501eaabSShri Abhyankar   ierr = PetscMalloc((M+extra_rows)*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
3317f501eaabSShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
3318f501eaabSShri Abhyankar   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
3319f501eaabSShri Abhyankar 
3320f501eaabSShri Abhyankar   /* read in column indices */
3321f501eaabSShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscInt),&jj);CHKERRQ(ierr);
3322f501eaabSShri Abhyankar   ierr = PetscBinaryRead(fd,jj,nz,PETSC_INT);CHKERRQ(ierr);
3323f501eaabSShri Abhyankar   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;
3324f501eaabSShri Abhyankar 
3325f501eaabSShri Abhyankar   /* loop over row lengths determining block row lengths */
3326f501eaabSShri Abhyankar   ierr     = PetscMalloc(mbs*sizeof(PetscInt),&browlengths);CHKERRQ(ierr);
3327f501eaabSShri Abhyankar   ierr     = PetscMemzero(browlengths,mbs*sizeof(PetscInt));CHKERRQ(ierr);
3328f501eaabSShri Abhyankar   ierr     = PetscMalloc2(mbs,PetscInt,&mask,mbs,PetscInt,&masked);CHKERRQ(ierr);
3329f501eaabSShri Abhyankar   ierr     = PetscMemzero(mask,mbs*sizeof(PetscInt));CHKERRQ(ierr);
3330f501eaabSShri Abhyankar   rowcount = 0;
3331f501eaabSShri Abhyankar   nzcount = 0;
3332f501eaabSShri Abhyankar   for (i=0; i<mbs; i++) {
3333f501eaabSShri Abhyankar     nmask = 0;
3334f501eaabSShri Abhyankar     for (j=0; j<bs; j++) {
3335f501eaabSShri Abhyankar       kmax = rowlengths[rowcount];
3336f501eaabSShri Abhyankar       for (k=0; k<kmax; k++) {
3337f501eaabSShri Abhyankar         tmp = jj[nzcount++]/bs;
3338f501eaabSShri Abhyankar         if (!mask[tmp]) {masked[nmask++] = tmp; mask[tmp] = 1;}
3339f501eaabSShri Abhyankar       }
3340f501eaabSShri Abhyankar       rowcount++;
3341f501eaabSShri Abhyankar     }
3342f501eaabSShri Abhyankar     browlengths[i] += nmask;
3343f501eaabSShri Abhyankar     /* zero out the mask elements we set */
3344f501eaabSShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
3345f501eaabSShri Abhyankar   }
3346f501eaabSShri Abhyankar 
33472f480046SShri Abhyankar   /* Do preallocation  */
3348f501eaabSShri Abhyankar   ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(newmat,bs,0,browlengths);CHKERRQ(ierr);
3349f501eaabSShri Abhyankar   a = (Mat_SeqBAIJ*)newmat->data;
3350f501eaabSShri Abhyankar 
3351f501eaabSShri Abhyankar   /* set matrix "i" values */
3352f501eaabSShri Abhyankar   a->i[0] = 0;
3353f501eaabSShri Abhyankar   for (i=1; i<= mbs; i++) {
3354f501eaabSShri Abhyankar     a->i[i]      = a->i[i-1] + browlengths[i-1];
3355f501eaabSShri Abhyankar     a->ilen[i-1] = browlengths[i-1];
3356f501eaabSShri Abhyankar   }
3357f501eaabSShri Abhyankar   a->nz         = 0;
3358f501eaabSShri Abhyankar   for (i=0; i<mbs; i++) a->nz += browlengths[i];
3359f501eaabSShri Abhyankar 
3360f501eaabSShri Abhyankar   /* read in nonzero values */
3361f501eaabSShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscScalar),&aa);CHKERRQ(ierr);
3362f501eaabSShri Abhyankar   ierr = PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);CHKERRQ(ierr);
3363f501eaabSShri Abhyankar   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;
3364f501eaabSShri Abhyankar 
3365f501eaabSShri Abhyankar   /* set "a" and "j" values into matrix */
3366f501eaabSShri Abhyankar   nzcount = 0; jcount = 0;
3367f501eaabSShri Abhyankar   for (i=0; i<mbs; i++) {
3368f501eaabSShri Abhyankar     nzcountb = nzcount;
3369f501eaabSShri Abhyankar     nmask    = 0;
3370f501eaabSShri Abhyankar     for (j=0; j<bs; j++) {
3371f501eaabSShri Abhyankar       kmax = rowlengths[i*bs+j];
3372f501eaabSShri Abhyankar       for (k=0; k<kmax; k++) {
3373f501eaabSShri Abhyankar         tmp = jj[nzcount++]/bs;
3374f501eaabSShri Abhyankar 	if (!mask[tmp]) { masked[nmask++] = tmp; mask[tmp] = 1;}
3375f501eaabSShri Abhyankar       }
3376f501eaabSShri Abhyankar     }
3377f501eaabSShri Abhyankar     /* sort the masked values */
3378f501eaabSShri Abhyankar     ierr = PetscSortInt(nmask,masked);CHKERRQ(ierr);
3379f501eaabSShri Abhyankar 
3380f501eaabSShri Abhyankar     /* set "j" values into matrix */
3381f501eaabSShri Abhyankar     maskcount = 1;
3382f501eaabSShri Abhyankar     for (j=0; j<nmask; j++) {
3383f501eaabSShri Abhyankar       a->j[jcount++]  = masked[j];
3384f501eaabSShri Abhyankar       mask[masked[j]] = maskcount++;
3385f501eaabSShri Abhyankar     }
3386f501eaabSShri Abhyankar     /* set "a" values into matrix */
3387f501eaabSShri Abhyankar     ishift = bs2*a->i[i];
3388f501eaabSShri Abhyankar     for (j=0; j<bs; j++) {
3389f501eaabSShri Abhyankar       kmax = rowlengths[i*bs+j];
3390f501eaabSShri Abhyankar       for (k=0; k<kmax; k++) {
3391f501eaabSShri Abhyankar         tmp       = jj[nzcountb]/bs ;
3392f501eaabSShri Abhyankar         block     = mask[tmp] - 1;
3393f501eaabSShri Abhyankar         point     = jj[nzcountb] - bs*tmp;
3394f501eaabSShri Abhyankar         idx       = ishift + bs2*block + j + bs*point;
3395f501eaabSShri Abhyankar         a->a[idx] = (MatScalar)aa[nzcountb++];
3396f501eaabSShri Abhyankar       }
3397f501eaabSShri Abhyankar     }
3398f501eaabSShri Abhyankar     /* zero out the mask elements we set */
3399f501eaabSShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
3400f501eaabSShri Abhyankar   }
3401f501eaabSShri Abhyankar   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");
3402f501eaabSShri Abhyankar 
3403f501eaabSShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3404f501eaabSShri Abhyankar   ierr = PetscFree(browlengths);CHKERRQ(ierr);
3405f501eaabSShri Abhyankar   ierr = PetscFree(aa);CHKERRQ(ierr);
3406f501eaabSShri Abhyankar   ierr = PetscFree(jj);CHKERRQ(ierr);
3407f501eaabSShri Abhyankar   ierr = PetscFree2(mask,masked);CHKERRQ(ierr);
3408f501eaabSShri Abhyankar 
3409f501eaabSShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3410f501eaabSShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3411f501eaabSShri Abhyankar   ierr = MatView_Private(newmat);CHKERRQ(ierr);
3412f501eaabSShri Abhyankar   PetscFunctionReturn(0);
3413f501eaabSShri Abhyankar }
3414f501eaabSShri Abhyankar 
3415f501eaabSShri Abhyankar #undef __FUNCT__
34164a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqBAIJ"
3417273d9f13SBarry Smith /*@C
3418273d9f13SBarry Smith    MatCreateSeqBAIJ - Creates a sparse matrix in block AIJ (block
3419273d9f13SBarry Smith    compressed row) format.  For good matrix assembly performance the
3420273d9f13SBarry Smith    user should preallocate the matrix storage by setting the parameter nz
3421273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3422273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
34232593348eSBarry Smith 
3424273d9f13SBarry Smith    Collective on MPI_Comm
3425273d9f13SBarry Smith 
3426273d9f13SBarry Smith    Input Parameters:
3427273d9f13SBarry Smith +  comm - MPI communicator, set to PETSC_COMM_SELF
3428273d9f13SBarry Smith .  bs - size of block
3429273d9f13SBarry Smith .  m - number of rows
3430273d9f13SBarry Smith .  n - number of columns
343135d8aa7fSBarry Smith .  nz - number of nonzero blocks  per block row (same for all rows)
343235d8aa7fSBarry Smith -  nnz - array containing the number of nonzero blocks in the various block rows
3433273d9f13SBarry Smith          (possibly different for each block row) or PETSC_NULL
3434273d9f13SBarry Smith 
3435273d9f13SBarry Smith    Output Parameter:
3436273d9f13SBarry Smith .  A - the matrix
3437273d9f13SBarry Smith 
3438175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3439ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3440175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3441175b88e8SBarry Smith 
3442273d9f13SBarry Smith    Options Database Keys:
3443273d9f13SBarry Smith .   -mat_no_unroll - uses code that does not unroll the loops in the
3444273d9f13SBarry Smith                      block calculations (much slower)
3445273d9f13SBarry Smith .    -mat_block_size - size of the blocks to use
3446273d9f13SBarry Smith 
3447273d9f13SBarry Smith    Level: intermediate
3448273d9f13SBarry Smith 
3449273d9f13SBarry Smith    Notes:
3450d1be2dadSMatthew Knepley    The number of rows and columns must be divisible by blocksize.
3451d1be2dadSMatthew Knepley 
345249a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
345349a6f317SBarry Smith 
345435d8aa7fSBarry Smith    A nonzero block is any block that as 1 or more nonzeros in it
345535d8aa7fSBarry Smith 
3456273d9f13SBarry Smith    The block AIJ format is fully compatible with standard Fortran 77
3457273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3458273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3459273d9f13SBarry Smith 
3460273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3461273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3462273d9f13SBarry Smith    allocation.  For additional details, see the users manual chapter on
3463273d9f13SBarry Smith    matrices.
3464273d9f13SBarry Smith 
3465273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIBAIJ()
3466273d9f13SBarry Smith @*/
3467be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
3468273d9f13SBarry Smith {
3469dfbe8321SBarry Smith   PetscErrorCode ierr;
3470273d9f13SBarry Smith 
3471273d9f13SBarry Smith   PetscFunctionBegin;
3472f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3473f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3474273d9f13SBarry Smith   ierr = MatSetType(*A,MATSEQBAIJ);CHKERRQ(ierr);
3475ab93d7beSBarry Smith   ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(*A,bs,nz,(PetscInt*)nnz);CHKERRQ(ierr);
3476273d9f13SBarry Smith   PetscFunctionReturn(0);
3477273d9f13SBarry Smith }
3478273d9f13SBarry Smith 
34794a2ae208SSatish Balay #undef __FUNCT__
34804a2ae208SSatish Balay #define __FUNCT__ "MatSeqBAIJSetPreallocation"
3481273d9f13SBarry Smith /*@C
3482273d9f13SBarry Smith    MatSeqBAIJSetPreallocation - Sets the block size and expected nonzeros
3483273d9f13SBarry Smith    per row in the matrix. For good matrix assembly performance the
3484273d9f13SBarry Smith    user should preallocate the matrix storage by setting the parameter nz
3485273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3486273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3487273d9f13SBarry Smith 
3488273d9f13SBarry Smith    Collective on MPI_Comm
3489273d9f13SBarry Smith 
3490273d9f13SBarry Smith    Input Parameters:
3491273d9f13SBarry Smith +  A - the matrix
3492273d9f13SBarry Smith .  bs - size of block
3493273d9f13SBarry Smith .  nz - number of block nonzeros per block row (same for all rows)
3494273d9f13SBarry Smith -  nnz - array containing the number of block nonzeros in the various block rows
3495273d9f13SBarry Smith          (possibly different for each block row) or PETSC_NULL
3496273d9f13SBarry Smith 
3497273d9f13SBarry Smith    Options Database Keys:
3498273d9f13SBarry Smith .   -mat_no_unroll - uses code that does not unroll the loops in the
3499273d9f13SBarry Smith                      block calculations (much slower)
3500273d9f13SBarry Smith .    -mat_block_size - size of the blocks to use
3501273d9f13SBarry Smith 
3502273d9f13SBarry Smith    Level: intermediate
3503273d9f13SBarry Smith 
3504273d9f13SBarry Smith    Notes:
350549a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
350649a6f317SBarry Smith 
3507aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3508aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3509aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3510aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3511aa95bbe8SBarry Smith 
3512273d9f13SBarry Smith    The block AIJ format is fully compatible with standard Fortran 77
3513273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3514273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3515273d9f13SBarry Smith 
3516273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3517273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3518273d9f13SBarry Smith    allocation.  For additional details, see the users manual chapter on
3519273d9f13SBarry Smith    matrices.
3520273d9f13SBarry Smith 
3521aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIBAIJ(), MatGetInfo()
3522273d9f13SBarry Smith @*/
3523be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
3524273d9f13SBarry Smith {
3525c1ac3661SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,PetscInt,const PetscInt[]);
3526273d9f13SBarry Smith 
3527273d9f13SBarry Smith   PetscFunctionBegin;
3528a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqBAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
3529a23d5eceSKris Buschelman   if (f) {
3530a23d5eceSKris Buschelman     ierr = (*f)(B,bs,nz,nnz);CHKERRQ(ierr);
3531273d9f13SBarry Smith   }
3532273d9f13SBarry Smith   PetscFunctionReturn(0);
3533273d9f13SBarry Smith }
3534a1d92eedSBarry Smith 
3535c75a6043SHong Zhang #undef __FUNCT__
3536725b52f3SLisandro Dalcin #define __FUNCT__ "MatSeqBAIJSetPreallocationCSR"
3537725b52f3SLisandro Dalcin /*@C
3538725b52f3SLisandro Dalcin    MatSeqBAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format
3539725b52f3SLisandro Dalcin    (the default sequential PETSc format).
3540725b52f3SLisandro Dalcin 
3541725b52f3SLisandro Dalcin    Collective on MPI_Comm
3542725b52f3SLisandro Dalcin 
3543725b52f3SLisandro Dalcin    Input Parameters:
3544725b52f3SLisandro Dalcin +  A - the matrix
3545725b52f3SLisandro Dalcin .  i - the indices into j for the start of each local row (starts with zero)
3546725b52f3SLisandro Dalcin .  j - the column indices for each local row (starts with zero) these must be sorted for each row
3547725b52f3SLisandro Dalcin -  v - optional values in the matrix
3548725b52f3SLisandro Dalcin 
3549725b52f3SLisandro Dalcin    Level: developer
3550725b52f3SLisandro Dalcin 
3551725b52f3SLisandro Dalcin .keywords: matrix, aij, compressed row, sparse
3552725b52f3SLisandro Dalcin 
3553725b52f3SLisandro Dalcin .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatSeqBAIJSetPreallocation(), MATSEQBAIJ
3554725b52f3SLisandro Dalcin @*/
3555725b52f3SLisandro Dalcin PetscErrorCode PETSCMAT_DLLEXPORT MatSeqBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3556725b52f3SLisandro Dalcin {
3557725b52f3SLisandro Dalcin   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]);
3558725b52f3SLisandro Dalcin 
3559725b52f3SLisandro Dalcin   PetscFunctionBegin;
3560725b52f3SLisandro Dalcin   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqBAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
3561725b52f3SLisandro Dalcin   if (f) {
3562725b52f3SLisandro Dalcin     ierr = (*f)(B,bs,i,j,v);CHKERRQ(ierr);
3563725b52f3SLisandro Dalcin   }
3564725b52f3SLisandro Dalcin   PetscFunctionReturn(0);
3565725b52f3SLisandro Dalcin }
3566725b52f3SLisandro Dalcin 
3567725b52f3SLisandro Dalcin 
3568725b52f3SLisandro Dalcin #undef __FUNCT__
3569c75a6043SHong Zhang #define __FUNCT__ "MatCreateSeqBAIJWithArrays"
3570c75a6043SHong Zhang /*@
3571c75a6043SHong Zhang      MatCreateSeqBAIJWithArrays - Creates an sequential BAIJ matrix using matrix elements
3572c75a6043SHong Zhang               (upper triangular entries in CSR format) provided by the user.
3573c75a6043SHong Zhang 
3574c75a6043SHong Zhang      Collective on MPI_Comm
3575c75a6043SHong Zhang 
3576c75a6043SHong Zhang    Input Parameters:
3577c75a6043SHong Zhang +  comm - must be an MPI communicator of size 1
3578c75a6043SHong Zhang .  bs - size of block
3579c75a6043SHong Zhang .  m - number of rows
3580c75a6043SHong Zhang .  n - number of columns
3581c75a6043SHong Zhang .  i - row indices
3582c75a6043SHong Zhang .  j - column indices
3583c75a6043SHong Zhang -  a - matrix values
3584c75a6043SHong Zhang 
3585c75a6043SHong Zhang    Output Parameter:
3586c75a6043SHong Zhang .  mat - the matrix
3587c75a6043SHong Zhang 
3588c75a6043SHong Zhang    Level: intermediate
3589c75a6043SHong Zhang 
3590c75a6043SHong Zhang    Notes:
3591c75a6043SHong Zhang        The i, j, and a arrays are not copied by this routine, the user must free these arrays
3592c75a6043SHong Zhang     once the matrix is destroyed
3593c75a6043SHong Zhang 
3594c75a6043SHong Zhang        You cannot set new nonzero locations into this matrix, that will generate an error.
3595c75a6043SHong Zhang 
3596c75a6043SHong Zhang        The i and j indices are 0 based
3597c75a6043SHong Zhang 
3598c75a6043SHong Zhang .seealso: MatCreate(), MatCreateMPIBAIJ(), MatCreateSeqBAIJ()
3599c75a6043SHong Zhang 
3600c75a6043SHong Zhang @*/
3601c75a6043SHong Zhang PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
3602c75a6043SHong Zhang {
3603c75a6043SHong Zhang   PetscErrorCode ierr;
3604c75a6043SHong Zhang   PetscInt       ii;
3605c75a6043SHong Zhang   Mat_SeqBAIJ    *baij;
3606c75a6043SHong Zhang 
3607c75a6043SHong Zhang   PetscFunctionBegin;
3608e32f2f54SBarry Smith   if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"block size %D > 1 is not supported yet",bs);
3609e32f2f54SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
3610c75a6043SHong Zhang 
3611c75a6043SHong Zhang   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3612c75a6043SHong Zhang   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3613c75a6043SHong Zhang   ierr = MatSetType(*mat,MATSEQBAIJ);CHKERRQ(ierr);
3614c75a6043SHong Zhang   ierr = MatSeqBAIJSetPreallocation_SeqBAIJ(*mat,bs,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3615c75a6043SHong Zhang   baij = (Mat_SeqBAIJ*)(*mat)->data;
3616c75a6043SHong Zhang   ierr = PetscMalloc2(m,PetscInt,&baij->imax,m,PetscInt,&baij->ilen);CHKERRQ(ierr);
36171784c0f5SBarry Smith   ierr = PetscLogObjectMemory(*mat,2*m*sizeof(PetscInt));CHKERRQ(ierr);
3618c75a6043SHong Zhang 
3619c75a6043SHong Zhang   baij->i = i;
3620c75a6043SHong Zhang   baij->j = j;
3621c75a6043SHong Zhang   baij->a = a;
3622c75a6043SHong Zhang   baij->singlemalloc = PETSC_FALSE;
3623c75a6043SHong Zhang   baij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3624e6b907acSBarry Smith   baij->free_a       = PETSC_FALSE;
3625e6b907acSBarry Smith   baij->free_ij       = PETSC_FALSE;
3626c75a6043SHong Zhang 
3627c75a6043SHong Zhang   for (ii=0; ii<m; ii++) {
3628c75a6043SHong Zhang     baij->ilen[ii] = baij->imax[ii] = i[ii+1] - i[ii];
3629c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
3630e32f2f54SBarry 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]);
3631c75a6043SHong Zhang #endif
3632c75a6043SHong Zhang   }
3633c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
3634c75a6043SHong Zhang   for (ii=0; ii<baij->i[m]; ii++) {
3635e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
3636e32f2f54SBarry 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]);
3637c75a6043SHong Zhang   }
3638c75a6043SHong Zhang #endif
3639c75a6043SHong Zhang 
3640c75a6043SHong Zhang   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3641c75a6043SHong Zhang   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3642c75a6043SHong Zhang   PetscFunctionReturn(0);
3643c75a6043SHong Zhang }
3644