xref: /petsc/src/mat/impls/baij/mpi/mpibaij.c (revision 4683f7a4294525e5491c43fa80b5076ad80d7637)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
279bdfe76SSatish Balay 
37c4f633dSBarry Smith #include "../src/mat/impls/baij/mpi/mpibaij.h"   /*I  "petscmat.h"  I*/
4f3da1532SBarry Smith #include "petscblaslapack.h"
579bdfe76SSatish Balay 
6dfbe8321SBarry Smith EXTERN PetscErrorCode MatSetUpMultiply_MPIBAIJ(Mat);
7dfbe8321SBarry Smith EXTERN PetscErrorCode DisAssemble_MPIBAIJ(Mat);
8b24ad042SBarry Smith EXTERN PetscErrorCode MatIncreaseOverlap_MPIBAIJ(Mat,PetscInt,IS[],PetscInt);
9b24ad042SBarry Smith EXTERN PetscErrorCode MatGetSubMatrices_MPIBAIJ(Mat,PetscInt,const IS[],const IS[],MatReuse,Mat *[]);
10b24ad042SBarry Smith EXTERN PetscErrorCode MatGetValues_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt [],PetscScalar []);
11b24ad042SBarry Smith EXTERN PetscErrorCode MatSetValues_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt [],const PetscScalar [],InsertMode);
12b24ad042SBarry Smith EXTERN PetscErrorCode MatSetValuesBlocked_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
13b24ad042SBarry Smith EXTERN PetscErrorCode MatGetRow_SeqBAIJ(Mat,PetscInt,PetscInt*,PetscInt*[],PetscScalar*[]);
14b24ad042SBarry Smith EXTERN PetscErrorCode MatRestoreRow_SeqBAIJ(Mat,PetscInt,PetscInt*,PetscInt*[],PetscScalar*[]);
15f4df32b1SMatthew Knepley EXTERN PetscErrorCode MatZeroRows_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscScalar);
1693fea6afSBarry Smith 
174a2ae208SSatish Balay #undef __FUNCT__
18985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_MPIBAIJ"
19985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_MPIBAIJ(Mat A,Vec v,PetscInt idx[])
207843d17aSBarry Smith {
217843d17aSBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
22dfbe8321SBarry Smith   PetscErrorCode ierr;
23985db425SBarry Smith   PetscInt       i,*idxb = 0;
2487828ca2SBarry Smith   PetscScalar    *va,*vb;
257843d17aSBarry Smith   Vec            vtmp;
267843d17aSBarry Smith 
277843d17aSBarry Smith   PetscFunctionBegin;
28985db425SBarry Smith   ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr);
291ebc52fbSHong Zhang   ierr = VecGetArray(v,&va);CHKERRQ(ierr);
30985db425SBarry Smith   if (idx) {
31192daf7cSBarry Smith     for (i=0; i<A->rmap->n; i++) {if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;}
32985db425SBarry Smith   }
337843d17aSBarry Smith 
34d0f46423SBarry Smith   ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr);
35d0f46423SBarry Smith   if (idx) {ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr);}
36985db425SBarry Smith   ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr);
371ebc52fbSHong Zhang   ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr);
387843d17aSBarry Smith 
39d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++){
40d0f46423SBarry Smith     if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {va[i] = vb[i]; if (idx) idx[i] = A->cmap->bs*a->garray[idxb[i]/A->cmap->bs] + (idxb[i] % A->cmap->bs);}
417843d17aSBarry Smith   }
427843d17aSBarry Smith 
431ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&va);CHKERRQ(ierr);
441ebc52fbSHong Zhang   ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr);
45985db425SBarry Smith   if (idxb) {ierr = PetscFree(idxb);CHKERRQ(ierr);}
46ac355199SBarry Smith   ierr = VecDestroy(vtmp);CHKERRQ(ierr);
477843d17aSBarry Smith   PetscFunctionReturn(0);
487843d17aSBarry Smith }
497843d17aSBarry Smith 
507fc3c18eSBarry Smith EXTERN_C_BEGIN
514a2ae208SSatish Balay #undef __FUNCT__
524a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIBAIJ"
53be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_MPIBAIJ(Mat mat)
547fc3c18eSBarry Smith {
557fc3c18eSBarry Smith   Mat_MPIBAIJ    *aij = (Mat_MPIBAIJ *)mat->data;
56dfbe8321SBarry Smith   PetscErrorCode ierr;
577fc3c18eSBarry Smith 
587fc3c18eSBarry Smith   PetscFunctionBegin;
597fc3c18eSBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
607fc3c18eSBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
617fc3c18eSBarry Smith   PetscFunctionReturn(0);
627fc3c18eSBarry Smith }
637fc3c18eSBarry Smith EXTERN_C_END
647fc3c18eSBarry Smith 
657fc3c18eSBarry Smith EXTERN_C_BEGIN
664a2ae208SSatish Balay #undef __FUNCT__
674a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIBAIJ"
68be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_MPIBAIJ(Mat mat)
697fc3c18eSBarry Smith {
707fc3c18eSBarry Smith   Mat_MPIBAIJ    *aij = (Mat_MPIBAIJ *)mat->data;
71dfbe8321SBarry Smith   PetscErrorCode ierr;
727fc3c18eSBarry Smith 
737fc3c18eSBarry Smith   PetscFunctionBegin;
747fc3c18eSBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
757fc3c18eSBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
767fc3c18eSBarry Smith   PetscFunctionReturn(0);
777fc3c18eSBarry Smith }
787fc3c18eSBarry Smith EXTERN_C_END
797fc3c18eSBarry Smith 
80537820f0SBarry Smith /*
81537820f0SBarry Smith      Local utility routine that creates a mapping from the global column
8257b952d6SSatish Balay    number to the local number in the off-diagonal part of the local
83e06f6af7SJed Brown    storage of the matrix.  This is done in a non scalable way since the
8457b952d6SSatish Balay    length of colmap equals the global matrix length.
8557b952d6SSatish Balay */
864a2ae208SSatish Balay #undef __FUNCT__
874a2ae208SSatish Balay #define __FUNCT__ "CreateColmap_MPIBAIJ_Private"
88dfbe8321SBarry Smith PetscErrorCode CreateColmap_MPIBAIJ_Private(Mat mat)
8957b952d6SSatish Balay {
9057b952d6SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
9157b952d6SSatish Balay   Mat_SeqBAIJ    *B = (Mat_SeqBAIJ*)baij->B->data;
926849ba73SBarry Smith   PetscErrorCode ierr;
93d0f46423SBarry Smith   PetscInt       nbs = B->nbs,i,bs=mat->rmap->bs;
9457b952d6SSatish Balay 
95d64ed03dSBarry Smith   PetscFunctionBegin;
96aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
97f14a1c24SBarry Smith   ierr = PetscTableCreate(baij->nbs,&baij->colmap);CHKERRQ(ierr);
9848e59246SSatish Balay   for (i=0; i<nbs; i++){
990f5bd95cSBarry Smith     ierr = PetscTableAdd(baij->colmap,baij->garray[i]+1,i*bs+1);CHKERRQ(ierr);
10048e59246SSatish Balay   }
10148e59246SSatish Balay #else
102b24ad042SBarry Smith   ierr = PetscMalloc((baij->Nbs+1)*sizeof(PetscInt),&baij->colmap);CHKERRQ(ierr);
10352e6d16bSBarry Smith   ierr = PetscLogObjectMemory(mat,baij->Nbs*sizeof(PetscInt));CHKERRQ(ierr);
104b24ad042SBarry Smith   ierr = PetscMemzero(baij->colmap,baij->Nbs*sizeof(PetscInt));CHKERRQ(ierr);
105928fc39bSSatish Balay   for (i=0; i<nbs; i++) baij->colmap[baij->garray[i]] = i*bs+1;
10648e59246SSatish Balay #endif
1073a40ed3dSBarry Smith   PetscFunctionReturn(0);
10857b952d6SSatish Balay }
10957b952d6SSatish Balay 
110f5e9677aSSatish Balay #define  MatSetValues_SeqBAIJ_A_Private(row,col,value,addv) \
11180c1aa95SSatish Balay { \
11280c1aa95SSatish Balay  \
11380c1aa95SSatish Balay     brow = row/bs;  \
11480c1aa95SSatish Balay     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow]; \
115ac7a638eSSatish Balay     rmax = aimax[brow]; nrow = ailen[brow]; \
11680c1aa95SSatish Balay       bcol = col/bs; \
11780c1aa95SSatish Balay       ridx = row % bs; cidx = col % bs; \
118ab26458aSBarry Smith       low = 0; high = nrow; \
119ab26458aSBarry Smith       while (high-low > 3) { \
120ab26458aSBarry Smith         t = (low+high)/2; \
121ab26458aSBarry Smith         if (rp[t] > bcol) high = t; \
122ab26458aSBarry Smith         else              low  = t; \
123ab26458aSBarry Smith       } \
124ab26458aSBarry Smith       for (_i=low; _i<high; _i++) { \
12580c1aa95SSatish Balay         if (rp[_i] > bcol) break; \
12680c1aa95SSatish Balay         if (rp[_i] == bcol) { \
12780c1aa95SSatish Balay           bap  = ap +  bs2*_i + bs*cidx + ridx; \
128eada6651SSatish Balay           if (addv == ADD_VALUES) *bap += value;  \
129eada6651SSatish Balay           else                    *bap  = value;  \
130ac7a638eSSatish Balay           goto a_noinsert; \
13180c1aa95SSatish Balay         } \
13280c1aa95SSatish Balay       } \
13389280ab3SLois Curfman McInnes       if (a->nonew == 1) goto a_noinsert; \
134e32f2f54SBarry Smith       if (a->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
135fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,aimax,a->nonew,MatScalar); \
13680c1aa95SSatish Balay       N = nrow++ - 1;  \
13780c1aa95SSatish Balay       /* shift up all the later entries in this row */ \
13880c1aa95SSatish Balay       for (ii=N; ii>=_i; ii--) { \
13980c1aa95SSatish Balay         rp[ii+1] = rp[ii]; \
1403eda8832SBarry Smith         ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr); \
14180c1aa95SSatish Balay       } \
1423eda8832SBarry Smith       if (N>=_i) { ierr = PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));CHKERRQ(ierr); }  \
14380c1aa95SSatish Balay       rp[_i]                      = bcol;  \
14480c1aa95SSatish Balay       ap[bs2*_i + bs*cidx + ridx] = value;  \
145ac7a638eSSatish Balay       a_noinsert:; \
14680c1aa95SSatish Balay     ailen[brow] = nrow; \
14780c1aa95SSatish Balay }
14857b952d6SSatish Balay 
149ac7a638eSSatish Balay #define  MatSetValues_SeqBAIJ_B_Private(row,col,value,addv) \
150ac7a638eSSatish Balay { \
151ac7a638eSSatish Balay     brow = row/bs;  \
152ac7a638eSSatish Balay     rp   = bj + bi[brow]; ap = ba + bs2*bi[brow]; \
153ac7a638eSSatish Balay     rmax = bimax[brow]; nrow = bilen[brow]; \
154ac7a638eSSatish Balay       bcol = col/bs; \
155ac7a638eSSatish Balay       ridx = row % bs; cidx = col % bs; \
156ac7a638eSSatish Balay       low = 0; high = nrow; \
157ac7a638eSSatish Balay       while (high-low > 3) { \
158ac7a638eSSatish Balay         t = (low+high)/2; \
159ac7a638eSSatish Balay         if (rp[t] > bcol) high = t; \
160ac7a638eSSatish Balay         else              low  = t; \
161ac7a638eSSatish Balay       } \
162ac7a638eSSatish Balay       for (_i=low; _i<high; _i++) { \
163ac7a638eSSatish Balay         if (rp[_i] > bcol) break; \
164ac7a638eSSatish Balay         if (rp[_i] == bcol) { \
165ac7a638eSSatish Balay           bap  = ap +  bs2*_i + bs*cidx + ridx; \
166ac7a638eSSatish Balay           if (addv == ADD_VALUES) *bap += value;  \
167ac7a638eSSatish Balay           else                    *bap  = value;  \
168ac7a638eSSatish Balay           goto b_noinsert; \
169ac7a638eSSatish Balay         } \
170ac7a638eSSatish Balay       } \
17189280ab3SLois Curfman McInnes       if (b->nonew == 1) goto b_noinsert; \
172e32f2f54SBarry Smith       if (b->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
173fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(B,b->mbs,bs2,nrow,brow,bcol,rmax,ba,bi,bj,rp,ap,bimax,b->nonew,MatScalar); \
174085a36d4SBarry Smith       CHKMEMQ;\
175ac7a638eSSatish Balay       N = nrow++ - 1;  \
176ac7a638eSSatish Balay       /* shift up all the later entries in this row */ \
177ac7a638eSSatish Balay       for (ii=N; ii>=_i; ii--) { \
178ac7a638eSSatish Balay         rp[ii+1] = rp[ii]; \
1793eda8832SBarry Smith         ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr); \
180ac7a638eSSatish Balay       } \
1813eda8832SBarry Smith       if (N>=_i) { ierr = PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));CHKERRQ(ierr);}  \
182ac7a638eSSatish Balay       rp[_i]                      = bcol;  \
183ac7a638eSSatish Balay       ap[bs2*_i + bs*cidx + ridx] = value;  \
184ac7a638eSSatish Balay       b_noinsert:; \
185ac7a638eSSatish Balay     bilen[brow] = nrow; \
186ac7a638eSSatish Balay }
187ac7a638eSSatish Balay 
1884a2ae208SSatish Balay #undef __FUNCT__
1894a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIBAIJ"
190b24ad042SBarry Smith PetscErrorCode MatSetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
19157b952d6SSatish Balay {
19257b952d6SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
19393fea6afSBarry Smith   MatScalar      value;
194273d9f13SBarry Smith   PetscTruth     roworiented = baij->roworiented;
195dfbe8321SBarry Smith   PetscErrorCode ierr;
196b24ad042SBarry Smith   PetscInt       i,j,row,col;
197d0f46423SBarry Smith   PetscInt       rstart_orig=mat->rmap->rstart;
198d0f46423SBarry Smith   PetscInt       rend_orig=mat->rmap->rend,cstart_orig=mat->cmap->rstart;
199d0f46423SBarry Smith   PetscInt       cend_orig=mat->cmap->rend,bs=mat->rmap->bs;
20057b952d6SSatish Balay 
201eada6651SSatish Balay   /* Some Variables required in the macro */
20280c1aa95SSatish Balay   Mat            A = baij->A;
20380c1aa95SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)(A)->data;
204b24ad042SBarry Smith   PetscInt       *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j;
2053eda8832SBarry Smith   MatScalar      *aa=a->a;
206ac7a638eSSatish Balay 
207ac7a638eSSatish Balay   Mat            B = baij->B;
208ac7a638eSSatish Balay   Mat_SeqBAIJ    *b = (Mat_SeqBAIJ*)(B)->data;
209b24ad042SBarry Smith   PetscInt       *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j;
2103eda8832SBarry Smith   MatScalar      *ba=b->a;
211ac7a638eSSatish Balay 
212b24ad042SBarry Smith   PetscInt       *rp,ii,nrow,_i,rmax,N,brow,bcol;
213b24ad042SBarry Smith   PetscInt       low,high,t,ridx,cidx,bs2=a->bs2;
2143eda8832SBarry Smith   MatScalar      *ap,*bap;
21580c1aa95SSatish Balay 
216d64ed03dSBarry Smith   PetscFunctionBegin;
21771fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
21857b952d6SSatish Balay   for (i=0; i<m; i++) {
2195ef9f2a5SBarry Smith     if (im[i] < 0) continue;
2202515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
221e32f2f54SBarry Smith     if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1);
222639f9d9dSBarry Smith #endif
22357b952d6SSatish Balay     if (im[i] >= rstart_orig && im[i] < rend_orig) {
22457b952d6SSatish Balay       row = im[i] - rstart_orig;
22557b952d6SSatish Balay       for (j=0; j<n; j++) {
22657b952d6SSatish Balay         if (in[j] >= cstart_orig && in[j] < cend_orig){
22757b952d6SSatish Balay           col = in[j] - cstart_orig;
22857b952d6SSatish Balay           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
229f5e9677aSSatish Balay           MatSetValues_SeqBAIJ_A_Private(row,col,value,addv);
23080c1aa95SSatish Balay           /* ierr = MatSetValues_SeqBAIJ(baij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
23173959e64SBarry Smith         } else if (in[j] < 0) continue;
2322515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
233cb9801acSJed Brown         else if (in[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[i],mat->cmap->N-1);
234639f9d9dSBarry Smith #endif
23557b952d6SSatish Balay         else {
23657b952d6SSatish Balay           if (mat->was_assembled) {
237905e6a2fSBarry Smith             if (!baij->colmap) {
238905e6a2fSBarry Smith               ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
239905e6a2fSBarry Smith             }
240aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
2410f5bd95cSBarry Smith             ierr = PetscTableFind(baij->colmap,in[j]/bs + 1,&col);CHKERRQ(ierr);
242bba1ac68SSatish Balay             col  = col - 1;
24348e59246SSatish Balay #else
244bba1ac68SSatish Balay             col = baij->colmap[in[j]/bs] - 1;
24548e59246SSatish Balay #endif
24657b952d6SSatish Balay             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
24757b952d6SSatish Balay               ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
2488295de27SSatish Balay               col =  in[j];
2499bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
2509bf004c3SSatish Balay               B = baij->B;
2519bf004c3SSatish Balay               b = (Mat_SeqBAIJ*)(B)->data;
2529bf004c3SSatish Balay               bimax=b->imax;bi=b->i;bilen=b->ilen;bj=b->j;
2539bf004c3SSatish Balay               ba=b->a;
254bba1ac68SSatish Balay             } else col += in[j]%bs;
2558295de27SSatish Balay           } else col = in[j];
25657b952d6SSatish Balay           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
25790da58bdSSatish Balay           MatSetValues_SeqBAIJ_B_Private(row,col,value,addv);
25890da58bdSSatish Balay           /* ierr = MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
25957b952d6SSatish Balay         }
26057b952d6SSatish Balay       }
261d64ed03dSBarry Smith     } else {
26290f02eecSBarry Smith       if (!baij->donotstash) {
263ff2fd236SBarry Smith         if (roworiented) {
264b400d20cSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);CHKERRQ(ierr);
265ff2fd236SBarry Smith         } else {
266b400d20cSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);CHKERRQ(ierr);
26757b952d6SSatish Balay         }
26857b952d6SSatish Balay       }
26957b952d6SSatish Balay     }
27090f02eecSBarry Smith   }
2713a40ed3dSBarry Smith   PetscFunctionReturn(0);
27257b952d6SSatish Balay }
27357b952d6SSatish Balay 
2744a2ae208SSatish Balay #undef __FUNCT__
27597e5c40aSBarry Smith #define __FUNCT__ "MatSetValuesBlocked_MPIBAIJ"
27697e5c40aSBarry Smith PetscErrorCode MatSetValuesBlocked_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
277ab26458aSBarry Smith {
278ab26458aSBarry Smith   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
279dd6ea824SBarry Smith   const PetscScalar *value;
280f15d580aSBarry Smith   MatScalar         *barray=baij->barray;
281273d9f13SBarry Smith   PetscTruth        roworiented = baij->roworiented;
282dfbe8321SBarry Smith   PetscErrorCode    ierr;
283899cda47SBarry Smith   PetscInt          i,j,ii,jj,row,col,rstart=baij->rstartbs;
284899cda47SBarry Smith   PetscInt          rend=baij->rendbs,cstart=baij->cstartbs,stepval;
285d0f46423SBarry Smith   PetscInt          cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;
286ab26458aSBarry Smith 
287b16ae2b1SBarry Smith   PetscFunctionBegin;
28830793edcSSatish Balay   if(!barray) {
28982502324SSatish Balay     ierr         = PetscMalloc(bs2*sizeof(MatScalar),&barray);CHKERRQ(ierr);
29082502324SSatish Balay     baij->barray = barray;
29130793edcSSatish Balay   }
29230793edcSSatish Balay 
293ab26458aSBarry Smith   if (roworiented) {
294ab26458aSBarry Smith     stepval = (n-1)*bs;
295ab26458aSBarry Smith   } else {
296ab26458aSBarry Smith     stepval = (m-1)*bs;
297ab26458aSBarry Smith   }
298ab26458aSBarry Smith   for (i=0; i<m; i++) {
2995ef9f2a5SBarry Smith     if (im[i] < 0) continue;
3002515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
301e32f2f54SBarry Smith     if (im[i] >= baij->Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large, row %D max %D",im[i],baij->Mbs-1);
302ab26458aSBarry Smith #endif
303ab26458aSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
304ab26458aSBarry Smith       row = im[i] - rstart;
305ab26458aSBarry Smith       for (j=0; j<n; j++) {
30615b57d14SSatish Balay         /* If NumCol = 1 then a copy is not required */
30715b57d14SSatish Balay         if ((roworiented) && (n == 1)) {
308f15d580aSBarry Smith           barray = (MatScalar*)v + i*bs2;
30915b57d14SSatish Balay         } else if((!roworiented) && (m == 1)) {
310f15d580aSBarry Smith           barray = (MatScalar*)v + j*bs2;
31115b57d14SSatish Balay         } else { /* Here a copy is required */
312ab26458aSBarry Smith           if (roworiented) {
313ab26458aSBarry Smith             value = v + i*(stepval+bs)*bs + j*bs;
314ab26458aSBarry Smith           } else {
315ab26458aSBarry Smith             value = v + j*(stepval+bs)*bs + i*bs;
316abef11f7SSatish Balay           }
31747513183SBarry Smith           for (ii=0; ii<bs; ii++,value+=stepval) {
31847513183SBarry Smith             for (jj=0; jj<bs; jj++) {
31930793edcSSatish Balay               *barray++  = *value++;
32047513183SBarry Smith             }
32147513183SBarry Smith           }
32230793edcSSatish Balay           barray -=bs2;
32315b57d14SSatish Balay         }
324abef11f7SSatish Balay 
325abef11f7SSatish Balay         if (in[j] >= cstart && in[j] < cend){
326abef11f7SSatish Balay           col  = in[j] - cstart;
32797e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
328ab26458aSBarry Smith         }
3295ef9f2a5SBarry Smith         else if (in[j] < 0) continue;
3302515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
331cb9801acSJed Brown         else if (in[j] >= baij->Nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large, col %D max %D",in[j],baij->Nbs-1);
332ab26458aSBarry Smith #endif
333ab26458aSBarry Smith         else {
334ab26458aSBarry Smith           if (mat->was_assembled) {
335ab26458aSBarry Smith             if (!baij->colmap) {
336ab26458aSBarry Smith               ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
337ab26458aSBarry Smith             }
338a5eb4965SSatish Balay 
3392515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
340aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
341b24ad042SBarry Smith             { PetscInt data;
3420f5bd95cSBarry Smith               ierr = PetscTableFind(baij->colmap,in[j]+1,&data);CHKERRQ(ierr);
343e32f2f54SBarry Smith               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
344fa46199cSSatish Balay             }
34548e59246SSatish Balay #else
346e32f2f54SBarry Smith             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
347a5eb4965SSatish Balay #endif
34848e59246SSatish Balay #endif
349aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
3500f5bd95cSBarry Smith 	    ierr = PetscTableFind(baij->colmap,in[j]+1,&col);CHKERRQ(ierr);
351fa46199cSSatish Balay             col  = (col - 1)/bs;
35248e59246SSatish Balay #else
353a5eb4965SSatish Balay             col = (baij->colmap[in[j]] - 1)/bs;
35448e59246SSatish Balay #endif
355ab26458aSBarry Smith             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
356ab26458aSBarry Smith               ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
357ab26458aSBarry Smith               col =  in[j];
358ab26458aSBarry Smith             }
359ab26458aSBarry Smith           }
360ab26458aSBarry Smith           else col = in[j];
36197e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
362ab26458aSBarry Smith         }
363ab26458aSBarry Smith       }
364d64ed03dSBarry Smith     } else {
365ab26458aSBarry Smith       if (!baij->donotstash) {
366ff2fd236SBarry Smith         if (roworiented) {
3676fa18ffdSBarry Smith           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
368ff2fd236SBarry Smith         } else {
3696fa18ffdSBarry Smith           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
370ff2fd236SBarry Smith         }
371abef11f7SSatish Balay       }
372ab26458aSBarry Smith     }
373ab26458aSBarry Smith   }
3743a40ed3dSBarry Smith   PetscFunctionReturn(0);
375ab26458aSBarry Smith }
3766fa18ffdSBarry Smith 
3770bdbc534SSatish Balay #define HASH_KEY 0.6180339887
378b24ad042SBarry Smith #define HASH(size,key,tmp) (tmp = (key)*HASH_KEY,(PetscInt)((size)*(tmp-(PetscInt)tmp)))
379b24ad042SBarry Smith /* #define HASH(size,key) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
380b24ad042SBarry Smith /* #define HASH(size,key,tmp) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
3814a2ae208SSatish Balay #undef __FUNCT__
38297e5c40aSBarry Smith #define __FUNCT__ "MatSetValues_MPIBAIJ_HT"
38397e5c40aSBarry Smith PetscErrorCode MatSetValues_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
3840bdbc534SSatish Balay {
3850bdbc534SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
386273d9f13SBarry Smith   PetscTruth     roworiented = baij->roworiented;
387dfbe8321SBarry Smith   PetscErrorCode ierr;
388b24ad042SBarry Smith   PetscInt       i,j,row,col;
389d0f46423SBarry Smith   PetscInt       rstart_orig=mat->rmap->rstart;
390d0f46423SBarry Smith   PetscInt       rend_orig=mat->rmap->rend,Nbs=baij->Nbs;
391d0f46423SBarry Smith   PetscInt       h1,key,size=baij->ht_size,bs=mat->rmap->bs,*HT=baij->ht,idx;
392329f5518SBarry Smith   PetscReal      tmp;
3933eda8832SBarry Smith   MatScalar      **HD = baij->hd,value;
3942515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
395b24ad042SBarry Smith   PetscInt       total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
3964a15367fSSatish Balay #endif
3970bdbc534SSatish Balay 
3980bdbc534SSatish Balay   PetscFunctionBegin;
39971fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
4000bdbc534SSatish Balay   for (i=0; i<m; i++) {
4012515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
402e32f2f54SBarry Smith     if (im[i] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
403e32f2f54SBarry Smith     if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1);
4040bdbc534SSatish Balay #endif
4050bdbc534SSatish Balay       row = im[i];
406c2760754SSatish Balay     if (row >= rstart_orig && row < rend_orig) {
4070bdbc534SSatish Balay       for (j=0; j<n; j++) {
4080bdbc534SSatish Balay         col = in[j];
4096fa18ffdSBarry Smith         if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
410b24ad042SBarry Smith         /* Look up PetscInto the Hash Table */
411c2760754SSatish Balay         key = (row/bs)*Nbs+(col/bs)+1;
412c2760754SSatish Balay         h1  = HASH(size,key,tmp);
4130bdbc534SSatish Balay 
414c2760754SSatish Balay 
415c2760754SSatish Balay         idx = h1;
4162515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
417187ce0cbSSatish Balay         insert_ct++;
418187ce0cbSSatish Balay         total_ct++;
419187ce0cbSSatish Balay         if (HT[idx] != key) {
420187ce0cbSSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++);
421187ce0cbSSatish Balay           if (idx == size) {
422187ce0cbSSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++);
423187ce0cbSSatish Balay             if (idx == h1) {
424e32f2f54SBarry Smith               SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
425187ce0cbSSatish Balay             }
426187ce0cbSSatish Balay           }
427187ce0cbSSatish Balay         }
428187ce0cbSSatish Balay #else
429c2760754SSatish Balay         if (HT[idx] != key) {
430c2760754SSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++);
431c2760754SSatish Balay           if (idx == size) {
432c2760754SSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++);
433c2760754SSatish Balay             if (idx == h1) {
434e32f2f54SBarry Smith               SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
435c2760754SSatish Balay             }
436c2760754SSatish Balay           }
437c2760754SSatish Balay         }
438187ce0cbSSatish Balay #endif
439c2760754SSatish Balay         /* A HASH table entry is found, so insert the values at the correct address */
440c2760754SSatish Balay         if (addv == ADD_VALUES) *(HD[idx]+ (col % bs)*bs + (row % bs)) += value;
441c2760754SSatish Balay         else                    *(HD[idx]+ (col % bs)*bs + (row % bs))  = value;
4420bdbc534SSatish Balay       }
4430bdbc534SSatish Balay     } else {
4440bdbc534SSatish Balay       if (!baij->donotstash) {
445ff2fd236SBarry Smith         if (roworiented) {
446b400d20cSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);CHKERRQ(ierr);
447ff2fd236SBarry Smith         } else {
448b400d20cSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);CHKERRQ(ierr);
4490bdbc534SSatish Balay         }
4500bdbc534SSatish Balay       }
4510bdbc534SSatish Balay     }
4520bdbc534SSatish Balay   }
4532515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
454187ce0cbSSatish Balay   baij->ht_total_ct = total_ct;
455187ce0cbSSatish Balay   baij->ht_insert_ct = insert_ct;
456187ce0cbSSatish Balay #endif
4570bdbc534SSatish Balay   PetscFunctionReturn(0);
4580bdbc534SSatish Balay }
4590bdbc534SSatish Balay 
4604a2ae208SSatish Balay #undef __FUNCT__
46197e5c40aSBarry Smith #define __FUNCT__ "MatSetValuesBlocked_MPIBAIJ_HT"
46297e5c40aSBarry Smith PetscErrorCode MatSetValuesBlocked_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
4630bdbc534SSatish Balay {
4640bdbc534SSatish Balay   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
465273d9f13SBarry Smith   PetscTruth        roworiented = baij->roworiented;
466dfbe8321SBarry Smith   PetscErrorCode    ierr;
467b24ad042SBarry Smith   PetscInt          i,j,ii,jj,row,col;
468899cda47SBarry Smith   PetscInt          rstart=baij->rstartbs;
469d0f46423SBarry Smith   PetscInt          rend=mat->rmap->rend,stepval,bs=mat->rmap->bs,bs2=baij->bs2,nbs2=n*bs2;
470b24ad042SBarry Smith   PetscInt          h1,key,size=baij->ht_size,idx,*HT=baij->ht,Nbs=baij->Nbs;
471329f5518SBarry Smith   PetscReal         tmp;
4723eda8832SBarry Smith   MatScalar         **HD = baij->hd,*baij_a;
473dd6ea824SBarry Smith   const PetscScalar *v_t,*value;
4742515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
475b24ad042SBarry Smith   PetscInt          total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
4764a15367fSSatish Balay #endif
4770bdbc534SSatish Balay 
478d0a41580SSatish Balay   PetscFunctionBegin;
479d0a41580SSatish Balay 
4800bdbc534SSatish Balay   if (roworiented) {
4810bdbc534SSatish Balay     stepval = (n-1)*bs;
4820bdbc534SSatish Balay   } else {
4830bdbc534SSatish Balay     stepval = (m-1)*bs;
4840bdbc534SSatish Balay   }
4850bdbc534SSatish Balay   for (i=0; i<m; i++) {
4862515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
487e32f2f54SBarry Smith     if (im[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",im[i]);
488e32f2f54SBarry Smith     if (im[i] >= baij->Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],baij->Mbs-1);
4890bdbc534SSatish Balay #endif
4900bdbc534SSatish Balay     row   = im[i];
491ab715e2cSSatish Balay     v_t   = v + i*nbs2;
492c2760754SSatish Balay     if (row >= rstart && row < rend) {
4930bdbc534SSatish Balay       for (j=0; j<n; j++) {
4940bdbc534SSatish Balay         col = in[j];
4950bdbc534SSatish Balay 
4960bdbc534SSatish Balay         /* Look up into the Hash Table */
497c2760754SSatish Balay         key = row*Nbs+col+1;
498c2760754SSatish Balay         h1  = HASH(size,key,tmp);
4990bdbc534SSatish Balay 
500c2760754SSatish Balay         idx = h1;
5012515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
502187ce0cbSSatish Balay         total_ct++;
503187ce0cbSSatish Balay         insert_ct++;
504187ce0cbSSatish Balay        if (HT[idx] != key) {
505187ce0cbSSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++);
506187ce0cbSSatish Balay           if (idx == size) {
507187ce0cbSSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++);
508187ce0cbSSatish Balay             if (idx == h1) {
509e32f2f54SBarry Smith               SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
510187ce0cbSSatish Balay             }
511187ce0cbSSatish Balay           }
512187ce0cbSSatish Balay         }
513187ce0cbSSatish Balay #else
514c2760754SSatish Balay         if (HT[idx] != key) {
515c2760754SSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++);
516c2760754SSatish Balay           if (idx == size) {
517c2760754SSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++);
518c2760754SSatish Balay             if (idx == h1) {
519e32f2f54SBarry Smith               SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
520c2760754SSatish Balay             }
521c2760754SSatish Balay           }
522c2760754SSatish Balay         }
523187ce0cbSSatish Balay #endif
524c2760754SSatish Balay         baij_a = HD[idx];
5250bdbc534SSatish Balay         if (roworiented) {
526c2760754SSatish Balay           /*value = v + i*(stepval+bs)*bs + j*bs;*/
527187ce0cbSSatish Balay           /* value = v + (i*(stepval+bs)+j)*bs; */
528187ce0cbSSatish Balay           value = v_t;
529187ce0cbSSatish Balay           v_t  += bs;
530fef45726SSatish Balay           if (addv == ADD_VALUES) {
531c2760754SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval) {
532c2760754SSatish Balay               for (jj=ii; jj<bs2; jj+=bs) {
533fef45726SSatish Balay                 baij_a[jj]  += *value++;
534b4cc0f5aSSatish Balay               }
535b4cc0f5aSSatish Balay             }
536fef45726SSatish Balay           } else {
537c2760754SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval) {
538c2760754SSatish Balay               for (jj=ii; jj<bs2; jj+=bs) {
539fef45726SSatish Balay                 baij_a[jj]  = *value++;
540fef45726SSatish Balay               }
541fef45726SSatish Balay             }
542fef45726SSatish Balay           }
5430bdbc534SSatish Balay         } else {
5440bdbc534SSatish Balay           value = v + j*(stepval+bs)*bs + i*bs;
545fef45726SSatish Balay           if (addv == ADD_VALUES) {
546b4cc0f5aSSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
5470bdbc534SSatish Balay               for (jj=0; jj<bs; jj++) {
548fef45726SSatish Balay                 baij_a[jj]  += *value++;
549fef45726SSatish Balay               }
550fef45726SSatish Balay             }
551fef45726SSatish Balay           } else {
552fef45726SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
553fef45726SSatish Balay               for (jj=0; jj<bs; jj++) {
554fef45726SSatish Balay                 baij_a[jj]  = *value++;
555fef45726SSatish Balay               }
556b4cc0f5aSSatish Balay             }
5570bdbc534SSatish Balay           }
5580bdbc534SSatish Balay         }
5590bdbc534SSatish Balay       }
5600bdbc534SSatish Balay     } else {
5610bdbc534SSatish Balay       if (!baij->donotstash) {
5620bdbc534SSatish Balay         if (roworiented) {
5638798bf22SSatish Balay           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
5640bdbc534SSatish Balay         } else {
5658798bf22SSatish Balay           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
5660bdbc534SSatish Balay         }
5670bdbc534SSatish Balay       }
5680bdbc534SSatish Balay     }
5690bdbc534SSatish Balay   }
5702515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
571187ce0cbSSatish Balay   baij->ht_total_ct = total_ct;
572187ce0cbSSatish Balay   baij->ht_insert_ct = insert_ct;
573187ce0cbSSatish Balay #endif
5740bdbc534SSatish Balay   PetscFunctionReturn(0);
5750bdbc534SSatish Balay }
576133cdb44SSatish Balay 
5774a2ae208SSatish Balay #undef __FUNCT__
5784a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIBAIJ"
579b24ad042SBarry Smith PetscErrorCode MatGetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
580d6de1c52SSatish Balay {
581d6de1c52SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
5826849ba73SBarry Smith   PetscErrorCode ierr;
583d0f46423SBarry Smith   PetscInt       bs=mat->rmap->bs,i,j,bsrstart = mat->rmap->rstart,bsrend = mat->rmap->rend;
584d0f46423SBarry Smith   PetscInt       bscstart = mat->cmap->rstart,bscend = mat->cmap->rend,row,col,data;
585d6de1c52SSatish Balay 
586133cdb44SSatish Balay   PetscFunctionBegin;
587d6de1c52SSatish Balay   for (i=0; i<m; i++) {
588e32f2f54SBarry Smith     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
589e32f2f54SBarry Smith     if (idxm[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",idxm[i],mat->rmap->N-1);
590d6de1c52SSatish Balay     if (idxm[i] >= bsrstart && idxm[i] < bsrend) {
591d6de1c52SSatish Balay       row = idxm[i] - bsrstart;
592d6de1c52SSatish Balay       for (j=0; j<n; j++) {
593e32f2f54SBarry Smith         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
594e32f2f54SBarry Smith         if (idxn[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",idxn[j],mat->cmap->N-1);
595d6de1c52SSatish Balay         if (idxn[j] >= bscstart && idxn[j] < bscend){
596d6de1c52SSatish Balay           col = idxn[j] - bscstart;
59798dd23e9SBarry Smith           ierr = MatGetValues_SeqBAIJ(baij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
598d64ed03dSBarry Smith         } else {
599905e6a2fSBarry Smith           if (!baij->colmap) {
600905e6a2fSBarry Smith             ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
601905e6a2fSBarry Smith           }
602aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
6030f5bd95cSBarry Smith           ierr = PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);CHKERRQ(ierr);
604fa46199cSSatish Balay           data --;
60548e59246SSatish Balay #else
60648e59246SSatish Balay           data = baij->colmap[idxn[j]/bs]-1;
60748e59246SSatish Balay #endif
60848e59246SSatish Balay           if((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0;
609d9d09a02SSatish Balay           else {
61048e59246SSatish Balay             col  = data + idxn[j]%bs;
61198dd23e9SBarry Smith             ierr = MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
612d6de1c52SSatish Balay           }
613d6de1c52SSatish Balay         }
614d6de1c52SSatish Balay       }
615d64ed03dSBarry Smith     } else {
616e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
617d6de1c52SSatish Balay     }
618d6de1c52SSatish Balay   }
6193a40ed3dSBarry Smith  PetscFunctionReturn(0);
620d6de1c52SSatish Balay }
621d6de1c52SSatish Balay 
6224a2ae208SSatish Balay #undef __FUNCT__
6234a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIBAIJ"
624dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIBAIJ(Mat mat,NormType type,PetscReal *nrm)
625d6de1c52SSatish Balay {
626d6de1c52SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
627d6de1c52SSatish Balay   Mat_SeqBAIJ    *amat = (Mat_SeqBAIJ*)baij->A->data,*bmat = (Mat_SeqBAIJ*)baij->B->data;
628dfbe8321SBarry Smith   PetscErrorCode ierr;
629d0f46423SBarry Smith   PetscInt       i,j,bs2=baij->bs2,bs=baij->A->rmap->bs,nz,row,col;
630329f5518SBarry Smith   PetscReal      sum = 0.0;
6313eda8832SBarry Smith   MatScalar      *v;
632d6de1c52SSatish Balay 
633d64ed03dSBarry Smith   PetscFunctionBegin;
634d6de1c52SSatish Balay   if (baij->size == 1) {
635064f8208SBarry Smith     ierr =  MatNorm(baij->A,type,nrm);CHKERRQ(ierr);
636d6de1c52SSatish Balay   } else {
637d6de1c52SSatish Balay     if (type == NORM_FROBENIUS) {
638d6de1c52SSatish Balay       v = amat->a;
6398a62d963SHong Zhang       nz = amat->nz*bs2;
6408a62d963SHong Zhang       for (i=0; i<nz; i++) {
641aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
642329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
643d6de1c52SSatish Balay #else
644d6de1c52SSatish Balay         sum += (*v)*(*v); v++;
645d6de1c52SSatish Balay #endif
646d6de1c52SSatish Balay       }
647d6de1c52SSatish Balay       v = bmat->a;
6488a62d963SHong Zhang       nz = bmat->nz*bs2;
6498a62d963SHong Zhang       for (i=0; i<nz; i++) {
650aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
651329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
652d6de1c52SSatish Balay #else
653d6de1c52SSatish Balay         sum += (*v)*(*v); v++;
654d6de1c52SSatish Balay #endif
655d6de1c52SSatish Balay       }
6567adad957SLisandro Dalcin       ierr = MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr);
657064f8208SBarry Smith       *nrm = sqrt(*nrm);
6588a62d963SHong Zhang     } else if (type == NORM_1) { /* max column sum */
6598a62d963SHong Zhang       PetscReal *tmp,*tmp2;
660899cda47SBarry Smith       PetscInt  *jj,*garray=baij->garray,cstart=baij->rstartbs;
661fca92195SBarry Smith       ierr = PetscMalloc2(mat->cmap->N,PetscReal,&tmp,mat->cmap->N,PetscReal,&tmp2);CHKERRQ(ierr);
662d0f46423SBarry Smith       ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr);
6638a62d963SHong Zhang       v = amat->a; jj = amat->j;
6648a62d963SHong Zhang       for (i=0; i<amat->nz; i++) {
6658a62d963SHong Zhang         for (j=0; j<bs; j++){
6668a62d963SHong Zhang           col = bs*(cstart + *jj) + j; /* column index */
6678a62d963SHong Zhang           for (row=0; row<bs; row++){
6688a62d963SHong Zhang             tmp[col] += PetscAbsScalar(*v);  v++;
6698a62d963SHong Zhang           }
6708a62d963SHong Zhang         }
6718a62d963SHong Zhang         jj++;
6728a62d963SHong Zhang       }
6738a62d963SHong Zhang       v = bmat->a; jj = bmat->j;
6748a62d963SHong Zhang       for (i=0; i<bmat->nz; i++) {
6758a62d963SHong Zhang         for (j=0; j<bs; j++){
6768a62d963SHong Zhang           col = bs*garray[*jj] + j;
6778a62d963SHong Zhang           for (row=0; row<bs; row++){
6788a62d963SHong Zhang             tmp[col] += PetscAbsScalar(*v); v++;
6798a62d963SHong Zhang           }
6808a62d963SHong Zhang         }
6818a62d963SHong Zhang         jj++;
6828a62d963SHong Zhang       }
683d0f46423SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr);
6848a62d963SHong Zhang       *nrm = 0.0;
685d0f46423SBarry Smith       for (j=0; j<mat->cmap->N; j++) {
6868a62d963SHong Zhang         if (tmp2[j] > *nrm) *nrm = tmp2[j];
6878a62d963SHong Zhang       }
688fca92195SBarry Smith       ierr = PetscFree2(tmp,tmp2);CHKERRQ(ierr);
6898a62d963SHong Zhang     } else if (type == NORM_INFINITY) { /* max row sum */
690577dd1f9SKris Buschelman       PetscReal *sums;
691cb9801acSJed Brown       ierr = PetscMalloc(bs*sizeof(PetscReal),&sums);CHKERRQ(ierr);
6928a62d963SHong Zhang       sum = 0.0;
6938a62d963SHong Zhang       for (j=0; j<amat->mbs; j++) {
6948a62d963SHong Zhang         for (row=0; row<bs; row++) sums[row] = 0.0;
6958a62d963SHong Zhang         v = amat->a + bs2*amat->i[j];
6968a62d963SHong Zhang         nz = amat->i[j+1]-amat->i[j];
6978a62d963SHong Zhang         for (i=0; i<nz; i++) {
6988a62d963SHong Zhang           for (col=0; col<bs; col++){
6998a62d963SHong Zhang             for (row=0; row<bs; row++){
7008a62d963SHong Zhang               sums[row] += PetscAbsScalar(*v); v++;
7018a62d963SHong Zhang             }
7028a62d963SHong Zhang           }
7038a62d963SHong Zhang         }
7048a62d963SHong Zhang         v = bmat->a + bs2*bmat->i[j];
7058a62d963SHong Zhang         nz = bmat->i[j+1]-bmat->i[j];
7068a62d963SHong Zhang         for (i=0; i<nz; i++) {
7078a62d963SHong Zhang           for (col=0; col<bs; col++){
7088a62d963SHong Zhang             for (row=0; row<bs; row++){
7098a62d963SHong Zhang               sums[row] += PetscAbsScalar(*v); v++;
7108a62d963SHong Zhang             }
7118a62d963SHong Zhang           }
7128a62d963SHong Zhang         }
7138a62d963SHong Zhang         for (row=0; row<bs; row++){
7148a62d963SHong Zhang           if (sums[row] > sum) sum = sums[row];
7158a62d963SHong Zhang         }
7168a62d963SHong Zhang       }
7177adad957SLisandro Dalcin       ierr = MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPI_MAX,((PetscObject)mat)->comm);CHKERRQ(ierr);
718577dd1f9SKris Buschelman       ierr = PetscFree(sums);CHKERRQ(ierr);
719e7e72b3dSBarry Smith     } else SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"No support for this norm yet");
720d64ed03dSBarry Smith   }
7213a40ed3dSBarry Smith   PetscFunctionReturn(0);
722d6de1c52SSatish Balay }
72357b952d6SSatish Balay 
724fef45726SSatish Balay /*
725fef45726SSatish Balay   Creates the hash table, and sets the table
726fef45726SSatish Balay   This table is created only once.
727fef45726SSatish Balay   If new entried need to be added to the matrix
728fef45726SSatish Balay   then the hash table has to be destroyed and
729fef45726SSatish Balay   recreated.
730fef45726SSatish Balay */
7314a2ae208SSatish Balay #undef __FUNCT__
7324a2ae208SSatish Balay #define __FUNCT__ "MatCreateHashTable_MPIBAIJ_Private"
733dfbe8321SBarry Smith PetscErrorCode MatCreateHashTable_MPIBAIJ_Private(Mat mat,PetscReal factor)
734596b8d2eSBarry Smith {
735596b8d2eSBarry Smith   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
736596b8d2eSBarry Smith   Mat            A = baij->A,B=baij->B;
737596b8d2eSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data,*b=(Mat_SeqBAIJ *)B->data;
738b24ad042SBarry Smith   PetscInt       i,j,k,nz=a->nz+b->nz,h1,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
7396849ba73SBarry Smith   PetscErrorCode ierr;
740fca92195SBarry Smith   PetscInt       ht_size,bs2=baij->bs2,rstart=baij->rstartbs;
741899cda47SBarry Smith   PetscInt       cstart=baij->cstartbs,*garray=baij->garray,row,col,Nbs=baij->Nbs;
742b24ad042SBarry Smith   PetscInt       *HT,key;
7433eda8832SBarry Smith   MatScalar      **HD;
744329f5518SBarry Smith   PetscReal      tmp;
7456cf91177SBarry Smith #if defined(PETSC_USE_INFO)
746b24ad042SBarry Smith   PetscInt       ct=0,max=0;
7474a15367fSSatish Balay #endif
748fef45726SSatish Balay 
749d64ed03dSBarry Smith   PetscFunctionBegin;
750fca92195SBarry Smith   if (baij->ht) PetscFunctionReturn(0);
751fef45726SSatish Balay 
752fca92195SBarry Smith   baij->ht_size = (PetscInt)(factor*nz);
753fca92195SBarry Smith   ht_size       = baij->ht_size;
7540bdbc534SSatish Balay 
755fef45726SSatish Balay   /* Allocate Memory for Hash Table */
756fca92195SBarry Smith   ierr = PetscMalloc2(ht_size,MatScalar*,&baij->hd,ht_size,PetscInt,&baij->ht);CHKERRQ(ierr);
757fca92195SBarry Smith   ierr = PetscMemzero(baij->hd,ht_size*sizeof(MatScalar*));CHKERRQ(ierr);
758fca92195SBarry Smith   ierr = PetscMemzero(baij->ht,ht_size*sizeof(PetscInt));CHKERRQ(ierr);
759b9e4cc15SSatish Balay   HD   = baij->hd;
760a07cd24cSSatish Balay   HT   = baij->ht;
761b9e4cc15SSatish Balay 
762596b8d2eSBarry Smith   /* Loop Over A */
7630bdbc534SSatish Balay   for (i=0; i<a->mbs; i++) {
764596b8d2eSBarry Smith     for (j=ai[i]; j<ai[i+1]; j++) {
7650bdbc534SSatish Balay       row = i+rstart;
7660bdbc534SSatish Balay       col = aj[j]+cstart;
767596b8d2eSBarry Smith 
768187ce0cbSSatish Balay       key = row*Nbs + col + 1;
769fca92195SBarry Smith       h1  = HASH(ht_size,key,tmp);
770fca92195SBarry Smith       for (k=0; k<ht_size; k++){
771fca92195SBarry Smith         if (!HT[(h1+k)%ht_size]) {
772fca92195SBarry Smith           HT[(h1+k)%ht_size] = key;
773fca92195SBarry Smith           HD[(h1+k)%ht_size] = a->a + j*bs2;
774596b8d2eSBarry Smith           break;
7756cf91177SBarry Smith #if defined(PETSC_USE_INFO)
776187ce0cbSSatish Balay         } else {
777187ce0cbSSatish Balay           ct++;
778187ce0cbSSatish Balay #endif
779596b8d2eSBarry Smith         }
780187ce0cbSSatish Balay       }
7816cf91177SBarry Smith #if defined(PETSC_USE_INFO)
782187ce0cbSSatish Balay       if (k> max) max = k;
783187ce0cbSSatish Balay #endif
784596b8d2eSBarry Smith     }
785596b8d2eSBarry Smith   }
786596b8d2eSBarry Smith   /* Loop Over B */
7870bdbc534SSatish Balay   for (i=0; i<b->mbs; i++) {
788596b8d2eSBarry Smith     for (j=bi[i]; j<bi[i+1]; j++) {
7890bdbc534SSatish Balay       row = i+rstart;
7900bdbc534SSatish Balay       col = garray[bj[j]];
791187ce0cbSSatish Balay       key = row*Nbs + col + 1;
792fca92195SBarry Smith       h1  = HASH(ht_size,key,tmp);
793fca92195SBarry Smith       for (k=0; k<ht_size; k++){
794fca92195SBarry Smith         if (!HT[(h1+k)%ht_size]) {
795fca92195SBarry Smith           HT[(h1+k)%ht_size] = key;
796fca92195SBarry Smith           HD[(h1+k)%ht_size] = b->a + j*bs2;
797596b8d2eSBarry Smith           break;
7986cf91177SBarry Smith #if defined(PETSC_USE_INFO)
799187ce0cbSSatish Balay         } else {
800187ce0cbSSatish Balay           ct++;
801187ce0cbSSatish Balay #endif
802596b8d2eSBarry Smith         }
803187ce0cbSSatish Balay       }
8046cf91177SBarry Smith #if defined(PETSC_USE_INFO)
805187ce0cbSSatish Balay       if (k> max) max = k;
806187ce0cbSSatish Balay #endif
807596b8d2eSBarry Smith     }
808596b8d2eSBarry Smith   }
809596b8d2eSBarry Smith 
810596b8d2eSBarry Smith   /* Print Summary */
8116cf91177SBarry Smith #if defined(PETSC_USE_INFO)
812fca92195SBarry Smith   for (i=0,j=0; i<ht_size; i++) {
813596b8d2eSBarry Smith     if (HT[i]) {j++;}
814c38d4ed2SBarry Smith   }
8151e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Average Search = %5.2f,max search = %D\n",(!j)? 0.0:((PetscReal)(ct+j))/j,max);CHKERRQ(ierr);
816187ce0cbSSatish Balay #endif
8173a40ed3dSBarry Smith   PetscFunctionReturn(0);
818596b8d2eSBarry Smith }
81957b952d6SSatish Balay 
8204a2ae208SSatish Balay #undef __FUNCT__
8214a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIBAIJ"
822dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIBAIJ(Mat mat,MatAssemblyType mode)
823bbb85fb3SSatish Balay {
824bbb85fb3SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
825dfbe8321SBarry Smith   PetscErrorCode ierr;
826b24ad042SBarry Smith   PetscInt       nstash,reallocs;
827bbb85fb3SSatish Balay   InsertMode     addv;
828bbb85fb3SSatish Balay 
829bbb85fb3SSatish Balay   PetscFunctionBegin;
830bbb85fb3SSatish Balay   if (baij->donotstash) {
831bbb85fb3SSatish Balay     PetscFunctionReturn(0);
832bbb85fb3SSatish Balay   }
833bbb85fb3SSatish Balay 
834bbb85fb3SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
8357adad957SLisandro Dalcin   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,((PetscObject)mat)->comm);CHKERRQ(ierr);
836e7e72b3dSBarry Smith   if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
837bbb85fb3SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
838bbb85fb3SSatish Balay 
839d0f46423SBarry Smith   ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr);
8401e2582c4SBarry Smith   ierr = MatStashScatterBegin_Private(mat,&mat->bstash,baij->rangebs);CHKERRQ(ierr);
8418798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
8421e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Stash has %D entries,uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
84346680499SSatish Balay   ierr = MatStashGetInfo_Private(&mat->bstash,&nstash,&reallocs);CHKERRQ(ierr);
8441e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
845bbb85fb3SSatish Balay   PetscFunctionReturn(0);
846bbb85fb3SSatish Balay }
847bbb85fb3SSatish Balay 
8484a2ae208SSatish Balay #undef __FUNCT__
8494a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIBAIJ"
850dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIBAIJ(Mat mat,MatAssemblyType mode)
851bbb85fb3SSatish Balay {
852bbb85fb3SSatish Balay   Mat_MPIBAIJ    *baij=(Mat_MPIBAIJ*)mat->data;
85391c97fd4SSatish Balay   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ*)baij->A->data;
8546849ba73SBarry Smith   PetscErrorCode ierr;
855b24ad042SBarry Smith   PetscInt       i,j,rstart,ncols,flg,bs2=baij->bs2;
856e44c0bd4SBarry Smith   PetscInt       *row,*col;
857e44c0bd4SBarry Smith   PetscTruth     r1,r2,r3,other_disassembled;
8583eda8832SBarry Smith   MatScalar      *val;
859bbb85fb3SSatish Balay   InsertMode     addv = mat->insertmode;
860b24ad042SBarry Smith   PetscMPIInt    n;
861bbb85fb3SSatish Balay 
86291c97fd4SSatish Balay   /* do not use 'b=(Mat_SeqBAIJ*)baij->B->data' as B can be reset in disassembly */
863bbb85fb3SSatish Balay   PetscFunctionBegin;
864bbb85fb3SSatish Balay   if (!baij->donotstash) {
865a2d1c673SSatish Balay     while (1) {
8668798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
867a2d1c673SSatish Balay       if (!flg) break;
868a2d1c673SSatish Balay 
869bbb85fb3SSatish Balay       for (i=0; i<n;) {
870bbb85fb3SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
871bbb85fb3SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
872bbb85fb3SSatish Balay         if (j < n) ncols = j-i;
873bbb85fb3SSatish Balay         else       ncols = n-i;
874bbb85fb3SSatish Balay         /* Now assemble all these values with a single function call */
87597e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
876bbb85fb3SSatish Balay         i = j;
877bbb85fb3SSatish Balay       }
878bbb85fb3SSatish Balay     }
8798798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
880a2d1c673SSatish Balay     /* Now process the block-stash. Since the values are stashed column-oriented,
881a2d1c673SSatish Balay        set the roworiented flag to column oriented, and after MatSetValues()
882a2d1c673SSatish Balay        restore the original flags */
883a2d1c673SSatish Balay     r1 = baij->roworiented;
884a2d1c673SSatish Balay     r2 = a->roworiented;
88591c97fd4SSatish Balay     r3 = ((Mat_SeqBAIJ*)baij->B->data)->roworiented;
8867c922b88SBarry Smith     baij->roworiented = PETSC_FALSE;
8877c922b88SBarry Smith     a->roworiented    = PETSC_FALSE;
88891c97fd4SSatish Balay     (((Mat_SeqBAIJ*)baij->B->data))->roworiented    = PETSC_FALSE; /* b->roworiented */
889a2d1c673SSatish Balay     while (1) {
8908798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->bstash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
891a2d1c673SSatish Balay       if (!flg) break;
892a2d1c673SSatish Balay 
893a2d1c673SSatish Balay       for (i=0; i<n;) {
894a2d1c673SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
895a2d1c673SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
896a2d1c673SSatish Balay         if (j < n) ncols = j-i;
897a2d1c673SSatish Balay         else       ncols = n-i;
89897e5c40aSBarry Smith         ierr = MatSetValuesBlocked_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i*bs2,addv);CHKERRQ(ierr);
899a2d1c673SSatish Balay         i = j;
900a2d1c673SSatish Balay       }
901a2d1c673SSatish Balay     }
9028798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->bstash);CHKERRQ(ierr);
903a2d1c673SSatish Balay     baij->roworiented = r1;
904a2d1c673SSatish Balay     a->roworiented    = r2;
90591c97fd4SSatish Balay     ((Mat_SeqBAIJ*)baij->B->data)->roworiented    = r3; /* b->roworiented */
906bbb85fb3SSatish Balay   }
907bbb85fb3SSatish Balay 
908bbb85fb3SSatish Balay   ierr = MatAssemblyBegin(baij->A,mode);CHKERRQ(ierr);
909bbb85fb3SSatish Balay   ierr = MatAssemblyEnd(baij->A,mode);CHKERRQ(ierr);
910bbb85fb3SSatish Balay 
911bbb85fb3SSatish Balay   /* determine if any processor has disassembled, if so we must
912bbb85fb3SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
913bbb85fb3SSatish Balay   /*
914bbb85fb3SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
915bbb85fb3SSatish Balay      no processor disassembled thus we can skip this stuff
916bbb85fb3SSatish Balay   */
917bbb85fb3SSatish Balay   if (!((Mat_SeqBAIJ*)baij->B->data)->nonew)  {
9187adad957SLisandro Dalcin     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,((PetscObject)mat)->comm);CHKERRQ(ierr);
919bbb85fb3SSatish Balay     if (mat->was_assembled && !other_disassembled) {
920bbb85fb3SSatish Balay       ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
921bbb85fb3SSatish Balay     }
922bbb85fb3SSatish Balay   }
923bbb85fb3SSatish Balay 
924bbb85fb3SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
925bbb85fb3SSatish Balay     ierr = MatSetUpMultiply_MPIBAIJ(mat);CHKERRQ(ierr);
926bbb85fb3SSatish Balay   }
92791c97fd4SSatish Balay   ((Mat_SeqBAIJ*)baij->B->data)->compressedrow.use = PETSC_TRUE; /* b->compressedrow.use */
928bbb85fb3SSatish Balay   ierr = MatAssemblyBegin(baij->B,mode);CHKERRQ(ierr);
929bbb85fb3SSatish Balay   ierr = MatAssemblyEnd(baij->B,mode);CHKERRQ(ierr);
930bbb85fb3SSatish Balay 
9316cf91177SBarry Smith #if defined(PETSC_USE_INFO)
932bbb85fb3SSatish Balay   if (baij->ht && mode== MAT_FINAL_ASSEMBLY) {
9331e2582c4SBarry Smith     ierr = PetscInfo1(mat,"Average Hash Table Search in MatSetValues = %5.2f\n",((PetscReal)baij->ht_total_ct)/baij->ht_insert_ct);CHKERRQ(ierr);
934bbb85fb3SSatish Balay     baij->ht_total_ct  = 0;
935bbb85fb3SSatish Balay     baij->ht_insert_ct = 0;
936bbb85fb3SSatish Balay   }
937bbb85fb3SSatish Balay #endif
938bbb85fb3SSatish Balay   if (baij->ht_flag && !baij->ht && mode == MAT_FINAL_ASSEMBLY) {
939bbb85fb3SSatish Balay     ierr = MatCreateHashTable_MPIBAIJ_Private(mat,baij->ht_fact);CHKERRQ(ierr);
940bbb85fb3SSatish Balay     mat->ops->setvalues        = MatSetValues_MPIBAIJ_HT;
941bbb85fb3SSatish Balay     mat->ops->setvaluesblocked = MatSetValuesBlocked_MPIBAIJ_HT;
942bbb85fb3SSatish Balay   }
943bbb85fb3SSatish Balay 
944fca92195SBarry Smith   ierr = PetscFree2(baij->rowvalues,baij->rowindices);CHKERRQ(ierr);
945606d414cSSatish Balay   baij->rowvalues = 0;
946bbb85fb3SSatish Balay   PetscFunctionReturn(0);
947bbb85fb3SSatish Balay }
94857b952d6SSatish Balay 
9494a2ae208SSatish Balay #undef __FUNCT__
9504a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIBAIJ_ASCIIorDraworSocket"
9516849ba73SBarry Smith static PetscErrorCode MatView_MPIBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
95257b952d6SSatish Balay {
95357b952d6SSatish Balay   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
954dfbe8321SBarry Smith   PetscErrorCode    ierr;
955b24ad042SBarry Smith   PetscMPIInt       size = baij->size,rank = baij->rank;
956d0f46423SBarry Smith   PetscInt          bs = mat->rmap->bs;
95732077d6dSBarry Smith   PetscTruth        iascii,isdraw;
958b0a32e0cSBarry Smith   PetscViewer       sviewer;
959f3ef73ceSBarry Smith   PetscViewerFormat format;
96057b952d6SSatish Balay 
961d64ed03dSBarry Smith   PetscFunctionBegin;
9622692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
9632692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
96432077d6dSBarry Smith   if (iascii) {
965b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
966456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
9674e220ebcSLois Curfman McInnes       MatInfo info;
9687adad957SLisandro Dalcin       ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr);
969d41123aaSBarry Smith       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
97077431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D bs %D mem %D\n",
971d0f46423SBarry Smith               rank,mat->rmap->N,(PetscInt)info.nz_used*bs,(PetscInt)info.nz_allocated*bs,
972d0f46423SBarry Smith               mat->rmap->bs,(PetscInt)info.memory);CHKERRQ(ierr);
973d132466eSBarry Smith       ierr = MatGetInfo(baij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
97477431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used*bs);CHKERRQ(ierr);
975d132466eSBarry Smith       ierr = MatGetInfo(baij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
97677431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used*bs);CHKERRQ(ierr);
977b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
97807d81ca4SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr);
97957b952d6SSatish Balay       ierr = VecScatterView(baij->Mvctx,viewer);CHKERRQ(ierr);
9803a40ed3dSBarry Smith       PetscFunctionReturn(0);
981fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
98277431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);CHKERRQ(ierr);
9833a40ed3dSBarry Smith       PetscFunctionReturn(0);
98404929863SHong Zhang     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
98504929863SHong Zhang       PetscFunctionReturn(0);
98657b952d6SSatish Balay     }
98757b952d6SSatish Balay   }
98857b952d6SSatish Balay 
9890f5bd95cSBarry Smith   if (isdraw) {
990b0a32e0cSBarry Smith     PetscDraw       draw;
99157b952d6SSatish Balay     PetscTruth isnull;
992b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
993b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
99457b952d6SSatish Balay   }
99557b952d6SSatish Balay 
99657b952d6SSatish Balay   if (size == 1) {
9977adad957SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)baij->A,((PetscObject)mat)->name);CHKERRQ(ierr);
99857b952d6SSatish Balay     ierr = MatView(baij->A,viewer);CHKERRQ(ierr);
999d64ed03dSBarry Smith   } else {
100057b952d6SSatish Balay     /* assemble the entire matrix onto first processor. */
100157b952d6SSatish Balay     Mat         A;
100257b952d6SSatish Balay     Mat_SeqBAIJ *Aloc;
1003d0f46423SBarry Smith     PetscInt    M = mat->rmap->N,N = mat->cmap->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs;
10043eda8832SBarry Smith     MatScalar   *a;
100557b952d6SSatish Balay 
1006f204ca49SKris Buschelman     /* Here we are creating a temporary matrix, so will assume MPIBAIJ is acceptable */
1007f204ca49SKris Buschelman     /* Perhaps this should be the type of mat? */
10087adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)mat)->comm,&A);CHKERRQ(ierr);
100957b952d6SSatish Balay     if (!rank) {
1010f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr);
1011d64ed03dSBarry Smith     } else {
1012f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr);
101357b952d6SSatish Balay     }
1014f204ca49SKris Buschelman     ierr = MatSetType(A,MATMPIBAIJ);CHKERRQ(ierr);
1015d0f46423SBarry Smith     ierr = MatMPIBAIJSetPreallocation(A,mat->rmap->bs,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
101652e6d16bSBarry Smith     ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr);
101757b952d6SSatish Balay 
101857b952d6SSatish Balay     /* copy over the A part */
101957b952d6SSatish Balay     Aloc = (Mat_SeqBAIJ*)baij->A->data;
102057b952d6SSatish Balay     ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1021b24ad042SBarry Smith     ierr = PetscMalloc(bs*sizeof(PetscInt),&rvals);CHKERRQ(ierr);
102257b952d6SSatish Balay 
102357b952d6SSatish Balay     for (i=0; i<mbs; i++) {
1024899cda47SBarry Smith       rvals[0] = bs*(baij->rstartbs + i);
102557b952d6SSatish Balay       for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
102657b952d6SSatish Balay       for (j=ai[i]; j<ai[i+1]; j++) {
1027899cda47SBarry Smith         col = (baij->cstartbs+aj[j])*bs;
102857b952d6SSatish Balay         for (k=0; k<bs; k++) {
102997e5c40aSBarry Smith           ierr = MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);CHKERRQ(ierr);
1030cee3aa6bSSatish Balay           col++; a += bs;
103157b952d6SSatish Balay         }
103257b952d6SSatish Balay       }
103357b952d6SSatish Balay     }
103457b952d6SSatish Balay     /* copy over the B part */
103557b952d6SSatish Balay     Aloc = (Mat_SeqBAIJ*)baij->B->data;
103657b952d6SSatish Balay     ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
103757b952d6SSatish Balay     for (i=0; i<mbs; i++) {
1038899cda47SBarry Smith       rvals[0] = bs*(baij->rstartbs + i);
103957b952d6SSatish Balay       for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
104057b952d6SSatish Balay       for (j=ai[i]; j<ai[i+1]; j++) {
104157b952d6SSatish Balay         col = baij->garray[aj[j]]*bs;
104257b952d6SSatish Balay         for (k=0; k<bs; k++) {
104397e5c40aSBarry Smith           ierr = MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);CHKERRQ(ierr);
1044cee3aa6bSSatish Balay           col++; a += bs;
104557b952d6SSatish Balay         }
104657b952d6SSatish Balay       }
104757b952d6SSatish Balay     }
1048606d414cSSatish Balay     ierr = PetscFree(rvals);CHKERRQ(ierr);
10496d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10506d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
105155843e3eSBarry Smith     /*
105255843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
1053b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
105455843e3eSBarry Smith     */
1055b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
1056f14a1c24SBarry Smith     if (!rank) {
10577adad957SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject)((Mat_MPIBAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr);
10586831982aSBarry Smith       ierr = MatView(((Mat_MPIBAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
105957b952d6SSatish Balay     }
1060b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
106157b952d6SSatish Balay     ierr = MatDestroy(A);CHKERRQ(ierr);
106257b952d6SSatish Balay   }
10633a40ed3dSBarry Smith   PetscFunctionReturn(0);
106457b952d6SSatish Balay }
106557b952d6SSatish Balay 
10664a2ae208SSatish Balay #undef __FUNCT__
10674a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIBAIJ"
1068dfbe8321SBarry Smith PetscErrorCode MatView_MPIBAIJ(Mat mat,PetscViewer viewer)
106957b952d6SSatish Balay {
1070dfbe8321SBarry Smith   PetscErrorCode ierr;
107132077d6dSBarry Smith   PetscTruth     iascii,isdraw,issocket,isbinary;
107257b952d6SSatish Balay 
1073d64ed03dSBarry Smith   PetscFunctionBegin;
10742692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
10752692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
10762692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr);
10772692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
107832077d6dSBarry Smith   if (iascii || isdraw || issocket || isbinary) {
10797b2a1423SBarry Smith     ierr = MatView_MPIBAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
10805cd90555SBarry Smith   } else {
108165e19b50SBarry Smith     SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Viewer type %s not supported by MPIBAIJ matrices",((PetscObject)viewer)->type_name);
108257b952d6SSatish Balay   }
10833a40ed3dSBarry Smith   PetscFunctionReturn(0);
108457b952d6SSatish Balay }
108557b952d6SSatish Balay 
10864a2ae208SSatish Balay #undef __FUNCT__
10874a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIBAIJ"
1088dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIBAIJ(Mat mat)
108979bdfe76SSatish Balay {
109079bdfe76SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
1091dfbe8321SBarry Smith   PetscErrorCode ierr;
109279bdfe76SSatish Balay 
1093d64ed03dSBarry Smith   PetscFunctionBegin;
1094aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1095d0f46423SBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%D,Cols=%D",mat->rmap->N,mat->cmap->N);
109679bdfe76SSatish Balay #endif
10978798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
10988798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->bstash);CHKERRQ(ierr);
109979bdfe76SSatish Balay   ierr = MatDestroy(baij->A);CHKERRQ(ierr);
110079bdfe76SSatish Balay   ierr = MatDestroy(baij->B);CHKERRQ(ierr);
1101aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
11029c666560SBarry Smith   if (baij->colmap) {ierr = PetscTableDestroy(baij->colmap);CHKERRQ(ierr);}
110348e59246SSatish Balay #else
110405b42c5fSBarry Smith   ierr = PetscFree(baij->colmap);CHKERRQ(ierr);
110548e59246SSatish Balay #endif
110605b42c5fSBarry Smith   ierr = PetscFree(baij->garray);CHKERRQ(ierr);
1107606d414cSSatish Balay   if (baij->lvec)   {ierr = VecDestroy(baij->lvec);CHKERRQ(ierr);}
1108606d414cSSatish Balay   if (baij->Mvctx)  {ierr = VecScatterDestroy(baij->Mvctx);CHKERRQ(ierr);}
1109fca92195SBarry Smith   ierr = PetscFree2(baij->rowvalues,baij->rowindices);CHKERRQ(ierr);
111005b42c5fSBarry Smith   ierr = PetscFree(baij->barray);CHKERRQ(ierr);
1111fca92195SBarry Smith   ierr = PetscFree2(baij->hd,baij->ht);CHKERRQ(ierr);
1112899cda47SBarry Smith   ierr = PetscFree(baij->rangebs);CHKERRQ(ierr);
1113606d414cSSatish Balay   ierr = PetscFree(baij);CHKERRQ(ierr);
1114901853e0SKris Buschelman 
1115dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr);
1116901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
1117901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
1118901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);CHKERRQ(ierr);
1119901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
1120aac34f13SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
1121901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);CHKERRQ(ierr);
1122901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatSetHashTableFactor_C","",PETSC_NULL);CHKERRQ(ierr);
11233a40ed3dSBarry Smith   PetscFunctionReturn(0);
112479bdfe76SSatish Balay }
112579bdfe76SSatish Balay 
11264a2ae208SSatish Balay #undef __FUNCT__
11274a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIBAIJ"
1128dfbe8321SBarry Smith PetscErrorCode MatMult_MPIBAIJ(Mat A,Vec xx,Vec yy)
1129cee3aa6bSSatish Balay {
1130cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1131dfbe8321SBarry Smith   PetscErrorCode ierr;
1132b24ad042SBarry Smith   PetscInt       nt;
1133cee3aa6bSSatish Balay 
1134d64ed03dSBarry Smith   PetscFunctionBegin;
1135e1311b90SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
1136e7e72b3dSBarry Smith   if (nt != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");
1137e1311b90SBarry Smith   ierr = VecGetLocalSize(yy,&nt);CHKERRQ(ierr);
1138e7e72b3dSBarry Smith   if (nt != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible parition of A and yy");
1139ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1140f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
1141ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1142f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
11433a40ed3dSBarry Smith   PetscFunctionReturn(0);
1144cee3aa6bSSatish Balay }
1145cee3aa6bSSatish Balay 
11464a2ae208SSatish Balay #undef __FUNCT__
11474a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIBAIJ"
1148dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1149cee3aa6bSSatish Balay {
1150cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1151dfbe8321SBarry Smith   PetscErrorCode ierr;
1152d64ed03dSBarry Smith 
1153d64ed03dSBarry Smith   PetscFunctionBegin;
1154ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1155f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1156ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1157f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
11583a40ed3dSBarry Smith   PetscFunctionReturn(0);
1159cee3aa6bSSatish Balay }
1160cee3aa6bSSatish Balay 
11614a2ae208SSatish Balay #undef __FUNCT__
11624a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIBAIJ"
1163dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIBAIJ(Mat A,Vec xx,Vec yy)
1164cee3aa6bSSatish Balay {
1165cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1166dfbe8321SBarry Smith   PetscErrorCode ierr;
1167a5ff213dSBarry Smith   PetscTruth     merged;
1168cee3aa6bSSatish Balay 
1169d64ed03dSBarry Smith   PetscFunctionBegin;
1170a5ff213dSBarry Smith   ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr);
1171cee3aa6bSSatish Balay   /* do nondiagonal part */
11727c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1173a5ff213dSBarry Smith   if (!merged) {
1174cee3aa6bSSatish Balay     /* send it on its way */
1175ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1176cee3aa6bSSatish Balay     /* do local part */
11777c922b88SBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1178cee3aa6bSSatish Balay     /* receive remote parts: note this assumes the values are not actually */
1179a5ff213dSBarry Smith     /* inserted in yy until the next line */
1180ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1181a5ff213dSBarry Smith   } else {
1182a5ff213dSBarry Smith     /* do local part */
1183a5ff213dSBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1184a5ff213dSBarry Smith     /* send it on its way */
1185ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1186a5ff213dSBarry Smith     /* values actually were received in the Begin() but we need to call this nop */
1187ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1188a5ff213dSBarry Smith   }
11893a40ed3dSBarry Smith   PetscFunctionReturn(0);
1190cee3aa6bSSatish Balay }
1191cee3aa6bSSatish Balay 
11924a2ae208SSatish Balay #undef __FUNCT__
11934a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIBAIJ"
1194dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1195cee3aa6bSSatish Balay {
1196cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1197dfbe8321SBarry Smith   PetscErrorCode ierr;
1198cee3aa6bSSatish Balay 
1199d64ed03dSBarry Smith   PetscFunctionBegin;
1200cee3aa6bSSatish Balay   /* do nondiagonal part */
12017c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1202cee3aa6bSSatish Balay   /* send it on its way */
1203ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1204cee3aa6bSSatish Balay   /* do local part */
12057c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1206cee3aa6bSSatish Balay   /* receive remote parts: note this assumes the values are not actually */
1207cee3aa6bSSatish Balay   /* inserted in yy until the next line, which is true for my implementation*/
1208cee3aa6bSSatish Balay   /* but is not perhaps always true. */
1209ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
12103a40ed3dSBarry Smith   PetscFunctionReturn(0);
1211cee3aa6bSSatish Balay }
1212cee3aa6bSSatish Balay 
1213cee3aa6bSSatish Balay /*
1214cee3aa6bSSatish Balay   This only works correctly for square matrices where the subblock A->A is the
1215cee3aa6bSSatish Balay    diagonal block
1216cee3aa6bSSatish Balay */
12174a2ae208SSatish Balay #undef __FUNCT__
12184a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIBAIJ"
1219dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIBAIJ(Mat A,Vec v)
1220cee3aa6bSSatish Balay {
1221cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1222dfbe8321SBarry Smith   PetscErrorCode ierr;
1223d64ed03dSBarry Smith 
1224d64ed03dSBarry Smith   PetscFunctionBegin;
1225e32f2f54SBarry Smith   if (A->rmap->N != A->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
12263a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
12273a40ed3dSBarry Smith   PetscFunctionReturn(0);
1228cee3aa6bSSatish Balay }
1229cee3aa6bSSatish Balay 
12304a2ae208SSatish Balay #undef __FUNCT__
12314a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIBAIJ"
1232f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIBAIJ(Mat A,PetscScalar aa)
1233cee3aa6bSSatish Balay {
1234cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1235dfbe8321SBarry Smith   PetscErrorCode ierr;
1236d64ed03dSBarry Smith 
1237d64ed03dSBarry Smith   PetscFunctionBegin;
1238f4df32b1SMatthew Knepley   ierr = MatScale(a->A,aa);CHKERRQ(ierr);
1239f4df32b1SMatthew Knepley   ierr = MatScale(a->B,aa);CHKERRQ(ierr);
12403a40ed3dSBarry Smith   PetscFunctionReturn(0);
1241cee3aa6bSSatish Balay }
1242026e39d0SSatish Balay 
12434a2ae208SSatish Balay #undef __FUNCT__
12444a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIBAIJ"
1245b24ad042SBarry Smith PetscErrorCode MatGetRow_MPIBAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1246acdf5bf4SSatish Balay {
1247acdf5bf4SSatish Balay   Mat_MPIBAIJ    *mat = (Mat_MPIBAIJ*)matin->data;
124887828ca2SBarry Smith   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
12496849ba73SBarry Smith   PetscErrorCode ierr;
1250d0f46423SBarry Smith   PetscInt       bs = matin->rmap->bs,bs2 = mat->bs2,i,*cworkA,*cworkB,**pcA,**pcB;
1251d0f46423SBarry Smith   PetscInt       nztot,nzA,nzB,lrow,brstart = matin->rmap->rstart,brend = matin->rmap->rend;
1252899cda47SBarry Smith   PetscInt       *cmap,*idx_p,cstart = mat->cstartbs;
1253acdf5bf4SSatish Balay 
1254d64ed03dSBarry Smith   PetscFunctionBegin;
1255e7e72b3dSBarry Smith   if (row < brstart || row >= brend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local rows");
1256e32f2f54SBarry Smith   if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active");
1257acdf5bf4SSatish Balay   mat->getrowactive = PETSC_TRUE;
1258acdf5bf4SSatish Balay 
1259acdf5bf4SSatish Balay   if (!mat->rowvalues && (idx || v)) {
1260acdf5bf4SSatish Balay     /*
1261acdf5bf4SSatish Balay         allocate enough space to hold information from the longest row.
1262acdf5bf4SSatish Balay     */
1263acdf5bf4SSatish Balay     Mat_SeqBAIJ *Aa = (Mat_SeqBAIJ*)mat->A->data,*Ba = (Mat_SeqBAIJ*)mat->B->data;
1264b24ad042SBarry Smith     PetscInt     max = 1,mbs = mat->mbs,tmp;
1265bd16c2feSSatish Balay     for (i=0; i<mbs; i++) {
1266acdf5bf4SSatish Balay       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1267acdf5bf4SSatish Balay       if (max < tmp) { max = tmp; }
1268acdf5bf4SSatish Balay     }
1269fca92195SBarry Smith     ierr = PetscMalloc2(max*bs2,PetscScalar,&mat->rowvalues,max*bs2,PetscInt,&mat->rowindices);CHKERRQ(ierr);
1270acdf5bf4SSatish Balay   }
1271d9d09a02SSatish Balay   lrow = row - brstart;
1272acdf5bf4SSatish Balay 
1273acdf5bf4SSatish Balay   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1274acdf5bf4SSatish Balay   if (!v)   {pvA = 0; pvB = 0;}
1275acdf5bf4SSatish Balay   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1276f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1277f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1278acdf5bf4SSatish Balay   nztot = nzA + nzB;
1279acdf5bf4SSatish Balay 
1280acdf5bf4SSatish Balay   cmap  = mat->garray;
1281acdf5bf4SSatish Balay   if (v  || idx) {
1282acdf5bf4SSatish Balay     if (nztot) {
1283acdf5bf4SSatish Balay       /* Sort by increasing column numbers, assuming A and B already sorted */
1284b24ad042SBarry Smith       PetscInt imark = -1;
1285acdf5bf4SSatish Balay       if (v) {
1286acdf5bf4SSatish Balay         *v = v_p = mat->rowvalues;
1287acdf5bf4SSatish Balay         for (i=0; i<nzB; i++) {
1288d9d09a02SSatish Balay           if (cmap[cworkB[i]/bs] < cstart)   v_p[i] = vworkB[i];
1289acdf5bf4SSatish Balay           else break;
1290acdf5bf4SSatish Balay         }
1291acdf5bf4SSatish Balay         imark = i;
1292acdf5bf4SSatish Balay         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1293acdf5bf4SSatish Balay         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1294acdf5bf4SSatish Balay       }
1295acdf5bf4SSatish Balay       if (idx) {
1296acdf5bf4SSatish Balay         *idx = idx_p = mat->rowindices;
1297acdf5bf4SSatish Balay         if (imark > -1) {
1298acdf5bf4SSatish Balay           for (i=0; i<imark; i++) {
1299bd16c2feSSatish Balay             idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1300acdf5bf4SSatish Balay           }
1301acdf5bf4SSatish Balay         } else {
1302acdf5bf4SSatish Balay           for (i=0; i<nzB; i++) {
1303d9d09a02SSatish Balay             if (cmap[cworkB[i]/bs] < cstart)
1304d9d09a02SSatish Balay               idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1305acdf5bf4SSatish Balay             else break;
1306acdf5bf4SSatish Balay           }
1307acdf5bf4SSatish Balay           imark = i;
1308acdf5bf4SSatish Balay         }
1309d9d09a02SSatish Balay         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart*bs + cworkA[i];
1310d9d09a02SSatish Balay         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1311acdf5bf4SSatish Balay       }
1312d64ed03dSBarry Smith     } else {
1313d212a18eSSatish Balay       if (idx) *idx = 0;
1314d212a18eSSatish Balay       if (v)   *v   = 0;
1315d212a18eSSatish Balay     }
1316acdf5bf4SSatish Balay   }
1317acdf5bf4SSatish Balay   *nz = nztot;
1318f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1319f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
13203a40ed3dSBarry Smith   PetscFunctionReturn(0);
1321acdf5bf4SSatish Balay }
1322acdf5bf4SSatish Balay 
13234a2ae208SSatish Balay #undef __FUNCT__
13244a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIBAIJ"
1325b24ad042SBarry Smith PetscErrorCode MatRestoreRow_MPIBAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1326acdf5bf4SSatish Balay {
1327acdf5bf4SSatish Balay   Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1328d64ed03dSBarry Smith 
1329d64ed03dSBarry Smith   PetscFunctionBegin;
1330e7e72b3dSBarry Smith   if (!baij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
1331acdf5bf4SSatish Balay   baij->getrowactive = PETSC_FALSE;
13323a40ed3dSBarry Smith   PetscFunctionReturn(0);
1333acdf5bf4SSatish Balay }
1334acdf5bf4SSatish Balay 
13354a2ae208SSatish Balay #undef __FUNCT__
13364a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIBAIJ"
1337dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIBAIJ(Mat A)
133858667388SSatish Balay {
133958667388SSatish Balay   Mat_MPIBAIJ    *l = (Mat_MPIBAIJ*)A->data;
1340dfbe8321SBarry Smith   PetscErrorCode ierr;
1341d64ed03dSBarry Smith 
1342d64ed03dSBarry Smith   PetscFunctionBegin;
134358667388SSatish Balay   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
134458667388SSatish Balay   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
13453a40ed3dSBarry Smith   PetscFunctionReturn(0);
134658667388SSatish Balay }
13470ac07820SSatish Balay 
13484a2ae208SSatish Balay #undef __FUNCT__
13494a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIBAIJ"
1350dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIBAIJ(Mat matin,MatInfoType flag,MatInfo *info)
13510ac07820SSatish Balay {
13524e220ebcSLois Curfman McInnes   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)matin->data;
13534e220ebcSLois Curfman McInnes   Mat            A = a->A,B = a->B;
1354dfbe8321SBarry Smith   PetscErrorCode ierr;
1355329f5518SBarry Smith   PetscReal      isend[5],irecv[5];
13560ac07820SSatish Balay 
1357d64ed03dSBarry Smith   PetscFunctionBegin;
1358d0f46423SBarry Smith   info->block_size     = (PetscReal)matin->rmap->bs;
13594e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
13600e4b21beSBarry Smith   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1361de87f314SBarry Smith   isend[3] = info->memory;  isend[4] = info->mallocs;
13624e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
13630e4b21beSBarry Smith   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1364de87f314SBarry Smith   isend[3] += info->memory;  isend[4] += info->mallocs;
13650ac07820SSatish Balay   if (flag == MAT_LOCAL) {
13664e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
13674e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
13684e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
13694e220ebcSLois Curfman McInnes     info->memory       = isend[3];
13704e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
13710ac07820SSatish Balay   } else if (flag == MAT_GLOBAL_MAX) {
13727adad957SLisandro Dalcin     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,((PetscObject)matin)->comm);CHKERRQ(ierr);
13734e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
13744e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
13754e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
13764e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
13774e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
13780ac07820SSatish Balay   } else if (flag == MAT_GLOBAL_SUM) {
13797adad957SLisandro Dalcin     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,((PetscObject)matin)->comm);CHKERRQ(ierr);
13804e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
13814e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
13824e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
13834e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
13844e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1385d41123aaSBarry Smith   } else {
138665e19b50SBarry Smith     SETERRQ1(((PetscObject)matin)->comm,PETSC_ERR_ARG_WRONG,"Unknown MatInfoType argument %d",(int)flag);
13870ac07820SSatish Balay   }
13884e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
13894e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
13904e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
13913a40ed3dSBarry Smith   PetscFunctionReturn(0);
13920ac07820SSatish Balay }
13930ac07820SSatish Balay 
13944a2ae208SSatish Balay #undef __FUNCT__
13954a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIBAIJ"
13964e0d8c25SBarry Smith PetscErrorCode MatSetOption_MPIBAIJ(Mat A,MatOption op,PetscTruth flg)
139758667388SSatish Balay {
139858667388SSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1399dfbe8321SBarry Smith   PetscErrorCode ierr;
140058667388SSatish Balay 
1401d64ed03dSBarry Smith   PetscFunctionBegin;
140212c028f9SKris Buschelman   switch (op) {
1403512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
140412c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
140528b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1406a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
140712c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
14084e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
14094e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
141012c028f9SKris Buschelman     break;
141112c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
14124e0d8c25SBarry Smith     a->roworiented = flg;
14134e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
14144e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
141512c028f9SKris Buschelman     break;
14164e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1417290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
141812c028f9SKris Buschelman     break;
141912c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
14204e0d8c25SBarry Smith     a->donotstash = flg;
142112c028f9SKris Buschelman     break;
142212c028f9SKris Buschelman   case MAT_USE_HASH_TABLE:
14234e0d8c25SBarry Smith     a->ht_flag = flg;
142412c028f9SKris Buschelman     break;
142577e54ba9SKris Buschelman   case MAT_SYMMETRIC:
142677e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
14272188ac68SBarry Smith   case MAT_HERMITIAN:
14282188ac68SBarry Smith   case MAT_SYMMETRY_ETERNAL:
14294e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
143077e54ba9SKris Buschelman     break;
143112c028f9SKris Buschelman   default:
143265e19b50SBarry Smith     SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"unknown option %d",op);
1433d64ed03dSBarry Smith   }
14343a40ed3dSBarry Smith   PetscFunctionReturn(0);
143558667388SSatish Balay }
143658667388SSatish Balay 
14374a2ae208SSatish Balay #undef __FUNCT__
14384a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIBAIJ("
1439fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIBAIJ(Mat A,MatReuse reuse,Mat *matout)
14400ac07820SSatish Balay {
14410ac07820SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)A->data;
14420ac07820SSatish Balay   Mat_SeqBAIJ    *Aloc;
14430ac07820SSatish Balay   Mat            B;
1444dfbe8321SBarry Smith   PetscErrorCode ierr;
1445d0f46423SBarry Smith   PetscInt       M=A->rmap->N,N=A->cmap->N,*ai,*aj,i,*rvals,j,k,col;
1446d0f46423SBarry Smith   PetscInt       bs=A->rmap->bs,mbs=baij->mbs;
14473eda8832SBarry Smith   MatScalar      *a;
14480ac07820SSatish Balay 
1449d64ed03dSBarry Smith   PetscFunctionBegin;
1450e7e72b3dSBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1451fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
14527adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr);
1453d0f46423SBarry Smith     ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr);
14547adad957SLisandro Dalcin     ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr);
1455d0f46423SBarry Smith     ierr = MatMPIBAIJSetPreallocation(B,A->rmap->bs,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
1456fc4dec0aSBarry Smith   } else {
1457fc4dec0aSBarry Smith     B = *matout;
1458fc4dec0aSBarry Smith   }
14590ac07820SSatish Balay 
14600ac07820SSatish Balay   /* copy over the A part */
14610ac07820SSatish Balay   Aloc = (Mat_SeqBAIJ*)baij->A->data;
14620ac07820SSatish Balay   ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1463b24ad042SBarry Smith   ierr = PetscMalloc(bs*sizeof(PetscInt),&rvals);CHKERRQ(ierr);
14640ac07820SSatish Balay 
14650ac07820SSatish Balay   for (i=0; i<mbs; i++) {
1466899cda47SBarry Smith     rvals[0] = bs*(baij->rstartbs + i);
14670ac07820SSatish Balay     for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
14680ac07820SSatish Balay     for (j=ai[i]; j<ai[i+1]; j++) {
1469899cda47SBarry Smith       col = (baij->cstartbs+aj[j])*bs;
14700ac07820SSatish Balay       for (k=0; k<bs; k++) {
147197e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);CHKERRQ(ierr);
14720ac07820SSatish Balay         col++; a += bs;
14730ac07820SSatish Balay       }
14740ac07820SSatish Balay     }
14750ac07820SSatish Balay   }
14760ac07820SSatish Balay   /* copy over the B part */
14770ac07820SSatish Balay   Aloc = (Mat_SeqBAIJ*)baij->B->data;
14780ac07820SSatish Balay   ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
14790ac07820SSatish Balay   for (i=0; i<mbs; i++) {
1480899cda47SBarry Smith     rvals[0] = bs*(baij->rstartbs + i);
14810ac07820SSatish Balay     for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
14820ac07820SSatish Balay     for (j=ai[i]; j<ai[i+1]; j++) {
14830ac07820SSatish Balay       col = baij->garray[aj[j]]*bs;
14840ac07820SSatish Balay       for (k=0; k<bs; k++) {
148597e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);CHKERRQ(ierr);
14860ac07820SSatish Balay         col++; a += bs;
14870ac07820SSatish Balay       }
14880ac07820SSatish Balay     }
14890ac07820SSatish Balay   }
1490606d414cSSatish Balay   ierr = PetscFree(rvals);CHKERRQ(ierr);
14910ac07820SSatish Balay   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14920ac07820SSatish Balay   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14930ac07820SSatish Balay 
1494815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout != A) {
14950ac07820SSatish Balay     *matout = B;
14960ac07820SSatish Balay   } else {
1497273d9f13SBarry Smith     ierr = MatHeaderCopy(A,B);CHKERRQ(ierr);
14980ac07820SSatish Balay   }
14993a40ed3dSBarry Smith   PetscFunctionReturn(0);
15000ac07820SSatish Balay }
15010e95ebc0SSatish Balay 
15024a2ae208SSatish Balay #undef __FUNCT__
15034a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIBAIJ"
1504dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIBAIJ(Mat mat,Vec ll,Vec rr)
15050e95ebc0SSatish Balay {
150636c4a09eSSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
150736c4a09eSSatish Balay   Mat            a = baij->A,b = baij->B;
1508dfbe8321SBarry Smith   PetscErrorCode ierr;
1509b24ad042SBarry Smith   PetscInt       s1,s2,s3;
15100e95ebc0SSatish Balay 
1511d64ed03dSBarry Smith   PetscFunctionBegin;
151236c4a09eSSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
151336c4a09eSSatish Balay   if (rr) {
151436c4a09eSSatish Balay     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
1515e32f2f54SBarry Smith     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
151636c4a09eSSatish Balay     /* Overlap communication with computation. */
1517ca9f406cSSatish Balay     ierr = VecScatterBegin(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
151836c4a09eSSatish Balay   }
15190e95ebc0SSatish Balay   if (ll) {
15200e95ebc0SSatish Balay     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
1521e32f2f54SBarry Smith     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1522a21fb8cbSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,PETSC_NULL);CHKERRQ(ierr);
15230e95ebc0SSatish Balay   }
152436c4a09eSSatish Balay   /* scale  the diagonal block */
152536c4a09eSSatish Balay   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
152636c4a09eSSatish Balay 
152736c4a09eSSatish Balay   if (rr) {
152836c4a09eSSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
1529ca9f406cSSatish Balay     ierr = VecScatterEnd(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1530a21fb8cbSBarry Smith     ierr = (*b->ops->diagonalscale)(b,PETSC_NULL,baij->lvec);CHKERRQ(ierr);
153136c4a09eSSatish Balay   }
153236c4a09eSSatish Balay 
15333a40ed3dSBarry Smith   PetscFunctionReturn(0);
15340e95ebc0SSatish Balay }
15350e95ebc0SSatish Balay 
15364a2ae208SSatish Balay #undef __FUNCT__
15374a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIBAIJ"
1538f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_MPIBAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
15390ac07820SSatish Balay {
15400ac07820SSatish Balay   Mat_MPIBAIJ    *l = (Mat_MPIBAIJ*)A->data;
15416849ba73SBarry Smith   PetscErrorCode ierr;
1542b24ad042SBarry Smith   PetscMPIInt    imdex,size = l->size,n,rank = l->rank;
1543d0f46423SBarry Smith   PetscInt       i,*owners = A->rmap->range;
1544b24ad042SBarry Smith   PetscInt       *nprocs,j,idx,nsends,row;
1545b24ad042SBarry Smith   PetscInt       nmax,*svalues,*starts,*owner,nrecvs;
15467adad957SLisandro Dalcin   PetscInt       *rvalues,tag = ((PetscObject)A)->tag,count,base,slen,*source,lastidx = -1;
1547d0f46423SBarry Smith   PetscInt       *lens,*lrows,*values,rstart_bs=A->rmap->rstart;
15487adad957SLisandro Dalcin   MPI_Comm       comm = ((PetscObject)A)->comm;
15490ac07820SSatish Balay   MPI_Request    *send_waits,*recv_waits;
15500ac07820SSatish Balay   MPI_Status     recv_status,*send_status;
15516543fbbaSBarry Smith #if defined(PETSC_DEBUG)
15526543fbbaSBarry Smith   PetscTruth     found = PETSC_FALSE;
15536543fbbaSBarry Smith #endif
15540ac07820SSatish Balay 
1555d64ed03dSBarry Smith   PetscFunctionBegin;
15560ac07820SSatish Balay   /*  first count number of contributors to each processor */
1557b24ad042SBarry Smith   ierr  = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
1558b24ad042SBarry Smith   ierr  = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
1559b24ad042SBarry Smith   ierr  = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/
15606543fbbaSBarry Smith   j = 0;
15610ac07820SSatish Balay   for (i=0; i<N; i++) {
15626543fbbaSBarry Smith     if (lastidx > (idx = rows[i])) j = 0;
15636543fbbaSBarry Smith     lastidx = idx;
15646543fbbaSBarry Smith     for (; j<size; j++) {
1565357c27ecSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
15666543fbbaSBarry Smith         nprocs[2*j]++;
15676543fbbaSBarry Smith         nprocs[2*j+1] = 1;
15686543fbbaSBarry Smith         owner[i] = j;
15696543fbbaSBarry Smith #if defined(PETSC_DEBUG)
15706543fbbaSBarry Smith         found = PETSC_TRUE;
15716543fbbaSBarry Smith #endif
15726543fbbaSBarry Smith         break;
15730ac07820SSatish Balay       }
15740ac07820SSatish Balay     }
15756543fbbaSBarry Smith #if defined(PETSC_DEBUG)
1576e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
15776543fbbaSBarry Smith     found = PETSC_FALSE;
15786543fbbaSBarry Smith #endif
15790ac07820SSatish Balay   }
1580c1dc657dSBarry Smith   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
15810ac07820SSatish Balay 
15820ac07820SSatish Balay   /* inform other processors of number of messages and max length*/
1583c1dc657dSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
15840ac07820SSatish Balay 
15850ac07820SSatish Balay   /* post receives:   */
1586b24ad042SBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr);
1587b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
15880ac07820SSatish Balay   for (i=0; i<nrecvs; i++) {
1589b24ad042SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
15900ac07820SSatish Balay   }
15910ac07820SSatish Balay 
15920ac07820SSatish Balay   /* do sends:
15930ac07820SSatish Balay      1) starts[i] gives the starting index in svalues for stuff going to
15940ac07820SSatish Balay      the ith processor
15950ac07820SSatish Balay   */
1596b24ad042SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr);
1597b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
1598b24ad042SBarry Smith   ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
15990ac07820SSatish Balay   starts[0]  = 0;
1600c1dc657dSBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
16010ac07820SSatish Balay   for (i=0; i<N; i++) {
16020ac07820SSatish Balay     svalues[starts[owner[i]]++] = rows[i];
16030ac07820SSatish Balay   }
16040ac07820SSatish Balay 
16050ac07820SSatish Balay   starts[0] = 0;
1606c1dc657dSBarry Smith   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
16070ac07820SSatish Balay   count = 0;
16080ac07820SSatish Balay   for (i=0; i<size; i++) {
1609c1dc657dSBarry Smith     if (nprocs[2*i+1]) {
1610b24ad042SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
16110ac07820SSatish Balay     }
16120ac07820SSatish Balay   }
1613606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
16140ac07820SSatish Balay 
1615357c27ecSBarry Smith   base = owners[rank];
16160ac07820SSatish Balay 
16170ac07820SSatish Balay   /*  wait on receives */
1618fca92195SBarry Smith   ierr   = PetscMalloc2(nrecvs+1,PetscInt,&lens,nrecvs+1,PetscInt,&source);CHKERRQ(ierr);
1619fca92195SBarry Smith   count  = nrecvs;
1620fca92195SBarry Smith   slen = 0;
16210ac07820SSatish Balay   while (count) {
1622ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
16230ac07820SSatish Balay     /* unpack receives into our local space */
1624b24ad042SBarry Smith     ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr);
16250ac07820SSatish Balay     source[imdex]  = recv_status.MPI_SOURCE;
16260ac07820SSatish Balay     lens[imdex]    = n;
16270ac07820SSatish Balay     slen          += n;
16280ac07820SSatish Balay     count--;
16290ac07820SSatish Balay   }
1630606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
16310ac07820SSatish Balay 
16320ac07820SSatish Balay   /* move the data into the send scatter */
1633b24ad042SBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr);
16340ac07820SSatish Balay   count = 0;
16350ac07820SSatish Balay   for (i=0; i<nrecvs; i++) {
16360ac07820SSatish Balay     values = rvalues + i*nmax;
16370ac07820SSatish Balay     for (j=0; j<lens[i]; j++) {
16380ac07820SSatish Balay       lrows[count++] = values[j] - base;
16390ac07820SSatish Balay     }
16400ac07820SSatish Balay   }
1641606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
1642fca92195SBarry Smith   ierr = PetscFree2(lens,source);CHKERRQ(ierr);
1643606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
1644606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
16450ac07820SSatish Balay 
16460ac07820SSatish Balay   /* actually zap the local rows */
164772dacd9aSBarry Smith   /*
164872dacd9aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
1649a8c7a070SBarry Smith      is square and the user wishes to set the diagonal we use separate
165072dacd9aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
165172dacd9aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
165272dacd9aSBarry Smith 
165372dacd9aSBarry Smith   */
16549c957beeSSatish Balay   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
1655f4df32b1SMatthew Knepley   ierr = MatZeroRows_SeqBAIJ(l->B,slen,lrows,0.0);CHKERRQ(ierr);
1656d0f46423SBarry Smith   if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) {
1657f4df32b1SMatthew Knepley     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,diag);CHKERRQ(ierr);
1658f4df32b1SMatthew Knepley   } else if (diag != 0.0) {
1659f4df32b1SMatthew Knepley     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,0.0);CHKERRQ(ierr);
1660e7e72b3dSBarry Smith     if (((Mat_SeqBAIJ*)l->A->data)->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options \n\
1661512a5fc5SBarry Smith        MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
1662a07cd24cSSatish Balay     for (i=0; i<slen; i++) {
1663a07cd24cSSatish Balay       row  = lrows[i] + rstart_bs;
1664f4df32b1SMatthew Knepley       ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
1665a07cd24cSSatish Balay     }
1666a07cd24cSSatish Balay     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1667a07cd24cSSatish Balay     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16689c957beeSSatish Balay   } else {
1669f4df32b1SMatthew Knepley     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,0.0);CHKERRQ(ierr);
1670a07cd24cSSatish Balay   }
16719c957beeSSatish Balay 
1672606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
1673a07cd24cSSatish Balay 
16740ac07820SSatish Balay   /* wait on sends */
16750ac07820SSatish Balay   if (nsends) {
167682502324SSatish Balay     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
1677ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
1678606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
16790ac07820SSatish Balay   }
1680606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
1681606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
16820ac07820SSatish Balay 
16833a40ed3dSBarry Smith   PetscFunctionReturn(0);
16840ac07820SSatish Balay }
168572dacd9aSBarry Smith 
16864a2ae208SSatish Balay #undef __FUNCT__
16874a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIBAIJ"
1688dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIBAIJ(Mat A)
1689bb5a7306SBarry Smith {
1690bb5a7306SBarry Smith   Mat_MPIBAIJ    *a   = (Mat_MPIBAIJ*)A->data;
1691dfbe8321SBarry Smith   PetscErrorCode ierr;
1692d64ed03dSBarry Smith 
1693d64ed03dSBarry Smith   PetscFunctionBegin;
1694bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
16953a40ed3dSBarry Smith   PetscFunctionReturn(0);
1696bb5a7306SBarry Smith }
1697bb5a7306SBarry Smith 
16986849ba73SBarry Smith static PetscErrorCode MatDuplicate_MPIBAIJ(Mat,MatDuplicateOption,Mat *);
16990ac07820SSatish Balay 
17004a2ae208SSatish Balay #undef __FUNCT__
17014a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIBAIJ"
1702dfbe8321SBarry Smith PetscErrorCode MatEqual_MPIBAIJ(Mat A,Mat B,PetscTruth *flag)
17037fc3c18eSBarry Smith {
17047fc3c18eSBarry Smith   Mat_MPIBAIJ    *matB = (Mat_MPIBAIJ*)B->data,*matA = (Mat_MPIBAIJ*)A->data;
17057fc3c18eSBarry Smith   Mat            a,b,c,d;
17067fc3c18eSBarry Smith   PetscTruth     flg;
1707dfbe8321SBarry Smith   PetscErrorCode ierr;
17087fc3c18eSBarry Smith 
17097fc3c18eSBarry Smith   PetscFunctionBegin;
17107fc3c18eSBarry Smith   a = matA->A; b = matA->B;
17117fc3c18eSBarry Smith   c = matB->A; d = matB->B;
17127fc3c18eSBarry Smith 
17137fc3c18eSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1714abc0a331SBarry Smith   if (flg) {
17157fc3c18eSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
17167fc3c18eSBarry Smith   }
17177adad957SLisandro Dalcin   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,((PetscObject)A)->comm);CHKERRQ(ierr);
17187fc3c18eSBarry Smith   PetscFunctionReturn(0);
17197fc3c18eSBarry Smith }
17207fc3c18eSBarry Smith 
17213c896bc6SHong Zhang #undef __FUNCT__
17223c896bc6SHong Zhang #define __FUNCT__ "MatCopy_MPIBAIJ"
17233c896bc6SHong Zhang PetscErrorCode MatCopy_MPIBAIJ(Mat A,Mat B,MatStructure str)
17243c896bc6SHong Zhang {
17253c896bc6SHong Zhang   PetscErrorCode ierr;
17263c896bc6SHong Zhang   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ *)A->data;
17273c896bc6SHong Zhang   Mat_MPIBAIJ    *b = (Mat_MPIBAIJ *)B->data;
17283c896bc6SHong Zhang 
17293c896bc6SHong Zhang   PetscFunctionBegin;
17303c896bc6SHong Zhang   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
17313c896bc6SHong Zhang   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
17323c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
17333c896bc6SHong Zhang   } else {
17343c896bc6SHong Zhang     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
17353c896bc6SHong Zhang     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
17363c896bc6SHong Zhang   }
17373c896bc6SHong Zhang   PetscFunctionReturn(0);
17383c896bc6SHong Zhang }
1739273d9f13SBarry Smith 
17404a2ae208SSatish Balay #undef __FUNCT__
17414a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIBAIJ"
1742dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_MPIBAIJ(Mat A)
1743273d9f13SBarry Smith {
1744dfbe8321SBarry Smith   PetscErrorCode ierr;
1745273d9f13SBarry Smith 
1746273d9f13SBarry Smith   PetscFunctionBegin;
1747db4efbfdSBarry Smith   ierr =  MatMPIBAIJSetPreallocation(A,-PetscMax(A->rmap->bs,1),PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1748273d9f13SBarry Smith   PetscFunctionReturn(0);
1749273d9f13SBarry Smith }
1750273d9f13SBarry Smith 
17514fe895cdSHong Zhang #undef __FUNCT__
17524fe895cdSHong Zhang #define __FUNCT__ "MatAXPY_MPIBAIJ"
17534fe895cdSHong Zhang PetscErrorCode MatAXPY_MPIBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
17544fe895cdSHong Zhang {
17554fe895cdSHong Zhang   PetscErrorCode ierr;
17564fe895cdSHong Zhang   Mat_MPIBAIJ    *xx=(Mat_MPIBAIJ *)X->data,*yy=(Mat_MPIBAIJ *)Y->data;
17574fe895cdSHong Zhang   PetscBLASInt   bnz,one=1;
17584fe895cdSHong Zhang   Mat_SeqBAIJ    *x,*y;
17594fe895cdSHong Zhang 
17604fe895cdSHong Zhang   PetscFunctionBegin;
17614fe895cdSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
17624fe895cdSHong Zhang     PetscScalar alpha = a;
17634fe895cdSHong Zhang     x = (Mat_SeqBAIJ *)xx->A->data;
17644fe895cdSHong Zhang     y = (Mat_SeqBAIJ *)yy->A->data;
17650805154bSBarry Smith     bnz = PetscBLASIntCast(x->nz);
17664fe895cdSHong Zhang     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
17674fe895cdSHong Zhang     x = (Mat_SeqBAIJ *)xx->B->data;
17684fe895cdSHong Zhang     y = (Mat_SeqBAIJ *)yy->B->data;
17690805154bSBarry Smith     bnz = PetscBLASIntCast(x->nz);
17704fe895cdSHong Zhang     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
17714fe895cdSHong Zhang   } else {
17724fe895cdSHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
17734fe895cdSHong Zhang   }
17744fe895cdSHong Zhang   PetscFunctionReturn(0);
17754fe895cdSHong Zhang }
17764fe895cdSHong Zhang 
177799cafbc1SBarry Smith #undef __FUNCT__
177841c166b1SJed Brown #define __FUNCT__ "MatSetBlockSize_MPIBAIJ"
177941c166b1SJed Brown PetscErrorCode MatSetBlockSize_MPIBAIJ(Mat A,PetscInt bs)
178041c166b1SJed Brown {
178141c166b1SJed Brown   Mat_MPIBAIJ    *a   = (Mat_MPIBAIJ*)A->data;
1782829b6ff0SJed Brown   PetscInt rbs,cbs;
178341c166b1SJed Brown   PetscErrorCode ierr;
178441c166b1SJed Brown 
178541c166b1SJed Brown   PetscFunctionBegin;
178641c166b1SJed Brown   ierr = MatSetBlockSize(a->A,bs);CHKERRQ(ierr);
178741c166b1SJed Brown   ierr = MatSetBlockSize(a->B,bs);CHKERRQ(ierr);
1788829b6ff0SJed Brown   ierr = PetscLayoutGetBlockSize(A->rmap,&rbs);CHKERRQ(ierr);
1789829b6ff0SJed Brown   ierr = PetscLayoutGetBlockSize(A->cmap,&cbs);CHKERRQ(ierr);
1790e32f2f54SBarry Smith   if (rbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with BAIJ %d",bs,rbs);
1791e32f2f54SBarry Smith   if (cbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with BAIJ %d",bs,cbs);
179241c166b1SJed Brown   PetscFunctionReturn(0);
179341c166b1SJed Brown }
179441c166b1SJed Brown 
179541c166b1SJed Brown #undef __FUNCT__
179699cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIBAIJ"
179799cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIBAIJ(Mat A)
179899cafbc1SBarry Smith {
179999cafbc1SBarry Smith   Mat_MPIBAIJ   *a = (Mat_MPIBAIJ*)A->data;
180099cafbc1SBarry Smith   PetscErrorCode ierr;
180199cafbc1SBarry Smith 
180299cafbc1SBarry Smith   PetscFunctionBegin;
180399cafbc1SBarry Smith   ierr = MatRealPart(a->A);CHKERRQ(ierr);
180499cafbc1SBarry Smith   ierr = MatRealPart(a->B);CHKERRQ(ierr);
180599cafbc1SBarry Smith   PetscFunctionReturn(0);
180699cafbc1SBarry Smith }
180799cafbc1SBarry Smith 
180899cafbc1SBarry Smith #undef __FUNCT__
180999cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIBAIJ"
181099cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIBAIJ(Mat A)
181199cafbc1SBarry Smith {
181299cafbc1SBarry Smith   Mat_MPIBAIJ   *a = (Mat_MPIBAIJ*)A->data;
181399cafbc1SBarry Smith   PetscErrorCode ierr;
181499cafbc1SBarry Smith 
181599cafbc1SBarry Smith   PetscFunctionBegin;
181699cafbc1SBarry Smith   ierr = MatImaginaryPart(a->A);CHKERRQ(ierr);
181799cafbc1SBarry Smith   ierr = MatImaginaryPart(a->B);CHKERRQ(ierr);
181899cafbc1SBarry Smith   PetscFunctionReturn(0);
181999cafbc1SBarry Smith }
182099cafbc1SBarry Smith 
182182094794SBarry Smith #undef __FUNCT__
182282094794SBarry Smith #define __FUNCT__ "MatGetSubMatrix_MPIBAIJ"
18234aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIBAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
18244aa3045dSJed Brown {
18254aa3045dSJed Brown   PetscErrorCode ierr;
18264aa3045dSJed Brown   IS             iscol_local;
18274aa3045dSJed Brown   PetscInt       csize;
18284aa3045dSJed Brown 
18294aa3045dSJed Brown   PetscFunctionBegin;
18304aa3045dSJed Brown   ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr);
1831b79d0421SJed Brown   if (call == MAT_REUSE_MATRIX) {
1832b79d0421SJed Brown     ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr);
1833e32f2f54SBarry Smith     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
1834b79d0421SJed Brown   } else {
18354aa3045dSJed Brown     ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr);
1836b79d0421SJed Brown   }
18374aa3045dSJed Brown   ierr = MatGetSubMatrix_MPIBAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr);
1838b79d0421SJed Brown   if (call == MAT_INITIAL_MATRIX) {
1839b79d0421SJed Brown     ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr);
18404aa3045dSJed Brown     ierr = ISDestroy(iscol_local);CHKERRQ(ierr);
1841b79d0421SJed Brown   }
18424aa3045dSJed Brown   PetscFunctionReturn(0);
18434aa3045dSJed Brown }
18444aa3045dSJed Brown 
18454aa3045dSJed Brown #undef __FUNCT__
18464aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIBAIJ"
184782094794SBarry Smith /*
184882094794SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqBAIJ
184982094794SBarry Smith   in local and then by concatenating the local matrices the end result.
185082094794SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIBAIJ()
185182094794SBarry Smith */
18524aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIBAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
185382094794SBarry Smith {
185482094794SBarry Smith   PetscErrorCode ierr;
185582094794SBarry Smith   PetscMPIInt    rank,size;
185682094794SBarry Smith   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs;
185782094794SBarry Smith   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
185882094794SBarry Smith   Mat            *local,M,Mreuse;
185982094794SBarry Smith   MatScalar      *vwork,*aa;
186082094794SBarry Smith   MPI_Comm       comm = ((PetscObject)mat)->comm;
186182094794SBarry Smith   Mat_SeqBAIJ    *aij;
186282094794SBarry Smith 
186382094794SBarry Smith 
186482094794SBarry Smith   PetscFunctionBegin;
186582094794SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
186682094794SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
186782094794SBarry Smith 
186882094794SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
186982094794SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
1870e32f2f54SBarry Smith     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
187182094794SBarry Smith     local = &Mreuse;
187282094794SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
187382094794SBarry Smith   } else {
187482094794SBarry Smith     ierr   = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
187582094794SBarry Smith     Mreuse = *local;
187682094794SBarry Smith     ierr   = PetscFree(local);CHKERRQ(ierr);
187782094794SBarry Smith   }
187882094794SBarry Smith 
187982094794SBarry Smith   /*
188082094794SBarry Smith       m - number of local rows
188182094794SBarry Smith       n - number of columns (same on all processors)
188282094794SBarry Smith       rstart - first row in new global matrix generated
188382094794SBarry Smith   */
188482094794SBarry Smith   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
188582094794SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
188682094794SBarry Smith   m    = m/bs;
188782094794SBarry Smith   n    = n/bs;
188882094794SBarry Smith 
188982094794SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
189082094794SBarry Smith     aij = (Mat_SeqBAIJ*)(Mreuse)->data;
189182094794SBarry Smith     ii  = aij->i;
189282094794SBarry Smith     jj  = aij->j;
189382094794SBarry Smith 
189482094794SBarry Smith     /*
189582094794SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
189682094794SBarry Smith         portions of the matrix in order to do correct preallocation
189782094794SBarry Smith     */
189882094794SBarry Smith 
189982094794SBarry Smith     /* first get start and end of "diagonal" columns */
190082094794SBarry Smith     if (csize == PETSC_DECIDE) {
190182094794SBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
190282094794SBarry Smith       if (mglobal == n*bs) { /* square matrix */
190382094794SBarry Smith 	nlocal = m;
190482094794SBarry Smith       } else {
190582094794SBarry Smith         nlocal = n/size + ((n % size) > rank);
190682094794SBarry Smith       }
190782094794SBarry Smith     } else {
190882094794SBarry Smith       nlocal = csize/bs;
190982094794SBarry Smith     }
191082094794SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
191182094794SBarry Smith     rstart = rend - nlocal;
191265e19b50SBarry Smith     if (rank == size - 1 && rend != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,n);
191382094794SBarry Smith 
191482094794SBarry Smith     /* next, compute all the lengths */
191582094794SBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr);
191682094794SBarry Smith     olens = dlens + m;
191782094794SBarry Smith     for (i=0; i<m; i++) {
191882094794SBarry Smith       jend = ii[i+1] - ii[i];
191982094794SBarry Smith       olen = 0;
192082094794SBarry Smith       dlen = 0;
192182094794SBarry Smith       for (j=0; j<jend; j++) {
192282094794SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
192382094794SBarry Smith         else dlen++;
192482094794SBarry Smith         jj++;
192582094794SBarry Smith       }
192682094794SBarry Smith       olens[i] = olen;
192782094794SBarry Smith       dlens[i] = dlen;
192882094794SBarry Smith     }
192982094794SBarry Smith     ierr = MatCreate(comm,&M);CHKERRQ(ierr);
193082094794SBarry Smith     ierr = MatSetSizes(M,bs*m,bs*nlocal,PETSC_DECIDE,bs*n);CHKERRQ(ierr);
193182094794SBarry Smith     ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr);
193282094794SBarry Smith     ierr = MatMPIBAIJSetPreallocation(M,bs,0,dlens,0,olens);CHKERRQ(ierr);
193382094794SBarry Smith     ierr = PetscFree(dlens);CHKERRQ(ierr);
193482094794SBarry Smith   } else {
193582094794SBarry Smith     PetscInt ml,nl;
193682094794SBarry Smith 
193782094794SBarry Smith     M = *newmat;
193882094794SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
1939e32f2f54SBarry Smith     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
194082094794SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
194182094794SBarry Smith     /*
194282094794SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
194382094794SBarry Smith        rather than the slower MatSetValues().
194482094794SBarry Smith     */
194582094794SBarry Smith     M->was_assembled = PETSC_TRUE;
194682094794SBarry Smith     M->assembled     = PETSC_FALSE;
194782094794SBarry Smith   }
194882094794SBarry Smith   ierr = MatSetOption(M,MAT_ROW_ORIENTED,PETSC_FALSE);CHKERRQ(ierr);
194982094794SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
195082094794SBarry Smith   aij = (Mat_SeqBAIJ*)(Mreuse)->data;
195182094794SBarry Smith   ii  = aij->i;
195282094794SBarry Smith   jj  = aij->j;
195382094794SBarry Smith   aa  = aij->a;
195482094794SBarry Smith   for (i=0; i<m; i++) {
195582094794SBarry Smith     row   = rstart/bs + i;
195682094794SBarry Smith     nz    = ii[i+1] - ii[i];
195782094794SBarry Smith     cwork = jj;     jj += nz;
195882094794SBarry Smith     vwork = aa;     aa += nz;
195982094794SBarry Smith     ierr = MatSetValuesBlocked_MPIBAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
196082094794SBarry Smith   }
196182094794SBarry Smith 
196282094794SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
196382094794SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
196482094794SBarry Smith   *newmat = M;
196582094794SBarry Smith 
196682094794SBarry Smith   /* save submatrix used in processor for next request */
196782094794SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
196882094794SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
196982094794SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
197082094794SBarry Smith   }
197182094794SBarry Smith 
197282094794SBarry Smith   PetscFunctionReturn(0);
197382094794SBarry Smith }
197482094794SBarry Smith 
197582094794SBarry Smith #undef __FUNCT__
197682094794SBarry Smith #define __FUNCT__ "MatPermute_MPIBAIJ"
197782094794SBarry Smith PetscErrorCode MatPermute_MPIBAIJ(Mat A,IS rowp,IS colp,Mat *B)
197882094794SBarry Smith {
197982094794SBarry Smith   MPI_Comm       comm,pcomm;
198082094794SBarry Smith   PetscInt       first,local_size,nrows;
198182094794SBarry Smith   const PetscInt *rows;
1982dbf0e21dSBarry Smith   PetscMPIInt    size;
198382094794SBarry Smith   IS             crowp,growp,irowp,lrowp,lcolp,icolp;
198482094794SBarry Smith   PetscErrorCode ierr;
198582094794SBarry Smith 
198682094794SBarry Smith   PetscFunctionBegin;
198782094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr);
198882094794SBarry Smith   /* make a collective version of 'rowp' */
198982094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)rowp,&pcomm);CHKERRQ(ierr);
199082094794SBarry Smith   if (pcomm==comm) {
199182094794SBarry Smith     crowp = rowp;
199282094794SBarry Smith   } else {
199382094794SBarry Smith     ierr = ISGetSize(rowp,&nrows);CHKERRQ(ierr);
199482094794SBarry Smith     ierr = ISGetIndices(rowp,&rows);CHKERRQ(ierr);
199582094794SBarry Smith     ierr = ISCreateGeneral(comm,nrows,rows,&crowp);CHKERRQ(ierr);
199682094794SBarry Smith     ierr = ISRestoreIndices(rowp,&rows);CHKERRQ(ierr);
199782094794SBarry Smith   }
199882094794SBarry Smith   /* collect the global row permutation and invert it */
199982094794SBarry Smith   ierr = ISAllGather(crowp,&growp);CHKERRQ(ierr);
200082094794SBarry Smith   ierr = ISSetPermutation(growp);CHKERRQ(ierr);
200182094794SBarry Smith   if (pcomm!=comm) {
200282094794SBarry Smith     ierr = ISDestroy(crowp);CHKERRQ(ierr);
200382094794SBarry Smith   }
200482094794SBarry Smith   ierr = ISInvertPermutation(growp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
200582094794SBarry Smith   /* get the local target indices */
200682094794SBarry Smith   ierr = MatGetOwnershipRange(A,&first,PETSC_NULL);CHKERRQ(ierr);
200782094794SBarry Smith   ierr = MatGetLocalSize(A,&local_size,PETSC_NULL);CHKERRQ(ierr);
200882094794SBarry Smith   ierr = ISGetIndices(irowp,&rows);CHKERRQ(ierr);
200982094794SBarry Smith   ierr = ISCreateGeneral(MPI_COMM_SELF,local_size,rows+first,&lrowp);CHKERRQ(ierr);
201082094794SBarry Smith   ierr = ISRestoreIndices(irowp,&rows);CHKERRQ(ierr);
201182094794SBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
201282094794SBarry Smith   /* the column permutation is so much easier;
201382094794SBarry Smith      make a local version of 'colp' and invert it */
201482094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)colp,&pcomm);CHKERRQ(ierr);
2015dbf0e21dSBarry Smith   ierr = MPI_Comm_size(pcomm,&size);CHKERRQ(ierr);
2016dbf0e21dSBarry Smith   if (size==1) {
201782094794SBarry Smith     lcolp = colp;
201882094794SBarry Smith   } else {
201982094794SBarry Smith     ierr = ISGetSize(colp,&nrows);CHKERRQ(ierr);
202082094794SBarry Smith     ierr = ISGetIndices(colp,&rows);CHKERRQ(ierr);
202182094794SBarry Smith     ierr = ISCreateGeneral(MPI_COMM_SELF,nrows,rows,&lcolp);CHKERRQ(ierr);
202282094794SBarry Smith   }
2023dbf0e21dSBarry Smith   ierr = ISSetPermutation(lcolp);CHKERRQ(ierr);
202482094794SBarry Smith   ierr = ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
20254aa3045dSJed Brown   ierr = ISSetPermutation(icolp);CHKERRQ(ierr);
2026dbf0e21dSBarry Smith   if (size>1) {
202782094794SBarry Smith     ierr = ISRestoreIndices(colp,&rows);CHKERRQ(ierr);
202882094794SBarry Smith     ierr = ISDestroy(lcolp);CHKERRQ(ierr);
202982094794SBarry Smith   }
203082094794SBarry Smith   /* now we just get the submatrix */
20314aa3045dSJed Brown   ierr = MatGetSubMatrix_MPIBAIJ_Private(A,lrowp,icolp,local_size,MAT_INITIAL_MATRIX,B);CHKERRQ(ierr);
203282094794SBarry Smith   /* clean up */
203382094794SBarry Smith   ierr = ISDestroy(lrowp);CHKERRQ(ierr);
203482094794SBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
203582094794SBarry Smith   PetscFunctionReturn(0);
203682094794SBarry Smith }
203782094794SBarry Smith 
20388c7482ecSBarry Smith #undef __FUNCT__
20398c7482ecSBarry Smith #define __FUNCT__ "MatGetGhosts_MPIBAIJ"
20408c7482ecSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatGetGhosts_MPIBAIJ(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
20418c7482ecSBarry Smith {
20428c7482ecSBarry Smith   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*) mat->data;
20438c7482ecSBarry Smith   Mat_SeqBAIJ    *B = (Mat_SeqBAIJ*)baij->B->data;
20448c7482ecSBarry Smith 
20458c7482ecSBarry Smith   PetscFunctionBegin;
20468c7482ecSBarry Smith   if (nghosts) { *nghosts = B->nbs;}
20478c7482ecSBarry Smith   if (ghosts) {*ghosts = baij->garray;}
20488c7482ecSBarry Smith   PetscFunctionReturn(0);
20498c7482ecSBarry Smith }
20508c7482ecSBarry Smith 
2051f6d58c54SBarry Smith EXTERN PetscErrorCode CreateColmap_MPIBAIJ_Private(Mat);
2052f6d58c54SBarry Smith 
2053f6d58c54SBarry Smith #undef __FUNCT__
2054f6d58c54SBarry Smith #define __FUNCT__ "MatFDColoringCreate_MPIBAIJ"
2055f6d58c54SBarry Smith /*
2056f6d58c54SBarry Smith     This routine is almost identical to MatFDColoringCreate_MPIBAIJ()!
2057f6d58c54SBarry Smith */
2058f6d58c54SBarry Smith PetscErrorCode MatFDColoringCreate_MPIBAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
2059f6d58c54SBarry Smith {
2060f6d58c54SBarry Smith   Mat_MPIBAIJ            *baij = (Mat_MPIBAIJ*)mat->data;
2061f6d58c54SBarry Smith   PetscErrorCode        ierr;
2062f6d58c54SBarry Smith   PetscMPIInt           size,*ncolsonproc,*disp,nn;
2063f6d58c54SBarry Smith   PetscInt              bs,i,n,nrows,j,k,m,*rows = 0,*A_ci,*A_cj,ncols,col;
2064f6d58c54SBarry Smith   const PetscInt        *is;
2065f6d58c54SBarry Smith   PetscInt              nis = iscoloring->n,nctot,*cols,*B_ci,*B_cj;
2066f6d58c54SBarry Smith   PetscInt              *rowhit,M,cstart,cend,colb;
2067f6d58c54SBarry Smith   PetscInt              *columnsforrow,l;
2068f6d58c54SBarry Smith   IS                    *isa;
2069f6d58c54SBarry Smith   PetscTruth             done,flg;
2070f6d58c54SBarry Smith   ISLocalToGlobalMapping map = mat->bmapping;
2071f6d58c54SBarry Smith   PetscInt               *ltog = (map ? map->indices : (PetscInt*) PETSC_NULL) ,ctype=c->ctype;
2072f6d58c54SBarry Smith 
2073f6d58c54SBarry Smith   PetscFunctionBegin;
2074e7e72b3dSBarry Smith   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();");
2075e7e72b3dSBarry Smith   if (ctype == IS_COLORING_GHOSTED && !map) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_INCOMP,"When using ghosted differencing matrix must have local to global mapping provided with MatSetLocalToGlobalMappingBlock");
2076f6d58c54SBarry Smith 
2077f6d58c54SBarry Smith   ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr);
2078f6d58c54SBarry Smith   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
2079f6d58c54SBarry Smith   M                = mat->rmap->n/bs;
2080f6d58c54SBarry Smith   cstart           = mat->cmap->rstart/bs;
2081f6d58c54SBarry Smith   cend             = mat->cmap->rend/bs;
2082f6d58c54SBarry Smith   c->M             = mat->rmap->N/bs;  /* set the global rows and columns and local rows */
2083f6d58c54SBarry Smith   c->N             = mat->cmap->N/bs;
2084f6d58c54SBarry Smith   c->m             = mat->rmap->n/bs;
2085f6d58c54SBarry Smith   c->rstart        = mat->rmap->rstart/bs;
2086f6d58c54SBarry Smith 
2087f6d58c54SBarry Smith   c->ncolors       = nis;
2088f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt),&c->ncolumns);CHKERRQ(ierr);
2089f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->columns);CHKERRQ(ierr);
2090f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt),&c->nrows);CHKERRQ(ierr);
2091f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->rows);CHKERRQ(ierr);
2092f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->columnsforrow);CHKERRQ(ierr);
2093f6d58c54SBarry Smith   ierr = PetscLogObjectMemory(c,5*nis*sizeof(PetscInt));CHKERRQ(ierr);
2094f6d58c54SBarry Smith 
2095f6d58c54SBarry Smith   /* Allow access to data structures of local part of matrix */
2096f6d58c54SBarry Smith   if (!baij->colmap) {
2097f6d58c54SBarry Smith     ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
2098f6d58c54SBarry Smith   }
2099f6d58c54SBarry Smith   ierr = MatGetColumnIJ(baij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
2100f6d58c54SBarry Smith   ierr = MatGetColumnIJ(baij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
2101f6d58c54SBarry Smith 
2102f6d58c54SBarry Smith   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&rowhit);CHKERRQ(ierr);
2103f6d58c54SBarry Smith   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&columnsforrow);CHKERRQ(ierr);
2104f6d58c54SBarry Smith 
2105f6d58c54SBarry Smith   for (i=0; i<nis; i++) {
2106f6d58c54SBarry Smith     ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr);
2107f6d58c54SBarry Smith     ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr);
2108f6d58c54SBarry Smith     c->ncolumns[i] = n;
2109f6d58c54SBarry Smith     if (n) {
2110f6d58c54SBarry Smith       ierr = PetscMalloc(n*sizeof(PetscInt),&c->columns[i]);CHKERRQ(ierr);
2111f6d58c54SBarry Smith       ierr = PetscLogObjectMemory(c,n*sizeof(PetscInt));CHKERRQ(ierr);
2112f6d58c54SBarry Smith       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));CHKERRQ(ierr);
2113f6d58c54SBarry Smith     } else {
2114f6d58c54SBarry Smith       c->columns[i]  = 0;
2115f6d58c54SBarry Smith     }
2116f6d58c54SBarry Smith 
2117f6d58c54SBarry Smith     if (ctype == IS_COLORING_GLOBAL){
2118f6d58c54SBarry Smith       /* Determine the total (parallel) number of columns of this color */
2119f6d58c54SBarry Smith       ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
2120fca92195SBarry Smith       ierr = PetscMalloc2(size,PetscMPIInt,&ncolsonproc,size,PetscMPIInt,&disp);CHKERRQ(ierr);
2121f6d58c54SBarry Smith 
2122f6d58c54SBarry Smith       nn   = PetscMPIIntCast(n);
2123f6d58c54SBarry Smith       ierr = MPI_Allgather(&nn,1,MPI_INT,ncolsonproc,1,MPI_INT,((PetscObject)mat)->comm);CHKERRQ(ierr);
2124f6d58c54SBarry Smith       nctot = 0; for (j=0; j<size; j++) {nctot += ncolsonproc[j];}
2125f6d58c54SBarry Smith       if (!nctot) {
2126f6d58c54SBarry Smith         ierr = PetscInfo(mat,"Coloring of matrix has some unneeded colors with no corresponding rows\n");CHKERRQ(ierr);
2127f6d58c54SBarry Smith       }
2128f6d58c54SBarry Smith 
2129f6d58c54SBarry Smith       disp[0] = 0;
2130f6d58c54SBarry Smith       for (j=1; j<size; j++) {
2131f6d58c54SBarry Smith         disp[j] = disp[j-1] + ncolsonproc[j-1];
2132f6d58c54SBarry Smith       }
2133f6d58c54SBarry Smith 
2134f6d58c54SBarry Smith       /* Get complete list of columns for color on each processor */
2135f6d58c54SBarry Smith       ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
2136f6d58c54SBarry Smith       ierr = MPI_Allgatherv((void*)is,n,MPIU_INT,cols,ncolsonproc,disp,MPIU_INT,((PetscObject)mat)->comm);CHKERRQ(ierr);
2137fca92195SBarry Smith       ierr = PetscFree2(ncolsonproc,disp);CHKERRQ(ierr);
2138f6d58c54SBarry Smith     } else if (ctype == IS_COLORING_GHOSTED){
2139f6d58c54SBarry Smith       /* Determine local number of columns of this color on this process, including ghost points */
2140f6d58c54SBarry Smith       nctot = n;
2141f6d58c54SBarry Smith       ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
2142f6d58c54SBarry Smith       ierr = PetscMemcpy(cols,is,n*sizeof(PetscInt));CHKERRQ(ierr);
2143f6d58c54SBarry Smith     } else {
2144e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not provided for this MatFDColoring type");
2145f6d58c54SBarry Smith     }
2146f6d58c54SBarry Smith 
2147f6d58c54SBarry Smith     /*
2148f6d58c54SBarry Smith        Mark all rows affect by these columns
2149f6d58c54SBarry Smith     */
2150f6d58c54SBarry Smith     /* Temporary option to allow for debugging/testing */
2151f6d58c54SBarry Smith     flg  = PETSC_FALSE;
2152f6d58c54SBarry Smith     ierr = PetscOptionsGetTruth(PETSC_NULL,"-matfdcoloring_slow",&flg,PETSC_NULL);CHKERRQ(ierr);
2153f6d58c54SBarry Smith     if (!flg) {/*-----------------------------------------------------------------------------*/
2154f6d58c54SBarry Smith       /* crude, fast version */
2155f6d58c54SBarry Smith       ierr = PetscMemzero(rowhit,M*sizeof(PetscInt));CHKERRQ(ierr);
2156f6d58c54SBarry Smith       /* loop over columns*/
2157f6d58c54SBarry Smith       for (j=0; j<nctot; j++) {
2158f6d58c54SBarry Smith         if (ctype == IS_COLORING_GHOSTED) {
2159f6d58c54SBarry Smith           col = ltog[cols[j]];
2160f6d58c54SBarry Smith         } else {
2161f6d58c54SBarry Smith           col  = cols[j];
2162f6d58c54SBarry Smith         }
2163f6d58c54SBarry Smith         if (col >= cstart && col < cend) {
2164f6d58c54SBarry Smith           /* column is in diagonal block of matrix */
2165f6d58c54SBarry Smith           rows = A_cj + A_ci[col-cstart];
2166f6d58c54SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
2167f6d58c54SBarry Smith         } else {
2168f6d58c54SBarry Smith #if defined (PETSC_USE_CTABLE)
2169cb9801acSJed Brown           ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr);
2170f6d58c54SBarry Smith 	  colb --;
2171f6d58c54SBarry Smith #else
2172f6d58c54SBarry Smith           colb = baij->colmap[col] - 1;
2173f6d58c54SBarry Smith #endif
2174f6d58c54SBarry Smith           if (colb == -1) {
2175f6d58c54SBarry Smith             m = 0;
2176f6d58c54SBarry Smith           } else {
2177f6d58c54SBarry Smith             colb = colb/bs;
2178f6d58c54SBarry Smith             rows = B_cj + B_ci[colb];
2179f6d58c54SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
2180f6d58c54SBarry Smith           }
2181f6d58c54SBarry Smith         }
2182f6d58c54SBarry Smith         /* loop over columns marking them in rowhit */
2183f6d58c54SBarry Smith         for (k=0; k<m; k++) {
2184f6d58c54SBarry Smith           rowhit[*rows++] = col + 1;
2185f6d58c54SBarry Smith         }
2186f6d58c54SBarry Smith       }
2187f6d58c54SBarry Smith 
2188f6d58c54SBarry Smith       /* count the number of hits */
2189f6d58c54SBarry Smith       nrows = 0;
2190f6d58c54SBarry Smith       for (j=0; j<M; j++) {
2191f6d58c54SBarry Smith         if (rowhit[j]) nrows++;
2192f6d58c54SBarry Smith       }
2193f6d58c54SBarry Smith       c->nrows[i]         = nrows;
2194f6d58c54SBarry Smith       ierr                = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr);
2195f6d58c54SBarry Smith       ierr                = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr);
2196f6d58c54SBarry Smith       ierr = PetscLogObjectMemory(c,2*(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr);
2197f6d58c54SBarry Smith       nrows = 0;
2198f6d58c54SBarry Smith       for (j=0; j<M; j++) {
2199f6d58c54SBarry Smith         if (rowhit[j]) {
2200f6d58c54SBarry Smith           c->rows[i][nrows]           = j;
2201f6d58c54SBarry Smith           c->columnsforrow[i][nrows] = rowhit[j] - 1;
2202f6d58c54SBarry Smith           nrows++;
2203f6d58c54SBarry Smith         }
2204f6d58c54SBarry Smith       }
2205f6d58c54SBarry Smith     } else {/*-------------------------------------------------------------------------------*/
2206f6d58c54SBarry Smith       /* slow version, using rowhit as a linked list */
2207f6d58c54SBarry Smith       PetscInt currentcol,fm,mfm;
2208f6d58c54SBarry Smith       rowhit[M] = M;
2209f6d58c54SBarry Smith       nrows     = 0;
2210f6d58c54SBarry Smith       /* loop over columns*/
2211f6d58c54SBarry Smith       for (j=0; j<nctot; j++) {
2212f6d58c54SBarry Smith         if (ctype == IS_COLORING_GHOSTED) {
2213f6d58c54SBarry Smith           col = ltog[cols[j]];
2214f6d58c54SBarry Smith         } else {
2215f6d58c54SBarry Smith           col  = cols[j];
2216f6d58c54SBarry Smith         }
2217f6d58c54SBarry Smith         if (col >= cstart && col < cend) {
2218f6d58c54SBarry Smith           /* column is in diagonal block of matrix */
2219f6d58c54SBarry Smith           rows = A_cj + A_ci[col-cstart];
2220f6d58c54SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
2221f6d58c54SBarry Smith         } else {
2222f6d58c54SBarry Smith #if defined (PETSC_USE_CTABLE)
2223f6d58c54SBarry Smith           ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr);
2224f6d58c54SBarry Smith           colb --;
2225f6d58c54SBarry Smith #else
2226f6d58c54SBarry Smith           colb = baij->colmap[col] - 1;
2227f6d58c54SBarry Smith #endif
2228f6d58c54SBarry Smith           if (colb == -1) {
2229f6d58c54SBarry Smith             m = 0;
2230f6d58c54SBarry Smith           } else {
2231f6d58c54SBarry Smith             colb = colb/bs;
2232f6d58c54SBarry Smith             rows = B_cj + B_ci[colb];
2233f6d58c54SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
2234f6d58c54SBarry Smith           }
2235f6d58c54SBarry Smith         }
2236f6d58c54SBarry Smith 
2237f6d58c54SBarry Smith         /* loop over columns marking them in rowhit */
2238f6d58c54SBarry Smith         fm    = M; /* fm points to first entry in linked list */
2239f6d58c54SBarry Smith         for (k=0; k<m; k++) {
2240f6d58c54SBarry Smith           currentcol = *rows++;
2241f6d58c54SBarry Smith 	  /* is it already in the list? */
2242f6d58c54SBarry Smith           do {
2243f6d58c54SBarry Smith             mfm  = fm;
2244f6d58c54SBarry Smith             fm   = rowhit[fm];
2245f6d58c54SBarry Smith           } while (fm < currentcol);
2246f6d58c54SBarry Smith           /* not in list so add it */
2247f6d58c54SBarry Smith           if (fm != currentcol) {
2248f6d58c54SBarry Smith             nrows++;
2249f6d58c54SBarry Smith             columnsforrow[currentcol] = col;
2250f6d58c54SBarry Smith             /* next three lines insert new entry into linked list */
2251f6d58c54SBarry Smith             rowhit[mfm]               = currentcol;
2252f6d58c54SBarry Smith             rowhit[currentcol]        = fm;
2253f6d58c54SBarry Smith             fm                        = currentcol;
2254f6d58c54SBarry Smith             /* fm points to present position in list since we know the columns are sorted */
2255f6d58c54SBarry Smith           } else {
2256e32f2f54SBarry Smith             SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid coloring of matrix detected");
2257f6d58c54SBarry Smith           }
2258f6d58c54SBarry Smith         }
2259f6d58c54SBarry Smith       }
2260f6d58c54SBarry Smith       c->nrows[i]         = nrows;
2261f6d58c54SBarry Smith       ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr);
2262f6d58c54SBarry Smith       ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr);
2263f6d58c54SBarry Smith       ierr = PetscLogObjectMemory(c,(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr);
2264f6d58c54SBarry Smith       /* now store the linked list of rows into c->rows[i] */
2265f6d58c54SBarry Smith       nrows = 0;
2266f6d58c54SBarry Smith       fm    = rowhit[M];
2267f6d58c54SBarry Smith       do {
2268f6d58c54SBarry Smith         c->rows[i][nrows]            = fm;
2269f6d58c54SBarry Smith         c->columnsforrow[i][nrows++] = columnsforrow[fm];
2270f6d58c54SBarry Smith         fm                           = rowhit[fm];
2271f6d58c54SBarry Smith       } while (fm < M);
2272f6d58c54SBarry Smith     } /* ---------------------------------------------------------------------------------------*/
2273f6d58c54SBarry Smith     ierr = PetscFree(cols);CHKERRQ(ierr);
2274f6d58c54SBarry Smith   }
2275f6d58c54SBarry Smith 
2276f6d58c54SBarry Smith   /* Optimize by adding the vscale, and scaleforrow[][] fields */
2277f6d58c54SBarry Smith   /*
2278f6d58c54SBarry Smith        vscale will contain the "diagonal" on processor scalings followed by the off processor
2279f6d58c54SBarry Smith   */
2280f6d58c54SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
2281f6d58c54SBarry Smith     PetscInt *garray;
2282f6d58c54SBarry Smith     ierr = PetscMalloc(baij->B->cmap->n*sizeof(PetscInt),&garray);CHKERRQ(ierr);
2283f6d58c54SBarry Smith     for (i=0; i<baij->B->cmap->n/bs; i++) {
2284f6d58c54SBarry Smith       for (j=0; j<bs; j++) {
2285f6d58c54SBarry Smith         garray[i*bs+j] = bs*baij->garray[i]+j;
2286f6d58c54SBarry Smith       }
2287f6d58c54SBarry Smith     }
2288f6d58c54SBarry Smith     ierr = VecCreateGhost(((PetscObject)mat)->comm,baij->A->rmap->n,PETSC_DETERMINE,baij->B->cmap->n,garray,&c->vscale);CHKERRQ(ierr);
2289f6d58c54SBarry Smith     ierr = PetscFree(garray);CHKERRQ(ierr);
2290f6d58c54SBarry Smith     CHKMEMQ;
2291f6d58c54SBarry Smith     ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr);
2292f6d58c54SBarry Smith     for (k=0; k<c->ncolors; k++) {
2293f6d58c54SBarry Smith       ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr);
2294f6d58c54SBarry Smith       for (l=0; l<c->nrows[k]; l++) {
2295f6d58c54SBarry Smith         col = c->columnsforrow[k][l];
2296f6d58c54SBarry Smith         if (col >= cstart && col < cend) {
2297f6d58c54SBarry Smith           /* column is in diagonal block of matrix */
2298f6d58c54SBarry Smith           colb = col - cstart;
2299f6d58c54SBarry Smith         } else {
2300f6d58c54SBarry Smith           /* column  is in "off-processor" part */
2301f6d58c54SBarry Smith #if defined (PETSC_USE_CTABLE)
2302f6d58c54SBarry Smith           ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr);
2303f6d58c54SBarry Smith           colb --;
2304f6d58c54SBarry Smith #else
2305f6d58c54SBarry Smith           colb = baij->colmap[col] - 1;
2306f6d58c54SBarry Smith #endif
2307f6d58c54SBarry Smith           colb = colb/bs;
2308f6d58c54SBarry Smith           colb += cend - cstart;
2309f6d58c54SBarry Smith         }
2310f6d58c54SBarry Smith         c->vscaleforrow[k][l] = colb;
2311f6d58c54SBarry Smith       }
2312f6d58c54SBarry Smith     }
2313f6d58c54SBarry Smith   } else if (ctype == IS_COLORING_GHOSTED) {
2314f6d58c54SBarry Smith     /* Get gtol mapping */
2315f6d58c54SBarry Smith     PetscInt N = mat->cmap->N, *gtol;
2316f6d58c54SBarry Smith     ierr = PetscMalloc((N+1)*sizeof(PetscInt),&gtol);CHKERRQ(ierr);
2317f6d58c54SBarry Smith     for (i=0; i<N; i++) gtol[i] = -1;
2318f6d58c54SBarry Smith     for (i=0; i<map->n; i++) gtol[ltog[i]] = i;
2319f6d58c54SBarry Smith 
2320f6d58c54SBarry Smith     c->vscale = 0; /* will be created in MatFDColoringApply() */
2321f6d58c54SBarry Smith     ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr);
2322f6d58c54SBarry Smith     for (k=0; k<c->ncolors; k++) {
2323f6d58c54SBarry Smith       ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr);
2324f6d58c54SBarry Smith       for (l=0; l<c->nrows[k]; l++) {
2325f6d58c54SBarry Smith         col = c->columnsforrow[k][l];      /* global column index */
2326f6d58c54SBarry Smith         c->vscaleforrow[k][l] = gtol[col]; /* local column index */
2327f6d58c54SBarry Smith       }
2328f6d58c54SBarry Smith     }
2329f6d58c54SBarry Smith     ierr = PetscFree(gtol);CHKERRQ(ierr);
2330f6d58c54SBarry Smith   }
2331f6d58c54SBarry Smith   ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr);
2332f6d58c54SBarry Smith 
2333f6d58c54SBarry Smith   ierr = PetscFree(rowhit);CHKERRQ(ierr);
2334f6d58c54SBarry Smith   ierr = PetscFree(columnsforrow);CHKERRQ(ierr);
2335f6d58c54SBarry Smith   ierr = MatRestoreColumnIJ(baij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
2336f6d58c54SBarry Smith   ierr = MatRestoreColumnIJ(baij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
2337f6d58c54SBarry Smith     CHKMEMQ;
2338f6d58c54SBarry Smith   PetscFunctionReturn(0);
2339f6d58c54SBarry Smith }
2340f6d58c54SBarry Smith 
2341f6d58c54SBarry Smith #undef __FUNCT__
2342f6d58c54SBarry Smith #define __FUNCT__ "MatGetSeqNonzerostructure_MPIBAIJ"
2343f6d58c54SBarry Smith PetscErrorCode MatGetSeqNonzerostructure_MPIBAIJ(Mat A,Mat *newmat)
2344f6d58c54SBarry Smith {
2345f6d58c54SBarry Smith   Mat            B;
2346f6d58c54SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ *)A->data;
2347f6d58c54SBarry Smith   Mat_SeqBAIJ    *ad = (Mat_SeqBAIJ*)a->A->data,*bd = (Mat_SeqBAIJ*)a->B->data;
2348f6d58c54SBarry Smith   Mat_SeqAIJ     *b;
2349f6d58c54SBarry Smith   PetscErrorCode ierr;
2350f6d58c54SBarry Smith   PetscMPIInt    size,rank,*recvcounts = 0,*displs = 0;
2351f6d58c54SBarry Smith   PetscInt       sendcount,i,*rstarts = A->rmap->range,n,cnt,j,bs = A->rmap->bs;
2352f6d58c54SBarry Smith   PetscInt       m,*garray = a->garray,*lens,*jsendbuf,*a_jsendbuf,*b_jsendbuf;
2353f6d58c54SBarry Smith 
2354f6d58c54SBarry Smith   PetscFunctionBegin;
2355f6d58c54SBarry Smith   ierr = MPI_Comm_size(((PetscObject)A)->comm,&size);CHKERRQ(ierr);
2356f6d58c54SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)A)->comm,&rank);CHKERRQ(ierr);
2357f6d58c54SBarry Smith 
2358f6d58c54SBarry Smith   /* ----------------------------------------------------------------
2359f6d58c54SBarry Smith      Tell every processor the number of nonzeros per row
2360f6d58c54SBarry Smith   */
2361f6d58c54SBarry Smith   ierr = PetscMalloc((A->rmap->N/bs)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
2362f6d58c54SBarry Smith   for (i=A->rmap->rstart/bs; i<A->rmap->rend/bs; i++) {
2363f6d58c54SBarry Smith     lens[i] = ad->i[i-A->rmap->rstart/bs+1] - ad->i[i-A->rmap->rstart/bs] + bd->i[i-A->rmap->rstart/bs+1] - bd->i[i-A->rmap->rstart/bs];
2364f6d58c54SBarry Smith   }
2365f6d58c54SBarry Smith   sendcount = A->rmap->rend/bs - A->rmap->rstart/bs;
2366f6d58c54SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscMPIInt),&recvcounts);CHKERRQ(ierr);
2367f6d58c54SBarry Smith   displs     = recvcounts + size;
2368f6d58c54SBarry Smith   for (i=0; i<size; i++) {
2369f6d58c54SBarry Smith     recvcounts[i] = A->rmap->range[i+1]/bs - A->rmap->range[i]/bs;
2370f6d58c54SBarry Smith     displs[i]     = A->rmap->range[i]/bs;
2371f6d58c54SBarry Smith   }
2372f6d58c54SBarry Smith #if defined(PETSC_HAVE_MPI_IN_PLACE)
2373f6d58c54SBarry Smith   ierr  = MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,lens,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr);
2374f6d58c54SBarry Smith #else
2375f6d58c54SBarry Smith   ierr  = MPI_Allgatherv(lens+A->rmap->rstart/bs,sendcount,MPIU_INT,lens,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr);
2376f6d58c54SBarry Smith #endif
2377f6d58c54SBarry Smith   /* ---------------------------------------------------------------
2378f6d58c54SBarry Smith      Create the sequential matrix of the same type as the local block diagonal
2379f6d58c54SBarry Smith   */
2380f6d58c54SBarry Smith   ierr  = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr);
2381f6d58c54SBarry Smith   ierr  = MatSetSizes(B,A->rmap->N/bs,A->cmap->N/bs,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
2382f6d58c54SBarry Smith   ierr  = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
2383f6d58c54SBarry Smith   ierr  = MatSeqAIJSetPreallocation(B,0,lens);CHKERRQ(ierr);
2384f6d58c54SBarry Smith   b = (Mat_SeqAIJ *)B->data;
2385f6d58c54SBarry Smith 
2386f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2387f6d58c54SBarry Smith     Copy my part of matrix column indices over
2388f6d58c54SBarry Smith   */
2389f6d58c54SBarry Smith   sendcount  = ad->nz + bd->nz;
2390f6d58c54SBarry Smith   jsendbuf   = b->j + b->i[rstarts[rank]/bs];
2391f6d58c54SBarry Smith   a_jsendbuf = ad->j;
2392f6d58c54SBarry Smith   b_jsendbuf = bd->j;
2393f6d58c54SBarry Smith   n          = A->rmap->rend/bs - A->rmap->rstart/bs;
2394f6d58c54SBarry Smith   cnt        = 0;
2395f6d58c54SBarry Smith   for (i=0; i<n; i++) {
2396f6d58c54SBarry Smith 
2397f6d58c54SBarry Smith     /* put in lower diagonal portion */
2398f6d58c54SBarry Smith     m = bd->i[i+1] - bd->i[i];
2399f6d58c54SBarry Smith     while (m > 0) {
2400f6d58c54SBarry Smith       /* is it above diagonal (in bd (compressed) numbering) */
2401f6d58c54SBarry Smith       if (garray[*b_jsendbuf] > A->rmap->rstart/bs + i) break;
2402f6d58c54SBarry Smith       jsendbuf[cnt++] = garray[*b_jsendbuf++];
2403f6d58c54SBarry Smith       m--;
2404f6d58c54SBarry Smith     }
2405f6d58c54SBarry Smith 
2406f6d58c54SBarry Smith     /* put in diagonal portion */
2407f6d58c54SBarry Smith     for (j=ad->i[i]; j<ad->i[i+1]; j++) {
2408f6d58c54SBarry Smith       jsendbuf[cnt++] = A->rmap->rstart/bs + *a_jsendbuf++;
2409f6d58c54SBarry Smith     }
2410f6d58c54SBarry Smith 
2411f6d58c54SBarry Smith     /* put in upper diagonal portion */
2412f6d58c54SBarry Smith     while (m-- > 0) {
2413f6d58c54SBarry Smith       jsendbuf[cnt++] = garray[*b_jsendbuf++];
2414f6d58c54SBarry Smith     }
2415f6d58c54SBarry Smith   }
2416e32f2f54SBarry Smith   if (cnt != sendcount) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupted PETSc matrix: nz given %D actual nz %D",sendcount,cnt);
2417f6d58c54SBarry Smith 
2418f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2419f6d58c54SBarry Smith     Gather all column indices to all processors
2420f6d58c54SBarry Smith   */
2421f6d58c54SBarry Smith   for (i=0; i<size; i++) {
2422f6d58c54SBarry Smith     recvcounts[i] = 0;
2423f6d58c54SBarry Smith     for (j=A->rmap->range[i]/bs; j<A->rmap->range[i+1]/bs; j++) {
2424f6d58c54SBarry Smith       recvcounts[i] += lens[j];
2425f6d58c54SBarry Smith     }
2426f6d58c54SBarry Smith   }
2427f6d58c54SBarry Smith   displs[0]  = 0;
2428f6d58c54SBarry Smith   for (i=1; i<size; i++) {
2429f6d58c54SBarry Smith     displs[i] = displs[i-1] + recvcounts[i-1];
2430f6d58c54SBarry Smith   }
2431f6d58c54SBarry Smith #if defined(PETSC_HAVE_MPI_IN_PLACE)
2432f6d58c54SBarry Smith   ierr = MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,b->j,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr);
2433f6d58c54SBarry Smith #else
2434f6d58c54SBarry Smith   ierr = MPI_Allgatherv(jsendbuf,sendcount,MPIU_INT,b->j,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr);
2435f6d58c54SBarry Smith #endif
2436f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2437f6d58c54SBarry Smith     Assemble the matrix into useable form (note numerical values not yet set)
2438f6d58c54SBarry Smith   */
2439f6d58c54SBarry Smith   /* set the b->ilen (length of each row) values */
2440f6d58c54SBarry Smith   ierr = PetscMemcpy(b->ilen,lens,(A->rmap->N/bs)*sizeof(PetscInt));CHKERRQ(ierr);
2441f6d58c54SBarry Smith   /* set the b->i indices */
2442f6d58c54SBarry Smith   b->i[0] = 0;
2443f6d58c54SBarry Smith   for (i=1; i<=A->rmap->N/bs; i++) {
2444f6d58c54SBarry Smith     b->i[i] = b->i[i-1] + lens[i-1];
2445f6d58c54SBarry Smith   }
2446f6d58c54SBarry Smith   ierr = PetscFree(lens);CHKERRQ(ierr);
2447f6d58c54SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2448f6d58c54SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2449f6d58c54SBarry Smith   ierr = PetscFree(recvcounts);CHKERRQ(ierr);
2450f6d58c54SBarry Smith 
2451f6d58c54SBarry Smith   if (A->symmetric){
2452f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
2453f6d58c54SBarry Smith   } else if (A->hermitian) {
2454f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
2455f6d58c54SBarry Smith   } else if (A->structurally_symmetric) {
2456f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
2457f6d58c54SBarry Smith   }
2458f6d58c54SBarry Smith   *newmat = B;
2459f6d58c54SBarry Smith   PetscFunctionReturn(0);
2460f6d58c54SBarry Smith }
2461f6d58c54SBarry Smith 
2462b1a666ecSBarry Smith #undef __FUNCT__
2463b1a666ecSBarry Smith #define __FUNCT__ "MatSOR_MPIBAIJ"
2464b1a666ecSBarry Smith PetscErrorCode MatSOR_MPIBAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
2465b1a666ecSBarry Smith {
2466b1a666ecSBarry Smith   Mat_MPIBAIJ    *mat = (Mat_MPIBAIJ*)matin->data;
2467b1a666ecSBarry Smith   PetscErrorCode ierr;
2468b1a666ecSBarry Smith   Vec            bb1 = 0;
2469b1a666ecSBarry Smith 
2470b1a666ecSBarry Smith   PetscFunctionBegin;
2471b1a666ecSBarry Smith   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS) {
2472b1a666ecSBarry Smith     ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
2473b1a666ecSBarry Smith   }
2474b1a666ecSBarry Smith 
2475b1a666ecSBarry Smith   if (flag == SOR_APPLY_UPPER) {
2476b1a666ecSBarry Smith     ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2477b1a666ecSBarry Smith     PetscFunctionReturn(0);
2478b1a666ecSBarry Smith   }
2479b1a666ecSBarry Smith 
2480b1a666ecSBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
2481b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2482b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2483b1a666ecSBarry Smith       its--;
2484b1a666ecSBarry Smith     }
2485b1a666ecSBarry Smith 
2486b1a666ecSBarry Smith     while (its--) {
2487b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2488b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2489b1a666ecSBarry Smith 
2490b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2491b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2492b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2493b1a666ecSBarry Smith 
2494b1a666ecSBarry Smith       /* local sweep */
2495b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2496b1a666ecSBarry Smith     }
2497b1a666ecSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
2498b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2499b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2500b1a666ecSBarry Smith       its--;
2501b1a666ecSBarry Smith     }
2502b1a666ecSBarry Smith     while (its--) {
2503b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2504b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2505b1a666ecSBarry Smith 
2506b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2507b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2508b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2509b1a666ecSBarry Smith 
2510b1a666ecSBarry Smith       /* local sweep */
2511b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2512b1a666ecSBarry Smith     }
2513b1a666ecSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
2514b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2515b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2516b1a666ecSBarry Smith       its--;
2517b1a666ecSBarry Smith     }
2518b1a666ecSBarry Smith     while (its--) {
2519b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2520b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2521b1a666ecSBarry Smith 
2522b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2523b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2524b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2525b1a666ecSBarry Smith 
2526b1a666ecSBarry Smith       /* local sweep */
2527b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2528b1a666ecSBarry Smith     }
2529e7e72b3dSBarry Smith   } else SETERRQ(((PetscObject)matin)->comm,PETSC_ERR_SUP,"Parallel version of SOR requested not supported");
2530b1a666ecSBarry Smith 
2531b1a666ecSBarry Smith   if (bb1) {ierr = VecDestroy(bb1);CHKERRQ(ierr);}
2532b1a666ecSBarry Smith   PetscFunctionReturn(0);
2533b1a666ecSBarry Smith }
2534b1a666ecSBarry Smith 
2535f6d58c54SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_BAIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2536f6d58c54SBarry Smith 
25378c7482ecSBarry Smith 
253879bdfe76SSatish Balay /* -------------------------------------------------------------------*/
2539cc2dc46cSBarry Smith static struct _MatOps MatOps_Values = {
2540cc2dc46cSBarry Smith        MatSetValues_MPIBAIJ,
2541cc2dc46cSBarry Smith        MatGetRow_MPIBAIJ,
2542cc2dc46cSBarry Smith        MatRestoreRow_MPIBAIJ,
2543cc2dc46cSBarry Smith        MatMult_MPIBAIJ,
254497304618SKris Buschelman /* 4*/ MatMultAdd_MPIBAIJ,
25457c922b88SBarry Smith        MatMultTranspose_MPIBAIJ,
25467c922b88SBarry Smith        MatMultTransposeAdd_MPIBAIJ,
2547cc2dc46cSBarry Smith        0,
2548cc2dc46cSBarry Smith        0,
2549cc2dc46cSBarry Smith        0,
255097304618SKris Buschelman /*10*/ 0,
2551cc2dc46cSBarry Smith        0,
2552cc2dc46cSBarry Smith        0,
2553b1a666ecSBarry Smith        MatSOR_MPIBAIJ,
2554cc2dc46cSBarry Smith        MatTranspose_MPIBAIJ,
255597304618SKris Buschelman /*15*/ MatGetInfo_MPIBAIJ,
25567fc3c18eSBarry Smith        MatEqual_MPIBAIJ,
2557cc2dc46cSBarry Smith        MatGetDiagonal_MPIBAIJ,
2558cc2dc46cSBarry Smith        MatDiagonalScale_MPIBAIJ,
2559cc2dc46cSBarry Smith        MatNorm_MPIBAIJ,
256097304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIBAIJ,
2561cc2dc46cSBarry Smith        MatAssemblyEnd_MPIBAIJ,
2562cc2dc46cSBarry Smith        MatSetOption_MPIBAIJ,
2563cc2dc46cSBarry Smith        MatZeroEntries_MPIBAIJ,
2564d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIBAIJ,
2565cc2dc46cSBarry Smith        0,
2566cc2dc46cSBarry Smith        0,
2567cc2dc46cSBarry Smith        0,
2568cc2dc46cSBarry Smith        0,
2569d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_MPIBAIJ,
2570273d9f13SBarry Smith        0,
2571cc2dc46cSBarry Smith        0,
2572cc2dc46cSBarry Smith        0,
2573cc2dc46cSBarry Smith        0,
2574d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIBAIJ,
2575cc2dc46cSBarry Smith        0,
2576cc2dc46cSBarry Smith        0,
2577cc2dc46cSBarry Smith        0,
2578cc2dc46cSBarry Smith        0,
2579d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIBAIJ,
2580cc2dc46cSBarry Smith        MatGetSubMatrices_MPIBAIJ,
2581cc2dc46cSBarry Smith        MatIncreaseOverlap_MPIBAIJ,
2582cc2dc46cSBarry Smith        MatGetValues_MPIBAIJ,
25833c896bc6SHong Zhang        MatCopy_MPIBAIJ,
2584d519adbfSMatthew Knepley /*44*/ 0,
2585cc2dc46cSBarry Smith        MatScale_MPIBAIJ,
2586cc2dc46cSBarry Smith        0,
2587cc2dc46cSBarry Smith        0,
2588cc2dc46cSBarry Smith        0,
258941c166b1SJed Brown /*49*/ MatSetBlockSize_MPIBAIJ,
2590cc2dc46cSBarry Smith        0,
2591cc2dc46cSBarry Smith        0,
2592cc2dc46cSBarry Smith        0,
2593cc2dc46cSBarry Smith        0,
2594f6d58c54SBarry Smith /*54*/ MatFDColoringCreate_MPIBAIJ,
2595cc2dc46cSBarry Smith        0,
2596cc2dc46cSBarry Smith        MatSetUnfactored_MPIBAIJ,
259782094794SBarry Smith        MatPermute_MPIBAIJ,
2598cc2dc46cSBarry Smith        MatSetValuesBlocked_MPIBAIJ,
2599d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIBAIJ,
2600f14a1c24SBarry Smith        MatDestroy_MPIBAIJ,
2601f14a1c24SBarry Smith        MatView_MPIBAIJ,
2602357abbc8SBarry Smith        0,
26037843d17aSBarry Smith        0,
2604d519adbfSMatthew Knepley /*64*/ 0,
26057843d17aSBarry Smith        0,
26067843d17aSBarry Smith        0,
26077843d17aSBarry Smith        0,
26087843d17aSBarry Smith        0,
2609d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIBAIJ,
26107843d17aSBarry Smith        0,
261197304618SKris Buschelman        0,
261297304618SKris Buschelman        0,
261397304618SKris Buschelman        0,
2614d519adbfSMatthew Knepley /*74*/ 0,
2615f6d58c54SBarry Smith        MatFDColoringApply_BAIJ,
261697304618SKris Buschelman        0,
261797304618SKris Buschelman        0,
261897304618SKris Buschelman        0,
2619d519adbfSMatthew Knepley /*79*/ 0,
262097304618SKris Buschelman        0,
262197304618SKris Buschelman        0,
262297304618SKris Buschelman        0,
2623865e5f61SKris Buschelman        MatLoad_MPIBAIJ,
2624d519adbfSMatthew Knepley /*84*/ 0,
2625865e5f61SKris Buschelman        0,
2626865e5f61SKris Buschelman        0,
2627865e5f61SKris Buschelman        0,
2628865e5f61SKris Buschelman        0,
2629d519adbfSMatthew Knepley /*89*/ 0,
2630865e5f61SKris Buschelman        0,
2631865e5f61SKris Buschelman        0,
2632865e5f61SKris Buschelman        0,
2633865e5f61SKris Buschelman        0,
2634d519adbfSMatthew Knepley /*94*/ 0,
2635865e5f61SKris Buschelman        0,
2636865e5f61SKris Buschelman        0,
263799cafbc1SBarry Smith        0,
263899cafbc1SBarry Smith        0,
2639d519adbfSMatthew Knepley /*99*/ 0,
264099cafbc1SBarry Smith        0,
264199cafbc1SBarry Smith        0,
264299cafbc1SBarry Smith        0,
264399cafbc1SBarry Smith        0,
2644d519adbfSMatthew Knepley /*104*/0,
264599cafbc1SBarry Smith        MatRealPart_MPIBAIJ,
26468c7482ecSBarry Smith        MatImaginaryPart_MPIBAIJ,
26478c7482ecSBarry Smith        0,
26488c7482ecSBarry Smith        0,
2649d519adbfSMatthew Knepley /*109*/0,
26508c7482ecSBarry Smith        0,
26518c7482ecSBarry Smith        0,
26528c7482ecSBarry Smith        0,
26538c7482ecSBarry Smith        0,
2654f6d58c54SBarry Smith /*114*/MatGetSeqNonzerostructure_MPIBAIJ,
26558c7482ecSBarry Smith        0,
2656*4683f7a4SShri Abhyankar        MatGetGhosts_MPIBAIJ,
2657*4683f7a4SShri Abhyankar        0,
2658*4683f7a4SShri Abhyankar        0,
2659*4683f7a4SShri Abhyankar /*119*/0,
2660*4683f7a4SShri Abhyankar        0,
2661*4683f7a4SShri Abhyankar        0,
2662*4683f7a4SShri Abhyankar        0,
2663*4683f7a4SShri Abhyankar        MatLoadnew_MPIBAIJ
26648c7482ecSBarry Smith };
266579bdfe76SSatish Balay 
2666e18c124aSSatish Balay EXTERN_C_BEGIN
26674a2ae208SSatish Balay #undef __FUNCT__
26684a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonalBlock_MPIBAIJ"
2669be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetDiagonalBlock_MPIBAIJ(Mat A,PetscTruth *iscopy,MatReuse reuse,Mat *a)
26705ef9f2a5SBarry Smith {
26715ef9f2a5SBarry Smith   PetscFunctionBegin;
26725ef9f2a5SBarry Smith   *a      = ((Mat_MPIBAIJ *)A->data)->A;
26735ef9f2a5SBarry Smith   *iscopy = PETSC_FALSE;
26745ef9f2a5SBarry Smith   PetscFunctionReturn(0);
26755ef9f2a5SBarry Smith }
2676e18c124aSSatish Balay EXTERN_C_END
267779bdfe76SSatish Balay 
2678273d9f13SBarry Smith EXTERN_C_BEGIN
2679f69a0ea3SMatthew Knepley extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIBAIJ_MPISBAIJ(Mat, MatType,MatReuse,Mat*);
2680d94109b8SHong Zhang EXTERN_C_END
2681d94109b8SHong Zhang 
2682b8d659d7SLisandro Dalcin EXTERN_C_BEGIN
2683aac34f13SBarry Smith #undef __FUNCT__
2684aac34f13SBarry Smith #define __FUNCT__ "MatMPIBAIJSetPreallocationCSR_MPIBAIJ"
2685cf12db73SBarry Smith PetscErrorCode MatMPIBAIJSetPreallocationCSR_MPIBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
2686aac34f13SBarry Smith {
2687b8d659d7SLisandro Dalcin   PetscInt       m,rstart,cstart,cend;
2688b8d659d7SLisandro Dalcin   PetscInt       i,j,d,nz,nz_max=0,*d_nnz=0,*o_nnz=0;
2689b8d659d7SLisandro Dalcin   const PetscInt *JJ=0;
2690b8d659d7SLisandro Dalcin   PetscScalar    *values=0;
2691aac34f13SBarry Smith   PetscErrorCode ierr;
2692aac34f13SBarry Smith 
2693aac34f13SBarry Smith   PetscFunctionBegin;
2694b8d659d7SLisandro Dalcin 
269565e19b50SBarry Smith   if (bs < 1) SETERRQ1(((PetscObject)B)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
269626283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
269726283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
269826283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
269926283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2700d0f46423SBarry Smith   m      = B->rmap->n/bs;
2701d0f46423SBarry Smith   rstart = B->rmap->rstart/bs;
2702d0f46423SBarry Smith   cstart = B->cmap->rstart/bs;
2703d0f46423SBarry Smith   cend   = B->cmap->rend/bs;
2704b8d659d7SLisandro Dalcin 
2705e32f2f54SBarry Smith   if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
2706fca92195SBarry Smith   ierr  = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr);
2707aac34f13SBarry Smith   for (i=0; i<m; i++) {
2708cf12db73SBarry Smith     nz = ii[i+1] - ii[i];
2709e32f2f54SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative number of columns %D",i,nz);
2710b8d659d7SLisandro Dalcin     nz_max = PetscMax(nz_max,nz);
2711cf12db73SBarry Smith     JJ  = jj + ii[i];
2712b8d659d7SLisandro Dalcin     for (j=0; j<nz; j++) {
2713aac34f13SBarry Smith       if (*JJ >= cstart) break;
2714aac34f13SBarry Smith       JJ++;
2715aac34f13SBarry Smith     }
2716aac34f13SBarry Smith     d = 0;
2717b8d659d7SLisandro Dalcin     for (; j<nz; j++) {
2718aac34f13SBarry Smith       if (*JJ++ >= cend) break;
2719aac34f13SBarry Smith       d++;
2720aac34f13SBarry Smith     }
2721aac34f13SBarry Smith     d_nnz[i] = d;
2722b8d659d7SLisandro Dalcin     o_nnz[i] = nz - d;
2723aac34f13SBarry Smith   }
2724aac34f13SBarry Smith   ierr = MatMPIBAIJSetPreallocation(B,bs,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
2725fca92195SBarry Smith   ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr);
2726aac34f13SBarry Smith 
2727b8d659d7SLisandro Dalcin   values = (PetscScalar*)V;
2728b8d659d7SLisandro Dalcin   if (!values) {
2729fca92195SBarry Smith     ierr = PetscMalloc(bs*bs*nz_max*sizeof(PetscScalar),&values);CHKERRQ(ierr);
2730b8d659d7SLisandro Dalcin     ierr = PetscMemzero(values,bs*bs*nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
2731b8d659d7SLisandro Dalcin   }
2732b8d659d7SLisandro Dalcin   for (i=0; i<m; i++) {
2733b8d659d7SLisandro Dalcin     PetscInt          row    = i + rstart;
2734cf12db73SBarry Smith     PetscInt          ncols  = ii[i+1] - ii[i];
2735cf12db73SBarry Smith     const PetscInt    *icols = jj + ii[i];
2736cf12db73SBarry Smith     const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
2737b8d659d7SLisandro Dalcin     ierr = MatSetValuesBlocked_MPIBAIJ(B,1,&row,ncols,icols,svals,INSERT_VALUES);CHKERRQ(ierr);
2738aac34f13SBarry Smith   }
2739aac34f13SBarry Smith 
2740b8d659d7SLisandro Dalcin   if (!V) { ierr = PetscFree(values);CHKERRQ(ierr); }
2741aac34f13SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2742aac34f13SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2743aac34f13SBarry Smith 
2744aac34f13SBarry Smith   PetscFunctionReturn(0);
2745aac34f13SBarry Smith }
2746b8d659d7SLisandro Dalcin EXTERN_C_END
2747aac34f13SBarry Smith 
2748aac34f13SBarry Smith #undef __FUNCT__
2749aac34f13SBarry Smith #define __FUNCT__ "MatMPIBAIJSetPreallocationCSR"
2750aac34f13SBarry Smith /*@C
2751aac34f13SBarry Smith    MatMPIBAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
2752aac34f13SBarry Smith    (the default parallel PETSc format).
2753aac34f13SBarry Smith 
2754aac34f13SBarry Smith    Collective on MPI_Comm
2755aac34f13SBarry Smith 
2756aac34f13SBarry Smith    Input Parameters:
2757aac34f13SBarry Smith +  A - the matrix
2758aac34f13SBarry Smith .  i - the indices into j for the start of each local row (starts with zero)
2759aac34f13SBarry Smith .  j - the column indices for each local row (starts with zero) these must be sorted for each row
2760aac34f13SBarry Smith -  v - optional values in the matrix
2761aac34f13SBarry Smith 
2762aac34f13SBarry Smith    Level: developer
2763aac34f13SBarry Smith 
2764aac34f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2765aac34f13SBarry Smith 
2766aac34f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIBAIJSetPreallocation(), MatCreateMPIAIJ(), MPIAIJ
2767aac34f13SBarry Smith @*/
2768be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2769aac34f13SBarry Smith {
2770aac34f13SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]);
2771aac34f13SBarry Smith 
2772aac34f13SBarry Smith   PetscFunctionBegin;
2773aac34f13SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIBAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
2774aac34f13SBarry Smith   if (f) {
2775aac34f13SBarry Smith     ierr = (*f)(B,bs,i,j,v);CHKERRQ(ierr);
2776aac34f13SBarry Smith   }
2777aac34f13SBarry Smith   PetscFunctionReturn(0);
2778aac34f13SBarry Smith }
2779aac34f13SBarry Smith 
2780d94109b8SHong Zhang EXTERN_C_BEGIN
27814a2ae208SSatish Balay #undef __FUNCT__
2782a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIBAIJSetPreallocation_MPIBAIJ"
2783be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJSetPreallocation_MPIBAIJ(Mat B,PetscInt bs,PetscInt d_nz,PetscInt *d_nnz,PetscInt o_nz,PetscInt *o_nnz)
2784a23d5eceSKris Buschelman {
2785a23d5eceSKris Buschelman   Mat_MPIBAIJ    *b;
2786dfbe8321SBarry Smith   PetscErrorCode ierr;
2787db4efbfdSBarry Smith   PetscInt       i, newbs = PetscAbs(bs);
2788a23d5eceSKris Buschelman 
2789a23d5eceSKris Buschelman   PetscFunctionBegin;
2790db4efbfdSBarry Smith   if (bs < 0) {
27917adad957SLisandro Dalcin     ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Options for MPIBAIJ matrix","Mat");CHKERRQ(ierr);
2792db4efbfdSBarry Smith       ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatMPIBAIJSetPreallocation",newbs,&newbs,PETSC_NULL);CHKERRQ(ierr);
27938c07d4e3SBarry Smith     ierr = PetscOptionsEnd();CHKERRQ(ierr);
2794db4efbfdSBarry Smith     bs   = PetscAbs(bs);
2795db4efbfdSBarry Smith   }
2796e7e72b3dSBarry Smith   if ((d_nnz || o_nnz) && newbs != bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot change blocksize from command line if setting d_nnz or o_nnz");
2797db4efbfdSBarry Smith   bs = newbs;
2798db4efbfdSBarry Smith 
2799a23d5eceSKris Buschelman 
2800e7e72b3dSBarry Smith   if (bs < 1) SETERRQ(((PetscObject)B)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive");
2801a23d5eceSKris Buschelman   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
2802a23d5eceSKris Buschelman   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
2803e32f2f54SBarry Smith   if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz);
2804e32f2f54SBarry Smith   if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz);
2805899cda47SBarry Smith 
280626283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
280726283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
280826283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
280926283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2810899cda47SBarry Smith 
2811a23d5eceSKris Buschelman   if (d_nnz) {
2812d0f46423SBarry Smith     for (i=0; i<B->rmap->n/bs; i++) {
2813e32f2f54SBarry Smith       if (d_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than -1: local row %D value %D",i,d_nnz[i]);
2814a23d5eceSKris Buschelman     }
2815a23d5eceSKris Buschelman   }
2816a23d5eceSKris Buschelman   if (o_nnz) {
2817d0f46423SBarry Smith     for (i=0; i<B->rmap->n/bs; i++) {
2818e32f2f54SBarry Smith       if (o_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than -1: local row %D value %D",i,o_nnz[i]);
2819a23d5eceSKris Buschelman     }
2820a23d5eceSKris Buschelman   }
2821a23d5eceSKris Buschelman 
2822a23d5eceSKris Buschelman   b = (Mat_MPIBAIJ*)B->data;
2823a23d5eceSKris Buschelman   b->bs2 = bs*bs;
2824d0f46423SBarry Smith   b->mbs = B->rmap->n/bs;
2825d0f46423SBarry Smith   b->nbs = B->cmap->n/bs;
2826d0f46423SBarry Smith   b->Mbs = B->rmap->N/bs;
2827d0f46423SBarry Smith   b->Nbs = B->cmap->N/bs;
2828a23d5eceSKris Buschelman 
2829a23d5eceSKris Buschelman   for (i=0; i<=b->size; i++) {
2830d0f46423SBarry Smith     b->rangebs[i] = B->rmap->range[i]/bs;
2831a23d5eceSKris Buschelman   }
2832d0f46423SBarry Smith   b->rstartbs = B->rmap->rstart/bs;
2833d0f46423SBarry Smith   b->rendbs   = B->rmap->rend/bs;
2834d0f46423SBarry Smith   b->cstartbs = B->cmap->rstart/bs;
2835d0f46423SBarry Smith   b->cendbs   = B->cmap->rend/bs;
2836a23d5eceSKris Buschelman 
2837526dfc15SBarry Smith   if (!B->preallocated) {
2838f69a0ea3SMatthew Knepley     ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr);
2839d0f46423SBarry Smith     ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr);
28409c097c71SKris Buschelman     ierr = MatSetType(b->A,MATSEQBAIJ);CHKERRQ(ierr);
284152e6d16bSBarry Smith     ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr);
2842f69a0ea3SMatthew Knepley     ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr);
2843d0f46423SBarry Smith     ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr);
28449c097c71SKris Buschelman     ierr = MatSetType(b->B,MATSEQBAIJ);CHKERRQ(ierr);
284552e6d16bSBarry Smith     ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr);
28467adad957SLisandro Dalcin     ierr = MatStashCreate_Private(((PetscObject)B)->comm,bs,&B->bstash);CHKERRQ(ierr);
2847526dfc15SBarry Smith   }
2848a23d5eceSKris Buschelman 
2849526dfc15SBarry Smith   ierr = MatSeqBAIJSetPreallocation(b->A,bs,d_nz,d_nnz);CHKERRQ(ierr);
2850526dfc15SBarry Smith   ierr = MatSeqBAIJSetPreallocation(b->B,bs,o_nz,o_nnz);CHKERRQ(ierr);
2851526dfc15SBarry Smith   B->preallocated = PETSC_TRUE;
2852a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2853a23d5eceSKris Buschelman }
2854a23d5eceSKris Buschelman EXTERN_C_END
2855a23d5eceSKris Buschelman 
2856a23d5eceSKris Buschelman EXTERN_C_BEGIN
2857be1d678aSKris Buschelman EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalScaleLocal_MPIBAIJ(Mat,Vec);
2858be1d678aSKris Buschelman EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatSetHashTableFactor_MPIBAIJ(Mat,PetscReal);
285992b32695SKris Buschelman EXTERN_C_END
28605bf65638SKris Buschelman 
286182094794SBarry Smith 
286282094794SBarry Smith EXTERN_C_BEGIN
286382094794SBarry Smith #undef __FUNCT__
286482094794SBarry Smith #define __FUNCT__ "MatConvert_MPIBAIJ_MPIAdj"
286582094794SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIBAIJ_MPIAdj(Mat B, const MatType newtype,MatReuse reuse,Mat *adj)
286682094794SBarry Smith {
286782094794SBarry Smith   Mat_MPIBAIJ    *b = (Mat_MPIBAIJ*)B->data;
286882094794SBarry Smith   PetscErrorCode ierr;
286982094794SBarry Smith   Mat_SeqBAIJ    *d = (Mat_SeqBAIJ*) b->A->data,*o = (Mat_SeqBAIJ*) b->B->data;
287082094794SBarry Smith   PetscInt       M = B->rmap->n/B->rmap->bs,i,*ii,*jj,cnt,j,k,rstart = B->rmap->rstart/B->rmap->bs;
287182094794SBarry Smith   const PetscInt *id = d->i, *jd = d->j, *io = o->i, *jo = o->j, *garray = b->garray;
287282094794SBarry Smith 
287382094794SBarry Smith   PetscFunctionBegin;
287482094794SBarry Smith   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&ii);CHKERRQ(ierr);
287582094794SBarry Smith   ii[0] = 0;
287682094794SBarry Smith   CHKMEMQ;
287782094794SBarry Smith   for (i=0; i<M; i++) {
2878e32f2f54SBarry Smith     if ((id[i+1] - id[i]) < 0) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Indices wrong %D %D %D",i,id[i],id[i+1]);
2879e32f2f54SBarry Smith     if ((io[i+1] - io[i]) < 0) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Indices wrong %D %D %D",i,io[i],io[i+1]);
288082094794SBarry Smith     ii[i+1] = ii[i] + id[i+1] - id[i] + io[i+1] - io[i];
288182094794SBarry Smith     /* remove one from count of matrix has diagonal */
288282094794SBarry Smith     for (j=id[i]; j<id[i+1]; j++) {
288382094794SBarry Smith       if (jd[j] == i) {ii[i+1]--;break;}
288482094794SBarry Smith     }
288582094794SBarry Smith   CHKMEMQ;
288682094794SBarry Smith   }
288782094794SBarry Smith   ierr = PetscMalloc(ii[M]*sizeof(PetscInt),&jj);CHKERRQ(ierr);
288882094794SBarry Smith   cnt = 0;
288982094794SBarry Smith   for (i=0; i<M; i++) {
289082094794SBarry Smith     for (j=io[i]; j<io[i+1]; j++) {
289182094794SBarry Smith       if (garray[jo[j]] > rstart) break;
289282094794SBarry Smith       jj[cnt++] = garray[jo[j]];
289382094794SBarry Smith   CHKMEMQ;
289482094794SBarry Smith     }
289582094794SBarry Smith     for (k=id[i]; k<id[i+1]; k++) {
289682094794SBarry Smith       if (jd[k] != i) {
289782094794SBarry Smith         jj[cnt++] = rstart + jd[k];
289882094794SBarry Smith   CHKMEMQ;
289982094794SBarry Smith       }
290082094794SBarry Smith     }
290182094794SBarry Smith     for (;j<io[i+1]; j++) {
290282094794SBarry Smith       jj[cnt++] = garray[jo[j]];
290382094794SBarry Smith   CHKMEMQ;
290482094794SBarry Smith     }
290582094794SBarry Smith   }
290682094794SBarry Smith   ierr = MatCreateMPIAdj(((PetscObject)B)->comm,M,B->cmap->N/B->rmap->bs,ii,jj,PETSC_NULL,adj);CHKERRQ(ierr);
290782094794SBarry Smith   PetscFunctionReturn(0);
290882094794SBarry Smith }
2909dbf0e21dSBarry Smith EXTERN_C_END
291082094794SBarry Smith 
2911450b117fSShri Abhyankar EXTERN_C_BEGIN
2912450b117fSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
2913bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_baij_mumps(Mat,MatFactorType,Mat*);
2914450b117fSShri Abhyankar #endif
2915450b117fSShri Abhyankar EXTERN_C_END
2916450b117fSShri Abhyankar 
29170bad9183SKris Buschelman /*MC
2918fafad747SKris Buschelman    MATMPIBAIJ - MATMPIBAIJ = "mpibaij" - A matrix type to be used for distributed block sparse matrices.
29190bad9183SKris Buschelman 
29200bad9183SKris Buschelman    Options Database Keys:
29218c07d4e3SBarry Smith + -mat_type mpibaij - sets the matrix type to "mpibaij" during a call to MatSetFromOptions()
29228c07d4e3SBarry Smith . -mat_block_size <bs> - set the blocksize used to store the matrix
29238c07d4e3SBarry Smith - -mat_use_hash_table <fact>
29240bad9183SKris Buschelman 
29250bad9183SKris Buschelman   Level: beginner
29260bad9183SKris Buschelman 
29270bad9183SKris Buschelman .seealso: MatCreateMPIBAIJ
29280bad9183SKris Buschelman M*/
29290bad9183SKris Buschelman 
293092b32695SKris Buschelman EXTERN_C_BEGIN
2931a23d5eceSKris Buschelman #undef __FUNCT__
29324a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIBAIJ"
2933be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MPIBAIJ(Mat B)
2934273d9f13SBarry Smith {
2935273d9f13SBarry Smith   Mat_MPIBAIJ    *b;
2936dfbe8321SBarry Smith   PetscErrorCode ierr;
2937273d9f13SBarry Smith   PetscTruth     flg;
2938273d9f13SBarry Smith 
2939273d9f13SBarry Smith   PetscFunctionBegin;
294038f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_MPIBAIJ,&b);CHKERRQ(ierr);
294182502324SSatish Balay   B->data = (void*)b;
294282502324SSatish Balay 
2943085a36d4SBarry Smith 
2944273d9f13SBarry Smith   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2945273d9f13SBarry Smith   B->mapping    = 0;
2946273d9f13SBarry Smith   B->assembled  = PETSC_FALSE;
2947273d9f13SBarry Smith 
2948273d9f13SBarry Smith   B->insertmode = NOT_SET_VALUES;
29497adad957SLisandro Dalcin   ierr = MPI_Comm_rank(((PetscObject)B)->comm,&b->rank);CHKERRQ(ierr);
29507adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&b->size);CHKERRQ(ierr);
2951273d9f13SBarry Smith 
2952273d9f13SBarry Smith   /* build local table of row and column ownerships */
2953899cda47SBarry Smith   ierr = PetscMalloc((b->size+1)*sizeof(PetscInt),&b->rangebs);CHKERRQ(ierr);
2954273d9f13SBarry Smith 
2955273d9f13SBarry Smith   /* build cache for off array entries formed */
29567adad957SLisandro Dalcin   ierr = MatStashCreate_Private(((PetscObject)B)->comm,1,&B->stash);CHKERRQ(ierr);
2957273d9f13SBarry Smith   b->donotstash  = PETSC_FALSE;
2958273d9f13SBarry Smith   b->colmap      = PETSC_NULL;
2959273d9f13SBarry Smith   b->garray      = PETSC_NULL;
2960273d9f13SBarry Smith   b->roworiented = PETSC_TRUE;
2961273d9f13SBarry Smith 
2962273d9f13SBarry Smith   /* stuff used in block assembly */
2963273d9f13SBarry Smith   b->barray       = 0;
2964273d9f13SBarry Smith 
2965273d9f13SBarry Smith   /* stuff used for matrix vector multiply */
2966273d9f13SBarry Smith   b->lvec         = 0;
2967273d9f13SBarry Smith   b->Mvctx        = 0;
2968273d9f13SBarry Smith 
2969273d9f13SBarry Smith   /* stuff for MatGetRow() */
2970273d9f13SBarry Smith   b->rowindices   = 0;
2971273d9f13SBarry Smith   b->rowvalues    = 0;
2972273d9f13SBarry Smith   b->getrowactive = PETSC_FALSE;
2973273d9f13SBarry Smith 
2974273d9f13SBarry Smith   /* hash table stuff */
2975273d9f13SBarry Smith   b->ht           = 0;
2976273d9f13SBarry Smith   b->hd           = 0;
2977273d9f13SBarry Smith   b->ht_size      = 0;
2978273d9f13SBarry Smith   b->ht_flag      = PETSC_FALSE;
2979273d9f13SBarry Smith   b->ht_fact      = 0;
2980273d9f13SBarry Smith   b->ht_total_ct  = 0;
2981273d9f13SBarry Smith   b->ht_insert_ct = 0;
2982273d9f13SBarry Smith 
29837adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)B)->comm,PETSC_NULL,"Options for loading MPIBAIJ matrix 1","Mat");CHKERRQ(ierr);
29848c07d4e3SBarry Smith     ierr = PetscOptionsTruth("-mat_use_hash_table","Use hash table to save memory in constructing matrix","MatSetOption",PETSC_FALSE,&flg,PETSC_NULL);CHKERRQ(ierr);
2985273d9f13SBarry Smith     if (flg) {
2986f6275e2eSBarry Smith       PetscReal fact = 1.39;
29874e0d8c25SBarry Smith       ierr = MatSetOption(B,MAT_USE_HASH_TABLE,PETSC_TRUE);CHKERRQ(ierr);
29888c07d4e3SBarry Smith       ierr = PetscOptionsReal("-mat_use_hash_table","Use hash table factor","MatMPIBAIJSetHashTableFactor",fact,&fact,PETSC_NULL);CHKERRQ(ierr);
2989273d9f13SBarry Smith       if (fact <= 1.0) fact = 1.39;
2990273d9f13SBarry Smith       ierr = MatMPIBAIJSetHashTableFactor(B,fact);CHKERRQ(ierr);
29911e2582c4SBarry Smith       ierr = PetscInfo1(B,"Hash table Factor used %5.2f\n",fact);CHKERRQ(ierr);
2992273d9f13SBarry Smith     }
29938c07d4e3SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
29948c07d4e3SBarry Smith 
2995450b117fSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
2996bccb9932SShri Abhyankar   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", "MatGetFactor_baij_mumps",MatGetFactor_baij_mumps);CHKERRQ(ierr);
2997450b117fSShri Abhyankar #endif
299882094794SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpibaij_mpiadj_C",
299982094794SBarry Smith                                      "MatConvert_MPIBAIJ_MPIAdj",
300082094794SBarry Smith                                       MatConvert_MPIBAIJ_MPIAdj);CHKERRQ(ierr);
3001273d9f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3002273d9f13SBarry Smith                                      "MatStoreValues_MPIBAIJ",
3003273d9f13SBarry Smith                                      MatStoreValues_MPIBAIJ);CHKERRQ(ierr);
3004273d9f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3005273d9f13SBarry Smith                                      "MatRetrieveValues_MPIBAIJ",
3006273d9f13SBarry Smith                                      MatRetrieveValues_MPIBAIJ);CHKERRQ(ierr);
3007273d9f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
3008273d9f13SBarry Smith                                      "MatGetDiagonalBlock_MPIBAIJ",
3009273d9f13SBarry Smith                                      MatGetDiagonalBlock_MPIBAIJ);CHKERRQ(ierr);
3010a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIBAIJSetPreallocation_C",
3011a23d5eceSKris Buschelman                                      "MatMPIBAIJSetPreallocation_MPIBAIJ",
3012a23d5eceSKris Buschelman                                      MatMPIBAIJSetPreallocation_MPIBAIJ);CHKERRQ(ierr);
3013aac34f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIBAIJSetPreallocationCSR_C",
301444ec7894SLisandro Dalcin 				     "MatMPIBAIJSetPreallocationCSR_MPIBAIJ",
3015aac34f13SBarry Smith 				     MatMPIBAIJSetPreallocationCSR_MPIBAIJ);CHKERRQ(ierr);
301692b32695SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
301792b32695SKris Buschelman                                      "MatDiagonalScaleLocal_MPIBAIJ",
301892b32695SKris Buschelman                                      MatDiagonalScaleLocal_MPIBAIJ);CHKERRQ(ierr);
30195bf65638SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSetHashTableFactor_C",
30205bf65638SKris Buschelman                                      "MatSetHashTableFactor_MPIBAIJ",
30215bf65638SKris Buschelman                                      MatSetHashTableFactor_MPIBAIJ);CHKERRQ(ierr);
302217667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIBAIJ);CHKERRQ(ierr);
3023273d9f13SBarry Smith   PetscFunctionReturn(0);
3024273d9f13SBarry Smith }
3025273d9f13SBarry Smith EXTERN_C_END
3026273d9f13SBarry Smith 
3027209238afSKris Buschelman /*MC
3028002d173eSKris Buschelman    MATBAIJ - MATBAIJ = "baij" - A matrix type to be used for block sparse matrices.
3029209238afSKris Buschelman 
3030209238afSKris Buschelman    This matrix type is identical to MATSEQBAIJ when constructed with a single process communicator,
3031209238afSKris Buschelman    and MATMPIBAIJ otherwise.
3032209238afSKris Buschelman 
3033209238afSKris Buschelman    Options Database Keys:
3034209238afSKris Buschelman . -mat_type baij - sets the matrix type to "baij" during a call to MatSetFromOptions()
3035209238afSKris Buschelman 
3036209238afSKris Buschelman   Level: beginner
3037209238afSKris Buschelman 
3038aac34f13SBarry Smith .seealso: MatCreateMPIBAIJ(),MATSEQBAIJ,MATMPIBAIJ, MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
3039209238afSKris Buschelman M*/
3040209238afSKris Buschelman 
3041209238afSKris Buschelman EXTERN_C_BEGIN
3042209238afSKris Buschelman #undef __FUNCT__
3043209238afSKris Buschelman #define __FUNCT__ "MatCreate_BAIJ"
3044be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_BAIJ(Mat A)
3045dfbe8321SBarry Smith {
30466849ba73SBarry Smith   PetscErrorCode ierr;
3047b24ad042SBarry Smith   PetscMPIInt    size;
3048209238afSKris Buschelman 
3049209238afSKris Buschelman   PetscFunctionBegin;
30507adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)A)->comm,&size);CHKERRQ(ierr);
3051209238afSKris Buschelman   if (size == 1) {
3052209238afSKris Buschelman     ierr = MatSetType(A,MATSEQBAIJ);CHKERRQ(ierr);
3053209238afSKris Buschelman   } else {
3054209238afSKris Buschelman     ierr = MatSetType(A,MATMPIBAIJ);CHKERRQ(ierr);
3055209238afSKris Buschelman   }
3056209238afSKris Buschelman   PetscFunctionReturn(0);
3057209238afSKris Buschelman }
3058209238afSKris Buschelman EXTERN_C_END
3059209238afSKris Buschelman 
30604a2ae208SSatish Balay #undef __FUNCT__
30614a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJSetPreallocation"
3062273d9f13SBarry Smith /*@C
3063aac34f13SBarry Smith    MatMPIBAIJSetPreallocation - Allocates memory for a sparse parallel matrix in block AIJ format
3064273d9f13SBarry Smith    (block compressed row).  For good matrix assembly performance
3065273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
3066273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
3067273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
3068273d9f13SBarry Smith 
3069273d9f13SBarry Smith    Collective on Mat
3070273d9f13SBarry Smith 
3071273d9f13SBarry Smith    Input Parameters:
3072273d9f13SBarry Smith +  A - the matrix
3073273d9f13SBarry Smith .  bs   - size of blockk
3074273d9f13SBarry Smith .  d_nz  - number of block nonzeros per block row in diagonal portion of local
3075273d9f13SBarry Smith            submatrix  (same for all local rows)
3076273d9f13SBarry Smith .  d_nnz - array containing the number of block nonzeros in the various block rows
3077273d9f13SBarry Smith            of the in diagonal portion of the local (possibly different for each block
3078273d9f13SBarry Smith            row) or PETSC_NULL.  You must leave room for the diagonal entry even if it is zero.
3079273d9f13SBarry Smith .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
3080273d9f13SBarry Smith            submatrix (same for all local rows).
3081273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various block rows of the
3082273d9f13SBarry Smith            off-diagonal portion of the local submatrix (possibly different for
3083273d9f13SBarry Smith            each block row) or PETSC_NULL.
3084273d9f13SBarry Smith 
308549a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
3086273d9f13SBarry Smith 
3087273d9f13SBarry Smith    Options Database Keys:
30888c07d4e3SBarry Smith +   -mat_block_size - size of the blocks to use
30898c07d4e3SBarry Smith -   -mat_use_hash_table <fact>
3090273d9f13SBarry Smith 
3091273d9f13SBarry Smith    Notes:
3092273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one processor
3093273d9f13SBarry Smith    than it must be used on all processors that share the object for that argument.
3094273d9f13SBarry Smith 
3095273d9f13SBarry Smith    Storage Information:
3096273d9f13SBarry Smith    For a square global matrix we define each processor's diagonal portion
3097273d9f13SBarry Smith    to be its local rows and the corresponding columns (a square submatrix);
3098273d9f13SBarry Smith    each processor's off-diagonal portion encompasses the remainder of the
3099273d9f13SBarry Smith    local matrix (a rectangular submatrix).
3100273d9f13SBarry Smith 
3101273d9f13SBarry Smith    The user can specify preallocated storage for the diagonal part of
3102273d9f13SBarry Smith    the local submatrix with either d_nz or d_nnz (not both).  Set
3103273d9f13SBarry Smith    d_nz=PETSC_DEFAULT and d_nnz=PETSC_NULL for PETSc to control dynamic
3104273d9f13SBarry Smith    memory allocation.  Likewise, specify preallocated storage for the
3105273d9f13SBarry Smith    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
3106273d9f13SBarry Smith 
3107273d9f13SBarry Smith    Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
3108273d9f13SBarry Smith    the figure below we depict these three local rows and all columns (0-11).
3109273d9f13SBarry Smith 
3110273d9f13SBarry Smith .vb
3111273d9f13SBarry Smith            0 1 2 3 4 5 6 7 8 9 10 11
3112273d9f13SBarry Smith           -------------------
3113273d9f13SBarry Smith    row 3  |  o o o d d d o o o o o o
3114273d9f13SBarry Smith    row 4  |  o o o d d d o o o o o o
3115273d9f13SBarry Smith    row 5  |  o o o d d d o o o o o o
3116273d9f13SBarry Smith           -------------------
3117273d9f13SBarry Smith .ve
3118273d9f13SBarry Smith 
3119273d9f13SBarry Smith    Thus, any entries in the d locations are stored in the d (diagonal)
3120273d9f13SBarry Smith    submatrix, and any entries in the o locations are stored in the
3121273d9f13SBarry Smith    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
3122273d9f13SBarry Smith    stored simply in the MATSEQBAIJ format for compressed row storage.
3123273d9f13SBarry Smith 
3124273d9f13SBarry Smith    Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3125273d9f13SBarry Smith    and o_nz should indicate the number of block nonzeros per row in the o matrix.
3126273d9f13SBarry Smith    In general, for PDE problems in which most nonzeros are near the diagonal,
3127273d9f13SBarry Smith    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
3128273d9f13SBarry Smith    or you will get TERRIBLE performance; see the users' manual chapter on
3129273d9f13SBarry Smith    matrices.
3130273d9f13SBarry Smith 
3131aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3132aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3133aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3134aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3135aa95bbe8SBarry Smith 
3136273d9f13SBarry Smith    Level: intermediate
3137273d9f13SBarry Smith 
3138273d9f13SBarry Smith .keywords: matrix, block, aij, compressed row, sparse, parallel
3139273d9f13SBarry Smith 
3140aac34f13SBarry Smith .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateMPIBAIJ(), MatMPIBAIJSetPreallocationCSR()
3141273d9f13SBarry Smith @*/
3142be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3143273d9f13SBarry Smith {
3144b24ad042SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,PetscInt,const PetscInt[],PetscInt,const PetscInt[]);
3145273d9f13SBarry Smith 
3146273d9f13SBarry Smith   PetscFunctionBegin;
3147a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIBAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
3148a23d5eceSKris Buschelman   if (f) {
3149a23d5eceSKris Buschelman     ierr = (*f)(B,bs,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
3150273d9f13SBarry Smith   }
3151273d9f13SBarry Smith   PetscFunctionReturn(0);
3152273d9f13SBarry Smith }
3153273d9f13SBarry Smith 
31544a2ae208SSatish Balay #undef __FUNCT__
31554a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIBAIJ"
315679bdfe76SSatish Balay /*@C
315779bdfe76SSatish Balay    MatCreateMPIBAIJ - Creates a sparse parallel matrix in block AIJ format
315879bdfe76SSatish Balay    (block compressed row).  For good matrix assembly performance
315979bdfe76SSatish Balay    the user should preallocate the matrix storage by setting the parameters
316079bdfe76SSatish Balay    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
316179bdfe76SSatish Balay    performance can be increased by more than a factor of 50.
316279bdfe76SSatish Balay 
3163db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3164db81eaa0SLois Curfman McInnes 
316579bdfe76SSatish Balay    Input Parameters:
3166db81eaa0SLois Curfman McInnes +  comm - MPI communicator
316779bdfe76SSatish Balay .  bs   - size of blockk
316879bdfe76SSatish Balay .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
316992e8d321SLois Curfman McInnes            This value should be the same as the local size used in creating the
317092e8d321SLois Curfman McInnes            y vector for the matrix-vector product y = Ax.
317192e8d321SLois Curfman McInnes .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
317292e8d321SLois Curfman McInnes            This value should be the same as the local size used in creating the
317392e8d321SLois Curfman McInnes            x vector for the matrix-vector product y = Ax.
3174be79a94dSBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3175be79a94dSBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
317647a75d0bSBarry Smith .  d_nz  - number of nonzero blocks per block row in diagonal portion of local
317779bdfe76SSatish Balay            submatrix  (same for all local rows)
317847a75d0bSBarry Smith .  d_nnz - array containing the number of nonzero blocks in the various block rows
317992e8d321SLois Curfman McInnes            of the in diagonal portion of the local (possibly different for each block
3180db81eaa0SLois Curfman McInnes            row) or PETSC_NULL.  You must leave room for the diagonal entry even if it is zero.
318147a75d0bSBarry Smith .  o_nz  - number of nonzero blocks per block row in the off-diagonal portion of local
318279bdfe76SSatish Balay            submatrix (same for all local rows).
318347a75d0bSBarry Smith -  o_nnz - array containing the number of nonzero blocks in the various block rows of the
318492e8d321SLois Curfman McInnes            off-diagonal portion of the local submatrix (possibly different for
318592e8d321SLois Curfman McInnes            each block row) or PETSC_NULL.
318679bdfe76SSatish Balay 
318779bdfe76SSatish Balay    Output Parameter:
318879bdfe76SSatish Balay .  A - the matrix
318979bdfe76SSatish Balay 
3190db81eaa0SLois Curfman McInnes    Options Database Keys:
31918c07d4e3SBarry Smith +   -mat_block_size - size of the blocks to use
31928c07d4e3SBarry Smith -   -mat_use_hash_table <fact>
31933ffaccefSLois Curfman McInnes 
3194175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3195ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3196175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3197175b88e8SBarry Smith 
3198b259b22eSLois Curfman McInnes    Notes:
319949a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
320049a6f317SBarry Smith 
320147a75d0bSBarry Smith    A nonzero block is any block that as 1 or more nonzeros in it
320247a75d0bSBarry Smith 
320379bdfe76SSatish Balay    The user MUST specify either the local or global matrix dimensions
320479bdfe76SSatish Balay    (possibly both).
320579bdfe76SSatish Balay 
3206be79a94dSBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one processor
3207be79a94dSBarry Smith    than it must be used on all processors that share the object for that argument.
3208be79a94dSBarry Smith 
320979bdfe76SSatish Balay    Storage Information:
321079bdfe76SSatish Balay    For a square global matrix we define each processor's diagonal portion
321179bdfe76SSatish Balay    to be its local rows and the corresponding columns (a square submatrix);
321279bdfe76SSatish Balay    each processor's off-diagonal portion encompasses the remainder of the
321379bdfe76SSatish Balay    local matrix (a rectangular submatrix).
321479bdfe76SSatish Balay 
321579bdfe76SSatish Balay    The user can specify preallocated storage for the diagonal part of
321679bdfe76SSatish Balay    the local submatrix with either d_nz or d_nnz (not both).  Set
321779bdfe76SSatish Balay    d_nz=PETSC_DEFAULT and d_nnz=PETSC_NULL for PETSc to control dynamic
321879bdfe76SSatish Balay    memory allocation.  Likewise, specify preallocated storage for the
321979bdfe76SSatish Balay    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
322079bdfe76SSatish Balay 
322179bdfe76SSatish Balay    Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
322279bdfe76SSatish Balay    the figure below we depict these three local rows and all columns (0-11).
322379bdfe76SSatish Balay 
3224db81eaa0SLois Curfman McInnes .vb
3225db81eaa0SLois Curfman McInnes            0 1 2 3 4 5 6 7 8 9 10 11
3226db81eaa0SLois Curfman McInnes           -------------------
3227db81eaa0SLois Curfman McInnes    row 3  |  o o o d d d o o o o o o
3228db81eaa0SLois Curfman McInnes    row 4  |  o o o d d d o o o o o o
3229db81eaa0SLois Curfman McInnes    row 5  |  o o o d d d o o o o o o
3230db81eaa0SLois Curfman McInnes           -------------------
3231db81eaa0SLois Curfman McInnes .ve
323279bdfe76SSatish Balay 
323379bdfe76SSatish Balay    Thus, any entries in the d locations are stored in the d (diagonal)
323479bdfe76SSatish Balay    submatrix, and any entries in the o locations are stored in the
323579bdfe76SSatish Balay    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
323657b952d6SSatish Balay    stored simply in the MATSEQBAIJ format for compressed row storage.
323779bdfe76SSatish Balay 
3238d64ed03dSBarry Smith    Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3239d64ed03dSBarry Smith    and o_nz should indicate the number of block nonzeros per row in the o matrix.
324079bdfe76SSatish Balay    In general, for PDE problems in which most nonzeros are near the diagonal,
324192e8d321SLois Curfman McInnes    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
324292e8d321SLois Curfman McInnes    or you will get TERRIBLE performance; see the users' manual chapter on
32436da5968aSLois Curfman McInnes    matrices.
324479bdfe76SSatish Balay 
3245027ccd11SLois Curfman McInnes    Level: intermediate
3246027ccd11SLois Curfman McInnes 
324792e8d321SLois Curfman McInnes .keywords: matrix, block, aij, compressed row, sparse, parallel
324879bdfe76SSatish Balay 
3249aac34f13SBarry Smith .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateMPIBAIJ(), MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
325079bdfe76SSatish Balay @*/
3251be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMPIBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A)
325279bdfe76SSatish Balay {
32536849ba73SBarry Smith   PetscErrorCode ierr;
3254b24ad042SBarry Smith   PetscMPIInt    size;
325579bdfe76SSatish Balay 
3256d64ed03dSBarry Smith   PetscFunctionBegin;
3257f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3258f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr);
3259d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3260273d9f13SBarry Smith   if (size > 1) {
3261273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIBAIJ);CHKERRQ(ierr);
3262273d9f13SBarry Smith     ierr = MatMPIBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
3263273d9f13SBarry Smith   } else {
3264273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQBAIJ);CHKERRQ(ierr);
3265273d9f13SBarry Smith     ierr = MatSeqBAIJSetPreallocation(*A,bs,d_nz,d_nnz);CHKERRQ(ierr);
32663914022bSBarry Smith   }
32673a40ed3dSBarry Smith   PetscFunctionReturn(0);
326879bdfe76SSatish Balay }
3269026e39d0SSatish Balay 
32704a2ae208SSatish Balay #undef __FUNCT__
32714a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIBAIJ"
32726849ba73SBarry Smith static PetscErrorCode MatDuplicate_MPIBAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
32730ac07820SSatish Balay {
32740ac07820SSatish Balay   Mat            mat;
32750ac07820SSatish Balay   Mat_MPIBAIJ    *a,*oldmat = (Mat_MPIBAIJ*)matin->data;
3276dfbe8321SBarry Smith   PetscErrorCode ierr;
3277b24ad042SBarry Smith   PetscInt       len=0;
32780ac07820SSatish Balay 
3279d64ed03dSBarry Smith   PetscFunctionBegin;
32800ac07820SSatish Balay   *newmat       = 0;
32817adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)matin)->comm,&mat);CHKERRQ(ierr);
3282d0f46423SBarry Smith   ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr);
32837adad957SLisandro Dalcin   ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr);
32841d5dac46SHong Zhang   ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
32857fff6886SHong Zhang 
3286d5f3da31SBarry Smith   mat->factortype   = matin->factortype;
3287273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
32880ac07820SSatish Balay   mat->assembled    = PETSC_TRUE;
32897fff6886SHong Zhang   mat->insertmode   = NOT_SET_VALUES;
32907fff6886SHong Zhang 
3291273d9f13SBarry Smith   a      = (Mat_MPIBAIJ*)mat->data;
3292d0f46423SBarry Smith   mat->rmap->bs  = matin->rmap->bs;
32930ac07820SSatish Balay   a->bs2   = oldmat->bs2;
32940ac07820SSatish Balay   a->mbs   = oldmat->mbs;
32950ac07820SSatish Balay   a->nbs   = oldmat->nbs;
32960ac07820SSatish Balay   a->Mbs   = oldmat->Mbs;
32970ac07820SSatish Balay   a->Nbs   = oldmat->Nbs;
32980ac07820SSatish Balay 
329926283091SBarry Smith   ierr = PetscLayoutCopy(matin->rmap,&mat->rmap);CHKERRQ(ierr);
330026283091SBarry Smith   ierr = PetscLayoutCopy(matin->cmap,&mat->cmap);CHKERRQ(ierr);
3301899cda47SBarry Smith 
33020ac07820SSatish Balay   a->size         = oldmat->size;
33030ac07820SSatish Balay   a->rank         = oldmat->rank;
3304aef5e8e0SSatish Balay   a->donotstash   = oldmat->donotstash;
3305aef5e8e0SSatish Balay   a->roworiented  = oldmat->roworiented;
3306aef5e8e0SSatish Balay   a->rowindices   = 0;
33070ac07820SSatish Balay   a->rowvalues    = 0;
33080ac07820SSatish Balay   a->getrowactive = PETSC_FALSE;
330930793edcSSatish Balay   a->barray       = 0;
3310899cda47SBarry Smith   a->rstartbs     = oldmat->rstartbs;
3311899cda47SBarry Smith   a->rendbs       = oldmat->rendbs;
3312899cda47SBarry Smith   a->cstartbs     = oldmat->cstartbs;
3313899cda47SBarry Smith   a->cendbs       = oldmat->cendbs;
33140ac07820SSatish Balay 
3315133cdb44SSatish Balay   /* hash table stuff */
3316133cdb44SSatish Balay   a->ht           = 0;
3317133cdb44SSatish Balay   a->hd           = 0;
3318133cdb44SSatish Balay   a->ht_size      = 0;
3319133cdb44SSatish Balay   a->ht_flag      = oldmat->ht_flag;
332025fdafccSSatish Balay   a->ht_fact      = oldmat->ht_fact;
3321133cdb44SSatish Balay   a->ht_total_ct  = 0;
3322133cdb44SSatish Balay   a->ht_insert_ct = 0;
3323133cdb44SSatish Balay 
3324899cda47SBarry Smith   ierr = PetscMemcpy(a->rangebs,oldmat->rangebs,(a->size+1)*sizeof(PetscInt));CHKERRQ(ierr);
33250ac07820SSatish Balay   if (oldmat->colmap) {
3326aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
33270f5bd95cSBarry Smith   ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
332848e59246SSatish Balay #else
3329b24ad042SBarry Smith   ierr = PetscMalloc((a->Nbs)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr);
333052e6d16bSBarry Smith   ierr = PetscLogObjectMemory(mat,(a->Nbs)*sizeof(PetscInt));CHKERRQ(ierr);
3331b24ad042SBarry Smith   ierr = PetscMemcpy(a->colmap,oldmat->colmap,(a->Nbs)*sizeof(PetscInt));CHKERRQ(ierr);
333248e59246SSatish Balay #endif
33330ac07820SSatish Balay   } else a->colmap = 0;
33344beb1cfeSHong Zhang 
33350ac07820SSatish Balay   if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) {
3336b24ad042SBarry Smith     ierr = PetscMalloc(len*sizeof(PetscInt),&a->garray);CHKERRQ(ierr);
333752e6d16bSBarry Smith     ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr);
3338b24ad042SBarry Smith     ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr);
33390ac07820SSatish Balay   } else a->garray = 0;
33400ac07820SSatish Balay 
3341533163c2SBarry Smith   ierr = MatStashCreate_Private(((PetscObject)matin)->comm,matin->rmap->bs,&mat->bstash);CHKERRQ(ierr);
33420ac07820SSatish Balay   ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
334352e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr);
33440ac07820SSatish Balay   ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
334552e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr);
33467fff6886SHong Zhang 
33472e8a6d31SBarry Smith   ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
334852e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr);
33492e8a6d31SBarry Smith   ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
335052e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr);
33517adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr);
33520ac07820SSatish Balay   *newmat = mat;
33534beb1cfeSHong Zhang 
33543a40ed3dSBarry Smith   PetscFunctionReturn(0);
33550ac07820SSatish Balay }
335657b952d6SSatish Balay 
33574a2ae208SSatish Balay #undef __FUNCT__
33584a2ae208SSatish Balay #define __FUNCT__ "MatLoad_MPIBAIJ"
3359a313700dSBarry Smith PetscErrorCode MatLoad_MPIBAIJ(PetscViewer viewer, const MatType type,Mat *newmat)
336057b952d6SSatish Balay {
336157b952d6SSatish Balay   Mat            A;
33626849ba73SBarry Smith   PetscErrorCode ierr;
3363b24ad042SBarry Smith   int            fd;
3364b24ad042SBarry Smith   PetscInt       i,nz,j,rstart,rend;
336587828ca2SBarry Smith   PetscScalar    *vals,*buf;
336657b952d6SSatish Balay   MPI_Comm       comm = ((PetscObject)viewer)->comm;
336757b952d6SSatish Balay   MPI_Status     status;
3368b24ad042SBarry Smith   PetscMPIInt    rank,size,maxnz;
3369b24ad042SBarry Smith   PetscInt       header[4],*rowlengths = 0,M,N,m,*rowners,*cols;
3370910ba992SMatthew Knepley   PetscInt       *locrowlens = PETSC_NULL,*procsnz = PETSC_NULL,*browners = PETSC_NULL;
3371167e7480SBarry Smith   PetscInt       jj,*mycols,*ibuf,bs=1,Mbs,mbs,extra_rows,mmax;
3372dc231df0SBarry Smith   PetscMPIInt    tag = ((PetscObject)viewer)->tag;
3373910ba992SMatthew Knepley   PetscInt       *dlens = PETSC_NULL,*odlens = PETSC_NULL,*mask = PETSC_NULL,*masked1 = PETSC_NULL,*masked2 = PETSC_NULL,rowcount,odcount;
3374dc231df0SBarry Smith   PetscInt       dcount,kmax,k,nzcount,tmp,mend;
337557b952d6SSatish Balay 
3376d64ed03dSBarry Smith   PetscFunctionBegin;
337777925062SSatish Balay   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading MPIBAIJ matrix 2","Mat");CHKERRQ(ierr);
33788c07d4e3SBarry Smith     ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
33798c07d4e3SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
338057b952d6SSatish Balay 
3381d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3382d132466eSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
338357b952d6SSatish Balay   if (!rank) {
3384b0a32e0cSBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
3385e5638eb3SBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
3386e32f2f54SBarry Smith     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
33876c5fab8fSBarry Smith   }
3388d64ed03dSBarry Smith 
3389b24ad042SBarry Smith   ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr);
339057b952d6SSatish Balay   M = header[1]; N = header[2];
339157b952d6SSatish Balay 
3392e7e72b3dSBarry Smith   if (M != N) SETERRQ(((PetscObject)viewer)->comm,PETSC_ERR_SUP,"Can only do square matrices");
339357b952d6SSatish Balay 
339457b952d6SSatish Balay   /*
339557b952d6SSatish Balay      This code adds extra rows to make sure the number of rows is
339657b952d6SSatish Balay      divisible by the blocksize
339757b952d6SSatish Balay   */
339857b952d6SSatish Balay   Mbs        = M/bs;
3399dc231df0SBarry Smith   extra_rows = bs - M + bs*Mbs;
340057b952d6SSatish Balay   if (extra_rows == bs) extra_rows = 0;
340157b952d6SSatish Balay   else                  Mbs++;
340257b952d6SSatish Balay   if (extra_rows && !rank) {
34031e2582c4SBarry Smith     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
340457b952d6SSatish Balay   }
3405537820f0SBarry Smith 
340657b952d6SSatish Balay   /* determine ownership of all rows */
340757b952d6SSatish Balay   mbs        = Mbs/size + ((Mbs % size) > rank);
340857b952d6SSatish Balay   m          = mbs*bs;
3409dc231df0SBarry Smith   ierr       = PetscMalloc2(size+1,PetscInt,&rowners,size+1,PetscInt,&browners);CHKERRQ(ierr);
3410b24ad042SBarry Smith   ierr       = MPI_Allgather(&mbs,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
3411167e7480SBarry Smith 
3412167e7480SBarry Smith   /* process 0 needs enough room for process with most rows */
3413167e7480SBarry Smith   if (!rank) {
3414167e7480SBarry Smith     mmax = rowners[1];
3415167e7480SBarry Smith     for (i=2; i<size; i++) {
3416167e7480SBarry Smith       mmax = PetscMax(mmax,rowners[i]);
3417167e7480SBarry Smith     }
3418ca02efcfSSatish Balay     mmax*=bs;
3419167e7480SBarry Smith   } else mmax = m;
3420167e7480SBarry Smith 
342157b952d6SSatish Balay   rowners[0] = 0;
3422cee3aa6bSSatish Balay   for (i=2; i<=size; i++)  rowners[i] += rowners[i-1];
3423cee3aa6bSSatish Balay   for (i=0; i<=size;  i++) browners[i] = rowners[i]*bs;
342457b952d6SSatish Balay   rstart = rowners[rank];
342557b952d6SSatish Balay   rend   = rowners[rank+1];
342657b952d6SSatish Balay 
342757b952d6SSatish Balay   /* distribute row lengths to all processors */
342819c38ff2SBarry Smith   ierr = PetscMalloc((mmax+1)*sizeof(PetscInt),&locrowlens);CHKERRQ(ierr);
342957b952d6SSatish Balay   if (!rank) {
3430dc231df0SBarry Smith     mend = m;
3431dc231df0SBarry Smith     if (size == 1) mend = mend - extra_rows;
3432dc231df0SBarry Smith     ierr = PetscBinaryRead(fd,locrowlens,mend,PETSC_INT);CHKERRQ(ierr);
3433dc231df0SBarry Smith     for (j=mend; j<m; j++) locrowlens[j] = 1;
3434dc231df0SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
3435b24ad042SBarry Smith     ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr);
3436b24ad042SBarry Smith     ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr);
3437dc231df0SBarry Smith     for (j=0; j<m; j++) {
3438dc231df0SBarry Smith       procsnz[0] += locrowlens[j];
3439dc231df0SBarry Smith     }
3440dc231df0SBarry Smith     for (i=1; i<size; i++) {
3441dc231df0SBarry Smith       mend = browners[i+1] - browners[i];
3442dc231df0SBarry Smith       if (i == size-1) mend = mend - extra_rows;
3443dc231df0SBarry Smith       ierr = PetscBinaryRead(fd,rowlengths,mend,PETSC_INT);CHKERRQ(ierr);
3444dc231df0SBarry Smith       for (j=mend; j<browners[i+1] - browners[i]; j++) rowlengths[j] = 1;
3445dc231df0SBarry Smith       /* calculate the number of nonzeros on each processor */
3446dc231df0SBarry Smith       for (j=0; j<browners[i+1]-browners[i]; j++) {
344757b952d6SSatish Balay         procsnz[i] += rowlengths[j];
344857b952d6SSatish Balay       }
3449dc231df0SBarry Smith       ierr = MPI_Send(rowlengths,browners[i+1]-browners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
345057b952d6SSatish Balay     }
3451606d414cSSatish Balay     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3452dc231df0SBarry Smith   } else {
3453dc231df0SBarry Smith     ierr = MPI_Recv(locrowlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
3454dc231df0SBarry Smith   }
345557b952d6SSatish Balay 
3456dc231df0SBarry Smith   if (!rank) {
345757b952d6SSatish Balay     /* determine max buffer needed and allocate it */
34588a8e0b3aSBarry Smith     maxnz = procsnz[0];
3459cdc0ba36SBarry Smith     for (i=1; i<size; i++) {
346057b952d6SSatish Balay       maxnz = PetscMax(maxnz,procsnz[i]);
346157b952d6SSatish Balay     }
3462b24ad042SBarry Smith     ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr);
346357b952d6SSatish Balay 
346457b952d6SSatish Balay     /* read in my part of the matrix column indices  */
346557b952d6SSatish Balay     nz     = procsnz[0];
346619c38ff2SBarry Smith     ierr   = PetscMalloc((nz+1)*sizeof(PetscInt),&ibuf);CHKERRQ(ierr);
346757b952d6SSatish Balay     mycols = ibuf;
3468cee3aa6bSSatish Balay     if (size == 1)  nz -= extra_rows;
3469e5638eb3SBarry Smith     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
3470cee3aa6bSSatish Balay     if (size == 1)  for (i=0; i< extra_rows; i++) { mycols[nz+i] = M+i; }
3471cee3aa6bSSatish Balay 
347257b952d6SSatish Balay     /* read in every ones (except the last) and ship off */
347357b952d6SSatish Balay     for (i=1; i<size-1; i++) {
347457b952d6SSatish Balay       nz   = procsnz[i];
3475e5638eb3SBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
3476b24ad042SBarry Smith       ierr = MPI_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
347757b952d6SSatish Balay     }
347857b952d6SSatish Balay     /* read in the stuff for the last proc */
347957b952d6SSatish Balay     if (size != 1) {
348057b952d6SSatish Balay       nz   = procsnz[size-1] - extra_rows;  /* the extra rows are not on the disk */
3481e5638eb3SBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
348257b952d6SSatish Balay       for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
3483b24ad042SBarry Smith       ierr = MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);CHKERRQ(ierr);
348457b952d6SSatish Balay     }
3485606d414cSSatish Balay     ierr = PetscFree(cols);CHKERRQ(ierr);
3486d64ed03dSBarry Smith   } else {
348757b952d6SSatish Balay     /* determine buffer space needed for message */
348857b952d6SSatish Balay     nz = 0;
348957b952d6SSatish Balay     for (i=0; i<m; i++) {
349057b952d6SSatish Balay       nz += locrowlens[i];
349157b952d6SSatish Balay     }
349219c38ff2SBarry Smith     ierr   = PetscMalloc((nz+1)*sizeof(PetscInt),&ibuf);CHKERRQ(ierr);
349357b952d6SSatish Balay     mycols = ibuf;
349457b952d6SSatish Balay     /* receive message of column indices*/
3495b24ad042SBarry Smith     ierr = MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
3496b24ad042SBarry Smith     ierr = MPI_Get_count(&status,MPIU_INT,&maxnz);CHKERRQ(ierr);
3497e32f2f54SBarry Smith     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
349857b952d6SSatish Balay   }
349957b952d6SSatish Balay 
350057b952d6SSatish Balay   /* loop over local rows, determining number of off diagonal entries */
3501dc231df0SBarry Smith   ierr     = PetscMalloc2(rend-rstart,PetscInt,&dlens,rend-rstart,PetscInt,&odlens);CHKERRQ(ierr);
3502dc231df0SBarry Smith   ierr     = PetscMalloc3(Mbs,PetscInt,&mask,Mbs,PetscInt,&masked1,Mbs,PetscInt,&masked2);CHKERRQ(ierr);
3503dc231df0SBarry Smith   ierr     = PetscMemzero(mask,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
3504dc231df0SBarry Smith   ierr     = PetscMemzero(masked1,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
3505dc231df0SBarry Smith   ierr     = PetscMemzero(masked2,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
350657b952d6SSatish Balay   rowcount = 0; nzcount = 0;
350757b952d6SSatish Balay   for (i=0; i<mbs; i++) {
350857b952d6SSatish Balay     dcount  = 0;
350957b952d6SSatish Balay     odcount = 0;
351057b952d6SSatish Balay     for (j=0; j<bs; j++) {
351157b952d6SSatish Balay       kmax = locrowlens[rowcount];
351257b952d6SSatish Balay       for (k=0; k<kmax; k++) {
351357b952d6SSatish Balay         tmp = mycols[nzcount++]/bs;
351457b952d6SSatish Balay         if (!mask[tmp]) {
351557b952d6SSatish Balay           mask[tmp] = 1;
351657b952d6SSatish Balay           if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp;
351757b952d6SSatish Balay           else masked1[dcount++] = tmp;
351857b952d6SSatish Balay         }
351957b952d6SSatish Balay       }
352057b952d6SSatish Balay       rowcount++;
352157b952d6SSatish Balay     }
3522cee3aa6bSSatish Balay 
352357b952d6SSatish Balay     dlens[i]  = dcount;
352457b952d6SSatish Balay     odlens[i] = odcount;
3525cee3aa6bSSatish Balay 
352657b952d6SSatish Balay     /* zero out the mask elements we set */
352757b952d6SSatish Balay     for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
352857b952d6SSatish Balay     for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
352957b952d6SSatish Balay   }
3530cee3aa6bSSatish Balay 
353157b952d6SSatish Balay   /* create our matrix */
3532f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&A);CHKERRQ(ierr);
3533f69a0ea3SMatthew Knepley   ierr = MatSetSizes(A,m,m,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
3534cb9801acSJed Brown   ierr = MatSetType(A,type);CHKERRQ(ierr);
353578ae41b4SKris Buschelman   ierr = MatMPIBAIJSetPreallocation(A,bs,0,dlens,0,odlens);CHKERRQ(ierr);
353678ae41b4SKris Buschelman 
353757b952d6SSatish Balay   if (!rank) {
353819c38ff2SBarry Smith     ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&buf);CHKERRQ(ierr);
353957b952d6SSatish Balay     /* read in my part of the matrix numerical values  */
354057b952d6SSatish Balay     nz = procsnz[0];
354157b952d6SSatish Balay     vals = buf;
3542cee3aa6bSSatish Balay     mycols = ibuf;
3543cee3aa6bSSatish Balay     if (size == 1)  nz -= extra_rows;
3544e5638eb3SBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
3545cee3aa6bSSatish Balay     if (size == 1)  for (i=0; i< extra_rows; i++) { vals[nz+i] = 1.0; }
3546537820f0SBarry Smith 
354757b952d6SSatish Balay     /* insert into matrix */
354857b952d6SSatish Balay     jj      = rstart*bs;
354957b952d6SSatish Balay     for (i=0; i<m; i++) {
3550dc231df0SBarry Smith       ierr = MatSetValues_MPIBAIJ(A,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);CHKERRQ(ierr);
355157b952d6SSatish Balay       mycols += locrowlens[i];
355257b952d6SSatish Balay       vals   += locrowlens[i];
355357b952d6SSatish Balay       jj++;
355457b952d6SSatish Balay     }
355557b952d6SSatish Balay     /* read in other processors (except the last one) and ship out */
355657b952d6SSatish Balay     for (i=1; i<size-1; i++) {
355757b952d6SSatish Balay       nz   = procsnz[i];
355857b952d6SSatish Balay       vals = buf;
3559e5638eb3SBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
35607adad957SLisandro Dalcin       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)A)->tag,comm);CHKERRQ(ierr);
356157b952d6SSatish Balay     }
356257b952d6SSatish Balay     /* the last proc */
356357b952d6SSatish Balay     if (size != 1){
356457b952d6SSatish Balay       nz   = procsnz[i] - extra_rows;
3565cee3aa6bSSatish Balay       vals = buf;
3566e5638eb3SBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
356757b952d6SSatish Balay       for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
35687adad957SLisandro Dalcin       ierr = MPI_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,((PetscObject)A)->tag,comm);CHKERRQ(ierr);
356957b952d6SSatish Balay     }
3570606d414cSSatish Balay     ierr = PetscFree(procsnz);CHKERRQ(ierr);
3571d64ed03dSBarry Smith   } else {
357257b952d6SSatish Balay     /* receive numeric values */
357319c38ff2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&buf);CHKERRQ(ierr);
357457b952d6SSatish Balay 
357557b952d6SSatish Balay     /* receive message of values*/
357657b952d6SSatish Balay     vals   = buf;
3577cee3aa6bSSatish Balay     mycols = ibuf;
35787adad957SLisandro Dalcin     ierr   = MPI_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)A)->tag,comm,&status);CHKERRQ(ierr);
3579ca161407SBarry Smith     ierr   = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
3580e32f2f54SBarry Smith     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
358157b952d6SSatish Balay 
358257b952d6SSatish Balay     /* insert into matrix */
358357b952d6SSatish Balay     jj      = rstart*bs;
3584cee3aa6bSSatish Balay     for (i=0; i<m; i++) {
3585dc231df0SBarry Smith       ierr    = MatSetValues_MPIBAIJ(A,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);CHKERRQ(ierr);
358657b952d6SSatish Balay       mycols += locrowlens[i];
358757b952d6SSatish Balay       vals   += locrowlens[i];
358857b952d6SSatish Balay       jj++;
358957b952d6SSatish Balay     }
359057b952d6SSatish Balay   }
3591606d414cSSatish Balay   ierr = PetscFree(locrowlens);CHKERRQ(ierr);
3592606d414cSSatish Balay   ierr = PetscFree(buf);CHKERRQ(ierr);
3593606d414cSSatish Balay   ierr = PetscFree(ibuf);CHKERRQ(ierr);
3594dc231df0SBarry Smith   ierr = PetscFree2(rowners,browners);CHKERRQ(ierr);
3595dc231df0SBarry Smith   ierr = PetscFree2(dlens,odlens);CHKERRQ(ierr);
3596dc231df0SBarry Smith   ierr = PetscFree3(mask,masked1,masked2);CHKERRQ(ierr);
35976d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35986d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
359978ae41b4SKris Buschelman 
360078ae41b4SKris Buschelman   *newmat = A;
36013a40ed3dSBarry Smith   PetscFunctionReturn(0);
360257b952d6SSatish Balay }
360357b952d6SSatish Balay 
36044a2ae208SSatish Balay #undef __FUNCT__
3605*4683f7a4SShri Abhyankar #define __FUNCT__ "MatLoadnew_MPIBAIJ"
3606*4683f7a4SShri Abhyankar PetscErrorCode MatLoadnew_MPIBAIJ(PetscViewer viewer, Mat newmat)
3607*4683f7a4SShri Abhyankar {
3608*4683f7a4SShri Abhyankar   PetscErrorCode ierr;
3609*4683f7a4SShri Abhyankar   int            fd;
3610*4683f7a4SShri Abhyankar   PetscInt       i,nz,j,rstart,rend;
3611*4683f7a4SShri Abhyankar   PetscScalar    *vals,*buf;
3612*4683f7a4SShri Abhyankar   MPI_Comm       comm = ((PetscObject)viewer)->comm;
3613*4683f7a4SShri Abhyankar   MPI_Status     status;
3614*4683f7a4SShri Abhyankar   PetscMPIInt    rank,size,maxnz;
3615*4683f7a4SShri Abhyankar   PetscInt       header[4],*rowlengths = 0,M,N,m,*rowners,*cols;
3616*4683f7a4SShri Abhyankar   PetscInt       *locrowlens = PETSC_NULL,*procsnz = PETSC_NULL,*browners = PETSC_NULL;
3617*4683f7a4SShri Abhyankar   PetscInt       jj,*mycols,*ibuf,bs=1,Mbs,mbs,extra_rows,mmax;
3618*4683f7a4SShri Abhyankar   PetscMPIInt    tag = ((PetscObject)viewer)->tag;
3619*4683f7a4SShri Abhyankar   PetscInt       *dlens = PETSC_NULL,*odlens = PETSC_NULL,*mask = PETSC_NULL,*masked1 = PETSC_NULL,*masked2 = PETSC_NULL,rowcount,odcount;
3620*4683f7a4SShri Abhyankar   PetscInt       dcount,kmax,k,nzcount,tmp,mend,sizesset=1,grows,gcols;
3621*4683f7a4SShri Abhyankar 
3622*4683f7a4SShri Abhyankar   PetscFunctionBegin;
3623*4683f7a4SShri Abhyankar   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading MPIBAIJ matrix 2","Mat");CHKERRQ(ierr);
3624*4683f7a4SShri Abhyankar     ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
3625*4683f7a4SShri Abhyankar   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3626*4683f7a4SShri Abhyankar 
3627*4683f7a4SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3628*4683f7a4SShri Abhyankar   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
3629*4683f7a4SShri Abhyankar   if (!rank) {
3630*4683f7a4SShri Abhyankar     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
3631*4683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
3632*4683f7a4SShri Abhyankar     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
3633*4683f7a4SShri Abhyankar   }
3634*4683f7a4SShri Abhyankar 
3635*4683f7a4SShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) sizesset = 0;
3636*4683f7a4SShri Abhyankar 
3637*4683f7a4SShri Abhyankar   ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr);
3638*4683f7a4SShri Abhyankar   M = header[1]; N = header[2];
3639*4683f7a4SShri Abhyankar 
3640*4683f7a4SShri Abhyankar   /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */
3641*4683f7a4SShri Abhyankar   if (sizesset && newmat->rmap->N < 0) newmat->rmap->N = M;
3642*4683f7a4SShri Abhyankar   if (sizesset && newmat->cmap->N < 0) newmat->cmap->N = N;
3643*4683f7a4SShri Abhyankar 
3644*4683f7a4SShri Abhyankar   /* If global sizes are set, check if they are consistent with that given in the file */
3645*4683f7a4SShri Abhyankar   if (sizesset) {
3646*4683f7a4SShri Abhyankar     ierr = MatGetSize(newmat,&grows,&gcols);CHKERRQ(ierr);
3647*4683f7a4SShri Abhyankar   }
3648*4683f7a4SShri Abhyankar   if (sizesset && newmat->rmap->N != grows) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Incostintent # of rows:Matrix in file has (%d) and input matrix has (%d)",M,grows);
3649*4683f7a4SShri Abhyankar   if (sizesset && newmat->cmap->N != gcols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Incostintent # of cols:Matrix in file has (%d) and input matrix has (%d)",N,gcols);
3650*4683f7a4SShri Abhyankar 
3651*4683f7a4SShri Abhyankar   if (M != N) SETERRQ(((PetscObject)viewer)->comm,PETSC_ERR_SUP,"Can only do square matrices");
3652*4683f7a4SShri Abhyankar 
3653*4683f7a4SShri Abhyankar   /*
3654*4683f7a4SShri Abhyankar      This code adds extra rows to make sure the number of rows is
3655*4683f7a4SShri Abhyankar      divisible by the blocksize
3656*4683f7a4SShri Abhyankar   */
3657*4683f7a4SShri Abhyankar   Mbs        = M/bs;
3658*4683f7a4SShri Abhyankar   extra_rows = bs - M + bs*Mbs;
3659*4683f7a4SShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
3660*4683f7a4SShri Abhyankar   else                  Mbs++;
3661*4683f7a4SShri Abhyankar   if (extra_rows && !rank) {
3662*4683f7a4SShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
3663*4683f7a4SShri Abhyankar   }
3664*4683f7a4SShri Abhyankar 
3665*4683f7a4SShri Abhyankar   /* determine ownership of all rows */
3666*4683f7a4SShri Abhyankar   if (newmat->rmap->n < 0) { /* PETSC_DECIDE */
3667*4683f7a4SShri Abhyankar     mbs        = Mbs/size + ((Mbs % size) > rank);
3668*4683f7a4SShri Abhyankar     m          = mbs*bs;
3669*4683f7a4SShri Abhyankar   } else { /* User set */
3670*4683f7a4SShri Abhyankar     m          = newmat->rmap->n;
3671*4683f7a4SShri Abhyankar     mbs        = m/bs;
3672*4683f7a4SShri Abhyankar   }
3673*4683f7a4SShri Abhyankar   ierr       = PetscMalloc2(size+1,PetscInt,&rowners,size+1,PetscInt,&browners);CHKERRQ(ierr);
3674*4683f7a4SShri Abhyankar   ierr       = MPI_Allgather(&mbs,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
3675*4683f7a4SShri Abhyankar 
3676*4683f7a4SShri Abhyankar   /* process 0 needs enough room for process with most rows */
3677*4683f7a4SShri Abhyankar   if (!rank) {
3678*4683f7a4SShri Abhyankar     mmax = rowners[1];
3679*4683f7a4SShri Abhyankar     for (i=2; i<size; i++) {
3680*4683f7a4SShri Abhyankar       mmax = PetscMax(mmax,rowners[i]);
3681*4683f7a4SShri Abhyankar     }
3682*4683f7a4SShri Abhyankar     mmax*=bs;
3683*4683f7a4SShri Abhyankar   } else mmax = m;
3684*4683f7a4SShri Abhyankar 
3685*4683f7a4SShri Abhyankar   rowners[0] = 0;
3686*4683f7a4SShri Abhyankar   for (i=2; i<=size; i++)  rowners[i] += rowners[i-1];
3687*4683f7a4SShri Abhyankar   for (i=0; i<=size;  i++) browners[i] = rowners[i]*bs;
3688*4683f7a4SShri Abhyankar   rstart = rowners[rank];
3689*4683f7a4SShri Abhyankar   rend   = rowners[rank+1];
3690*4683f7a4SShri Abhyankar 
3691*4683f7a4SShri Abhyankar   /* distribute row lengths to all processors */
3692*4683f7a4SShri Abhyankar   ierr = PetscMalloc((mmax+1)*sizeof(PetscInt),&locrowlens);CHKERRQ(ierr);
3693*4683f7a4SShri Abhyankar   if (!rank) {
3694*4683f7a4SShri Abhyankar     mend = m;
3695*4683f7a4SShri Abhyankar     if (size == 1) mend = mend - extra_rows;
3696*4683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,locrowlens,mend,PETSC_INT);CHKERRQ(ierr);
3697*4683f7a4SShri Abhyankar     for (j=mend; j<m; j++) locrowlens[j] = 1;
3698*4683f7a4SShri Abhyankar     ierr = PetscMalloc(m*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
3699*4683f7a4SShri Abhyankar     ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr);
3700*4683f7a4SShri Abhyankar     ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr);
3701*4683f7a4SShri Abhyankar     for (j=0; j<m; j++) {
3702*4683f7a4SShri Abhyankar       procsnz[0] += locrowlens[j];
3703*4683f7a4SShri Abhyankar     }
3704*4683f7a4SShri Abhyankar     for (i=1; i<size; i++) {
3705*4683f7a4SShri Abhyankar       mend = browners[i+1] - browners[i];
3706*4683f7a4SShri Abhyankar       if (i == size-1) mend = mend - extra_rows;
3707*4683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,rowlengths,mend,PETSC_INT);CHKERRQ(ierr);
3708*4683f7a4SShri Abhyankar       for (j=mend; j<browners[i+1] - browners[i]; j++) rowlengths[j] = 1;
3709*4683f7a4SShri Abhyankar       /* calculate the number of nonzeros on each processor */
3710*4683f7a4SShri Abhyankar       for (j=0; j<browners[i+1]-browners[i]; j++) {
3711*4683f7a4SShri Abhyankar         procsnz[i] += rowlengths[j];
3712*4683f7a4SShri Abhyankar       }
3713*4683f7a4SShri Abhyankar       ierr = MPI_Send(rowlengths,browners[i+1]-browners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
3714*4683f7a4SShri Abhyankar     }
3715*4683f7a4SShri Abhyankar     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3716*4683f7a4SShri Abhyankar   } else {
3717*4683f7a4SShri Abhyankar     ierr = MPI_Recv(locrowlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
3718*4683f7a4SShri Abhyankar   }
3719*4683f7a4SShri Abhyankar 
3720*4683f7a4SShri Abhyankar   if (!rank) {
3721*4683f7a4SShri Abhyankar     /* determine max buffer needed and allocate it */
3722*4683f7a4SShri Abhyankar     maxnz = procsnz[0];
3723*4683f7a4SShri Abhyankar     for (i=1; i<size; i++) {
3724*4683f7a4SShri Abhyankar       maxnz = PetscMax(maxnz,procsnz[i]);
3725*4683f7a4SShri Abhyankar     }
3726*4683f7a4SShri Abhyankar     ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr);
3727*4683f7a4SShri Abhyankar 
3728*4683f7a4SShri Abhyankar     /* read in my part of the matrix column indices  */
3729*4683f7a4SShri Abhyankar     nz     = procsnz[0];
3730*4683f7a4SShri Abhyankar     ierr   = PetscMalloc((nz+1)*sizeof(PetscInt),&ibuf);CHKERRQ(ierr);
3731*4683f7a4SShri Abhyankar     mycols = ibuf;
3732*4683f7a4SShri Abhyankar     if (size == 1)  nz -= extra_rows;
3733*4683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
3734*4683f7a4SShri Abhyankar     if (size == 1)  for (i=0; i< extra_rows; i++) { mycols[nz+i] = M+i; }
3735*4683f7a4SShri Abhyankar 
3736*4683f7a4SShri Abhyankar     /* read in every ones (except the last) and ship off */
3737*4683f7a4SShri Abhyankar     for (i=1; i<size-1; i++) {
3738*4683f7a4SShri Abhyankar       nz   = procsnz[i];
3739*4683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
3740*4683f7a4SShri Abhyankar       ierr = MPI_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
3741*4683f7a4SShri Abhyankar     }
3742*4683f7a4SShri Abhyankar     /* read in the stuff for the last proc */
3743*4683f7a4SShri Abhyankar     if (size != 1) {
3744*4683f7a4SShri Abhyankar       nz   = procsnz[size-1] - extra_rows;  /* the extra rows are not on the disk */
3745*4683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
3746*4683f7a4SShri Abhyankar       for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
3747*4683f7a4SShri Abhyankar       ierr = MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);CHKERRQ(ierr);
3748*4683f7a4SShri Abhyankar     }
3749*4683f7a4SShri Abhyankar     ierr = PetscFree(cols);CHKERRQ(ierr);
3750*4683f7a4SShri Abhyankar   } else {
3751*4683f7a4SShri Abhyankar     /* determine buffer space needed for message */
3752*4683f7a4SShri Abhyankar     nz = 0;
3753*4683f7a4SShri Abhyankar     for (i=0; i<m; i++) {
3754*4683f7a4SShri Abhyankar       nz += locrowlens[i];
3755*4683f7a4SShri Abhyankar     }
3756*4683f7a4SShri Abhyankar     ierr   = PetscMalloc((nz+1)*sizeof(PetscInt),&ibuf);CHKERRQ(ierr);
3757*4683f7a4SShri Abhyankar     mycols = ibuf;
3758*4683f7a4SShri Abhyankar     /* receive message of column indices*/
3759*4683f7a4SShri Abhyankar     ierr = MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
3760*4683f7a4SShri Abhyankar     ierr = MPI_Get_count(&status,MPIU_INT,&maxnz);CHKERRQ(ierr);
3761*4683f7a4SShri Abhyankar     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
3762*4683f7a4SShri Abhyankar   }
3763*4683f7a4SShri Abhyankar 
3764*4683f7a4SShri Abhyankar   /* loop over local rows, determining number of off diagonal entries */
3765*4683f7a4SShri Abhyankar   ierr     = PetscMalloc2(rend-rstart,PetscInt,&dlens,rend-rstart,PetscInt,&odlens);CHKERRQ(ierr);
3766*4683f7a4SShri Abhyankar   ierr     = PetscMalloc3(Mbs,PetscInt,&mask,Mbs,PetscInt,&masked1,Mbs,PetscInt,&masked2);CHKERRQ(ierr);
3767*4683f7a4SShri Abhyankar   ierr     = PetscMemzero(mask,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
3768*4683f7a4SShri Abhyankar   ierr     = PetscMemzero(masked1,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
3769*4683f7a4SShri Abhyankar   ierr     = PetscMemzero(masked2,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
3770*4683f7a4SShri Abhyankar   rowcount = 0; nzcount = 0;
3771*4683f7a4SShri Abhyankar   for (i=0; i<mbs; i++) {
3772*4683f7a4SShri Abhyankar     dcount  = 0;
3773*4683f7a4SShri Abhyankar     odcount = 0;
3774*4683f7a4SShri Abhyankar     for (j=0; j<bs; j++) {
3775*4683f7a4SShri Abhyankar       kmax = locrowlens[rowcount];
3776*4683f7a4SShri Abhyankar       for (k=0; k<kmax; k++) {
3777*4683f7a4SShri Abhyankar         tmp = mycols[nzcount++]/bs;
3778*4683f7a4SShri Abhyankar         if (!mask[tmp]) {
3779*4683f7a4SShri Abhyankar           mask[tmp] = 1;
3780*4683f7a4SShri Abhyankar           if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp;
3781*4683f7a4SShri Abhyankar           else masked1[dcount++] = tmp;
3782*4683f7a4SShri Abhyankar         }
3783*4683f7a4SShri Abhyankar       }
3784*4683f7a4SShri Abhyankar       rowcount++;
3785*4683f7a4SShri Abhyankar     }
3786*4683f7a4SShri Abhyankar 
3787*4683f7a4SShri Abhyankar     dlens[i]  = dcount;
3788*4683f7a4SShri Abhyankar     odlens[i] = odcount;
3789*4683f7a4SShri Abhyankar 
3790*4683f7a4SShri Abhyankar     /* zero out the mask elements we set */
3791*4683f7a4SShri Abhyankar     for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
3792*4683f7a4SShri Abhyankar     for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
3793*4683f7a4SShri Abhyankar   }
3794*4683f7a4SShri Abhyankar 
3795*4683f7a4SShri Abhyankar 
3796*4683f7a4SShri Abhyankar   if (!sizesset) {
3797*4683f7a4SShri Abhyankar     ierr = MatSetSizes(newmat,m,m,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
3798*4683f7a4SShri Abhyankar   }
3799*4683f7a4SShri Abhyankar   ierr = MatMPIBAIJSetPreallocation(newmat,bs,0,dlens,0,odlens);CHKERRQ(ierr);
3800*4683f7a4SShri Abhyankar 
3801*4683f7a4SShri Abhyankar   if (!rank) {
3802*4683f7a4SShri Abhyankar     ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&buf);CHKERRQ(ierr);
3803*4683f7a4SShri Abhyankar     /* read in my part of the matrix numerical values  */
3804*4683f7a4SShri Abhyankar     nz = procsnz[0];
3805*4683f7a4SShri Abhyankar     vals = buf;
3806*4683f7a4SShri Abhyankar     mycols = ibuf;
3807*4683f7a4SShri Abhyankar     if (size == 1)  nz -= extra_rows;
3808*4683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
3809*4683f7a4SShri Abhyankar     if (size == 1)  for (i=0; i< extra_rows; i++) { vals[nz+i] = 1.0; }
3810*4683f7a4SShri Abhyankar 
3811*4683f7a4SShri Abhyankar     /* insert into matrix */
3812*4683f7a4SShri Abhyankar     jj      = rstart*bs;
3813*4683f7a4SShri Abhyankar     for (i=0; i<m; i++) {
3814*4683f7a4SShri Abhyankar       ierr = MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);CHKERRQ(ierr);
3815*4683f7a4SShri Abhyankar       mycols += locrowlens[i];
3816*4683f7a4SShri Abhyankar       vals   += locrowlens[i];
3817*4683f7a4SShri Abhyankar       jj++;
3818*4683f7a4SShri Abhyankar     }
3819*4683f7a4SShri Abhyankar     /* read in other processors (except the last one) and ship out */
3820*4683f7a4SShri Abhyankar     for (i=1; i<size-1; i++) {
3821*4683f7a4SShri Abhyankar       nz   = procsnz[i];
3822*4683f7a4SShri Abhyankar       vals = buf;
3823*4683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
3824*4683f7a4SShri Abhyankar       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newmat)->tag,comm);CHKERRQ(ierr);
3825*4683f7a4SShri Abhyankar     }
3826*4683f7a4SShri Abhyankar     /* the last proc */
3827*4683f7a4SShri Abhyankar     if (size != 1){
3828*4683f7a4SShri Abhyankar       nz   = procsnz[i] - extra_rows;
3829*4683f7a4SShri Abhyankar       vals = buf;
3830*4683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
3831*4683f7a4SShri Abhyankar       for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
3832*4683f7a4SShri Abhyankar       ierr = MPI_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,((PetscObject)newmat)->tag,comm);CHKERRQ(ierr);
3833*4683f7a4SShri Abhyankar     }
3834*4683f7a4SShri Abhyankar     ierr = PetscFree(procsnz);CHKERRQ(ierr);
3835*4683f7a4SShri Abhyankar   } else {
3836*4683f7a4SShri Abhyankar     /* receive numeric values */
3837*4683f7a4SShri Abhyankar     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&buf);CHKERRQ(ierr);
3838*4683f7a4SShri Abhyankar 
3839*4683f7a4SShri Abhyankar     /* receive message of values*/
3840*4683f7a4SShri Abhyankar     vals   = buf;
3841*4683f7a4SShri Abhyankar     mycols = ibuf;
3842*4683f7a4SShri Abhyankar     ierr   = MPI_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newmat)->tag,comm,&status);CHKERRQ(ierr);
3843*4683f7a4SShri Abhyankar     ierr   = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
3844*4683f7a4SShri Abhyankar     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
3845*4683f7a4SShri Abhyankar 
3846*4683f7a4SShri Abhyankar     /* insert into matrix */
3847*4683f7a4SShri Abhyankar     jj      = rstart*bs;
3848*4683f7a4SShri Abhyankar     for (i=0; i<m; i++) {
3849*4683f7a4SShri Abhyankar       ierr    = MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);CHKERRQ(ierr);
3850*4683f7a4SShri Abhyankar       mycols += locrowlens[i];
3851*4683f7a4SShri Abhyankar       vals   += locrowlens[i];
3852*4683f7a4SShri Abhyankar       jj++;
3853*4683f7a4SShri Abhyankar     }
3854*4683f7a4SShri Abhyankar   }
3855*4683f7a4SShri Abhyankar   ierr = PetscFree(locrowlens);CHKERRQ(ierr);
3856*4683f7a4SShri Abhyankar   ierr = PetscFree(buf);CHKERRQ(ierr);
3857*4683f7a4SShri Abhyankar   ierr = PetscFree(ibuf);CHKERRQ(ierr);
3858*4683f7a4SShri Abhyankar   ierr = PetscFree2(rowners,browners);CHKERRQ(ierr);
3859*4683f7a4SShri Abhyankar   ierr = PetscFree2(dlens,odlens);CHKERRQ(ierr);
3860*4683f7a4SShri Abhyankar   ierr = PetscFree3(mask,masked1,masked2);CHKERRQ(ierr);
3861*4683f7a4SShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3862*4683f7a4SShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3863*4683f7a4SShri Abhyankar 
3864*4683f7a4SShri Abhyankar   PetscFunctionReturn(0);
3865*4683f7a4SShri Abhyankar }
3866*4683f7a4SShri Abhyankar 
3867*4683f7a4SShri Abhyankar #undef __FUNCT__
38684a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJSetHashTableFactor"
3869133cdb44SSatish Balay /*@
3870133cdb44SSatish Balay    MatMPIBAIJSetHashTableFactor - Sets the factor required to compute the size of the HashTable.
3871133cdb44SSatish Balay 
3872133cdb44SSatish Balay    Input Parameters:
3873133cdb44SSatish Balay .  mat  - the matrix
3874133cdb44SSatish Balay .  fact - factor
3875133cdb44SSatish Balay 
3876fee21e36SBarry Smith    Collective on Mat
3877fee21e36SBarry Smith 
38788c890885SBarry Smith    Level: advanced
38798c890885SBarry Smith 
3880133cdb44SSatish Balay   Notes:
38818c07d4e3SBarry Smith    This can also be set by the command line option: -mat_use_hash_table <fact>
3882133cdb44SSatish Balay 
3883133cdb44SSatish Balay .keywords: matrix, hashtable, factor, HT
3884133cdb44SSatish Balay 
3885133cdb44SSatish Balay .seealso: MatSetOption()
3886133cdb44SSatish Balay @*/
3887be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJSetHashTableFactor(Mat mat,PetscReal fact)
3888133cdb44SSatish Balay {
3889dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscReal);
38905bf65638SKris Buschelman 
38915bf65638SKris Buschelman   PetscFunctionBegin;
38925bf65638SKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSetHashTableFactor_C",(void (**)(void))&f);CHKERRQ(ierr);
38935bf65638SKris Buschelman   if (f) {
38945bf65638SKris Buschelman     ierr = (*f)(mat,fact);CHKERRQ(ierr);
38955bf65638SKris Buschelman   }
38965bf65638SKris Buschelman   PetscFunctionReturn(0);
38975bf65638SKris Buschelman }
38985bf65638SKris Buschelman 
3899be1d678aSKris Buschelman EXTERN_C_BEGIN
39005bf65638SKris Buschelman #undef __FUNCT__
39015bf65638SKris Buschelman #define __FUNCT__ "MatSetHashTableFactor_MPIBAIJ"
3902be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSetHashTableFactor_MPIBAIJ(Mat mat,PetscReal fact)
39035bf65638SKris Buschelman {
390425fdafccSSatish Balay   Mat_MPIBAIJ *baij;
3905133cdb44SSatish Balay 
3906133cdb44SSatish Balay   PetscFunctionBegin;
3907133cdb44SSatish Balay   baij = (Mat_MPIBAIJ*)mat->data;
3908133cdb44SSatish Balay   baij->ht_fact = fact;
3909133cdb44SSatish Balay   PetscFunctionReturn(0);
3910133cdb44SSatish Balay }
3911be1d678aSKris Buschelman EXTERN_C_END
3912f2a5309cSSatish Balay 
39134a2ae208SSatish Balay #undef __FUNCT__
39144a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJGetSeqBAIJ"
3915be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJGetSeqBAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[])
3916f2a5309cSSatish Balay {
3917f2a5309cSSatish Balay   Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
3918f2a5309cSSatish Balay   PetscFunctionBegin;
3919f2a5309cSSatish Balay   *Ad     = a->A;
3920f2a5309cSSatish Balay   *Ao     = a->B;
3921195d93cdSBarry Smith   *colmap = a->garray;
3922f2a5309cSSatish Balay   PetscFunctionReturn(0);
3923f2a5309cSSatish Balay }
392485535b8eSBarry Smith 
392585535b8eSBarry Smith /*
392685535b8eSBarry Smith     Special version for direct calls from Fortran (to eliminate two function call overheads
392785535b8eSBarry Smith */
392885535b8eSBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
392985535b8eSBarry Smith #define matmpibaijsetvaluesblocked_ MATMPIBAIJSETVALUESBLOCKED
393085535b8eSBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
393185535b8eSBarry Smith #define matmpibaijsetvaluesblocked_ matmpibaijsetvaluesblocked
393285535b8eSBarry Smith #endif
393385535b8eSBarry Smith 
393485535b8eSBarry Smith #undef __FUNCT__
393585535b8eSBarry Smith #define __FUNCT__ "matmpibiajsetvaluesblocked"
393685535b8eSBarry Smith /*@C
393785535b8eSBarry Smith   MatMPIBAIJSetValuesBlocked - Direct Fortran call to replace call to MatSetValuesBlocked()
393885535b8eSBarry Smith 
393985535b8eSBarry Smith   Collective on Mat
394085535b8eSBarry Smith 
394185535b8eSBarry Smith   Input Parameters:
394285535b8eSBarry Smith + mat - the matrix
394385535b8eSBarry Smith . min - number of input rows
394485535b8eSBarry Smith . im - input rows
394585535b8eSBarry Smith . nin - number of input columns
394685535b8eSBarry Smith . in - input columns
394785535b8eSBarry Smith . v - numerical values input
394885535b8eSBarry Smith - addvin - INSERT_VALUES or ADD_VALUES
394985535b8eSBarry Smith 
395085535b8eSBarry Smith   Notes: This has a complete copy of MatSetValuesBlocked_MPIBAIJ() which is terrible code un-reuse.
395185535b8eSBarry Smith 
395285535b8eSBarry Smith   Level: advanced
395385535b8eSBarry Smith 
395485535b8eSBarry Smith .seealso:   MatSetValuesBlocked()
395585535b8eSBarry Smith @*/
395685535b8eSBarry Smith PetscErrorCode matmpibaijsetvaluesblocked_(Mat *matin,PetscInt *min,const PetscInt im[],PetscInt *nin,const PetscInt in[],const MatScalar v[],InsertMode *addvin)
395785535b8eSBarry Smith {
395885535b8eSBarry Smith   /* convert input arguments to C version */
395985535b8eSBarry Smith   Mat             mat = *matin;
396085535b8eSBarry Smith   PetscInt        m = *min, n = *nin;
396185535b8eSBarry Smith   InsertMode      addv = *addvin;
396285535b8eSBarry Smith 
396385535b8eSBarry Smith   Mat_MPIBAIJ     *baij = (Mat_MPIBAIJ*)mat->data;
396485535b8eSBarry Smith   const MatScalar *value;
396585535b8eSBarry Smith   MatScalar       *barray=baij->barray;
396685535b8eSBarry Smith   PetscTruth      roworiented = baij->roworiented;
396785535b8eSBarry Smith   PetscErrorCode  ierr;
396885535b8eSBarry Smith   PetscInt        i,j,ii,jj,row,col,rstart=baij->rstartbs;
396985535b8eSBarry Smith   PetscInt        rend=baij->rendbs,cstart=baij->cstartbs,stepval;
3970d0f46423SBarry Smith   PetscInt        cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;
397185535b8eSBarry Smith 
397285535b8eSBarry Smith   PetscFunctionBegin;
397385535b8eSBarry Smith   /* tasks normally handled by MatSetValuesBlocked() */
397485535b8eSBarry Smith   if (mat->insertmode == NOT_SET_VALUES) {
397585535b8eSBarry Smith     mat->insertmode = addv;
397685535b8eSBarry Smith   }
397785535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
3978e7e72b3dSBarry Smith   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
3979e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
398085535b8eSBarry Smith #endif
398185535b8eSBarry Smith   if (mat->assembled) {
398285535b8eSBarry Smith     mat->was_assembled = PETSC_TRUE;
398385535b8eSBarry Smith     mat->assembled     = PETSC_FALSE;
398485535b8eSBarry Smith   }
398585535b8eSBarry Smith   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
398685535b8eSBarry Smith 
398785535b8eSBarry Smith 
398885535b8eSBarry Smith   if(!barray) {
398985535b8eSBarry Smith     ierr         = PetscMalloc(bs2*sizeof(MatScalar),&barray);CHKERRQ(ierr);
399085535b8eSBarry Smith     baij->barray = barray;
399185535b8eSBarry Smith   }
399285535b8eSBarry Smith 
399385535b8eSBarry Smith   if (roworiented) {
399485535b8eSBarry Smith     stepval = (n-1)*bs;
399585535b8eSBarry Smith   } else {
399685535b8eSBarry Smith     stepval = (m-1)*bs;
399785535b8eSBarry Smith   }
399885535b8eSBarry Smith   for (i=0; i<m; i++) {
399985535b8eSBarry Smith     if (im[i] < 0) continue;
400085535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
4001e32f2f54SBarry Smith     if (im[i] >= baij->Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large, row %D max %D",im[i],baij->Mbs-1);
400285535b8eSBarry Smith #endif
400385535b8eSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
400485535b8eSBarry Smith       row = im[i] - rstart;
400585535b8eSBarry Smith       for (j=0; j<n; j++) {
400685535b8eSBarry Smith         /* If NumCol = 1 then a copy is not required */
400785535b8eSBarry Smith         if ((roworiented) && (n == 1)) {
400885535b8eSBarry Smith           barray = (MatScalar*)v + i*bs2;
400985535b8eSBarry Smith         } else if((!roworiented) && (m == 1)) {
401085535b8eSBarry Smith           barray = (MatScalar*)v + j*bs2;
401185535b8eSBarry Smith         } else { /* Here a copy is required */
401285535b8eSBarry Smith           if (roworiented) {
401385535b8eSBarry Smith             value = v + i*(stepval+bs)*bs + j*bs;
401485535b8eSBarry Smith           } else {
401585535b8eSBarry Smith             value = v + j*(stepval+bs)*bs + i*bs;
401685535b8eSBarry Smith           }
401785535b8eSBarry Smith           for (ii=0; ii<bs; ii++,value+=stepval) {
401885535b8eSBarry Smith             for (jj=0; jj<bs; jj++) {
401985535b8eSBarry Smith               *barray++  = *value++;
402085535b8eSBarry Smith             }
402185535b8eSBarry Smith           }
402285535b8eSBarry Smith           barray -=bs2;
402385535b8eSBarry Smith         }
402485535b8eSBarry Smith 
402585535b8eSBarry Smith         if (in[j] >= cstart && in[j] < cend){
402685535b8eSBarry Smith           col  = in[j] - cstart;
402797e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
402885535b8eSBarry Smith         }
402985535b8eSBarry Smith         else if (in[j] < 0) continue;
403085535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
4031cb9801acSJed Brown         else if (in[j] >= baij->Nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large, col %D max %D",in[j],baij->Nbs-1);
403285535b8eSBarry Smith #endif
403385535b8eSBarry Smith         else {
403485535b8eSBarry Smith           if (mat->was_assembled) {
403585535b8eSBarry Smith             if (!baij->colmap) {
403685535b8eSBarry Smith               ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
403785535b8eSBarry Smith             }
403885535b8eSBarry Smith 
403985535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
404085535b8eSBarry Smith #if defined (PETSC_USE_CTABLE)
404185535b8eSBarry Smith             { PetscInt data;
404285535b8eSBarry Smith               ierr = PetscTableFind(baij->colmap,in[j]+1,&data);CHKERRQ(ierr);
4043e32f2f54SBarry Smith               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
404485535b8eSBarry Smith             }
404585535b8eSBarry Smith #else
4046e32f2f54SBarry Smith             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
404785535b8eSBarry Smith #endif
404885535b8eSBarry Smith #endif
404985535b8eSBarry Smith #if defined (PETSC_USE_CTABLE)
405085535b8eSBarry Smith 	    ierr = PetscTableFind(baij->colmap,in[j]+1,&col);CHKERRQ(ierr);
405185535b8eSBarry Smith             col  = (col - 1)/bs;
405285535b8eSBarry Smith #else
405385535b8eSBarry Smith             col = (baij->colmap[in[j]] - 1)/bs;
405485535b8eSBarry Smith #endif
405585535b8eSBarry Smith             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
405685535b8eSBarry Smith               ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
405785535b8eSBarry Smith               col =  in[j];
405885535b8eSBarry Smith             }
405985535b8eSBarry Smith           }
406085535b8eSBarry Smith           else col = in[j];
406197e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
406285535b8eSBarry Smith         }
406385535b8eSBarry Smith       }
406485535b8eSBarry Smith     } else {
406585535b8eSBarry Smith       if (!baij->donotstash) {
406685535b8eSBarry Smith         if (roworiented) {
406785535b8eSBarry Smith           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
406885535b8eSBarry Smith         } else {
406985535b8eSBarry Smith           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
407085535b8eSBarry Smith         }
407185535b8eSBarry Smith       }
407285535b8eSBarry Smith     }
407385535b8eSBarry Smith   }
407485535b8eSBarry Smith 
407585535b8eSBarry Smith   /* task normally handled by MatSetValuesBlocked() */
407685535b8eSBarry Smith   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
407785535b8eSBarry Smith   PetscFunctionReturn(0);
407885535b8eSBarry Smith }
4079