xref: /petsc/src/mat/impls/baij/mpi/mpibaij.c (revision 93dfae198b7b88e3d1d0b167d98d82ce5d6af8d8)
179bdfe76SSatish Balay 
2c6db04a5SJed Brown #include <../src/mat/impls/baij/mpi/mpibaij.h>   /*I  "petscmat.h"  I*/
3c6db04a5SJed Brown #include <petscblaslapack.h>
479bdfe76SSatish Balay 
509573ac7SBarry Smith extern PetscErrorCode MatSetUpMultiply_MPIBAIJ(Mat);
6ab9863d7SBarry Smith extern PetscErrorCode MatDisAssemble_MPIBAIJ(Mat);
709573ac7SBarry Smith extern PetscErrorCode MatGetValues_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt [],PetscScalar []);
809573ac7SBarry Smith extern PetscErrorCode MatSetValues_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt [],const PetscScalar [],InsertMode);
909573ac7SBarry Smith extern PetscErrorCode MatSetValuesBlocked_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
1009573ac7SBarry Smith extern PetscErrorCode MatGetRow_SeqBAIJ(Mat,PetscInt,PetscInt*,PetscInt*[],PetscScalar*[]);
1109573ac7SBarry Smith extern PetscErrorCode MatRestoreRow_SeqBAIJ(Mat,PetscInt,PetscInt*,PetscInt*[],PetscScalar*[]);
1209573ac7SBarry Smith extern PetscErrorCode MatZeroRows_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscScalar,Vec,Vec);
1393fea6afSBarry Smith 
144a2ae208SSatish Balay #undef __FUNCT__
15985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_MPIBAIJ"
16985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_MPIBAIJ(Mat A,Vec v,PetscInt idx[])
177843d17aSBarry Smith {
187843d17aSBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
19dfbe8321SBarry Smith   PetscErrorCode ierr;
20985db425SBarry Smith   PetscInt       i,*idxb = 0;
2187828ca2SBarry Smith   PetscScalar    *va,*vb;
227843d17aSBarry Smith   Vec            vtmp;
237843d17aSBarry Smith 
247843d17aSBarry Smith   PetscFunctionBegin;
25985db425SBarry Smith   ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr);
261ebc52fbSHong Zhang   ierr = VecGetArray(v,&va);CHKERRQ(ierr);
27985db425SBarry Smith   if (idx) {
2826fbe8dcSKarl Rupp     for (i=0; i<A->rmap->n; i++) {
2926fbe8dcSKarl Rupp       if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
3026fbe8dcSKarl Rupp     }
31985db425SBarry Smith   }
327843d17aSBarry Smith 
33d0f46423SBarry Smith   ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr);
34d0f46423SBarry Smith   if (idx) {ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr);}
35985db425SBarry Smith   ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr);
361ebc52fbSHong Zhang   ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr);
377843d17aSBarry Smith 
38d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
3926fbe8dcSKarl Rupp     if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
4026fbe8dcSKarl Rupp       va[i] = vb[i];
4126fbe8dcSKarl Rupp       if (idx) idx[i] = A->cmap->bs*a->garray[idxb[i]/A->cmap->bs] + (idxb[i] % A->cmap->bs);
4226fbe8dcSKarl Rupp     }
437843d17aSBarry Smith   }
447843d17aSBarry Smith 
451ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&va);CHKERRQ(ierr);
461ebc52fbSHong Zhang   ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr);
47c31cb41cSBarry Smith   ierr = PetscFree(idxb);CHKERRQ(ierr);
486bf464f9SBarry Smith   ierr = VecDestroy(&vtmp);CHKERRQ(ierr);
497843d17aSBarry Smith   PetscFunctionReturn(0);
507843d17aSBarry Smith }
517843d17aSBarry Smith 
524a2ae208SSatish Balay #undef __FUNCT__
534a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIBAIJ"
547087cfbeSBarry Smith PetscErrorCode  MatStoreValues_MPIBAIJ(Mat mat)
557fc3c18eSBarry Smith {
567fc3c18eSBarry Smith   Mat_MPIBAIJ    *aij = (Mat_MPIBAIJ*)mat->data;
57dfbe8321SBarry Smith   PetscErrorCode ierr;
587fc3c18eSBarry Smith 
597fc3c18eSBarry Smith   PetscFunctionBegin;
607fc3c18eSBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
617fc3c18eSBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
627fc3c18eSBarry Smith   PetscFunctionReturn(0);
637fc3c18eSBarry Smith }
647fc3c18eSBarry Smith 
654a2ae208SSatish Balay #undef __FUNCT__
664a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIBAIJ"
677087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_MPIBAIJ(Mat mat)
687fc3c18eSBarry Smith {
697fc3c18eSBarry Smith   Mat_MPIBAIJ    *aij = (Mat_MPIBAIJ*)mat->data;
70dfbe8321SBarry Smith   PetscErrorCode ierr;
717fc3c18eSBarry Smith 
727fc3c18eSBarry Smith   PetscFunctionBegin;
737fc3c18eSBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
747fc3c18eSBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
757fc3c18eSBarry Smith   PetscFunctionReturn(0);
767fc3c18eSBarry Smith }
777fc3c18eSBarry Smith 
78537820f0SBarry Smith /*
79537820f0SBarry Smith      Local utility routine that creates a mapping from the global column
8057b952d6SSatish Balay    number to the local number in the off-diagonal part of the local
81e06f6af7SJed Brown    storage of the matrix.  This is done in a non scalable way since the
8257b952d6SSatish Balay    length of colmap equals the global matrix length.
8357b952d6SSatish Balay */
844a2ae208SSatish Balay #undef __FUNCT__
85ab9863d7SBarry Smith #define __FUNCT__ "MatCreateColmap_MPIBAIJ_Private"
86ab9863d7SBarry Smith PetscErrorCode MatCreateColmap_MPIBAIJ_Private(Mat mat)
8757b952d6SSatish Balay {
8857b952d6SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
8957b952d6SSatish Balay   Mat_SeqBAIJ    *B    = (Mat_SeqBAIJ*)baij->B->data;
906849ba73SBarry Smith   PetscErrorCode ierr;
91d0f46423SBarry Smith   PetscInt       nbs = B->nbs,i,bs=mat->rmap->bs;
9257b952d6SSatish Balay 
93d64ed03dSBarry Smith   PetscFunctionBegin;
94aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
95e23dfa41SBarry Smith   ierr = PetscTableCreate(baij->nbs,baij->Nbs+1,&baij->colmap);CHKERRQ(ierr);
9648e59246SSatish Balay   for (i=0; i<nbs; i++) {
973861aac3SJed Brown     ierr = PetscTableAdd(baij->colmap,baij->garray[i]+1,i*bs+1,INSERT_VALUES);CHKERRQ(ierr);
9848e59246SSatish Balay   }
9948e59246SSatish Balay #else
100b24ad042SBarry Smith   ierr = PetscMalloc((baij->Nbs+1)*sizeof(PetscInt),&baij->colmap);CHKERRQ(ierr);
1013bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)mat,baij->Nbs*sizeof(PetscInt));CHKERRQ(ierr);
102b24ad042SBarry Smith   ierr = PetscMemzero(baij->colmap,baij->Nbs*sizeof(PetscInt));CHKERRQ(ierr);
103928fc39bSSatish Balay   for (i=0; i<nbs; i++) baij->colmap[baij->garray[i]] = i*bs+1;
10448e59246SSatish Balay #endif
1053a40ed3dSBarry Smith   PetscFunctionReturn(0);
10657b952d6SSatish Balay }
10757b952d6SSatish Balay 
108f5e9677aSSatish Balay #define  MatSetValues_SeqBAIJ_A_Private(row,col,value,addv) \
10980c1aa95SSatish Balay   { \
11080c1aa95SSatish Balay  \
11180c1aa95SSatish Balay     brow = row/bs;  \
11280c1aa95SSatish Balay     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow]; \
113ac7a638eSSatish Balay     rmax = aimax[brow]; nrow = ailen[brow]; \
11480c1aa95SSatish Balay     bcol = col/bs; \
11580c1aa95SSatish Balay     ridx = row % bs; cidx = col % bs; \
116ab26458aSBarry Smith     low  = 0; high = nrow; \
117ab26458aSBarry Smith     while (high-low > 3) { \
118ab26458aSBarry Smith       t = (low+high)/2; \
119ab26458aSBarry Smith       if (rp[t] > bcol) high = t; \
120ab26458aSBarry Smith       else              low  = t; \
121ab26458aSBarry Smith     } \
122ab26458aSBarry Smith     for (_i=low; _i<high; _i++) { \
12380c1aa95SSatish Balay       if (rp[_i] > bcol) break; \
12480c1aa95SSatish Balay       if (rp[_i] == bcol) { \
12580c1aa95SSatish Balay         bap = ap +  bs2*_i + bs*cidx + ridx; \
126eada6651SSatish Balay         if (addv == ADD_VALUES) *bap += value;  \
127eada6651SSatish Balay         else                    *bap  = value;  \
128ac7a638eSSatish Balay         goto a_noinsert; \
12980c1aa95SSatish Balay       } \
13080c1aa95SSatish Balay     } \
13189280ab3SLois Curfman McInnes     if (a->nonew == 1) goto a_noinsert; \
132e32f2f54SBarry Smith     if (a->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
133fef13f97SBarry Smith     MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,aimax,a->nonew,MatScalar); \
13480c1aa95SSatish Balay     N = nrow++ - 1;  \
13580c1aa95SSatish Balay     /* shift up all the later entries in this row */ \
13680c1aa95SSatish Balay     for (ii=N; ii>=_i; ii--) { \
13780c1aa95SSatish Balay       rp[ii+1] = rp[ii]; \
1383eda8832SBarry Smith       ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr); \
13980c1aa95SSatish Balay     } \
1403eda8832SBarry Smith     if (N>=_i) { ierr = PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));CHKERRQ(ierr); }  \
14180c1aa95SSatish Balay     rp[_i]                      = bcol;  \
14280c1aa95SSatish Balay     ap[bs2*_i + bs*cidx + ridx] = value;  \
143ac7a638eSSatish Balay a_noinsert:; \
14480c1aa95SSatish Balay     ailen[brow] = nrow; \
14580c1aa95SSatish Balay   }
14657b952d6SSatish Balay 
147ac7a638eSSatish Balay #define  MatSetValues_SeqBAIJ_B_Private(row,col,value,addv) \
148ac7a638eSSatish Balay   { \
149ac7a638eSSatish Balay     brow = row/bs;  \
150ac7a638eSSatish Balay     rp   = bj + bi[brow]; ap = ba + bs2*bi[brow]; \
151ac7a638eSSatish Balay     rmax = bimax[brow]; nrow = bilen[brow]; \
152ac7a638eSSatish Balay     bcol = col/bs; \
153ac7a638eSSatish Balay     ridx = row % bs; cidx = col % bs; \
154ac7a638eSSatish Balay     low  = 0; high = nrow; \
155ac7a638eSSatish Balay     while (high-low > 3) { \
156ac7a638eSSatish Balay       t = (low+high)/2; \
157ac7a638eSSatish Balay       if (rp[t] > bcol) high = t; \
158ac7a638eSSatish Balay       else              low  = t; \
159ac7a638eSSatish Balay     } \
160ac7a638eSSatish Balay     for (_i=low; _i<high; _i++) { \
161ac7a638eSSatish Balay       if (rp[_i] > bcol) break; \
162ac7a638eSSatish Balay       if (rp[_i] == bcol) { \
163ac7a638eSSatish Balay         bap = ap +  bs2*_i + bs*cidx + ridx; \
164ac7a638eSSatish Balay         if (addv == ADD_VALUES) *bap += value;  \
165ac7a638eSSatish Balay         else                    *bap  = value;  \
166ac7a638eSSatish Balay         goto b_noinsert; \
167ac7a638eSSatish Balay       } \
168ac7a638eSSatish Balay     } \
16989280ab3SLois Curfman McInnes     if (b->nonew == 1) goto b_noinsert; \
170e32f2f54SBarry Smith     if (b->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
171fef13f97SBarry Smith     MatSeqXAIJReallocateAIJ(B,b->mbs,bs2,nrow,brow,bcol,rmax,ba,bi,bj,rp,ap,bimax,b->nonew,MatScalar); \
172ac7a638eSSatish Balay     N = nrow++ - 1;  \
173ac7a638eSSatish Balay     /* shift up all the later entries in this row */ \
174ac7a638eSSatish Balay     for (ii=N; ii>=_i; ii--) { \
175ac7a638eSSatish Balay       rp[ii+1] = rp[ii]; \
1763eda8832SBarry Smith       ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr); \
177ac7a638eSSatish Balay     } \
1783eda8832SBarry Smith     if (N>=_i) { ierr = PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));CHKERRQ(ierr);}  \
179ac7a638eSSatish Balay     rp[_i]                      = bcol;  \
180ac7a638eSSatish Balay     ap[bs2*_i + bs*cidx + ridx] = value;  \
181ac7a638eSSatish Balay b_noinsert:; \
182ac7a638eSSatish Balay     bilen[brow] = nrow; \
183ac7a638eSSatish Balay   }
184ac7a638eSSatish Balay 
1854a2ae208SSatish Balay #undef __FUNCT__
1864a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIBAIJ"
187b24ad042SBarry Smith PetscErrorCode MatSetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
18857b952d6SSatish Balay {
18957b952d6SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
19093fea6afSBarry Smith   MatScalar      value;
191ace3abfcSBarry Smith   PetscBool      roworiented = baij->roworiented;
192dfbe8321SBarry Smith   PetscErrorCode ierr;
193b24ad042SBarry Smith   PetscInt       i,j,row,col;
194d0f46423SBarry Smith   PetscInt       rstart_orig=mat->rmap->rstart;
195d0f46423SBarry Smith   PetscInt       rend_orig  =mat->rmap->rend,cstart_orig=mat->cmap->rstart;
196d0f46423SBarry Smith   PetscInt       cend_orig  =mat->cmap->rend,bs=mat->rmap->bs;
19757b952d6SSatish Balay 
198eada6651SSatish Balay   /* Some Variables required in the macro */
19980c1aa95SSatish Balay   Mat         A     = baij->A;
20080c1aa95SSatish Balay   Mat_SeqBAIJ *a    = (Mat_SeqBAIJ*)(A)->data;
201b24ad042SBarry Smith   PetscInt    *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j;
2023eda8832SBarry Smith   MatScalar   *aa   =a->a;
203ac7a638eSSatish Balay 
204ac7a638eSSatish Balay   Mat         B     = baij->B;
205ac7a638eSSatish Balay   Mat_SeqBAIJ *b    = (Mat_SeqBAIJ*)(B)->data;
206b24ad042SBarry Smith   PetscInt    *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j;
2073eda8832SBarry Smith   MatScalar   *ba   =b->a;
208ac7a638eSSatish Balay 
209b24ad042SBarry Smith   PetscInt  *rp,ii,nrow,_i,rmax,N,brow,bcol;
210b24ad042SBarry Smith   PetscInt  low,high,t,ridx,cidx,bs2=a->bs2;
2113eda8832SBarry Smith   MatScalar *ap,*bap;
21280c1aa95SSatish Balay 
213d64ed03dSBarry Smith   PetscFunctionBegin;
21471fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
21557b952d6SSatish Balay   for (i=0; i<m; i++) {
2165ef9f2a5SBarry Smith     if (im[i] < 0) continue;
2172515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
218e32f2f54SBarry 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);
219639f9d9dSBarry Smith #endif
22057b952d6SSatish Balay     if (im[i] >= rstart_orig && im[i] < rend_orig) {
22157b952d6SSatish Balay       row = im[i] - rstart_orig;
22257b952d6SSatish Balay       for (j=0; j<n; j++) {
22357b952d6SSatish Balay         if (in[j] >= cstart_orig && in[j] < cend_orig) {
22457b952d6SSatish Balay           col = in[j] - cstart_orig;
225db4deed7SKarl Rupp           if (roworiented) value = v[i*n+j];
226db4deed7SKarl Rupp           else             value = v[i+j*m];
227f5e9677aSSatish Balay           MatSetValues_SeqBAIJ_A_Private(row,col,value,addv);
22880c1aa95SSatish Balay           /* ierr = MatSetValues_SeqBAIJ(baij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
22973959e64SBarry Smith         } else if (in[j] < 0) continue;
2302515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
231660746e0SBarry Smith         else if (in[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap->N-1);
232639f9d9dSBarry Smith #endif
23357b952d6SSatish Balay         else {
23457b952d6SSatish Balay           if (mat->was_assembled) {
235905e6a2fSBarry Smith             if (!baij->colmap) {
236ab9863d7SBarry Smith               ierr = MatCreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
237905e6a2fSBarry Smith             }
238aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
2390f5bd95cSBarry Smith             ierr = PetscTableFind(baij->colmap,in[j]/bs + 1,&col);CHKERRQ(ierr);
240bba1ac68SSatish Balay             col  = col - 1;
24148e59246SSatish Balay #else
242bba1ac68SSatish Balay             col = baij->colmap[in[j]/bs] - 1;
24348e59246SSatish Balay #endif
244c9ef50b2SBarry Smith             if (col < 0 && !((Mat_SeqBAIJ*)(baij->B->data))->nonew) {
245ab9863d7SBarry Smith               ierr = MatDisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
2468295de27SSatish Balay               col  =  in[j];
2479bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
2489bf004c3SSatish Balay               B    = baij->B;
2499bf004c3SSatish Balay               b    = (Mat_SeqBAIJ*)(B)->data;
2509bf004c3SSatish Balay               bimax=b->imax;bi=b->i;bilen=b->ilen;bj=b->j;
2519bf004c3SSatish Balay               ba   =b->a;
252c9ef50b2SBarry Smith             } else if (col < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", im[i], in[j]);
253c9ef50b2SBarry Smith             else col += in[j]%bs;
2548295de27SSatish Balay           } else col = in[j];
255db4deed7SKarl Rupp           if (roworiented) value = v[i*n+j];
256db4deed7SKarl Rupp           else             value = v[i+j*m];
25790da58bdSSatish Balay           MatSetValues_SeqBAIJ_B_Private(row,col,value,addv);
25890da58bdSSatish Balay           /* ierr = MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
25957b952d6SSatish Balay         }
26057b952d6SSatish Balay       }
261d64ed03dSBarry Smith     } else {
2624cb17eb5SBarry Smith       if (mat->nooffprocentries) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Setting off process row %D even though MatSetOption(,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) was set",im[i]);
26390f02eecSBarry Smith       if (!baij->donotstash) {
2645080c13bSMatthew G Knepley         mat->assembled = PETSC_FALSE;
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;
283ace3abfcSBarry Smith   PetscBool         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 
29526fbe8dcSKarl Rupp   if (roworiented) stepval = (n-1)*bs;
29626fbe8dcSKarl Rupp   else stepval = (m-1)*bs;
29726fbe8dcSKarl Rupp 
298ab26458aSBarry Smith   for (i=0; i<m; i++) {
2995ef9f2a5SBarry Smith     if (im[i] < 0) continue;
3002515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
301e32f2f54SBarry Smith     if (im[i] >= baij->Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large, row %D max %D",im[i],baij->Mbs-1);
302ab26458aSBarry Smith #endif
303ab26458aSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
304ab26458aSBarry Smith       row = im[i] - rstart;
305ab26458aSBarry Smith       for (j=0; j<n; j++) {
30615b57d14SSatish Balay         /* If NumCol = 1 then a copy is not required */
30715b57d14SSatish Balay         if ((roworiented) && (n == 1)) {
308f15d580aSBarry Smith           barray = (MatScalar*)v + i*bs2;
30915b57d14SSatish Balay         } else if ((!roworiented) && (m == 1)) {
310f15d580aSBarry Smith           barray = (MatScalar*)v + j*bs2;
31115b57d14SSatish Balay         } else { /* Here a copy is required */
312ab26458aSBarry Smith           if (roworiented) {
31353ef36baSBarry Smith             value = v + (i*(stepval+bs) + j)*bs;
314ab26458aSBarry Smith           } else {
31553ef36baSBarry Smith             value = v + (j*(stepval+bs) + i)*bs;
316abef11f7SSatish Balay           }
31753ef36baSBarry Smith           for (ii=0; ii<bs; ii++,value+=bs+stepval) {
31826fbe8dcSKarl Rupp             for (jj=0; jj<bs; jj++) barray[jj] = value[jj];
31953ef36baSBarry Smith             barray += bs;
32047513183SBarry Smith           }
32130793edcSSatish Balay           barray -= bs2;
32215b57d14SSatish Balay         }
323abef11f7SSatish Balay 
324abef11f7SSatish Balay         if (in[j] >= cstart && in[j] < cend) {
325abef11f7SSatish Balay           col  = in[j] - cstart;
32697e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
32726fbe8dcSKarl Rupp         } else if (in[j] < 0) continue;
3282515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
329cb9801acSJed Brown         else if (in[j] >= baij->Nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large, col %D max %D",in[j],baij->Nbs-1);
330ab26458aSBarry Smith #endif
331ab26458aSBarry Smith         else {
332ab26458aSBarry Smith           if (mat->was_assembled) {
333ab26458aSBarry Smith             if (!baij->colmap) {
334ab9863d7SBarry Smith               ierr = MatCreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
335ab26458aSBarry Smith             }
336a5eb4965SSatish Balay 
3372515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
338aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
339b24ad042SBarry Smith             { PetscInt data;
3400f5bd95cSBarry Smith               ierr = PetscTableFind(baij->colmap,in[j]+1,&data);CHKERRQ(ierr);
341e32f2f54SBarry Smith               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
342fa46199cSSatish Balay             }
34348e59246SSatish Balay #else
344e32f2f54SBarry Smith             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
345a5eb4965SSatish Balay #endif
34648e59246SSatish Balay #endif
347aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
3480f5bd95cSBarry Smith             ierr = PetscTableFind(baij->colmap,in[j]+1,&col);CHKERRQ(ierr);
349fa46199cSSatish Balay             col  = (col - 1)/bs;
35048e59246SSatish Balay #else
351a5eb4965SSatish Balay             col = (baij->colmap[in[j]] - 1)/bs;
35248e59246SSatish Balay #endif
3530e9bae81SBarry Smith             if (col < 0 && !((Mat_SeqBAIJ*)(baij->B->data))->nonew) {
354ab9863d7SBarry Smith               ierr = MatDisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
355ab26458aSBarry Smith               col  =  in[j];
3560e9bae81SBarry Smith             } else if (col < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", bs*im[i], bs*in[j]);
357db4deed7SKarl Rupp           } else col = in[j];
35897e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
359ab26458aSBarry Smith         }
360ab26458aSBarry Smith       }
361d64ed03dSBarry Smith     } else {
3624cb17eb5SBarry Smith       if (mat->nooffprocentries) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Setting off process row %D even though MatSetOption(,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) was set",im[i]);
363ab26458aSBarry Smith       if (!baij->donotstash) {
364ff2fd236SBarry Smith         if (roworiented) {
3656fa18ffdSBarry Smith           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
366ff2fd236SBarry Smith         } else {
3676fa18ffdSBarry Smith           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
368ff2fd236SBarry Smith         }
369abef11f7SSatish Balay       }
370ab26458aSBarry Smith     }
371ab26458aSBarry Smith   }
3723a40ed3dSBarry Smith   PetscFunctionReturn(0);
373ab26458aSBarry Smith }
3746fa18ffdSBarry Smith 
3750bdbc534SSatish Balay #define HASH_KEY 0.6180339887
376b24ad042SBarry Smith #define HASH(size,key,tmp) (tmp = (key)*HASH_KEY,(PetscInt)((size)*(tmp-(PetscInt)tmp)))
377b24ad042SBarry Smith /* #define HASH(size,key) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
378b24ad042SBarry Smith /* #define HASH(size,key,tmp) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
3794a2ae208SSatish Balay #undef __FUNCT__
38097e5c40aSBarry Smith #define __FUNCT__ "MatSetValues_MPIBAIJ_HT"
38197e5c40aSBarry Smith PetscErrorCode MatSetValues_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
3820bdbc534SSatish Balay {
3830bdbc534SSatish Balay   Mat_MPIBAIJ    *baij       = (Mat_MPIBAIJ*)mat->data;
384ace3abfcSBarry Smith   PetscBool      roworiented = baij->roworiented;
385dfbe8321SBarry Smith   PetscErrorCode ierr;
386b24ad042SBarry Smith   PetscInt       i,j,row,col;
387d0f46423SBarry Smith   PetscInt       rstart_orig=mat->rmap->rstart;
388d0f46423SBarry Smith   PetscInt       rend_orig  =mat->rmap->rend,Nbs=baij->Nbs;
389d0f46423SBarry Smith   PetscInt       h1,key,size=baij->ht_size,bs=mat->rmap->bs,*HT=baij->ht,idx;
390329f5518SBarry Smith   PetscReal      tmp;
3913eda8832SBarry Smith   MatScalar      **HD = baij->hd,value;
3922515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
393b24ad042SBarry Smith   PetscInt total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
3944a15367fSSatish Balay #endif
3950bdbc534SSatish Balay 
3960bdbc534SSatish Balay   PetscFunctionBegin;
39771fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
3980bdbc534SSatish Balay   for (i=0; i<m; i++) {
3992515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
400e32f2f54SBarry Smith     if (im[i] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
401e32f2f54SBarry 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);
4020bdbc534SSatish Balay #endif
4030bdbc534SSatish Balay     row = im[i];
404c2760754SSatish Balay     if (row >= rstart_orig && row < rend_orig) {
4050bdbc534SSatish Balay       for (j=0; j<n; j++) {
4060bdbc534SSatish Balay         col = in[j];
407db4deed7SKarl Rupp         if (roworiented) value = v[i*n+j];
408db4deed7SKarl Rupp         else             value = v[i+j*m];
409b24ad042SBarry Smith         /* Look up PetscInto the Hash Table */
410c2760754SSatish Balay         key = (row/bs)*Nbs+(col/bs)+1;
411c2760754SSatish Balay         h1  = HASH(size,key,tmp);
4120bdbc534SSatish Balay 
413c2760754SSatish Balay 
414c2760754SSatish Balay         idx = h1;
4152515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
416187ce0cbSSatish Balay         insert_ct++;
417187ce0cbSSatish Balay         total_ct++;
418187ce0cbSSatish Balay         if (HT[idx] != key) {
419187ce0cbSSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++) ;
420187ce0cbSSatish Balay           if (idx == size) {
421187ce0cbSSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++) ;
422f23aa3ddSBarry Smith             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
423187ce0cbSSatish Balay           }
424187ce0cbSSatish Balay         }
425187ce0cbSSatish Balay #else
426c2760754SSatish Balay         if (HT[idx] != key) {
427c2760754SSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++) ;
428c2760754SSatish Balay           if (idx == size) {
429c2760754SSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++) ;
430f23aa3ddSBarry Smith             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
431c2760754SSatish Balay           }
432c2760754SSatish Balay         }
433187ce0cbSSatish Balay #endif
434c2760754SSatish Balay         /* A HASH table entry is found, so insert the values at the correct address */
435c2760754SSatish Balay         if (addv == ADD_VALUES) *(HD[idx]+ (col % bs)*bs + (row % bs)) += value;
436c2760754SSatish Balay         else                    *(HD[idx]+ (col % bs)*bs + (row % bs))  = value;
4370bdbc534SSatish Balay       }
43826fbe8dcSKarl Rupp     } else if (!baij->donotstash) {
439ff2fd236SBarry Smith       if (roworiented) {
440b400d20cSBarry Smith         ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);CHKERRQ(ierr);
441ff2fd236SBarry Smith       } else {
442b400d20cSBarry Smith         ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);CHKERRQ(ierr);
4430bdbc534SSatish Balay       }
4440bdbc534SSatish Balay     }
4450bdbc534SSatish Balay   }
4462515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
447187ce0cbSSatish Balay   baij->ht_total_ct  = total_ct;
448187ce0cbSSatish Balay   baij->ht_insert_ct = insert_ct;
449187ce0cbSSatish Balay #endif
4500bdbc534SSatish Balay   PetscFunctionReturn(0);
4510bdbc534SSatish Balay }
4520bdbc534SSatish Balay 
4534a2ae208SSatish Balay #undef __FUNCT__
45497e5c40aSBarry Smith #define __FUNCT__ "MatSetValuesBlocked_MPIBAIJ_HT"
45597e5c40aSBarry Smith PetscErrorCode MatSetValuesBlocked_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
4560bdbc534SSatish Balay {
4570bdbc534SSatish Balay   Mat_MPIBAIJ       *baij       = (Mat_MPIBAIJ*)mat->data;
458ace3abfcSBarry Smith   PetscBool         roworiented = baij->roworiented;
459dfbe8321SBarry Smith   PetscErrorCode    ierr;
460b24ad042SBarry Smith   PetscInt          i,j,ii,jj,row,col;
461899cda47SBarry Smith   PetscInt          rstart=baij->rstartbs;
462d0f46423SBarry Smith   PetscInt          rend  =mat->rmap->rend,stepval,bs=mat->rmap->bs,bs2=baij->bs2,nbs2=n*bs2;
463b24ad042SBarry Smith   PetscInt          h1,key,size=baij->ht_size,idx,*HT=baij->ht,Nbs=baij->Nbs;
464329f5518SBarry Smith   PetscReal         tmp;
4653eda8832SBarry Smith   MatScalar         **HD = baij->hd,*baij_a;
466dd6ea824SBarry Smith   const PetscScalar *v_t,*value;
4672515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
468b24ad042SBarry Smith   PetscInt total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
4694a15367fSSatish Balay #endif
4700bdbc534SSatish Balay 
471d0a41580SSatish Balay   PetscFunctionBegin;
47226fbe8dcSKarl Rupp   if (roworiented) stepval = (n-1)*bs;
47326fbe8dcSKarl Rupp   else stepval = (m-1)*bs;
47426fbe8dcSKarl Rupp 
4750bdbc534SSatish Balay   for (i=0; i<m; i++) {
4762515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
477e32f2f54SBarry Smith     if (im[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",im[i]);
478e32f2f54SBarry 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);
4790bdbc534SSatish Balay #endif
4800bdbc534SSatish Balay     row = im[i];
481ab715e2cSSatish Balay     v_t = v + i*nbs2;
482c2760754SSatish Balay     if (row >= rstart && row < rend) {
4830bdbc534SSatish Balay       for (j=0; j<n; j++) {
4840bdbc534SSatish Balay         col = in[j];
4850bdbc534SSatish Balay 
4860bdbc534SSatish Balay         /* Look up into the Hash Table */
487c2760754SSatish Balay         key = row*Nbs+col+1;
488c2760754SSatish Balay         h1  = HASH(size,key,tmp);
4890bdbc534SSatish Balay 
490c2760754SSatish Balay         idx = h1;
4912515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
492187ce0cbSSatish Balay         total_ct++;
493187ce0cbSSatish Balay         insert_ct++;
494187ce0cbSSatish Balay         if (HT[idx] != key) {
495187ce0cbSSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++) ;
496187ce0cbSSatish Balay           if (idx == size) {
497187ce0cbSSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++) ;
498f23aa3ddSBarry Smith             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
499187ce0cbSSatish Balay           }
500187ce0cbSSatish Balay         }
501187ce0cbSSatish Balay #else
502c2760754SSatish Balay         if (HT[idx] != key) {
503c2760754SSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++) ;
504c2760754SSatish Balay           if (idx == size) {
505c2760754SSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++) ;
506f23aa3ddSBarry Smith             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
507c2760754SSatish Balay           }
508c2760754SSatish Balay         }
509187ce0cbSSatish Balay #endif
510c2760754SSatish Balay         baij_a = HD[idx];
5110bdbc534SSatish Balay         if (roworiented) {
512c2760754SSatish Balay           /*value = v + i*(stepval+bs)*bs + j*bs;*/
513187ce0cbSSatish Balay           /* value = v + (i*(stepval+bs)+j)*bs; */
514187ce0cbSSatish Balay           value = v_t;
515187ce0cbSSatish Balay           v_t  += bs;
516fef45726SSatish Balay           if (addv == ADD_VALUES) {
517c2760754SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval) {
518c2760754SSatish Balay               for (jj=ii; jj<bs2; jj+=bs) {
519fef45726SSatish Balay                 baij_a[jj] += *value++;
520b4cc0f5aSSatish Balay               }
521b4cc0f5aSSatish Balay             }
522fef45726SSatish Balay           } else {
523c2760754SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval) {
524c2760754SSatish Balay               for (jj=ii; jj<bs2; jj+=bs) {
525fef45726SSatish Balay                 baij_a[jj] = *value++;
526fef45726SSatish Balay               }
527fef45726SSatish Balay             }
528fef45726SSatish Balay           }
5290bdbc534SSatish Balay         } else {
5300bdbc534SSatish Balay           value = v + j*(stepval+bs)*bs + i*bs;
531fef45726SSatish Balay           if (addv == ADD_VALUES) {
532b4cc0f5aSSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
5330bdbc534SSatish Balay               for (jj=0; jj<bs; jj++) {
534fef45726SSatish Balay                 baij_a[jj] += *value++;
535fef45726SSatish Balay               }
536fef45726SSatish Balay             }
537fef45726SSatish Balay           } else {
538fef45726SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
539fef45726SSatish Balay               for (jj=0; jj<bs; jj++) {
540fef45726SSatish Balay                 baij_a[jj] = *value++;
541fef45726SSatish Balay               }
542b4cc0f5aSSatish Balay             }
5430bdbc534SSatish Balay           }
5440bdbc534SSatish Balay         }
5450bdbc534SSatish Balay       }
5460bdbc534SSatish Balay     } else {
5470bdbc534SSatish Balay       if (!baij->donotstash) {
5480bdbc534SSatish Balay         if (roworiented) {
5498798bf22SSatish Balay           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
5500bdbc534SSatish Balay         } else {
5518798bf22SSatish Balay           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
5520bdbc534SSatish Balay         }
5530bdbc534SSatish Balay       }
5540bdbc534SSatish Balay     }
5550bdbc534SSatish Balay   }
5562515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
557187ce0cbSSatish Balay   baij->ht_total_ct  = total_ct;
558187ce0cbSSatish Balay   baij->ht_insert_ct = insert_ct;
559187ce0cbSSatish Balay #endif
5600bdbc534SSatish Balay   PetscFunctionReturn(0);
5610bdbc534SSatish Balay }
562133cdb44SSatish Balay 
5634a2ae208SSatish Balay #undef __FUNCT__
5644a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIBAIJ"
565b24ad042SBarry Smith PetscErrorCode MatGetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
566d6de1c52SSatish Balay {
567d6de1c52SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
5686849ba73SBarry Smith   PetscErrorCode ierr;
569d0f46423SBarry Smith   PetscInt       bs       = mat->rmap->bs,i,j,bsrstart = mat->rmap->rstart,bsrend = mat->rmap->rend;
570d0f46423SBarry Smith   PetscInt       bscstart = mat->cmap->rstart,bscend = mat->cmap->rend,row,col,data;
571d6de1c52SSatish Balay 
572133cdb44SSatish Balay   PetscFunctionBegin;
573d6de1c52SSatish Balay   for (i=0; i<m; i++) {
574e32f2f54SBarry Smith     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
575e32f2f54SBarry 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);
576d6de1c52SSatish Balay     if (idxm[i] >= bsrstart && idxm[i] < bsrend) {
577d6de1c52SSatish Balay       row = idxm[i] - bsrstart;
578d6de1c52SSatish Balay       for (j=0; j<n; j++) {
579e32f2f54SBarry Smith         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
580e32f2f54SBarry 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);
581d6de1c52SSatish Balay         if (idxn[j] >= bscstart && idxn[j] < bscend) {
582d6de1c52SSatish Balay           col  = idxn[j] - bscstart;
58398dd23e9SBarry Smith           ierr = MatGetValues_SeqBAIJ(baij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
584d64ed03dSBarry Smith         } else {
585905e6a2fSBarry Smith           if (!baij->colmap) {
586ab9863d7SBarry Smith             ierr = MatCreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
587905e6a2fSBarry Smith           }
588aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
5890f5bd95cSBarry Smith           ierr = PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);CHKERRQ(ierr);
590fa46199cSSatish Balay           data--;
59148e59246SSatish Balay #else
59248e59246SSatish Balay           data = baij->colmap[idxn[j]/bs]-1;
59348e59246SSatish Balay #endif
59448e59246SSatish Balay           if ((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0;
595d9d09a02SSatish Balay           else {
59648e59246SSatish Balay             col  = data + idxn[j]%bs;
59798dd23e9SBarry Smith             ierr = MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
598d6de1c52SSatish Balay           }
599d6de1c52SSatish Balay         }
600d6de1c52SSatish Balay       }
601f23aa3ddSBarry Smith     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
602d6de1c52SSatish Balay   }
6033a40ed3dSBarry Smith   PetscFunctionReturn(0);
604d6de1c52SSatish Balay }
605d6de1c52SSatish Balay 
6064a2ae208SSatish Balay #undef __FUNCT__
6074a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIBAIJ"
608dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIBAIJ(Mat mat,NormType type,PetscReal *nrm)
609d6de1c52SSatish Balay {
610d6de1c52SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
611d6de1c52SSatish Balay   Mat_SeqBAIJ    *amat = (Mat_SeqBAIJ*)baij->A->data,*bmat = (Mat_SeqBAIJ*)baij->B->data;
612dfbe8321SBarry Smith   PetscErrorCode ierr;
613d0f46423SBarry Smith   PetscInt       i,j,bs2=baij->bs2,bs=baij->A->rmap->bs,nz,row,col;
614329f5518SBarry Smith   PetscReal      sum = 0.0;
6153eda8832SBarry Smith   MatScalar      *v;
616d6de1c52SSatish Balay 
617d64ed03dSBarry Smith   PetscFunctionBegin;
618d6de1c52SSatish Balay   if (baij->size == 1) {
619064f8208SBarry Smith     ierr =  MatNorm(baij->A,type,nrm);CHKERRQ(ierr);
620d6de1c52SSatish Balay   } else {
621d6de1c52SSatish Balay     if (type == NORM_FROBENIUS) {
622d6de1c52SSatish Balay       v  = amat->a;
6238a62d963SHong Zhang       nz = amat->nz*bs2;
6248a62d963SHong Zhang       for (i=0; i<nz; i++) {
625329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
626d6de1c52SSatish Balay       }
627d6de1c52SSatish Balay       v  = bmat->a;
6288a62d963SHong Zhang       nz = bmat->nz*bs2;
6298a62d963SHong Zhang       for (i=0; i<nz; i++) {
630329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
631d6de1c52SSatish Balay       }
632ce94432eSBarry Smith       ierr = MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
6338f1a2a5eSBarry Smith       *nrm = PetscSqrtReal(*nrm);
6348a62d963SHong Zhang     } else if (type == NORM_1) { /* max column sum */
6358a62d963SHong Zhang       PetscReal *tmp,*tmp2;
636899cda47SBarry Smith       PetscInt  *jj,*garray=baij->garray,cstart=baij->rstartbs;
637fca92195SBarry Smith       ierr = PetscMalloc2(mat->cmap->N,PetscReal,&tmp,mat->cmap->N,PetscReal,&tmp2);CHKERRQ(ierr);
638d0f46423SBarry Smith       ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr);
6398a62d963SHong Zhang       v    = amat->a; jj = amat->j;
6408a62d963SHong Zhang       for (i=0; i<amat->nz; i++) {
6418a62d963SHong Zhang         for (j=0; j<bs; j++) {
6428a62d963SHong Zhang           col = bs*(cstart + *jj) + j; /* column index */
6438a62d963SHong Zhang           for (row=0; row<bs; row++) {
6448a62d963SHong Zhang             tmp[col] += PetscAbsScalar(*v);  v++;
6458a62d963SHong Zhang           }
6468a62d963SHong Zhang         }
6478a62d963SHong Zhang         jj++;
6488a62d963SHong Zhang       }
6498a62d963SHong Zhang       v = bmat->a; jj = bmat->j;
6508a62d963SHong Zhang       for (i=0; i<bmat->nz; i++) {
6518a62d963SHong Zhang         for (j=0; j<bs; j++) {
6528a62d963SHong Zhang           col = bs*garray[*jj] + j;
6538a62d963SHong Zhang           for (row=0; row<bs; row++) {
6548a62d963SHong Zhang             tmp[col] += PetscAbsScalar(*v); v++;
6558a62d963SHong Zhang           }
6568a62d963SHong Zhang         }
6578a62d963SHong Zhang         jj++;
6588a62d963SHong Zhang       }
659ce94432eSBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
6608a62d963SHong Zhang       *nrm = 0.0;
661d0f46423SBarry Smith       for (j=0; j<mat->cmap->N; j++) {
6628a62d963SHong Zhang         if (tmp2[j] > *nrm) *nrm = tmp2[j];
6638a62d963SHong Zhang       }
664fca92195SBarry Smith       ierr = PetscFree2(tmp,tmp2);CHKERRQ(ierr);
6658a62d963SHong Zhang     } else if (type == NORM_INFINITY) { /* max row sum */
666577dd1f9SKris Buschelman       PetscReal *sums;
667cb9801acSJed Brown       ierr = PetscMalloc(bs*sizeof(PetscReal),&sums);CHKERRQ(ierr);
6688a62d963SHong Zhang       sum  = 0.0;
6698a62d963SHong Zhang       for (j=0; j<amat->mbs; j++) {
6708a62d963SHong Zhang         for (row=0; row<bs; row++) sums[row] = 0.0;
6718a62d963SHong Zhang         v  = amat->a + bs2*amat->i[j];
6728a62d963SHong Zhang         nz = amat->i[j+1]-amat->i[j];
6738a62d963SHong Zhang         for (i=0; i<nz; i++) {
6748a62d963SHong Zhang           for (col=0; col<bs; col++) {
6758a62d963SHong Zhang             for (row=0; row<bs; row++) {
6768a62d963SHong Zhang               sums[row] += PetscAbsScalar(*v); v++;
6778a62d963SHong Zhang             }
6788a62d963SHong Zhang           }
6798a62d963SHong Zhang         }
6808a62d963SHong Zhang         v  = bmat->a + bs2*bmat->i[j];
6818a62d963SHong Zhang         nz = bmat->i[j+1]-bmat->i[j];
6828a62d963SHong Zhang         for (i=0; i<nz; i++) {
6838a62d963SHong Zhang           for (col=0; col<bs; col++) {
6848a62d963SHong Zhang             for (row=0; row<bs; row++) {
6858a62d963SHong Zhang               sums[row] += PetscAbsScalar(*v); v++;
6868a62d963SHong Zhang             }
6878a62d963SHong Zhang           }
6888a62d963SHong Zhang         }
6898a62d963SHong Zhang         for (row=0; row<bs; row++) {
6908a62d963SHong Zhang           if (sums[row] > sum) sum = sums[row];
6918a62d963SHong Zhang         }
6928a62d963SHong Zhang       }
693ce94432eSBarry Smith       ierr = MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
694577dd1f9SKris Buschelman       ierr = PetscFree(sums);CHKERRQ(ierr);
695ce94432eSBarry Smith     } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for this norm yet");
696d64ed03dSBarry Smith   }
6973a40ed3dSBarry Smith   PetscFunctionReturn(0);
698d6de1c52SSatish Balay }
69957b952d6SSatish Balay 
700fef45726SSatish Balay /*
701fef45726SSatish Balay   Creates the hash table, and sets the table
702fef45726SSatish Balay   This table is created only once.
703fef45726SSatish Balay   If new entried need to be added to the matrix
704fef45726SSatish Balay   then the hash table has to be destroyed and
705fef45726SSatish Balay   recreated.
706fef45726SSatish Balay */
7074a2ae208SSatish Balay #undef __FUNCT__
7084a2ae208SSatish Balay #define __FUNCT__ "MatCreateHashTable_MPIBAIJ_Private"
709dfbe8321SBarry Smith PetscErrorCode MatCreateHashTable_MPIBAIJ_Private(Mat mat,PetscReal factor)
710596b8d2eSBarry Smith {
711596b8d2eSBarry Smith   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
712596b8d2eSBarry Smith   Mat            A     = baij->A,B=baij->B;
713596b8d2eSBarry Smith   Mat_SeqBAIJ    *a    = (Mat_SeqBAIJ*)A->data,*b=(Mat_SeqBAIJ*)B->data;
714b24ad042SBarry Smith   PetscInt       i,j,k,nz=a->nz+b->nz,h1,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
7156849ba73SBarry Smith   PetscErrorCode ierr;
716fca92195SBarry Smith   PetscInt       ht_size,bs2=baij->bs2,rstart=baij->rstartbs;
717899cda47SBarry Smith   PetscInt       cstart=baij->cstartbs,*garray=baij->garray,row,col,Nbs=baij->Nbs;
718b24ad042SBarry Smith   PetscInt       *HT,key;
7193eda8832SBarry Smith   MatScalar      **HD;
720329f5518SBarry Smith   PetscReal      tmp;
7216cf91177SBarry Smith #if defined(PETSC_USE_INFO)
722b24ad042SBarry Smith   PetscInt ct=0,max=0;
7234a15367fSSatish Balay #endif
724fef45726SSatish Balay 
725d64ed03dSBarry Smith   PetscFunctionBegin;
726fca92195SBarry Smith   if (baij->ht) PetscFunctionReturn(0);
727fef45726SSatish Balay 
728fca92195SBarry Smith   baij->ht_size = (PetscInt)(factor*nz);
729fca92195SBarry Smith   ht_size       = baij->ht_size;
7300bdbc534SSatish Balay 
731fef45726SSatish Balay   /* Allocate Memory for Hash Table */
732fca92195SBarry Smith   ierr = PetscMalloc2(ht_size,MatScalar*,&baij->hd,ht_size,PetscInt,&baij->ht);CHKERRQ(ierr);
733fca92195SBarry Smith   ierr = PetscMemzero(baij->hd,ht_size*sizeof(MatScalar*));CHKERRQ(ierr);
734fca92195SBarry Smith   ierr = PetscMemzero(baij->ht,ht_size*sizeof(PetscInt));CHKERRQ(ierr);
735b9e4cc15SSatish Balay   HD   = baij->hd;
736a07cd24cSSatish Balay   HT   = baij->ht;
737b9e4cc15SSatish Balay 
738596b8d2eSBarry Smith   /* Loop Over A */
7390bdbc534SSatish Balay   for (i=0; i<a->mbs; i++) {
740596b8d2eSBarry Smith     for (j=ai[i]; j<ai[i+1]; j++) {
7410bdbc534SSatish Balay       row = i+rstart;
7420bdbc534SSatish Balay       col = aj[j]+cstart;
743596b8d2eSBarry Smith 
744187ce0cbSSatish Balay       key = row*Nbs + col + 1;
745fca92195SBarry Smith       h1  = HASH(ht_size,key,tmp);
746fca92195SBarry Smith       for (k=0; k<ht_size; k++) {
747fca92195SBarry Smith         if (!HT[(h1+k)%ht_size]) {
748fca92195SBarry Smith           HT[(h1+k)%ht_size] = key;
749fca92195SBarry Smith           HD[(h1+k)%ht_size] = a->a + j*bs2;
750596b8d2eSBarry Smith           break;
7516cf91177SBarry Smith #if defined(PETSC_USE_INFO)
752187ce0cbSSatish Balay         } else {
753187ce0cbSSatish Balay           ct++;
754187ce0cbSSatish Balay #endif
755596b8d2eSBarry Smith         }
756187ce0cbSSatish Balay       }
7576cf91177SBarry Smith #if defined(PETSC_USE_INFO)
758187ce0cbSSatish Balay       if (k> max) max = k;
759187ce0cbSSatish Balay #endif
760596b8d2eSBarry Smith     }
761596b8d2eSBarry Smith   }
762596b8d2eSBarry Smith   /* Loop Over B */
7630bdbc534SSatish Balay   for (i=0; i<b->mbs; i++) {
764596b8d2eSBarry Smith     for (j=bi[i]; j<bi[i+1]; j++) {
7650bdbc534SSatish Balay       row = i+rstart;
7660bdbc534SSatish Balay       col = garray[bj[j]];
767187ce0cbSSatish Balay       key = row*Nbs + col + 1;
768fca92195SBarry Smith       h1  = HASH(ht_size,key,tmp);
769fca92195SBarry Smith       for (k=0; k<ht_size; k++) {
770fca92195SBarry Smith         if (!HT[(h1+k)%ht_size]) {
771fca92195SBarry Smith           HT[(h1+k)%ht_size] = key;
772fca92195SBarry Smith           HD[(h1+k)%ht_size] = b->a + j*bs2;
773596b8d2eSBarry Smith           break;
7746cf91177SBarry Smith #if defined(PETSC_USE_INFO)
775187ce0cbSSatish Balay         } else {
776187ce0cbSSatish Balay           ct++;
777187ce0cbSSatish Balay #endif
778596b8d2eSBarry Smith         }
779187ce0cbSSatish Balay       }
7806cf91177SBarry Smith #if defined(PETSC_USE_INFO)
781187ce0cbSSatish Balay       if (k> max) max = k;
782187ce0cbSSatish Balay #endif
783596b8d2eSBarry Smith     }
784596b8d2eSBarry Smith   }
785596b8d2eSBarry Smith 
786596b8d2eSBarry Smith   /* Print Summary */
7876cf91177SBarry Smith #if defined(PETSC_USE_INFO)
788fca92195SBarry Smith   for (i=0,j=0; i<ht_size; i++) {
78926fbe8dcSKarl Rupp     if (HT[i]) j++;
790c38d4ed2SBarry Smith   }
7911e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Average Search = %5.2f,max search = %D\n",(!j)? 0.0:((PetscReal)(ct+j))/j,max);CHKERRQ(ierr);
792187ce0cbSSatish Balay #endif
7933a40ed3dSBarry Smith   PetscFunctionReturn(0);
794596b8d2eSBarry Smith }
79557b952d6SSatish Balay 
7964a2ae208SSatish Balay #undef __FUNCT__
7974a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIBAIJ"
798dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIBAIJ(Mat mat,MatAssemblyType mode)
799bbb85fb3SSatish Balay {
800bbb85fb3SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
801dfbe8321SBarry Smith   PetscErrorCode ierr;
802b24ad042SBarry Smith   PetscInt       nstash,reallocs;
803bbb85fb3SSatish Balay   InsertMode     addv;
804bbb85fb3SSatish Balay 
805bbb85fb3SSatish Balay   PetscFunctionBegin;
80626fbe8dcSKarl Rupp   if (baij->donotstash || mat->nooffprocentries) PetscFunctionReturn(0);
807bbb85fb3SSatish Balay 
808bbb85fb3SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
809ce94432eSBarry Smith   ierr = MPI_Allreduce((PetscEnum*)&mat->insertmode,(PetscEnum*)&addv,1,MPIU_ENUM,MPI_BOR,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
810ce94432eSBarry Smith   if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
811bbb85fb3SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
812bbb85fb3SSatish Balay 
813d0f46423SBarry Smith   ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr);
8141e2582c4SBarry Smith   ierr = MatStashScatterBegin_Private(mat,&mat->bstash,baij->rangebs);CHKERRQ(ierr);
8158798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
8161e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Stash has %D entries,uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
81746680499SSatish Balay   ierr = MatStashGetInfo_Private(&mat->bstash,&nstash,&reallocs);CHKERRQ(ierr);
8181e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
819bbb85fb3SSatish Balay   PetscFunctionReturn(0);
820bbb85fb3SSatish Balay }
821bbb85fb3SSatish Balay 
8224a2ae208SSatish Balay #undef __FUNCT__
8234a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIBAIJ"
824dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIBAIJ(Mat mat,MatAssemblyType mode)
825bbb85fb3SSatish Balay {
826bbb85fb3SSatish Balay   Mat_MPIBAIJ    *baij=(Mat_MPIBAIJ*)mat->data;
82791c97fd4SSatish Balay   Mat_SeqBAIJ    *a   =(Mat_SeqBAIJ*)baij->A->data;
8286849ba73SBarry Smith   PetscErrorCode ierr;
829b24ad042SBarry Smith   PetscInt       i,j,rstart,ncols,flg,bs2=baij->bs2;
830e44c0bd4SBarry Smith   PetscInt       *row,*col;
831ace3abfcSBarry Smith   PetscBool      r1,r2,r3,other_disassembled;
8323eda8832SBarry Smith   MatScalar      *val;
833bbb85fb3SSatish Balay   InsertMode     addv = mat->insertmode;
834b24ad042SBarry Smith   PetscMPIInt    n;
835bbb85fb3SSatish Balay 
836bbb85fb3SSatish Balay   PetscFunctionBegin;
8375fd66863SKarl Rupp   /* do not use 'b=(Mat_SeqBAIJ*)baij->B->data' as B can be reset in disassembly */
8384cb17eb5SBarry Smith   if (!baij->donotstash && !mat->nooffprocentries) {
839a2d1c673SSatish Balay     while (1) {
8408798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
841a2d1c673SSatish Balay       if (!flg) break;
842a2d1c673SSatish Balay 
843bbb85fb3SSatish Balay       for (i=0; i<n;) {
844bbb85fb3SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
84526fbe8dcSKarl Rupp         for (j=i,rstart=row[j]; j<n; j++) {
84626fbe8dcSKarl Rupp           if (row[j] != rstart) break;
84726fbe8dcSKarl Rupp         }
848bbb85fb3SSatish Balay         if (j < n) ncols = j-i;
849bbb85fb3SSatish Balay         else       ncols = n-i;
850bbb85fb3SSatish Balay         /* Now assemble all these values with a single function call */
85197e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
852bbb85fb3SSatish Balay         i    = j;
853bbb85fb3SSatish Balay       }
854bbb85fb3SSatish Balay     }
8558798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
856a2d1c673SSatish Balay     /* Now process the block-stash. Since the values are stashed column-oriented,
857a2d1c673SSatish Balay        set the roworiented flag to column oriented, and after MatSetValues()
858a2d1c673SSatish Balay        restore the original flags */
859a2d1c673SSatish Balay     r1 = baij->roworiented;
860a2d1c673SSatish Balay     r2 = a->roworiented;
86191c97fd4SSatish Balay     r3 = ((Mat_SeqBAIJ*)baij->B->data)->roworiented;
86226fbe8dcSKarl Rupp 
8637c922b88SBarry Smith     baij->roworiented = PETSC_FALSE;
8647c922b88SBarry Smith     a->roworiented    = PETSC_FALSE;
86526fbe8dcSKarl Rupp 
86691c97fd4SSatish Balay     (((Mat_SeqBAIJ*)baij->B->data))->roworiented = PETSC_FALSE; /* b->roworiented */
867a2d1c673SSatish Balay     while (1) {
8688798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->bstash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
869a2d1c673SSatish Balay       if (!flg) break;
870a2d1c673SSatish Balay 
871a2d1c673SSatish Balay       for (i=0; i<n;) {
872a2d1c673SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
87326fbe8dcSKarl Rupp         for (j=i,rstart=row[j]; j<n; j++) {
87426fbe8dcSKarl Rupp           if (row[j] != rstart) break;
87526fbe8dcSKarl Rupp         }
876a2d1c673SSatish Balay         if (j < n) ncols = j-i;
877a2d1c673SSatish Balay         else       ncols = n-i;
87897e5c40aSBarry Smith         ierr = MatSetValuesBlocked_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i*bs2,addv);CHKERRQ(ierr);
879a2d1c673SSatish Balay         i    = j;
880a2d1c673SSatish Balay       }
881a2d1c673SSatish Balay     }
8828798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->bstash);CHKERRQ(ierr);
88326fbe8dcSKarl Rupp 
884a2d1c673SSatish Balay     baij->roworiented = r1;
885a2d1c673SSatish Balay     a->roworiented    = r2;
88626fbe8dcSKarl Rupp 
88791c97fd4SSatish Balay     ((Mat_SeqBAIJ*)baij->B->data)->roworiented = r3; /* b->roworiented */
888bbb85fb3SSatish Balay   }
889bbb85fb3SSatish Balay 
890bbb85fb3SSatish Balay   ierr = MatAssemblyBegin(baij->A,mode);CHKERRQ(ierr);
891bbb85fb3SSatish Balay   ierr = MatAssemblyEnd(baij->A,mode);CHKERRQ(ierr);
892bbb85fb3SSatish Balay 
893bbb85fb3SSatish Balay   /* determine if any processor has disassembled, if so we must
894bbb85fb3SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
895bbb85fb3SSatish Balay   /*
896bbb85fb3SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
897bbb85fb3SSatish Balay      no processor disassembled thus we can skip this stuff
898bbb85fb3SSatish Balay   */
899bbb85fb3SSatish Balay   if (!((Mat_SeqBAIJ*)baij->B->data)->nonew) {
900ce94432eSBarry Smith     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
901bbb85fb3SSatish Balay     if (mat->was_assembled && !other_disassembled) {
902ab9863d7SBarry Smith       ierr = MatDisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
903bbb85fb3SSatish Balay     }
904bbb85fb3SSatish Balay   }
905bbb85fb3SSatish Balay 
906bbb85fb3SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
907bbb85fb3SSatish Balay     ierr = MatSetUpMultiply_MPIBAIJ(mat);CHKERRQ(ierr);
908bbb85fb3SSatish Balay   }
9094e35b6f3SSatish Balay   ierr = MatSetOption(baij->B,MAT_CHECK_COMPRESSED_ROW,PETSC_FALSE);CHKERRQ(ierr);
910bbb85fb3SSatish Balay   ierr = MatAssemblyBegin(baij->B,mode);CHKERRQ(ierr);
911bbb85fb3SSatish Balay   ierr = MatAssemblyEnd(baij->B,mode);CHKERRQ(ierr);
912bbb85fb3SSatish Balay 
9136cf91177SBarry Smith #if defined(PETSC_USE_INFO)
914bbb85fb3SSatish Balay   if (baij->ht && mode== MAT_FINAL_ASSEMBLY) {
9151e2582c4SBarry Smith     ierr = PetscInfo1(mat,"Average Hash Table Search in MatSetValues = %5.2f\n",((PetscReal)baij->ht_total_ct)/baij->ht_insert_ct);CHKERRQ(ierr);
91626fbe8dcSKarl Rupp 
917bbb85fb3SSatish Balay     baij->ht_total_ct  = 0;
918bbb85fb3SSatish Balay     baij->ht_insert_ct = 0;
919bbb85fb3SSatish Balay   }
920bbb85fb3SSatish Balay #endif
921bbb85fb3SSatish Balay   if (baij->ht_flag && !baij->ht && mode == MAT_FINAL_ASSEMBLY) {
922bbb85fb3SSatish Balay     ierr = MatCreateHashTable_MPIBAIJ_Private(mat,baij->ht_fact);CHKERRQ(ierr);
92326fbe8dcSKarl Rupp 
924bbb85fb3SSatish Balay     mat->ops->setvalues        = MatSetValues_MPIBAIJ_HT;
925bbb85fb3SSatish Balay     mat->ops->setvaluesblocked = MatSetValuesBlocked_MPIBAIJ_HT;
926bbb85fb3SSatish Balay   }
927bbb85fb3SSatish Balay 
928fca92195SBarry Smith   ierr = PetscFree2(baij->rowvalues,baij->rowindices);CHKERRQ(ierr);
92926fbe8dcSKarl Rupp 
930606d414cSSatish Balay   baij->rowvalues = 0;
931bbb85fb3SSatish Balay   PetscFunctionReturn(0);
932bbb85fb3SSatish Balay }
93357b952d6SSatish Balay 
9349804daf3SBarry Smith #include <petscdraw.h>
9354a2ae208SSatish Balay #undef __FUNCT__
9364a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIBAIJ_ASCIIorDraworSocket"
9376849ba73SBarry Smith static PetscErrorCode MatView_MPIBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
93857b952d6SSatish Balay {
93957b952d6SSatish Balay   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
940dfbe8321SBarry Smith   PetscErrorCode    ierr;
941b24ad042SBarry Smith   PetscMPIInt       size = baij->size,rank = baij->rank;
942d0f46423SBarry Smith   PetscInt          bs   = mat->rmap->bs;
943ace3abfcSBarry Smith   PetscBool         iascii,isdraw;
944b0a32e0cSBarry Smith   PetscViewer       sviewer;
945f3ef73ceSBarry Smith   PetscViewerFormat format;
94657b952d6SSatish Balay 
947d64ed03dSBarry Smith   PetscFunctionBegin;
948251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
949251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
95032077d6dSBarry Smith   if (iascii) {
951b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
952456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
9534e220ebcSLois Curfman McInnes       MatInfo info;
954ce94432eSBarry Smith       ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);CHKERRQ(ierr);
955d41123aaSBarry Smith       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
9567b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr);
95777431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D bs %D mem %D\n",
95816608c43SJed Brown                                                 rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,mat->rmap->bs,(PetscInt)info.memory);CHKERRQ(ierr);
959d132466eSBarry Smith       ierr = MatGetInfo(baij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
960e6dd01d4SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
961d132466eSBarry Smith       ierr = MatGetInfo(baij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
962e6dd01d4SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
963b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
9647b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr);
96507d81ca4SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr);
96657b952d6SSatish Balay       ierr = VecScatterView(baij->Mvctx,viewer);CHKERRQ(ierr);
9673a40ed3dSBarry Smith       PetscFunctionReturn(0);
968fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
96977431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);CHKERRQ(ierr);
9703a40ed3dSBarry Smith       PetscFunctionReturn(0);
97104929863SHong Zhang     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
97204929863SHong Zhang       PetscFunctionReturn(0);
97357b952d6SSatish Balay     }
97457b952d6SSatish Balay   }
97557b952d6SSatish Balay 
9760f5bd95cSBarry Smith   if (isdraw) {
977b0a32e0cSBarry Smith     PetscDraw draw;
978ace3abfcSBarry Smith     PetscBool isnull;
979b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
980b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
98157b952d6SSatish Balay   }
98257b952d6SSatish Balay 
98357b952d6SSatish Balay   if (size == 1) {
9847adad957SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)baij->A,((PetscObject)mat)->name);CHKERRQ(ierr);
98557b952d6SSatish Balay     ierr = MatView(baij->A,viewer);CHKERRQ(ierr);
986d64ed03dSBarry Smith   } else {
98757b952d6SSatish Balay     /* assemble the entire matrix onto first processor. */
98857b952d6SSatish Balay     Mat         A;
98957b952d6SSatish Balay     Mat_SeqBAIJ *Aloc;
990d0f46423SBarry Smith     PetscInt    M = mat->rmap->N,N = mat->cmap->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs;
9913eda8832SBarry Smith     MatScalar   *a;
99257b952d6SSatish Balay 
993f204ca49SKris Buschelman     /* Here we are creating a temporary matrix, so will assume MPIBAIJ is acceptable */
994f204ca49SKris Buschelman     /* Perhaps this should be the type of mat? */
995ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)mat),&A);CHKERRQ(ierr);
99657b952d6SSatish Balay     if (!rank) {
997f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr);
998d64ed03dSBarry Smith     } else {
999f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr);
100057b952d6SSatish Balay     }
1001f204ca49SKris Buschelman     ierr = MatSetType(A,MATMPIBAIJ);CHKERRQ(ierr);
10020298fd71SBarry Smith     ierr = MatMPIBAIJSetPreallocation(A,mat->rmap->bs,0,NULL,0,NULL);CHKERRQ(ierr);
10032b82e772SSatish Balay     ierr = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);
10043bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)A);CHKERRQ(ierr);
100557b952d6SSatish Balay 
100657b952d6SSatish Balay     /* copy over the A part */
100757b952d6SSatish Balay     Aloc = (Mat_SeqBAIJ*)baij->A->data;
100857b952d6SSatish Balay     ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1009b24ad042SBarry Smith     ierr = PetscMalloc(bs*sizeof(PetscInt),&rvals);CHKERRQ(ierr);
101057b952d6SSatish Balay 
101157b952d6SSatish Balay     for (i=0; i<mbs; i++) {
1012899cda47SBarry Smith       rvals[0] = bs*(baij->rstartbs + i);
101326fbe8dcSKarl Rupp       for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
101457b952d6SSatish Balay       for (j=ai[i]; j<ai[i+1]; j++) {
1015899cda47SBarry Smith         col = (baij->cstartbs+aj[j])*bs;
101657b952d6SSatish Balay         for (k=0; k<bs; k++) {
101797e5c40aSBarry Smith           ierr      = MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);CHKERRQ(ierr);
1018cee3aa6bSSatish Balay           col++; a += bs;
101957b952d6SSatish Balay         }
102057b952d6SSatish Balay       }
102157b952d6SSatish Balay     }
102257b952d6SSatish Balay     /* copy over the B part */
102357b952d6SSatish Balay     Aloc = (Mat_SeqBAIJ*)baij->B->data;
102457b952d6SSatish Balay     ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
102557b952d6SSatish Balay     for (i=0; i<mbs; i++) {
1026899cda47SBarry Smith       rvals[0] = bs*(baij->rstartbs + i);
102726fbe8dcSKarl Rupp       for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
102857b952d6SSatish Balay       for (j=ai[i]; j<ai[i+1]; j++) {
102957b952d6SSatish Balay         col = baij->garray[aj[j]]*bs;
103057b952d6SSatish Balay         for (k=0; k<bs; k++) {
103197e5c40aSBarry Smith           ierr      = MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);CHKERRQ(ierr);
1032cee3aa6bSSatish Balay           col++; a += bs;
103357b952d6SSatish Balay         }
103457b952d6SSatish Balay       }
103557b952d6SSatish Balay     }
1036606d414cSSatish Balay     ierr = PetscFree(rvals);CHKERRQ(ierr);
10376d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10386d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
103955843e3eSBarry Smith     /*
104055843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
1041b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
104255843e3eSBarry Smith     */
1043b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
1044f14a1c24SBarry Smith     if (!rank) {
10457adad957SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject)((Mat_MPIBAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr);
10467566de4bSShri Abhyankar       /* Set the type name to MATMPIBAIJ so that the correct type can be printed out by PetscObjectPrintClassNamePrefixType() in MatView_SeqBAIJ_ASCII()*/
10477566de4bSShri Abhyankar       PetscStrcpy(((PetscObject)((Mat_MPIBAIJ*)(A->data))->A)->type_name,MATMPIBAIJ);
10486831982aSBarry Smith       ierr = MatView(((Mat_MPIBAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
104957b952d6SSatish Balay     }
1050b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
10516bf464f9SBarry Smith     ierr = MatDestroy(&A);CHKERRQ(ierr);
105257b952d6SSatish Balay   }
10533a40ed3dSBarry Smith   PetscFunctionReturn(0);
105457b952d6SSatish Balay }
105557b952d6SSatish Balay 
10564a2ae208SSatish Balay #undef __FUNCT__
1057660746e0SBarry Smith #define __FUNCT__ "MatView_MPIBAIJ_Binary"
1058660746e0SBarry Smith static PetscErrorCode MatView_MPIBAIJ_Binary(Mat mat,PetscViewer viewer)
1059660746e0SBarry Smith {
1060660746e0SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)mat->data;
1061660746e0SBarry Smith   Mat_SeqBAIJ    *A = (Mat_SeqBAIJ*)a->A->data;
1062660746e0SBarry Smith   Mat_SeqBAIJ    *B = (Mat_SeqBAIJ*)a->B->data;
1063660746e0SBarry Smith   PetscErrorCode ierr;
10645f48b12bSBarry Smith   PetscInt       i,*row_lens,*crow_lens,bs = mat->rmap->bs,j,k,bs2=a->bs2,header[4],nz,rlen;
1065e96a6426SSatish Balay   PetscInt       *range=0,nzmax,*column_indices,cnt,col,*garray = a->garray,cstart = mat->cmap->rstart/bs,len,pcnt,l,ll;
1066660746e0SBarry Smith   int            fd;
1067660746e0SBarry Smith   PetscScalar    *column_values;
1068660746e0SBarry Smith   FILE           *file;
1069660746e0SBarry Smith   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
1070638eb2ebSBarry Smith   PetscInt       message_count,flowcontrolcount;
1071660746e0SBarry Smith 
1072660746e0SBarry Smith   PetscFunctionBegin;
1073ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);CHKERRQ(ierr);
1074ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr);
1075660746e0SBarry Smith   nz   = bs2*(A->nz + B->nz);
1076660746e0SBarry Smith   rlen = mat->rmap->n;
1077660746e0SBarry Smith   if (!rank) {
1078660746e0SBarry Smith     header[0] = MAT_FILE_CLASSID;
1079660746e0SBarry Smith     header[1] = mat->rmap->N;
1080660746e0SBarry Smith     header[2] = mat->cmap->N;
108126fbe8dcSKarl Rupp 
1082ce94432eSBarry Smith     ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1083660746e0SBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
1084660746e0SBarry Smith     ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1085660746e0SBarry Smith     /* get largest number of rows any processor has */
1086660746e0SBarry Smith     range = mat->rmap->range;
1087660746e0SBarry Smith     for (i=1; i<size; i++) {
1088660746e0SBarry Smith       rlen = PetscMax(rlen,range[i+1] - range[i]);
1089660746e0SBarry Smith     }
1090660746e0SBarry Smith   } else {
1091ce94432eSBarry Smith     ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1092660746e0SBarry Smith   }
1093660746e0SBarry Smith 
1094660746e0SBarry Smith   ierr = PetscMalloc((rlen/bs)*sizeof(PetscInt),&crow_lens);CHKERRQ(ierr);
1095660746e0SBarry Smith   /* compute lengths of each row  */
1096660746e0SBarry Smith   for (i=0; i<a->mbs; i++) {
1097660746e0SBarry Smith     crow_lens[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
1098660746e0SBarry Smith   }
1099660746e0SBarry Smith   /* store the row lengths to the file */
1100638eb2ebSBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1101660746e0SBarry Smith   if (!rank) {
1102660746e0SBarry Smith     MPI_Status status;
1103660746e0SBarry Smith     ierr = PetscMalloc(rlen*sizeof(PetscInt),&row_lens);CHKERRQ(ierr);
1104660746e0SBarry Smith     rlen = (range[1] - range[0])/bs;
1105660746e0SBarry Smith     for (i=0; i<rlen; i++) {
1106660746e0SBarry Smith       for (j=0; j<bs; j++) {
1107660746e0SBarry Smith         row_lens[i*bs+j] = bs*crow_lens[i];
1108660746e0SBarry Smith       }
1109660746e0SBarry Smith     }
1110660746e0SBarry Smith     ierr = PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1111660746e0SBarry Smith     for (i=1; i<size; i++) {
1112660746e0SBarry Smith       rlen = (range[i+1] - range[i])/bs;
1113639ff905SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
1114ce94432eSBarry Smith       ierr = MPI_Recv(crow_lens,rlen,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr);
1115660746e0SBarry Smith       for (k=0; k<rlen; k++) {
1116660746e0SBarry Smith         for (j=0; j<bs; j++) {
1117660746e0SBarry Smith           row_lens[k*bs+j] = bs*crow_lens[k];
1118660746e0SBarry Smith         }
1119660746e0SBarry Smith       }
1120660746e0SBarry Smith       ierr = PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1121660746e0SBarry Smith     }
1122639ff905SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
1123660746e0SBarry Smith     ierr = PetscFree(row_lens);CHKERRQ(ierr);
1124660746e0SBarry Smith   } else {
1125639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
1126ce94432eSBarry Smith     ierr = MPI_Send(crow_lens,mat->rmap->n/bs,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1127639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
1128660746e0SBarry Smith   }
1129660746e0SBarry Smith   ierr = PetscFree(crow_lens);CHKERRQ(ierr);
1130660746e0SBarry Smith 
1131660746e0SBarry Smith   /* load up the local column indices. Include for all rows not just one for each block row since process 0 does not have the
1132660746e0SBarry Smith      information needed to make it for each row from a block row. This does require more communication but still not more than
1133660746e0SBarry Smith      the communication needed for the nonzero values  */
1134660746e0SBarry Smith   nzmax = nz; /*  space a largest processor needs */
1135ce94432eSBarry Smith   ierr  = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1136660746e0SBarry Smith   ierr  = PetscMalloc(nzmax*sizeof(PetscInt),&column_indices);CHKERRQ(ierr);
1137660746e0SBarry Smith   cnt   = 0;
1138660746e0SBarry Smith   for (i=0; i<a->mbs; i++) {
1139660746e0SBarry Smith     pcnt = cnt;
1140660746e0SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
1141660746e0SBarry Smith       if ((col = garray[B->j[j]]) > cstart) break;
1142660746e0SBarry Smith       for (l=0; l<bs; l++) {
1143660746e0SBarry Smith         column_indices[cnt++] = bs*col+l;
1144660746e0SBarry Smith       }
1145660746e0SBarry Smith     }
1146660746e0SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
1147660746e0SBarry Smith       for (l=0; l<bs; l++) {
1148660746e0SBarry Smith         column_indices[cnt++] = bs*(A->j[k] + cstart)+l;
1149660746e0SBarry Smith       }
1150660746e0SBarry Smith     }
1151660746e0SBarry Smith     for (; j<B->i[i+1]; j++) {
1152660746e0SBarry Smith       for (l=0; l<bs; l++) {
1153660746e0SBarry Smith         column_indices[cnt++] = bs*garray[B->j[j]]+l;
1154660746e0SBarry Smith       }
1155660746e0SBarry Smith     }
1156660746e0SBarry Smith     len = cnt - pcnt;
1157660746e0SBarry Smith     for (k=1; k<bs; k++) {
1158660746e0SBarry Smith       ierr = PetscMemcpy(&column_indices[cnt],&column_indices[pcnt],len*sizeof(PetscInt));CHKERRQ(ierr);
1159660746e0SBarry Smith       cnt += len;
1160660746e0SBarry Smith     }
1161660746e0SBarry Smith   }
1162660746e0SBarry Smith   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1163660746e0SBarry Smith 
1164660746e0SBarry Smith   /* store the columns to the file */
1165638eb2ebSBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1166660746e0SBarry Smith   if (!rank) {
1167660746e0SBarry Smith     MPI_Status status;
1168660746e0SBarry Smith     ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1169660746e0SBarry Smith     for (i=1; i<size; i++) {
1170639ff905SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
1171ce94432eSBarry Smith       ierr = MPI_Recv(&cnt,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr);
1172ce94432eSBarry Smith       ierr = MPI_Recv(column_indices,cnt,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr);
1173660746e0SBarry Smith       ierr = PetscBinaryWrite(fd,column_indices,cnt,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1174660746e0SBarry Smith     }
1175639ff905SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
1176660746e0SBarry Smith   } else {
1177639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
1178ce94432eSBarry Smith     ierr = MPI_Send(&cnt,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1179ce94432eSBarry Smith     ierr = MPI_Send(column_indices,cnt,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1180639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
1181660746e0SBarry Smith   }
1182660746e0SBarry Smith   ierr = PetscFree(column_indices);CHKERRQ(ierr);
1183660746e0SBarry Smith 
1184660746e0SBarry Smith   /* load up the numerical values */
1185660746e0SBarry Smith   ierr = PetscMalloc(nzmax*sizeof(PetscScalar),&column_values);CHKERRQ(ierr);
1186660746e0SBarry Smith   cnt  = 0;
1187660746e0SBarry Smith   for (i=0; i<a->mbs; i++) {
1188660746e0SBarry Smith     rlen = bs*(B->i[i+1] - B->i[i] + A->i[i+1] - A->i[i]);
1189660746e0SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
1190660746e0SBarry Smith       if (garray[B->j[j]] > cstart) break;
1191660746e0SBarry Smith       for (l=0; l<bs; l++) {
1192660746e0SBarry Smith         for (ll=0; ll<bs; ll++) {
1193660746e0SBarry Smith           column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1194660746e0SBarry Smith         }
1195660746e0SBarry Smith       }
1196660746e0SBarry Smith       cnt += bs;
1197660746e0SBarry Smith     }
1198660746e0SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
1199660746e0SBarry Smith       for (l=0; l<bs; l++) {
1200660746e0SBarry Smith         for (ll=0; ll<bs; ll++) {
1201660746e0SBarry Smith           column_values[cnt + l*rlen + ll] = A->a[bs2*k+l+bs*ll];
1202660746e0SBarry Smith         }
1203660746e0SBarry Smith       }
1204660746e0SBarry Smith       cnt += bs;
1205660746e0SBarry Smith     }
1206660746e0SBarry Smith     for (; j<B->i[i+1]; j++) {
1207660746e0SBarry Smith       for (l=0; l<bs; l++) {
1208660746e0SBarry Smith         for (ll=0; ll<bs; ll++) {
1209660746e0SBarry Smith           column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1210660746e0SBarry Smith         }
1211660746e0SBarry Smith       }
1212660746e0SBarry Smith       cnt += bs;
1213660746e0SBarry Smith     }
1214660746e0SBarry Smith     cnt += (bs-1)*rlen;
1215660746e0SBarry Smith   }
1216660746e0SBarry Smith   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1217660746e0SBarry Smith 
1218660746e0SBarry Smith   /* store the column values to the file */
1219638eb2ebSBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1220660746e0SBarry Smith   if (!rank) {
1221660746e0SBarry Smith     MPI_Status status;
1222660746e0SBarry Smith     ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
1223660746e0SBarry Smith     for (i=1; i<size; i++) {
1224639ff905SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
1225ce94432eSBarry Smith       ierr = MPI_Recv(&cnt,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr);
1226ce94432eSBarry Smith       ierr = MPI_Recv(column_values,cnt,MPIU_SCALAR,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr);
1227660746e0SBarry Smith       ierr = PetscBinaryWrite(fd,column_values,cnt,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
1228660746e0SBarry Smith     }
1229639ff905SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
1230660746e0SBarry Smith   } else {
1231639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
1232ce94432eSBarry Smith     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1233ce94432eSBarry Smith     ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1234639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
1235660746e0SBarry Smith   }
1236660746e0SBarry Smith   ierr = PetscFree(column_values);CHKERRQ(ierr);
1237660746e0SBarry Smith 
1238660746e0SBarry Smith   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
1239660746e0SBarry Smith   if (file) {
1240660746e0SBarry Smith     fprintf(file,"-matload_block_size %d\n",(int)mat->rmap->bs);
1241660746e0SBarry Smith   }
1242660746e0SBarry Smith   PetscFunctionReturn(0);
1243660746e0SBarry Smith }
1244660746e0SBarry Smith 
1245660746e0SBarry Smith #undef __FUNCT__
12464a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIBAIJ"
1247dfbe8321SBarry Smith PetscErrorCode MatView_MPIBAIJ(Mat mat,PetscViewer viewer)
124857b952d6SSatish Balay {
1249dfbe8321SBarry Smith   PetscErrorCode ierr;
1250ace3abfcSBarry Smith   PetscBool      iascii,isdraw,issocket,isbinary;
125157b952d6SSatish Balay 
1252d64ed03dSBarry Smith   PetscFunctionBegin;
1253251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1254251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1255251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr);
1256251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
1257660746e0SBarry Smith   if (iascii || isdraw || issocket) {
12587b2a1423SBarry Smith     ierr = MatView_MPIBAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
1259660746e0SBarry Smith   } else if (isbinary) {
1260660746e0SBarry Smith     ierr = MatView_MPIBAIJ_Binary(mat,viewer);CHKERRQ(ierr);
126157b952d6SSatish Balay   }
12623a40ed3dSBarry Smith   PetscFunctionReturn(0);
126357b952d6SSatish Balay }
126457b952d6SSatish Balay 
12654a2ae208SSatish Balay #undef __FUNCT__
12664a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIBAIJ"
1267dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIBAIJ(Mat mat)
126879bdfe76SSatish Balay {
126979bdfe76SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
1270dfbe8321SBarry Smith   PetscErrorCode ierr;
127179bdfe76SSatish Balay 
1272d64ed03dSBarry Smith   PetscFunctionBegin;
1273aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1274d0f46423SBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%D,Cols=%D",mat->rmap->N,mat->cmap->N);
127579bdfe76SSatish Balay #endif
12768798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
12778798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->bstash);CHKERRQ(ierr);
12786bf464f9SBarry Smith   ierr = MatDestroy(&baij->A);CHKERRQ(ierr);
12796bf464f9SBarry Smith   ierr = MatDestroy(&baij->B);CHKERRQ(ierr);
1280aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
12816bc0bbbfSBarry Smith   ierr = PetscTableDestroy(&baij->colmap);CHKERRQ(ierr);
128248e59246SSatish Balay #else
128305b42c5fSBarry Smith   ierr = PetscFree(baij->colmap);CHKERRQ(ierr);
128448e59246SSatish Balay #endif
128505b42c5fSBarry Smith   ierr = PetscFree(baij->garray);CHKERRQ(ierr);
12866bf464f9SBarry Smith   ierr = VecDestroy(&baij->lvec);CHKERRQ(ierr);
12876bf464f9SBarry Smith   ierr = VecScatterDestroy(&baij->Mvctx);CHKERRQ(ierr);
1288fca92195SBarry Smith   ierr = PetscFree2(baij->rowvalues,baij->rowindices);CHKERRQ(ierr);
128905b42c5fSBarry Smith   ierr = PetscFree(baij->barray);CHKERRQ(ierr);
1290fca92195SBarry Smith   ierr = PetscFree2(baij->hd,baij->ht);CHKERRQ(ierr);
1291899cda47SBarry Smith   ierr = PetscFree(baij->rangebs);CHKERRQ(ierr);
1292bf0cc555SLisandro Dalcin   ierr = PetscFree(mat->data);CHKERRQ(ierr);
1293901853e0SKris Buschelman 
1294dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr);
1295bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);CHKERRQ(ierr);
1296bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
1297bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C",NULL);CHKERRQ(ierr);
1298bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
1299bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
1300bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",NULL);CHKERRQ(ierr);
1301bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatSetHashTableFactor_C",NULL);CHKERRQ(ierr);
1302bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_mpisbaij_C",NULL);CHKERRQ(ierr);
1303bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_mpibstrm_C",NULL);CHKERRQ(ierr);
13043a40ed3dSBarry Smith   PetscFunctionReturn(0);
130579bdfe76SSatish Balay }
130679bdfe76SSatish Balay 
13074a2ae208SSatish Balay #undef __FUNCT__
13084a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIBAIJ"
1309dfbe8321SBarry Smith PetscErrorCode MatMult_MPIBAIJ(Mat A,Vec xx,Vec yy)
1310cee3aa6bSSatish Balay {
1311cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1312dfbe8321SBarry Smith   PetscErrorCode ierr;
1313b24ad042SBarry Smith   PetscInt       nt;
1314cee3aa6bSSatish Balay 
1315d64ed03dSBarry Smith   PetscFunctionBegin;
1316e1311b90SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
1317e7e72b3dSBarry Smith   if (nt != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");
1318e1311b90SBarry Smith   ierr = VecGetLocalSize(yy,&nt);CHKERRQ(ierr);
1319e7e72b3dSBarry Smith   if (nt != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible parition of A and yy");
1320ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1321f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
1322ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1323f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
13243a40ed3dSBarry Smith   PetscFunctionReturn(0);
1325cee3aa6bSSatish Balay }
1326cee3aa6bSSatish Balay 
13274a2ae208SSatish Balay #undef __FUNCT__
13284a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIBAIJ"
1329dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1330cee3aa6bSSatish Balay {
1331cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1332dfbe8321SBarry Smith   PetscErrorCode ierr;
1333d64ed03dSBarry Smith 
1334d64ed03dSBarry Smith   PetscFunctionBegin;
1335ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1336f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1337ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1338f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
13393a40ed3dSBarry Smith   PetscFunctionReturn(0);
1340cee3aa6bSSatish Balay }
1341cee3aa6bSSatish Balay 
13424a2ae208SSatish Balay #undef __FUNCT__
13434a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIBAIJ"
1344dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIBAIJ(Mat A,Vec xx,Vec yy)
1345cee3aa6bSSatish Balay {
1346cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1347dfbe8321SBarry Smith   PetscErrorCode ierr;
1348ace3abfcSBarry Smith   PetscBool      merged;
1349cee3aa6bSSatish Balay 
1350d64ed03dSBarry Smith   PetscFunctionBegin;
1351a5ff213dSBarry Smith   ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr);
1352cee3aa6bSSatish Balay   /* do nondiagonal part */
13537c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1354a5ff213dSBarry Smith   if (!merged) {
1355cee3aa6bSSatish Balay     /* send it on its way */
1356ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1357cee3aa6bSSatish Balay     /* do local part */
13587c922b88SBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1359cee3aa6bSSatish Balay     /* receive remote parts: note this assumes the values are not actually */
1360a5ff213dSBarry Smith     /* inserted in yy until the next line */
1361ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1362a5ff213dSBarry Smith   } else {
1363a5ff213dSBarry Smith     /* do local part */
1364a5ff213dSBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1365a5ff213dSBarry Smith     /* send it on its way */
1366ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1367a5ff213dSBarry Smith     /* values actually were received in the Begin() but we need to call this nop */
1368ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1369a5ff213dSBarry Smith   }
13703a40ed3dSBarry Smith   PetscFunctionReturn(0);
1371cee3aa6bSSatish Balay }
1372cee3aa6bSSatish Balay 
13734a2ae208SSatish Balay #undef __FUNCT__
13744a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIBAIJ"
1375dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1376cee3aa6bSSatish Balay {
1377cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1378dfbe8321SBarry Smith   PetscErrorCode ierr;
1379cee3aa6bSSatish Balay 
1380d64ed03dSBarry Smith   PetscFunctionBegin;
1381cee3aa6bSSatish Balay   /* do nondiagonal part */
13827c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1383cee3aa6bSSatish Balay   /* send it on its way */
1384ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1385cee3aa6bSSatish Balay   /* do local part */
13867c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1387cee3aa6bSSatish Balay   /* receive remote parts: note this assumes the values are not actually */
1388cee3aa6bSSatish Balay   /* inserted in yy until the next line, which is true for my implementation*/
1389cee3aa6bSSatish Balay   /* but is not perhaps always true. */
1390ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
13913a40ed3dSBarry Smith   PetscFunctionReturn(0);
1392cee3aa6bSSatish Balay }
1393cee3aa6bSSatish Balay 
1394cee3aa6bSSatish Balay /*
1395cee3aa6bSSatish Balay   This only works correctly for square matrices where the subblock A->A is the
1396cee3aa6bSSatish Balay    diagonal block
1397cee3aa6bSSatish Balay */
13984a2ae208SSatish Balay #undef __FUNCT__
13994a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIBAIJ"
1400dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIBAIJ(Mat A,Vec v)
1401cee3aa6bSSatish Balay {
1402cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1403dfbe8321SBarry Smith   PetscErrorCode ierr;
1404d64ed03dSBarry Smith 
1405d64ed03dSBarry Smith   PetscFunctionBegin;
1406e32f2f54SBarry 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");
14073a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
14083a40ed3dSBarry Smith   PetscFunctionReturn(0);
1409cee3aa6bSSatish Balay }
1410cee3aa6bSSatish Balay 
14114a2ae208SSatish Balay #undef __FUNCT__
14124a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIBAIJ"
1413f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIBAIJ(Mat A,PetscScalar aa)
1414cee3aa6bSSatish Balay {
1415cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1416dfbe8321SBarry Smith   PetscErrorCode ierr;
1417d64ed03dSBarry Smith 
1418d64ed03dSBarry Smith   PetscFunctionBegin;
1419f4df32b1SMatthew Knepley   ierr = MatScale(a->A,aa);CHKERRQ(ierr);
1420f4df32b1SMatthew Knepley   ierr = MatScale(a->B,aa);CHKERRQ(ierr);
14213a40ed3dSBarry Smith   PetscFunctionReturn(0);
1422cee3aa6bSSatish Balay }
1423026e39d0SSatish Balay 
14244a2ae208SSatish Balay #undef __FUNCT__
14254a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIBAIJ"
1426b24ad042SBarry Smith PetscErrorCode MatGetRow_MPIBAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1427acdf5bf4SSatish Balay {
1428acdf5bf4SSatish Balay   Mat_MPIBAIJ    *mat = (Mat_MPIBAIJ*)matin->data;
142987828ca2SBarry Smith   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
14306849ba73SBarry Smith   PetscErrorCode ierr;
1431d0f46423SBarry Smith   PetscInt       bs = matin->rmap->bs,bs2 = mat->bs2,i,*cworkA,*cworkB,**pcA,**pcB;
1432d0f46423SBarry Smith   PetscInt       nztot,nzA,nzB,lrow,brstart = matin->rmap->rstart,brend = matin->rmap->rend;
1433899cda47SBarry Smith   PetscInt       *cmap,*idx_p,cstart = mat->cstartbs;
1434acdf5bf4SSatish Balay 
1435d64ed03dSBarry Smith   PetscFunctionBegin;
1436e7e72b3dSBarry Smith   if (row < brstart || row >= brend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local rows");
1437e32f2f54SBarry Smith   if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active");
1438acdf5bf4SSatish Balay   mat->getrowactive = PETSC_TRUE;
1439acdf5bf4SSatish Balay 
1440acdf5bf4SSatish Balay   if (!mat->rowvalues && (idx || v)) {
1441acdf5bf4SSatish Balay     /*
1442acdf5bf4SSatish Balay         allocate enough space to hold information from the longest row.
1443acdf5bf4SSatish Balay     */
1444acdf5bf4SSatish Balay     Mat_SeqBAIJ *Aa = (Mat_SeqBAIJ*)mat->A->data,*Ba = (Mat_SeqBAIJ*)mat->B->data;
1445b24ad042SBarry Smith     PetscInt    max = 1,mbs = mat->mbs,tmp;
1446bd16c2feSSatish Balay     for (i=0; i<mbs; i++) {
1447acdf5bf4SSatish Balay       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
144826fbe8dcSKarl Rupp       if (max < tmp) max = tmp;
1449acdf5bf4SSatish Balay     }
1450fca92195SBarry Smith     ierr = PetscMalloc2(max*bs2,PetscScalar,&mat->rowvalues,max*bs2,PetscInt,&mat->rowindices);CHKERRQ(ierr);
1451acdf5bf4SSatish Balay   }
1452d9d09a02SSatish Balay   lrow = row - brstart;
1453acdf5bf4SSatish Balay 
1454acdf5bf4SSatish Balay   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1455acdf5bf4SSatish Balay   if (!v)   {pvA = 0; pvB = 0;}
1456acdf5bf4SSatish Balay   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1457f830108cSBarry Smith   ierr  = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1458f830108cSBarry Smith   ierr  = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1459acdf5bf4SSatish Balay   nztot = nzA + nzB;
1460acdf5bf4SSatish Balay 
1461acdf5bf4SSatish Balay   cmap = mat->garray;
1462acdf5bf4SSatish Balay   if (v  || idx) {
1463acdf5bf4SSatish Balay     if (nztot) {
1464acdf5bf4SSatish Balay       /* Sort by increasing column numbers, assuming A and B already sorted */
1465b24ad042SBarry Smith       PetscInt imark = -1;
1466acdf5bf4SSatish Balay       if (v) {
1467acdf5bf4SSatish Balay         *v = v_p = mat->rowvalues;
1468acdf5bf4SSatish Balay         for (i=0; i<nzB; i++) {
1469d9d09a02SSatish Balay           if (cmap[cworkB[i]/bs] < cstart) v_p[i] = vworkB[i];
1470acdf5bf4SSatish Balay           else break;
1471acdf5bf4SSatish Balay         }
1472acdf5bf4SSatish Balay         imark = i;
1473acdf5bf4SSatish Balay         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1474acdf5bf4SSatish Balay         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1475acdf5bf4SSatish Balay       }
1476acdf5bf4SSatish Balay       if (idx) {
1477acdf5bf4SSatish Balay         *idx = idx_p = mat->rowindices;
1478acdf5bf4SSatish Balay         if (imark > -1) {
1479acdf5bf4SSatish Balay           for (i=0; i<imark; i++) {
1480bd16c2feSSatish Balay             idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1481acdf5bf4SSatish Balay           }
1482acdf5bf4SSatish Balay         } else {
1483acdf5bf4SSatish Balay           for (i=0; i<nzB; i++) {
148426fbe8dcSKarl Rupp             if (cmap[cworkB[i]/bs] < cstart) idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1485acdf5bf4SSatish Balay             else break;
1486acdf5bf4SSatish Balay           }
1487acdf5bf4SSatish Balay           imark = i;
1488acdf5bf4SSatish Balay         }
1489d9d09a02SSatish Balay         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart*bs + cworkA[i];
1490d9d09a02SSatish Balay         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1491acdf5bf4SSatish Balay       }
1492d64ed03dSBarry Smith     } else {
1493d212a18eSSatish Balay       if (idx) *idx = 0;
1494d212a18eSSatish Balay       if (v)   *v   = 0;
1495d212a18eSSatish Balay     }
1496acdf5bf4SSatish Balay   }
1497acdf5bf4SSatish Balay   *nz  = nztot;
1498f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1499f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
15003a40ed3dSBarry Smith   PetscFunctionReturn(0);
1501acdf5bf4SSatish Balay }
1502acdf5bf4SSatish Balay 
15034a2ae208SSatish Balay #undef __FUNCT__
15044a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIBAIJ"
1505b24ad042SBarry Smith PetscErrorCode MatRestoreRow_MPIBAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1506acdf5bf4SSatish Balay {
1507acdf5bf4SSatish Balay   Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1508d64ed03dSBarry Smith 
1509d64ed03dSBarry Smith   PetscFunctionBegin;
1510e7e72b3dSBarry Smith   if (!baij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
1511acdf5bf4SSatish Balay   baij->getrowactive = PETSC_FALSE;
15123a40ed3dSBarry Smith   PetscFunctionReturn(0);
1513acdf5bf4SSatish Balay }
1514acdf5bf4SSatish Balay 
15154a2ae208SSatish Balay #undef __FUNCT__
15164a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIBAIJ"
1517dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIBAIJ(Mat A)
151858667388SSatish Balay {
151958667388SSatish Balay   Mat_MPIBAIJ    *l = (Mat_MPIBAIJ*)A->data;
1520dfbe8321SBarry Smith   PetscErrorCode ierr;
1521d64ed03dSBarry Smith 
1522d64ed03dSBarry Smith   PetscFunctionBegin;
152358667388SSatish Balay   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
152458667388SSatish Balay   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
15253a40ed3dSBarry Smith   PetscFunctionReturn(0);
152658667388SSatish Balay }
15270ac07820SSatish Balay 
15284a2ae208SSatish Balay #undef __FUNCT__
15294a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIBAIJ"
1530dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIBAIJ(Mat matin,MatInfoType flag,MatInfo *info)
15310ac07820SSatish Balay {
15324e220ebcSLois Curfman McInnes   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)matin->data;
15334e220ebcSLois Curfman McInnes   Mat            A  = a->A,B = a->B;
1534dfbe8321SBarry Smith   PetscErrorCode ierr;
1535329f5518SBarry Smith   PetscReal      isend[5],irecv[5];
15360ac07820SSatish Balay 
1537d64ed03dSBarry Smith   PetscFunctionBegin;
1538d0f46423SBarry Smith   info->block_size = (PetscReal)matin->rmap->bs;
153926fbe8dcSKarl Rupp 
15404e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
154126fbe8dcSKarl Rupp 
15420e4b21beSBarry Smith   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1543de87f314SBarry Smith   isend[3] = info->memory;  isend[4] = info->mallocs;
154426fbe8dcSKarl Rupp 
15454e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
154626fbe8dcSKarl Rupp 
15470e4b21beSBarry Smith   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1548de87f314SBarry Smith   isend[3] += info->memory;  isend[4] += info->mallocs;
154926fbe8dcSKarl Rupp 
15500ac07820SSatish Balay   if (flag == MAT_LOCAL) {
15514e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
15524e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
15534e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
15544e220ebcSLois Curfman McInnes     info->memory       = isend[3];
15554e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
15560ac07820SSatish Balay   } else if (flag == MAT_GLOBAL_MAX) {
1557ce94432eSBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));CHKERRQ(ierr);
155826fbe8dcSKarl Rupp 
15594e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
15604e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
15614e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
15624e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
15634e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
15640ac07820SSatish Balay   } else if (flag == MAT_GLOBAL_SUM) {
1565ce94432eSBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));CHKERRQ(ierr);
156626fbe8dcSKarl Rupp 
15674e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
15684e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
15694e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
15704e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
15714e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1572ce94432eSBarry Smith   } else SETERRQ1(PetscObjectComm((PetscObject)matin),PETSC_ERR_ARG_WRONG,"Unknown MatInfoType argument %d",(int)flag);
15734e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
15744e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
15754e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
15763a40ed3dSBarry Smith   PetscFunctionReturn(0);
15770ac07820SSatish Balay }
15780ac07820SSatish Balay 
15794a2ae208SSatish Balay #undef __FUNCT__
15804a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIBAIJ"
1581ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIBAIJ(Mat A,MatOption op,PetscBool flg)
158258667388SSatish Balay {
158358667388SSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1584dfbe8321SBarry Smith   PetscErrorCode ierr;
158558667388SSatish Balay 
1586d64ed03dSBarry Smith   PetscFunctionBegin;
158712c028f9SKris Buschelman   switch (op) {
1588512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
158912c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
159028b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1591a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
159212c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
15934e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
15944e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
159512c028f9SKris Buschelman     break;
159612c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
15974e0d8c25SBarry Smith     a->roworiented = flg;
159826fbe8dcSKarl Rupp 
15994e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
16004e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
160112c028f9SKris Buschelman     break;
16024e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1603290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
160412c028f9SKris Buschelman     break;
160512c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
16064e0d8c25SBarry Smith     a->donotstash = flg;
160712c028f9SKris Buschelman     break;
160812c028f9SKris Buschelman   case MAT_USE_HASH_TABLE:
16094e0d8c25SBarry Smith     a->ht_flag = flg;
161012c028f9SKris Buschelman     break;
161177e54ba9SKris Buschelman   case MAT_SYMMETRIC:
161277e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
16132188ac68SBarry Smith   case MAT_HERMITIAN:
16142188ac68SBarry Smith   case MAT_SYMMETRY_ETERNAL:
16154e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
161677e54ba9SKris Buschelman     break;
161712c028f9SKris Buschelman   default:
1618ce94432eSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"unknown option %d",op);
1619d64ed03dSBarry Smith   }
16203a40ed3dSBarry Smith   PetscFunctionReturn(0);
162158667388SSatish Balay }
162258667388SSatish Balay 
16234a2ae208SSatish Balay #undef __FUNCT__
16246a719282SBarry Smith #define __FUNCT__ "MatTranspose_MPIBAIJ"
1625fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIBAIJ(Mat A,MatReuse reuse,Mat *matout)
16260ac07820SSatish Balay {
16270ac07820SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)A->data;
16280ac07820SSatish Balay   Mat_SeqBAIJ    *Aloc;
16290ac07820SSatish Balay   Mat            B;
1630dfbe8321SBarry Smith   PetscErrorCode ierr;
1631d0f46423SBarry Smith   PetscInt       M =A->rmap->N,N=A->cmap->N,*ai,*aj,i,*rvals,j,k,col;
1632d0f46423SBarry Smith   PetscInt       bs=A->rmap->bs,mbs=baij->mbs;
16333eda8832SBarry Smith   MatScalar      *a;
16340ac07820SSatish Balay 
1635d64ed03dSBarry Smith   PetscFunctionBegin;
1636ce94432eSBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1637fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
1638ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
1639d0f46423SBarry Smith     ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr);
16407adad957SLisandro Dalcin     ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr);
16412e72b8d9SBarry Smith     /* Do not know preallocation information, but must set block size */
16420298fd71SBarry Smith     ierr = MatMPIBAIJSetPreallocation(B,A->rmap->bs,PETSC_DECIDE,NULL,PETSC_DECIDE,NULL);CHKERRQ(ierr);
1643fc4dec0aSBarry Smith   } else {
1644fc4dec0aSBarry Smith     B = *matout;
1645fc4dec0aSBarry Smith   }
16460ac07820SSatish Balay 
16470ac07820SSatish Balay   /* copy over the A part */
16480ac07820SSatish Balay   Aloc = (Mat_SeqBAIJ*)baij->A->data;
16490ac07820SSatish Balay   ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1650b24ad042SBarry Smith   ierr = PetscMalloc(bs*sizeof(PetscInt),&rvals);CHKERRQ(ierr);
16510ac07820SSatish Balay 
16520ac07820SSatish Balay   for (i=0; i<mbs; i++) {
1653899cda47SBarry Smith     rvals[0] = bs*(baij->rstartbs + i);
165426fbe8dcSKarl Rupp     for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
16550ac07820SSatish Balay     for (j=ai[i]; j<ai[i+1]; j++) {
1656899cda47SBarry Smith       col = (baij->cstartbs+aj[j])*bs;
16570ac07820SSatish Balay       for (k=0; k<bs; k++) {
165897e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);CHKERRQ(ierr);
165926fbe8dcSKarl Rupp 
16600ac07820SSatish Balay         col++; a += bs;
16610ac07820SSatish Balay       }
16620ac07820SSatish Balay     }
16630ac07820SSatish Balay   }
16640ac07820SSatish Balay   /* copy over the B part */
16650ac07820SSatish Balay   Aloc = (Mat_SeqBAIJ*)baij->B->data;
16660ac07820SSatish Balay   ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
16670ac07820SSatish Balay   for (i=0; i<mbs; i++) {
1668899cda47SBarry Smith     rvals[0] = bs*(baij->rstartbs + i);
166926fbe8dcSKarl Rupp     for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
16700ac07820SSatish Balay     for (j=ai[i]; j<ai[i+1]; j++) {
16710ac07820SSatish Balay       col = baij->garray[aj[j]]*bs;
16720ac07820SSatish Balay       for (k=0; k<bs; k++) {
167397e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);CHKERRQ(ierr);
167426fbe8dcSKarl Rupp         col++;
167526fbe8dcSKarl Rupp         a += bs;
16760ac07820SSatish Balay       }
16770ac07820SSatish Balay     }
16780ac07820SSatish Balay   }
1679606d414cSSatish Balay   ierr = PetscFree(rvals);CHKERRQ(ierr);
16800ac07820SSatish Balay   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16810ac07820SSatish Balay   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16820ac07820SSatish Balay 
168326fbe8dcSKarl Rupp   if (reuse == MAT_INITIAL_MATRIX || *matout != A) *matout = B;
168426fbe8dcSKarl Rupp   else {
1685eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,B);CHKERRQ(ierr);
16860ac07820SSatish Balay   }
16873a40ed3dSBarry Smith   PetscFunctionReturn(0);
16880ac07820SSatish Balay }
16890e95ebc0SSatish Balay 
16904a2ae208SSatish Balay #undef __FUNCT__
16914a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIBAIJ"
1692dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIBAIJ(Mat mat,Vec ll,Vec rr)
16930e95ebc0SSatish Balay {
169436c4a09eSSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
169536c4a09eSSatish Balay   Mat            a     = baij->A,b = baij->B;
1696dfbe8321SBarry Smith   PetscErrorCode ierr;
1697b24ad042SBarry Smith   PetscInt       s1,s2,s3;
16980e95ebc0SSatish Balay 
1699d64ed03dSBarry Smith   PetscFunctionBegin;
170036c4a09eSSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
170136c4a09eSSatish Balay   if (rr) {
170236c4a09eSSatish Balay     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
1703e32f2f54SBarry Smith     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
170436c4a09eSSatish Balay     /* Overlap communication with computation. */
1705ca9f406cSSatish Balay     ierr = VecScatterBegin(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
170636c4a09eSSatish Balay   }
17070e95ebc0SSatish Balay   if (ll) {
17080e95ebc0SSatish Balay     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
1709e32f2f54SBarry Smith     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
17100298fd71SBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,NULL);CHKERRQ(ierr);
17110e95ebc0SSatish Balay   }
171236c4a09eSSatish Balay   /* scale  the diagonal block */
171336c4a09eSSatish Balay   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
171436c4a09eSSatish Balay 
171536c4a09eSSatish Balay   if (rr) {
171636c4a09eSSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
1717ca9f406cSSatish Balay     ierr = VecScatterEnd(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
17180298fd71SBarry Smith     ierr = (*b->ops->diagonalscale)(b,NULL,baij->lvec);CHKERRQ(ierr);
171936c4a09eSSatish Balay   }
17203a40ed3dSBarry Smith   PetscFunctionReturn(0);
17210e95ebc0SSatish Balay }
17220e95ebc0SSatish Balay 
17234a2ae208SSatish Balay #undef __FUNCT__
17244a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIBAIJ"
17252b40b63fSBarry Smith PetscErrorCode MatZeroRows_MPIBAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
17260ac07820SSatish Balay {
17270ac07820SSatish Balay   Mat_MPIBAIJ       *l = (Mat_MPIBAIJ*)A->data;
17286849ba73SBarry Smith   PetscErrorCode    ierr;
1729b24ad042SBarry Smith   PetscMPIInt       imdex,size = l->size,n,rank = l->rank;
1730d0f46423SBarry Smith   PetscInt          i,*owners = A->rmap->range;
1731b24ad042SBarry Smith   PetscInt          *nprocs,j,idx,nsends,row;
1732b24ad042SBarry Smith   PetscInt          nmax,*svalues,*starts,*owner,nrecvs;
17337adad957SLisandro Dalcin   PetscInt          *rvalues,tag = ((PetscObject)A)->tag,count,base,slen,*source,lastidx = -1;
1734d0f46423SBarry Smith   PetscInt          *lens,*lrows,*values,rstart_bs=A->rmap->rstart;
1735ce94432eSBarry Smith   MPI_Comm          comm;
17360ac07820SSatish Balay   MPI_Request       *send_waits,*recv_waits;
17370ac07820SSatish Balay   MPI_Status        recv_status,*send_status;
173897b48c8fSBarry Smith   const PetscScalar *xx;
173997b48c8fSBarry Smith   PetscScalar       *bb;
17406543fbbaSBarry Smith #if defined(PETSC_DEBUG)
1741ace3abfcSBarry Smith   PetscBool         found = PETSC_FALSE;
17426543fbbaSBarry Smith #endif
17430ac07820SSatish Balay 
1744d64ed03dSBarry Smith   PetscFunctionBegin;
1745ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr);
17460ac07820SSatish Balay   /*  first count number of contributors to each processor */
1747b24ad042SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
1748b24ad042SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
1749b24ad042SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr);  /* see note*/
17506543fbbaSBarry Smith   j    = 0;
17510ac07820SSatish Balay   for (i=0; i<N; i++) {
17526543fbbaSBarry Smith     if (lastidx > (idx = rows[i])) j = 0;
17536543fbbaSBarry Smith     lastidx = idx;
17546543fbbaSBarry Smith     for (; j<size; j++) {
1755357c27ecSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
17566543fbbaSBarry Smith         nprocs[2*j]++;
17576543fbbaSBarry Smith         nprocs[2*j+1] = 1;
17586543fbbaSBarry Smith         owner[i]      = j;
17596543fbbaSBarry Smith #if defined(PETSC_DEBUG)
17606543fbbaSBarry Smith         found = PETSC_TRUE;
17616543fbbaSBarry Smith #endif
17626543fbbaSBarry Smith         break;
17630ac07820SSatish Balay       }
17640ac07820SSatish Balay     }
17656543fbbaSBarry Smith #if defined(PETSC_DEBUG)
1766e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
17676543fbbaSBarry Smith     found = PETSC_FALSE;
17686543fbbaSBarry Smith #endif
17690ac07820SSatish Balay   }
177026fbe8dcSKarl Rupp   nsends = 0;  for (i=0; i<size; i++) nsends += nprocs[2*i+1];
17710ac07820SSatish Balay 
17727367270fSBarry Smith   if (A->nooffproczerorows) {
17737367270fSBarry Smith     if (nsends > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"You called MatSetOption(,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) but set an off process zero row");
17747367270fSBarry Smith     nrecvs = nsends;
17757367270fSBarry Smith     nmax   = N;
17767367270fSBarry Smith   } else {
17770ac07820SSatish Balay     /* inform other processors of number of messages and max length*/
1778c1dc657dSBarry Smith     ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
17797367270fSBarry Smith   }
17800ac07820SSatish Balay 
17810ac07820SSatish Balay   /* post receives:   */
1782b24ad042SBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr);
1783b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
17840ac07820SSatish Balay   for (i=0; i<nrecvs; i++) {
1785b24ad042SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
17860ac07820SSatish Balay   }
17870ac07820SSatish Balay 
17880ac07820SSatish Balay   /* do sends:
17890ac07820SSatish Balay      1) starts[i] gives the starting index in svalues for stuff going to
17900ac07820SSatish Balay      the ith processor
17910ac07820SSatish Balay   */
1792b24ad042SBarry Smith   ierr      = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr);
1793b0a32e0cSBarry Smith   ierr      = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
1794b24ad042SBarry Smith   ierr      = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
17950ac07820SSatish Balay   starts[0] = 0;
179626fbe8dcSKarl Rupp   for (i=1; i<size; i++) starts[i] = starts[i-1] + nprocs[2*i-2];
17970ac07820SSatish Balay   for (i=0; i<N; i++) {
17980ac07820SSatish Balay     svalues[starts[owner[i]]++] = rows[i];
17990ac07820SSatish Balay   }
18000ac07820SSatish Balay 
18010ac07820SSatish Balay   starts[0] = 0;
180226fbe8dcSKarl Rupp   for (i=1; i<size+1; i++) starts[i] = starts[i-1] + nprocs[2*i-2];
18030ac07820SSatish Balay   count = 0;
18040ac07820SSatish Balay   for (i=0; i<size; i++) {
1805c1dc657dSBarry Smith     if (nprocs[2*i+1]) {
1806b24ad042SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
18070ac07820SSatish Balay     }
18080ac07820SSatish Balay   }
1809606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
18100ac07820SSatish Balay 
1811357c27ecSBarry Smith   base = owners[rank];
18120ac07820SSatish Balay 
18130ac07820SSatish Balay   /*  wait on receives */
1814fca92195SBarry Smith   ierr  = PetscMalloc2(nrecvs+1,PetscInt,&lens,nrecvs+1,PetscInt,&source);CHKERRQ(ierr);
1815fca92195SBarry Smith   count = nrecvs;
1816fca92195SBarry Smith   slen  = 0;
18170ac07820SSatish Balay   while (count) {
1818ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
18190ac07820SSatish Balay     /* unpack receives into our local space */
1820b24ad042SBarry Smith     ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr);
182126fbe8dcSKarl Rupp 
18220ac07820SSatish Balay     source[imdex] = recv_status.MPI_SOURCE;
18230ac07820SSatish Balay     lens[imdex]   = n;
18240ac07820SSatish Balay     slen         += n;
18250ac07820SSatish Balay     count--;
18260ac07820SSatish Balay   }
1827606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
18280ac07820SSatish Balay 
18290ac07820SSatish Balay   /* move the data into the send scatter */
1830b24ad042SBarry Smith   ierr  = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr);
18310ac07820SSatish Balay   count = 0;
18320ac07820SSatish Balay   for (i=0; i<nrecvs; i++) {
18330ac07820SSatish Balay     values = rvalues + i*nmax;
18340ac07820SSatish Balay     for (j=0; j<lens[i]; j++) {
18350ac07820SSatish Balay       lrows[count++] = values[j] - base;
18360ac07820SSatish Balay     }
18370ac07820SSatish Balay   }
1838606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
1839fca92195SBarry Smith   ierr = PetscFree2(lens,source);CHKERRQ(ierr);
1840606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
1841606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
18420ac07820SSatish Balay 
184397b48c8fSBarry Smith   /* fix right hand side if needed */
184497b48c8fSBarry Smith   if (x && b) {
184597b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
184697b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
184797b48c8fSBarry Smith     for (i=0; i<slen; i++) {
184897b48c8fSBarry Smith       bb[lrows[i]] = diag*xx[lrows[i]];
184997b48c8fSBarry Smith     }
185097b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
185197b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
185297b48c8fSBarry Smith   }
185397b48c8fSBarry Smith 
18540ac07820SSatish Balay   /* actually zap the local rows */
185572dacd9aSBarry Smith   /*
185672dacd9aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
1857a8c7a070SBarry Smith      is square and the user wishes to set the diagonal we use separate
185872dacd9aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
185972dacd9aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
186072dacd9aSBarry Smith 
186172dacd9aSBarry Smith   */
18629c957beeSSatish Balay   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
18632b40b63fSBarry Smith   ierr = MatZeroRows_SeqBAIJ(l->B,slen,lrows,0.0,0,0);CHKERRQ(ierr);
1864d0f46423SBarry Smith   if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) {
18652b40b63fSBarry Smith     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,diag,0,0);CHKERRQ(ierr);
1866f4df32b1SMatthew Knepley   } else if (diag != 0.0) {
18672b40b63fSBarry Smith     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr);
1868e7e72b3dSBarry Smith     if (((Mat_SeqBAIJ*)l->A->data)->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options \n\
1869512a5fc5SBarry Smith        MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
1870a07cd24cSSatish Balay     for (i=0; i<slen; i++) {
1871a07cd24cSSatish Balay       row  = lrows[i] + rstart_bs;
1872f4df32b1SMatthew Knepley       ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
1873a07cd24cSSatish Balay     }
1874a07cd24cSSatish Balay     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1875a07cd24cSSatish Balay     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18769c957beeSSatish Balay   } else {
18772b40b63fSBarry Smith     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr);
1878a07cd24cSSatish Balay   }
18799c957beeSSatish Balay 
1880606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
1881a07cd24cSSatish Balay 
18820ac07820SSatish Balay   /* wait on sends */
18830ac07820SSatish Balay   if (nsends) {
188482502324SSatish Balay     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
1885ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
1886606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
18870ac07820SSatish Balay   }
1888606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
1889606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
18903a40ed3dSBarry Smith   PetscFunctionReturn(0);
18910ac07820SSatish Balay }
189272dacd9aSBarry Smith 
18934a2ae208SSatish Balay #undef __FUNCT__
18944a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIBAIJ"
1895dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIBAIJ(Mat A)
1896bb5a7306SBarry Smith {
1897bb5a7306SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1898dfbe8321SBarry Smith   PetscErrorCode ierr;
1899d64ed03dSBarry Smith 
1900d64ed03dSBarry Smith   PetscFunctionBegin;
1901bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
19023a40ed3dSBarry Smith   PetscFunctionReturn(0);
1903bb5a7306SBarry Smith }
1904bb5a7306SBarry Smith 
19056849ba73SBarry Smith static PetscErrorCode MatDuplicate_MPIBAIJ(Mat,MatDuplicateOption,Mat*);
19060ac07820SSatish Balay 
19074a2ae208SSatish Balay #undef __FUNCT__
19084a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIBAIJ"
1909ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIBAIJ(Mat A,Mat B,PetscBool  *flag)
19107fc3c18eSBarry Smith {
19117fc3c18eSBarry Smith   Mat_MPIBAIJ    *matB = (Mat_MPIBAIJ*)B->data,*matA = (Mat_MPIBAIJ*)A->data;
19127fc3c18eSBarry Smith   Mat            a,b,c,d;
1913ace3abfcSBarry Smith   PetscBool      flg;
1914dfbe8321SBarry Smith   PetscErrorCode ierr;
19157fc3c18eSBarry Smith 
19167fc3c18eSBarry Smith   PetscFunctionBegin;
19177fc3c18eSBarry Smith   a = matA->A; b = matA->B;
19187fc3c18eSBarry Smith   c = matB->A; d = matB->B;
19197fc3c18eSBarry Smith 
19207fc3c18eSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1921abc0a331SBarry Smith   if (flg) {
19227fc3c18eSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
19237fc3c18eSBarry Smith   }
1924ce94432eSBarry Smith   ierr = MPI_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
19257fc3c18eSBarry Smith   PetscFunctionReturn(0);
19267fc3c18eSBarry Smith }
19277fc3c18eSBarry Smith 
19283c896bc6SHong Zhang #undef __FUNCT__
19293c896bc6SHong Zhang #define __FUNCT__ "MatCopy_MPIBAIJ"
19303c896bc6SHong Zhang PetscErrorCode MatCopy_MPIBAIJ(Mat A,Mat B,MatStructure str)
19313c896bc6SHong Zhang {
19323c896bc6SHong Zhang   PetscErrorCode ierr;
19333c896bc6SHong Zhang   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
19343c896bc6SHong Zhang   Mat_MPIBAIJ    *b = (Mat_MPIBAIJ*)B->data;
19353c896bc6SHong Zhang 
19363c896bc6SHong Zhang   PetscFunctionBegin;
19373c896bc6SHong Zhang   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
19383c896bc6SHong Zhang   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
19393c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
19403c896bc6SHong Zhang   } else {
19413c896bc6SHong Zhang     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
19423c896bc6SHong Zhang     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
19433c896bc6SHong Zhang   }
19443c896bc6SHong Zhang   PetscFunctionReturn(0);
19453c896bc6SHong Zhang }
1946273d9f13SBarry Smith 
19474a2ae208SSatish Balay #undef __FUNCT__
19484994cf47SJed Brown #define __FUNCT__ "MatSetUp_MPIBAIJ"
19494994cf47SJed Brown PetscErrorCode MatSetUp_MPIBAIJ(Mat A)
1950273d9f13SBarry Smith {
1951dfbe8321SBarry Smith   PetscErrorCode ierr;
1952273d9f13SBarry Smith 
1953273d9f13SBarry Smith   PetscFunctionBegin;
1954535b19f3SBarry Smith   ierr =  MatMPIBAIJSetPreallocation(A,A->rmap->bs,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1955273d9f13SBarry Smith   PetscFunctionReturn(0);
1956273d9f13SBarry Smith }
1957273d9f13SBarry Smith 
19584fe895cdSHong Zhang #undef __FUNCT__
19594fe895cdSHong Zhang #define __FUNCT__ "MatAXPY_MPIBAIJ"
19604fe895cdSHong Zhang PetscErrorCode MatAXPY_MPIBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
19614fe895cdSHong Zhang {
19624fe895cdSHong Zhang   PetscErrorCode ierr;
19634fe895cdSHong Zhang   Mat_MPIBAIJ    *xx=(Mat_MPIBAIJ*)X->data,*yy=(Mat_MPIBAIJ*)Y->data;
19644fe895cdSHong Zhang   PetscBLASInt   bnz,one=1;
19654fe895cdSHong Zhang   Mat_SeqBAIJ    *x,*y;
19664fe895cdSHong Zhang 
19674fe895cdSHong Zhang   PetscFunctionBegin;
19684fe895cdSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
19694fe895cdSHong Zhang     PetscScalar alpha = a;
19704fe895cdSHong Zhang     x    = (Mat_SeqBAIJ*)xx->A->data;
19714fe895cdSHong Zhang     y    = (Mat_SeqBAIJ*)yy->A->data;
1972c5df96a5SBarry Smith     ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
19738b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
19744fe895cdSHong Zhang     x    = (Mat_SeqBAIJ*)xx->B->data;
19754fe895cdSHong Zhang     y    = (Mat_SeqBAIJ*)yy->B->data;
1976c5df96a5SBarry Smith     ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
19778b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
19784fe895cdSHong Zhang   } else {
19794fe895cdSHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
19804fe895cdSHong Zhang   }
19814fe895cdSHong Zhang   PetscFunctionReturn(0);
19824fe895cdSHong Zhang }
19834fe895cdSHong Zhang 
198499cafbc1SBarry Smith #undef __FUNCT__
198599cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIBAIJ"
198699cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIBAIJ(Mat A)
198799cafbc1SBarry Smith {
198899cafbc1SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
198999cafbc1SBarry Smith   PetscErrorCode ierr;
199099cafbc1SBarry Smith 
199199cafbc1SBarry Smith   PetscFunctionBegin;
199299cafbc1SBarry Smith   ierr = MatRealPart(a->A);CHKERRQ(ierr);
199399cafbc1SBarry Smith   ierr = MatRealPart(a->B);CHKERRQ(ierr);
199499cafbc1SBarry Smith   PetscFunctionReturn(0);
199599cafbc1SBarry Smith }
199699cafbc1SBarry Smith 
199799cafbc1SBarry Smith #undef __FUNCT__
199899cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIBAIJ"
199999cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIBAIJ(Mat A)
200099cafbc1SBarry Smith {
200199cafbc1SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
200299cafbc1SBarry Smith   PetscErrorCode ierr;
200399cafbc1SBarry Smith 
200499cafbc1SBarry Smith   PetscFunctionBegin;
200599cafbc1SBarry Smith   ierr = MatImaginaryPart(a->A);CHKERRQ(ierr);
200699cafbc1SBarry Smith   ierr = MatImaginaryPart(a->B);CHKERRQ(ierr);
200799cafbc1SBarry Smith   PetscFunctionReturn(0);
200899cafbc1SBarry Smith }
200999cafbc1SBarry Smith 
201082094794SBarry Smith #undef __FUNCT__
201182094794SBarry Smith #define __FUNCT__ "MatGetSubMatrix_MPIBAIJ"
20124aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIBAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
20134aa3045dSJed Brown {
20144aa3045dSJed Brown   PetscErrorCode ierr;
20154aa3045dSJed Brown   IS             iscol_local;
20164aa3045dSJed Brown   PetscInt       csize;
20174aa3045dSJed Brown 
20184aa3045dSJed Brown   PetscFunctionBegin;
20194aa3045dSJed Brown   ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr);
2020b79d0421SJed Brown   if (call == MAT_REUSE_MATRIX) {
2021b79d0421SJed Brown     ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr);
2022e32f2f54SBarry Smith     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2023b79d0421SJed Brown   } else {
20244aa3045dSJed Brown     ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr);
2025b79d0421SJed Brown   }
20264aa3045dSJed Brown   ierr = MatGetSubMatrix_MPIBAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr);
2027b79d0421SJed Brown   if (call == MAT_INITIAL_MATRIX) {
2028b79d0421SJed Brown     ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr);
20296bf464f9SBarry Smith     ierr = ISDestroy(&iscol_local);CHKERRQ(ierr);
2030b79d0421SJed Brown   }
20314aa3045dSJed Brown   PetscFunctionReturn(0);
20324aa3045dSJed Brown }
203329dcf524SDmitry Karpeev extern PetscErrorCode MatGetSubMatrices_MPIBAIJ_local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool*,PetscBool*,Mat*);
20344aa3045dSJed Brown #undef __FUNCT__
2035dd183c9eSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIBAIJ_Private"
203682094794SBarry Smith /*
203782094794SBarry Smith   Not great since it makes two copies of the submatrix, first an SeqBAIJ
203882094794SBarry Smith   in local and then by concatenating the local matrices the end result.
203982094794SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIBAIJ()
204082094794SBarry Smith */
20414aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIBAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
204282094794SBarry Smith {
204382094794SBarry Smith   PetscErrorCode ierr;
204482094794SBarry Smith   PetscMPIInt    rank,size;
204582094794SBarry Smith   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs;
204629dcf524SDmitry Karpeev   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol,nrow;
204729dcf524SDmitry Karpeev   Mat            M,Mreuse;
204882094794SBarry Smith   MatScalar      *vwork,*aa;
2049ce94432eSBarry Smith   MPI_Comm       comm;
205029dcf524SDmitry Karpeev   IS             isrow_new, iscol_new;
205129dcf524SDmitry Karpeev   PetscBool      idflag,allrows, allcols;
205282094794SBarry Smith   Mat_SeqBAIJ    *aij;
205382094794SBarry Smith 
205482094794SBarry Smith   PetscFunctionBegin;
2055ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr);
205682094794SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
205782094794SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
205829dcf524SDmitry Karpeev   /* The compression and expansion should be avoided. Doesn't point
205929dcf524SDmitry Karpeev      out errors, might change the indices, hence buggey */
206029dcf524SDmitry Karpeev   ierr = ISCompressIndicesGeneral(mat->rmap->N,mat->rmap->n,mat->rmap->bs,1,&isrow,&isrow_new);CHKERRQ(ierr);
206129dcf524SDmitry Karpeev   ierr = ISCompressIndicesGeneral(mat->cmap->N,mat->cmap->n,mat->cmap->bs,1,&iscol,&iscol_new);CHKERRQ(ierr);
206282094794SBarry Smith 
206329dcf524SDmitry Karpeev   /* Check for special case: each processor gets entire matrix columns */
206429dcf524SDmitry Karpeev   ierr = ISIdentity(iscol,&idflag);CHKERRQ(ierr);
206529dcf524SDmitry Karpeev   ierr = ISGetLocalSize(iscol,&ncol);CHKERRQ(ierr);
206626fbe8dcSKarl Rupp   if (idflag && ncol == mat->cmap->N) allcols = PETSC_TRUE;
206726fbe8dcSKarl Rupp   else allcols = PETSC_FALSE;
206829dcf524SDmitry Karpeev 
206929dcf524SDmitry Karpeev   ierr = ISIdentity(isrow,&idflag);CHKERRQ(ierr);
207029dcf524SDmitry Karpeev   ierr = ISGetLocalSize(isrow,&nrow);CHKERRQ(ierr);
207126fbe8dcSKarl Rupp   if (idflag && nrow == mat->rmap->N) allrows = PETSC_TRUE;
207226fbe8dcSKarl Rupp   else allrows = PETSC_FALSE;
207326fbe8dcSKarl Rupp 
207482094794SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
207582094794SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);CHKERRQ(ierr);
2076e32f2f54SBarry Smith     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
207775f6568bSJed Brown     ierr = MatGetSubMatrices_MPIBAIJ_local(mat,1,&isrow_new,&iscol_new,MAT_REUSE_MATRIX,&allrows,&allcols,&Mreuse);CHKERRQ(ierr);
207882094794SBarry Smith   } else {
207975f6568bSJed Brown     ierr = MatGetSubMatrices_MPIBAIJ_local(mat,1,&isrow_new,&iscol_new,MAT_INITIAL_MATRIX,&allrows,&allcols,&Mreuse);CHKERRQ(ierr);
208082094794SBarry Smith   }
208129dcf524SDmitry Karpeev   ierr = ISDestroy(&isrow_new);CHKERRQ(ierr);
208229dcf524SDmitry Karpeev   ierr = ISDestroy(&iscol_new);CHKERRQ(ierr);
208382094794SBarry Smith   /*
208482094794SBarry Smith       m - number of local rows
208582094794SBarry Smith       n - number of columns (same on all processors)
208682094794SBarry Smith       rstart - first row in new global matrix generated
208782094794SBarry Smith   */
208882094794SBarry Smith   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
208982094794SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
209082094794SBarry Smith   m    = m/bs;
209182094794SBarry Smith   n    = n/bs;
209282094794SBarry Smith 
209382094794SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
209482094794SBarry Smith     aij = (Mat_SeqBAIJ*)(Mreuse)->data;
209582094794SBarry Smith     ii  = aij->i;
209682094794SBarry Smith     jj  = aij->j;
209782094794SBarry Smith 
209882094794SBarry Smith     /*
209982094794SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
210082094794SBarry Smith         portions of the matrix in order to do correct preallocation
210182094794SBarry Smith     */
210282094794SBarry Smith 
210382094794SBarry Smith     /* first get start and end of "diagonal" columns */
210482094794SBarry Smith     if (csize == PETSC_DECIDE) {
210582094794SBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
210682094794SBarry Smith       if (mglobal == n*bs) { /* square matrix */
210782094794SBarry Smith         nlocal = m;
210882094794SBarry Smith       } else {
210982094794SBarry Smith         nlocal = n/size + ((n % size) > rank);
211082094794SBarry Smith       }
211182094794SBarry Smith     } else {
211282094794SBarry Smith       nlocal = csize/bs;
211382094794SBarry Smith     }
211482094794SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
211582094794SBarry Smith     rstart = rend - nlocal;
211665e19b50SBarry Smith     if (rank == size - 1 && rend != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,n);
211782094794SBarry Smith 
211882094794SBarry Smith     /* next, compute all the lengths */
2119eb9baa12SBarry Smith     ierr  = PetscMalloc2(m+1,PetscInt,&dlens,m+1,PetscInt,&olens);CHKERRQ(ierr);
212082094794SBarry Smith     for (i=0; i<m; i++) {
212182094794SBarry Smith       jend = ii[i+1] - ii[i];
212282094794SBarry Smith       olen = 0;
212382094794SBarry Smith       dlen = 0;
212482094794SBarry Smith       for (j=0; j<jend; j++) {
212582094794SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
212682094794SBarry Smith         else dlen++;
212782094794SBarry Smith         jj++;
212882094794SBarry Smith       }
212982094794SBarry Smith       olens[i] = olen;
213082094794SBarry Smith       dlens[i] = dlen;
213182094794SBarry Smith     }
213282094794SBarry Smith     ierr = MatCreate(comm,&M);CHKERRQ(ierr);
213382094794SBarry Smith     ierr = MatSetSizes(M,bs*m,bs*nlocal,PETSC_DECIDE,bs*n);CHKERRQ(ierr);
213482094794SBarry Smith     ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr);
213582094794SBarry Smith     ierr = MatMPIBAIJSetPreallocation(M,bs,0,dlens,0,olens);CHKERRQ(ierr);
2136eb9baa12SBarry Smith     ierr = PetscFree2(dlens,olens);CHKERRQ(ierr);
213782094794SBarry Smith   } else {
213882094794SBarry Smith     PetscInt ml,nl;
213982094794SBarry Smith 
214082094794SBarry Smith     M    = *newmat;
214182094794SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
2142e32f2f54SBarry Smith     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
214382094794SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
214482094794SBarry Smith     /*
214582094794SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
214682094794SBarry Smith        rather than the slower MatSetValues().
214782094794SBarry Smith     */
214882094794SBarry Smith     M->was_assembled = PETSC_TRUE;
214982094794SBarry Smith     M->assembled     = PETSC_FALSE;
215082094794SBarry Smith   }
215182094794SBarry Smith   ierr = MatSetOption(M,MAT_ROW_ORIENTED,PETSC_FALSE);CHKERRQ(ierr);
215282094794SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
215382094794SBarry Smith   aij  = (Mat_SeqBAIJ*)(Mreuse)->data;
215482094794SBarry Smith   ii   = aij->i;
215582094794SBarry Smith   jj   = aij->j;
215682094794SBarry Smith   aa   = aij->a;
215782094794SBarry Smith   for (i=0; i<m; i++) {
215882094794SBarry Smith     row   = rstart/bs + i;
215982094794SBarry Smith     nz    = ii[i+1] - ii[i];
216082094794SBarry Smith     cwork = jj;     jj += nz;
216175f6568bSJed Brown     vwork = aa;     aa += nz*bs*bs;
216282094794SBarry Smith     ierr  = MatSetValuesBlocked_MPIBAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
216382094794SBarry Smith   }
216482094794SBarry Smith 
216582094794SBarry Smith   ierr    = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
216682094794SBarry Smith   ierr    = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
216782094794SBarry Smith   *newmat = M;
216882094794SBarry Smith 
216982094794SBarry Smith   /* save submatrix used in processor for next request */
217082094794SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
217182094794SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
217282094794SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
217382094794SBarry Smith   }
217482094794SBarry Smith   PetscFunctionReturn(0);
217582094794SBarry Smith }
217682094794SBarry Smith 
217782094794SBarry Smith #undef __FUNCT__
217882094794SBarry Smith #define __FUNCT__ "MatPermute_MPIBAIJ"
217982094794SBarry Smith PetscErrorCode MatPermute_MPIBAIJ(Mat A,IS rowp,IS colp,Mat *B)
218082094794SBarry Smith {
218182094794SBarry Smith   MPI_Comm       comm,pcomm;
218275f6568bSJed Brown   PetscInt       first,rlocal_size,clocal_size,nrows;
218382094794SBarry Smith   const PetscInt *rows;
2184dbf0e21dSBarry Smith   PetscMPIInt    size;
218575f6568bSJed Brown   IS             crowp,growp,irowp,lrowp,lcolp;
218682094794SBarry Smith   PetscErrorCode ierr;
218782094794SBarry Smith 
218882094794SBarry Smith   PetscFunctionBegin;
218982094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr);
219082094794SBarry Smith   /* make a collective version of 'rowp' */
219182094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)rowp,&pcomm);CHKERRQ(ierr);
219282094794SBarry Smith   if (pcomm==comm) {
219382094794SBarry Smith     crowp = rowp;
219482094794SBarry Smith   } else {
219582094794SBarry Smith     ierr = ISGetSize(rowp,&nrows);CHKERRQ(ierr);
219682094794SBarry Smith     ierr = ISGetIndices(rowp,&rows);CHKERRQ(ierr);
219770b3c8c7SBarry Smith     ierr = ISCreateGeneral(comm,nrows,rows,PETSC_COPY_VALUES,&crowp);CHKERRQ(ierr);
219882094794SBarry Smith     ierr = ISRestoreIndices(rowp,&rows);CHKERRQ(ierr);
219982094794SBarry Smith   }
220082094794SBarry Smith   /* collect the global row permutation and invert it */
220182094794SBarry Smith   ierr = ISAllGather(crowp,&growp);CHKERRQ(ierr);
220282094794SBarry Smith   ierr = ISSetPermutation(growp);CHKERRQ(ierr);
220382094794SBarry Smith   if (pcomm!=comm) {
22046bf464f9SBarry Smith     ierr = ISDestroy(&crowp);CHKERRQ(ierr);
220582094794SBarry Smith   }
220682094794SBarry Smith   ierr = ISInvertPermutation(growp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
220775f6568bSJed Brown   ierr = ISDestroy(&growp);CHKERRQ(ierr);
220882094794SBarry Smith   /* get the local target indices */
22090298fd71SBarry Smith   ierr = MatGetOwnershipRange(A,&first,NULL);CHKERRQ(ierr);
221075f6568bSJed Brown   ierr = MatGetLocalSize(A,&rlocal_size,&clocal_size);CHKERRQ(ierr);
221182094794SBarry Smith   ierr = ISGetIndices(irowp,&rows);CHKERRQ(ierr);
221275f6568bSJed Brown   ierr = ISCreateGeneral(MPI_COMM_SELF,rlocal_size,rows+first,PETSC_COPY_VALUES,&lrowp);CHKERRQ(ierr);
221382094794SBarry Smith   ierr = ISRestoreIndices(irowp,&rows);CHKERRQ(ierr);
22146bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
221582094794SBarry Smith   /* the column permutation is so much easier;
221682094794SBarry Smith      make a local version of 'colp' and invert it */
221782094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)colp,&pcomm);CHKERRQ(ierr);
2218dbf0e21dSBarry Smith   ierr = MPI_Comm_size(pcomm,&size);CHKERRQ(ierr);
2219dbf0e21dSBarry Smith   if (size==1) {
222082094794SBarry Smith     lcolp = colp;
222182094794SBarry Smith   } else {
222275f6568bSJed Brown     ierr = ISAllGather(colp,&lcolp);CHKERRQ(ierr);
222382094794SBarry Smith   }
2224dbf0e21dSBarry Smith   ierr = ISSetPermutation(lcolp);CHKERRQ(ierr);
222575f6568bSJed Brown   /* now we just get the submatrix */
222675f6568bSJed Brown   ierr = MatGetSubMatrix_MPIBAIJ_Private(A,lrowp,lcolp,clocal_size,MAT_INITIAL_MATRIX,B);CHKERRQ(ierr);
2227dbf0e21dSBarry Smith   if (size>1) {
22286bf464f9SBarry Smith     ierr = ISDestroy(&lcolp);CHKERRQ(ierr);
222982094794SBarry Smith   }
223082094794SBarry Smith   /* clean up */
22316bf464f9SBarry Smith   ierr = ISDestroy(&lrowp);CHKERRQ(ierr);
223282094794SBarry Smith   PetscFunctionReturn(0);
223382094794SBarry Smith }
223482094794SBarry Smith 
22358c7482ecSBarry Smith #undef __FUNCT__
22368c7482ecSBarry Smith #define __FUNCT__ "MatGetGhosts_MPIBAIJ"
22377087cfbeSBarry Smith PetscErrorCode  MatGetGhosts_MPIBAIJ(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
22388c7482ecSBarry Smith {
22398c7482ecSBarry Smith   Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*) mat->data;
22408c7482ecSBarry Smith   Mat_SeqBAIJ *B    = (Mat_SeqBAIJ*)baij->B->data;
22418c7482ecSBarry Smith 
22428c7482ecSBarry Smith   PetscFunctionBegin;
224326fbe8dcSKarl Rupp   if (nghosts) *nghosts = B->nbs;
224426fbe8dcSKarl Rupp   if (ghosts) *ghosts = baij->garray;
22458c7482ecSBarry Smith   PetscFunctionReturn(0);
22468c7482ecSBarry Smith }
22478c7482ecSBarry Smith 
2248f6d58c54SBarry Smith #undef __FUNCT__
2249d1adec66SJed Brown #define __FUNCT__ "MatGetSeqNonzeroStructure_MPIBAIJ"
2250d1adec66SJed Brown PetscErrorCode MatGetSeqNonzeroStructure_MPIBAIJ(Mat A,Mat *newmat)
2251f6d58c54SBarry Smith {
2252f6d58c54SBarry Smith   Mat            B;
2253f6d58c54SBarry Smith   Mat_MPIBAIJ    *a  = (Mat_MPIBAIJ*)A->data;
2254f6d58c54SBarry Smith   Mat_SeqBAIJ    *ad = (Mat_SeqBAIJ*)a->A->data,*bd = (Mat_SeqBAIJ*)a->B->data;
2255f6d58c54SBarry Smith   Mat_SeqAIJ     *b;
2256f6d58c54SBarry Smith   PetscErrorCode ierr;
2257f6d58c54SBarry Smith   PetscMPIInt    size,rank,*recvcounts = 0,*displs = 0;
2258f6d58c54SBarry Smith   PetscInt       sendcount,i,*rstarts = A->rmap->range,n,cnt,j,bs = A->rmap->bs;
2259f6d58c54SBarry Smith   PetscInt       m,*garray = a->garray,*lens,*jsendbuf,*a_jsendbuf,*b_jsendbuf;
2260f6d58c54SBarry Smith 
2261f6d58c54SBarry Smith   PetscFunctionBegin;
2262ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);CHKERRQ(ierr);
2263ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);CHKERRQ(ierr);
2264f6d58c54SBarry Smith 
2265f6d58c54SBarry Smith   /* ----------------------------------------------------------------
2266f6d58c54SBarry Smith      Tell every processor the number of nonzeros per row
2267f6d58c54SBarry Smith   */
2268f6d58c54SBarry Smith   ierr = PetscMalloc((A->rmap->N/bs)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
2269f6d58c54SBarry Smith   for (i=A->rmap->rstart/bs; i<A->rmap->rend/bs; i++) {
2270f6d58c54SBarry 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];
2271f6d58c54SBarry Smith   }
2272f6d58c54SBarry Smith   sendcount = A->rmap->rend/bs - A->rmap->rstart/bs;
2273f6d58c54SBarry Smith   ierr      = PetscMalloc(2*size*sizeof(PetscMPIInt),&recvcounts);CHKERRQ(ierr);
2274f6d58c54SBarry Smith   displs    = recvcounts + size;
2275f6d58c54SBarry Smith   for (i=0; i<size; i++) {
2276f6d58c54SBarry Smith     recvcounts[i] = A->rmap->range[i+1]/bs - A->rmap->range[i]/bs;
2277f6d58c54SBarry Smith     displs[i]     = A->rmap->range[i]/bs;
2278f6d58c54SBarry Smith   }
2279f6d58c54SBarry Smith #if defined(PETSC_HAVE_MPI_IN_PLACE)
2280ce94432eSBarry Smith   ierr = MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,lens,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
2281f6d58c54SBarry Smith #else
2282ce94432eSBarry Smith   ierr = MPI_Allgatherv(lens+A->rmap->rstart/bs,sendcount,MPIU_INT,lens,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
2283f6d58c54SBarry Smith #endif
2284f6d58c54SBarry Smith   /* ---------------------------------------------------------------
2285f6d58c54SBarry Smith      Create the sequential matrix of the same type as the local block diagonal
2286f6d58c54SBarry Smith   */
2287f6d58c54SBarry Smith   ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr);
2288f6d58c54SBarry Smith   ierr = MatSetSizes(B,A->rmap->N/bs,A->cmap->N/bs,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
2289f6d58c54SBarry Smith   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
2290f6d58c54SBarry Smith   ierr = MatSeqAIJSetPreallocation(B,0,lens);CHKERRQ(ierr);
2291f6d58c54SBarry Smith   b    = (Mat_SeqAIJ*)B->data;
2292f6d58c54SBarry Smith 
2293f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2294f6d58c54SBarry Smith     Copy my part of matrix column indices over
2295f6d58c54SBarry Smith   */
2296f6d58c54SBarry Smith   sendcount  = ad->nz + bd->nz;
2297f6d58c54SBarry Smith   jsendbuf   = b->j + b->i[rstarts[rank]/bs];
2298f6d58c54SBarry Smith   a_jsendbuf = ad->j;
2299f6d58c54SBarry Smith   b_jsendbuf = bd->j;
2300f6d58c54SBarry Smith   n          = A->rmap->rend/bs - A->rmap->rstart/bs;
2301f6d58c54SBarry Smith   cnt        = 0;
2302f6d58c54SBarry Smith   for (i=0; i<n; i++) {
2303f6d58c54SBarry Smith 
2304f6d58c54SBarry Smith     /* put in lower diagonal portion */
2305f6d58c54SBarry Smith     m = bd->i[i+1] - bd->i[i];
2306f6d58c54SBarry Smith     while (m > 0) {
2307f6d58c54SBarry Smith       /* is it above diagonal (in bd (compressed) numbering) */
2308f6d58c54SBarry Smith       if (garray[*b_jsendbuf] > A->rmap->rstart/bs + i) break;
2309f6d58c54SBarry Smith       jsendbuf[cnt++] = garray[*b_jsendbuf++];
2310f6d58c54SBarry Smith       m--;
2311f6d58c54SBarry Smith     }
2312f6d58c54SBarry Smith 
2313f6d58c54SBarry Smith     /* put in diagonal portion */
2314f6d58c54SBarry Smith     for (j=ad->i[i]; j<ad->i[i+1]; j++) {
2315f6d58c54SBarry Smith       jsendbuf[cnt++] = A->rmap->rstart/bs + *a_jsendbuf++;
2316f6d58c54SBarry Smith     }
2317f6d58c54SBarry Smith 
2318f6d58c54SBarry Smith     /* put in upper diagonal portion */
2319f6d58c54SBarry Smith     while (m-- > 0) {
2320f6d58c54SBarry Smith       jsendbuf[cnt++] = garray[*b_jsendbuf++];
2321f6d58c54SBarry Smith     }
2322f6d58c54SBarry Smith   }
2323e32f2f54SBarry Smith   if (cnt != sendcount) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupted PETSc matrix: nz given %D actual nz %D",sendcount,cnt);
2324f6d58c54SBarry Smith 
2325f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2326f6d58c54SBarry Smith     Gather all column indices to all processors
2327f6d58c54SBarry Smith   */
2328f6d58c54SBarry Smith   for (i=0; i<size; i++) {
2329f6d58c54SBarry Smith     recvcounts[i] = 0;
2330f6d58c54SBarry Smith     for (j=A->rmap->range[i]/bs; j<A->rmap->range[i+1]/bs; j++) {
2331f6d58c54SBarry Smith       recvcounts[i] += lens[j];
2332f6d58c54SBarry Smith     }
2333f6d58c54SBarry Smith   }
2334f6d58c54SBarry Smith   displs[0] = 0;
2335f6d58c54SBarry Smith   for (i=1; i<size; i++) {
2336f6d58c54SBarry Smith     displs[i] = displs[i-1] + recvcounts[i-1];
2337f6d58c54SBarry Smith   }
2338f6d58c54SBarry Smith #if defined(PETSC_HAVE_MPI_IN_PLACE)
2339ce94432eSBarry Smith   ierr = MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,b->j,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
2340f6d58c54SBarry Smith #else
2341ce94432eSBarry Smith   ierr = MPI_Allgatherv(jsendbuf,sendcount,MPIU_INT,b->j,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
2342f6d58c54SBarry Smith #endif
2343f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2344f6d58c54SBarry Smith     Assemble the matrix into useable form (note numerical values not yet set)
2345f6d58c54SBarry Smith   */
2346f6d58c54SBarry Smith   /* set the b->ilen (length of each row) values */
2347f6d58c54SBarry Smith   ierr = PetscMemcpy(b->ilen,lens,(A->rmap->N/bs)*sizeof(PetscInt));CHKERRQ(ierr);
2348f6d58c54SBarry Smith   /* set the b->i indices */
2349f6d58c54SBarry Smith   b->i[0] = 0;
2350f6d58c54SBarry Smith   for (i=1; i<=A->rmap->N/bs; i++) {
2351f6d58c54SBarry Smith     b->i[i] = b->i[i-1] + lens[i-1];
2352f6d58c54SBarry Smith   }
2353f6d58c54SBarry Smith   ierr = PetscFree(lens);CHKERRQ(ierr);
2354f6d58c54SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2355f6d58c54SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2356f6d58c54SBarry Smith   ierr = PetscFree(recvcounts);CHKERRQ(ierr);
2357f6d58c54SBarry Smith 
2358f6d58c54SBarry Smith   if (A->symmetric) {
2359f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
2360f6d58c54SBarry Smith   } else if (A->hermitian) {
2361f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
2362f6d58c54SBarry Smith   } else if (A->structurally_symmetric) {
2363f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
2364f6d58c54SBarry Smith   }
2365f6d58c54SBarry Smith   *newmat = B;
2366f6d58c54SBarry Smith   PetscFunctionReturn(0);
2367f6d58c54SBarry Smith }
2368f6d58c54SBarry Smith 
2369b1a666ecSBarry Smith #undef __FUNCT__
2370b1a666ecSBarry Smith #define __FUNCT__ "MatSOR_MPIBAIJ"
2371b1a666ecSBarry Smith PetscErrorCode MatSOR_MPIBAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
2372b1a666ecSBarry Smith {
2373b1a666ecSBarry Smith   Mat_MPIBAIJ    *mat = (Mat_MPIBAIJ*)matin->data;
2374b1a666ecSBarry Smith   PetscErrorCode ierr;
2375b1a666ecSBarry Smith   Vec            bb1 = 0;
2376b1a666ecSBarry Smith 
2377b1a666ecSBarry Smith   PetscFunctionBegin;
2378b1a666ecSBarry Smith   if (flag == SOR_APPLY_UPPER) {
2379b1a666ecSBarry Smith     ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2380b1a666ecSBarry Smith     PetscFunctionReturn(0);
2381b1a666ecSBarry Smith   }
2382b1a666ecSBarry Smith 
23834e980039SJed Brown   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS) {
23844e980039SJed Brown     ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
23854e980039SJed Brown   }
23864e980039SJed Brown 
2387b1a666ecSBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
2388b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2389b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2390b1a666ecSBarry Smith       its--;
2391b1a666ecSBarry Smith     }
2392b1a666ecSBarry Smith 
2393b1a666ecSBarry Smith     while (its--) {
2394b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2395b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2396b1a666ecSBarry Smith 
2397b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2398b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2399b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2400b1a666ecSBarry Smith 
2401b1a666ecSBarry Smith       /* local sweep */
2402b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2403b1a666ecSBarry Smith     }
2404b1a666ecSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
2405b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2406b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2407b1a666ecSBarry Smith       its--;
2408b1a666ecSBarry Smith     }
2409b1a666ecSBarry Smith     while (its--) {
2410b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2411b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2412b1a666ecSBarry Smith 
2413b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2414b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2415b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2416b1a666ecSBarry Smith 
2417b1a666ecSBarry Smith       /* local sweep */
2418b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2419b1a666ecSBarry Smith     }
2420b1a666ecSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
2421b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2422b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2423b1a666ecSBarry Smith       its--;
2424b1a666ecSBarry Smith     }
2425b1a666ecSBarry Smith     while (its--) {
2426b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2427b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2428b1a666ecSBarry Smith 
2429b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2430b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2431b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2432b1a666ecSBarry Smith 
2433b1a666ecSBarry Smith       /* local sweep */
2434b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2435b1a666ecSBarry Smith     }
2436ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)matin),PETSC_ERR_SUP,"Parallel version of SOR requested not supported");
2437b1a666ecSBarry Smith 
24386bf464f9SBarry Smith   ierr = VecDestroy(&bb1);CHKERRQ(ierr);
2439b1a666ecSBarry Smith   PetscFunctionReturn(0);
2440b1a666ecSBarry Smith }
2441b1a666ecSBarry Smith 
2442bbead8a2SBarry Smith #undef __FUNCT__
2443bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_MPIBAIJ"
2444713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_MPIBAIJ(Mat A,const PetscScalar **values)
2445bbead8a2SBarry Smith {
2446bbead8a2SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*) A->data;
2447bbead8a2SBarry Smith   PetscErrorCode ierr;
2448bbead8a2SBarry Smith 
2449bbead8a2SBarry Smith   PetscFunctionBegin;
2450bbead8a2SBarry Smith   ierr = MatInvertBlockDiagonal(a->A,values);CHKERRQ(ierr);
2451bbead8a2SBarry Smith   PetscFunctionReturn(0);
2452bbead8a2SBarry Smith }
2453bbead8a2SBarry Smith 
24548c7482ecSBarry Smith 
245579bdfe76SSatish Balay /* -------------------------------------------------------------------*/
24563964eb88SJed Brown static struct _MatOps MatOps_Values = {MatSetValues_MPIBAIJ,
2457cc2dc46cSBarry Smith                                        MatGetRow_MPIBAIJ,
2458cc2dc46cSBarry Smith                                        MatRestoreRow_MPIBAIJ,
2459cc2dc46cSBarry Smith                                        MatMult_MPIBAIJ,
246097304618SKris Buschelman                                 /* 4*/ MatMultAdd_MPIBAIJ,
24617c922b88SBarry Smith                                        MatMultTranspose_MPIBAIJ,
24627c922b88SBarry Smith                                        MatMultTransposeAdd_MPIBAIJ,
2463cc2dc46cSBarry Smith                                        0,
2464cc2dc46cSBarry Smith                                        0,
2465cc2dc46cSBarry Smith                                        0,
246697304618SKris Buschelman                                 /*10*/ 0,
2467cc2dc46cSBarry Smith                                        0,
2468cc2dc46cSBarry Smith                                        0,
2469b1a666ecSBarry Smith                                        MatSOR_MPIBAIJ,
2470cc2dc46cSBarry Smith                                        MatTranspose_MPIBAIJ,
247197304618SKris Buschelman                                 /*15*/ MatGetInfo_MPIBAIJ,
24727fc3c18eSBarry Smith                                        MatEqual_MPIBAIJ,
2473cc2dc46cSBarry Smith                                        MatGetDiagonal_MPIBAIJ,
2474cc2dc46cSBarry Smith                                        MatDiagonalScale_MPIBAIJ,
2475cc2dc46cSBarry Smith                                        MatNorm_MPIBAIJ,
247697304618SKris Buschelman                                 /*20*/ MatAssemblyBegin_MPIBAIJ,
2477cc2dc46cSBarry Smith                                        MatAssemblyEnd_MPIBAIJ,
2478cc2dc46cSBarry Smith                                        MatSetOption_MPIBAIJ,
2479cc2dc46cSBarry Smith                                        MatZeroEntries_MPIBAIJ,
2480d519adbfSMatthew Knepley                                 /*24*/ MatZeroRows_MPIBAIJ,
2481cc2dc46cSBarry Smith                                        0,
2482cc2dc46cSBarry Smith                                        0,
2483cc2dc46cSBarry Smith                                        0,
2484cc2dc46cSBarry Smith                                        0,
24854994cf47SJed Brown                                 /*29*/ MatSetUp_MPIBAIJ,
2486273d9f13SBarry Smith                                        0,
2487cc2dc46cSBarry Smith                                        0,
2488cc2dc46cSBarry Smith                                        0,
2489cc2dc46cSBarry Smith                                        0,
2490d519adbfSMatthew Knepley                                 /*34*/ MatDuplicate_MPIBAIJ,
2491cc2dc46cSBarry Smith                                        0,
2492cc2dc46cSBarry Smith                                        0,
2493cc2dc46cSBarry Smith                                        0,
2494cc2dc46cSBarry Smith                                        0,
2495d519adbfSMatthew Knepley                                 /*39*/ MatAXPY_MPIBAIJ,
2496cc2dc46cSBarry Smith                                        MatGetSubMatrices_MPIBAIJ,
2497cc2dc46cSBarry Smith                                        MatIncreaseOverlap_MPIBAIJ,
2498cc2dc46cSBarry Smith                                        MatGetValues_MPIBAIJ,
24993c896bc6SHong Zhang                                        MatCopy_MPIBAIJ,
2500d519adbfSMatthew Knepley                                 /*44*/ 0,
2501cc2dc46cSBarry Smith                                        MatScale_MPIBAIJ,
2502cc2dc46cSBarry Smith                                        0,
2503cc2dc46cSBarry Smith                                        0,
2504cc2dc46cSBarry Smith                                        0,
2505f73d5cc4SBarry Smith                                 /*49*/ 0,
2506cc2dc46cSBarry Smith                                        0,
2507cc2dc46cSBarry Smith                                        0,
2508cc2dc46cSBarry Smith                                        0,
2509cc2dc46cSBarry Smith                                        0,
2510*93dfae19SHong Zhang                                 /*54*/ MatFDColoringCreate_MPIXAIJ,
2511cc2dc46cSBarry Smith                                        0,
2512cc2dc46cSBarry Smith                                        MatSetUnfactored_MPIBAIJ,
251382094794SBarry Smith                                        MatPermute_MPIBAIJ,
2514cc2dc46cSBarry Smith                                        MatSetValuesBlocked_MPIBAIJ,
2515d519adbfSMatthew Knepley                                 /*59*/ MatGetSubMatrix_MPIBAIJ,
2516f14a1c24SBarry Smith                                        MatDestroy_MPIBAIJ,
2517f14a1c24SBarry Smith                                        MatView_MPIBAIJ,
2518357abbc8SBarry Smith                                        0,
25197843d17aSBarry Smith                                        0,
2520d519adbfSMatthew Knepley                                 /*64*/ 0,
25217843d17aSBarry Smith                                        0,
25227843d17aSBarry Smith                                        0,
25237843d17aSBarry Smith                                        0,
25247843d17aSBarry Smith                                        0,
2525d519adbfSMatthew Knepley                                 /*69*/ MatGetRowMaxAbs_MPIBAIJ,
25267843d17aSBarry Smith                                        0,
252797304618SKris Buschelman                                        0,
252897304618SKris Buschelman                                        0,
252997304618SKris Buschelman                                        0,
2530d519adbfSMatthew Knepley                                 /*74*/ 0,
2531f6d58c54SBarry Smith                                        MatFDColoringApply_BAIJ,
253297304618SKris Buschelman                                        0,
253397304618SKris Buschelman                                        0,
253497304618SKris Buschelman                                        0,
2535d519adbfSMatthew Knepley                                 /*79*/ 0,
253697304618SKris Buschelman                                        0,
253797304618SKris Buschelman                                        0,
253897304618SKris Buschelman                                        0,
25395bba2384SShri Abhyankar                                        MatLoad_MPIBAIJ,
2540d519adbfSMatthew Knepley                                 /*84*/ 0,
2541865e5f61SKris Buschelman                                        0,
2542865e5f61SKris Buschelman                                        0,
2543865e5f61SKris Buschelman                                        0,
2544865e5f61SKris Buschelman                                        0,
2545d519adbfSMatthew Knepley                                 /*89*/ 0,
2546865e5f61SKris Buschelman                                        0,
2547865e5f61SKris Buschelman                                        0,
2548865e5f61SKris Buschelman                                        0,
2549865e5f61SKris Buschelman                                        0,
2550d519adbfSMatthew Knepley                                 /*94*/ 0,
2551865e5f61SKris Buschelman                                        0,
2552865e5f61SKris Buschelman                                        0,
255399cafbc1SBarry Smith                                        0,
255499cafbc1SBarry Smith                                        0,
2555d519adbfSMatthew Knepley                                 /*99*/ 0,
255699cafbc1SBarry Smith                                        0,
255799cafbc1SBarry Smith                                        0,
255899cafbc1SBarry Smith                                        0,
255999cafbc1SBarry Smith                                        0,
2560d519adbfSMatthew Knepley                                 /*104*/0,
256199cafbc1SBarry Smith                                        MatRealPart_MPIBAIJ,
25628c7482ecSBarry Smith                                        MatImaginaryPart_MPIBAIJ,
25638c7482ecSBarry Smith                                        0,
25648c7482ecSBarry Smith                                        0,
2565d519adbfSMatthew Knepley                                 /*109*/0,
25668c7482ecSBarry Smith                                        0,
25678c7482ecSBarry Smith                                        0,
25688c7482ecSBarry Smith                                        0,
25698c7482ecSBarry Smith                                        0,
2570d1adec66SJed Brown                                 /*114*/MatGetSeqNonzeroStructure_MPIBAIJ,
25718c7482ecSBarry Smith                                        0,
25724683f7a4SShri Abhyankar                                        MatGetGhosts_MPIBAIJ,
25734683f7a4SShri Abhyankar                                        0,
25744683f7a4SShri Abhyankar                                        0,
25754683f7a4SShri Abhyankar                                 /*119*/0,
25764683f7a4SShri Abhyankar                                        0,
25774683f7a4SShri Abhyankar                                        0,
2578bbead8a2SBarry Smith                                        0,
2579e8271787SHong Zhang                                        MatGetMultiProcBlock_MPIBAIJ,
2580bbead8a2SBarry Smith                                 /*124*/0,
2581bbead8a2SBarry Smith                                        0,
25823964eb88SJed Brown                                        MatInvertBlockDiagonal_MPIBAIJ,
25833964eb88SJed Brown                                        0,
25843964eb88SJed Brown                                        0,
25853964eb88SJed Brown                                /*129*/ 0,
25863964eb88SJed Brown                                        0,
25873964eb88SJed Brown                                        0,
25883964eb88SJed Brown                                        0,
25893964eb88SJed Brown                                        0,
25903964eb88SJed Brown                                /*134*/ 0,
25913964eb88SJed Brown                                        0,
25923964eb88SJed Brown                                        0,
25933964eb88SJed Brown                                        0,
25943964eb88SJed Brown                                        0,
25953964eb88SJed Brown                                /*139*/ 0,
2596f86b9fbaSHong Zhang                                        0,
2597f86b9fbaSHong Zhang                                        MatFDColoringSetUp_MPIXAIJ
25988c7482ecSBarry Smith };
259979bdfe76SSatish Balay 
26004a2ae208SSatish Balay #undef __FUNCT__
26014a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonalBlock_MPIBAIJ"
260211bd1e4dSLisandro Dalcin PetscErrorCode  MatGetDiagonalBlock_MPIBAIJ(Mat A,Mat *a)
26035ef9f2a5SBarry Smith {
26045ef9f2a5SBarry Smith   PetscFunctionBegin;
26055ef9f2a5SBarry Smith   *a = ((Mat_MPIBAIJ*)A->data)->A;
26065ef9f2a5SBarry Smith   PetscFunctionReturn(0);
26075ef9f2a5SBarry Smith }
260879bdfe76SSatish Balay 
26098cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIBAIJ_MPISBAIJ(Mat, MatType,MatReuse,Mat*);
2610d94109b8SHong Zhang 
2611aac34f13SBarry Smith #undef __FUNCT__
2612aac34f13SBarry Smith #define __FUNCT__ "MatMPIBAIJSetPreallocationCSR_MPIBAIJ"
2613cf12db73SBarry Smith PetscErrorCode MatMPIBAIJSetPreallocationCSR_MPIBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
2614aac34f13SBarry Smith {
2615b8d659d7SLisandro Dalcin   PetscInt       m,rstart,cstart,cend;
2616b8d659d7SLisandro Dalcin   PetscInt       i,j,d,nz,nz_max=0,*d_nnz=0,*o_nnz=0;
2617b8d659d7SLisandro Dalcin   const PetscInt *JJ    =0;
2618b8d659d7SLisandro Dalcin   PetscScalar    *values=0;
2619aac34f13SBarry Smith   PetscErrorCode ierr;
2620aac34f13SBarry Smith 
2621aac34f13SBarry Smith   PetscFunctionBegin;
262226283091SBarry Smith   ierr   = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
262326283091SBarry Smith   ierr   = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
262426283091SBarry Smith   ierr   = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
262526283091SBarry Smith   ierr   = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2626e02043d6SBarry Smith   ierr   = PetscLayoutGetBlockSize(B->rmap,&bs);CHKERRQ(ierr);
2627d0f46423SBarry Smith   m      = B->rmap->n/bs;
2628d0f46423SBarry Smith   rstart = B->rmap->rstart/bs;
2629d0f46423SBarry Smith   cstart = B->cmap->rstart/bs;
2630d0f46423SBarry Smith   cend   = B->cmap->rend/bs;
2631b8d659d7SLisandro Dalcin 
2632e32f2f54SBarry Smith   if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
2633fca92195SBarry Smith   ierr = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr);
2634aac34f13SBarry Smith   for (i=0; i<m; i++) {
2635cf12db73SBarry Smith     nz = ii[i+1] - ii[i];
2636e32f2f54SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative number of columns %D",i,nz);
2637b8d659d7SLisandro Dalcin     nz_max = PetscMax(nz_max,nz);
2638cf12db73SBarry Smith     JJ     = jj + ii[i];
2639b8d659d7SLisandro Dalcin     for (j=0; j<nz; j++) {
2640aac34f13SBarry Smith       if (*JJ >= cstart) break;
2641aac34f13SBarry Smith       JJ++;
2642aac34f13SBarry Smith     }
2643aac34f13SBarry Smith     d = 0;
2644b8d659d7SLisandro Dalcin     for (; j<nz; j++) {
2645aac34f13SBarry Smith       if (*JJ++ >= cend) break;
2646aac34f13SBarry Smith       d++;
2647aac34f13SBarry Smith     }
2648aac34f13SBarry Smith     d_nnz[i] = d;
2649b8d659d7SLisandro Dalcin     o_nnz[i] = nz - d;
2650aac34f13SBarry Smith   }
2651aac34f13SBarry Smith   ierr = MatMPIBAIJSetPreallocation(B,bs,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
2652fca92195SBarry Smith   ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr);
2653aac34f13SBarry Smith 
2654b8d659d7SLisandro Dalcin   values = (PetscScalar*)V;
2655b8d659d7SLisandro Dalcin   if (!values) {
2656fca92195SBarry Smith     ierr = PetscMalloc(bs*bs*nz_max*sizeof(PetscScalar),&values);CHKERRQ(ierr);
2657b8d659d7SLisandro Dalcin     ierr = PetscMemzero(values,bs*bs*nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
2658b8d659d7SLisandro Dalcin   }
2659b8d659d7SLisandro Dalcin   for (i=0; i<m; i++) {
2660b8d659d7SLisandro Dalcin     PetscInt          row    = i + rstart;
2661cf12db73SBarry Smith     PetscInt          ncols  = ii[i+1] - ii[i];
2662cf12db73SBarry Smith     const PetscInt    *icols = jj + ii[i];
2663cf12db73SBarry Smith     const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
2664b8d659d7SLisandro Dalcin     ierr = MatSetValuesBlocked_MPIBAIJ(B,1,&row,ncols,icols,svals,INSERT_VALUES);CHKERRQ(ierr);
2665aac34f13SBarry Smith   }
2666aac34f13SBarry Smith 
2667b8d659d7SLisandro Dalcin   if (!V) { ierr = PetscFree(values);CHKERRQ(ierr); }
2668aac34f13SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2669aac34f13SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
26707827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
2671aac34f13SBarry Smith   PetscFunctionReturn(0);
2672aac34f13SBarry Smith }
2673aac34f13SBarry Smith 
2674aac34f13SBarry Smith #undef __FUNCT__
2675aac34f13SBarry Smith #define __FUNCT__ "MatMPIBAIJSetPreallocationCSR"
2676aac34f13SBarry Smith /*@C
2677dfb205c3SBarry Smith    MatMPIBAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in BAIJ format
2678aac34f13SBarry Smith    (the default parallel PETSc format).
2679aac34f13SBarry Smith 
2680aac34f13SBarry Smith    Collective on MPI_Comm
2681aac34f13SBarry Smith 
2682aac34f13SBarry Smith    Input Parameters:
2683aac34f13SBarry Smith +  A - the matrix
2684dfb205c3SBarry Smith .  bs - the block size
2685aac34f13SBarry Smith .  i - the indices into j for the start of each local row (starts with zero)
2686aac34f13SBarry Smith .  j - the column indices for each local row (starts with zero) these must be sorted for each row
2687aac34f13SBarry Smith -  v - optional values in the matrix
2688aac34f13SBarry Smith 
2689aac34f13SBarry Smith    Level: developer
2690aac34f13SBarry Smith 
2691aac34f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2692aac34f13SBarry Smith 
269369b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIBAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ
2694aac34f13SBarry Smith @*/
26957087cfbeSBarry Smith PetscErrorCode  MatMPIBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2696aac34f13SBarry Smith {
26974ac538c5SBarry Smith   PetscErrorCode ierr;
2698aac34f13SBarry Smith 
2699aac34f13SBarry Smith   PetscFunctionBegin;
27006ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
27016ba663aaSJed Brown   PetscValidType(B,1);
27026ba663aaSJed Brown   PetscValidLogicalCollectiveInt(B,bs,2);
27034ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatMPIBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));CHKERRQ(ierr);
2704aac34f13SBarry Smith   PetscFunctionReturn(0);
2705aac34f13SBarry Smith }
2706aac34f13SBarry Smith 
27074a2ae208SSatish Balay #undef __FUNCT__
2708a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIBAIJSetPreallocation_MPIBAIJ"
2709b2573a8aSBarry Smith PetscErrorCode  MatMPIBAIJSetPreallocation_MPIBAIJ(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt *d_nnz,PetscInt o_nz,const PetscInt *o_nnz)
2710a23d5eceSKris Buschelman {
2711a23d5eceSKris Buschelman   Mat_MPIBAIJ    *b;
2712dfbe8321SBarry Smith   PetscErrorCode ierr;
2713535b19f3SBarry Smith   PetscInt       i;
2714a23d5eceSKris Buschelman 
2715a23d5eceSKris Buschelman   PetscFunctionBegin;
271626283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
271726283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
271826283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
271926283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2720e02043d6SBarry Smith   ierr = PetscLayoutGetBlockSize(B->rmap,&bs);CHKERRQ(ierr);
2721899cda47SBarry Smith 
2722a23d5eceSKris Buschelman   if (d_nnz) {
2723d0f46423SBarry Smith     for (i=0; i<B->rmap->n/bs; i++) {
2724e32f2f54SBarry 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]);
2725a23d5eceSKris Buschelman     }
2726a23d5eceSKris Buschelman   }
2727a23d5eceSKris Buschelman   if (o_nnz) {
2728d0f46423SBarry Smith     for (i=0; i<B->rmap->n/bs; i++) {
2729e32f2f54SBarry 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]);
2730a23d5eceSKris Buschelman     }
2731a23d5eceSKris Buschelman   }
2732a23d5eceSKris Buschelman 
2733a23d5eceSKris Buschelman   b      = (Mat_MPIBAIJ*)B->data;
2734a23d5eceSKris Buschelman   b->bs2 = bs*bs;
2735d0f46423SBarry Smith   b->mbs = B->rmap->n/bs;
2736d0f46423SBarry Smith   b->nbs = B->cmap->n/bs;
2737d0f46423SBarry Smith   b->Mbs = B->rmap->N/bs;
2738d0f46423SBarry Smith   b->Nbs = B->cmap->N/bs;
2739a23d5eceSKris Buschelman 
2740a23d5eceSKris Buschelman   for (i=0; i<=b->size; i++) {
2741d0f46423SBarry Smith     b->rangebs[i] = B->rmap->range[i]/bs;
2742a23d5eceSKris Buschelman   }
2743d0f46423SBarry Smith   b->rstartbs = B->rmap->rstart/bs;
2744d0f46423SBarry Smith   b->rendbs   = B->rmap->rend/bs;
2745d0f46423SBarry Smith   b->cstartbs = B->cmap->rstart/bs;
2746d0f46423SBarry Smith   b->cendbs   = B->cmap->rend/bs;
2747a23d5eceSKris Buschelman 
2748526dfc15SBarry Smith   if (!B->preallocated) {
2749f69a0ea3SMatthew Knepley     ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr);
2750d0f46423SBarry Smith     ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr);
27519c097c71SKris Buschelman     ierr = MatSetType(b->A,MATSEQBAIJ);CHKERRQ(ierr);
27523bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);CHKERRQ(ierr);
2753f69a0ea3SMatthew Knepley     ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr);
2754d0f46423SBarry Smith     ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr);
27559c097c71SKris Buschelman     ierr = MatSetType(b->B,MATSEQBAIJ);CHKERRQ(ierr);
27563bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);CHKERRQ(ierr);
2757ce94432eSBarry Smith     ierr = MatStashCreate_Private(PetscObjectComm((PetscObject)B),bs,&B->bstash);CHKERRQ(ierr);
2758526dfc15SBarry Smith   }
2759a23d5eceSKris Buschelman 
2760526dfc15SBarry Smith   ierr = MatSeqBAIJSetPreallocation(b->A,bs,d_nz,d_nnz);CHKERRQ(ierr);
2761526dfc15SBarry Smith   ierr = MatSeqBAIJSetPreallocation(b->B,bs,o_nz,o_nnz);CHKERRQ(ierr);
2762526dfc15SBarry Smith   B->preallocated = PETSC_TRUE;
2763a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2764a23d5eceSKris Buschelman }
2765a23d5eceSKris Buschelman 
27667087cfbeSBarry Smith extern PetscErrorCode  MatDiagonalScaleLocal_MPIBAIJ(Mat,Vec);
27677087cfbeSBarry Smith extern PetscErrorCode  MatSetHashTableFactor_MPIBAIJ(Mat,PetscReal);
27685bf65638SKris Buschelman 
276982094794SBarry Smith #undef __FUNCT__
277082094794SBarry Smith #define __FUNCT__ "MatConvert_MPIBAIJ_MPIAdj"
27718cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIBAIJ_MPIAdj(Mat B, MatType newtype,MatReuse reuse,Mat *adj)
277282094794SBarry Smith {
277382094794SBarry Smith   Mat_MPIBAIJ    *b = (Mat_MPIBAIJ*)B->data;
277482094794SBarry Smith   PetscErrorCode ierr;
277582094794SBarry Smith   Mat_SeqBAIJ    *d  = (Mat_SeqBAIJ*) b->A->data,*o = (Mat_SeqBAIJ*) b->B->data;
277682094794SBarry Smith   PetscInt       M   = B->rmap->n/B->rmap->bs,i,*ii,*jj,cnt,j,k,rstart = B->rmap->rstart/B->rmap->bs;
277782094794SBarry Smith   const PetscInt *id = d->i, *jd = d->j, *io = o->i, *jo = o->j, *garray = b->garray;
277882094794SBarry Smith 
277982094794SBarry Smith   PetscFunctionBegin;
278082094794SBarry Smith   ierr  = PetscMalloc((M+1)*sizeof(PetscInt),&ii);CHKERRQ(ierr);
278182094794SBarry Smith   ii[0] = 0;
278282094794SBarry Smith   for (i=0; i<M; i++) {
2783e32f2f54SBarry 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]);
2784e32f2f54SBarry 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]);
278582094794SBarry Smith     ii[i+1] = ii[i] + id[i+1] - id[i] + io[i+1] - io[i];
27865ee9ba1cSJed Brown     /* remove one from count of matrix has diagonal */
27875ee9ba1cSJed Brown     for (j=id[i]; j<id[i+1]; j++) {
27885ee9ba1cSJed Brown       if (jd[j] == i) {ii[i+1]--;break;}
27895ee9ba1cSJed Brown     }
279082094794SBarry Smith   }
279182094794SBarry Smith   ierr = PetscMalloc(ii[M]*sizeof(PetscInt),&jj);CHKERRQ(ierr);
279282094794SBarry Smith   cnt  = 0;
279382094794SBarry Smith   for (i=0; i<M; i++) {
279482094794SBarry Smith     for (j=io[i]; j<io[i+1]; j++) {
279582094794SBarry Smith       if (garray[jo[j]] > rstart) break;
279682094794SBarry Smith       jj[cnt++] = garray[jo[j]];
279782094794SBarry Smith     }
279882094794SBarry Smith     for (k=id[i]; k<id[i+1]; k++) {
27995ee9ba1cSJed Brown       if (jd[k] != i) {
280082094794SBarry Smith         jj[cnt++] = rstart + jd[k];
280182094794SBarry Smith       }
28025ee9ba1cSJed Brown     }
280382094794SBarry Smith     for (; j<io[i+1]; j++) {
280482094794SBarry Smith       jj[cnt++] = garray[jo[j]];
280582094794SBarry Smith     }
280682094794SBarry Smith   }
2807ce94432eSBarry Smith   ierr = MatCreateMPIAdj(PetscObjectComm((PetscObject)B),M,B->cmap->N/B->rmap->bs,ii,jj,NULL,adj);CHKERRQ(ierr);
280882094794SBarry Smith   PetscFunctionReturn(0);
280982094794SBarry Smith }
281082094794SBarry Smith 
2811c6db04a5SJed Brown #include <../src/mat/impls/aij/mpi/mpiaij.h>
281262471d69SBarry Smith 
28138cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_SeqBAIJ_SeqAIJ(Mat,MatType,MatReuse,Mat*);
2814b2573a8aSBarry Smith 
281562471d69SBarry Smith #undef __FUNCT__
281662471d69SBarry Smith #define __FUNCT__ "MatConvert_MPIBAIJ_MPIAIJ"
28178cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIBAIJ_MPIAIJ(Mat A,MatType newtype,MatReuse reuse,Mat *newmat)
281862471d69SBarry Smith {
281962471d69SBarry Smith   PetscErrorCode ierr;
282062471d69SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
282162471d69SBarry Smith   Mat            B;
282285a69837SSatish Balay   Mat_MPIAIJ     *b;
282362471d69SBarry Smith 
282462471d69SBarry Smith   PetscFunctionBegin;
2825ce94432eSBarry Smith   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix must be assembled");
282662471d69SBarry Smith 
2827ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
282862471d69SBarry Smith   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
28296d0a4a0eSHong Zhang   ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr);
28300298fd71SBarry Smith   ierr = MatSeqAIJSetPreallocation(B,0,NULL);CHKERRQ(ierr);
28310298fd71SBarry Smith   ierr = MatMPIAIJSetPreallocation(B,0,NULL,0,NULL);CHKERRQ(ierr);
283262471d69SBarry Smith   b    = (Mat_MPIAIJ*) B->data;
283362471d69SBarry Smith 
28346bf464f9SBarry Smith   ierr = MatDestroy(&b->A);CHKERRQ(ierr);
28356bf464f9SBarry Smith   ierr = MatDestroy(&b->B);CHKERRQ(ierr);
2836ab9863d7SBarry Smith   ierr = MatDisAssemble_MPIBAIJ(A);CHKERRQ(ierr);
283762471d69SBarry Smith   ierr = MatConvert_SeqBAIJ_SeqAIJ(a->A, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->A);CHKERRQ(ierr);
283862471d69SBarry Smith   ierr = MatConvert_SeqBAIJ_SeqAIJ(a->B, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->B);CHKERRQ(ierr);
283962471d69SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
284062471d69SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
28416a719282SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
28426a719282SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
284362471d69SBarry Smith   if (reuse == MAT_REUSE_MATRIX) {
284462471d69SBarry Smith     ierr = MatHeaderReplace(A,B);CHKERRQ(ierr);
284562471d69SBarry Smith   } else {
284662471d69SBarry Smith    *newmat = B;
284762471d69SBarry Smith   }
284862471d69SBarry Smith   PetscFunctionReturn(0);
284962471d69SBarry Smith }
285062471d69SBarry Smith 
2851450b117fSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
28528cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_baij_mumps(Mat,MatFactorType,Mat*);
2853450b117fSShri Abhyankar #endif
2854450b117fSShri Abhyankar 
28550bad9183SKris Buschelman /*MC
2856fafad747SKris Buschelman    MATMPIBAIJ - MATMPIBAIJ = "mpibaij" - A matrix type to be used for distributed block sparse matrices.
28570bad9183SKris Buschelman 
28580bad9183SKris Buschelman    Options Database Keys:
28598c07d4e3SBarry Smith + -mat_type mpibaij - sets the matrix type to "mpibaij" during a call to MatSetFromOptions()
28608c07d4e3SBarry Smith . -mat_block_size <bs> - set the blocksize used to store the matrix
28618c07d4e3SBarry Smith - -mat_use_hash_table <fact>
28620bad9183SKris Buschelman 
28630bad9183SKris Buschelman   Level: beginner
28640bad9183SKris Buschelman 
28650bad9183SKris Buschelman .seealso: MatCreateMPIBAIJ
28660bad9183SKris Buschelman M*/
28670bad9183SKris Buschelman 
28688cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIBAIJ_MPIBSTRM(Mat,MatType,MatReuse,Mat*);
2869c0cdd4a1SDahai Guo 
2870a23d5eceSKris Buschelman #undef __FUNCT__
28714a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIBAIJ"
28728cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_MPIBAIJ(Mat B)
2873273d9f13SBarry Smith {
2874273d9f13SBarry Smith   Mat_MPIBAIJ    *b;
2875dfbe8321SBarry Smith   PetscErrorCode ierr;
2876ace3abfcSBarry Smith   PetscBool      flg;
2877273d9f13SBarry Smith 
2878273d9f13SBarry Smith   PetscFunctionBegin;
287938f2d2fdSLisandro Dalcin   ierr    = PetscNewLog(B,Mat_MPIBAIJ,&b);CHKERRQ(ierr);
288082502324SSatish Balay   B->data = (void*)b;
288182502324SSatish Balay 
2882273d9f13SBarry Smith   ierr         = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
2883273d9f13SBarry Smith   B->assembled = PETSC_FALSE;
2884273d9f13SBarry Smith 
2885273d9f13SBarry Smith   B->insertmode = NOT_SET_VALUES;
2886ce94432eSBarry Smith   ierr          = MPI_Comm_rank(PetscObjectComm((PetscObject)B),&b->rank);CHKERRQ(ierr);
2887ce94432eSBarry Smith   ierr          = MPI_Comm_size(PetscObjectComm((PetscObject)B),&b->size);CHKERRQ(ierr);
2888273d9f13SBarry Smith 
2889273d9f13SBarry Smith   /* build local table of row and column ownerships */
2890899cda47SBarry Smith   ierr = PetscMalloc((b->size+1)*sizeof(PetscInt),&b->rangebs);CHKERRQ(ierr);
2891273d9f13SBarry Smith 
2892273d9f13SBarry Smith   /* build cache for off array entries formed */
2893ce94432eSBarry Smith   ierr = MatStashCreate_Private(PetscObjectComm((PetscObject)B),1,&B->stash);CHKERRQ(ierr);
289426fbe8dcSKarl Rupp 
2895273d9f13SBarry Smith   b->donotstash  = PETSC_FALSE;
28960298fd71SBarry Smith   b->colmap      = NULL;
28970298fd71SBarry Smith   b->garray      = NULL;
2898273d9f13SBarry Smith   b->roworiented = PETSC_TRUE;
2899273d9f13SBarry Smith 
2900273d9f13SBarry Smith   /* stuff used in block assembly */
2901273d9f13SBarry Smith   b->barray = 0;
2902273d9f13SBarry Smith 
2903273d9f13SBarry Smith   /* stuff used for matrix vector multiply */
2904273d9f13SBarry Smith   b->lvec  = 0;
2905273d9f13SBarry Smith   b->Mvctx = 0;
2906273d9f13SBarry Smith 
2907273d9f13SBarry Smith   /* stuff for MatGetRow() */
2908273d9f13SBarry Smith   b->rowindices   = 0;
2909273d9f13SBarry Smith   b->rowvalues    = 0;
2910273d9f13SBarry Smith   b->getrowactive = PETSC_FALSE;
2911273d9f13SBarry Smith 
2912273d9f13SBarry Smith   /* hash table stuff */
2913273d9f13SBarry Smith   b->ht           = 0;
2914273d9f13SBarry Smith   b->hd           = 0;
2915273d9f13SBarry Smith   b->ht_size      = 0;
2916273d9f13SBarry Smith   b->ht_flag      = PETSC_FALSE;
2917273d9f13SBarry Smith   b->ht_fact      = 0;
2918273d9f13SBarry Smith   b->ht_total_ct  = 0;
2919273d9f13SBarry Smith   b->ht_insert_ct = 0;
2920273d9f13SBarry Smith 
29217a868f3eSHong Zhang   /* stuff for MatGetSubMatrices_MPIBAIJ_local() */
29227a868f3eSHong Zhang   b->ijonly = PETSC_FALSE;
29237a868f3eSHong Zhang 
2924ce94432eSBarry Smith   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)B),NULL,"Options for loading MPIBAIJ matrix 1","Mat");CHKERRQ(ierr);
29250298fd71SBarry Smith   ierr = PetscOptionsBool("-mat_use_hash_table","Use hash table to save memory in constructing matrix","MatSetOption",PETSC_FALSE,&flg,NULL);CHKERRQ(ierr);
2926273d9f13SBarry Smith   if (flg) {
2927f6275e2eSBarry Smith     PetscReal fact = 1.39;
29284e0d8c25SBarry Smith     ierr = MatSetOption(B,MAT_USE_HASH_TABLE,PETSC_TRUE);CHKERRQ(ierr);
29290298fd71SBarry Smith     ierr = PetscOptionsReal("-mat_use_hash_table","Use hash table factor","MatMPIBAIJSetHashTableFactor",fact,&fact,NULL);CHKERRQ(ierr);
2930273d9f13SBarry Smith     if (fact <= 1.0) fact = 1.39;
2931273d9f13SBarry Smith     ierr = MatMPIBAIJSetHashTableFactor(B,fact);CHKERRQ(ierr);
29321e2582c4SBarry Smith     ierr = PetscInfo1(B,"Hash table Factor used %5.2f\n",fact);CHKERRQ(ierr);
2933273d9f13SBarry Smith   }
29348c07d4e3SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
29358c07d4e3SBarry Smith 
2936450b117fSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
2937bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C",MatGetFactor_baij_mumps);CHKERRQ(ierr);
2938450b117fSShri Abhyankar #endif
2939bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpiadj_C",MatConvert_MPIBAIJ_MPIAdj);CHKERRQ(ierr);
2940bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpiaij_C",MatConvert_MPIBAIJ_MPIAIJ);CHKERRQ(ierr);
2941bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpisbaij_C",MatConvert_MPIBAIJ_MPISBAIJ);CHKERRQ(ierr);
2942bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPIBAIJ);CHKERRQ(ierr);
2943bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPIBAIJ);CHKERRQ(ierr);
2944bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C",MatGetDiagonalBlock_MPIBAIJ);CHKERRQ(ierr);
2945bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMPIBAIJSetPreallocation_C",MatMPIBAIJSetPreallocation_MPIBAIJ);CHKERRQ(ierr);
2946bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatMPIBAIJSetPreallocationCSR_C",MatMPIBAIJSetPreallocationCSR_MPIBAIJ);CHKERRQ(ierr);
2947bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatDiagonalScaleLocal_C",MatDiagonalScaleLocal_MPIBAIJ);CHKERRQ(ierr);
2948bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSetHashTableFactor_C",MatSetHashTableFactor_MPIBAIJ);CHKERRQ(ierr);
2949bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpibstrm_C",MatConvert_MPIBAIJ_MPIBSTRM);CHKERRQ(ierr);
295017667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIBAIJ);CHKERRQ(ierr);
2951273d9f13SBarry Smith   PetscFunctionReturn(0);
2952273d9f13SBarry Smith }
2953273d9f13SBarry Smith 
2954209238afSKris Buschelman /*MC
2955002d173eSKris Buschelman    MATBAIJ - MATBAIJ = "baij" - A matrix type to be used for block sparse matrices.
2956209238afSKris Buschelman 
2957209238afSKris Buschelman    This matrix type is identical to MATSEQBAIJ when constructed with a single process communicator,
2958209238afSKris Buschelman    and MATMPIBAIJ otherwise.
2959209238afSKris Buschelman 
2960209238afSKris Buschelman    Options Database Keys:
2961209238afSKris Buschelman . -mat_type baij - sets the matrix type to "baij" during a call to MatSetFromOptions()
2962209238afSKris Buschelman 
2963209238afSKris Buschelman   Level: beginner
2964209238afSKris Buschelman 
296569b1f4b7SBarry Smith .seealso: MatCreateBAIJ(),MATSEQBAIJ,MATMPIBAIJ, MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
2966209238afSKris Buschelman M*/
2967209238afSKris Buschelman 
29684a2ae208SSatish Balay #undef __FUNCT__
29694a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJSetPreallocation"
2970273d9f13SBarry Smith /*@C
2971aac34f13SBarry Smith    MatMPIBAIJSetPreallocation - Allocates memory for a sparse parallel matrix in block AIJ format
2972273d9f13SBarry Smith    (block compressed row).  For good matrix assembly performance
2973273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
2974273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
2975273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
2976273d9f13SBarry Smith 
2977273d9f13SBarry Smith    Collective on Mat
2978273d9f13SBarry Smith 
2979273d9f13SBarry Smith    Input Parameters:
2980273d9f13SBarry Smith +  A - the matrix
2981e8271787SHong Zhang .  bs   - size of block
2982273d9f13SBarry Smith .  d_nz  - number of block nonzeros per block row in diagonal portion of local
2983273d9f13SBarry Smith            submatrix  (same for all local rows)
2984273d9f13SBarry Smith .  d_nnz - array containing the number of block nonzeros in the various block rows
2985273d9f13SBarry Smith            of the in diagonal portion of the local (possibly different for each block
29860298fd71SBarry Smith            row) or NULL.  If you plan to factor the matrix you must leave room for the diagonal entry and
298795742e49SBarry Smith            set it even if it is zero.
2988273d9f13SBarry Smith .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
2989273d9f13SBarry Smith            submatrix (same for all local rows).
2990273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various block rows of the
2991273d9f13SBarry Smith            off-diagonal portion of the local submatrix (possibly different for
29920298fd71SBarry Smith            each block row) or NULL.
2993273d9f13SBarry Smith 
299449a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
2995273d9f13SBarry Smith 
2996273d9f13SBarry Smith    Options Database Keys:
29978c07d4e3SBarry Smith +   -mat_block_size - size of the blocks to use
29988c07d4e3SBarry Smith -   -mat_use_hash_table <fact>
2999273d9f13SBarry Smith 
3000273d9f13SBarry Smith    Notes:
3001273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one processor
3002273d9f13SBarry Smith    than it must be used on all processors that share the object for that argument.
3003273d9f13SBarry Smith 
3004273d9f13SBarry Smith    Storage Information:
3005273d9f13SBarry Smith    For a square global matrix we define each processor's diagonal portion
3006273d9f13SBarry Smith    to be its local rows and the corresponding columns (a square submatrix);
3007273d9f13SBarry Smith    each processor's off-diagonal portion encompasses the remainder of the
3008273d9f13SBarry Smith    local matrix (a rectangular submatrix).
3009273d9f13SBarry Smith 
3010273d9f13SBarry Smith    The user can specify preallocated storage for the diagonal part of
3011273d9f13SBarry Smith    the local submatrix with either d_nz or d_nnz (not both).  Set
30120298fd71SBarry Smith    d_nz=PETSC_DEFAULT and d_nnz=NULL for PETSc to control dynamic
3013273d9f13SBarry Smith    memory allocation.  Likewise, specify preallocated storage for the
3014273d9f13SBarry Smith    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
3015273d9f13SBarry Smith 
3016273d9f13SBarry Smith    Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
3017273d9f13SBarry Smith    the figure below we depict these three local rows and all columns (0-11).
3018273d9f13SBarry Smith 
3019273d9f13SBarry Smith .vb
3020273d9f13SBarry Smith            0 1 2 3 4 5 6 7 8 9 10 11
3021a4b1a0f6SJed Brown           --------------------------
3022273d9f13SBarry Smith    row 3  |o o o d d d o o o o  o  o
3023273d9f13SBarry Smith    row 4  |o o o d d d o o o o  o  o
3024273d9f13SBarry Smith    row 5  |o o o d d d o o o o  o  o
3025a4b1a0f6SJed Brown           --------------------------
3026273d9f13SBarry Smith .ve
3027273d9f13SBarry Smith 
3028273d9f13SBarry Smith    Thus, any entries in the d locations are stored in the d (diagonal)
3029273d9f13SBarry Smith    submatrix, and any entries in the o locations are stored in the
3030273d9f13SBarry Smith    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
3031273d9f13SBarry Smith    stored simply in the MATSEQBAIJ format for compressed row storage.
3032273d9f13SBarry Smith 
3033273d9f13SBarry Smith    Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3034273d9f13SBarry Smith    and o_nz should indicate the number of block nonzeros per row in the o matrix.
3035273d9f13SBarry Smith    In general, for PDE problems in which most nonzeros are near the diagonal,
3036273d9f13SBarry Smith    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
3037273d9f13SBarry Smith    or you will get TERRIBLE performance; see the users' manual chapter on
3038273d9f13SBarry Smith    matrices.
3039273d9f13SBarry Smith 
3040aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3041aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3042aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3043aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3044aa95bbe8SBarry Smith 
3045273d9f13SBarry Smith    Level: intermediate
3046273d9f13SBarry Smith 
3047273d9f13SBarry Smith .keywords: matrix, block, aij, compressed row, sparse, parallel
3048273d9f13SBarry Smith 
3049ab978733SBarry Smith .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateBAIJ(), MatMPIBAIJSetPreallocationCSR(), PetscSplitOwnership()
3050273d9f13SBarry Smith @*/
30517087cfbeSBarry Smith PetscErrorCode  MatMPIBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3052273d9f13SBarry Smith {
30534ac538c5SBarry Smith   PetscErrorCode ierr;
3054273d9f13SBarry Smith 
3055273d9f13SBarry Smith   PetscFunctionBegin;
30566ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
30576ba663aaSJed Brown   PetscValidType(B,1);
30586ba663aaSJed Brown   PetscValidLogicalCollectiveInt(B,bs,2);
30594ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatMPIBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,bs,d_nz,d_nnz,o_nz,o_nnz));CHKERRQ(ierr);
3060273d9f13SBarry Smith   PetscFunctionReturn(0);
3061273d9f13SBarry Smith }
3062273d9f13SBarry Smith 
30634a2ae208SSatish Balay #undef __FUNCT__
306469b1f4b7SBarry Smith #define __FUNCT__ "MatCreateBAIJ"
306579bdfe76SSatish Balay /*@C
306669b1f4b7SBarry Smith    MatCreateBAIJ - Creates a sparse parallel matrix in block AIJ format
306779bdfe76SSatish Balay    (block compressed row).  For good matrix assembly performance
306879bdfe76SSatish Balay    the user should preallocate the matrix storage by setting the parameters
306979bdfe76SSatish Balay    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
307079bdfe76SSatish Balay    performance can be increased by more than a factor of 50.
307179bdfe76SSatish Balay 
3072db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3073db81eaa0SLois Curfman McInnes 
307479bdfe76SSatish Balay    Input Parameters:
3075db81eaa0SLois Curfman McInnes +  comm - MPI communicator
307679bdfe76SSatish Balay .  bs   - size of blockk
307779bdfe76SSatish Balay .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
307892e8d321SLois Curfman McInnes            This value should be the same as the local size used in creating the
307992e8d321SLois Curfman McInnes            y vector for the matrix-vector product y = Ax.
308092e8d321SLois Curfman McInnes .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
308192e8d321SLois Curfman McInnes            This value should be the same as the local size used in creating the
308292e8d321SLois Curfman McInnes            x vector for the matrix-vector product y = Ax.
3083be79a94dSBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3084be79a94dSBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
308547a75d0bSBarry Smith .  d_nz  - number of nonzero blocks per block row in diagonal portion of local
308679bdfe76SSatish Balay            submatrix  (same for all local rows)
308747a75d0bSBarry Smith .  d_nnz - array containing the number of nonzero blocks in the various block rows
308892e8d321SLois Curfman McInnes            of the in diagonal portion of the local (possibly different for each block
30890298fd71SBarry Smith            row) or NULL.  If you plan to factor the matrix you must leave room for the diagonal entry
309095742e49SBarry Smith            and set it even if it is zero.
309147a75d0bSBarry Smith .  o_nz  - number of nonzero blocks per block row in the off-diagonal portion of local
309279bdfe76SSatish Balay            submatrix (same for all local rows).
309347a75d0bSBarry Smith -  o_nnz - array containing the number of nonzero blocks in the various block rows of the
309492e8d321SLois Curfman McInnes            off-diagonal portion of the local submatrix (possibly different for
30950298fd71SBarry Smith            each block row) or NULL.
309679bdfe76SSatish Balay 
309779bdfe76SSatish Balay    Output Parameter:
309879bdfe76SSatish Balay .  A - the matrix
309979bdfe76SSatish Balay 
3100db81eaa0SLois Curfman McInnes    Options Database Keys:
31018c07d4e3SBarry Smith +   -mat_block_size - size of the blocks to use
31028c07d4e3SBarry Smith -   -mat_use_hash_table <fact>
31033ffaccefSLois Curfman McInnes 
3104175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3105ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3106175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3107175b88e8SBarry Smith 
3108b259b22eSLois Curfman McInnes    Notes:
310949a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
311049a6f317SBarry Smith 
311147a75d0bSBarry Smith    A nonzero block is any block that as 1 or more nonzeros in it
311247a75d0bSBarry Smith 
311379bdfe76SSatish Balay    The user MUST specify either the local or global matrix dimensions
311479bdfe76SSatish Balay    (possibly both).
311579bdfe76SSatish Balay 
3116be79a94dSBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one processor
3117be79a94dSBarry Smith    than it must be used on all processors that share the object for that argument.
3118be79a94dSBarry Smith 
311979bdfe76SSatish Balay    Storage Information:
312079bdfe76SSatish Balay    For a square global matrix we define each processor's diagonal portion
312179bdfe76SSatish Balay    to be its local rows and the corresponding columns (a square submatrix);
312279bdfe76SSatish Balay    each processor's off-diagonal portion encompasses the remainder of the
312379bdfe76SSatish Balay    local matrix (a rectangular submatrix).
312479bdfe76SSatish Balay 
312579bdfe76SSatish Balay    The user can specify preallocated storage for the diagonal part of
312679bdfe76SSatish Balay    the local submatrix with either d_nz or d_nnz (not both).  Set
31270298fd71SBarry Smith    d_nz=PETSC_DEFAULT and d_nnz=NULL for PETSc to control dynamic
312879bdfe76SSatish Balay    memory allocation.  Likewise, specify preallocated storage for the
312979bdfe76SSatish Balay    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
313079bdfe76SSatish Balay 
313179bdfe76SSatish Balay    Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
313279bdfe76SSatish Balay    the figure below we depict these three local rows and all columns (0-11).
313379bdfe76SSatish Balay 
3134db81eaa0SLois Curfman McInnes .vb
3135db81eaa0SLois Curfman McInnes            0 1 2 3 4 5 6 7 8 9 10 11
3136a4b1a0f6SJed Brown           --------------------------
3137db81eaa0SLois Curfman McInnes    row 3  |o o o d d d o o o o  o  o
3138db81eaa0SLois Curfman McInnes    row 4  |o o o d d d o o o o  o  o
3139db81eaa0SLois Curfman McInnes    row 5  |o o o d d d o o o o  o  o
3140a4b1a0f6SJed Brown           --------------------------
3141db81eaa0SLois Curfman McInnes .ve
314279bdfe76SSatish Balay 
314379bdfe76SSatish Balay    Thus, any entries in the d locations are stored in the d (diagonal)
314479bdfe76SSatish Balay    submatrix, and any entries in the o locations are stored in the
314579bdfe76SSatish Balay    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
314657b952d6SSatish Balay    stored simply in the MATSEQBAIJ format for compressed row storage.
314779bdfe76SSatish Balay 
3148d64ed03dSBarry Smith    Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3149d64ed03dSBarry Smith    and o_nz should indicate the number of block nonzeros per row in the o matrix.
315079bdfe76SSatish Balay    In general, for PDE problems in which most nonzeros are near the diagonal,
315192e8d321SLois Curfman McInnes    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
315292e8d321SLois Curfman McInnes    or you will get TERRIBLE performance; see the users' manual chapter on
31536da5968aSLois Curfman McInnes    matrices.
315479bdfe76SSatish Balay 
3155027ccd11SLois Curfman McInnes    Level: intermediate
3156027ccd11SLois Curfman McInnes 
315792e8d321SLois Curfman McInnes .keywords: matrix, block, aij, compressed row, sparse, parallel
315879bdfe76SSatish Balay 
315969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateBAIJ(), MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
316079bdfe76SSatish Balay @*/
316169b1f4b7SBarry Smith PetscErrorCode  MatCreateBAIJ(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)
316279bdfe76SSatish Balay {
31636849ba73SBarry Smith   PetscErrorCode ierr;
3164b24ad042SBarry Smith   PetscMPIInt    size;
316579bdfe76SSatish Balay 
3166d64ed03dSBarry Smith   PetscFunctionBegin;
3167f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3168f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr);
3169d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3170273d9f13SBarry Smith   if (size > 1) {
3171273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIBAIJ);CHKERRQ(ierr);
3172273d9f13SBarry Smith     ierr = MatMPIBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
3173273d9f13SBarry Smith   } else {
3174273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQBAIJ);CHKERRQ(ierr);
3175273d9f13SBarry Smith     ierr = MatSeqBAIJSetPreallocation(*A,bs,d_nz,d_nnz);CHKERRQ(ierr);
31763914022bSBarry Smith   }
31773a40ed3dSBarry Smith   PetscFunctionReturn(0);
317879bdfe76SSatish Balay }
3179026e39d0SSatish Balay 
31804a2ae208SSatish Balay #undef __FUNCT__
31814a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIBAIJ"
31826849ba73SBarry Smith static PetscErrorCode MatDuplicate_MPIBAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
31830ac07820SSatish Balay {
31840ac07820SSatish Balay   Mat            mat;
31850ac07820SSatish Balay   Mat_MPIBAIJ    *a,*oldmat = (Mat_MPIBAIJ*)matin->data;
3186dfbe8321SBarry Smith   PetscErrorCode ierr;
3187b24ad042SBarry Smith   PetscInt       len=0;
31880ac07820SSatish Balay 
3189d64ed03dSBarry Smith   PetscFunctionBegin;
31900ac07820SSatish Balay   *newmat = 0;
3191ce94432eSBarry Smith   ierr    = MatCreate(PetscObjectComm((PetscObject)matin),&mat);CHKERRQ(ierr);
3192d0f46423SBarry Smith   ierr    = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr);
31937adad957SLisandro Dalcin   ierr    = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr);
31941d5dac46SHong Zhang   ierr    = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
31957fff6886SHong Zhang 
3196d5f3da31SBarry Smith   mat->factortype   = matin->factortype;
3197273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
31980ac07820SSatish Balay   mat->assembled    = PETSC_TRUE;
31997fff6886SHong Zhang   mat->insertmode   = NOT_SET_VALUES;
32007fff6886SHong Zhang 
3201273d9f13SBarry Smith   a             = (Mat_MPIBAIJ*)mat->data;
3202d0f46423SBarry Smith   mat->rmap->bs = matin->rmap->bs;
32030ac07820SSatish Balay   a->bs2        = oldmat->bs2;
32040ac07820SSatish Balay   a->mbs        = oldmat->mbs;
32050ac07820SSatish Balay   a->nbs        = oldmat->nbs;
32060ac07820SSatish Balay   a->Mbs        = oldmat->Mbs;
32070ac07820SSatish Balay   a->Nbs        = oldmat->Nbs;
32080ac07820SSatish Balay 
32091e1e43feSBarry Smith   ierr = PetscLayoutReference(matin->rmap,&mat->rmap);CHKERRQ(ierr);
32101e1e43feSBarry Smith   ierr = PetscLayoutReference(matin->cmap,&mat->cmap);CHKERRQ(ierr);
3211899cda47SBarry Smith 
32120ac07820SSatish Balay   a->size         = oldmat->size;
32130ac07820SSatish Balay   a->rank         = oldmat->rank;
3214aef5e8e0SSatish Balay   a->donotstash   = oldmat->donotstash;
3215aef5e8e0SSatish Balay   a->roworiented  = oldmat->roworiented;
3216aef5e8e0SSatish Balay   a->rowindices   = 0;
32170ac07820SSatish Balay   a->rowvalues    = 0;
32180ac07820SSatish Balay   a->getrowactive = PETSC_FALSE;
321930793edcSSatish Balay   a->barray       = 0;
3220899cda47SBarry Smith   a->rstartbs     = oldmat->rstartbs;
3221899cda47SBarry Smith   a->rendbs       = oldmat->rendbs;
3222899cda47SBarry Smith   a->cstartbs     = oldmat->cstartbs;
3223899cda47SBarry Smith   a->cendbs       = oldmat->cendbs;
32240ac07820SSatish Balay 
3225133cdb44SSatish Balay   /* hash table stuff */
3226133cdb44SSatish Balay   a->ht           = 0;
3227133cdb44SSatish Balay   a->hd           = 0;
3228133cdb44SSatish Balay   a->ht_size      = 0;
3229133cdb44SSatish Balay   a->ht_flag      = oldmat->ht_flag;
323025fdafccSSatish Balay   a->ht_fact      = oldmat->ht_fact;
3231133cdb44SSatish Balay   a->ht_total_ct  = 0;
3232133cdb44SSatish Balay   a->ht_insert_ct = 0;
3233133cdb44SSatish Balay 
3234899cda47SBarry Smith   ierr = PetscMemcpy(a->rangebs,oldmat->rangebs,(a->size+1)*sizeof(PetscInt));CHKERRQ(ierr);
32350ac07820SSatish Balay   if (oldmat->colmap) {
3236aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
32370f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
323848e59246SSatish Balay #else
3239b24ad042SBarry Smith     ierr = PetscMalloc((a->Nbs)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr);
32403bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,(a->Nbs)*sizeof(PetscInt));CHKERRQ(ierr);
3241b24ad042SBarry Smith     ierr = PetscMemcpy(a->colmap,oldmat->colmap,(a->Nbs)*sizeof(PetscInt));CHKERRQ(ierr);
324248e59246SSatish Balay #endif
32430ac07820SSatish Balay   } else a->colmap = 0;
32444beb1cfeSHong Zhang 
32450ac07820SSatish Balay   if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) {
3246b24ad042SBarry Smith     ierr = PetscMalloc(len*sizeof(PetscInt),&a->garray);CHKERRQ(ierr);
32473bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));CHKERRQ(ierr);
3248b24ad042SBarry Smith     ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr);
32490ac07820SSatish Balay   } else a->garray = 0;
32500ac07820SSatish Balay 
3251ce94432eSBarry Smith   ierr = MatStashCreate_Private(PetscObjectComm((PetscObject)matin),matin->rmap->bs,&mat->bstash);CHKERRQ(ierr);
32520ac07820SSatish Balay   ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
32533bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);CHKERRQ(ierr);
32540ac07820SSatish Balay   ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
32553bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);CHKERRQ(ierr);
32567fff6886SHong Zhang 
32572e8a6d31SBarry Smith   ierr    = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
32583bb1ff40SBarry Smith   ierr    = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);CHKERRQ(ierr);
32592e8a6d31SBarry Smith   ierr    = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
32603bb1ff40SBarry Smith   ierr    = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);CHKERRQ(ierr);
3261140e18c1SBarry Smith   ierr    = PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr);
32620ac07820SSatish Balay   *newmat = mat;
32633a40ed3dSBarry Smith   PetscFunctionReturn(0);
32640ac07820SSatish Balay }
326557b952d6SSatish Balay 
32664a2ae208SSatish Balay #undef __FUNCT__
32675bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIBAIJ"
3268112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIBAIJ(Mat newmat,PetscViewer viewer)
32694683f7a4SShri Abhyankar {
32704683f7a4SShri Abhyankar   PetscErrorCode ierr;
32714683f7a4SShri Abhyankar   int            fd;
32724683f7a4SShri Abhyankar   PetscInt       i,nz,j,rstart,rend;
32734683f7a4SShri Abhyankar   PetscScalar    *vals,*buf;
3274ce94432eSBarry Smith   MPI_Comm       comm;
32754683f7a4SShri Abhyankar   MPI_Status     status;
32764683f7a4SShri Abhyankar   PetscMPIInt    rank,size,maxnz;
32774683f7a4SShri Abhyankar   PetscInt       header[4],*rowlengths = 0,M,N,m,*rowners,*cols;
32780298fd71SBarry Smith   PetscInt       *locrowlens = NULL,*procsnz = NULL,*browners = NULL;
32794683f7a4SShri Abhyankar   PetscInt       jj,*mycols,*ibuf,bs=1,Mbs,mbs,extra_rows,mmax;
32804683f7a4SShri Abhyankar   PetscMPIInt    tag    = ((PetscObject)viewer)->tag;
32810298fd71SBarry Smith   PetscInt       *dlens = NULL,*odlens = NULL,*mask = NULL,*masked1 = NULL,*masked2 = NULL,rowcount,odcount;
32824683f7a4SShri Abhyankar   PetscInt       dcount,kmax,k,nzcount,tmp,mend,sizesset=1,grows,gcols;
32834683f7a4SShri Abhyankar 
32844683f7a4SShri Abhyankar   PetscFunctionBegin;
3285ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
32860298fd71SBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"Options for loading MPIBAIJ matrix 2","Mat");CHKERRQ(ierr);
32870298fd71SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
32884683f7a4SShri Abhyankar   ierr = PetscOptionsEnd();CHKERRQ(ierr);
32894683f7a4SShri Abhyankar 
32904683f7a4SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
32914683f7a4SShri Abhyankar   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
32924683f7a4SShri Abhyankar   if (!rank) {
32934683f7a4SShri Abhyankar     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
32944683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,(char*)header,4,PETSC_INT);CHKERRQ(ierr);
32954683f7a4SShri Abhyankar     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
32964683f7a4SShri Abhyankar   }
32974683f7a4SShri Abhyankar 
32984683f7a4SShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) sizesset = 0;
32994683f7a4SShri Abhyankar 
33004683f7a4SShri Abhyankar   ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr);
33014683f7a4SShri Abhyankar   M    = header[1]; N = header[2];
33024683f7a4SShri Abhyankar 
33034683f7a4SShri Abhyankar   /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */
33044683f7a4SShri Abhyankar   if (sizesset && newmat->rmap->N < 0) newmat->rmap->N = M;
33054683f7a4SShri Abhyankar   if (sizesset && newmat->cmap->N < 0) newmat->cmap->N = N;
33064683f7a4SShri Abhyankar 
33074683f7a4SShri Abhyankar   /* If global sizes are set, check if they are consistent with that given in the file */
33084683f7a4SShri Abhyankar   if (sizesset) {
33094683f7a4SShri Abhyankar     ierr = MatGetSize(newmat,&grows,&gcols);CHKERRQ(ierr);
33104683f7a4SShri Abhyankar   }
3311abd38a8fSBarry Smith   if (sizesset && newmat->rmap->N != grows) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows:Matrix in file has (%d) and input matrix has (%d)",M,grows);
3312abd38a8fSBarry Smith   if (sizesset && newmat->cmap->N != gcols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of cols:Matrix in file has (%d) and input matrix has (%d)",N,gcols);
33134683f7a4SShri Abhyankar 
3314ce94432eSBarry Smith   if (M != N) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Can only do square matrices");
33154683f7a4SShri Abhyankar 
33164683f7a4SShri Abhyankar   /*
33174683f7a4SShri Abhyankar      This code adds extra rows to make sure the number of rows is
33184683f7a4SShri Abhyankar      divisible by the blocksize
33194683f7a4SShri Abhyankar   */
33204683f7a4SShri Abhyankar   Mbs        = M/bs;
33214683f7a4SShri Abhyankar   extra_rows = bs - M + bs*Mbs;
33224683f7a4SShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
33234683f7a4SShri Abhyankar   else                  Mbs++;
33244683f7a4SShri Abhyankar   if (extra_rows && !rank) {
33254683f7a4SShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
33264683f7a4SShri Abhyankar   }
33274683f7a4SShri Abhyankar 
33284683f7a4SShri Abhyankar   /* determine ownership of all rows */
33294683f7a4SShri Abhyankar   if (newmat->rmap->n < 0) { /* PETSC_DECIDE */
33304683f7a4SShri Abhyankar     mbs = Mbs/size + ((Mbs % size) > rank);
33314683f7a4SShri Abhyankar     m   = mbs*bs;
33324683f7a4SShri Abhyankar   } else { /* User set */
33334683f7a4SShri Abhyankar     m   = newmat->rmap->n;
33344683f7a4SShri Abhyankar     mbs = m/bs;
33354683f7a4SShri Abhyankar   }
33364683f7a4SShri Abhyankar   ierr = PetscMalloc2(size+1,PetscInt,&rowners,size+1,PetscInt,&browners);CHKERRQ(ierr);
33374683f7a4SShri Abhyankar   ierr = MPI_Allgather(&mbs,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
33384683f7a4SShri Abhyankar 
33394683f7a4SShri Abhyankar   /* process 0 needs enough room for process with most rows */
33404683f7a4SShri Abhyankar   if (!rank) {
33414683f7a4SShri Abhyankar     mmax = rowners[1];
33421251c579SMatthew G Knepley     for (i=2; i<=size; i++) {
33434683f7a4SShri Abhyankar       mmax = PetscMax(mmax,rowners[i]);
33444683f7a4SShri Abhyankar     }
33454683f7a4SShri Abhyankar     mmax*=bs;
33463964eb88SJed Brown   } else mmax = -1;             /* unused, but compiler warns anyway */
33474683f7a4SShri Abhyankar 
33484683f7a4SShri Abhyankar   rowners[0] = 0;
33494683f7a4SShri Abhyankar   for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
33504683f7a4SShri Abhyankar   for (i=0; i<=size; i++) browners[i] = rowners[i]*bs;
33514683f7a4SShri Abhyankar   rstart = rowners[rank];
33524683f7a4SShri Abhyankar   rend   = rowners[rank+1];
33534683f7a4SShri Abhyankar 
33544683f7a4SShri Abhyankar   /* distribute row lengths to all processors */
33555aa9a6beSBarry Smith   ierr = PetscMalloc(m*sizeof(PetscInt),&locrowlens);CHKERRQ(ierr);
33564683f7a4SShri Abhyankar   if (!rank) {
33574683f7a4SShri Abhyankar     mend = m;
33584683f7a4SShri Abhyankar     if (size == 1) mend = mend - extra_rows;
33594683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,locrowlens,mend,PETSC_INT);CHKERRQ(ierr);
33604683f7a4SShri Abhyankar     for (j=mend; j<m; j++) locrowlens[j] = 1;
33615aa9a6beSBarry Smith     ierr = PetscMalloc(mmax*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
33624683f7a4SShri Abhyankar     ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr);
33634683f7a4SShri Abhyankar     ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr);
33644683f7a4SShri Abhyankar     for (j=0; j<m; j++) {
33654683f7a4SShri Abhyankar       procsnz[0] += locrowlens[j];
33664683f7a4SShri Abhyankar     }
33674683f7a4SShri Abhyankar     for (i=1; i<size; i++) {
33684683f7a4SShri Abhyankar       mend = browners[i+1] - browners[i];
33694683f7a4SShri Abhyankar       if (i == size-1) mend = mend - extra_rows;
33704683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,rowlengths,mend,PETSC_INT);CHKERRQ(ierr);
33714683f7a4SShri Abhyankar       for (j=mend; j<browners[i+1] - browners[i]; j++) rowlengths[j] = 1;
33724683f7a4SShri Abhyankar       /* calculate the number of nonzeros on each processor */
33734683f7a4SShri Abhyankar       for (j=0; j<browners[i+1]-browners[i]; j++) {
33744683f7a4SShri Abhyankar         procsnz[i] += rowlengths[j];
33754683f7a4SShri Abhyankar       }
33764683f7a4SShri Abhyankar       ierr = MPI_Send(rowlengths,browners[i+1]-browners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
33774683f7a4SShri Abhyankar     }
33784683f7a4SShri Abhyankar     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
33794683f7a4SShri Abhyankar   } else {
33804683f7a4SShri Abhyankar     ierr = MPI_Recv(locrowlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
33814683f7a4SShri Abhyankar   }
33824683f7a4SShri Abhyankar 
33834683f7a4SShri Abhyankar   if (!rank) {
33844683f7a4SShri Abhyankar     /* determine max buffer needed and allocate it */
33854683f7a4SShri Abhyankar     maxnz = procsnz[0];
33864683f7a4SShri Abhyankar     for (i=1; i<size; i++) {
33874683f7a4SShri Abhyankar       maxnz = PetscMax(maxnz,procsnz[i]);
33884683f7a4SShri Abhyankar     }
33894683f7a4SShri Abhyankar     ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr);
33904683f7a4SShri Abhyankar 
33914683f7a4SShri Abhyankar     /* read in my part of the matrix column indices  */
33924683f7a4SShri Abhyankar     nz     = procsnz[0];
33934683f7a4SShri Abhyankar     ierr   = PetscMalloc((nz+1)*sizeof(PetscInt),&ibuf);CHKERRQ(ierr);
33944683f7a4SShri Abhyankar     mycols = ibuf;
33954683f7a4SShri Abhyankar     if (size == 1) nz -= extra_rows;
33964683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
339726fbe8dcSKarl Rupp     if (size == 1) {
339826fbe8dcSKarl Rupp       for (i=0; i< extra_rows; i++) mycols[nz+i] = M+i;
339926fbe8dcSKarl Rupp     }
34004683f7a4SShri Abhyankar 
34014683f7a4SShri Abhyankar     /* read in every ones (except the last) and ship off */
34024683f7a4SShri Abhyankar     for (i=1; i<size-1; i++) {
34034683f7a4SShri Abhyankar       nz   = procsnz[i];
34044683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
34054683f7a4SShri Abhyankar       ierr = MPI_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
34064683f7a4SShri Abhyankar     }
34074683f7a4SShri Abhyankar     /* read in the stuff for the last proc */
34084683f7a4SShri Abhyankar     if (size != 1) {
34094683f7a4SShri Abhyankar       nz   = procsnz[size-1] - extra_rows;  /* the extra rows are not on the disk */
34104683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
34114683f7a4SShri Abhyankar       for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
34124683f7a4SShri Abhyankar       ierr = MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);CHKERRQ(ierr);
34134683f7a4SShri Abhyankar     }
34144683f7a4SShri Abhyankar     ierr = PetscFree(cols);CHKERRQ(ierr);
34154683f7a4SShri Abhyankar   } else {
34164683f7a4SShri Abhyankar     /* determine buffer space needed for message */
34174683f7a4SShri Abhyankar     nz = 0;
34184683f7a4SShri Abhyankar     for (i=0; i<m; i++) {
34194683f7a4SShri Abhyankar       nz += locrowlens[i];
34204683f7a4SShri Abhyankar     }
34214683f7a4SShri Abhyankar     ierr   = PetscMalloc((nz+1)*sizeof(PetscInt),&ibuf);CHKERRQ(ierr);
34224683f7a4SShri Abhyankar     mycols = ibuf;
34234683f7a4SShri Abhyankar     /* receive message of column indices*/
34244683f7a4SShri Abhyankar     ierr = MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
34254683f7a4SShri Abhyankar     ierr = MPI_Get_count(&status,MPIU_INT,&maxnz);CHKERRQ(ierr);
34264683f7a4SShri Abhyankar     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
34274683f7a4SShri Abhyankar   }
34284683f7a4SShri Abhyankar 
34294683f7a4SShri Abhyankar   /* loop over local rows, determining number of off diagonal entries */
34304683f7a4SShri Abhyankar   ierr     = PetscMalloc2(rend-rstart,PetscInt,&dlens,rend-rstart,PetscInt,&odlens);CHKERRQ(ierr);
34314683f7a4SShri Abhyankar   ierr     = PetscMalloc3(Mbs,PetscInt,&mask,Mbs,PetscInt,&masked1,Mbs,PetscInt,&masked2);CHKERRQ(ierr);
34324683f7a4SShri Abhyankar   ierr     = PetscMemzero(mask,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
34334683f7a4SShri Abhyankar   ierr     = PetscMemzero(masked1,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
34344683f7a4SShri Abhyankar   ierr     = PetscMemzero(masked2,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
34354683f7a4SShri Abhyankar   rowcount = 0; nzcount = 0;
34364683f7a4SShri Abhyankar   for (i=0; i<mbs; i++) {
34374683f7a4SShri Abhyankar     dcount  = 0;
34384683f7a4SShri Abhyankar     odcount = 0;
34394683f7a4SShri Abhyankar     for (j=0; j<bs; j++) {
34404683f7a4SShri Abhyankar       kmax = locrowlens[rowcount];
34414683f7a4SShri Abhyankar       for (k=0; k<kmax; k++) {
34424683f7a4SShri Abhyankar         tmp = mycols[nzcount++]/bs;
34434683f7a4SShri Abhyankar         if (!mask[tmp]) {
34444683f7a4SShri Abhyankar           mask[tmp] = 1;
34454683f7a4SShri Abhyankar           if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp;
34464683f7a4SShri Abhyankar           else masked1[dcount++] = tmp;
34474683f7a4SShri Abhyankar         }
34484683f7a4SShri Abhyankar       }
34494683f7a4SShri Abhyankar       rowcount++;
34504683f7a4SShri Abhyankar     }
34514683f7a4SShri Abhyankar 
34524683f7a4SShri Abhyankar     dlens[i]  = dcount;
34534683f7a4SShri Abhyankar     odlens[i] = odcount;
34544683f7a4SShri Abhyankar 
34554683f7a4SShri Abhyankar     /* zero out the mask elements we set */
34564683f7a4SShri Abhyankar     for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
34574683f7a4SShri Abhyankar     for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
34584683f7a4SShri Abhyankar   }
34594683f7a4SShri Abhyankar 
34604683f7a4SShri Abhyankar 
34614683f7a4SShri Abhyankar   if (!sizesset) {
34624683f7a4SShri Abhyankar     ierr = MatSetSizes(newmat,m,m,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
34634683f7a4SShri Abhyankar   }
34644683f7a4SShri Abhyankar   ierr = MatMPIBAIJSetPreallocation(newmat,bs,0,dlens,0,odlens);CHKERRQ(ierr);
34654683f7a4SShri Abhyankar 
34664683f7a4SShri Abhyankar   if (!rank) {
34674683f7a4SShri Abhyankar     ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&buf);CHKERRQ(ierr);
34684683f7a4SShri Abhyankar     /* read in my part of the matrix numerical values  */
34694683f7a4SShri Abhyankar     nz     = procsnz[0];
34704683f7a4SShri Abhyankar     vals   = buf;
34714683f7a4SShri Abhyankar     mycols = ibuf;
34724683f7a4SShri Abhyankar     if (size == 1) nz -= extra_rows;
34734683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
347426fbe8dcSKarl Rupp     if (size == 1) {
347526fbe8dcSKarl Rupp       for (i=0; i< extra_rows; i++) vals[nz+i] = 1.0;
347626fbe8dcSKarl Rupp     }
34774683f7a4SShri Abhyankar 
34784683f7a4SShri Abhyankar     /* insert into matrix */
34794683f7a4SShri Abhyankar     jj = rstart*bs;
34804683f7a4SShri Abhyankar     for (i=0; i<m; i++) {
34814683f7a4SShri Abhyankar       ierr    = MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);CHKERRQ(ierr);
34824683f7a4SShri Abhyankar       mycols += locrowlens[i];
34834683f7a4SShri Abhyankar       vals   += locrowlens[i];
34844683f7a4SShri Abhyankar       jj++;
34854683f7a4SShri Abhyankar     }
34864683f7a4SShri Abhyankar     /* read in other processors (except the last one) and ship out */
34874683f7a4SShri Abhyankar     for (i=1; i<size-1; i++) {
34884683f7a4SShri Abhyankar       nz   = procsnz[i];
34894683f7a4SShri Abhyankar       vals = buf;
34904683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
3491479e424cSMichael Lange       ierr = MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newmat)->tag,comm);CHKERRQ(ierr);
34924683f7a4SShri Abhyankar     }
34934683f7a4SShri Abhyankar     /* the last proc */
34944683f7a4SShri Abhyankar     if (size != 1) {
34954683f7a4SShri Abhyankar       nz   = procsnz[i] - extra_rows;
34964683f7a4SShri Abhyankar       vals = buf;
34974683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
34984683f7a4SShri Abhyankar       for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
3499479e424cSMichael Lange       ierr = MPIULong_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,((PetscObject)newmat)->tag,comm);CHKERRQ(ierr);
35004683f7a4SShri Abhyankar     }
35014683f7a4SShri Abhyankar     ierr = PetscFree(procsnz);CHKERRQ(ierr);
35024683f7a4SShri Abhyankar   } else {
35034683f7a4SShri Abhyankar     /* receive numeric values */
35044683f7a4SShri Abhyankar     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&buf);CHKERRQ(ierr);
35054683f7a4SShri Abhyankar 
35064683f7a4SShri Abhyankar     /* receive message of values*/
35074683f7a4SShri Abhyankar     vals   = buf;
35084683f7a4SShri Abhyankar     mycols = ibuf;
3509479e424cSMichael Lange     ierr   = MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newmat)->tag,comm);CHKERRQ(ierr);
35104683f7a4SShri Abhyankar 
35114683f7a4SShri Abhyankar     /* insert into matrix */
35124683f7a4SShri Abhyankar     jj = rstart*bs;
35134683f7a4SShri Abhyankar     for (i=0; i<m; i++) {
35144683f7a4SShri Abhyankar       ierr    = MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);CHKERRQ(ierr);
35154683f7a4SShri Abhyankar       mycols += locrowlens[i];
35164683f7a4SShri Abhyankar       vals   += locrowlens[i];
35174683f7a4SShri Abhyankar       jj++;
35184683f7a4SShri Abhyankar     }
35194683f7a4SShri Abhyankar   }
35204683f7a4SShri Abhyankar   ierr = PetscFree(locrowlens);CHKERRQ(ierr);
35214683f7a4SShri Abhyankar   ierr = PetscFree(buf);CHKERRQ(ierr);
35224683f7a4SShri Abhyankar   ierr = PetscFree(ibuf);CHKERRQ(ierr);
35234683f7a4SShri Abhyankar   ierr = PetscFree2(rowners,browners);CHKERRQ(ierr);
35244683f7a4SShri Abhyankar   ierr = PetscFree2(dlens,odlens);CHKERRQ(ierr);
35254683f7a4SShri Abhyankar   ierr = PetscFree3(mask,masked1,masked2);CHKERRQ(ierr);
35264683f7a4SShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35274683f7a4SShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35284683f7a4SShri Abhyankar   PetscFunctionReturn(0);
35294683f7a4SShri Abhyankar }
35304683f7a4SShri Abhyankar 
35314683f7a4SShri Abhyankar #undef __FUNCT__
35324a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJSetHashTableFactor"
3533133cdb44SSatish Balay /*@
3534133cdb44SSatish Balay    MatMPIBAIJSetHashTableFactor - Sets the factor required to compute the size of the HashTable.
3535133cdb44SSatish Balay 
3536133cdb44SSatish Balay    Input Parameters:
3537133cdb44SSatish Balay .  mat  - the matrix
3538133cdb44SSatish Balay .  fact - factor
3539133cdb44SSatish Balay 
3540c5eb9154SBarry Smith    Not Collective, each process can use a different factor
3541fee21e36SBarry Smith 
35428c890885SBarry Smith    Level: advanced
35438c890885SBarry Smith 
3544133cdb44SSatish Balay   Notes:
35458c07d4e3SBarry Smith    This can also be set by the command line option: -mat_use_hash_table <fact>
3546133cdb44SSatish Balay 
3547133cdb44SSatish Balay .keywords: matrix, hashtable, factor, HT
3548133cdb44SSatish Balay 
3549133cdb44SSatish Balay .seealso: MatSetOption()
3550133cdb44SSatish Balay @*/
35517087cfbeSBarry Smith PetscErrorCode  MatMPIBAIJSetHashTableFactor(Mat mat,PetscReal fact)
3552133cdb44SSatish Balay {
35534ac538c5SBarry Smith   PetscErrorCode ierr;
35545bf65638SKris Buschelman 
35555bf65638SKris Buschelman   PetscFunctionBegin;
35564ac538c5SBarry Smith   ierr = PetscTryMethod(mat,"MatSetHashTableFactor_C",(Mat,PetscReal),(mat,fact));CHKERRQ(ierr);
35575bf65638SKris Buschelman   PetscFunctionReturn(0);
35585bf65638SKris Buschelman }
35595bf65638SKris Buschelman 
35605bf65638SKris Buschelman #undef __FUNCT__
35615bf65638SKris Buschelman #define __FUNCT__ "MatSetHashTableFactor_MPIBAIJ"
35627087cfbeSBarry Smith PetscErrorCode  MatSetHashTableFactor_MPIBAIJ(Mat mat,PetscReal fact)
35635bf65638SKris Buschelman {
356425fdafccSSatish Balay   Mat_MPIBAIJ *baij;
3565133cdb44SSatish Balay 
3566133cdb44SSatish Balay   PetscFunctionBegin;
3567133cdb44SSatish Balay   baij          = (Mat_MPIBAIJ*)mat->data;
3568133cdb44SSatish Balay   baij->ht_fact = fact;
3569133cdb44SSatish Balay   PetscFunctionReturn(0);
3570133cdb44SSatish Balay }
3571f2a5309cSSatish Balay 
35724a2ae208SSatish Balay #undef __FUNCT__
35734a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJGetSeqBAIJ"
35749230625dSJed Brown PetscErrorCode  MatMPIBAIJGetSeqBAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[])
3575f2a5309cSSatish Balay {
3576f2a5309cSSatish Balay   Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
35775fd66863SKarl Rupp 
3578f2a5309cSSatish Balay   PetscFunctionBegin;
3579f2a5309cSSatish Balay   *Ad     = a->A;
3580f2a5309cSSatish Balay   *Ao     = a->B;
3581195d93cdSBarry Smith   *colmap = a->garray;
3582f2a5309cSSatish Balay   PetscFunctionReturn(0);
3583f2a5309cSSatish Balay }
358485535b8eSBarry Smith 
358585535b8eSBarry Smith /*
358685535b8eSBarry Smith     Special version for direct calls from Fortran (to eliminate two function call overheads
358785535b8eSBarry Smith */
358885535b8eSBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
358985535b8eSBarry Smith #define matmpibaijsetvaluesblocked_ MATMPIBAIJSETVALUESBLOCKED
359085535b8eSBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
359185535b8eSBarry Smith #define matmpibaijsetvaluesblocked_ matmpibaijsetvaluesblocked
359285535b8eSBarry Smith #endif
359385535b8eSBarry Smith 
359485535b8eSBarry Smith #undef __FUNCT__
359585535b8eSBarry Smith #define __FUNCT__ "matmpibiajsetvaluesblocked"
359685535b8eSBarry Smith /*@C
359785535b8eSBarry Smith   MatMPIBAIJSetValuesBlocked - Direct Fortran call to replace call to MatSetValuesBlocked()
359885535b8eSBarry Smith 
359985535b8eSBarry Smith   Collective on Mat
360085535b8eSBarry Smith 
360185535b8eSBarry Smith   Input Parameters:
360285535b8eSBarry Smith + mat - the matrix
360385535b8eSBarry Smith . min - number of input rows
360485535b8eSBarry Smith . im - input rows
360585535b8eSBarry Smith . nin - number of input columns
360685535b8eSBarry Smith . in - input columns
360785535b8eSBarry Smith . v - numerical values input
360885535b8eSBarry Smith - addvin - INSERT_VALUES or ADD_VALUES
360985535b8eSBarry Smith 
361085535b8eSBarry Smith   Notes: This has a complete copy of MatSetValuesBlocked_MPIBAIJ() which is terrible code un-reuse.
361185535b8eSBarry Smith 
361285535b8eSBarry Smith   Level: advanced
361385535b8eSBarry Smith 
361485535b8eSBarry Smith .seealso:   MatSetValuesBlocked()
361585535b8eSBarry Smith @*/
361685535b8eSBarry Smith PetscErrorCode matmpibaijsetvaluesblocked_(Mat *matin,PetscInt *min,const PetscInt im[],PetscInt *nin,const PetscInt in[],const MatScalar v[],InsertMode *addvin)
361785535b8eSBarry Smith {
361885535b8eSBarry Smith   /* convert input arguments to C version */
361985535b8eSBarry Smith   Mat        mat  = *matin;
362085535b8eSBarry Smith   PetscInt   m    = *min, n = *nin;
362185535b8eSBarry Smith   InsertMode addv = *addvin;
362285535b8eSBarry Smith 
362385535b8eSBarry Smith   Mat_MPIBAIJ     *baij = (Mat_MPIBAIJ*)mat->data;
362485535b8eSBarry Smith   const MatScalar *value;
362585535b8eSBarry Smith   MatScalar       *barray     = baij->barray;
3626ace3abfcSBarry Smith   PetscBool       roworiented = baij->roworiented;
362785535b8eSBarry Smith   PetscErrorCode  ierr;
362885535b8eSBarry Smith   PetscInt        i,j,ii,jj,row,col,rstart=baij->rstartbs;
362985535b8eSBarry Smith   PetscInt        rend=baij->rendbs,cstart=baij->cstartbs,stepval;
3630d0f46423SBarry Smith   PetscInt        cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;
363185535b8eSBarry Smith 
363285535b8eSBarry Smith   PetscFunctionBegin;
363385535b8eSBarry Smith   /* tasks normally handled by MatSetValuesBlocked() */
363426fbe8dcSKarl Rupp   if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv;
363585535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
3636e7e72b3dSBarry Smith   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
3637e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
363885535b8eSBarry Smith #endif
363985535b8eSBarry Smith   if (mat->assembled) {
364085535b8eSBarry Smith     mat->was_assembled = PETSC_TRUE;
364185535b8eSBarry Smith     mat->assembled     = PETSC_FALSE;
364285535b8eSBarry Smith   }
364385535b8eSBarry Smith   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
364485535b8eSBarry Smith 
364585535b8eSBarry Smith 
364685535b8eSBarry Smith   if (!barray) {
364785535b8eSBarry Smith     ierr         = PetscMalloc(bs2*sizeof(MatScalar),&barray);CHKERRQ(ierr);
364885535b8eSBarry Smith     baij->barray = barray;
364985535b8eSBarry Smith   }
365085535b8eSBarry Smith 
365126fbe8dcSKarl Rupp   if (roworiented) stepval = (n-1)*bs;
365226fbe8dcSKarl Rupp   else stepval = (m-1)*bs;
365326fbe8dcSKarl Rupp 
365485535b8eSBarry Smith   for (i=0; i<m; i++) {
365585535b8eSBarry Smith     if (im[i] < 0) continue;
365685535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
3657e32f2f54SBarry 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);
365885535b8eSBarry Smith #endif
365985535b8eSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
366085535b8eSBarry Smith       row = im[i] - rstart;
366185535b8eSBarry Smith       for (j=0; j<n; j++) {
366285535b8eSBarry Smith         /* If NumCol = 1 then a copy is not required */
366385535b8eSBarry Smith         if ((roworiented) && (n == 1)) {
366485535b8eSBarry Smith           barray = (MatScalar*)v + i*bs2;
366585535b8eSBarry Smith         } else if ((!roworiented) && (m == 1)) {
366685535b8eSBarry Smith           barray = (MatScalar*)v + j*bs2;
366785535b8eSBarry Smith         } else { /* Here a copy is required */
366885535b8eSBarry Smith           if (roworiented) {
366985535b8eSBarry Smith             value = v + i*(stepval+bs)*bs + j*bs;
367085535b8eSBarry Smith           } else {
367185535b8eSBarry Smith             value = v + j*(stepval+bs)*bs + i*bs;
367285535b8eSBarry Smith           }
367385535b8eSBarry Smith           for (ii=0; ii<bs; ii++,value+=stepval) {
367485535b8eSBarry Smith             for (jj=0; jj<bs; jj++) {
367585535b8eSBarry Smith               *barray++ = *value++;
367685535b8eSBarry Smith             }
367785535b8eSBarry Smith           }
367885535b8eSBarry Smith           barray -=bs2;
367985535b8eSBarry Smith         }
368085535b8eSBarry Smith 
368185535b8eSBarry Smith         if (in[j] >= cstart && in[j] < cend) {
368285535b8eSBarry Smith           col  = in[j] - cstart;
368397e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
368426fbe8dcSKarl Rupp         } else if (in[j] < 0) continue;
368585535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
3686cb9801acSJed Brown         else if (in[j] >= baij->Nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large, col %D max %D",in[j],baij->Nbs-1);
368785535b8eSBarry Smith #endif
368885535b8eSBarry Smith         else {
368985535b8eSBarry Smith           if (mat->was_assembled) {
369085535b8eSBarry Smith             if (!baij->colmap) {
3691ab9863d7SBarry Smith               ierr = MatCreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
369285535b8eSBarry Smith             }
369385535b8eSBarry Smith 
369485535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
369585535b8eSBarry Smith #if defined(PETSC_USE_CTABLE)
369685535b8eSBarry Smith             { PetscInt data;
369785535b8eSBarry Smith               ierr = PetscTableFind(baij->colmap,in[j]+1,&data);CHKERRQ(ierr);
3698e32f2f54SBarry Smith               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
369985535b8eSBarry Smith             }
370085535b8eSBarry Smith #else
3701e32f2f54SBarry Smith             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
370285535b8eSBarry Smith #endif
370385535b8eSBarry Smith #endif
370485535b8eSBarry Smith #if defined(PETSC_USE_CTABLE)
370585535b8eSBarry Smith             ierr = PetscTableFind(baij->colmap,in[j]+1,&col);CHKERRQ(ierr);
370685535b8eSBarry Smith             col  = (col - 1)/bs;
370785535b8eSBarry Smith #else
370885535b8eSBarry Smith             col = (baij->colmap[in[j]] - 1)/bs;
370985535b8eSBarry Smith #endif
371085535b8eSBarry Smith             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
3711ab9863d7SBarry Smith               ierr = MatDisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
371285535b8eSBarry Smith               col  =  in[j];
371385535b8eSBarry Smith             }
371426fbe8dcSKarl Rupp           } else col = in[j];
371597e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
371685535b8eSBarry Smith         }
371785535b8eSBarry Smith       }
371885535b8eSBarry Smith     } else {
371985535b8eSBarry Smith       if (!baij->donotstash) {
372085535b8eSBarry Smith         if (roworiented) {
372185535b8eSBarry Smith           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
372285535b8eSBarry Smith         } else {
372385535b8eSBarry Smith           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
372485535b8eSBarry Smith         }
372585535b8eSBarry Smith       }
372685535b8eSBarry Smith     }
372785535b8eSBarry Smith   }
372885535b8eSBarry Smith 
372985535b8eSBarry Smith   /* task normally handled by MatSetValuesBlocked() */
373085535b8eSBarry Smith   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
373185535b8eSBarry Smith   PetscFunctionReturn(0);
373285535b8eSBarry Smith }
3733dfb205c3SBarry Smith 
3734dfb205c3SBarry Smith #undef __FUNCT__
3735dfb205c3SBarry Smith #define __FUNCT__ "MatCreateMPIBAIJWithArrays"
3736dfb205c3SBarry Smith /*@
3737dfb205c3SBarry Smith      MatCreateMPIBAIJWithArrays - creates a MPI BAIJ matrix using arrays that contain in standard
3738dfb205c3SBarry Smith          CSR format the local rows.
3739dfb205c3SBarry Smith 
3740dfb205c3SBarry Smith    Collective on MPI_Comm
3741dfb205c3SBarry Smith 
3742dfb205c3SBarry Smith    Input Parameters:
3743dfb205c3SBarry Smith +  comm - MPI communicator
3744dfb205c3SBarry Smith .  bs - the block size, only a block size of 1 is supported
3745dfb205c3SBarry Smith .  m - number of local rows (Cannot be PETSC_DECIDE)
3746dfb205c3SBarry Smith .  n - This value should be the same as the local size used in creating the
3747dfb205c3SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
3748dfb205c3SBarry Smith        calculated if N is given) For square matrices n is almost always m.
3749dfb205c3SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3750dfb205c3SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
3751dfb205c3SBarry Smith .   i - row indices
3752dfb205c3SBarry Smith .   j - column indices
3753dfb205c3SBarry Smith -   a - matrix values
3754dfb205c3SBarry Smith 
3755dfb205c3SBarry Smith    Output Parameter:
3756dfb205c3SBarry Smith .   mat - the matrix
3757dfb205c3SBarry Smith 
3758dfb205c3SBarry Smith    Level: intermediate
3759dfb205c3SBarry Smith 
3760dfb205c3SBarry Smith    Notes:
3761dfb205c3SBarry Smith        The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
3762dfb205c3SBarry Smith      thus you CANNOT change the matrix entries by changing the values of a[] after you have
3763dfb205c3SBarry Smith      called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
3764dfb205c3SBarry Smith 
3765dfb205c3SBarry Smith        The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
3766dfb205c3SBarry Smith 
3767dfb205c3SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
3768dfb205c3SBarry Smith 
3769dfb205c3SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
377069b1f4b7SBarry Smith           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
3771dfb205c3SBarry Smith @*/
37727087cfbeSBarry Smith PetscErrorCode  MatCreateMPIBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat)
3773dfb205c3SBarry Smith {
3774dfb205c3SBarry Smith   PetscErrorCode ierr;
3775dfb205c3SBarry Smith 
3776dfb205c3SBarry Smith   PetscFunctionBegin;
3777f23aa3ddSBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
3778dfb205c3SBarry Smith   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
3779dfb205c3SBarry Smith   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3780dfb205c3SBarry Smith   ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr);
3781dfb205c3SBarry Smith   ierr = MatSetType(*mat,MATMPISBAIJ);CHKERRQ(ierr);
3782dfb205c3SBarry Smith   ierr = MatMPIBAIJSetPreallocationCSR(*mat,bs,i,j,a);CHKERRQ(ierr);
3783dfb205c3SBarry Smith   PetscFunctionReturn(0);
3784dfb205c3SBarry Smith }
3785