xref: /petsc/src/mat/impls/baij/mpi/mpibaij.c (revision e32f2f54e699d0aa6e733466c00da7e34666fe5e)
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 
11080c1aa95SSatish Balay #define CHUNKSIZE  10
11180c1aa95SSatish Balay 
112f5e9677aSSatish Balay #define  MatSetValues_SeqBAIJ_A_Private(row,col,value,addv) \
11380c1aa95SSatish Balay { \
11480c1aa95SSatish Balay  \
11580c1aa95SSatish Balay     brow = row/bs;  \
11680c1aa95SSatish Balay     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow]; \
117ac7a638eSSatish Balay     rmax = aimax[brow]; nrow = ailen[brow]; \
11880c1aa95SSatish Balay       bcol = col/bs; \
11980c1aa95SSatish Balay       ridx = row % bs; cidx = col % bs; \
120ab26458aSBarry Smith       low = 0; high = nrow; \
121ab26458aSBarry Smith       while (high-low > 3) { \
122ab26458aSBarry Smith         t = (low+high)/2; \
123ab26458aSBarry Smith         if (rp[t] > bcol) high = t; \
124ab26458aSBarry Smith         else              low  = t; \
125ab26458aSBarry Smith       } \
126ab26458aSBarry Smith       for (_i=low; _i<high; _i++) { \
12780c1aa95SSatish Balay         if (rp[_i] > bcol) break; \
12880c1aa95SSatish Balay         if (rp[_i] == bcol) { \
12980c1aa95SSatish Balay           bap  = ap +  bs2*_i + bs*cidx + ridx; \
130eada6651SSatish Balay           if (addv == ADD_VALUES) *bap += value;  \
131eada6651SSatish Balay           else                    *bap  = value;  \
132ac7a638eSSatish Balay           goto a_noinsert; \
13380c1aa95SSatish Balay         } \
13480c1aa95SSatish Balay       } \
13589280ab3SLois Curfman McInnes       if (a->nonew == 1) goto a_noinsert; \
136*e32f2f54SBarry Smith       if (a->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
137421e10b8SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,aimax,a->nonew,MatScalar); \
13880c1aa95SSatish Balay       N = nrow++ - 1;  \
13980c1aa95SSatish Balay       /* shift up all the later entries in this row */ \
14080c1aa95SSatish Balay       for (ii=N; ii>=_i; ii--) { \
14180c1aa95SSatish Balay         rp[ii+1] = rp[ii]; \
1423eda8832SBarry Smith         ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr); \
14380c1aa95SSatish Balay       } \
1443eda8832SBarry Smith       if (N>=_i) { ierr = PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));CHKERRQ(ierr); }  \
14580c1aa95SSatish Balay       rp[_i]                      = bcol;  \
14680c1aa95SSatish Balay       ap[bs2*_i + bs*cidx + ridx] = value;  \
147ac7a638eSSatish Balay       a_noinsert:; \
14880c1aa95SSatish Balay     ailen[brow] = nrow; \
14980c1aa95SSatish Balay }
15057b952d6SSatish Balay 
151ac7a638eSSatish Balay #define  MatSetValues_SeqBAIJ_B_Private(row,col,value,addv) \
152ac7a638eSSatish Balay { \
153ac7a638eSSatish Balay     brow = row/bs;  \
154ac7a638eSSatish Balay     rp   = bj + bi[brow]; ap = ba + bs2*bi[brow]; \
155ac7a638eSSatish Balay     rmax = bimax[brow]; nrow = bilen[brow]; \
156ac7a638eSSatish Balay       bcol = col/bs; \
157ac7a638eSSatish Balay       ridx = row % bs; cidx = col % bs; \
158ac7a638eSSatish Balay       low = 0; high = nrow; \
159ac7a638eSSatish Balay       while (high-low > 3) { \
160ac7a638eSSatish Balay         t = (low+high)/2; \
161ac7a638eSSatish Balay         if (rp[t] > bcol) high = t; \
162ac7a638eSSatish Balay         else              low  = t; \
163ac7a638eSSatish Balay       } \
164ac7a638eSSatish Balay       for (_i=low; _i<high; _i++) { \
165ac7a638eSSatish Balay         if (rp[_i] > bcol) break; \
166ac7a638eSSatish Balay         if (rp[_i] == bcol) { \
167ac7a638eSSatish Balay           bap  = ap +  bs2*_i + bs*cidx + ridx; \
168ac7a638eSSatish Balay           if (addv == ADD_VALUES) *bap += value;  \
169ac7a638eSSatish Balay           else                    *bap  = value;  \
170ac7a638eSSatish Balay           goto b_noinsert; \
171ac7a638eSSatish Balay         } \
172ac7a638eSSatish Balay       } \
17389280ab3SLois Curfman McInnes       if (b->nonew == 1) goto b_noinsert; \
174*e32f2f54SBarry Smith       if (b->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
175421e10b8SBarry Smith       MatSeqXAIJReallocateAIJ(B,b->mbs,bs2,nrow,brow,bcol,rmax,ba,bi,bj,rp,ap,bimax,b->nonew,MatScalar); \
176085a36d4SBarry Smith       CHKMEMQ;\
177ac7a638eSSatish Balay       N = nrow++ - 1;  \
178ac7a638eSSatish Balay       /* shift up all the later entries in this row */ \
179ac7a638eSSatish Balay       for (ii=N; ii>=_i; ii--) { \
180ac7a638eSSatish Balay         rp[ii+1] = rp[ii]; \
1813eda8832SBarry Smith         ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr); \
182ac7a638eSSatish Balay       } \
1833eda8832SBarry Smith       if (N>=_i) { ierr = PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));CHKERRQ(ierr);}  \
184ac7a638eSSatish Balay       rp[_i]                      = bcol;  \
185ac7a638eSSatish Balay       ap[bs2*_i + bs*cidx + ridx] = value;  \
186ac7a638eSSatish Balay       b_noinsert:; \
187ac7a638eSSatish Balay     bilen[brow] = nrow; \
188ac7a638eSSatish Balay }
189ac7a638eSSatish Balay 
1904a2ae208SSatish Balay #undef __FUNCT__
1914a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIBAIJ"
192b24ad042SBarry Smith PetscErrorCode MatSetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
19357b952d6SSatish Balay {
19457b952d6SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
19593fea6afSBarry Smith   MatScalar      value;
196273d9f13SBarry Smith   PetscTruth     roworiented = baij->roworiented;
197dfbe8321SBarry Smith   PetscErrorCode ierr;
198b24ad042SBarry Smith   PetscInt       i,j,row,col;
199d0f46423SBarry Smith   PetscInt       rstart_orig=mat->rmap->rstart;
200d0f46423SBarry Smith   PetscInt       rend_orig=mat->rmap->rend,cstart_orig=mat->cmap->rstart;
201d0f46423SBarry Smith   PetscInt       cend_orig=mat->cmap->rend,bs=mat->rmap->bs;
20257b952d6SSatish Balay 
203eada6651SSatish Balay   /* Some Variables required in the macro */
20480c1aa95SSatish Balay   Mat            A = baij->A;
20580c1aa95SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)(A)->data;
206b24ad042SBarry Smith   PetscInt       *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j;
2073eda8832SBarry Smith   MatScalar      *aa=a->a;
208ac7a638eSSatish Balay 
209ac7a638eSSatish Balay   Mat            B = baij->B;
210ac7a638eSSatish Balay   Mat_SeqBAIJ    *b = (Mat_SeqBAIJ*)(B)->data;
211b24ad042SBarry Smith   PetscInt       *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j;
2123eda8832SBarry Smith   MatScalar      *ba=b->a;
213ac7a638eSSatish Balay 
214b24ad042SBarry Smith   PetscInt       *rp,ii,nrow,_i,rmax,N,brow,bcol;
215b24ad042SBarry Smith   PetscInt       low,high,t,ridx,cidx,bs2=a->bs2;
2163eda8832SBarry Smith   MatScalar      *ap,*bap;
21780c1aa95SSatish Balay 
218d64ed03dSBarry Smith   PetscFunctionBegin;
21971fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
22057b952d6SSatish Balay   for (i=0; i<m; i++) {
2215ef9f2a5SBarry Smith     if (im[i] < 0) continue;
2222515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
223*e32f2f54SBarry 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);
224639f9d9dSBarry Smith #endif
22557b952d6SSatish Balay     if (im[i] >= rstart_orig && im[i] < rend_orig) {
22657b952d6SSatish Balay       row = im[i] - rstart_orig;
22757b952d6SSatish Balay       for (j=0; j<n; j++) {
22857b952d6SSatish Balay         if (in[j] >= cstart_orig && in[j] < cend_orig){
22957b952d6SSatish Balay           col = in[j] - cstart_orig;
23057b952d6SSatish Balay           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
231f5e9677aSSatish Balay           MatSetValues_SeqBAIJ_A_Private(row,col,value,addv);
23280c1aa95SSatish Balay           /* ierr = MatSetValues_SeqBAIJ(baij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
23373959e64SBarry Smith         } else if (in[j] < 0) continue;
2342515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
235*e32f2f54SBarry Smith         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);}
236639f9d9dSBarry Smith #endif
23757b952d6SSatish Balay         else {
23857b952d6SSatish Balay           if (mat->was_assembled) {
239905e6a2fSBarry Smith             if (!baij->colmap) {
240905e6a2fSBarry Smith               ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
241905e6a2fSBarry Smith             }
242aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
2430f5bd95cSBarry Smith             ierr = PetscTableFind(baij->colmap,in[j]/bs + 1,&col);CHKERRQ(ierr);
244bba1ac68SSatish Balay             col  = col - 1;
24548e59246SSatish Balay #else
246bba1ac68SSatish Balay             col = baij->colmap[in[j]/bs] - 1;
24748e59246SSatish Balay #endif
24857b952d6SSatish Balay             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
24957b952d6SSatish Balay               ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
2508295de27SSatish Balay               col =  in[j];
2519bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
2529bf004c3SSatish Balay               B = baij->B;
2539bf004c3SSatish Balay               b = (Mat_SeqBAIJ*)(B)->data;
2549bf004c3SSatish Balay               bimax=b->imax;bi=b->i;bilen=b->ilen;bj=b->j;
2559bf004c3SSatish Balay               ba=b->a;
256bba1ac68SSatish Balay             } else col += in[j]%bs;
2578295de27SSatish Balay           } else col = in[j];
25857b952d6SSatish Balay           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
25990da58bdSSatish Balay           MatSetValues_SeqBAIJ_B_Private(row,col,value,addv);
26090da58bdSSatish Balay           /* ierr = MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
26157b952d6SSatish Balay         }
26257b952d6SSatish Balay       }
263d64ed03dSBarry Smith     } else {
26490f02eecSBarry Smith       if (!baij->donotstash) {
265ff2fd236SBarry Smith         if (roworiented) {
266b400d20cSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);CHKERRQ(ierr);
267ff2fd236SBarry Smith         } else {
268b400d20cSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);CHKERRQ(ierr);
26957b952d6SSatish Balay         }
27057b952d6SSatish Balay       }
27157b952d6SSatish Balay     }
27290f02eecSBarry Smith   }
2733a40ed3dSBarry Smith   PetscFunctionReturn(0);
27457b952d6SSatish Balay }
27557b952d6SSatish Balay 
2764a2ae208SSatish Balay #undef __FUNCT__
27797e5c40aSBarry Smith #define __FUNCT__ "MatSetValuesBlocked_MPIBAIJ"
27897e5c40aSBarry Smith PetscErrorCode MatSetValuesBlocked_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
279ab26458aSBarry Smith {
280ab26458aSBarry Smith   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
281dd6ea824SBarry Smith   const PetscScalar *value;
282f15d580aSBarry Smith   MatScalar         *barray=baij->barray;
283273d9f13SBarry Smith   PetscTruth        roworiented = baij->roworiented;
284dfbe8321SBarry Smith   PetscErrorCode    ierr;
285899cda47SBarry Smith   PetscInt          i,j,ii,jj,row,col,rstart=baij->rstartbs;
286899cda47SBarry Smith   PetscInt          rend=baij->rendbs,cstart=baij->cstartbs,stepval;
287d0f46423SBarry Smith   PetscInt          cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;
288ab26458aSBarry Smith 
289b16ae2b1SBarry Smith   PetscFunctionBegin;
29030793edcSSatish Balay   if(!barray) {
29182502324SSatish Balay     ierr         = PetscMalloc(bs2*sizeof(MatScalar),&barray);CHKERRQ(ierr);
29282502324SSatish Balay     baij->barray = barray;
29330793edcSSatish Balay   }
29430793edcSSatish Balay 
295ab26458aSBarry Smith   if (roworiented) {
296ab26458aSBarry Smith     stepval = (n-1)*bs;
297ab26458aSBarry Smith   } else {
298ab26458aSBarry Smith     stepval = (m-1)*bs;
299ab26458aSBarry Smith   }
300ab26458aSBarry Smith   for (i=0; i<m; i++) {
3015ef9f2a5SBarry Smith     if (im[i] < 0) continue;
3022515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
303*e32f2f54SBarry 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);
304ab26458aSBarry Smith #endif
305ab26458aSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
306ab26458aSBarry Smith       row = im[i] - rstart;
307ab26458aSBarry Smith       for (j=0; j<n; j++) {
30815b57d14SSatish Balay         /* If NumCol = 1 then a copy is not required */
30915b57d14SSatish Balay         if ((roworiented) && (n == 1)) {
310f15d580aSBarry Smith           barray = (MatScalar*)v + i*bs2;
31115b57d14SSatish Balay         } else if((!roworiented) && (m == 1)) {
312f15d580aSBarry Smith           barray = (MatScalar*)v + j*bs2;
31315b57d14SSatish Balay         } else { /* Here a copy is required */
314ab26458aSBarry Smith           if (roworiented) {
315ab26458aSBarry Smith             value = v + i*(stepval+bs)*bs + j*bs;
316ab26458aSBarry Smith           } else {
317ab26458aSBarry Smith             value = v + j*(stepval+bs)*bs + i*bs;
318abef11f7SSatish Balay           }
31947513183SBarry Smith           for (ii=0; ii<bs; ii++,value+=stepval) {
32047513183SBarry Smith             for (jj=0; jj<bs; jj++) {
32130793edcSSatish Balay               *barray++  = *value++;
32247513183SBarry Smith             }
32347513183SBarry Smith           }
32430793edcSSatish Balay           barray -=bs2;
32515b57d14SSatish Balay         }
326abef11f7SSatish Balay 
327abef11f7SSatish Balay         if (in[j] >= cstart && in[j] < cend){
328abef11f7SSatish Balay           col  = in[j] - cstart;
32997e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
330ab26458aSBarry Smith         }
3315ef9f2a5SBarry Smith         else if (in[j] < 0) continue;
3322515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
333*e32f2f54SBarry Smith         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);}
334ab26458aSBarry Smith #endif
335ab26458aSBarry Smith         else {
336ab26458aSBarry Smith           if (mat->was_assembled) {
337ab26458aSBarry Smith             if (!baij->colmap) {
338ab26458aSBarry Smith               ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
339ab26458aSBarry Smith             }
340a5eb4965SSatish Balay 
3412515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
342aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
343b24ad042SBarry Smith             { PetscInt data;
3440f5bd95cSBarry Smith               ierr = PetscTableFind(baij->colmap,in[j]+1,&data);CHKERRQ(ierr);
345*e32f2f54SBarry Smith               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
346fa46199cSSatish Balay             }
34748e59246SSatish Balay #else
348*e32f2f54SBarry Smith             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
349a5eb4965SSatish Balay #endif
35048e59246SSatish Balay #endif
351aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
3520f5bd95cSBarry Smith 	    ierr = PetscTableFind(baij->colmap,in[j]+1,&col);CHKERRQ(ierr);
353fa46199cSSatish Balay             col  = (col - 1)/bs;
35448e59246SSatish Balay #else
355a5eb4965SSatish Balay             col = (baij->colmap[in[j]] - 1)/bs;
35648e59246SSatish Balay #endif
357ab26458aSBarry Smith             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
358ab26458aSBarry Smith               ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
359ab26458aSBarry Smith               col =  in[j];
360ab26458aSBarry Smith             }
361ab26458aSBarry Smith           }
362ab26458aSBarry Smith           else col = in[j];
36397e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
364ab26458aSBarry Smith         }
365ab26458aSBarry Smith       }
366d64ed03dSBarry Smith     } else {
367ab26458aSBarry Smith       if (!baij->donotstash) {
368ff2fd236SBarry Smith         if (roworiented) {
3696fa18ffdSBarry Smith           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
370ff2fd236SBarry Smith         } else {
3716fa18ffdSBarry Smith           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
372ff2fd236SBarry Smith         }
373abef11f7SSatish Balay       }
374ab26458aSBarry Smith     }
375ab26458aSBarry Smith   }
3763a40ed3dSBarry Smith   PetscFunctionReturn(0);
377ab26458aSBarry Smith }
3786fa18ffdSBarry Smith 
3790bdbc534SSatish Balay #define HASH_KEY 0.6180339887
380b24ad042SBarry Smith #define HASH(size,key,tmp) (tmp = (key)*HASH_KEY,(PetscInt)((size)*(tmp-(PetscInt)tmp)))
381b24ad042SBarry Smith /* #define HASH(size,key) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
382b24ad042SBarry Smith /* #define HASH(size,key,tmp) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
3834a2ae208SSatish Balay #undef __FUNCT__
38497e5c40aSBarry Smith #define __FUNCT__ "MatSetValues_MPIBAIJ_HT"
38597e5c40aSBarry Smith PetscErrorCode MatSetValues_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
3860bdbc534SSatish Balay {
3870bdbc534SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
388273d9f13SBarry Smith   PetscTruth     roworiented = baij->roworiented;
389dfbe8321SBarry Smith   PetscErrorCode ierr;
390b24ad042SBarry Smith   PetscInt       i,j,row,col;
391d0f46423SBarry Smith   PetscInt       rstart_orig=mat->rmap->rstart;
392d0f46423SBarry Smith   PetscInt       rend_orig=mat->rmap->rend,Nbs=baij->Nbs;
393d0f46423SBarry Smith   PetscInt       h1,key,size=baij->ht_size,bs=mat->rmap->bs,*HT=baij->ht,idx;
394329f5518SBarry Smith   PetscReal      tmp;
3953eda8832SBarry Smith   MatScalar      **HD = baij->hd,value;
3962515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
397b24ad042SBarry Smith   PetscInt       total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
3984a15367fSSatish Balay #endif
3990bdbc534SSatish Balay 
4000bdbc534SSatish Balay   PetscFunctionBegin;
40171fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
4020bdbc534SSatish Balay   for (i=0; i<m; i++) {
4032515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
404*e32f2f54SBarry Smith     if (im[i] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
405*e32f2f54SBarry 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);
4060bdbc534SSatish Balay #endif
4070bdbc534SSatish Balay       row = im[i];
408c2760754SSatish Balay     if (row >= rstart_orig && row < rend_orig) {
4090bdbc534SSatish Balay       for (j=0; j<n; j++) {
4100bdbc534SSatish Balay         col = in[j];
4116fa18ffdSBarry Smith         if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
412b24ad042SBarry Smith         /* Look up PetscInto the Hash Table */
413c2760754SSatish Balay         key = (row/bs)*Nbs+(col/bs)+1;
414c2760754SSatish Balay         h1  = HASH(size,key,tmp);
4150bdbc534SSatish Balay 
416c2760754SSatish Balay 
417c2760754SSatish Balay         idx = h1;
4182515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
419187ce0cbSSatish Balay         insert_ct++;
420187ce0cbSSatish Balay         total_ct++;
421187ce0cbSSatish Balay         if (HT[idx] != key) {
422187ce0cbSSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++);
423187ce0cbSSatish Balay           if (idx == size) {
424187ce0cbSSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++);
425187ce0cbSSatish Balay             if (idx == h1) {
426*e32f2f54SBarry Smith               SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
427187ce0cbSSatish Balay             }
428187ce0cbSSatish Balay           }
429187ce0cbSSatish Balay         }
430187ce0cbSSatish Balay #else
431c2760754SSatish Balay         if (HT[idx] != key) {
432c2760754SSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++);
433c2760754SSatish Balay           if (idx == size) {
434c2760754SSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++);
435c2760754SSatish Balay             if (idx == h1) {
436*e32f2f54SBarry Smith               SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
437c2760754SSatish Balay             }
438c2760754SSatish Balay           }
439c2760754SSatish Balay         }
440187ce0cbSSatish Balay #endif
441c2760754SSatish Balay         /* A HASH table entry is found, so insert the values at the correct address */
442c2760754SSatish Balay         if (addv == ADD_VALUES) *(HD[idx]+ (col % bs)*bs + (row % bs)) += value;
443c2760754SSatish Balay         else                    *(HD[idx]+ (col % bs)*bs + (row % bs))  = value;
4440bdbc534SSatish Balay       }
4450bdbc534SSatish Balay     } else {
4460bdbc534SSatish Balay       if (!baij->donotstash) {
447ff2fd236SBarry Smith         if (roworiented) {
448b400d20cSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);CHKERRQ(ierr);
449ff2fd236SBarry Smith         } else {
450b400d20cSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);CHKERRQ(ierr);
4510bdbc534SSatish Balay         }
4520bdbc534SSatish Balay       }
4530bdbc534SSatish Balay     }
4540bdbc534SSatish Balay   }
4552515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
456187ce0cbSSatish Balay   baij->ht_total_ct = total_ct;
457187ce0cbSSatish Balay   baij->ht_insert_ct = insert_ct;
458187ce0cbSSatish Balay #endif
4590bdbc534SSatish Balay   PetscFunctionReturn(0);
4600bdbc534SSatish Balay }
4610bdbc534SSatish Balay 
4624a2ae208SSatish Balay #undef __FUNCT__
46397e5c40aSBarry Smith #define __FUNCT__ "MatSetValuesBlocked_MPIBAIJ_HT"
46497e5c40aSBarry Smith PetscErrorCode MatSetValuesBlocked_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
4650bdbc534SSatish Balay {
4660bdbc534SSatish Balay   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
467273d9f13SBarry Smith   PetscTruth        roworiented = baij->roworiented;
468dfbe8321SBarry Smith   PetscErrorCode    ierr;
469b24ad042SBarry Smith   PetscInt          i,j,ii,jj,row,col;
470899cda47SBarry Smith   PetscInt          rstart=baij->rstartbs;
471d0f46423SBarry Smith   PetscInt          rend=mat->rmap->rend,stepval,bs=mat->rmap->bs,bs2=baij->bs2,nbs2=n*bs2;
472b24ad042SBarry Smith   PetscInt          h1,key,size=baij->ht_size,idx,*HT=baij->ht,Nbs=baij->Nbs;
473329f5518SBarry Smith   PetscReal         tmp;
4743eda8832SBarry Smith   MatScalar         **HD = baij->hd,*baij_a;
475dd6ea824SBarry Smith   const PetscScalar *v_t,*value;
4762515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
477b24ad042SBarry Smith   PetscInt          total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
4784a15367fSSatish Balay #endif
4790bdbc534SSatish Balay 
480d0a41580SSatish Balay   PetscFunctionBegin;
481d0a41580SSatish Balay 
4820bdbc534SSatish Balay   if (roworiented) {
4830bdbc534SSatish Balay     stepval = (n-1)*bs;
4840bdbc534SSatish Balay   } else {
4850bdbc534SSatish Balay     stepval = (m-1)*bs;
4860bdbc534SSatish Balay   }
4870bdbc534SSatish Balay   for (i=0; i<m; i++) {
4882515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
489*e32f2f54SBarry Smith     if (im[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",im[i]);
490*e32f2f54SBarry 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);
4910bdbc534SSatish Balay #endif
4920bdbc534SSatish Balay     row   = im[i];
493ab715e2cSSatish Balay     v_t   = v + i*nbs2;
494c2760754SSatish Balay     if (row >= rstart && row < rend) {
4950bdbc534SSatish Balay       for (j=0; j<n; j++) {
4960bdbc534SSatish Balay         col = in[j];
4970bdbc534SSatish Balay 
4980bdbc534SSatish Balay         /* Look up into the Hash Table */
499c2760754SSatish Balay         key = row*Nbs+col+1;
500c2760754SSatish Balay         h1  = HASH(size,key,tmp);
5010bdbc534SSatish Balay 
502c2760754SSatish Balay         idx = h1;
5032515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
504187ce0cbSSatish Balay         total_ct++;
505187ce0cbSSatish Balay         insert_ct++;
506187ce0cbSSatish Balay        if (HT[idx] != key) {
507187ce0cbSSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++);
508187ce0cbSSatish Balay           if (idx == size) {
509187ce0cbSSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++);
510187ce0cbSSatish Balay             if (idx == h1) {
511*e32f2f54SBarry Smith               SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
512187ce0cbSSatish Balay             }
513187ce0cbSSatish Balay           }
514187ce0cbSSatish Balay         }
515187ce0cbSSatish Balay #else
516c2760754SSatish Balay         if (HT[idx] != key) {
517c2760754SSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++);
518c2760754SSatish Balay           if (idx == size) {
519c2760754SSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++);
520c2760754SSatish Balay             if (idx == h1) {
521*e32f2f54SBarry Smith               SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
522c2760754SSatish Balay             }
523c2760754SSatish Balay           }
524c2760754SSatish Balay         }
525187ce0cbSSatish Balay #endif
526c2760754SSatish Balay         baij_a = HD[idx];
5270bdbc534SSatish Balay         if (roworiented) {
528c2760754SSatish Balay           /*value = v + i*(stepval+bs)*bs + j*bs;*/
529187ce0cbSSatish Balay           /* value = v + (i*(stepval+bs)+j)*bs; */
530187ce0cbSSatish Balay           value = v_t;
531187ce0cbSSatish Balay           v_t  += bs;
532fef45726SSatish Balay           if (addv == ADD_VALUES) {
533c2760754SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval) {
534c2760754SSatish Balay               for (jj=ii; jj<bs2; jj+=bs) {
535fef45726SSatish Balay                 baij_a[jj]  += *value++;
536b4cc0f5aSSatish Balay               }
537b4cc0f5aSSatish Balay             }
538fef45726SSatish Balay           } else {
539c2760754SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval) {
540c2760754SSatish Balay               for (jj=ii; jj<bs2; jj+=bs) {
541fef45726SSatish Balay                 baij_a[jj]  = *value++;
542fef45726SSatish Balay               }
543fef45726SSatish Balay             }
544fef45726SSatish Balay           }
5450bdbc534SSatish Balay         } else {
5460bdbc534SSatish Balay           value = v + j*(stepval+bs)*bs + i*bs;
547fef45726SSatish Balay           if (addv == ADD_VALUES) {
548b4cc0f5aSSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
5490bdbc534SSatish Balay               for (jj=0; jj<bs; jj++) {
550fef45726SSatish Balay                 baij_a[jj]  += *value++;
551fef45726SSatish Balay               }
552fef45726SSatish Balay             }
553fef45726SSatish Balay           } else {
554fef45726SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
555fef45726SSatish Balay               for (jj=0; jj<bs; jj++) {
556fef45726SSatish Balay                 baij_a[jj]  = *value++;
557fef45726SSatish Balay               }
558b4cc0f5aSSatish Balay             }
5590bdbc534SSatish Balay           }
5600bdbc534SSatish Balay         }
5610bdbc534SSatish Balay       }
5620bdbc534SSatish Balay     } else {
5630bdbc534SSatish Balay       if (!baij->donotstash) {
5640bdbc534SSatish Balay         if (roworiented) {
5658798bf22SSatish Balay           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
5660bdbc534SSatish Balay         } else {
5678798bf22SSatish Balay           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
5680bdbc534SSatish Balay         }
5690bdbc534SSatish Balay       }
5700bdbc534SSatish Balay     }
5710bdbc534SSatish Balay   }
5722515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
573187ce0cbSSatish Balay   baij->ht_total_ct = total_ct;
574187ce0cbSSatish Balay   baij->ht_insert_ct = insert_ct;
575187ce0cbSSatish Balay #endif
5760bdbc534SSatish Balay   PetscFunctionReturn(0);
5770bdbc534SSatish Balay }
578133cdb44SSatish Balay 
5794a2ae208SSatish Balay #undef __FUNCT__
5804a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIBAIJ"
581b24ad042SBarry Smith PetscErrorCode MatGetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
582d6de1c52SSatish Balay {
583d6de1c52SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
5846849ba73SBarry Smith   PetscErrorCode ierr;
585d0f46423SBarry Smith   PetscInt       bs=mat->rmap->bs,i,j,bsrstart = mat->rmap->rstart,bsrend = mat->rmap->rend;
586d0f46423SBarry Smith   PetscInt       bscstart = mat->cmap->rstart,bscend = mat->cmap->rend,row,col,data;
587d6de1c52SSatish Balay 
588133cdb44SSatish Balay   PetscFunctionBegin;
589d6de1c52SSatish Balay   for (i=0; i<m; i++) {
590*e32f2f54SBarry Smith     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
591*e32f2f54SBarry 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);
592d6de1c52SSatish Balay     if (idxm[i] >= bsrstart && idxm[i] < bsrend) {
593d6de1c52SSatish Balay       row = idxm[i] - bsrstart;
594d6de1c52SSatish Balay       for (j=0; j<n; j++) {
595*e32f2f54SBarry Smith         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
596*e32f2f54SBarry 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);
597d6de1c52SSatish Balay         if (idxn[j] >= bscstart && idxn[j] < bscend){
598d6de1c52SSatish Balay           col = idxn[j] - bscstart;
59998dd23e9SBarry Smith           ierr = MatGetValues_SeqBAIJ(baij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
600d64ed03dSBarry Smith         } else {
601905e6a2fSBarry Smith           if (!baij->colmap) {
602905e6a2fSBarry Smith             ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
603905e6a2fSBarry Smith           }
604aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
6050f5bd95cSBarry Smith           ierr = PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);CHKERRQ(ierr);
606fa46199cSSatish Balay           data --;
60748e59246SSatish Balay #else
60848e59246SSatish Balay           data = baij->colmap[idxn[j]/bs]-1;
60948e59246SSatish Balay #endif
61048e59246SSatish Balay           if((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0;
611d9d09a02SSatish Balay           else {
61248e59246SSatish Balay             col  = data + idxn[j]%bs;
61398dd23e9SBarry Smith             ierr = MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
614d6de1c52SSatish Balay           }
615d6de1c52SSatish Balay         }
616d6de1c52SSatish Balay       }
617d64ed03dSBarry Smith     } else {
618*e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
619d6de1c52SSatish Balay     }
620d6de1c52SSatish Balay   }
6213a40ed3dSBarry Smith  PetscFunctionReturn(0);
622d6de1c52SSatish Balay }
623d6de1c52SSatish Balay 
6244a2ae208SSatish Balay #undef __FUNCT__
6254a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIBAIJ"
626dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIBAIJ(Mat mat,NormType type,PetscReal *nrm)
627d6de1c52SSatish Balay {
628d6de1c52SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
629d6de1c52SSatish Balay   Mat_SeqBAIJ    *amat = (Mat_SeqBAIJ*)baij->A->data,*bmat = (Mat_SeqBAIJ*)baij->B->data;
630dfbe8321SBarry Smith   PetscErrorCode ierr;
631d0f46423SBarry Smith   PetscInt       i,j,bs2=baij->bs2,bs=baij->A->rmap->bs,nz,row,col;
632329f5518SBarry Smith   PetscReal      sum = 0.0;
6333eda8832SBarry Smith   MatScalar      *v;
634d6de1c52SSatish Balay 
635d64ed03dSBarry Smith   PetscFunctionBegin;
636d6de1c52SSatish Balay   if (baij->size == 1) {
637064f8208SBarry Smith     ierr =  MatNorm(baij->A,type,nrm);CHKERRQ(ierr);
638d6de1c52SSatish Balay   } else {
639d6de1c52SSatish Balay     if (type == NORM_FROBENIUS) {
640d6de1c52SSatish Balay       v = amat->a;
6418a62d963SHong Zhang       nz = amat->nz*bs2;
6428a62d963SHong Zhang       for (i=0; i<nz; i++) {
643aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
644329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
645d6de1c52SSatish Balay #else
646d6de1c52SSatish Balay         sum += (*v)*(*v); v++;
647d6de1c52SSatish Balay #endif
648d6de1c52SSatish Balay       }
649d6de1c52SSatish Balay       v = bmat->a;
6508a62d963SHong Zhang       nz = bmat->nz*bs2;
6518a62d963SHong Zhang       for (i=0; i<nz; i++) {
652aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
653329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
654d6de1c52SSatish Balay #else
655d6de1c52SSatish Balay         sum += (*v)*(*v); v++;
656d6de1c52SSatish Balay #endif
657d6de1c52SSatish Balay       }
6587adad957SLisandro Dalcin       ierr = MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr);
659064f8208SBarry Smith       *nrm = sqrt(*nrm);
6608a62d963SHong Zhang     } else if (type == NORM_1) { /* max column sum */
6618a62d963SHong Zhang       PetscReal *tmp,*tmp2;
662899cda47SBarry Smith       PetscInt  *jj,*garray=baij->garray,cstart=baij->rstartbs;
663fca92195SBarry Smith       ierr = PetscMalloc2(mat->cmap->N,PetscReal,&tmp,mat->cmap->N,PetscReal,&tmp2);CHKERRQ(ierr);
664d0f46423SBarry Smith       ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr);
6658a62d963SHong Zhang       v = amat->a; jj = amat->j;
6668a62d963SHong Zhang       for (i=0; i<amat->nz; i++) {
6678a62d963SHong Zhang         for (j=0; j<bs; j++){
6688a62d963SHong Zhang           col = bs*(cstart + *jj) + j; /* column index */
6698a62d963SHong Zhang           for (row=0; row<bs; row++){
6708a62d963SHong Zhang             tmp[col] += PetscAbsScalar(*v);  v++;
6718a62d963SHong Zhang           }
6728a62d963SHong Zhang         }
6738a62d963SHong Zhang         jj++;
6748a62d963SHong Zhang       }
6758a62d963SHong Zhang       v = bmat->a; jj = bmat->j;
6768a62d963SHong Zhang       for (i=0; i<bmat->nz; i++) {
6778a62d963SHong Zhang         for (j=0; j<bs; j++){
6788a62d963SHong Zhang           col = bs*garray[*jj] + j;
6798a62d963SHong Zhang           for (row=0; row<bs; row++){
6808a62d963SHong Zhang             tmp[col] += PetscAbsScalar(*v); v++;
6818a62d963SHong Zhang           }
6828a62d963SHong Zhang         }
6838a62d963SHong Zhang         jj++;
6848a62d963SHong Zhang       }
685d0f46423SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr);
6868a62d963SHong Zhang       *nrm = 0.0;
687d0f46423SBarry Smith       for (j=0; j<mat->cmap->N; j++) {
6888a62d963SHong Zhang         if (tmp2[j] > *nrm) *nrm = tmp2[j];
6898a62d963SHong Zhang       }
690fca92195SBarry Smith       ierr = PetscFree2(tmp,tmp2);CHKERRQ(ierr);
6918a62d963SHong Zhang     } else if (type == NORM_INFINITY) { /* max row sum */
692577dd1f9SKris Buschelman       PetscReal *sums;
693577dd1f9SKris Buschelman       ierr = PetscMalloc(bs*sizeof(PetscReal),&sums);CHKERRQ(ierr)
6948a62d963SHong Zhang       sum = 0.0;
6958a62d963SHong Zhang       for (j=0; j<amat->mbs; j++) {
6968a62d963SHong Zhang         for (row=0; row<bs; row++) sums[row] = 0.0;
6978a62d963SHong Zhang         v = amat->a + bs2*amat->i[j];
6988a62d963SHong Zhang         nz = amat->i[j+1]-amat->i[j];
6998a62d963SHong Zhang         for (i=0; i<nz; i++) {
7008a62d963SHong Zhang           for (col=0; col<bs; col++){
7018a62d963SHong Zhang             for (row=0; row<bs; row++){
7028a62d963SHong Zhang               sums[row] += PetscAbsScalar(*v); v++;
7038a62d963SHong Zhang             }
7048a62d963SHong Zhang           }
7058a62d963SHong Zhang         }
7068a62d963SHong Zhang         v = bmat->a + bs2*bmat->i[j];
7078a62d963SHong Zhang         nz = bmat->i[j+1]-bmat->i[j];
7088a62d963SHong Zhang         for (i=0; i<nz; i++) {
7098a62d963SHong Zhang           for (col=0; col<bs; col++){
7108a62d963SHong Zhang             for (row=0; row<bs; row++){
7118a62d963SHong Zhang               sums[row] += PetscAbsScalar(*v); v++;
7128a62d963SHong Zhang             }
7138a62d963SHong Zhang           }
7148a62d963SHong Zhang         }
7158a62d963SHong Zhang         for (row=0; row<bs; row++){
7168a62d963SHong Zhang           if (sums[row] > sum) sum = sums[row];
7178a62d963SHong Zhang         }
7188a62d963SHong Zhang       }
7197adad957SLisandro Dalcin       ierr = MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPI_MAX,((PetscObject)mat)->comm);CHKERRQ(ierr);
720577dd1f9SKris Buschelman       ierr = PetscFree(sums);CHKERRQ(ierr);
721d64ed03dSBarry Smith     } else {
722*e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for this norm yet");
723d6de1c52SSatish Balay     }
724d64ed03dSBarry Smith   }
7253a40ed3dSBarry Smith   PetscFunctionReturn(0);
726d6de1c52SSatish Balay }
72757b952d6SSatish Balay 
728fef45726SSatish Balay /*
729fef45726SSatish Balay   Creates the hash table, and sets the table
730fef45726SSatish Balay   This table is created only once.
731fef45726SSatish Balay   If new entried need to be added to the matrix
732fef45726SSatish Balay   then the hash table has to be destroyed and
733fef45726SSatish Balay   recreated.
734fef45726SSatish Balay */
7354a2ae208SSatish Balay #undef __FUNCT__
7364a2ae208SSatish Balay #define __FUNCT__ "MatCreateHashTable_MPIBAIJ_Private"
737dfbe8321SBarry Smith PetscErrorCode MatCreateHashTable_MPIBAIJ_Private(Mat mat,PetscReal factor)
738596b8d2eSBarry Smith {
739596b8d2eSBarry Smith   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
740596b8d2eSBarry Smith   Mat            A = baij->A,B=baij->B;
741596b8d2eSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data,*b=(Mat_SeqBAIJ *)B->data;
742b24ad042SBarry Smith   PetscInt       i,j,k,nz=a->nz+b->nz,h1,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
7436849ba73SBarry Smith   PetscErrorCode ierr;
744fca92195SBarry Smith   PetscInt       ht_size,bs2=baij->bs2,rstart=baij->rstartbs;
745899cda47SBarry Smith   PetscInt       cstart=baij->cstartbs,*garray=baij->garray,row,col,Nbs=baij->Nbs;
746b24ad042SBarry Smith   PetscInt       *HT,key;
7473eda8832SBarry Smith   MatScalar      **HD;
748329f5518SBarry Smith   PetscReal      tmp;
7496cf91177SBarry Smith #if defined(PETSC_USE_INFO)
750b24ad042SBarry Smith   PetscInt       ct=0,max=0;
7514a15367fSSatish Balay #endif
752fef45726SSatish Balay 
753d64ed03dSBarry Smith   PetscFunctionBegin;
754fca92195SBarry Smith   if (baij->ht) PetscFunctionReturn(0);
755fef45726SSatish Balay 
756fca92195SBarry Smith   baij->ht_size = (PetscInt)(factor*nz);
757fca92195SBarry Smith   ht_size       = baij->ht_size;
7580bdbc534SSatish Balay 
759fef45726SSatish Balay   /* Allocate Memory for Hash Table */
760fca92195SBarry Smith   ierr = PetscMalloc2(ht_size,MatScalar*,&baij->hd,ht_size,PetscInt,&baij->ht);CHKERRQ(ierr);
761fca92195SBarry Smith   ierr = PetscMemzero(baij->hd,ht_size*sizeof(MatScalar*));CHKERRQ(ierr);
762fca92195SBarry Smith   ierr = PetscMemzero(baij->ht,ht_size*sizeof(PetscInt));CHKERRQ(ierr);
763b9e4cc15SSatish Balay   HD   = baij->hd;
764a07cd24cSSatish Balay   HT   = baij->ht;
765b9e4cc15SSatish Balay 
766596b8d2eSBarry Smith   /* Loop Over A */
7670bdbc534SSatish Balay   for (i=0; i<a->mbs; i++) {
768596b8d2eSBarry Smith     for (j=ai[i]; j<ai[i+1]; j++) {
7690bdbc534SSatish Balay       row = i+rstart;
7700bdbc534SSatish Balay       col = aj[j]+cstart;
771596b8d2eSBarry Smith 
772187ce0cbSSatish Balay       key = row*Nbs + col + 1;
773fca92195SBarry Smith       h1  = HASH(ht_size,key,tmp);
774fca92195SBarry Smith       for (k=0; k<ht_size; k++){
775fca92195SBarry Smith         if (!HT[(h1+k)%ht_size]) {
776fca92195SBarry Smith           HT[(h1+k)%ht_size] = key;
777fca92195SBarry Smith           HD[(h1+k)%ht_size] = a->a + j*bs2;
778596b8d2eSBarry Smith           break;
7796cf91177SBarry Smith #if defined(PETSC_USE_INFO)
780187ce0cbSSatish Balay         } else {
781187ce0cbSSatish Balay           ct++;
782187ce0cbSSatish Balay #endif
783596b8d2eSBarry Smith         }
784187ce0cbSSatish Balay       }
7856cf91177SBarry Smith #if defined(PETSC_USE_INFO)
786187ce0cbSSatish Balay       if (k> max) max = k;
787187ce0cbSSatish Balay #endif
788596b8d2eSBarry Smith     }
789596b8d2eSBarry Smith   }
790596b8d2eSBarry Smith   /* Loop Over B */
7910bdbc534SSatish Balay   for (i=0; i<b->mbs; i++) {
792596b8d2eSBarry Smith     for (j=bi[i]; j<bi[i+1]; j++) {
7930bdbc534SSatish Balay       row = i+rstart;
7940bdbc534SSatish Balay       col = garray[bj[j]];
795187ce0cbSSatish Balay       key = row*Nbs + col + 1;
796fca92195SBarry Smith       h1  = HASH(ht_size,key,tmp);
797fca92195SBarry Smith       for (k=0; k<ht_size; k++){
798fca92195SBarry Smith         if (!HT[(h1+k)%ht_size]) {
799fca92195SBarry Smith           HT[(h1+k)%ht_size] = key;
800fca92195SBarry Smith           HD[(h1+k)%ht_size] = b->a + j*bs2;
801596b8d2eSBarry Smith           break;
8026cf91177SBarry Smith #if defined(PETSC_USE_INFO)
803187ce0cbSSatish Balay         } else {
804187ce0cbSSatish Balay           ct++;
805187ce0cbSSatish Balay #endif
806596b8d2eSBarry Smith         }
807187ce0cbSSatish Balay       }
8086cf91177SBarry Smith #if defined(PETSC_USE_INFO)
809187ce0cbSSatish Balay       if (k> max) max = k;
810187ce0cbSSatish Balay #endif
811596b8d2eSBarry Smith     }
812596b8d2eSBarry Smith   }
813596b8d2eSBarry Smith 
814596b8d2eSBarry Smith   /* Print Summary */
8156cf91177SBarry Smith #if defined(PETSC_USE_INFO)
816fca92195SBarry Smith   for (i=0,j=0; i<ht_size; i++) {
817596b8d2eSBarry Smith     if (HT[i]) {j++;}
818c38d4ed2SBarry Smith   }
8191e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Average Search = %5.2f,max search = %D\n",(!j)? 0.0:((PetscReal)(ct+j))/j,max);CHKERRQ(ierr);
820187ce0cbSSatish Balay #endif
8213a40ed3dSBarry Smith   PetscFunctionReturn(0);
822596b8d2eSBarry Smith }
82357b952d6SSatish Balay 
8244a2ae208SSatish Balay #undef __FUNCT__
8254a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIBAIJ"
826dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIBAIJ(Mat mat,MatAssemblyType mode)
827bbb85fb3SSatish Balay {
828bbb85fb3SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
829dfbe8321SBarry Smith   PetscErrorCode ierr;
830b24ad042SBarry Smith   PetscInt       nstash,reallocs;
831bbb85fb3SSatish Balay   InsertMode     addv;
832bbb85fb3SSatish Balay 
833bbb85fb3SSatish Balay   PetscFunctionBegin;
834bbb85fb3SSatish Balay   if (baij->donotstash) {
835bbb85fb3SSatish Balay     PetscFunctionReturn(0);
836bbb85fb3SSatish Balay   }
837bbb85fb3SSatish Balay 
838bbb85fb3SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
8397adad957SLisandro Dalcin   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,((PetscObject)mat)->comm);CHKERRQ(ierr);
840bbb85fb3SSatish Balay   if (addv == (ADD_VALUES|INSERT_VALUES)) {
841*e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
842bbb85fb3SSatish Balay   }
843bbb85fb3SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
844bbb85fb3SSatish Balay 
845d0f46423SBarry Smith   ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr);
8461e2582c4SBarry Smith   ierr = MatStashScatterBegin_Private(mat,&mat->bstash,baij->rangebs);CHKERRQ(ierr);
8478798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
8481e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Stash has %D entries,uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
84946680499SSatish Balay   ierr = MatStashGetInfo_Private(&mat->bstash,&nstash,&reallocs);CHKERRQ(ierr);
8501e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
851bbb85fb3SSatish Balay   PetscFunctionReturn(0);
852bbb85fb3SSatish Balay }
853bbb85fb3SSatish Balay 
8544a2ae208SSatish Balay #undef __FUNCT__
8554a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIBAIJ"
856dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIBAIJ(Mat mat,MatAssemblyType mode)
857bbb85fb3SSatish Balay {
858bbb85fb3SSatish Balay   Mat_MPIBAIJ    *baij=(Mat_MPIBAIJ*)mat->data;
85991c97fd4SSatish Balay   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ*)baij->A->data;
8606849ba73SBarry Smith   PetscErrorCode ierr;
861b24ad042SBarry Smith   PetscInt       i,j,rstart,ncols,flg,bs2=baij->bs2;
862e44c0bd4SBarry Smith   PetscInt       *row,*col;
863e44c0bd4SBarry Smith   PetscTruth     r1,r2,r3,other_disassembled;
8643eda8832SBarry Smith   MatScalar      *val;
865bbb85fb3SSatish Balay   InsertMode     addv = mat->insertmode;
866b24ad042SBarry Smith   PetscMPIInt    n;
867bbb85fb3SSatish Balay 
86891c97fd4SSatish Balay   /* do not use 'b=(Mat_SeqBAIJ*)baij->B->data' as B can be reset in disassembly */
869bbb85fb3SSatish Balay   PetscFunctionBegin;
870bbb85fb3SSatish Balay   if (!baij->donotstash) {
871a2d1c673SSatish Balay     while (1) {
8728798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
873a2d1c673SSatish Balay       if (!flg) break;
874a2d1c673SSatish Balay 
875bbb85fb3SSatish Balay       for (i=0; i<n;) {
876bbb85fb3SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
877bbb85fb3SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
878bbb85fb3SSatish Balay         if (j < n) ncols = j-i;
879bbb85fb3SSatish Balay         else       ncols = n-i;
880bbb85fb3SSatish Balay         /* Now assemble all these values with a single function call */
88197e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
882bbb85fb3SSatish Balay         i = j;
883bbb85fb3SSatish Balay       }
884bbb85fb3SSatish Balay     }
8858798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
886a2d1c673SSatish Balay     /* Now process the block-stash. Since the values are stashed column-oriented,
887a2d1c673SSatish Balay        set the roworiented flag to column oriented, and after MatSetValues()
888a2d1c673SSatish Balay        restore the original flags */
889a2d1c673SSatish Balay     r1 = baij->roworiented;
890a2d1c673SSatish Balay     r2 = a->roworiented;
89191c97fd4SSatish Balay     r3 = ((Mat_SeqBAIJ*)baij->B->data)->roworiented;
8927c922b88SBarry Smith     baij->roworiented = PETSC_FALSE;
8937c922b88SBarry Smith     a->roworiented    = PETSC_FALSE;
89491c97fd4SSatish Balay     (((Mat_SeqBAIJ*)baij->B->data))->roworiented    = PETSC_FALSE; /* b->roworiented */
895a2d1c673SSatish Balay     while (1) {
8968798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->bstash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
897a2d1c673SSatish Balay       if (!flg) break;
898a2d1c673SSatish Balay 
899a2d1c673SSatish Balay       for (i=0; i<n;) {
900a2d1c673SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
901a2d1c673SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
902a2d1c673SSatish Balay         if (j < n) ncols = j-i;
903a2d1c673SSatish Balay         else       ncols = n-i;
90497e5c40aSBarry Smith         ierr = MatSetValuesBlocked_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i*bs2,addv);CHKERRQ(ierr);
905a2d1c673SSatish Balay         i = j;
906a2d1c673SSatish Balay       }
907a2d1c673SSatish Balay     }
9088798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->bstash);CHKERRQ(ierr);
909a2d1c673SSatish Balay     baij->roworiented = r1;
910a2d1c673SSatish Balay     a->roworiented    = r2;
91191c97fd4SSatish Balay     ((Mat_SeqBAIJ*)baij->B->data)->roworiented    = r3; /* b->roworiented */
912bbb85fb3SSatish Balay   }
913bbb85fb3SSatish Balay 
914bbb85fb3SSatish Balay   ierr = MatAssemblyBegin(baij->A,mode);CHKERRQ(ierr);
915bbb85fb3SSatish Balay   ierr = MatAssemblyEnd(baij->A,mode);CHKERRQ(ierr);
916bbb85fb3SSatish Balay 
917bbb85fb3SSatish Balay   /* determine if any processor has disassembled, if so we must
918bbb85fb3SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
919bbb85fb3SSatish Balay   /*
920bbb85fb3SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
921bbb85fb3SSatish Balay      no processor disassembled thus we can skip this stuff
922bbb85fb3SSatish Balay   */
923bbb85fb3SSatish Balay   if (!((Mat_SeqBAIJ*)baij->B->data)->nonew)  {
9247adad957SLisandro Dalcin     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,((PetscObject)mat)->comm);CHKERRQ(ierr);
925bbb85fb3SSatish Balay     if (mat->was_assembled && !other_disassembled) {
926bbb85fb3SSatish Balay       ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
927bbb85fb3SSatish Balay     }
928bbb85fb3SSatish Balay   }
929bbb85fb3SSatish Balay 
930bbb85fb3SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
931bbb85fb3SSatish Balay     ierr = MatSetUpMultiply_MPIBAIJ(mat);CHKERRQ(ierr);
932bbb85fb3SSatish Balay   }
93391c97fd4SSatish Balay   ((Mat_SeqBAIJ*)baij->B->data)->compressedrow.use = PETSC_TRUE; /* b->compressedrow.use */
934bbb85fb3SSatish Balay   ierr = MatAssemblyBegin(baij->B,mode);CHKERRQ(ierr);
935bbb85fb3SSatish Balay   ierr = MatAssemblyEnd(baij->B,mode);CHKERRQ(ierr);
936bbb85fb3SSatish Balay 
9376cf91177SBarry Smith #if defined(PETSC_USE_INFO)
938bbb85fb3SSatish Balay   if (baij->ht && mode== MAT_FINAL_ASSEMBLY) {
9391e2582c4SBarry Smith     ierr = PetscInfo1(mat,"Average Hash Table Search in MatSetValues = %5.2f\n",((PetscReal)baij->ht_total_ct)/baij->ht_insert_ct);CHKERRQ(ierr);
940bbb85fb3SSatish Balay     baij->ht_total_ct  = 0;
941bbb85fb3SSatish Balay     baij->ht_insert_ct = 0;
942bbb85fb3SSatish Balay   }
943bbb85fb3SSatish Balay #endif
944bbb85fb3SSatish Balay   if (baij->ht_flag && !baij->ht && mode == MAT_FINAL_ASSEMBLY) {
945bbb85fb3SSatish Balay     ierr = MatCreateHashTable_MPIBAIJ_Private(mat,baij->ht_fact);CHKERRQ(ierr);
946bbb85fb3SSatish Balay     mat->ops->setvalues        = MatSetValues_MPIBAIJ_HT;
947bbb85fb3SSatish Balay     mat->ops->setvaluesblocked = MatSetValuesBlocked_MPIBAIJ_HT;
948bbb85fb3SSatish Balay   }
949bbb85fb3SSatish Balay 
950fca92195SBarry Smith   ierr = PetscFree2(baij->rowvalues,baij->rowindices);CHKERRQ(ierr);
951606d414cSSatish Balay   baij->rowvalues = 0;
952bbb85fb3SSatish Balay   PetscFunctionReturn(0);
953bbb85fb3SSatish Balay }
95457b952d6SSatish Balay 
9554a2ae208SSatish Balay #undef __FUNCT__
9564a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIBAIJ_ASCIIorDraworSocket"
9576849ba73SBarry Smith static PetscErrorCode MatView_MPIBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
95857b952d6SSatish Balay {
95957b952d6SSatish Balay   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
960dfbe8321SBarry Smith   PetscErrorCode    ierr;
961b24ad042SBarry Smith   PetscMPIInt       size = baij->size,rank = baij->rank;
962d0f46423SBarry Smith   PetscInt          bs = mat->rmap->bs;
96332077d6dSBarry Smith   PetscTruth        iascii,isdraw;
964b0a32e0cSBarry Smith   PetscViewer       sviewer;
965f3ef73ceSBarry Smith   PetscViewerFormat format;
96657b952d6SSatish Balay 
967d64ed03dSBarry Smith   PetscFunctionBegin;
96832077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
969fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
97032077d6dSBarry Smith   if (iascii) {
971b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
972456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
9734e220ebcSLois Curfman McInnes       MatInfo info;
9747adad957SLisandro Dalcin       ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr);
975d41123aaSBarry Smith       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
97677431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D bs %D mem %D\n",
977d0f46423SBarry Smith               rank,mat->rmap->N,(PetscInt)info.nz_used*bs,(PetscInt)info.nz_allocated*bs,
978d0f46423SBarry Smith               mat->rmap->bs,(PetscInt)info.memory);CHKERRQ(ierr);
979d132466eSBarry Smith       ierr = MatGetInfo(baij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
98077431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used*bs);CHKERRQ(ierr);
981d132466eSBarry Smith       ierr = MatGetInfo(baij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
98277431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used*bs);CHKERRQ(ierr);
983b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
98407d81ca4SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr);
98557b952d6SSatish Balay       ierr = VecScatterView(baij->Mvctx,viewer);CHKERRQ(ierr);
9863a40ed3dSBarry Smith       PetscFunctionReturn(0);
987fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
98877431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);CHKERRQ(ierr);
9893a40ed3dSBarry Smith       PetscFunctionReturn(0);
99004929863SHong Zhang     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
99104929863SHong Zhang       PetscFunctionReturn(0);
99257b952d6SSatish Balay     }
99357b952d6SSatish Balay   }
99457b952d6SSatish Balay 
9950f5bd95cSBarry Smith   if (isdraw) {
996b0a32e0cSBarry Smith     PetscDraw       draw;
99757b952d6SSatish Balay     PetscTruth isnull;
998b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
999b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
100057b952d6SSatish Balay   }
100157b952d6SSatish Balay 
100257b952d6SSatish Balay   if (size == 1) {
10037adad957SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)baij->A,((PetscObject)mat)->name);CHKERRQ(ierr);
100457b952d6SSatish Balay     ierr = MatView(baij->A,viewer);CHKERRQ(ierr);
1005d64ed03dSBarry Smith   } else {
100657b952d6SSatish Balay     /* assemble the entire matrix onto first processor. */
100757b952d6SSatish Balay     Mat         A;
100857b952d6SSatish Balay     Mat_SeqBAIJ *Aloc;
1009d0f46423SBarry Smith     PetscInt    M = mat->rmap->N,N = mat->cmap->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs;
10103eda8832SBarry Smith     MatScalar   *a;
101157b952d6SSatish Balay 
1012f204ca49SKris Buschelman     /* Here we are creating a temporary matrix, so will assume MPIBAIJ is acceptable */
1013f204ca49SKris Buschelman     /* Perhaps this should be the type of mat? */
10147adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)mat)->comm,&A);CHKERRQ(ierr);
101557b952d6SSatish Balay     if (!rank) {
1016f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr);
1017d64ed03dSBarry Smith     } else {
1018f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr);
101957b952d6SSatish Balay     }
1020f204ca49SKris Buschelman     ierr = MatSetType(A,MATMPIBAIJ);CHKERRQ(ierr);
1021d0f46423SBarry Smith     ierr = MatMPIBAIJSetPreallocation(A,mat->rmap->bs,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
102252e6d16bSBarry Smith     ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr);
102357b952d6SSatish Balay 
102457b952d6SSatish Balay     /* copy over the A part */
102557b952d6SSatish Balay     Aloc = (Mat_SeqBAIJ*)baij->A->data;
102657b952d6SSatish Balay     ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1027b24ad042SBarry Smith     ierr = PetscMalloc(bs*sizeof(PetscInt),&rvals);CHKERRQ(ierr);
102857b952d6SSatish Balay 
102957b952d6SSatish Balay     for (i=0; i<mbs; i++) {
1030899cda47SBarry Smith       rvals[0] = bs*(baij->rstartbs + i);
103157b952d6SSatish Balay       for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
103257b952d6SSatish Balay       for (j=ai[i]; j<ai[i+1]; j++) {
1033899cda47SBarry Smith         col = (baij->cstartbs+aj[j])*bs;
103457b952d6SSatish Balay         for (k=0; k<bs; k++) {
103597e5c40aSBarry Smith           ierr = MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);CHKERRQ(ierr);
1036cee3aa6bSSatish Balay           col++; a += bs;
103757b952d6SSatish Balay         }
103857b952d6SSatish Balay       }
103957b952d6SSatish Balay     }
104057b952d6SSatish Balay     /* copy over the B part */
104157b952d6SSatish Balay     Aloc = (Mat_SeqBAIJ*)baij->B->data;
104257b952d6SSatish Balay     ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
104357b952d6SSatish Balay     for (i=0; i<mbs; i++) {
1044899cda47SBarry Smith       rvals[0] = bs*(baij->rstartbs + i);
104557b952d6SSatish Balay       for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
104657b952d6SSatish Balay       for (j=ai[i]; j<ai[i+1]; j++) {
104757b952d6SSatish Balay         col = baij->garray[aj[j]]*bs;
104857b952d6SSatish Balay         for (k=0; k<bs; k++) {
104997e5c40aSBarry Smith           ierr = MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);CHKERRQ(ierr);
1050cee3aa6bSSatish Balay           col++; a += bs;
105157b952d6SSatish Balay         }
105257b952d6SSatish Balay       }
105357b952d6SSatish Balay     }
1054606d414cSSatish Balay     ierr = PetscFree(rvals);CHKERRQ(ierr);
10556d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10566d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
105755843e3eSBarry Smith     /*
105855843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
1059b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
106055843e3eSBarry Smith     */
1061b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
1062f14a1c24SBarry Smith     if (!rank) {
10637adad957SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject)((Mat_MPIBAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr);
10646831982aSBarry Smith       ierr = MatView(((Mat_MPIBAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
106557b952d6SSatish Balay     }
1066b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
106757b952d6SSatish Balay     ierr = MatDestroy(A);CHKERRQ(ierr);
106857b952d6SSatish Balay   }
10693a40ed3dSBarry Smith   PetscFunctionReturn(0);
107057b952d6SSatish Balay }
107157b952d6SSatish Balay 
10724a2ae208SSatish Balay #undef __FUNCT__
10734a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIBAIJ"
1074dfbe8321SBarry Smith PetscErrorCode MatView_MPIBAIJ(Mat mat,PetscViewer viewer)
107557b952d6SSatish Balay {
1076dfbe8321SBarry Smith   PetscErrorCode ierr;
107732077d6dSBarry Smith   PetscTruth     iascii,isdraw,issocket,isbinary;
107857b952d6SSatish Balay 
1079d64ed03dSBarry Smith   PetscFunctionBegin;
108032077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
1081fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
1082b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr);
1083fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
108432077d6dSBarry Smith   if (iascii || isdraw || issocket || isbinary) {
10857b2a1423SBarry Smith     ierr = MatView_MPIBAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
10865cd90555SBarry Smith   } else {
1087*e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by MPIBAIJ matrices",((PetscObject)viewer)->type_name);
108857b952d6SSatish Balay   }
10893a40ed3dSBarry Smith   PetscFunctionReturn(0);
109057b952d6SSatish Balay }
109157b952d6SSatish Balay 
10924a2ae208SSatish Balay #undef __FUNCT__
10934a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIBAIJ"
1094dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIBAIJ(Mat mat)
109579bdfe76SSatish Balay {
109679bdfe76SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
1097dfbe8321SBarry Smith   PetscErrorCode ierr;
109879bdfe76SSatish Balay 
1099d64ed03dSBarry Smith   PetscFunctionBegin;
1100aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1101d0f46423SBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%D,Cols=%D",mat->rmap->N,mat->cmap->N);
110279bdfe76SSatish Balay #endif
11038798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
11048798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->bstash);CHKERRQ(ierr);
110579bdfe76SSatish Balay   ierr = MatDestroy(baij->A);CHKERRQ(ierr);
110679bdfe76SSatish Balay   ierr = MatDestroy(baij->B);CHKERRQ(ierr);
1107aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
11089c666560SBarry Smith   if (baij->colmap) {ierr = PetscTableDestroy(baij->colmap);CHKERRQ(ierr);}
110948e59246SSatish Balay #else
111005b42c5fSBarry Smith   ierr = PetscFree(baij->colmap);CHKERRQ(ierr);
111148e59246SSatish Balay #endif
111205b42c5fSBarry Smith   ierr = PetscFree(baij->garray);CHKERRQ(ierr);
1113606d414cSSatish Balay   if (baij->lvec)   {ierr = VecDestroy(baij->lvec);CHKERRQ(ierr);}
1114606d414cSSatish Balay   if (baij->Mvctx)  {ierr = VecScatterDestroy(baij->Mvctx);CHKERRQ(ierr);}
1115fca92195SBarry Smith   ierr = PetscFree2(baij->rowvalues,baij->rowindices);CHKERRQ(ierr);
111605b42c5fSBarry Smith   ierr = PetscFree(baij->barray);CHKERRQ(ierr);
1117fca92195SBarry Smith   ierr = PetscFree2(baij->hd,baij->ht);CHKERRQ(ierr);
1118899cda47SBarry Smith   ierr = PetscFree(baij->rangebs);CHKERRQ(ierr);
1119606d414cSSatish Balay   ierr = PetscFree(baij);CHKERRQ(ierr);
1120901853e0SKris Buschelman 
1121dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr);
1122901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
1123901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
1124901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);CHKERRQ(ierr);
1125901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
1126aac34f13SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
1127901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);CHKERRQ(ierr);
1128901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatSetHashTableFactor_C","",PETSC_NULL);CHKERRQ(ierr);
11293a40ed3dSBarry Smith   PetscFunctionReturn(0);
113079bdfe76SSatish Balay }
113179bdfe76SSatish Balay 
11324a2ae208SSatish Balay #undef __FUNCT__
11334a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIBAIJ"
1134dfbe8321SBarry Smith PetscErrorCode MatMult_MPIBAIJ(Mat A,Vec xx,Vec yy)
1135cee3aa6bSSatish Balay {
1136cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1137dfbe8321SBarry Smith   PetscErrorCode ierr;
1138b24ad042SBarry Smith   PetscInt       nt;
1139cee3aa6bSSatish Balay 
1140d64ed03dSBarry Smith   PetscFunctionBegin;
1141e1311b90SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
1142d0f46423SBarry Smith   if (nt != A->cmap->n) {
1143*e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");
114447b4a8eaSLois Curfman McInnes   }
1145e1311b90SBarry Smith   ierr = VecGetLocalSize(yy,&nt);CHKERRQ(ierr);
1146d0f46423SBarry Smith   if (nt != A->rmap->n) {
1147*e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible parition of A and yy");
114847b4a8eaSLois Curfman McInnes   }
1149ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1150f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
1151ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1152f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
11533a40ed3dSBarry Smith   PetscFunctionReturn(0);
1154cee3aa6bSSatish Balay }
1155cee3aa6bSSatish Balay 
11564a2ae208SSatish Balay #undef __FUNCT__
11574a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIBAIJ"
1158dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1159cee3aa6bSSatish Balay {
1160cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1161dfbe8321SBarry Smith   PetscErrorCode ierr;
1162d64ed03dSBarry Smith 
1163d64ed03dSBarry Smith   PetscFunctionBegin;
1164ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1165f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1166ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1167f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
11683a40ed3dSBarry Smith   PetscFunctionReturn(0);
1169cee3aa6bSSatish Balay }
1170cee3aa6bSSatish Balay 
11714a2ae208SSatish Balay #undef __FUNCT__
11724a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIBAIJ"
1173dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIBAIJ(Mat A,Vec xx,Vec yy)
1174cee3aa6bSSatish Balay {
1175cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1176dfbe8321SBarry Smith   PetscErrorCode ierr;
1177a5ff213dSBarry Smith   PetscTruth     merged;
1178cee3aa6bSSatish Balay 
1179d64ed03dSBarry Smith   PetscFunctionBegin;
1180a5ff213dSBarry Smith   ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr);
1181cee3aa6bSSatish Balay   /* do nondiagonal part */
11827c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1183a5ff213dSBarry Smith   if (!merged) {
1184cee3aa6bSSatish Balay     /* send it on its way */
1185ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1186cee3aa6bSSatish Balay     /* do local part */
11877c922b88SBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1188cee3aa6bSSatish Balay     /* receive remote parts: note this assumes the values are not actually */
1189a5ff213dSBarry Smith     /* inserted in yy until the next line */
1190ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1191a5ff213dSBarry Smith   } else {
1192a5ff213dSBarry Smith     /* do local part */
1193a5ff213dSBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1194a5ff213dSBarry Smith     /* send it on its way */
1195ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1196a5ff213dSBarry Smith     /* values actually were received in the Begin() but we need to call this nop */
1197ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1198a5ff213dSBarry Smith   }
11993a40ed3dSBarry Smith   PetscFunctionReturn(0);
1200cee3aa6bSSatish Balay }
1201cee3aa6bSSatish Balay 
12024a2ae208SSatish Balay #undef __FUNCT__
12034a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIBAIJ"
1204dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1205cee3aa6bSSatish Balay {
1206cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1207dfbe8321SBarry Smith   PetscErrorCode ierr;
1208cee3aa6bSSatish Balay 
1209d64ed03dSBarry Smith   PetscFunctionBegin;
1210cee3aa6bSSatish Balay   /* do nondiagonal part */
12117c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1212cee3aa6bSSatish Balay   /* send it on its way */
1213ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1214cee3aa6bSSatish Balay   /* do local part */
12157c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1216cee3aa6bSSatish Balay   /* receive remote parts: note this assumes the values are not actually */
1217cee3aa6bSSatish Balay   /* inserted in yy until the next line, which is true for my implementation*/
1218cee3aa6bSSatish Balay   /* but is not perhaps always true. */
1219ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
12203a40ed3dSBarry Smith   PetscFunctionReturn(0);
1221cee3aa6bSSatish Balay }
1222cee3aa6bSSatish Balay 
1223cee3aa6bSSatish Balay /*
1224cee3aa6bSSatish Balay   This only works correctly for square matrices where the subblock A->A is the
1225cee3aa6bSSatish Balay    diagonal block
1226cee3aa6bSSatish Balay */
12274a2ae208SSatish Balay #undef __FUNCT__
12284a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIBAIJ"
1229dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIBAIJ(Mat A,Vec v)
1230cee3aa6bSSatish Balay {
1231cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1232dfbe8321SBarry Smith   PetscErrorCode ierr;
1233d64ed03dSBarry Smith 
1234d64ed03dSBarry Smith   PetscFunctionBegin;
1235*e32f2f54SBarry 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");
12363a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
12373a40ed3dSBarry Smith   PetscFunctionReturn(0);
1238cee3aa6bSSatish Balay }
1239cee3aa6bSSatish Balay 
12404a2ae208SSatish Balay #undef __FUNCT__
12414a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIBAIJ"
1242f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIBAIJ(Mat A,PetscScalar aa)
1243cee3aa6bSSatish Balay {
1244cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1245dfbe8321SBarry Smith   PetscErrorCode ierr;
1246d64ed03dSBarry Smith 
1247d64ed03dSBarry Smith   PetscFunctionBegin;
1248f4df32b1SMatthew Knepley   ierr = MatScale(a->A,aa);CHKERRQ(ierr);
1249f4df32b1SMatthew Knepley   ierr = MatScale(a->B,aa);CHKERRQ(ierr);
12503a40ed3dSBarry Smith   PetscFunctionReturn(0);
1251cee3aa6bSSatish Balay }
1252026e39d0SSatish Balay 
12534a2ae208SSatish Balay #undef __FUNCT__
12544a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIBAIJ"
1255b24ad042SBarry Smith PetscErrorCode MatGetRow_MPIBAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1256acdf5bf4SSatish Balay {
1257acdf5bf4SSatish Balay   Mat_MPIBAIJ    *mat = (Mat_MPIBAIJ*)matin->data;
125887828ca2SBarry Smith   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
12596849ba73SBarry Smith   PetscErrorCode ierr;
1260d0f46423SBarry Smith   PetscInt       bs = matin->rmap->bs,bs2 = mat->bs2,i,*cworkA,*cworkB,**pcA,**pcB;
1261d0f46423SBarry Smith   PetscInt       nztot,nzA,nzB,lrow,brstart = matin->rmap->rstart,brend = matin->rmap->rend;
1262899cda47SBarry Smith   PetscInt       *cmap,*idx_p,cstart = mat->cstartbs;
1263acdf5bf4SSatish Balay 
1264d64ed03dSBarry Smith   PetscFunctionBegin;
1265*e32f2f54SBarry Smith   if (row < brstart || row >= brend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local rows")
1266*e32f2f54SBarry Smith   if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active");
1267acdf5bf4SSatish Balay   mat->getrowactive = PETSC_TRUE;
1268acdf5bf4SSatish Balay 
1269acdf5bf4SSatish Balay   if (!mat->rowvalues && (idx || v)) {
1270acdf5bf4SSatish Balay     /*
1271acdf5bf4SSatish Balay         allocate enough space to hold information from the longest row.
1272acdf5bf4SSatish Balay     */
1273acdf5bf4SSatish Balay     Mat_SeqBAIJ *Aa = (Mat_SeqBAIJ*)mat->A->data,*Ba = (Mat_SeqBAIJ*)mat->B->data;
1274b24ad042SBarry Smith     PetscInt     max = 1,mbs = mat->mbs,tmp;
1275bd16c2feSSatish Balay     for (i=0; i<mbs; i++) {
1276acdf5bf4SSatish Balay       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1277acdf5bf4SSatish Balay       if (max < tmp) { max = tmp; }
1278acdf5bf4SSatish Balay     }
1279fca92195SBarry Smith     ierr = PetscMalloc2(max*bs2,PetscScalar,&mat->rowvalues,max*bs2,PetscInt,&mat->rowindices);CHKERRQ(ierr);
1280acdf5bf4SSatish Balay   }
1281d9d09a02SSatish Balay   lrow = row - brstart;
1282acdf5bf4SSatish Balay 
1283acdf5bf4SSatish Balay   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1284acdf5bf4SSatish Balay   if (!v)   {pvA = 0; pvB = 0;}
1285acdf5bf4SSatish Balay   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1286f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1287f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1288acdf5bf4SSatish Balay   nztot = nzA + nzB;
1289acdf5bf4SSatish Balay 
1290acdf5bf4SSatish Balay   cmap  = mat->garray;
1291acdf5bf4SSatish Balay   if (v  || idx) {
1292acdf5bf4SSatish Balay     if (nztot) {
1293acdf5bf4SSatish Balay       /* Sort by increasing column numbers, assuming A and B already sorted */
1294b24ad042SBarry Smith       PetscInt imark = -1;
1295acdf5bf4SSatish Balay       if (v) {
1296acdf5bf4SSatish Balay         *v = v_p = mat->rowvalues;
1297acdf5bf4SSatish Balay         for (i=0; i<nzB; i++) {
1298d9d09a02SSatish Balay           if (cmap[cworkB[i]/bs] < cstart)   v_p[i] = vworkB[i];
1299acdf5bf4SSatish Balay           else break;
1300acdf5bf4SSatish Balay         }
1301acdf5bf4SSatish Balay         imark = i;
1302acdf5bf4SSatish Balay         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1303acdf5bf4SSatish Balay         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1304acdf5bf4SSatish Balay       }
1305acdf5bf4SSatish Balay       if (idx) {
1306acdf5bf4SSatish Balay         *idx = idx_p = mat->rowindices;
1307acdf5bf4SSatish Balay         if (imark > -1) {
1308acdf5bf4SSatish Balay           for (i=0; i<imark; i++) {
1309bd16c2feSSatish Balay             idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1310acdf5bf4SSatish Balay           }
1311acdf5bf4SSatish Balay         } else {
1312acdf5bf4SSatish Balay           for (i=0; i<nzB; i++) {
1313d9d09a02SSatish Balay             if (cmap[cworkB[i]/bs] < cstart)
1314d9d09a02SSatish Balay               idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1315acdf5bf4SSatish Balay             else break;
1316acdf5bf4SSatish Balay           }
1317acdf5bf4SSatish Balay           imark = i;
1318acdf5bf4SSatish Balay         }
1319d9d09a02SSatish Balay         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart*bs + cworkA[i];
1320d9d09a02SSatish Balay         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1321acdf5bf4SSatish Balay       }
1322d64ed03dSBarry Smith     } else {
1323d212a18eSSatish Balay       if (idx) *idx = 0;
1324d212a18eSSatish Balay       if (v)   *v   = 0;
1325d212a18eSSatish Balay     }
1326acdf5bf4SSatish Balay   }
1327acdf5bf4SSatish Balay   *nz = nztot;
1328f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1329f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
13303a40ed3dSBarry Smith   PetscFunctionReturn(0);
1331acdf5bf4SSatish Balay }
1332acdf5bf4SSatish Balay 
13334a2ae208SSatish Balay #undef __FUNCT__
13344a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIBAIJ"
1335b24ad042SBarry Smith PetscErrorCode MatRestoreRow_MPIBAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1336acdf5bf4SSatish Balay {
1337acdf5bf4SSatish Balay   Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1338d64ed03dSBarry Smith 
1339d64ed03dSBarry Smith   PetscFunctionBegin;
1340abc0a331SBarry Smith   if (!baij->getrowactive) {
1341*e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
1342acdf5bf4SSatish Balay   }
1343acdf5bf4SSatish Balay   baij->getrowactive = PETSC_FALSE;
13443a40ed3dSBarry Smith   PetscFunctionReturn(0);
1345acdf5bf4SSatish Balay }
1346acdf5bf4SSatish Balay 
13474a2ae208SSatish Balay #undef __FUNCT__
13484a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIBAIJ"
1349dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIBAIJ(Mat A)
135058667388SSatish Balay {
135158667388SSatish Balay   Mat_MPIBAIJ    *l = (Mat_MPIBAIJ*)A->data;
1352dfbe8321SBarry Smith   PetscErrorCode ierr;
1353d64ed03dSBarry Smith 
1354d64ed03dSBarry Smith   PetscFunctionBegin;
135558667388SSatish Balay   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
135658667388SSatish Balay   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
13573a40ed3dSBarry Smith   PetscFunctionReturn(0);
135858667388SSatish Balay }
13590ac07820SSatish Balay 
13604a2ae208SSatish Balay #undef __FUNCT__
13614a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIBAIJ"
1362dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIBAIJ(Mat matin,MatInfoType flag,MatInfo *info)
13630ac07820SSatish Balay {
13644e220ebcSLois Curfman McInnes   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)matin->data;
13654e220ebcSLois Curfman McInnes   Mat            A = a->A,B = a->B;
1366dfbe8321SBarry Smith   PetscErrorCode ierr;
1367329f5518SBarry Smith   PetscReal      isend[5],irecv[5];
13680ac07820SSatish Balay 
1369d64ed03dSBarry Smith   PetscFunctionBegin;
1370d0f46423SBarry Smith   info->block_size     = (PetscReal)matin->rmap->bs;
13714e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
13720e4b21beSBarry Smith   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1373de87f314SBarry Smith   isend[3] = info->memory;  isend[4] = info->mallocs;
13744e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
13750e4b21beSBarry Smith   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1376de87f314SBarry Smith   isend[3] += info->memory;  isend[4] += info->mallocs;
13770ac07820SSatish Balay   if (flag == MAT_LOCAL) {
13784e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
13794e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
13804e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
13814e220ebcSLois Curfman McInnes     info->memory       = isend[3];
13824e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
13830ac07820SSatish Balay   } else if (flag == MAT_GLOBAL_MAX) {
13847adad957SLisandro Dalcin     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,((PetscObject)matin)->comm);CHKERRQ(ierr);
13854e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
13864e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
13874e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
13884e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
13894e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
13900ac07820SSatish Balay   } else if (flag == MAT_GLOBAL_SUM) {
13917adad957SLisandro Dalcin     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,((PetscObject)matin)->comm);CHKERRQ(ierr);
13924e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
13934e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
13944e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
13954e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
13964e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1397d41123aaSBarry Smith   } else {
1398*e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown MatInfoType argument %d",(int)flag);
13990ac07820SSatish Balay   }
14004e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
14014e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
14024e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
14033a40ed3dSBarry Smith   PetscFunctionReturn(0);
14040ac07820SSatish Balay }
14050ac07820SSatish Balay 
14064a2ae208SSatish Balay #undef __FUNCT__
14074a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIBAIJ"
14084e0d8c25SBarry Smith PetscErrorCode MatSetOption_MPIBAIJ(Mat A,MatOption op,PetscTruth flg)
140958667388SSatish Balay {
141058667388SSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1411dfbe8321SBarry Smith   PetscErrorCode ierr;
141258667388SSatish Balay 
1413d64ed03dSBarry Smith   PetscFunctionBegin;
141412c028f9SKris Buschelman   switch (op) {
1415512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
141612c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
141728b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1418a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
141912c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
14204e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
14214e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
142212c028f9SKris Buschelman     break;
142312c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
14244e0d8c25SBarry Smith     a->roworiented = flg;
14254e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
14264e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
142712c028f9SKris Buschelman     break;
14284e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1429290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
143012c028f9SKris Buschelman     break;
143112c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
14324e0d8c25SBarry Smith     a->donotstash = flg;
143312c028f9SKris Buschelman     break;
143412c028f9SKris Buschelman   case MAT_USE_HASH_TABLE:
14354e0d8c25SBarry Smith     a->ht_flag = flg;
143612c028f9SKris Buschelman     break;
143777e54ba9SKris Buschelman   case MAT_SYMMETRIC:
143877e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
14392188ac68SBarry Smith   case MAT_HERMITIAN:
14402188ac68SBarry Smith   case MAT_SYMMETRY_ETERNAL:
14414e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
144277e54ba9SKris Buschelman     break;
144312c028f9SKris Buschelman   default:
1444*e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1445d64ed03dSBarry Smith   }
14463a40ed3dSBarry Smith   PetscFunctionReturn(0);
144758667388SSatish Balay }
144858667388SSatish Balay 
14494a2ae208SSatish Balay #undef __FUNCT__
14504a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIBAIJ("
1451fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIBAIJ(Mat A,MatReuse reuse,Mat *matout)
14520ac07820SSatish Balay {
14530ac07820SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)A->data;
14540ac07820SSatish Balay   Mat_SeqBAIJ    *Aloc;
14550ac07820SSatish Balay   Mat            B;
1456dfbe8321SBarry Smith   PetscErrorCode ierr;
1457d0f46423SBarry Smith   PetscInt       M=A->rmap->N,N=A->cmap->N,*ai,*aj,i,*rvals,j,k,col;
1458d0f46423SBarry Smith   PetscInt       bs=A->rmap->bs,mbs=baij->mbs;
14593eda8832SBarry Smith   MatScalar      *a;
14600ac07820SSatish Balay 
1461d64ed03dSBarry Smith   PetscFunctionBegin;
1462*e32f2f54SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1463fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
14647adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr);
1465d0f46423SBarry Smith     ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr);
14667adad957SLisandro Dalcin     ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr);
1467d0f46423SBarry Smith     ierr = MatMPIBAIJSetPreallocation(B,A->rmap->bs,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
1468fc4dec0aSBarry Smith   } else {
1469fc4dec0aSBarry Smith     B = *matout;
1470fc4dec0aSBarry Smith   }
14710ac07820SSatish Balay 
14720ac07820SSatish Balay   /* copy over the A part */
14730ac07820SSatish Balay   Aloc = (Mat_SeqBAIJ*)baij->A->data;
14740ac07820SSatish Balay   ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1475b24ad042SBarry Smith   ierr = PetscMalloc(bs*sizeof(PetscInt),&rvals);CHKERRQ(ierr);
14760ac07820SSatish Balay 
14770ac07820SSatish Balay   for (i=0; i<mbs; i++) {
1478899cda47SBarry Smith     rvals[0] = bs*(baij->rstartbs + i);
14790ac07820SSatish Balay     for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
14800ac07820SSatish Balay     for (j=ai[i]; j<ai[i+1]; j++) {
1481899cda47SBarry Smith       col = (baij->cstartbs+aj[j])*bs;
14820ac07820SSatish Balay       for (k=0; k<bs; k++) {
148397e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);CHKERRQ(ierr);
14840ac07820SSatish Balay         col++; a += bs;
14850ac07820SSatish Balay       }
14860ac07820SSatish Balay     }
14870ac07820SSatish Balay   }
14880ac07820SSatish Balay   /* copy over the B part */
14890ac07820SSatish Balay   Aloc = (Mat_SeqBAIJ*)baij->B->data;
14900ac07820SSatish Balay   ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
14910ac07820SSatish Balay   for (i=0; i<mbs; i++) {
1492899cda47SBarry Smith     rvals[0] = bs*(baij->rstartbs + i);
14930ac07820SSatish Balay     for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
14940ac07820SSatish Balay     for (j=ai[i]; j<ai[i+1]; j++) {
14950ac07820SSatish Balay       col = baij->garray[aj[j]]*bs;
14960ac07820SSatish Balay       for (k=0; k<bs; k++) {
149797e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);CHKERRQ(ierr);
14980ac07820SSatish Balay         col++; a += bs;
14990ac07820SSatish Balay       }
15000ac07820SSatish Balay     }
15010ac07820SSatish Balay   }
1502606d414cSSatish Balay   ierr = PetscFree(rvals);CHKERRQ(ierr);
15030ac07820SSatish Balay   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15040ac07820SSatish Balay   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15050ac07820SSatish Balay 
1506815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout != A) {
15070ac07820SSatish Balay     *matout = B;
15080ac07820SSatish Balay   } else {
1509273d9f13SBarry Smith     ierr = MatHeaderCopy(A,B);CHKERRQ(ierr);
15100ac07820SSatish Balay   }
15113a40ed3dSBarry Smith   PetscFunctionReturn(0);
15120ac07820SSatish Balay }
15130e95ebc0SSatish Balay 
15144a2ae208SSatish Balay #undef __FUNCT__
15154a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIBAIJ"
1516dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIBAIJ(Mat mat,Vec ll,Vec rr)
15170e95ebc0SSatish Balay {
151836c4a09eSSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
151936c4a09eSSatish Balay   Mat            a = baij->A,b = baij->B;
1520dfbe8321SBarry Smith   PetscErrorCode ierr;
1521b24ad042SBarry Smith   PetscInt       s1,s2,s3;
15220e95ebc0SSatish Balay 
1523d64ed03dSBarry Smith   PetscFunctionBegin;
152436c4a09eSSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
152536c4a09eSSatish Balay   if (rr) {
152636c4a09eSSatish Balay     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
1527*e32f2f54SBarry Smith     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
152836c4a09eSSatish Balay     /* Overlap communication with computation. */
1529ca9f406cSSatish Balay     ierr = VecScatterBegin(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
153036c4a09eSSatish Balay   }
15310e95ebc0SSatish Balay   if (ll) {
15320e95ebc0SSatish Balay     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
1533*e32f2f54SBarry Smith     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1534a21fb8cbSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,PETSC_NULL);CHKERRQ(ierr);
15350e95ebc0SSatish Balay   }
153636c4a09eSSatish Balay   /* scale  the diagonal block */
153736c4a09eSSatish Balay   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
153836c4a09eSSatish Balay 
153936c4a09eSSatish Balay   if (rr) {
154036c4a09eSSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
1541ca9f406cSSatish Balay     ierr = VecScatterEnd(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1542a21fb8cbSBarry Smith     ierr = (*b->ops->diagonalscale)(b,PETSC_NULL,baij->lvec);CHKERRQ(ierr);
154336c4a09eSSatish Balay   }
154436c4a09eSSatish Balay 
15453a40ed3dSBarry Smith   PetscFunctionReturn(0);
15460e95ebc0SSatish Balay }
15470e95ebc0SSatish Balay 
15484a2ae208SSatish Balay #undef __FUNCT__
15494a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIBAIJ"
1550f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_MPIBAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
15510ac07820SSatish Balay {
15520ac07820SSatish Balay   Mat_MPIBAIJ    *l = (Mat_MPIBAIJ*)A->data;
15536849ba73SBarry Smith   PetscErrorCode ierr;
1554b24ad042SBarry Smith   PetscMPIInt    imdex,size = l->size,n,rank = l->rank;
1555d0f46423SBarry Smith   PetscInt       i,*owners = A->rmap->range;
1556b24ad042SBarry Smith   PetscInt       *nprocs,j,idx,nsends,row;
1557b24ad042SBarry Smith   PetscInt       nmax,*svalues,*starts,*owner,nrecvs;
15587adad957SLisandro Dalcin   PetscInt       *rvalues,tag = ((PetscObject)A)->tag,count,base,slen,*source,lastidx = -1;
1559d0f46423SBarry Smith   PetscInt       *lens,*lrows,*values,rstart_bs=A->rmap->rstart;
15607adad957SLisandro Dalcin   MPI_Comm       comm = ((PetscObject)A)->comm;
15610ac07820SSatish Balay   MPI_Request    *send_waits,*recv_waits;
15620ac07820SSatish Balay   MPI_Status     recv_status,*send_status;
15636543fbbaSBarry Smith #if defined(PETSC_DEBUG)
15646543fbbaSBarry Smith   PetscTruth     found = PETSC_FALSE;
15656543fbbaSBarry Smith #endif
15660ac07820SSatish Balay 
1567d64ed03dSBarry Smith   PetscFunctionBegin;
15680ac07820SSatish Balay   /*  first count number of contributors to each processor */
1569b24ad042SBarry Smith   ierr  = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
1570b24ad042SBarry Smith   ierr  = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
1571b24ad042SBarry Smith   ierr  = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/
15726543fbbaSBarry Smith   j = 0;
15730ac07820SSatish Balay   for (i=0; i<N; i++) {
15746543fbbaSBarry Smith     if (lastidx > (idx = rows[i])) j = 0;
15756543fbbaSBarry Smith     lastidx = idx;
15766543fbbaSBarry Smith     for (; j<size; j++) {
1577357c27ecSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
15786543fbbaSBarry Smith         nprocs[2*j]++;
15796543fbbaSBarry Smith         nprocs[2*j+1] = 1;
15806543fbbaSBarry Smith         owner[i] = j;
15816543fbbaSBarry Smith #if defined(PETSC_DEBUG)
15826543fbbaSBarry Smith         found = PETSC_TRUE;
15836543fbbaSBarry Smith #endif
15846543fbbaSBarry Smith         break;
15850ac07820SSatish Balay       }
15860ac07820SSatish Balay     }
15876543fbbaSBarry Smith #if defined(PETSC_DEBUG)
1588*e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
15896543fbbaSBarry Smith     found = PETSC_FALSE;
15906543fbbaSBarry Smith #endif
15910ac07820SSatish Balay   }
1592c1dc657dSBarry Smith   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
15930ac07820SSatish Balay 
15940ac07820SSatish Balay   /* inform other processors of number of messages and max length*/
1595c1dc657dSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
15960ac07820SSatish Balay 
15970ac07820SSatish Balay   /* post receives:   */
1598b24ad042SBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr);
1599b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
16000ac07820SSatish Balay   for (i=0; i<nrecvs; i++) {
1601b24ad042SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
16020ac07820SSatish Balay   }
16030ac07820SSatish Balay 
16040ac07820SSatish Balay   /* do sends:
16050ac07820SSatish Balay      1) starts[i] gives the starting index in svalues for stuff going to
16060ac07820SSatish Balay      the ith processor
16070ac07820SSatish Balay   */
1608b24ad042SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr);
1609b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
1610b24ad042SBarry Smith   ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
16110ac07820SSatish Balay   starts[0]  = 0;
1612c1dc657dSBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
16130ac07820SSatish Balay   for (i=0; i<N; i++) {
16140ac07820SSatish Balay     svalues[starts[owner[i]]++] = rows[i];
16150ac07820SSatish Balay   }
16160ac07820SSatish Balay 
16170ac07820SSatish Balay   starts[0] = 0;
1618c1dc657dSBarry Smith   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
16190ac07820SSatish Balay   count = 0;
16200ac07820SSatish Balay   for (i=0; i<size; i++) {
1621c1dc657dSBarry Smith     if (nprocs[2*i+1]) {
1622b24ad042SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
16230ac07820SSatish Balay     }
16240ac07820SSatish Balay   }
1625606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
16260ac07820SSatish Balay 
1627357c27ecSBarry Smith   base = owners[rank];
16280ac07820SSatish Balay 
16290ac07820SSatish Balay   /*  wait on receives */
1630fca92195SBarry Smith   ierr   = PetscMalloc2(nrecvs+1,PetscInt,&lens,nrecvs+1,PetscInt,&source);CHKERRQ(ierr);
1631fca92195SBarry Smith   count  = nrecvs;
1632fca92195SBarry Smith   slen = 0;
16330ac07820SSatish Balay   while (count) {
1634ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
16350ac07820SSatish Balay     /* unpack receives into our local space */
1636b24ad042SBarry Smith     ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr);
16370ac07820SSatish Balay     source[imdex]  = recv_status.MPI_SOURCE;
16380ac07820SSatish Balay     lens[imdex]    = n;
16390ac07820SSatish Balay     slen          += n;
16400ac07820SSatish Balay     count--;
16410ac07820SSatish Balay   }
1642606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
16430ac07820SSatish Balay 
16440ac07820SSatish Balay   /* move the data into the send scatter */
1645b24ad042SBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr);
16460ac07820SSatish Balay   count = 0;
16470ac07820SSatish Balay   for (i=0; i<nrecvs; i++) {
16480ac07820SSatish Balay     values = rvalues + i*nmax;
16490ac07820SSatish Balay     for (j=0; j<lens[i]; j++) {
16500ac07820SSatish Balay       lrows[count++] = values[j] - base;
16510ac07820SSatish Balay     }
16520ac07820SSatish Balay   }
1653606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
1654fca92195SBarry Smith   ierr = PetscFree2(lens,source);CHKERRQ(ierr);
1655606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
1656606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
16570ac07820SSatish Balay 
16580ac07820SSatish Balay   /* actually zap the local rows */
165972dacd9aSBarry Smith   /*
166072dacd9aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
1661a8c7a070SBarry Smith      is square and the user wishes to set the diagonal we use separate
166272dacd9aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
166372dacd9aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
166472dacd9aSBarry Smith 
166572dacd9aSBarry Smith   */
16669c957beeSSatish Balay   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
1667f4df32b1SMatthew Knepley   ierr = MatZeroRows_SeqBAIJ(l->B,slen,lrows,0.0);CHKERRQ(ierr);
1668d0f46423SBarry Smith   if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) {
1669f4df32b1SMatthew Knepley     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,diag);CHKERRQ(ierr);
1670f4df32b1SMatthew Knepley   } else if (diag != 0.0) {
1671f4df32b1SMatthew Knepley     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,0.0);CHKERRQ(ierr);
1672fa46199cSSatish Balay     if (((Mat_SeqBAIJ*)l->A->data)->nonew) {
1673*e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options \n\
1674512a5fc5SBarry Smith MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
16756525c446SSatish Balay     }
1676a07cd24cSSatish Balay     for (i=0; i<slen; i++) {
1677a07cd24cSSatish Balay       row  = lrows[i] + rstart_bs;
1678f4df32b1SMatthew Knepley       ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
1679a07cd24cSSatish Balay     }
1680a07cd24cSSatish Balay     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1681a07cd24cSSatish Balay     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16829c957beeSSatish Balay   } else {
1683f4df32b1SMatthew Knepley     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,0.0);CHKERRQ(ierr);
1684a07cd24cSSatish Balay   }
16859c957beeSSatish Balay 
1686606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
1687a07cd24cSSatish Balay 
16880ac07820SSatish Balay   /* wait on sends */
16890ac07820SSatish Balay   if (nsends) {
169082502324SSatish Balay     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
1691ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
1692606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
16930ac07820SSatish Balay   }
1694606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
1695606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
16960ac07820SSatish Balay 
16973a40ed3dSBarry Smith   PetscFunctionReturn(0);
16980ac07820SSatish Balay }
169972dacd9aSBarry Smith 
17004a2ae208SSatish Balay #undef __FUNCT__
17014a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIBAIJ"
1702dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIBAIJ(Mat A)
1703bb5a7306SBarry Smith {
1704bb5a7306SBarry Smith   Mat_MPIBAIJ    *a   = (Mat_MPIBAIJ*)A->data;
1705dfbe8321SBarry Smith   PetscErrorCode ierr;
1706d64ed03dSBarry Smith 
1707d64ed03dSBarry Smith   PetscFunctionBegin;
1708bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
17093a40ed3dSBarry Smith   PetscFunctionReturn(0);
1710bb5a7306SBarry Smith }
1711bb5a7306SBarry Smith 
17126849ba73SBarry Smith static PetscErrorCode MatDuplicate_MPIBAIJ(Mat,MatDuplicateOption,Mat *);
17130ac07820SSatish Balay 
17144a2ae208SSatish Balay #undef __FUNCT__
17154a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIBAIJ"
1716dfbe8321SBarry Smith PetscErrorCode MatEqual_MPIBAIJ(Mat A,Mat B,PetscTruth *flag)
17177fc3c18eSBarry Smith {
17187fc3c18eSBarry Smith   Mat_MPIBAIJ    *matB = (Mat_MPIBAIJ*)B->data,*matA = (Mat_MPIBAIJ*)A->data;
17197fc3c18eSBarry Smith   Mat            a,b,c,d;
17207fc3c18eSBarry Smith   PetscTruth     flg;
1721dfbe8321SBarry Smith   PetscErrorCode ierr;
17227fc3c18eSBarry Smith 
17237fc3c18eSBarry Smith   PetscFunctionBegin;
17247fc3c18eSBarry Smith   a = matA->A; b = matA->B;
17257fc3c18eSBarry Smith   c = matB->A; d = matB->B;
17267fc3c18eSBarry Smith 
17277fc3c18eSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1728abc0a331SBarry Smith   if (flg) {
17297fc3c18eSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
17307fc3c18eSBarry Smith   }
17317adad957SLisandro Dalcin   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,((PetscObject)A)->comm);CHKERRQ(ierr);
17327fc3c18eSBarry Smith   PetscFunctionReturn(0);
17337fc3c18eSBarry Smith }
17347fc3c18eSBarry Smith 
17353c896bc6SHong Zhang #undef __FUNCT__
17363c896bc6SHong Zhang #define __FUNCT__ "MatCopy_MPIBAIJ"
17373c896bc6SHong Zhang PetscErrorCode MatCopy_MPIBAIJ(Mat A,Mat B,MatStructure str)
17383c896bc6SHong Zhang {
17393c896bc6SHong Zhang   PetscErrorCode ierr;
17403c896bc6SHong Zhang   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ *)A->data;
17413c896bc6SHong Zhang   Mat_MPIBAIJ    *b = (Mat_MPIBAIJ *)B->data;
17423c896bc6SHong Zhang 
17433c896bc6SHong Zhang   PetscFunctionBegin;
17443c896bc6SHong Zhang   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
17453c896bc6SHong Zhang   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
17463c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
17473c896bc6SHong Zhang   } else {
17483c896bc6SHong Zhang     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
17493c896bc6SHong Zhang     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
17503c896bc6SHong Zhang   }
17513c896bc6SHong Zhang   PetscFunctionReturn(0);
17523c896bc6SHong Zhang }
1753273d9f13SBarry Smith 
17544a2ae208SSatish Balay #undef __FUNCT__
17554a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIBAIJ"
1756dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_MPIBAIJ(Mat A)
1757273d9f13SBarry Smith {
1758dfbe8321SBarry Smith   PetscErrorCode ierr;
1759273d9f13SBarry Smith 
1760273d9f13SBarry Smith   PetscFunctionBegin;
1761db4efbfdSBarry Smith   ierr =  MatMPIBAIJSetPreallocation(A,-PetscMax(A->rmap->bs,1),PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1762273d9f13SBarry Smith   PetscFunctionReturn(0);
1763273d9f13SBarry Smith }
1764273d9f13SBarry Smith 
17654fe895cdSHong Zhang #undef __FUNCT__
17664fe895cdSHong Zhang #define __FUNCT__ "MatAXPY_MPIBAIJ"
17674fe895cdSHong Zhang PetscErrorCode MatAXPY_MPIBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
17684fe895cdSHong Zhang {
17694fe895cdSHong Zhang   PetscErrorCode ierr;
17704fe895cdSHong Zhang   Mat_MPIBAIJ    *xx=(Mat_MPIBAIJ *)X->data,*yy=(Mat_MPIBAIJ *)Y->data;
17714fe895cdSHong Zhang   PetscBLASInt   bnz,one=1;
17724fe895cdSHong Zhang   Mat_SeqBAIJ    *x,*y;
17734fe895cdSHong Zhang 
17744fe895cdSHong Zhang   PetscFunctionBegin;
17754fe895cdSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
17764fe895cdSHong Zhang     PetscScalar alpha = a;
17774fe895cdSHong Zhang     x = (Mat_SeqBAIJ *)xx->A->data;
17784fe895cdSHong Zhang     y = (Mat_SeqBAIJ *)yy->A->data;
17790805154bSBarry Smith     bnz = PetscBLASIntCast(x->nz);
17804fe895cdSHong Zhang     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
17814fe895cdSHong Zhang     x = (Mat_SeqBAIJ *)xx->B->data;
17824fe895cdSHong Zhang     y = (Mat_SeqBAIJ *)yy->B->data;
17830805154bSBarry Smith     bnz = PetscBLASIntCast(x->nz);
17844fe895cdSHong Zhang     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
17854fe895cdSHong Zhang   } else {
17864fe895cdSHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
17874fe895cdSHong Zhang   }
17884fe895cdSHong Zhang   PetscFunctionReturn(0);
17894fe895cdSHong Zhang }
17904fe895cdSHong Zhang 
179199cafbc1SBarry Smith #undef __FUNCT__
179241c166b1SJed Brown #define __FUNCT__ "MatSetBlockSize_MPIBAIJ"
179341c166b1SJed Brown PetscErrorCode MatSetBlockSize_MPIBAIJ(Mat A,PetscInt bs)
179441c166b1SJed Brown {
179541c166b1SJed Brown   Mat_MPIBAIJ    *a   = (Mat_MPIBAIJ*)A->data;
1796829b6ff0SJed Brown   PetscInt rbs,cbs;
179741c166b1SJed Brown   PetscErrorCode ierr;
179841c166b1SJed Brown 
179941c166b1SJed Brown   PetscFunctionBegin;
180041c166b1SJed Brown   ierr = MatSetBlockSize(a->A,bs);CHKERRQ(ierr);
180141c166b1SJed Brown   ierr = MatSetBlockSize(a->B,bs);CHKERRQ(ierr);
1802829b6ff0SJed Brown   ierr = PetscLayoutGetBlockSize(A->rmap,&rbs);CHKERRQ(ierr);
1803829b6ff0SJed Brown   ierr = PetscLayoutGetBlockSize(A->cmap,&cbs);CHKERRQ(ierr);
1804*e32f2f54SBarry Smith   if (rbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with BAIJ %d",bs,rbs);
1805*e32f2f54SBarry Smith   if (cbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with BAIJ %d",bs,cbs);
180641c166b1SJed Brown   PetscFunctionReturn(0);
180741c166b1SJed Brown }
180841c166b1SJed Brown 
180941c166b1SJed Brown #undef __FUNCT__
181099cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIBAIJ"
181199cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIBAIJ(Mat A)
181299cafbc1SBarry Smith {
181399cafbc1SBarry Smith   Mat_MPIBAIJ   *a = (Mat_MPIBAIJ*)A->data;
181499cafbc1SBarry Smith   PetscErrorCode ierr;
181599cafbc1SBarry Smith 
181699cafbc1SBarry Smith   PetscFunctionBegin;
181799cafbc1SBarry Smith   ierr = MatRealPart(a->A);CHKERRQ(ierr);
181899cafbc1SBarry Smith   ierr = MatRealPart(a->B);CHKERRQ(ierr);
181999cafbc1SBarry Smith   PetscFunctionReturn(0);
182099cafbc1SBarry Smith }
182199cafbc1SBarry Smith 
182299cafbc1SBarry Smith #undef __FUNCT__
182399cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIBAIJ"
182499cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIBAIJ(Mat A)
182599cafbc1SBarry Smith {
182699cafbc1SBarry Smith   Mat_MPIBAIJ   *a = (Mat_MPIBAIJ*)A->data;
182799cafbc1SBarry Smith   PetscErrorCode ierr;
182899cafbc1SBarry Smith 
182999cafbc1SBarry Smith   PetscFunctionBegin;
183099cafbc1SBarry Smith   ierr = MatImaginaryPart(a->A);CHKERRQ(ierr);
183199cafbc1SBarry Smith   ierr = MatImaginaryPart(a->B);CHKERRQ(ierr);
183299cafbc1SBarry Smith   PetscFunctionReturn(0);
183399cafbc1SBarry Smith }
183499cafbc1SBarry Smith 
183582094794SBarry Smith #undef __FUNCT__
183682094794SBarry Smith #define __FUNCT__ "MatGetSubMatrix_MPIBAIJ"
18374aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIBAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
18384aa3045dSJed Brown {
18394aa3045dSJed Brown   PetscErrorCode ierr;
18404aa3045dSJed Brown   IS             iscol_local;
18414aa3045dSJed Brown   PetscInt       csize;
18424aa3045dSJed Brown 
18434aa3045dSJed Brown   PetscFunctionBegin;
18444aa3045dSJed Brown   ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr);
1845b79d0421SJed Brown   if (call == MAT_REUSE_MATRIX) {
1846b79d0421SJed Brown     ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr);
1847*e32f2f54SBarry Smith     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
1848b79d0421SJed Brown   } else {
18494aa3045dSJed Brown     ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr);
1850b79d0421SJed Brown   }
18514aa3045dSJed Brown   ierr = MatGetSubMatrix_MPIBAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr);
1852b79d0421SJed Brown   if (call == MAT_INITIAL_MATRIX) {
1853b79d0421SJed Brown     ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr);
18544aa3045dSJed Brown     ierr = ISDestroy(iscol_local);CHKERRQ(ierr);
1855b79d0421SJed Brown   }
18564aa3045dSJed Brown   PetscFunctionReturn(0);
18574aa3045dSJed Brown }
18584aa3045dSJed Brown 
18594aa3045dSJed Brown #undef __FUNCT__
18604aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIBAIJ"
186182094794SBarry Smith /*
186282094794SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqBAIJ
186382094794SBarry Smith   in local and then by concatenating the local matrices the end result.
186482094794SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIBAIJ()
186582094794SBarry Smith */
18664aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIBAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
186782094794SBarry Smith {
186882094794SBarry Smith   PetscErrorCode ierr;
186982094794SBarry Smith   PetscMPIInt    rank,size;
187082094794SBarry Smith   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs;
187182094794SBarry Smith   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
187282094794SBarry Smith   Mat            *local,M,Mreuse;
187382094794SBarry Smith   MatScalar      *vwork,*aa;
187482094794SBarry Smith   MPI_Comm       comm = ((PetscObject)mat)->comm;
187582094794SBarry Smith   Mat_SeqBAIJ    *aij;
187682094794SBarry Smith 
187782094794SBarry Smith 
187882094794SBarry Smith   PetscFunctionBegin;
187982094794SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
188082094794SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
188182094794SBarry Smith 
188282094794SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
188382094794SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
1884*e32f2f54SBarry Smith     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
188582094794SBarry Smith     local = &Mreuse;
188682094794SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
188782094794SBarry Smith   } else {
188882094794SBarry Smith     ierr   = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
188982094794SBarry Smith     Mreuse = *local;
189082094794SBarry Smith     ierr   = PetscFree(local);CHKERRQ(ierr);
189182094794SBarry Smith   }
189282094794SBarry Smith 
189382094794SBarry Smith   /*
189482094794SBarry Smith       m - number of local rows
189582094794SBarry Smith       n - number of columns (same on all processors)
189682094794SBarry Smith       rstart - first row in new global matrix generated
189782094794SBarry Smith   */
189882094794SBarry Smith   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
189982094794SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
190082094794SBarry Smith   m    = m/bs;
190182094794SBarry Smith   n    = n/bs;
190282094794SBarry Smith 
190382094794SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
190482094794SBarry Smith     aij = (Mat_SeqBAIJ*)(Mreuse)->data;
190582094794SBarry Smith     ii  = aij->i;
190682094794SBarry Smith     jj  = aij->j;
190782094794SBarry Smith 
190882094794SBarry Smith     /*
190982094794SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
191082094794SBarry Smith         portions of the matrix in order to do correct preallocation
191182094794SBarry Smith     */
191282094794SBarry Smith 
191382094794SBarry Smith     /* first get start and end of "diagonal" columns */
191482094794SBarry Smith     if (csize == PETSC_DECIDE) {
191582094794SBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
191682094794SBarry Smith       if (mglobal == n*bs) { /* square matrix */
191782094794SBarry Smith 	nlocal = m;
191882094794SBarry Smith       } else {
191982094794SBarry Smith         nlocal = n/size + ((n % size) > rank);
192082094794SBarry Smith       }
192182094794SBarry Smith     } else {
192282094794SBarry Smith       nlocal = csize/bs;
192382094794SBarry Smith     }
192482094794SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
192582094794SBarry Smith     rstart = rend - nlocal;
192682094794SBarry Smith     if (rank == size - 1 && rend != n) {
1927*e32f2f54SBarry Smith       SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,n);
192882094794SBarry Smith     }
192982094794SBarry Smith 
193082094794SBarry Smith     /* next, compute all the lengths */
193182094794SBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr);
193282094794SBarry Smith     olens = dlens + m;
193382094794SBarry Smith     for (i=0; i<m; i++) {
193482094794SBarry Smith       jend = ii[i+1] - ii[i];
193582094794SBarry Smith       olen = 0;
193682094794SBarry Smith       dlen = 0;
193782094794SBarry Smith       for (j=0; j<jend; j++) {
193882094794SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
193982094794SBarry Smith         else dlen++;
194082094794SBarry Smith         jj++;
194182094794SBarry Smith       }
194282094794SBarry Smith       olens[i] = olen;
194382094794SBarry Smith       dlens[i] = dlen;
194482094794SBarry Smith     }
194582094794SBarry Smith     ierr = MatCreate(comm,&M);CHKERRQ(ierr);
194682094794SBarry Smith     ierr = MatSetSizes(M,bs*m,bs*nlocal,PETSC_DECIDE,bs*n);CHKERRQ(ierr);
194782094794SBarry Smith     ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr);
194882094794SBarry Smith     ierr = MatMPIBAIJSetPreallocation(M,bs,0,dlens,0,olens);CHKERRQ(ierr);
194982094794SBarry Smith     ierr = PetscFree(dlens);CHKERRQ(ierr);
195082094794SBarry Smith   } else {
195182094794SBarry Smith     PetscInt ml,nl;
195282094794SBarry Smith 
195382094794SBarry Smith     M = *newmat;
195482094794SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
1955*e32f2f54SBarry Smith     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
195682094794SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
195782094794SBarry Smith     /*
195882094794SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
195982094794SBarry Smith        rather than the slower MatSetValues().
196082094794SBarry Smith     */
196182094794SBarry Smith     M->was_assembled = PETSC_TRUE;
196282094794SBarry Smith     M->assembled     = PETSC_FALSE;
196382094794SBarry Smith   }
196482094794SBarry Smith   ierr = MatSetOption(M,MAT_ROW_ORIENTED,PETSC_FALSE);CHKERRQ(ierr);
196582094794SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
196682094794SBarry Smith   aij = (Mat_SeqBAIJ*)(Mreuse)->data;
196782094794SBarry Smith   ii  = aij->i;
196882094794SBarry Smith   jj  = aij->j;
196982094794SBarry Smith   aa  = aij->a;
197082094794SBarry Smith   for (i=0; i<m; i++) {
197182094794SBarry Smith     row   = rstart/bs + i;
197282094794SBarry Smith     nz    = ii[i+1] - ii[i];
197382094794SBarry Smith     cwork = jj;     jj += nz;
197482094794SBarry Smith     vwork = aa;     aa += nz;
197582094794SBarry Smith     ierr = MatSetValuesBlocked_MPIBAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
197682094794SBarry Smith   }
197782094794SBarry Smith 
197882094794SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
197982094794SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
198082094794SBarry Smith   *newmat = M;
198182094794SBarry Smith 
198282094794SBarry Smith   /* save submatrix used in processor for next request */
198382094794SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
198482094794SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
198582094794SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
198682094794SBarry Smith   }
198782094794SBarry Smith 
198882094794SBarry Smith   PetscFunctionReturn(0);
198982094794SBarry Smith }
199082094794SBarry Smith 
199182094794SBarry Smith #undef __FUNCT__
199282094794SBarry Smith #define __FUNCT__ "MatPermute_MPIBAIJ"
199382094794SBarry Smith PetscErrorCode MatPermute_MPIBAIJ(Mat A,IS rowp,IS colp,Mat *B)
199482094794SBarry Smith {
199582094794SBarry Smith   MPI_Comm       comm,pcomm;
199682094794SBarry Smith   PetscInt       first,local_size,nrows;
199782094794SBarry Smith   const PetscInt *rows;
1998dbf0e21dSBarry Smith   PetscMPIInt    size;
199982094794SBarry Smith   IS             crowp,growp,irowp,lrowp,lcolp,icolp;
200082094794SBarry Smith   PetscErrorCode ierr;
200182094794SBarry Smith 
200282094794SBarry Smith   PetscFunctionBegin;
200382094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr);
200482094794SBarry Smith   /* make a collective version of 'rowp' */
200582094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)rowp,&pcomm);CHKERRQ(ierr);
200682094794SBarry Smith   if (pcomm==comm) {
200782094794SBarry Smith     crowp = rowp;
200882094794SBarry Smith   } else {
200982094794SBarry Smith     ierr = ISGetSize(rowp,&nrows);CHKERRQ(ierr);
201082094794SBarry Smith     ierr = ISGetIndices(rowp,&rows);CHKERRQ(ierr);
201182094794SBarry Smith     ierr = ISCreateGeneral(comm,nrows,rows,&crowp);CHKERRQ(ierr);
201282094794SBarry Smith     ierr = ISRestoreIndices(rowp,&rows);CHKERRQ(ierr);
201382094794SBarry Smith   }
201482094794SBarry Smith   /* collect the global row permutation and invert it */
201582094794SBarry Smith   ierr = ISAllGather(crowp,&growp);CHKERRQ(ierr);
201682094794SBarry Smith   ierr = ISSetPermutation(growp);CHKERRQ(ierr);
201782094794SBarry Smith   if (pcomm!=comm) {
201882094794SBarry Smith     ierr = ISDestroy(crowp);CHKERRQ(ierr);
201982094794SBarry Smith   }
202082094794SBarry Smith   ierr = ISInvertPermutation(growp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
202182094794SBarry Smith   /* get the local target indices */
202282094794SBarry Smith   ierr = MatGetOwnershipRange(A,&first,PETSC_NULL);CHKERRQ(ierr);
202382094794SBarry Smith   ierr = MatGetLocalSize(A,&local_size,PETSC_NULL);CHKERRQ(ierr);
202482094794SBarry Smith   ierr = ISGetIndices(irowp,&rows);CHKERRQ(ierr);
202582094794SBarry Smith   ierr = ISCreateGeneral(MPI_COMM_SELF,local_size,rows+first,&lrowp);CHKERRQ(ierr);
202682094794SBarry Smith   ierr = ISRestoreIndices(irowp,&rows);CHKERRQ(ierr);
202782094794SBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
202882094794SBarry Smith   /* the column permutation is so much easier;
202982094794SBarry Smith      make a local version of 'colp' and invert it */
203082094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)colp,&pcomm);CHKERRQ(ierr);
2031dbf0e21dSBarry Smith   ierr = MPI_Comm_size(pcomm,&size);CHKERRQ(ierr);
2032dbf0e21dSBarry Smith   if (size==1) {
203382094794SBarry Smith     lcolp = colp;
203482094794SBarry Smith   } else {
203582094794SBarry Smith     ierr = ISGetSize(colp,&nrows);CHKERRQ(ierr);
203682094794SBarry Smith     ierr = ISGetIndices(colp,&rows);CHKERRQ(ierr);
203782094794SBarry Smith     ierr = ISCreateGeneral(MPI_COMM_SELF,nrows,rows,&lcolp);CHKERRQ(ierr);
203882094794SBarry Smith   }
2039dbf0e21dSBarry Smith   ierr = ISSetPermutation(lcolp);CHKERRQ(ierr);
204082094794SBarry Smith   ierr = ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
20414aa3045dSJed Brown   ierr = ISSetPermutation(icolp);CHKERRQ(ierr);
2042dbf0e21dSBarry Smith   if (size>1) {
204382094794SBarry Smith     ierr = ISRestoreIndices(colp,&rows);CHKERRQ(ierr);
204482094794SBarry Smith     ierr = ISDestroy(lcolp);CHKERRQ(ierr);
204582094794SBarry Smith   }
204682094794SBarry Smith   /* now we just get the submatrix */
20474aa3045dSJed Brown   ierr = MatGetSubMatrix_MPIBAIJ_Private(A,lrowp,icolp,local_size,MAT_INITIAL_MATRIX,B);CHKERRQ(ierr);
204882094794SBarry Smith   /* clean up */
204982094794SBarry Smith   ierr = ISDestroy(lrowp);CHKERRQ(ierr);
205082094794SBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
205182094794SBarry Smith   PetscFunctionReturn(0);
205282094794SBarry Smith }
205382094794SBarry Smith 
20548c7482ecSBarry Smith #undef __FUNCT__
20558c7482ecSBarry Smith #define __FUNCT__ "MatGetGhosts_MPIBAIJ"
20568c7482ecSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatGetGhosts_MPIBAIJ(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
20578c7482ecSBarry Smith {
20588c7482ecSBarry Smith   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*) mat->data;
20598c7482ecSBarry Smith   Mat_SeqBAIJ    *B = (Mat_SeqBAIJ*)baij->B->data;
20608c7482ecSBarry Smith 
20618c7482ecSBarry Smith   PetscFunctionBegin;
20628c7482ecSBarry Smith   if (nghosts) { *nghosts = B->nbs;}
20638c7482ecSBarry Smith   if (ghosts) {*ghosts = baij->garray;}
20648c7482ecSBarry Smith   PetscFunctionReturn(0);
20658c7482ecSBarry Smith }
20668c7482ecSBarry Smith 
2067f6d58c54SBarry Smith EXTERN PetscErrorCode CreateColmap_MPIBAIJ_Private(Mat);
2068f6d58c54SBarry Smith 
2069f6d58c54SBarry Smith #undef __FUNCT__
2070f6d58c54SBarry Smith #define __FUNCT__ "MatFDColoringCreate_MPIBAIJ"
2071f6d58c54SBarry Smith /*
2072f6d58c54SBarry Smith     This routine is almost identical to MatFDColoringCreate_MPIBAIJ()!
2073f6d58c54SBarry Smith */
2074f6d58c54SBarry Smith PetscErrorCode MatFDColoringCreate_MPIBAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
2075f6d58c54SBarry Smith {
2076f6d58c54SBarry Smith   Mat_MPIBAIJ            *baij = (Mat_MPIBAIJ*)mat->data;
2077f6d58c54SBarry Smith   PetscErrorCode        ierr;
2078f6d58c54SBarry Smith   PetscMPIInt           size,*ncolsonproc,*disp,nn;
2079f6d58c54SBarry Smith   PetscInt              bs,i,n,nrows,j,k,m,*rows = 0,*A_ci,*A_cj,ncols,col;
2080f6d58c54SBarry Smith   const PetscInt        *is;
2081f6d58c54SBarry Smith   PetscInt              nis = iscoloring->n,nctot,*cols,*B_ci,*B_cj;
2082f6d58c54SBarry Smith   PetscInt              *rowhit,M,cstart,cend,colb;
2083f6d58c54SBarry Smith   PetscInt              *columnsforrow,l;
2084f6d58c54SBarry Smith   IS                    *isa;
2085f6d58c54SBarry Smith   PetscTruth             done,flg;
2086f6d58c54SBarry Smith   ISLocalToGlobalMapping map = mat->bmapping;
2087f6d58c54SBarry Smith   PetscInt               *ltog = (map ? map->indices : (PetscInt*) PETSC_NULL) ,ctype=c->ctype;
2088f6d58c54SBarry Smith 
2089f6d58c54SBarry Smith   PetscFunctionBegin;
2090f6d58c54SBarry Smith   if (!mat->assembled) {
2091*e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();");
2092f6d58c54SBarry Smith   }
2093*e32f2f54SBarry Smith   if (ctype == IS_COLORING_GHOSTED && !map) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"When using ghosted differencing matrix must have local to global mapping provided with MatSetLocalToGlobalMappingBlock");
2094f6d58c54SBarry Smith 
2095f6d58c54SBarry Smith   ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr);
2096f6d58c54SBarry Smith 
2097f6d58c54SBarry Smith   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
2098f6d58c54SBarry Smith   M                = mat->rmap->n/bs;
2099f6d58c54SBarry Smith   cstart           = mat->cmap->rstart/bs;
2100f6d58c54SBarry Smith   cend             = mat->cmap->rend/bs;
2101f6d58c54SBarry Smith   c->M             = mat->rmap->N/bs;  /* set the global rows and columns and local rows */
2102f6d58c54SBarry Smith   c->N             = mat->cmap->N/bs;
2103f6d58c54SBarry Smith   c->m             = mat->rmap->n/bs;
2104f6d58c54SBarry Smith   c->rstart        = mat->rmap->rstart/bs;
2105f6d58c54SBarry Smith 
2106f6d58c54SBarry Smith   c->ncolors       = nis;
2107f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt),&c->ncolumns);CHKERRQ(ierr);
2108f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->columns);CHKERRQ(ierr);
2109f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt),&c->nrows);CHKERRQ(ierr);
2110f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->rows);CHKERRQ(ierr);
2111f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->columnsforrow);CHKERRQ(ierr);
2112f6d58c54SBarry Smith   ierr = PetscLogObjectMemory(c,5*nis*sizeof(PetscInt));CHKERRQ(ierr);
2113f6d58c54SBarry Smith 
2114f6d58c54SBarry Smith   /* Allow access to data structures of local part of matrix */
2115f6d58c54SBarry Smith   if (!baij->colmap) {
2116f6d58c54SBarry Smith     ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
2117f6d58c54SBarry Smith   }
2118f6d58c54SBarry Smith   ierr = MatGetColumnIJ(baij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
2119f6d58c54SBarry Smith   ierr = MatGetColumnIJ(baij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
2120f6d58c54SBarry Smith 
2121f6d58c54SBarry Smith   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&rowhit);CHKERRQ(ierr);
2122f6d58c54SBarry Smith   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&columnsforrow);CHKERRQ(ierr);
2123f6d58c54SBarry Smith 
2124f6d58c54SBarry Smith   for (i=0; i<nis; i++) {
2125f6d58c54SBarry Smith     ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr);
2126f6d58c54SBarry Smith     ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr);
2127f6d58c54SBarry Smith     c->ncolumns[i] = n;
2128f6d58c54SBarry Smith     if (n) {
2129f6d58c54SBarry Smith       ierr = PetscMalloc(n*sizeof(PetscInt),&c->columns[i]);CHKERRQ(ierr);
2130f6d58c54SBarry Smith       ierr = PetscLogObjectMemory(c,n*sizeof(PetscInt));CHKERRQ(ierr);
2131f6d58c54SBarry Smith       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));CHKERRQ(ierr);
2132f6d58c54SBarry Smith     } else {
2133f6d58c54SBarry Smith       c->columns[i]  = 0;
2134f6d58c54SBarry Smith     }
2135f6d58c54SBarry Smith 
2136f6d58c54SBarry Smith     if (ctype == IS_COLORING_GLOBAL){
2137f6d58c54SBarry Smith       /* Determine the total (parallel) number of columns of this color */
2138f6d58c54SBarry Smith       ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
2139fca92195SBarry Smith       ierr = PetscMalloc2(size,PetscMPIInt,&ncolsonproc,size,PetscMPIInt,&disp);CHKERRQ(ierr);
2140f6d58c54SBarry Smith 
2141f6d58c54SBarry Smith       nn   = PetscMPIIntCast(n);
2142f6d58c54SBarry Smith       ierr = MPI_Allgather(&nn,1,MPI_INT,ncolsonproc,1,MPI_INT,((PetscObject)mat)->comm);CHKERRQ(ierr);
2143f6d58c54SBarry Smith       nctot = 0; for (j=0; j<size; j++) {nctot += ncolsonproc[j];}
2144f6d58c54SBarry Smith       if (!nctot) {
2145f6d58c54SBarry Smith         ierr = PetscInfo(mat,"Coloring of matrix has some unneeded colors with no corresponding rows\n");CHKERRQ(ierr);
2146f6d58c54SBarry Smith       }
2147f6d58c54SBarry Smith 
2148f6d58c54SBarry Smith       disp[0] = 0;
2149f6d58c54SBarry Smith       for (j=1; j<size; j++) {
2150f6d58c54SBarry Smith         disp[j] = disp[j-1] + ncolsonproc[j-1];
2151f6d58c54SBarry Smith       }
2152f6d58c54SBarry Smith 
2153f6d58c54SBarry Smith       /* Get complete list of columns for color on each processor */
2154f6d58c54SBarry Smith       ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
2155f6d58c54SBarry Smith       ierr = MPI_Allgatherv((void*)is,n,MPIU_INT,cols,ncolsonproc,disp,MPIU_INT,((PetscObject)mat)->comm);CHKERRQ(ierr);
2156fca92195SBarry Smith       ierr = PetscFree2(ncolsonproc,disp);CHKERRQ(ierr);
2157f6d58c54SBarry Smith     } else if (ctype == IS_COLORING_GHOSTED){
2158f6d58c54SBarry Smith       /* Determine local number of columns of this color on this process, including ghost points */
2159f6d58c54SBarry Smith       nctot = n;
2160f6d58c54SBarry Smith       ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
2161f6d58c54SBarry Smith       ierr = PetscMemcpy(cols,is,n*sizeof(PetscInt));CHKERRQ(ierr);
2162f6d58c54SBarry Smith     } else {
2163*e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not provided for this MatFDColoring type");
2164f6d58c54SBarry Smith     }
2165f6d58c54SBarry Smith 
2166f6d58c54SBarry Smith     /*
2167f6d58c54SBarry Smith        Mark all rows affect by these columns
2168f6d58c54SBarry Smith     */
2169f6d58c54SBarry Smith     /* Temporary option to allow for debugging/testing */
2170f6d58c54SBarry Smith     flg  = PETSC_FALSE;
2171f6d58c54SBarry Smith     ierr = PetscOptionsGetTruth(PETSC_NULL,"-matfdcoloring_slow",&flg,PETSC_NULL);CHKERRQ(ierr);
2172f6d58c54SBarry Smith     if (!flg) {/*-----------------------------------------------------------------------------*/
2173f6d58c54SBarry Smith       /* crude, fast version */
2174f6d58c54SBarry Smith       ierr = PetscMemzero(rowhit,M*sizeof(PetscInt));CHKERRQ(ierr);
2175f6d58c54SBarry Smith       /* loop over columns*/
2176f6d58c54SBarry Smith       for (j=0; j<nctot; j++) {
2177f6d58c54SBarry Smith         if (ctype == IS_COLORING_GHOSTED) {
2178f6d58c54SBarry Smith           col = ltog[cols[j]];
2179f6d58c54SBarry Smith         } else {
2180f6d58c54SBarry Smith           col  = cols[j];
2181f6d58c54SBarry Smith         }
2182f6d58c54SBarry Smith         if (col >= cstart && col < cend) {
2183f6d58c54SBarry Smith           /* column is in diagonal block of matrix */
2184f6d58c54SBarry Smith           rows = A_cj + A_ci[col-cstart];
2185f6d58c54SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
2186f6d58c54SBarry Smith         } else {
2187f6d58c54SBarry Smith #if defined (PETSC_USE_CTABLE)
2188f6d58c54SBarry Smith           ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr)
2189f6d58c54SBarry Smith 	  colb --;
2190f6d58c54SBarry Smith #else
2191f6d58c54SBarry Smith           colb = baij->colmap[col] - 1;
2192f6d58c54SBarry Smith #endif
2193f6d58c54SBarry Smith           if (colb == -1) {
2194f6d58c54SBarry Smith             m = 0;
2195f6d58c54SBarry Smith           } else {
2196f6d58c54SBarry Smith             colb = colb/bs;
2197f6d58c54SBarry Smith             rows = B_cj + B_ci[colb];
2198f6d58c54SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
2199f6d58c54SBarry Smith           }
2200f6d58c54SBarry Smith         }
2201f6d58c54SBarry Smith         /* loop over columns marking them in rowhit */
2202f6d58c54SBarry Smith         for (k=0; k<m; k++) {
2203f6d58c54SBarry Smith           rowhit[*rows++] = col + 1;
2204f6d58c54SBarry Smith         }
2205f6d58c54SBarry Smith       }
2206f6d58c54SBarry Smith 
2207f6d58c54SBarry Smith       /* count the number of hits */
2208f6d58c54SBarry Smith       nrows = 0;
2209f6d58c54SBarry Smith       for (j=0; j<M; j++) {
2210f6d58c54SBarry Smith         if (rowhit[j]) nrows++;
2211f6d58c54SBarry Smith       }
2212f6d58c54SBarry Smith       c->nrows[i]         = nrows;
2213f6d58c54SBarry Smith       ierr                = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr);
2214f6d58c54SBarry Smith       ierr                = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr);
2215f6d58c54SBarry Smith       ierr = PetscLogObjectMemory(c,2*(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr);
2216f6d58c54SBarry Smith       nrows = 0;
2217f6d58c54SBarry Smith       for (j=0; j<M; j++) {
2218f6d58c54SBarry Smith         if (rowhit[j]) {
2219f6d58c54SBarry Smith           c->rows[i][nrows]           = j;
2220f6d58c54SBarry Smith           c->columnsforrow[i][nrows] = rowhit[j] - 1;
2221f6d58c54SBarry Smith           nrows++;
2222f6d58c54SBarry Smith         }
2223f6d58c54SBarry Smith       }
2224f6d58c54SBarry Smith     } else {/*-------------------------------------------------------------------------------*/
2225f6d58c54SBarry Smith       /* slow version, using rowhit as a linked list */
2226f6d58c54SBarry Smith       PetscInt currentcol,fm,mfm;
2227f6d58c54SBarry Smith       rowhit[M] = M;
2228f6d58c54SBarry Smith       nrows     = 0;
2229f6d58c54SBarry Smith       /* loop over columns*/
2230f6d58c54SBarry Smith       for (j=0; j<nctot; j++) {
2231f6d58c54SBarry Smith         if (ctype == IS_COLORING_GHOSTED) {
2232f6d58c54SBarry Smith           col = ltog[cols[j]];
2233f6d58c54SBarry Smith         } else {
2234f6d58c54SBarry Smith           col  = cols[j];
2235f6d58c54SBarry Smith         }
2236f6d58c54SBarry Smith         if (col >= cstart && col < cend) {
2237f6d58c54SBarry Smith           /* column is in diagonal block of matrix */
2238f6d58c54SBarry Smith           rows = A_cj + A_ci[col-cstart];
2239f6d58c54SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
2240f6d58c54SBarry Smith         } else {
2241f6d58c54SBarry Smith #if defined (PETSC_USE_CTABLE)
2242f6d58c54SBarry Smith           ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr);
2243f6d58c54SBarry Smith           colb --;
2244f6d58c54SBarry Smith #else
2245f6d58c54SBarry Smith           colb = baij->colmap[col] - 1;
2246f6d58c54SBarry Smith #endif
2247f6d58c54SBarry Smith           if (colb == -1) {
2248f6d58c54SBarry Smith             m = 0;
2249f6d58c54SBarry Smith           } else {
2250f6d58c54SBarry Smith             colb = colb/bs;
2251f6d58c54SBarry Smith             rows = B_cj + B_ci[colb];
2252f6d58c54SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
2253f6d58c54SBarry Smith           }
2254f6d58c54SBarry Smith         }
2255f6d58c54SBarry Smith 
2256f6d58c54SBarry Smith         /* loop over columns marking them in rowhit */
2257f6d58c54SBarry Smith         fm    = M; /* fm points to first entry in linked list */
2258f6d58c54SBarry Smith         for (k=0; k<m; k++) {
2259f6d58c54SBarry Smith           currentcol = *rows++;
2260f6d58c54SBarry Smith 	  /* is it already in the list? */
2261f6d58c54SBarry Smith           do {
2262f6d58c54SBarry Smith             mfm  = fm;
2263f6d58c54SBarry Smith             fm   = rowhit[fm];
2264f6d58c54SBarry Smith           } while (fm < currentcol);
2265f6d58c54SBarry Smith           /* not in list so add it */
2266f6d58c54SBarry Smith           if (fm != currentcol) {
2267f6d58c54SBarry Smith             nrows++;
2268f6d58c54SBarry Smith             columnsforrow[currentcol] = col;
2269f6d58c54SBarry Smith             /* next three lines insert new entry into linked list */
2270f6d58c54SBarry Smith             rowhit[mfm]               = currentcol;
2271f6d58c54SBarry Smith             rowhit[currentcol]        = fm;
2272f6d58c54SBarry Smith             fm                        = currentcol;
2273f6d58c54SBarry Smith             /* fm points to present position in list since we know the columns are sorted */
2274f6d58c54SBarry Smith           } else {
2275*e32f2f54SBarry Smith             SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid coloring of matrix detected");
2276f6d58c54SBarry Smith           }
2277f6d58c54SBarry Smith         }
2278f6d58c54SBarry Smith       }
2279f6d58c54SBarry Smith       c->nrows[i]         = nrows;
2280f6d58c54SBarry Smith       ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr);
2281f6d58c54SBarry Smith       ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr);
2282f6d58c54SBarry Smith       ierr = PetscLogObjectMemory(c,(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr);
2283f6d58c54SBarry Smith       /* now store the linked list of rows into c->rows[i] */
2284f6d58c54SBarry Smith       nrows = 0;
2285f6d58c54SBarry Smith       fm    = rowhit[M];
2286f6d58c54SBarry Smith       do {
2287f6d58c54SBarry Smith         c->rows[i][nrows]            = fm;
2288f6d58c54SBarry Smith         c->columnsforrow[i][nrows++] = columnsforrow[fm];
2289f6d58c54SBarry Smith         fm                           = rowhit[fm];
2290f6d58c54SBarry Smith       } while (fm < M);
2291f6d58c54SBarry Smith     } /* ---------------------------------------------------------------------------------------*/
2292f6d58c54SBarry Smith     ierr = PetscFree(cols);CHKERRQ(ierr);
2293f6d58c54SBarry Smith   }
2294f6d58c54SBarry Smith 
2295f6d58c54SBarry Smith   /* Optimize by adding the vscale, and scaleforrow[][] fields */
2296f6d58c54SBarry Smith   /*
2297f6d58c54SBarry Smith        vscale will contain the "diagonal" on processor scalings followed by the off processor
2298f6d58c54SBarry Smith   */
2299f6d58c54SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
2300f6d58c54SBarry Smith     PetscInt *garray;
2301f6d58c54SBarry Smith     ierr = PetscMalloc(baij->B->cmap->n*sizeof(PetscInt),&garray);CHKERRQ(ierr);
2302f6d58c54SBarry Smith     for (i=0; i<baij->B->cmap->n/bs; i++) {
2303f6d58c54SBarry Smith       for (j=0; j<bs; j++) {
2304f6d58c54SBarry Smith         garray[i*bs+j] = bs*baij->garray[i]+j;
2305f6d58c54SBarry Smith       }
2306f6d58c54SBarry Smith     }
2307f6d58c54SBarry Smith     ierr = VecCreateGhost(((PetscObject)mat)->comm,baij->A->rmap->n,PETSC_DETERMINE,baij->B->cmap->n,garray,&c->vscale);CHKERRQ(ierr);
2308f6d58c54SBarry Smith     ierr = PetscFree(garray);CHKERRQ(ierr);
2309f6d58c54SBarry Smith     CHKMEMQ;
2310f6d58c54SBarry Smith     ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr);
2311f6d58c54SBarry Smith     for (k=0; k<c->ncolors; k++) {
2312f6d58c54SBarry Smith       ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr);
2313f6d58c54SBarry Smith       for (l=0; l<c->nrows[k]; l++) {
2314f6d58c54SBarry Smith         col = c->columnsforrow[k][l];
2315f6d58c54SBarry Smith         if (col >= cstart && col < cend) {
2316f6d58c54SBarry Smith           /* column is in diagonal block of matrix */
2317f6d58c54SBarry Smith           colb = col - cstart;
2318f6d58c54SBarry Smith         } else {
2319f6d58c54SBarry Smith           /* column  is in "off-processor" part */
2320f6d58c54SBarry Smith #if defined (PETSC_USE_CTABLE)
2321f6d58c54SBarry Smith           ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr);
2322f6d58c54SBarry Smith           colb --;
2323f6d58c54SBarry Smith #else
2324f6d58c54SBarry Smith           colb = baij->colmap[col] - 1;
2325f6d58c54SBarry Smith #endif
2326f6d58c54SBarry Smith           colb = colb/bs;
2327f6d58c54SBarry Smith           colb += cend - cstart;
2328f6d58c54SBarry Smith         }
2329f6d58c54SBarry Smith         c->vscaleforrow[k][l] = colb;
2330f6d58c54SBarry Smith       }
2331f6d58c54SBarry Smith     }
2332f6d58c54SBarry Smith   } else if (ctype == IS_COLORING_GHOSTED) {
2333f6d58c54SBarry Smith     /* Get gtol mapping */
2334f6d58c54SBarry Smith     PetscInt N = mat->cmap->N, *gtol;
2335f6d58c54SBarry Smith     ierr = PetscMalloc((N+1)*sizeof(PetscInt),&gtol);CHKERRQ(ierr);
2336f6d58c54SBarry Smith     for (i=0; i<N; i++) gtol[i] = -1;
2337f6d58c54SBarry Smith     for (i=0; i<map->n; i++) gtol[ltog[i]] = i;
2338f6d58c54SBarry Smith 
2339f6d58c54SBarry Smith     c->vscale = 0; /* will be created in MatFDColoringApply() */
2340f6d58c54SBarry Smith     ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr);
2341f6d58c54SBarry Smith     for (k=0; k<c->ncolors; k++) {
2342f6d58c54SBarry Smith       ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr);
2343f6d58c54SBarry Smith       for (l=0; l<c->nrows[k]; l++) {
2344f6d58c54SBarry Smith         col = c->columnsforrow[k][l];      /* global column index */
2345f6d58c54SBarry Smith         c->vscaleforrow[k][l] = gtol[col]; /* local column index */
2346f6d58c54SBarry Smith       }
2347f6d58c54SBarry Smith     }
2348f6d58c54SBarry Smith     ierr = PetscFree(gtol);CHKERRQ(ierr);
2349f6d58c54SBarry Smith   }
2350f6d58c54SBarry Smith   ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr);
2351f6d58c54SBarry Smith 
2352f6d58c54SBarry Smith   ierr = PetscFree(rowhit);CHKERRQ(ierr);
2353f6d58c54SBarry Smith   ierr = PetscFree(columnsforrow);CHKERRQ(ierr);
2354f6d58c54SBarry Smith   ierr = MatRestoreColumnIJ(baij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
2355f6d58c54SBarry Smith   ierr = MatRestoreColumnIJ(baij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
2356f6d58c54SBarry Smith     CHKMEMQ;
2357f6d58c54SBarry Smith   PetscFunctionReturn(0);
2358f6d58c54SBarry Smith }
2359f6d58c54SBarry Smith 
2360f6d58c54SBarry Smith #undef __FUNCT__
2361f6d58c54SBarry Smith #define __FUNCT__ "MatGetSeqNonzerostructure_MPIBAIJ"
2362f6d58c54SBarry Smith PetscErrorCode MatGetSeqNonzerostructure_MPIBAIJ(Mat A,Mat *newmat)
2363f6d58c54SBarry Smith {
2364f6d58c54SBarry Smith   Mat            B;
2365f6d58c54SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ *)A->data;
2366f6d58c54SBarry Smith   Mat_SeqBAIJ    *ad = (Mat_SeqBAIJ*)a->A->data,*bd = (Mat_SeqBAIJ*)a->B->data;
2367f6d58c54SBarry Smith   Mat_SeqAIJ     *b;
2368f6d58c54SBarry Smith   PetscErrorCode ierr;
2369f6d58c54SBarry Smith   PetscMPIInt    size,rank,*recvcounts = 0,*displs = 0;
2370f6d58c54SBarry Smith   PetscInt       sendcount,i,*rstarts = A->rmap->range,n,cnt,j,bs = A->rmap->bs;
2371f6d58c54SBarry Smith   PetscInt       m,*garray = a->garray,*lens,*jsendbuf,*a_jsendbuf,*b_jsendbuf;
2372f6d58c54SBarry Smith 
2373f6d58c54SBarry Smith   PetscFunctionBegin;
2374f6d58c54SBarry Smith   ierr = MPI_Comm_size(((PetscObject)A)->comm,&size);CHKERRQ(ierr);
2375f6d58c54SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)A)->comm,&rank);CHKERRQ(ierr);
2376f6d58c54SBarry Smith 
2377f6d58c54SBarry Smith   /* ----------------------------------------------------------------
2378f6d58c54SBarry Smith      Tell every processor the number of nonzeros per row
2379f6d58c54SBarry Smith   */
2380f6d58c54SBarry Smith   ierr = PetscMalloc((A->rmap->N/bs)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
2381f6d58c54SBarry Smith   for (i=A->rmap->rstart/bs; i<A->rmap->rend/bs; i++) {
2382f6d58c54SBarry 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];
2383f6d58c54SBarry Smith   }
2384f6d58c54SBarry Smith   sendcount = A->rmap->rend/bs - A->rmap->rstart/bs;
2385f6d58c54SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscMPIInt),&recvcounts);CHKERRQ(ierr);
2386f6d58c54SBarry Smith   displs     = recvcounts + size;
2387f6d58c54SBarry Smith   for (i=0; i<size; i++) {
2388f6d58c54SBarry Smith     recvcounts[i] = A->rmap->range[i+1]/bs - A->rmap->range[i]/bs;
2389f6d58c54SBarry Smith     displs[i]     = A->rmap->range[i]/bs;
2390f6d58c54SBarry Smith   }
2391f6d58c54SBarry Smith #if defined(PETSC_HAVE_MPI_IN_PLACE)
2392f6d58c54SBarry Smith   ierr  = MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,lens,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr);
2393f6d58c54SBarry Smith #else
2394f6d58c54SBarry Smith   ierr  = MPI_Allgatherv(lens+A->rmap->rstart/bs,sendcount,MPIU_INT,lens,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr);
2395f6d58c54SBarry Smith #endif
2396f6d58c54SBarry Smith   /* ---------------------------------------------------------------
2397f6d58c54SBarry Smith      Create the sequential matrix of the same type as the local block diagonal
2398f6d58c54SBarry Smith   */
2399f6d58c54SBarry Smith   ierr  = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr);
2400f6d58c54SBarry Smith   ierr  = MatSetSizes(B,A->rmap->N/bs,A->cmap->N/bs,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
2401f6d58c54SBarry Smith   ierr  = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
2402f6d58c54SBarry Smith   ierr  = MatSeqAIJSetPreallocation(B,0,lens);CHKERRQ(ierr);
2403f6d58c54SBarry Smith   b = (Mat_SeqAIJ *)B->data;
2404f6d58c54SBarry Smith 
2405f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2406f6d58c54SBarry Smith     Copy my part of matrix column indices over
2407f6d58c54SBarry Smith   */
2408f6d58c54SBarry Smith   sendcount  = ad->nz + bd->nz;
2409f6d58c54SBarry Smith   jsendbuf   = b->j + b->i[rstarts[rank]/bs];
2410f6d58c54SBarry Smith   a_jsendbuf = ad->j;
2411f6d58c54SBarry Smith   b_jsendbuf = bd->j;
2412f6d58c54SBarry Smith   n          = A->rmap->rend/bs - A->rmap->rstart/bs;
2413f6d58c54SBarry Smith   cnt        = 0;
2414f6d58c54SBarry Smith   for (i=0; i<n; i++) {
2415f6d58c54SBarry Smith 
2416f6d58c54SBarry Smith     /* put in lower diagonal portion */
2417f6d58c54SBarry Smith     m = bd->i[i+1] - bd->i[i];
2418f6d58c54SBarry Smith     while (m > 0) {
2419f6d58c54SBarry Smith       /* is it above diagonal (in bd (compressed) numbering) */
2420f6d58c54SBarry Smith       if (garray[*b_jsendbuf] > A->rmap->rstart/bs + i) break;
2421f6d58c54SBarry Smith       jsendbuf[cnt++] = garray[*b_jsendbuf++];
2422f6d58c54SBarry Smith       m--;
2423f6d58c54SBarry Smith     }
2424f6d58c54SBarry Smith 
2425f6d58c54SBarry Smith     /* put in diagonal portion */
2426f6d58c54SBarry Smith     for (j=ad->i[i]; j<ad->i[i+1]; j++) {
2427f6d58c54SBarry Smith       jsendbuf[cnt++] = A->rmap->rstart/bs + *a_jsendbuf++;
2428f6d58c54SBarry Smith     }
2429f6d58c54SBarry Smith 
2430f6d58c54SBarry Smith     /* put in upper diagonal portion */
2431f6d58c54SBarry Smith     while (m-- > 0) {
2432f6d58c54SBarry Smith       jsendbuf[cnt++] = garray[*b_jsendbuf++];
2433f6d58c54SBarry Smith     }
2434f6d58c54SBarry Smith   }
2435*e32f2f54SBarry Smith   if (cnt != sendcount) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupted PETSc matrix: nz given %D actual nz %D",sendcount,cnt);
2436f6d58c54SBarry Smith 
2437f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2438f6d58c54SBarry Smith     Gather all column indices to all processors
2439f6d58c54SBarry Smith   */
2440f6d58c54SBarry Smith   for (i=0; i<size; i++) {
2441f6d58c54SBarry Smith     recvcounts[i] = 0;
2442f6d58c54SBarry Smith     for (j=A->rmap->range[i]/bs; j<A->rmap->range[i+1]/bs; j++) {
2443f6d58c54SBarry Smith       recvcounts[i] += lens[j];
2444f6d58c54SBarry Smith     }
2445f6d58c54SBarry Smith   }
2446f6d58c54SBarry Smith   displs[0]  = 0;
2447f6d58c54SBarry Smith   for (i=1; i<size; i++) {
2448f6d58c54SBarry Smith     displs[i] = displs[i-1] + recvcounts[i-1];
2449f6d58c54SBarry Smith   }
2450f6d58c54SBarry Smith #if defined(PETSC_HAVE_MPI_IN_PLACE)
2451f6d58c54SBarry Smith   ierr = MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,b->j,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr);
2452f6d58c54SBarry Smith #else
2453f6d58c54SBarry Smith   ierr = MPI_Allgatherv(jsendbuf,sendcount,MPIU_INT,b->j,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr);
2454f6d58c54SBarry Smith #endif
2455f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2456f6d58c54SBarry Smith     Assemble the matrix into useable form (note numerical values not yet set)
2457f6d58c54SBarry Smith   */
2458f6d58c54SBarry Smith   /* set the b->ilen (length of each row) values */
2459f6d58c54SBarry Smith   ierr = PetscMemcpy(b->ilen,lens,(A->rmap->N/bs)*sizeof(PetscInt));CHKERRQ(ierr);
2460f6d58c54SBarry Smith   /* set the b->i indices */
2461f6d58c54SBarry Smith   b->i[0] = 0;
2462f6d58c54SBarry Smith   for (i=1; i<=A->rmap->N/bs; i++) {
2463f6d58c54SBarry Smith     b->i[i] = b->i[i-1] + lens[i-1];
2464f6d58c54SBarry Smith   }
2465f6d58c54SBarry Smith   ierr = PetscFree(lens);CHKERRQ(ierr);
2466f6d58c54SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2467f6d58c54SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2468f6d58c54SBarry Smith   ierr = PetscFree(recvcounts);CHKERRQ(ierr);
2469f6d58c54SBarry Smith 
2470f6d58c54SBarry Smith   if (A->symmetric){
2471f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
2472f6d58c54SBarry Smith   } else if (A->hermitian) {
2473f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
2474f6d58c54SBarry Smith   } else if (A->structurally_symmetric) {
2475f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
2476f6d58c54SBarry Smith   }
2477f6d58c54SBarry Smith   *newmat = B;
2478f6d58c54SBarry Smith   PetscFunctionReturn(0);
2479f6d58c54SBarry Smith }
2480f6d58c54SBarry Smith 
2481b1a666ecSBarry Smith #undef __FUNCT__
2482b1a666ecSBarry Smith #define __FUNCT__ "MatSOR_MPIBAIJ"
2483b1a666ecSBarry Smith PetscErrorCode MatSOR_MPIBAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
2484b1a666ecSBarry Smith {
2485b1a666ecSBarry Smith   Mat_MPIBAIJ    *mat = (Mat_MPIBAIJ*)matin->data;
2486b1a666ecSBarry Smith   PetscErrorCode ierr;
2487b1a666ecSBarry Smith   Vec            bb1 = 0;
2488b1a666ecSBarry Smith 
2489b1a666ecSBarry Smith   PetscFunctionBegin;
2490b1a666ecSBarry Smith   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS) {
2491b1a666ecSBarry Smith     ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
2492b1a666ecSBarry Smith   }
2493b1a666ecSBarry Smith 
2494b1a666ecSBarry Smith   if (flag == SOR_APPLY_UPPER) {
2495b1a666ecSBarry Smith     ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2496b1a666ecSBarry Smith     PetscFunctionReturn(0);
2497b1a666ecSBarry Smith   }
2498b1a666ecSBarry Smith 
2499b1a666ecSBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
2500b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2501b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2502b1a666ecSBarry Smith       its--;
2503b1a666ecSBarry Smith     }
2504b1a666ecSBarry Smith 
2505b1a666ecSBarry Smith     while (its--) {
2506b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2507b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2508b1a666ecSBarry Smith 
2509b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2510b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2511b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2512b1a666ecSBarry Smith 
2513b1a666ecSBarry Smith       /* local sweep */
2514b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2515b1a666ecSBarry Smith     }
2516b1a666ecSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
2517b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2518b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2519b1a666ecSBarry Smith       its--;
2520b1a666ecSBarry Smith     }
2521b1a666ecSBarry Smith     while (its--) {
2522b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2523b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2524b1a666ecSBarry Smith 
2525b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2526b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2527b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2528b1a666ecSBarry Smith 
2529b1a666ecSBarry Smith       /* local sweep */
2530b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2531b1a666ecSBarry Smith     }
2532b1a666ecSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
2533b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2534b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2535b1a666ecSBarry Smith       its--;
2536b1a666ecSBarry Smith     }
2537b1a666ecSBarry Smith     while (its--) {
2538b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2539b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2540b1a666ecSBarry Smith 
2541b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2542b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2543b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2544b1a666ecSBarry Smith 
2545b1a666ecSBarry Smith       /* local sweep */
2546b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2547b1a666ecSBarry Smith     }
2548b1a666ecSBarry Smith   } else {
2549*e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Parallel version of SOR requested not supported");
2550b1a666ecSBarry Smith   }
2551b1a666ecSBarry Smith 
2552b1a666ecSBarry Smith   if (bb1) {ierr = VecDestroy(bb1);CHKERRQ(ierr);}
2553b1a666ecSBarry Smith   PetscFunctionReturn(0);
2554b1a666ecSBarry Smith }
2555b1a666ecSBarry Smith 
2556f6d58c54SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_BAIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2557f6d58c54SBarry Smith 
25588c7482ecSBarry Smith 
255979bdfe76SSatish Balay /* -------------------------------------------------------------------*/
2560cc2dc46cSBarry Smith static struct _MatOps MatOps_Values = {
2561cc2dc46cSBarry Smith        MatSetValues_MPIBAIJ,
2562cc2dc46cSBarry Smith        MatGetRow_MPIBAIJ,
2563cc2dc46cSBarry Smith        MatRestoreRow_MPIBAIJ,
2564cc2dc46cSBarry Smith        MatMult_MPIBAIJ,
256597304618SKris Buschelman /* 4*/ MatMultAdd_MPIBAIJ,
25667c922b88SBarry Smith        MatMultTranspose_MPIBAIJ,
25677c922b88SBarry Smith        MatMultTransposeAdd_MPIBAIJ,
2568cc2dc46cSBarry Smith        0,
2569cc2dc46cSBarry Smith        0,
2570cc2dc46cSBarry Smith        0,
257197304618SKris Buschelman /*10*/ 0,
2572cc2dc46cSBarry Smith        0,
2573cc2dc46cSBarry Smith        0,
2574b1a666ecSBarry Smith        MatSOR_MPIBAIJ,
2575cc2dc46cSBarry Smith        MatTranspose_MPIBAIJ,
257697304618SKris Buschelman /*15*/ MatGetInfo_MPIBAIJ,
25777fc3c18eSBarry Smith        MatEqual_MPIBAIJ,
2578cc2dc46cSBarry Smith        MatGetDiagonal_MPIBAIJ,
2579cc2dc46cSBarry Smith        MatDiagonalScale_MPIBAIJ,
2580cc2dc46cSBarry Smith        MatNorm_MPIBAIJ,
258197304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIBAIJ,
2582cc2dc46cSBarry Smith        MatAssemblyEnd_MPIBAIJ,
2583cc2dc46cSBarry Smith        MatSetOption_MPIBAIJ,
2584cc2dc46cSBarry Smith        MatZeroEntries_MPIBAIJ,
2585d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIBAIJ,
2586cc2dc46cSBarry Smith        0,
2587cc2dc46cSBarry Smith        0,
2588cc2dc46cSBarry Smith        0,
2589cc2dc46cSBarry Smith        0,
2590d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_MPIBAIJ,
2591273d9f13SBarry Smith        0,
2592cc2dc46cSBarry Smith        0,
2593cc2dc46cSBarry Smith        0,
2594cc2dc46cSBarry Smith        0,
2595d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIBAIJ,
2596cc2dc46cSBarry Smith        0,
2597cc2dc46cSBarry Smith        0,
2598cc2dc46cSBarry Smith        0,
2599cc2dc46cSBarry Smith        0,
2600d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIBAIJ,
2601cc2dc46cSBarry Smith        MatGetSubMatrices_MPIBAIJ,
2602cc2dc46cSBarry Smith        MatIncreaseOverlap_MPIBAIJ,
2603cc2dc46cSBarry Smith        MatGetValues_MPIBAIJ,
26043c896bc6SHong Zhang        MatCopy_MPIBAIJ,
2605d519adbfSMatthew Knepley /*44*/ 0,
2606cc2dc46cSBarry Smith        MatScale_MPIBAIJ,
2607cc2dc46cSBarry Smith        0,
2608cc2dc46cSBarry Smith        0,
2609cc2dc46cSBarry Smith        0,
261041c166b1SJed Brown /*49*/ MatSetBlockSize_MPIBAIJ,
2611cc2dc46cSBarry Smith        0,
2612cc2dc46cSBarry Smith        0,
2613cc2dc46cSBarry Smith        0,
2614cc2dc46cSBarry Smith        0,
2615f6d58c54SBarry Smith /*54*/ MatFDColoringCreate_MPIBAIJ,
2616cc2dc46cSBarry Smith        0,
2617cc2dc46cSBarry Smith        MatSetUnfactored_MPIBAIJ,
261882094794SBarry Smith        MatPermute_MPIBAIJ,
2619cc2dc46cSBarry Smith        MatSetValuesBlocked_MPIBAIJ,
2620d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIBAIJ,
2621f14a1c24SBarry Smith        MatDestroy_MPIBAIJ,
2622f14a1c24SBarry Smith        MatView_MPIBAIJ,
2623357abbc8SBarry Smith        0,
26247843d17aSBarry Smith        0,
2625d519adbfSMatthew Knepley /*64*/ 0,
26267843d17aSBarry Smith        0,
26277843d17aSBarry Smith        0,
26287843d17aSBarry Smith        0,
26297843d17aSBarry Smith        0,
2630d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIBAIJ,
26317843d17aSBarry Smith        0,
263297304618SKris Buschelman        0,
263397304618SKris Buschelman        0,
263497304618SKris Buschelman        0,
2635d519adbfSMatthew Knepley /*74*/ 0,
2636f6d58c54SBarry Smith        MatFDColoringApply_BAIJ,
263797304618SKris Buschelman        0,
263897304618SKris Buschelman        0,
263997304618SKris Buschelman        0,
2640d519adbfSMatthew Knepley /*79*/ 0,
264197304618SKris Buschelman        0,
264297304618SKris Buschelman        0,
264397304618SKris Buschelman        0,
2644865e5f61SKris Buschelman        MatLoad_MPIBAIJ,
2645d519adbfSMatthew Knepley /*84*/ 0,
2646865e5f61SKris Buschelman        0,
2647865e5f61SKris Buschelman        0,
2648865e5f61SKris Buschelman        0,
2649865e5f61SKris Buschelman        0,
2650d519adbfSMatthew Knepley /*89*/ 0,
2651865e5f61SKris Buschelman        0,
2652865e5f61SKris Buschelman        0,
2653865e5f61SKris Buschelman        0,
2654865e5f61SKris Buschelman        0,
2655d519adbfSMatthew Knepley /*94*/ 0,
2656865e5f61SKris Buschelman        0,
2657865e5f61SKris Buschelman        0,
265899cafbc1SBarry Smith        0,
265999cafbc1SBarry Smith        0,
2660d519adbfSMatthew Knepley /*99*/ 0,
266199cafbc1SBarry Smith        0,
266299cafbc1SBarry Smith        0,
266399cafbc1SBarry Smith        0,
266499cafbc1SBarry Smith        0,
2665d519adbfSMatthew Knepley /*104*/0,
266699cafbc1SBarry Smith        MatRealPart_MPIBAIJ,
26678c7482ecSBarry Smith        MatImaginaryPart_MPIBAIJ,
26688c7482ecSBarry Smith        0,
26698c7482ecSBarry Smith        0,
2670d519adbfSMatthew Knepley /*109*/0,
26718c7482ecSBarry Smith        0,
26728c7482ecSBarry Smith        0,
26738c7482ecSBarry Smith        0,
26748c7482ecSBarry Smith        0,
2675f6d58c54SBarry Smith /*114*/MatGetSeqNonzerostructure_MPIBAIJ,
26768c7482ecSBarry Smith        0,
26778c7482ecSBarry Smith        MatGetGhosts_MPIBAIJ
26788c7482ecSBarry Smith };
267979bdfe76SSatish Balay 
2680e18c124aSSatish Balay EXTERN_C_BEGIN
26814a2ae208SSatish Balay #undef __FUNCT__
26824a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonalBlock_MPIBAIJ"
2683be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetDiagonalBlock_MPIBAIJ(Mat A,PetscTruth *iscopy,MatReuse reuse,Mat *a)
26845ef9f2a5SBarry Smith {
26855ef9f2a5SBarry Smith   PetscFunctionBegin;
26865ef9f2a5SBarry Smith   *a      = ((Mat_MPIBAIJ *)A->data)->A;
26875ef9f2a5SBarry Smith   *iscopy = PETSC_FALSE;
26885ef9f2a5SBarry Smith   PetscFunctionReturn(0);
26895ef9f2a5SBarry Smith }
2690e18c124aSSatish Balay EXTERN_C_END
269179bdfe76SSatish Balay 
2692273d9f13SBarry Smith EXTERN_C_BEGIN
2693f69a0ea3SMatthew Knepley extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIBAIJ_MPISBAIJ(Mat, MatType,MatReuse,Mat*);
2694d94109b8SHong Zhang EXTERN_C_END
2695d94109b8SHong Zhang 
2696b8d659d7SLisandro Dalcin EXTERN_C_BEGIN
2697aac34f13SBarry Smith #undef __FUNCT__
2698aac34f13SBarry Smith #define __FUNCT__ "MatMPIBAIJSetPreallocationCSR_MPIBAIJ"
2699cf12db73SBarry Smith PetscErrorCode MatMPIBAIJSetPreallocationCSR_MPIBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
2700aac34f13SBarry Smith {
2701b8d659d7SLisandro Dalcin   PetscInt       m,rstart,cstart,cend;
2702b8d659d7SLisandro Dalcin   PetscInt       i,j,d,nz,nz_max=0,*d_nnz=0,*o_nnz=0;
2703b8d659d7SLisandro Dalcin   const PetscInt *JJ=0;
2704b8d659d7SLisandro Dalcin   PetscScalar    *values=0;
2705aac34f13SBarry Smith   PetscErrorCode ierr;
2706aac34f13SBarry Smith 
2707aac34f13SBarry Smith   PetscFunctionBegin;
2708b8d659d7SLisandro Dalcin 
2709*e32f2f54SBarry Smith   if (bs < 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
271026283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
271126283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
271226283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
271326283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2714d0f46423SBarry Smith   m      = B->rmap->n/bs;
2715d0f46423SBarry Smith   rstart = B->rmap->rstart/bs;
2716d0f46423SBarry Smith   cstart = B->cmap->rstart/bs;
2717d0f46423SBarry Smith   cend   = B->cmap->rend/bs;
2718b8d659d7SLisandro Dalcin 
2719*e32f2f54SBarry Smith   if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
2720fca92195SBarry Smith   ierr  = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr);
2721aac34f13SBarry Smith   for (i=0; i<m; i++) {
2722cf12db73SBarry Smith     nz = ii[i+1] - ii[i];
2723*e32f2f54SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative number of columns %D",i,nz);
2724b8d659d7SLisandro Dalcin     nz_max = PetscMax(nz_max,nz);
2725cf12db73SBarry Smith     JJ  = jj + ii[i];
2726b8d659d7SLisandro Dalcin     for (j=0; j<nz; j++) {
2727aac34f13SBarry Smith       if (*JJ >= cstart) break;
2728aac34f13SBarry Smith       JJ++;
2729aac34f13SBarry Smith     }
2730aac34f13SBarry Smith     d = 0;
2731b8d659d7SLisandro Dalcin     for (; j<nz; j++) {
2732aac34f13SBarry Smith       if (*JJ++ >= cend) break;
2733aac34f13SBarry Smith       d++;
2734aac34f13SBarry Smith     }
2735aac34f13SBarry Smith     d_nnz[i] = d;
2736b8d659d7SLisandro Dalcin     o_nnz[i] = nz - d;
2737aac34f13SBarry Smith   }
2738aac34f13SBarry Smith   ierr = MatMPIBAIJSetPreallocation(B,bs,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
2739fca92195SBarry Smith   ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr);
2740aac34f13SBarry Smith 
2741b8d659d7SLisandro Dalcin   values = (PetscScalar*)V;
2742b8d659d7SLisandro Dalcin   if (!values) {
2743fca92195SBarry Smith     ierr = PetscMalloc(bs*bs*nz_max*sizeof(PetscScalar),&values);CHKERRQ(ierr);
2744b8d659d7SLisandro Dalcin     ierr = PetscMemzero(values,bs*bs*nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
2745b8d659d7SLisandro Dalcin   }
2746b8d659d7SLisandro Dalcin   for (i=0; i<m; i++) {
2747b8d659d7SLisandro Dalcin     PetscInt          row    = i + rstart;
2748cf12db73SBarry Smith     PetscInt          ncols  = ii[i+1] - ii[i];
2749cf12db73SBarry Smith     const PetscInt    *icols = jj + ii[i];
2750cf12db73SBarry Smith     const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
2751b8d659d7SLisandro Dalcin     ierr = MatSetValuesBlocked_MPIBAIJ(B,1,&row,ncols,icols,svals,INSERT_VALUES);CHKERRQ(ierr);
2752aac34f13SBarry Smith   }
2753aac34f13SBarry Smith 
2754b8d659d7SLisandro Dalcin   if (!V) { ierr = PetscFree(values);CHKERRQ(ierr); }
2755aac34f13SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2756aac34f13SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2757aac34f13SBarry Smith 
2758aac34f13SBarry Smith   PetscFunctionReturn(0);
2759aac34f13SBarry Smith }
2760b8d659d7SLisandro Dalcin EXTERN_C_END
2761aac34f13SBarry Smith 
2762aac34f13SBarry Smith #undef __FUNCT__
2763aac34f13SBarry Smith #define __FUNCT__ "MatMPIBAIJSetPreallocationCSR"
2764aac34f13SBarry Smith /*@C
2765aac34f13SBarry Smith    MatMPIBAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
2766aac34f13SBarry Smith    (the default parallel PETSc format).
2767aac34f13SBarry Smith 
2768aac34f13SBarry Smith    Collective on MPI_Comm
2769aac34f13SBarry Smith 
2770aac34f13SBarry Smith    Input Parameters:
2771aac34f13SBarry Smith +  A - the matrix
2772aac34f13SBarry Smith .  i - the indices into j for the start of each local row (starts with zero)
2773aac34f13SBarry Smith .  j - the column indices for each local row (starts with zero) these must be sorted for each row
2774aac34f13SBarry Smith -  v - optional values in the matrix
2775aac34f13SBarry Smith 
2776aac34f13SBarry Smith    Level: developer
2777aac34f13SBarry Smith 
2778aac34f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2779aac34f13SBarry Smith 
2780aac34f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIBAIJSetPreallocation(), MatCreateMPIAIJ(), MPIAIJ
2781aac34f13SBarry Smith @*/
2782be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2783aac34f13SBarry Smith {
2784aac34f13SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]);
2785aac34f13SBarry Smith 
2786aac34f13SBarry Smith   PetscFunctionBegin;
2787aac34f13SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIBAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
2788aac34f13SBarry Smith   if (f) {
2789aac34f13SBarry Smith     ierr = (*f)(B,bs,i,j,v);CHKERRQ(ierr);
2790aac34f13SBarry Smith   }
2791aac34f13SBarry Smith   PetscFunctionReturn(0);
2792aac34f13SBarry Smith }
2793aac34f13SBarry Smith 
2794d94109b8SHong Zhang EXTERN_C_BEGIN
27954a2ae208SSatish Balay #undef __FUNCT__
2796a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIBAIJSetPreallocation_MPIBAIJ"
2797be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJSetPreallocation_MPIBAIJ(Mat B,PetscInt bs,PetscInt d_nz,PetscInt *d_nnz,PetscInt o_nz,PetscInt *o_nnz)
2798a23d5eceSKris Buschelman {
2799a23d5eceSKris Buschelman   Mat_MPIBAIJ    *b;
2800dfbe8321SBarry Smith   PetscErrorCode ierr;
2801db4efbfdSBarry Smith   PetscInt       i, newbs = PetscAbs(bs);
2802a23d5eceSKris Buschelman 
2803a23d5eceSKris Buschelman   PetscFunctionBegin;
2804db4efbfdSBarry Smith   if (bs < 0) {
28057adad957SLisandro Dalcin     ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Options for MPIBAIJ matrix","Mat");CHKERRQ(ierr);
2806db4efbfdSBarry Smith       ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatMPIBAIJSetPreallocation",newbs,&newbs,PETSC_NULL);CHKERRQ(ierr);
28078c07d4e3SBarry Smith     ierr = PetscOptionsEnd();CHKERRQ(ierr);
2808db4efbfdSBarry Smith     bs   = PetscAbs(bs);
2809db4efbfdSBarry Smith   }
2810db4efbfdSBarry Smith   if ((d_nnz || o_nnz) && newbs != bs) {
2811*e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot change blocksize from command line if setting d_nnz or o_nnz");
2812db4efbfdSBarry Smith   }
2813db4efbfdSBarry Smith   bs = newbs;
2814db4efbfdSBarry Smith 
2815a23d5eceSKris Buschelman 
2816*e32f2f54SBarry Smith   if (bs < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive");
2817a23d5eceSKris Buschelman   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
2818a23d5eceSKris Buschelman   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
2819*e32f2f54SBarry Smith   if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz);
2820*e32f2f54SBarry Smith   if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz);
2821899cda47SBarry Smith 
282226283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
282326283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
282426283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
282526283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2826899cda47SBarry Smith 
2827a23d5eceSKris Buschelman   if (d_nnz) {
2828d0f46423SBarry Smith     for (i=0; i<B->rmap->n/bs; i++) {
2829*e32f2f54SBarry 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]);
2830a23d5eceSKris Buschelman     }
2831a23d5eceSKris Buschelman   }
2832a23d5eceSKris Buschelman   if (o_nnz) {
2833d0f46423SBarry Smith     for (i=0; i<B->rmap->n/bs; i++) {
2834*e32f2f54SBarry 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]);
2835a23d5eceSKris Buschelman     }
2836a23d5eceSKris Buschelman   }
2837a23d5eceSKris Buschelman 
2838a23d5eceSKris Buschelman   b = (Mat_MPIBAIJ*)B->data;
2839a23d5eceSKris Buschelman   b->bs2 = bs*bs;
2840d0f46423SBarry Smith   b->mbs = B->rmap->n/bs;
2841d0f46423SBarry Smith   b->nbs = B->cmap->n/bs;
2842d0f46423SBarry Smith   b->Mbs = B->rmap->N/bs;
2843d0f46423SBarry Smith   b->Nbs = B->cmap->N/bs;
2844a23d5eceSKris Buschelman 
2845a23d5eceSKris Buschelman   for (i=0; i<=b->size; i++) {
2846d0f46423SBarry Smith     b->rangebs[i] = B->rmap->range[i]/bs;
2847a23d5eceSKris Buschelman   }
2848d0f46423SBarry Smith   b->rstartbs = B->rmap->rstart/bs;
2849d0f46423SBarry Smith   b->rendbs   = B->rmap->rend/bs;
2850d0f46423SBarry Smith   b->cstartbs = B->cmap->rstart/bs;
2851d0f46423SBarry Smith   b->cendbs   = B->cmap->rend/bs;
2852a23d5eceSKris Buschelman 
2853526dfc15SBarry Smith   if (!B->preallocated) {
2854f69a0ea3SMatthew Knepley     ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr);
2855d0f46423SBarry Smith     ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr);
28569c097c71SKris Buschelman     ierr = MatSetType(b->A,MATSEQBAIJ);CHKERRQ(ierr);
285752e6d16bSBarry Smith     ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr);
2858f69a0ea3SMatthew Knepley     ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr);
2859d0f46423SBarry Smith     ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr);
28609c097c71SKris Buschelman     ierr = MatSetType(b->B,MATSEQBAIJ);CHKERRQ(ierr);
286152e6d16bSBarry Smith     ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr);
28627adad957SLisandro Dalcin     ierr = MatStashCreate_Private(((PetscObject)B)->comm,bs,&B->bstash);CHKERRQ(ierr);
2863526dfc15SBarry Smith   }
2864a23d5eceSKris Buschelman 
2865526dfc15SBarry Smith   ierr = MatSeqBAIJSetPreallocation(b->A,bs,d_nz,d_nnz);CHKERRQ(ierr);
2866526dfc15SBarry Smith   ierr = MatSeqBAIJSetPreallocation(b->B,bs,o_nz,o_nnz);CHKERRQ(ierr);
2867526dfc15SBarry Smith   B->preallocated = PETSC_TRUE;
2868a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2869a23d5eceSKris Buschelman }
2870a23d5eceSKris Buschelman EXTERN_C_END
2871a23d5eceSKris Buschelman 
2872a23d5eceSKris Buschelman EXTERN_C_BEGIN
2873be1d678aSKris Buschelman EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalScaleLocal_MPIBAIJ(Mat,Vec);
2874be1d678aSKris Buschelman EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatSetHashTableFactor_MPIBAIJ(Mat,PetscReal);
287592b32695SKris Buschelman EXTERN_C_END
28765bf65638SKris Buschelman 
287782094794SBarry Smith 
287882094794SBarry Smith EXTERN_C_BEGIN
287982094794SBarry Smith #undef __FUNCT__
288082094794SBarry Smith #define __FUNCT__ "MatConvert_MPIBAIJ_MPIAdj"
288182094794SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIBAIJ_MPIAdj(Mat B, const MatType newtype,MatReuse reuse,Mat *adj)
288282094794SBarry Smith {
288382094794SBarry Smith   Mat_MPIBAIJ    *b = (Mat_MPIBAIJ*)B->data;
288482094794SBarry Smith   PetscErrorCode ierr;
288582094794SBarry Smith   Mat_SeqBAIJ    *d = (Mat_SeqBAIJ*) b->A->data,*o = (Mat_SeqBAIJ*) b->B->data;
288682094794SBarry Smith   PetscInt       M = B->rmap->n/B->rmap->bs,i,*ii,*jj,cnt,j,k,rstart = B->rmap->rstart/B->rmap->bs;
288782094794SBarry Smith   const PetscInt *id = d->i, *jd = d->j, *io = o->i, *jo = o->j, *garray = b->garray;
288882094794SBarry Smith 
288982094794SBarry Smith   PetscFunctionBegin;
289082094794SBarry Smith   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&ii);CHKERRQ(ierr);
289182094794SBarry Smith   ii[0] = 0;
289282094794SBarry Smith   CHKMEMQ;
289382094794SBarry Smith   for (i=0; i<M; i++) {
2894*e32f2f54SBarry 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]);
2895*e32f2f54SBarry 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]);
289682094794SBarry Smith     ii[i+1] = ii[i] + id[i+1] - id[i] + io[i+1] - io[i];
289782094794SBarry Smith     /* remove one from count of matrix has diagonal */
289882094794SBarry Smith     for (j=id[i]; j<id[i+1]; j++) {
289982094794SBarry Smith       if (jd[j] == i) {ii[i+1]--;break;}
290082094794SBarry Smith     }
290182094794SBarry Smith   CHKMEMQ;
290282094794SBarry Smith   }
290382094794SBarry Smith   ierr = PetscMalloc(ii[M]*sizeof(PetscInt),&jj);CHKERRQ(ierr);
290482094794SBarry Smith   cnt = 0;
290582094794SBarry Smith   for (i=0; i<M; i++) {
290682094794SBarry Smith     for (j=io[i]; j<io[i+1]; j++) {
290782094794SBarry Smith       if (garray[jo[j]] > rstart) break;
290882094794SBarry Smith       jj[cnt++] = garray[jo[j]];
290982094794SBarry Smith   CHKMEMQ;
291082094794SBarry Smith     }
291182094794SBarry Smith     for (k=id[i]; k<id[i+1]; k++) {
291282094794SBarry Smith       if (jd[k] != i) {
291382094794SBarry Smith         jj[cnt++] = rstart + jd[k];
291482094794SBarry Smith   CHKMEMQ;
291582094794SBarry Smith       }
291682094794SBarry Smith     }
291782094794SBarry Smith     for (;j<io[i+1]; j++) {
291882094794SBarry Smith       jj[cnt++] = garray[jo[j]];
291982094794SBarry Smith   CHKMEMQ;
292082094794SBarry Smith     }
292182094794SBarry Smith   }
292282094794SBarry Smith   ierr = MatCreateMPIAdj(((PetscObject)B)->comm,M,B->cmap->N/B->rmap->bs,ii,jj,PETSC_NULL,adj);CHKERRQ(ierr);
292382094794SBarry Smith   PetscFunctionReturn(0);
292482094794SBarry Smith }
2925dbf0e21dSBarry Smith EXTERN_C_END
292682094794SBarry Smith 
2927450b117fSShri Abhyankar EXTERN_C_BEGIN
2928450b117fSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
2929450b117fSShri Abhyankar extern PetscErrorCode MatGetFactor_mpibaij_mumps(Mat,MatFactorType,Mat*);
2930450b117fSShri Abhyankar #endif
2931450b117fSShri Abhyankar EXTERN_C_END
2932450b117fSShri Abhyankar 
29330bad9183SKris Buschelman /*MC
2934fafad747SKris Buschelman    MATMPIBAIJ - MATMPIBAIJ = "mpibaij" - A matrix type to be used for distributed block sparse matrices.
29350bad9183SKris Buschelman 
29360bad9183SKris Buschelman    Options Database Keys:
29378c07d4e3SBarry Smith + -mat_type mpibaij - sets the matrix type to "mpibaij" during a call to MatSetFromOptions()
29388c07d4e3SBarry Smith . -mat_block_size <bs> - set the blocksize used to store the matrix
29398c07d4e3SBarry Smith - -mat_use_hash_table <fact>
29400bad9183SKris Buschelman 
29410bad9183SKris Buschelman   Level: beginner
29420bad9183SKris Buschelman 
29430bad9183SKris Buschelman .seealso: MatCreateMPIBAIJ
29440bad9183SKris Buschelman M*/
29450bad9183SKris Buschelman 
294692b32695SKris Buschelman EXTERN_C_BEGIN
2947a23d5eceSKris Buschelman #undef __FUNCT__
29484a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIBAIJ"
2949be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MPIBAIJ(Mat B)
2950273d9f13SBarry Smith {
2951273d9f13SBarry Smith   Mat_MPIBAIJ    *b;
2952dfbe8321SBarry Smith   PetscErrorCode ierr;
2953273d9f13SBarry Smith   PetscTruth     flg;
2954273d9f13SBarry Smith 
2955273d9f13SBarry Smith   PetscFunctionBegin;
295638f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_MPIBAIJ,&b);CHKERRQ(ierr);
295782502324SSatish Balay   B->data = (void*)b;
295882502324SSatish Balay 
2959085a36d4SBarry Smith 
2960273d9f13SBarry Smith   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2961273d9f13SBarry Smith   B->mapping    = 0;
2962273d9f13SBarry Smith   B->assembled  = PETSC_FALSE;
2963273d9f13SBarry Smith 
2964273d9f13SBarry Smith   B->insertmode = NOT_SET_VALUES;
29657adad957SLisandro Dalcin   ierr = MPI_Comm_rank(((PetscObject)B)->comm,&b->rank);CHKERRQ(ierr);
29667adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&b->size);CHKERRQ(ierr);
2967273d9f13SBarry Smith 
2968273d9f13SBarry Smith   /* build local table of row and column ownerships */
2969899cda47SBarry Smith   ierr = PetscMalloc((b->size+1)*sizeof(PetscInt),&b->rangebs);CHKERRQ(ierr);
2970273d9f13SBarry Smith 
2971273d9f13SBarry Smith   /* build cache for off array entries formed */
29727adad957SLisandro Dalcin   ierr = MatStashCreate_Private(((PetscObject)B)->comm,1,&B->stash);CHKERRQ(ierr);
2973273d9f13SBarry Smith   b->donotstash  = PETSC_FALSE;
2974273d9f13SBarry Smith   b->colmap      = PETSC_NULL;
2975273d9f13SBarry Smith   b->garray      = PETSC_NULL;
2976273d9f13SBarry Smith   b->roworiented = PETSC_TRUE;
2977273d9f13SBarry Smith 
2978273d9f13SBarry Smith   /* stuff used in block assembly */
2979273d9f13SBarry Smith   b->barray       = 0;
2980273d9f13SBarry Smith 
2981273d9f13SBarry Smith   /* stuff used for matrix vector multiply */
2982273d9f13SBarry Smith   b->lvec         = 0;
2983273d9f13SBarry Smith   b->Mvctx        = 0;
2984273d9f13SBarry Smith 
2985273d9f13SBarry Smith   /* stuff for MatGetRow() */
2986273d9f13SBarry Smith   b->rowindices   = 0;
2987273d9f13SBarry Smith   b->rowvalues    = 0;
2988273d9f13SBarry Smith   b->getrowactive = PETSC_FALSE;
2989273d9f13SBarry Smith 
2990273d9f13SBarry Smith   /* hash table stuff */
2991273d9f13SBarry Smith   b->ht           = 0;
2992273d9f13SBarry Smith   b->hd           = 0;
2993273d9f13SBarry Smith   b->ht_size      = 0;
2994273d9f13SBarry Smith   b->ht_flag      = PETSC_FALSE;
2995273d9f13SBarry Smith   b->ht_fact      = 0;
2996273d9f13SBarry Smith   b->ht_total_ct  = 0;
2997273d9f13SBarry Smith   b->ht_insert_ct = 0;
2998273d9f13SBarry Smith 
29997adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)B)->comm,PETSC_NULL,"Options for loading MPIBAIJ matrix 1","Mat");CHKERRQ(ierr);
30008c07d4e3SBarry Smith     ierr = PetscOptionsTruth("-mat_use_hash_table","Use hash table to save memory in constructing matrix","MatSetOption",PETSC_FALSE,&flg,PETSC_NULL);CHKERRQ(ierr);
3001273d9f13SBarry Smith     if (flg) {
3002f6275e2eSBarry Smith       PetscReal fact = 1.39;
30034e0d8c25SBarry Smith       ierr = MatSetOption(B,MAT_USE_HASH_TABLE,PETSC_TRUE);CHKERRQ(ierr);
30048c07d4e3SBarry Smith       ierr = PetscOptionsReal("-mat_use_hash_table","Use hash table factor","MatMPIBAIJSetHashTableFactor",fact,&fact,PETSC_NULL);CHKERRQ(ierr);
3005273d9f13SBarry Smith       if (fact <= 1.0) fact = 1.39;
3006273d9f13SBarry Smith       ierr = MatMPIBAIJSetHashTableFactor(B,fact);CHKERRQ(ierr);
30071e2582c4SBarry Smith       ierr = PetscInfo1(B,"Hash table Factor used %5.2f\n",fact);CHKERRQ(ierr);
3008273d9f13SBarry Smith     }
30098c07d4e3SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
30108c07d4e3SBarry Smith 
3011450b117fSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
3012450b117fSShri Abhyankar   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", "MatGetFactor_mpibaij_mumps",MatGetFactor_mpibaij_mumps);CHKERRQ(ierr);
3013450b117fSShri Abhyankar #endif
301482094794SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpibaij_mpiadj_C",
301582094794SBarry Smith                                      "MatConvert_MPIBAIJ_MPIAdj",
301682094794SBarry Smith                                       MatConvert_MPIBAIJ_MPIAdj);CHKERRQ(ierr);
3017273d9f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3018273d9f13SBarry Smith                                      "MatStoreValues_MPIBAIJ",
3019273d9f13SBarry Smith                                      MatStoreValues_MPIBAIJ);CHKERRQ(ierr);
3020273d9f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3021273d9f13SBarry Smith                                      "MatRetrieveValues_MPIBAIJ",
3022273d9f13SBarry Smith                                      MatRetrieveValues_MPIBAIJ);CHKERRQ(ierr);
3023273d9f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
3024273d9f13SBarry Smith                                      "MatGetDiagonalBlock_MPIBAIJ",
3025273d9f13SBarry Smith                                      MatGetDiagonalBlock_MPIBAIJ);CHKERRQ(ierr);
3026a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIBAIJSetPreallocation_C",
3027a23d5eceSKris Buschelman                                      "MatMPIBAIJSetPreallocation_MPIBAIJ",
3028a23d5eceSKris Buschelman                                      MatMPIBAIJSetPreallocation_MPIBAIJ);CHKERRQ(ierr);
3029aac34f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIBAIJSetPreallocationCSR_C",
303044ec7894SLisandro Dalcin 				     "MatMPIBAIJSetPreallocationCSR_MPIBAIJ",
3031aac34f13SBarry Smith 				     MatMPIBAIJSetPreallocationCSR_MPIBAIJ);CHKERRQ(ierr);
303292b32695SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
303392b32695SKris Buschelman                                      "MatDiagonalScaleLocal_MPIBAIJ",
303492b32695SKris Buschelman                                      MatDiagonalScaleLocal_MPIBAIJ);CHKERRQ(ierr);
30355bf65638SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSetHashTableFactor_C",
30365bf65638SKris Buschelman                                      "MatSetHashTableFactor_MPIBAIJ",
30375bf65638SKris Buschelman                                      MatSetHashTableFactor_MPIBAIJ);CHKERRQ(ierr);
303817667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIBAIJ);CHKERRQ(ierr);
3039273d9f13SBarry Smith   PetscFunctionReturn(0);
3040273d9f13SBarry Smith }
3041273d9f13SBarry Smith EXTERN_C_END
3042273d9f13SBarry Smith 
3043209238afSKris Buschelman /*MC
3044002d173eSKris Buschelman    MATBAIJ - MATBAIJ = "baij" - A matrix type to be used for block sparse matrices.
3045209238afSKris Buschelman 
3046209238afSKris Buschelman    This matrix type is identical to MATSEQBAIJ when constructed with a single process communicator,
3047209238afSKris Buschelman    and MATMPIBAIJ otherwise.
3048209238afSKris Buschelman 
3049209238afSKris Buschelman    Options Database Keys:
3050209238afSKris Buschelman . -mat_type baij - sets the matrix type to "baij" during a call to MatSetFromOptions()
3051209238afSKris Buschelman 
3052209238afSKris Buschelman   Level: beginner
3053209238afSKris Buschelman 
3054aac34f13SBarry Smith .seealso: MatCreateMPIBAIJ(),MATSEQBAIJ,MATMPIBAIJ, MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
3055209238afSKris Buschelman M*/
3056209238afSKris Buschelman 
3057209238afSKris Buschelman EXTERN_C_BEGIN
3058209238afSKris Buschelman #undef __FUNCT__
3059209238afSKris Buschelman #define __FUNCT__ "MatCreate_BAIJ"
3060be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_BAIJ(Mat A)
3061dfbe8321SBarry Smith {
30626849ba73SBarry Smith   PetscErrorCode ierr;
3063b24ad042SBarry Smith   PetscMPIInt    size;
3064209238afSKris Buschelman 
3065209238afSKris Buschelman   PetscFunctionBegin;
30667adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)A)->comm,&size);CHKERRQ(ierr);
3067209238afSKris Buschelman   if (size == 1) {
3068209238afSKris Buschelman     ierr = MatSetType(A,MATSEQBAIJ);CHKERRQ(ierr);
3069209238afSKris Buschelman   } else {
3070209238afSKris Buschelman     ierr = MatSetType(A,MATMPIBAIJ);CHKERRQ(ierr);
3071209238afSKris Buschelman   }
3072209238afSKris Buschelman   PetscFunctionReturn(0);
3073209238afSKris Buschelman }
3074209238afSKris Buschelman EXTERN_C_END
3075209238afSKris Buschelman 
30764a2ae208SSatish Balay #undef __FUNCT__
30774a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJSetPreallocation"
3078273d9f13SBarry Smith /*@C
3079aac34f13SBarry Smith    MatMPIBAIJSetPreallocation - Allocates memory for a sparse parallel matrix in block AIJ format
3080273d9f13SBarry Smith    (block compressed row).  For good matrix assembly performance
3081273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
3082273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
3083273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
3084273d9f13SBarry Smith 
3085273d9f13SBarry Smith    Collective on Mat
3086273d9f13SBarry Smith 
3087273d9f13SBarry Smith    Input Parameters:
3088273d9f13SBarry Smith +  A - the matrix
3089273d9f13SBarry Smith .  bs   - size of blockk
3090273d9f13SBarry Smith .  d_nz  - number of block nonzeros per block row in diagonal portion of local
3091273d9f13SBarry Smith            submatrix  (same for all local rows)
3092273d9f13SBarry Smith .  d_nnz - array containing the number of block nonzeros in the various block rows
3093273d9f13SBarry Smith            of the in diagonal portion of the local (possibly different for each block
3094273d9f13SBarry Smith            row) or PETSC_NULL.  You must leave room for the diagonal entry even if it is zero.
3095273d9f13SBarry Smith .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
3096273d9f13SBarry Smith            submatrix (same for all local rows).
3097273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various block rows of the
3098273d9f13SBarry Smith            off-diagonal portion of the local submatrix (possibly different for
3099273d9f13SBarry Smith            each block row) or PETSC_NULL.
3100273d9f13SBarry Smith 
310149a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
3102273d9f13SBarry Smith 
3103273d9f13SBarry Smith    Options Database Keys:
31048c07d4e3SBarry Smith +   -mat_block_size - size of the blocks to use
31058c07d4e3SBarry Smith -   -mat_use_hash_table <fact>
3106273d9f13SBarry Smith 
3107273d9f13SBarry Smith    Notes:
3108273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one processor
3109273d9f13SBarry Smith    than it must be used on all processors that share the object for that argument.
3110273d9f13SBarry Smith 
3111273d9f13SBarry Smith    Storage Information:
3112273d9f13SBarry Smith    For a square global matrix we define each processor's diagonal portion
3113273d9f13SBarry Smith    to be its local rows and the corresponding columns (a square submatrix);
3114273d9f13SBarry Smith    each processor's off-diagonal portion encompasses the remainder of the
3115273d9f13SBarry Smith    local matrix (a rectangular submatrix).
3116273d9f13SBarry Smith 
3117273d9f13SBarry Smith    The user can specify preallocated storage for the diagonal part of
3118273d9f13SBarry Smith    the local submatrix with either d_nz or d_nnz (not both).  Set
3119273d9f13SBarry Smith    d_nz=PETSC_DEFAULT and d_nnz=PETSC_NULL for PETSc to control dynamic
3120273d9f13SBarry Smith    memory allocation.  Likewise, specify preallocated storage for the
3121273d9f13SBarry Smith    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
3122273d9f13SBarry Smith 
3123273d9f13SBarry Smith    Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
3124273d9f13SBarry Smith    the figure below we depict these three local rows and all columns (0-11).
3125273d9f13SBarry Smith 
3126273d9f13SBarry Smith .vb
3127273d9f13SBarry Smith            0 1 2 3 4 5 6 7 8 9 10 11
3128273d9f13SBarry Smith           -------------------
3129273d9f13SBarry Smith    row 3  |  o o o d d d o o o o o o
3130273d9f13SBarry Smith    row 4  |  o o o d d d o o o o o o
3131273d9f13SBarry Smith    row 5  |  o o o d d d o o o o o o
3132273d9f13SBarry Smith           -------------------
3133273d9f13SBarry Smith .ve
3134273d9f13SBarry Smith 
3135273d9f13SBarry Smith    Thus, any entries in the d locations are stored in the d (diagonal)
3136273d9f13SBarry Smith    submatrix, and any entries in the o locations are stored in the
3137273d9f13SBarry Smith    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
3138273d9f13SBarry Smith    stored simply in the MATSEQBAIJ format for compressed row storage.
3139273d9f13SBarry Smith 
3140273d9f13SBarry Smith    Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3141273d9f13SBarry Smith    and o_nz should indicate the number of block nonzeros per row in the o matrix.
3142273d9f13SBarry Smith    In general, for PDE problems in which most nonzeros are near the diagonal,
3143273d9f13SBarry Smith    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
3144273d9f13SBarry Smith    or you will get TERRIBLE performance; see the users' manual chapter on
3145273d9f13SBarry Smith    matrices.
3146273d9f13SBarry Smith 
3147aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3148aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3149aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3150aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3151aa95bbe8SBarry Smith 
3152273d9f13SBarry Smith    Level: intermediate
3153273d9f13SBarry Smith 
3154273d9f13SBarry Smith .keywords: matrix, block, aij, compressed row, sparse, parallel
3155273d9f13SBarry Smith 
3156aac34f13SBarry Smith .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateMPIBAIJ(), MatMPIBAIJSetPreallocationCSR()
3157273d9f13SBarry Smith @*/
3158be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3159273d9f13SBarry Smith {
3160b24ad042SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,PetscInt,const PetscInt[],PetscInt,const PetscInt[]);
3161273d9f13SBarry Smith 
3162273d9f13SBarry Smith   PetscFunctionBegin;
3163a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIBAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
3164a23d5eceSKris Buschelman   if (f) {
3165a23d5eceSKris Buschelman     ierr = (*f)(B,bs,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
3166273d9f13SBarry Smith   }
3167273d9f13SBarry Smith   PetscFunctionReturn(0);
3168273d9f13SBarry Smith }
3169273d9f13SBarry Smith 
31704a2ae208SSatish Balay #undef __FUNCT__
31714a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIBAIJ"
317279bdfe76SSatish Balay /*@C
317379bdfe76SSatish Balay    MatCreateMPIBAIJ - Creates a sparse parallel matrix in block AIJ format
317479bdfe76SSatish Balay    (block compressed row).  For good matrix assembly performance
317579bdfe76SSatish Balay    the user should preallocate the matrix storage by setting the parameters
317679bdfe76SSatish Balay    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
317779bdfe76SSatish Balay    performance can be increased by more than a factor of 50.
317879bdfe76SSatish Balay 
3179db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3180db81eaa0SLois Curfman McInnes 
318179bdfe76SSatish Balay    Input Parameters:
3182db81eaa0SLois Curfman McInnes +  comm - MPI communicator
318379bdfe76SSatish Balay .  bs   - size of blockk
318479bdfe76SSatish Balay .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
318592e8d321SLois Curfman McInnes            This value should be the same as the local size used in creating the
318692e8d321SLois Curfman McInnes            y vector for the matrix-vector product y = Ax.
318792e8d321SLois Curfman McInnes .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
318892e8d321SLois Curfman McInnes            This value should be the same as the local size used in creating the
318992e8d321SLois Curfman McInnes            x vector for the matrix-vector product y = Ax.
3190be79a94dSBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3191be79a94dSBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
319247a75d0bSBarry Smith .  d_nz  - number of nonzero blocks per block row in diagonal portion of local
319379bdfe76SSatish Balay            submatrix  (same for all local rows)
319447a75d0bSBarry Smith .  d_nnz - array containing the number of nonzero blocks in the various block rows
319592e8d321SLois Curfman McInnes            of the in diagonal portion of the local (possibly different for each block
3196db81eaa0SLois Curfman McInnes            row) or PETSC_NULL.  You must leave room for the diagonal entry even if it is zero.
319747a75d0bSBarry Smith .  o_nz  - number of nonzero blocks per block row in the off-diagonal portion of local
319879bdfe76SSatish Balay            submatrix (same for all local rows).
319947a75d0bSBarry Smith -  o_nnz - array containing the number of nonzero blocks in the various block rows of the
320092e8d321SLois Curfman McInnes            off-diagonal portion of the local submatrix (possibly different for
320192e8d321SLois Curfman McInnes            each block row) or PETSC_NULL.
320279bdfe76SSatish Balay 
320379bdfe76SSatish Balay    Output Parameter:
320479bdfe76SSatish Balay .  A - the matrix
320579bdfe76SSatish Balay 
3206db81eaa0SLois Curfman McInnes    Options Database Keys:
32078c07d4e3SBarry Smith +   -mat_block_size - size of the blocks to use
32088c07d4e3SBarry Smith -   -mat_use_hash_table <fact>
32093ffaccefSLois Curfman McInnes 
3210175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3211ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3212175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3213175b88e8SBarry Smith 
3214b259b22eSLois Curfman McInnes    Notes:
321549a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
321649a6f317SBarry Smith 
321747a75d0bSBarry Smith    A nonzero block is any block that as 1 or more nonzeros in it
321847a75d0bSBarry Smith 
321979bdfe76SSatish Balay    The user MUST specify either the local or global matrix dimensions
322079bdfe76SSatish Balay    (possibly both).
322179bdfe76SSatish Balay 
3222be79a94dSBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one processor
3223be79a94dSBarry Smith    than it must be used on all processors that share the object for that argument.
3224be79a94dSBarry Smith 
322579bdfe76SSatish Balay    Storage Information:
322679bdfe76SSatish Balay    For a square global matrix we define each processor's diagonal portion
322779bdfe76SSatish Balay    to be its local rows and the corresponding columns (a square submatrix);
322879bdfe76SSatish Balay    each processor's off-diagonal portion encompasses the remainder of the
322979bdfe76SSatish Balay    local matrix (a rectangular submatrix).
323079bdfe76SSatish Balay 
323179bdfe76SSatish Balay    The user can specify preallocated storage for the diagonal part of
323279bdfe76SSatish Balay    the local submatrix with either d_nz or d_nnz (not both).  Set
323379bdfe76SSatish Balay    d_nz=PETSC_DEFAULT and d_nnz=PETSC_NULL for PETSc to control dynamic
323479bdfe76SSatish Balay    memory allocation.  Likewise, specify preallocated storage for the
323579bdfe76SSatish Balay    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
323679bdfe76SSatish Balay 
323779bdfe76SSatish Balay    Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
323879bdfe76SSatish Balay    the figure below we depict these three local rows and all columns (0-11).
323979bdfe76SSatish Balay 
3240db81eaa0SLois Curfman McInnes .vb
3241db81eaa0SLois Curfman McInnes            0 1 2 3 4 5 6 7 8 9 10 11
3242db81eaa0SLois Curfman McInnes           -------------------
3243db81eaa0SLois Curfman McInnes    row 3  |  o o o d d d o o o o o o
3244db81eaa0SLois Curfman McInnes    row 4  |  o o o d d d o o o o o o
3245db81eaa0SLois Curfman McInnes    row 5  |  o o o d d d o o o o o o
3246db81eaa0SLois Curfman McInnes           -------------------
3247db81eaa0SLois Curfman McInnes .ve
324879bdfe76SSatish Balay 
324979bdfe76SSatish Balay    Thus, any entries in the d locations are stored in the d (diagonal)
325079bdfe76SSatish Balay    submatrix, and any entries in the o locations are stored in the
325179bdfe76SSatish Balay    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
325257b952d6SSatish Balay    stored simply in the MATSEQBAIJ format for compressed row storage.
325379bdfe76SSatish Balay 
3254d64ed03dSBarry Smith    Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3255d64ed03dSBarry Smith    and o_nz should indicate the number of block nonzeros per row in the o matrix.
325679bdfe76SSatish Balay    In general, for PDE problems in which most nonzeros are near the diagonal,
325792e8d321SLois Curfman McInnes    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
325892e8d321SLois Curfman McInnes    or you will get TERRIBLE performance; see the users' manual chapter on
32596da5968aSLois Curfman McInnes    matrices.
326079bdfe76SSatish Balay 
3261027ccd11SLois Curfman McInnes    Level: intermediate
3262027ccd11SLois Curfman McInnes 
326392e8d321SLois Curfman McInnes .keywords: matrix, block, aij, compressed row, sparse, parallel
326479bdfe76SSatish Balay 
3265aac34f13SBarry Smith .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateMPIBAIJ(), MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
326679bdfe76SSatish Balay @*/
3267be1d678aSKris 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)
326879bdfe76SSatish Balay {
32696849ba73SBarry Smith   PetscErrorCode ierr;
3270b24ad042SBarry Smith   PetscMPIInt    size;
327179bdfe76SSatish Balay 
3272d64ed03dSBarry Smith   PetscFunctionBegin;
3273f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3274f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr);
3275d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3276273d9f13SBarry Smith   if (size > 1) {
3277273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIBAIJ);CHKERRQ(ierr);
3278273d9f13SBarry Smith     ierr = MatMPIBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
3279273d9f13SBarry Smith   } else {
3280273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQBAIJ);CHKERRQ(ierr);
3281273d9f13SBarry Smith     ierr = MatSeqBAIJSetPreallocation(*A,bs,d_nz,d_nnz);CHKERRQ(ierr);
32823914022bSBarry Smith   }
32833a40ed3dSBarry Smith   PetscFunctionReturn(0);
328479bdfe76SSatish Balay }
3285026e39d0SSatish Balay 
32864a2ae208SSatish Balay #undef __FUNCT__
32874a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIBAIJ"
32886849ba73SBarry Smith static PetscErrorCode MatDuplicate_MPIBAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
32890ac07820SSatish Balay {
32900ac07820SSatish Balay   Mat            mat;
32910ac07820SSatish Balay   Mat_MPIBAIJ    *a,*oldmat = (Mat_MPIBAIJ*)matin->data;
3292dfbe8321SBarry Smith   PetscErrorCode ierr;
3293b24ad042SBarry Smith   PetscInt       len=0;
32940ac07820SSatish Balay 
3295d64ed03dSBarry Smith   PetscFunctionBegin;
32960ac07820SSatish Balay   *newmat       = 0;
32977adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)matin)->comm,&mat);CHKERRQ(ierr);
3298d0f46423SBarry Smith   ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr);
32997adad957SLisandro Dalcin   ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr);
33001d5dac46SHong Zhang   ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
33017fff6886SHong Zhang 
3302d5f3da31SBarry Smith   mat->factortype   = matin->factortype;
3303273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
33040ac07820SSatish Balay   mat->assembled    = PETSC_TRUE;
33057fff6886SHong Zhang   mat->insertmode   = NOT_SET_VALUES;
33067fff6886SHong Zhang 
3307273d9f13SBarry Smith   a      = (Mat_MPIBAIJ*)mat->data;
3308d0f46423SBarry Smith   mat->rmap->bs  = matin->rmap->bs;
33090ac07820SSatish Balay   a->bs2   = oldmat->bs2;
33100ac07820SSatish Balay   a->mbs   = oldmat->mbs;
33110ac07820SSatish Balay   a->nbs   = oldmat->nbs;
33120ac07820SSatish Balay   a->Mbs   = oldmat->Mbs;
33130ac07820SSatish Balay   a->Nbs   = oldmat->Nbs;
33140ac07820SSatish Balay 
331526283091SBarry Smith   ierr = PetscLayoutCopy(matin->rmap,&mat->rmap);CHKERRQ(ierr);
331626283091SBarry Smith   ierr = PetscLayoutCopy(matin->cmap,&mat->cmap);CHKERRQ(ierr);
3317899cda47SBarry Smith 
33180ac07820SSatish Balay   a->size         = oldmat->size;
33190ac07820SSatish Balay   a->rank         = oldmat->rank;
3320aef5e8e0SSatish Balay   a->donotstash   = oldmat->donotstash;
3321aef5e8e0SSatish Balay   a->roworiented  = oldmat->roworiented;
3322aef5e8e0SSatish Balay   a->rowindices   = 0;
33230ac07820SSatish Balay   a->rowvalues    = 0;
33240ac07820SSatish Balay   a->getrowactive = PETSC_FALSE;
332530793edcSSatish Balay   a->barray       = 0;
3326899cda47SBarry Smith   a->rstartbs     = oldmat->rstartbs;
3327899cda47SBarry Smith   a->rendbs       = oldmat->rendbs;
3328899cda47SBarry Smith   a->cstartbs     = oldmat->cstartbs;
3329899cda47SBarry Smith   a->cendbs       = oldmat->cendbs;
33300ac07820SSatish Balay 
3331133cdb44SSatish Balay   /* hash table stuff */
3332133cdb44SSatish Balay   a->ht           = 0;
3333133cdb44SSatish Balay   a->hd           = 0;
3334133cdb44SSatish Balay   a->ht_size      = 0;
3335133cdb44SSatish Balay   a->ht_flag      = oldmat->ht_flag;
333625fdafccSSatish Balay   a->ht_fact      = oldmat->ht_fact;
3337133cdb44SSatish Balay   a->ht_total_ct  = 0;
3338133cdb44SSatish Balay   a->ht_insert_ct = 0;
3339133cdb44SSatish Balay 
3340899cda47SBarry Smith   ierr = PetscMemcpy(a->rangebs,oldmat->rangebs,(a->size+1)*sizeof(PetscInt));CHKERRQ(ierr);
33410ac07820SSatish Balay   if (oldmat->colmap) {
3342aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
33430f5bd95cSBarry Smith   ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
334448e59246SSatish Balay #else
3345b24ad042SBarry Smith   ierr = PetscMalloc((a->Nbs)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr);
334652e6d16bSBarry Smith   ierr = PetscLogObjectMemory(mat,(a->Nbs)*sizeof(PetscInt));CHKERRQ(ierr);
3347b24ad042SBarry Smith   ierr = PetscMemcpy(a->colmap,oldmat->colmap,(a->Nbs)*sizeof(PetscInt));CHKERRQ(ierr);
334848e59246SSatish Balay #endif
33490ac07820SSatish Balay   } else a->colmap = 0;
33504beb1cfeSHong Zhang 
33510ac07820SSatish Balay   if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) {
3352b24ad042SBarry Smith     ierr = PetscMalloc(len*sizeof(PetscInt),&a->garray);CHKERRQ(ierr);
335352e6d16bSBarry Smith     ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr);
3354b24ad042SBarry Smith     ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr);
33550ac07820SSatish Balay   } else a->garray = 0;
33560ac07820SSatish Balay 
3357533163c2SBarry Smith   ierr = MatStashCreate_Private(((PetscObject)matin)->comm,matin->rmap->bs,&mat->bstash);CHKERRQ(ierr);
33580ac07820SSatish Balay   ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
335952e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr);
33600ac07820SSatish Balay   ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
336152e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr);
33627fff6886SHong Zhang 
33632e8a6d31SBarry Smith   ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
336452e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr);
33652e8a6d31SBarry Smith   ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
336652e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr);
33677adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr);
33680ac07820SSatish Balay   *newmat = mat;
33694beb1cfeSHong Zhang 
33703a40ed3dSBarry Smith   PetscFunctionReturn(0);
33710ac07820SSatish Balay }
337257b952d6SSatish Balay 
33734a2ae208SSatish Balay #undef __FUNCT__
33744a2ae208SSatish Balay #define __FUNCT__ "MatLoad_MPIBAIJ"
3375a313700dSBarry Smith PetscErrorCode MatLoad_MPIBAIJ(PetscViewer viewer, const MatType type,Mat *newmat)
337657b952d6SSatish Balay {
337757b952d6SSatish Balay   Mat            A;
33786849ba73SBarry Smith   PetscErrorCode ierr;
3379b24ad042SBarry Smith   int            fd;
3380b24ad042SBarry Smith   PetscInt       i,nz,j,rstart,rend;
338187828ca2SBarry Smith   PetscScalar    *vals,*buf;
338257b952d6SSatish Balay   MPI_Comm       comm = ((PetscObject)viewer)->comm;
338357b952d6SSatish Balay   MPI_Status     status;
3384b24ad042SBarry Smith   PetscMPIInt    rank,size,maxnz;
3385b24ad042SBarry Smith   PetscInt       header[4],*rowlengths = 0,M,N,m,*rowners,*cols;
3386910ba992SMatthew Knepley   PetscInt       *locrowlens = PETSC_NULL,*procsnz = PETSC_NULL,*browners = PETSC_NULL;
3387167e7480SBarry Smith   PetscInt       jj,*mycols,*ibuf,bs=1,Mbs,mbs,extra_rows,mmax;
3388dc231df0SBarry Smith   PetscMPIInt    tag = ((PetscObject)viewer)->tag;
3389910ba992SMatthew Knepley   PetscInt       *dlens = PETSC_NULL,*odlens = PETSC_NULL,*mask = PETSC_NULL,*masked1 = PETSC_NULL,*masked2 = PETSC_NULL,rowcount,odcount;
3390dc231df0SBarry Smith   PetscInt       dcount,kmax,k,nzcount,tmp,mend;
339157b952d6SSatish Balay 
3392d64ed03dSBarry Smith   PetscFunctionBegin;
339377925062SSatish Balay   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading MPIBAIJ matrix 2","Mat");CHKERRQ(ierr);
33948c07d4e3SBarry Smith     ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
33958c07d4e3SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
339657b952d6SSatish Balay 
3397d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3398d132466eSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
339957b952d6SSatish Balay   if (!rank) {
3400b0a32e0cSBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
3401e5638eb3SBarry Smith     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
3402*e32f2f54SBarry Smith     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
34036c5fab8fSBarry Smith   }
3404d64ed03dSBarry Smith 
3405b24ad042SBarry Smith   ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr);
340657b952d6SSatish Balay   M = header[1]; N = header[2];
340757b952d6SSatish Balay 
3408*e32f2f54SBarry Smith   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
340957b952d6SSatish Balay 
341057b952d6SSatish Balay   /*
341157b952d6SSatish Balay      This code adds extra rows to make sure the number of rows is
341257b952d6SSatish Balay      divisible by the blocksize
341357b952d6SSatish Balay   */
341457b952d6SSatish Balay   Mbs        = M/bs;
3415dc231df0SBarry Smith   extra_rows = bs - M + bs*Mbs;
341657b952d6SSatish Balay   if (extra_rows == bs) extra_rows = 0;
341757b952d6SSatish Balay   else                  Mbs++;
341857b952d6SSatish Balay   if (extra_rows && !rank) {
34191e2582c4SBarry Smith     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
342057b952d6SSatish Balay   }
3421537820f0SBarry Smith 
342257b952d6SSatish Balay   /* determine ownership of all rows */
342357b952d6SSatish Balay   mbs        = Mbs/size + ((Mbs % size) > rank);
342457b952d6SSatish Balay   m          = mbs*bs;
3425dc231df0SBarry Smith   ierr       = PetscMalloc2(size+1,PetscInt,&rowners,size+1,PetscInt,&browners);CHKERRQ(ierr);
3426b24ad042SBarry Smith   ierr       = MPI_Allgather(&mbs,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
3427167e7480SBarry Smith 
3428167e7480SBarry Smith   /* process 0 needs enough room for process with most rows */
3429167e7480SBarry Smith   if (!rank) {
3430167e7480SBarry Smith     mmax = rowners[1];
3431167e7480SBarry Smith     for (i=2; i<size; i++) {
3432167e7480SBarry Smith       mmax = PetscMax(mmax,rowners[i]);
3433167e7480SBarry Smith     }
3434ca02efcfSSatish Balay     mmax*=bs;
3435167e7480SBarry Smith   } else mmax = m;
3436167e7480SBarry Smith 
343757b952d6SSatish Balay   rowners[0] = 0;
3438cee3aa6bSSatish Balay   for (i=2; i<=size; i++)  rowners[i] += rowners[i-1];
3439cee3aa6bSSatish Balay   for (i=0; i<=size;  i++) browners[i] = rowners[i]*bs;
344057b952d6SSatish Balay   rstart = rowners[rank];
344157b952d6SSatish Balay   rend   = rowners[rank+1];
344257b952d6SSatish Balay 
344357b952d6SSatish Balay   /* distribute row lengths to all processors */
344419c38ff2SBarry Smith   ierr = PetscMalloc((mmax+1)*sizeof(PetscInt),&locrowlens);CHKERRQ(ierr);
344557b952d6SSatish Balay   if (!rank) {
3446dc231df0SBarry Smith     mend = m;
3447dc231df0SBarry Smith     if (size == 1) mend = mend - extra_rows;
3448dc231df0SBarry Smith     ierr = PetscBinaryRead(fd,locrowlens,mend,PETSC_INT);CHKERRQ(ierr);
3449dc231df0SBarry Smith     for (j=mend; j<m; j++) locrowlens[j] = 1;
3450dc231df0SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
3451b24ad042SBarry Smith     ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr);
3452b24ad042SBarry Smith     ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr);
3453dc231df0SBarry Smith     for (j=0; j<m; j++) {
3454dc231df0SBarry Smith       procsnz[0] += locrowlens[j];
3455dc231df0SBarry Smith     }
3456dc231df0SBarry Smith     for (i=1; i<size; i++) {
3457dc231df0SBarry Smith       mend = browners[i+1] - browners[i];
3458dc231df0SBarry Smith       if (i == size-1) mend = mend - extra_rows;
3459dc231df0SBarry Smith       ierr = PetscBinaryRead(fd,rowlengths,mend,PETSC_INT);CHKERRQ(ierr);
3460dc231df0SBarry Smith       for (j=mend; j<browners[i+1] - browners[i]; j++) rowlengths[j] = 1;
3461dc231df0SBarry Smith       /* calculate the number of nonzeros on each processor */
3462dc231df0SBarry Smith       for (j=0; j<browners[i+1]-browners[i]; j++) {
346357b952d6SSatish Balay         procsnz[i] += rowlengths[j];
346457b952d6SSatish Balay       }
3465dc231df0SBarry Smith       ierr = MPI_Send(rowlengths,browners[i+1]-browners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
346657b952d6SSatish Balay     }
3467606d414cSSatish Balay     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
3468dc231df0SBarry Smith   } else {
3469dc231df0SBarry Smith     ierr = MPI_Recv(locrowlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
3470dc231df0SBarry Smith   }
347157b952d6SSatish Balay 
3472dc231df0SBarry Smith   if (!rank) {
347357b952d6SSatish Balay     /* determine max buffer needed and allocate it */
34748a8e0b3aSBarry Smith     maxnz = procsnz[0];
3475cdc0ba36SBarry Smith     for (i=1; i<size; i++) {
347657b952d6SSatish Balay       maxnz = PetscMax(maxnz,procsnz[i]);
347757b952d6SSatish Balay     }
3478b24ad042SBarry Smith     ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr);
347957b952d6SSatish Balay 
348057b952d6SSatish Balay     /* read in my part of the matrix column indices  */
348157b952d6SSatish Balay     nz     = procsnz[0];
348219c38ff2SBarry Smith     ierr   = PetscMalloc((nz+1)*sizeof(PetscInt),&ibuf);CHKERRQ(ierr);
348357b952d6SSatish Balay     mycols = ibuf;
3484cee3aa6bSSatish Balay     if (size == 1)  nz -= extra_rows;
3485e5638eb3SBarry Smith     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
3486cee3aa6bSSatish Balay     if (size == 1)  for (i=0; i< extra_rows; i++) { mycols[nz+i] = M+i; }
3487cee3aa6bSSatish Balay 
348857b952d6SSatish Balay     /* read in every ones (except the last) and ship off */
348957b952d6SSatish Balay     for (i=1; i<size-1; i++) {
349057b952d6SSatish Balay       nz   = procsnz[i];
3491e5638eb3SBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
3492b24ad042SBarry Smith       ierr = MPI_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
349357b952d6SSatish Balay     }
349457b952d6SSatish Balay     /* read in the stuff for the last proc */
349557b952d6SSatish Balay     if (size != 1) {
349657b952d6SSatish Balay       nz   = procsnz[size-1] - extra_rows;  /* the extra rows are not on the disk */
3497e5638eb3SBarry Smith       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
349857b952d6SSatish Balay       for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
3499b24ad042SBarry Smith       ierr = MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);CHKERRQ(ierr);
350057b952d6SSatish Balay     }
3501606d414cSSatish Balay     ierr = PetscFree(cols);CHKERRQ(ierr);
3502d64ed03dSBarry Smith   } else {
350357b952d6SSatish Balay     /* determine buffer space needed for message */
350457b952d6SSatish Balay     nz = 0;
350557b952d6SSatish Balay     for (i=0; i<m; i++) {
350657b952d6SSatish Balay       nz += locrowlens[i];
350757b952d6SSatish Balay     }
350819c38ff2SBarry Smith     ierr   = PetscMalloc((nz+1)*sizeof(PetscInt),&ibuf);CHKERRQ(ierr);
350957b952d6SSatish Balay     mycols = ibuf;
351057b952d6SSatish Balay     /* receive message of column indices*/
3511b24ad042SBarry Smith     ierr = MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
3512b24ad042SBarry Smith     ierr = MPI_Get_count(&status,MPIU_INT,&maxnz);CHKERRQ(ierr);
3513*e32f2f54SBarry Smith     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
351457b952d6SSatish Balay   }
351557b952d6SSatish Balay 
351657b952d6SSatish Balay   /* loop over local rows, determining number of off diagonal entries */
3517dc231df0SBarry Smith   ierr     = PetscMalloc2(rend-rstart,PetscInt,&dlens,rend-rstart,PetscInt,&odlens);CHKERRQ(ierr);
3518dc231df0SBarry Smith   ierr     = PetscMalloc3(Mbs,PetscInt,&mask,Mbs,PetscInt,&masked1,Mbs,PetscInt,&masked2);CHKERRQ(ierr);
3519dc231df0SBarry Smith   ierr     = PetscMemzero(mask,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
3520dc231df0SBarry Smith   ierr     = PetscMemzero(masked1,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
3521dc231df0SBarry Smith   ierr     = PetscMemzero(masked2,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
352257b952d6SSatish Balay   rowcount = 0; nzcount = 0;
352357b952d6SSatish Balay   for (i=0; i<mbs; i++) {
352457b952d6SSatish Balay     dcount  = 0;
352557b952d6SSatish Balay     odcount = 0;
352657b952d6SSatish Balay     for (j=0; j<bs; j++) {
352757b952d6SSatish Balay       kmax = locrowlens[rowcount];
352857b952d6SSatish Balay       for (k=0; k<kmax; k++) {
352957b952d6SSatish Balay         tmp = mycols[nzcount++]/bs;
353057b952d6SSatish Balay         if (!mask[tmp]) {
353157b952d6SSatish Balay           mask[tmp] = 1;
353257b952d6SSatish Balay           if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp;
353357b952d6SSatish Balay           else masked1[dcount++] = tmp;
353457b952d6SSatish Balay         }
353557b952d6SSatish Balay       }
353657b952d6SSatish Balay       rowcount++;
353757b952d6SSatish Balay     }
3538cee3aa6bSSatish Balay 
353957b952d6SSatish Balay     dlens[i]  = dcount;
354057b952d6SSatish Balay     odlens[i] = odcount;
3541cee3aa6bSSatish Balay 
354257b952d6SSatish Balay     /* zero out the mask elements we set */
354357b952d6SSatish Balay     for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
354457b952d6SSatish Balay     for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
354557b952d6SSatish Balay   }
3546cee3aa6bSSatish Balay 
354757b952d6SSatish Balay   /* create our matrix */
3548f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&A);CHKERRQ(ierr);
3549f69a0ea3SMatthew Knepley   ierr = MatSetSizes(A,m,m,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
355078ae41b4SKris Buschelman   ierr = MatSetType(A,type);CHKERRQ(ierr)
355178ae41b4SKris Buschelman   ierr = MatMPIBAIJSetPreallocation(A,bs,0,dlens,0,odlens);CHKERRQ(ierr);
355278ae41b4SKris Buschelman 
355357b952d6SSatish Balay   if (!rank) {
355419c38ff2SBarry Smith     ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&buf);CHKERRQ(ierr);
355557b952d6SSatish Balay     /* read in my part of the matrix numerical values  */
355657b952d6SSatish Balay     nz = procsnz[0];
355757b952d6SSatish Balay     vals = buf;
3558cee3aa6bSSatish Balay     mycols = ibuf;
3559cee3aa6bSSatish Balay     if (size == 1)  nz -= extra_rows;
3560e5638eb3SBarry Smith     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
3561cee3aa6bSSatish Balay     if (size == 1)  for (i=0; i< extra_rows; i++) { vals[nz+i] = 1.0; }
3562537820f0SBarry Smith 
356357b952d6SSatish Balay     /* insert into matrix */
356457b952d6SSatish Balay     jj      = rstart*bs;
356557b952d6SSatish Balay     for (i=0; i<m; i++) {
3566dc231df0SBarry Smith       ierr = MatSetValues_MPIBAIJ(A,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);CHKERRQ(ierr);
356757b952d6SSatish Balay       mycols += locrowlens[i];
356857b952d6SSatish Balay       vals   += locrowlens[i];
356957b952d6SSatish Balay       jj++;
357057b952d6SSatish Balay     }
357157b952d6SSatish Balay     /* read in other processors (except the last one) and ship out */
357257b952d6SSatish Balay     for (i=1; i<size-1; i++) {
357357b952d6SSatish Balay       nz   = procsnz[i];
357457b952d6SSatish Balay       vals = buf;
3575e5638eb3SBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
35767adad957SLisandro Dalcin       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)A)->tag,comm);CHKERRQ(ierr);
357757b952d6SSatish Balay     }
357857b952d6SSatish Balay     /* the last proc */
357957b952d6SSatish Balay     if (size != 1){
358057b952d6SSatish Balay       nz   = procsnz[i] - extra_rows;
3581cee3aa6bSSatish Balay       vals = buf;
3582e5638eb3SBarry Smith       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
358357b952d6SSatish Balay       for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
35847adad957SLisandro Dalcin       ierr = MPI_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,((PetscObject)A)->tag,comm);CHKERRQ(ierr);
358557b952d6SSatish Balay     }
3586606d414cSSatish Balay     ierr = PetscFree(procsnz);CHKERRQ(ierr);
3587d64ed03dSBarry Smith   } else {
358857b952d6SSatish Balay     /* receive numeric values */
358919c38ff2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&buf);CHKERRQ(ierr);
359057b952d6SSatish Balay 
359157b952d6SSatish Balay     /* receive message of values*/
359257b952d6SSatish Balay     vals   = buf;
3593cee3aa6bSSatish Balay     mycols = ibuf;
35947adad957SLisandro Dalcin     ierr   = MPI_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)A)->tag,comm,&status);CHKERRQ(ierr);
3595ca161407SBarry Smith     ierr   = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
3596*e32f2f54SBarry Smith     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
359757b952d6SSatish Balay 
359857b952d6SSatish Balay     /* insert into matrix */
359957b952d6SSatish Balay     jj      = rstart*bs;
3600cee3aa6bSSatish Balay     for (i=0; i<m; i++) {
3601dc231df0SBarry Smith       ierr    = MatSetValues_MPIBAIJ(A,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);CHKERRQ(ierr);
360257b952d6SSatish Balay       mycols += locrowlens[i];
360357b952d6SSatish Balay       vals   += locrowlens[i];
360457b952d6SSatish Balay       jj++;
360557b952d6SSatish Balay     }
360657b952d6SSatish Balay   }
3607606d414cSSatish Balay   ierr = PetscFree(locrowlens);CHKERRQ(ierr);
3608606d414cSSatish Balay   ierr = PetscFree(buf);CHKERRQ(ierr);
3609606d414cSSatish Balay   ierr = PetscFree(ibuf);CHKERRQ(ierr);
3610dc231df0SBarry Smith   ierr = PetscFree2(rowners,browners);CHKERRQ(ierr);
3611dc231df0SBarry Smith   ierr = PetscFree2(dlens,odlens);CHKERRQ(ierr);
3612dc231df0SBarry Smith   ierr = PetscFree3(mask,masked1,masked2);CHKERRQ(ierr);
36136d4a8577SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
36146d4a8577SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
361578ae41b4SKris Buschelman 
361678ae41b4SKris Buschelman   *newmat = A;
36173a40ed3dSBarry Smith   PetscFunctionReturn(0);
361857b952d6SSatish Balay }
361957b952d6SSatish Balay 
36204a2ae208SSatish Balay #undef __FUNCT__
36214a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJSetHashTableFactor"
3622133cdb44SSatish Balay /*@
3623133cdb44SSatish Balay    MatMPIBAIJSetHashTableFactor - Sets the factor required to compute the size of the HashTable.
3624133cdb44SSatish Balay 
3625133cdb44SSatish Balay    Input Parameters:
3626133cdb44SSatish Balay .  mat  - the matrix
3627133cdb44SSatish Balay .  fact - factor
3628133cdb44SSatish Balay 
3629fee21e36SBarry Smith    Collective on Mat
3630fee21e36SBarry Smith 
36318c890885SBarry Smith    Level: advanced
36328c890885SBarry Smith 
3633133cdb44SSatish Balay   Notes:
36348c07d4e3SBarry Smith    This can also be set by the command line option: -mat_use_hash_table <fact>
3635133cdb44SSatish Balay 
3636133cdb44SSatish Balay .keywords: matrix, hashtable, factor, HT
3637133cdb44SSatish Balay 
3638133cdb44SSatish Balay .seealso: MatSetOption()
3639133cdb44SSatish Balay @*/
3640be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJSetHashTableFactor(Mat mat,PetscReal fact)
3641133cdb44SSatish Balay {
3642dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscReal);
36435bf65638SKris Buschelman 
36445bf65638SKris Buschelman   PetscFunctionBegin;
36455bf65638SKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSetHashTableFactor_C",(void (**)(void))&f);CHKERRQ(ierr);
36465bf65638SKris Buschelman   if (f) {
36475bf65638SKris Buschelman     ierr = (*f)(mat,fact);CHKERRQ(ierr);
36485bf65638SKris Buschelman   }
36495bf65638SKris Buschelman   PetscFunctionReturn(0);
36505bf65638SKris Buschelman }
36515bf65638SKris Buschelman 
3652be1d678aSKris Buschelman EXTERN_C_BEGIN
36535bf65638SKris Buschelman #undef __FUNCT__
36545bf65638SKris Buschelman #define __FUNCT__ "MatSetHashTableFactor_MPIBAIJ"
3655be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSetHashTableFactor_MPIBAIJ(Mat mat,PetscReal fact)
36565bf65638SKris Buschelman {
365725fdafccSSatish Balay   Mat_MPIBAIJ *baij;
3658133cdb44SSatish Balay 
3659133cdb44SSatish Balay   PetscFunctionBegin;
3660133cdb44SSatish Balay   baij = (Mat_MPIBAIJ*)mat->data;
3661133cdb44SSatish Balay   baij->ht_fact = fact;
3662133cdb44SSatish Balay   PetscFunctionReturn(0);
3663133cdb44SSatish Balay }
3664be1d678aSKris Buschelman EXTERN_C_END
3665f2a5309cSSatish Balay 
36664a2ae208SSatish Balay #undef __FUNCT__
36674a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJGetSeqBAIJ"
3668be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJGetSeqBAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[])
3669f2a5309cSSatish Balay {
3670f2a5309cSSatish Balay   Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
3671f2a5309cSSatish Balay   PetscFunctionBegin;
3672f2a5309cSSatish Balay   *Ad     = a->A;
3673f2a5309cSSatish Balay   *Ao     = a->B;
3674195d93cdSBarry Smith   *colmap = a->garray;
3675f2a5309cSSatish Balay   PetscFunctionReturn(0);
3676f2a5309cSSatish Balay }
367785535b8eSBarry Smith 
367885535b8eSBarry Smith /*
367985535b8eSBarry Smith     Special version for direct calls from Fortran (to eliminate two function call overheads
368085535b8eSBarry Smith */
368185535b8eSBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
368285535b8eSBarry Smith #define matmpibaijsetvaluesblocked_ MATMPIBAIJSETVALUESBLOCKED
368385535b8eSBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
368485535b8eSBarry Smith #define matmpibaijsetvaluesblocked_ matmpibaijsetvaluesblocked
368585535b8eSBarry Smith #endif
368685535b8eSBarry Smith 
368785535b8eSBarry Smith #undef __FUNCT__
368885535b8eSBarry Smith #define __FUNCT__ "matmpibiajsetvaluesblocked"
368985535b8eSBarry Smith /*@C
369085535b8eSBarry Smith   MatMPIBAIJSetValuesBlocked - Direct Fortran call to replace call to MatSetValuesBlocked()
369185535b8eSBarry Smith 
369285535b8eSBarry Smith   Collective on Mat
369385535b8eSBarry Smith 
369485535b8eSBarry Smith   Input Parameters:
369585535b8eSBarry Smith + mat - the matrix
369685535b8eSBarry Smith . min - number of input rows
369785535b8eSBarry Smith . im - input rows
369885535b8eSBarry Smith . nin - number of input columns
369985535b8eSBarry Smith . in - input columns
370085535b8eSBarry Smith . v - numerical values input
370185535b8eSBarry Smith - addvin - INSERT_VALUES or ADD_VALUES
370285535b8eSBarry Smith 
370385535b8eSBarry Smith   Notes: This has a complete copy of MatSetValuesBlocked_MPIBAIJ() which is terrible code un-reuse.
370485535b8eSBarry Smith 
370585535b8eSBarry Smith   Level: advanced
370685535b8eSBarry Smith 
370785535b8eSBarry Smith .seealso:   MatSetValuesBlocked()
370885535b8eSBarry Smith @*/
370985535b8eSBarry Smith PetscErrorCode matmpibaijsetvaluesblocked_(Mat *matin,PetscInt *min,const PetscInt im[],PetscInt *nin,const PetscInt in[],const MatScalar v[],InsertMode *addvin)
371085535b8eSBarry Smith {
371185535b8eSBarry Smith   /* convert input arguments to C version */
371285535b8eSBarry Smith   Mat             mat = *matin;
371385535b8eSBarry Smith   PetscInt        m = *min, n = *nin;
371485535b8eSBarry Smith   InsertMode      addv = *addvin;
371585535b8eSBarry Smith 
371685535b8eSBarry Smith   Mat_MPIBAIJ     *baij = (Mat_MPIBAIJ*)mat->data;
371785535b8eSBarry Smith   const MatScalar *value;
371885535b8eSBarry Smith   MatScalar       *barray=baij->barray;
371985535b8eSBarry Smith   PetscTruth      roworiented = baij->roworiented;
372085535b8eSBarry Smith   PetscErrorCode  ierr;
372185535b8eSBarry Smith   PetscInt        i,j,ii,jj,row,col,rstart=baij->rstartbs;
372285535b8eSBarry Smith   PetscInt        rend=baij->rendbs,cstart=baij->cstartbs,stepval;
3723d0f46423SBarry Smith   PetscInt        cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;
372485535b8eSBarry Smith 
372585535b8eSBarry Smith   PetscFunctionBegin;
372685535b8eSBarry Smith   /* tasks normally handled by MatSetValuesBlocked() */
372785535b8eSBarry Smith   if (mat->insertmode == NOT_SET_VALUES) {
372885535b8eSBarry Smith     mat->insertmode = addv;
372985535b8eSBarry Smith   }
373085535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
373185535b8eSBarry Smith   else if (mat->insertmode != addv) {
3732*e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
373385535b8eSBarry Smith   }
3734*e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
373585535b8eSBarry Smith #endif
373685535b8eSBarry Smith   if (mat->assembled) {
373785535b8eSBarry Smith     mat->was_assembled = PETSC_TRUE;
373885535b8eSBarry Smith     mat->assembled     = PETSC_FALSE;
373985535b8eSBarry Smith   }
374085535b8eSBarry Smith   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
374185535b8eSBarry Smith 
374285535b8eSBarry Smith 
374385535b8eSBarry Smith   if(!barray) {
374485535b8eSBarry Smith     ierr         = PetscMalloc(bs2*sizeof(MatScalar),&barray);CHKERRQ(ierr);
374585535b8eSBarry Smith     baij->barray = barray;
374685535b8eSBarry Smith   }
374785535b8eSBarry Smith 
374885535b8eSBarry Smith   if (roworiented) {
374985535b8eSBarry Smith     stepval = (n-1)*bs;
375085535b8eSBarry Smith   } else {
375185535b8eSBarry Smith     stepval = (m-1)*bs;
375285535b8eSBarry Smith   }
375385535b8eSBarry Smith   for (i=0; i<m; i++) {
375485535b8eSBarry Smith     if (im[i] < 0) continue;
375585535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
3756*e32f2f54SBarry 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);
375785535b8eSBarry Smith #endif
375885535b8eSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
375985535b8eSBarry Smith       row = im[i] - rstart;
376085535b8eSBarry Smith       for (j=0; j<n; j++) {
376185535b8eSBarry Smith         /* If NumCol = 1 then a copy is not required */
376285535b8eSBarry Smith         if ((roworiented) && (n == 1)) {
376385535b8eSBarry Smith           barray = (MatScalar*)v + i*bs2;
376485535b8eSBarry Smith         } else if((!roworiented) && (m == 1)) {
376585535b8eSBarry Smith           barray = (MatScalar*)v + j*bs2;
376685535b8eSBarry Smith         } else { /* Here a copy is required */
376785535b8eSBarry Smith           if (roworiented) {
376885535b8eSBarry Smith             value = v + i*(stepval+bs)*bs + j*bs;
376985535b8eSBarry Smith           } else {
377085535b8eSBarry Smith             value = v + j*(stepval+bs)*bs + i*bs;
377185535b8eSBarry Smith           }
377285535b8eSBarry Smith           for (ii=0; ii<bs; ii++,value+=stepval) {
377385535b8eSBarry Smith             for (jj=0; jj<bs; jj++) {
377485535b8eSBarry Smith               *barray++  = *value++;
377585535b8eSBarry Smith             }
377685535b8eSBarry Smith           }
377785535b8eSBarry Smith           barray -=bs2;
377885535b8eSBarry Smith         }
377985535b8eSBarry Smith 
378085535b8eSBarry Smith         if (in[j] >= cstart && in[j] < cend){
378185535b8eSBarry Smith           col  = in[j] - cstart;
378297e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
378385535b8eSBarry Smith         }
378485535b8eSBarry Smith         else if (in[j] < 0) continue;
378585535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
3786*e32f2f54SBarry Smith         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);}
378785535b8eSBarry Smith #endif
378885535b8eSBarry Smith         else {
378985535b8eSBarry Smith           if (mat->was_assembled) {
379085535b8eSBarry Smith             if (!baij->colmap) {
379185535b8eSBarry Smith               ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
379285535b8eSBarry Smith             }
379385535b8eSBarry Smith 
379485535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
379585535b8eSBarry Smith #if defined (PETSC_USE_CTABLE)
379685535b8eSBarry Smith             { PetscInt data;
379785535b8eSBarry Smith               ierr = PetscTableFind(baij->colmap,in[j]+1,&data);CHKERRQ(ierr);
3798*e32f2f54SBarry Smith               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
379985535b8eSBarry Smith             }
380085535b8eSBarry Smith #else
3801*e32f2f54SBarry Smith             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
380285535b8eSBarry Smith #endif
380385535b8eSBarry Smith #endif
380485535b8eSBarry Smith #if defined (PETSC_USE_CTABLE)
380585535b8eSBarry Smith 	    ierr = PetscTableFind(baij->colmap,in[j]+1,&col);CHKERRQ(ierr);
380685535b8eSBarry Smith             col  = (col - 1)/bs;
380785535b8eSBarry Smith #else
380885535b8eSBarry Smith             col = (baij->colmap[in[j]] - 1)/bs;
380985535b8eSBarry Smith #endif
381085535b8eSBarry Smith             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
381185535b8eSBarry Smith               ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
381285535b8eSBarry Smith               col =  in[j];
381385535b8eSBarry Smith             }
381485535b8eSBarry Smith           }
381585535b8eSBarry Smith           else col = in[j];
381697e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
381785535b8eSBarry Smith         }
381885535b8eSBarry Smith       }
381985535b8eSBarry Smith     } else {
382085535b8eSBarry Smith       if (!baij->donotstash) {
382185535b8eSBarry Smith         if (roworiented) {
382285535b8eSBarry Smith           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
382385535b8eSBarry Smith         } else {
382485535b8eSBarry Smith           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
382585535b8eSBarry Smith         }
382685535b8eSBarry Smith       }
382785535b8eSBarry Smith     }
382885535b8eSBarry Smith   }
382985535b8eSBarry Smith 
383085535b8eSBarry Smith   /* task normally handled by MatSetValuesBlocked() */
383185535b8eSBarry Smith   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
383285535b8eSBarry Smith   PetscFunctionReturn(0);
383385535b8eSBarry Smith }
3834