xref: /petsc/src/mat/impls/baij/mpi/mpibaij.c (revision ce94432eddcd14845bc7e8083b7f8ea723b9bf7d)
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 
527fc3c18eSBarry Smith EXTERN_C_BEGIN
534a2ae208SSatish Balay #undef __FUNCT__
544a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIBAIJ"
557087cfbeSBarry Smith PetscErrorCode  MatStoreValues_MPIBAIJ(Mat mat)
567fc3c18eSBarry Smith {
577fc3c18eSBarry Smith   Mat_MPIBAIJ    *aij = (Mat_MPIBAIJ*)mat->data;
58dfbe8321SBarry Smith   PetscErrorCode ierr;
597fc3c18eSBarry Smith 
607fc3c18eSBarry Smith   PetscFunctionBegin;
617fc3c18eSBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
627fc3c18eSBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
637fc3c18eSBarry Smith   PetscFunctionReturn(0);
647fc3c18eSBarry Smith }
657fc3c18eSBarry Smith EXTERN_C_END
667fc3c18eSBarry Smith 
677fc3c18eSBarry Smith EXTERN_C_BEGIN
684a2ae208SSatish Balay #undef __FUNCT__
694a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIBAIJ"
707087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_MPIBAIJ(Mat mat)
717fc3c18eSBarry Smith {
727fc3c18eSBarry Smith   Mat_MPIBAIJ    *aij = (Mat_MPIBAIJ*)mat->data;
73dfbe8321SBarry Smith   PetscErrorCode ierr;
747fc3c18eSBarry Smith 
757fc3c18eSBarry Smith   PetscFunctionBegin;
767fc3c18eSBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
777fc3c18eSBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
787fc3c18eSBarry Smith   PetscFunctionReturn(0);
797fc3c18eSBarry Smith }
807fc3c18eSBarry Smith EXTERN_C_END
817fc3c18eSBarry Smith 
82537820f0SBarry Smith /*
83537820f0SBarry Smith      Local utility routine that creates a mapping from the global column
8457b952d6SSatish Balay    number to the local number in the off-diagonal part of the local
85e06f6af7SJed Brown    storage of the matrix.  This is done in a non scalable way since the
8657b952d6SSatish Balay    length of colmap equals the global matrix length.
8757b952d6SSatish Balay */
884a2ae208SSatish Balay #undef __FUNCT__
89ab9863d7SBarry Smith #define __FUNCT__ "MatCreateColmap_MPIBAIJ_Private"
90ab9863d7SBarry Smith PetscErrorCode MatCreateColmap_MPIBAIJ_Private(Mat mat)
9157b952d6SSatish Balay {
9257b952d6SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
9357b952d6SSatish Balay   Mat_SeqBAIJ    *B    = (Mat_SeqBAIJ*)baij->B->data;
946849ba73SBarry Smith   PetscErrorCode ierr;
95d0f46423SBarry Smith   PetscInt       nbs = B->nbs,i,bs=mat->rmap->bs;
9657b952d6SSatish Balay 
97d64ed03dSBarry Smith   PetscFunctionBegin;
98aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
99e23dfa41SBarry Smith   ierr = PetscTableCreate(baij->nbs,baij->Nbs+1,&baij->colmap);CHKERRQ(ierr);
10048e59246SSatish Balay   for (i=0; i<nbs; i++) {
1013861aac3SJed Brown     ierr = PetscTableAdd(baij->colmap,baij->garray[i]+1,i*bs+1,INSERT_VALUES);CHKERRQ(ierr);
10248e59246SSatish Balay   }
10348e59246SSatish Balay #else
104b24ad042SBarry Smith   ierr = PetscMalloc((baij->Nbs+1)*sizeof(PetscInt),&baij->colmap);CHKERRQ(ierr);
10552e6d16bSBarry Smith   ierr = PetscLogObjectMemory(mat,baij->Nbs*sizeof(PetscInt));CHKERRQ(ierr);
106b24ad042SBarry Smith   ierr = PetscMemzero(baij->colmap,baij->Nbs*sizeof(PetscInt));CHKERRQ(ierr);
107928fc39bSSatish Balay   for (i=0; i<nbs; i++) baij->colmap[baij->garray[i]] = i*bs+1;
10848e59246SSatish Balay #endif
1093a40ed3dSBarry Smith   PetscFunctionReturn(0);
11057b952d6SSatish Balay }
11157b952d6SSatish Balay 
112f5e9677aSSatish Balay #define  MatSetValues_SeqBAIJ_A_Private(row,col,value,addv) \
11380c1aa95SSatish Balay   { \
11480c1aa95SSatish Balay  \
11580c1aa95SSatish Balay     brow = row/bs;  \
11680c1aa95SSatish Balay     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow]; \
117ac7a638eSSatish Balay     rmax = aimax[brow]; nrow = ailen[brow]; \
11880c1aa95SSatish Balay     bcol = col/bs; \
11980c1aa95SSatish Balay     ridx = row % bs; cidx = col % bs; \
120ab26458aSBarry Smith     low  = 0; high = nrow; \
121ab26458aSBarry Smith     while (high-low > 3) { \
122ab26458aSBarry Smith       t = (low+high)/2; \
123ab26458aSBarry Smith       if (rp[t] > bcol) high = t; \
124ab26458aSBarry Smith       else              low  = t; \
125ab26458aSBarry Smith     } \
126ab26458aSBarry Smith     for (_i=low; _i<high; _i++) { \
12780c1aa95SSatish Balay       if (rp[_i] > bcol) break; \
12880c1aa95SSatish Balay       if (rp[_i] == bcol) { \
12980c1aa95SSatish Balay         bap = ap +  bs2*_i + bs*cidx + ridx; \
130eada6651SSatish Balay         if (addv == ADD_VALUES) *bap += value;  \
131eada6651SSatish Balay         else                    *bap  = value;  \
132ac7a638eSSatish Balay         goto a_noinsert; \
13380c1aa95SSatish Balay       } \
13480c1aa95SSatish Balay     } \
13589280ab3SLois Curfman McInnes     if (a->nonew == 1) goto a_noinsert; \
136e32f2f54SBarry Smith     if (a->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
137fef13f97SBarry Smith     MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,aimax,a->nonew,MatScalar); \
13880c1aa95SSatish Balay     N = nrow++ - 1;  \
13980c1aa95SSatish Balay     /* shift up all the later entries in this row */ \
14080c1aa95SSatish Balay     for (ii=N; ii>=_i; ii--) { \
14180c1aa95SSatish Balay       rp[ii+1] = rp[ii]; \
1423eda8832SBarry Smith       ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr); \
14380c1aa95SSatish Balay     } \
1443eda8832SBarry Smith     if (N>=_i) { ierr = PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));CHKERRQ(ierr); }  \
14580c1aa95SSatish Balay     rp[_i]                      = bcol;  \
14680c1aa95SSatish Balay     ap[bs2*_i + bs*cidx + ridx] = value;  \
147ac7a638eSSatish Balay a_noinsert:; \
14880c1aa95SSatish Balay     ailen[brow] = nrow; \
14980c1aa95SSatish Balay   }
15057b952d6SSatish Balay 
151ac7a638eSSatish Balay #define  MatSetValues_SeqBAIJ_B_Private(row,col,value,addv) \
152ac7a638eSSatish Balay   { \
153ac7a638eSSatish Balay     brow = row/bs;  \
154ac7a638eSSatish Balay     rp   = bj + bi[brow]; ap = ba + bs2*bi[brow]; \
155ac7a638eSSatish Balay     rmax = bimax[brow]; nrow = bilen[brow]; \
156ac7a638eSSatish Balay     bcol = col/bs; \
157ac7a638eSSatish Balay     ridx = row % bs; cidx = col % bs; \
158ac7a638eSSatish Balay     low  = 0; high = nrow; \
159ac7a638eSSatish Balay     while (high-low > 3) { \
160ac7a638eSSatish Balay       t = (low+high)/2; \
161ac7a638eSSatish Balay       if (rp[t] > bcol) high = t; \
162ac7a638eSSatish Balay       else              low  = t; \
163ac7a638eSSatish Balay     } \
164ac7a638eSSatish Balay     for (_i=low; _i<high; _i++) { \
165ac7a638eSSatish Balay       if (rp[_i] > bcol) break; \
166ac7a638eSSatish Balay       if (rp[_i] == bcol) { \
167ac7a638eSSatish Balay         bap = ap +  bs2*_i + bs*cidx + ridx; \
168ac7a638eSSatish Balay         if (addv == ADD_VALUES) *bap += value;  \
169ac7a638eSSatish Balay         else                    *bap  = value;  \
170ac7a638eSSatish Balay         goto b_noinsert; \
171ac7a638eSSatish Balay       } \
172ac7a638eSSatish Balay     } \
17389280ab3SLois Curfman McInnes     if (b->nonew == 1) goto b_noinsert; \
174e32f2f54SBarry Smith     if (b->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
175fef13f97SBarry Smith     MatSeqXAIJReallocateAIJ(B,b->mbs,bs2,nrow,brow,bcol,rmax,ba,bi,bj,rp,ap,bimax,b->nonew,MatScalar); \
176085a36d4SBarry Smith     CHKMEMQ; \
177ac7a638eSSatish Balay     N = nrow++ - 1;  \
178ac7a638eSSatish Balay     /* shift up all the later entries in this row */ \
179ac7a638eSSatish Balay     for (ii=N; ii>=_i; ii--) { \
180ac7a638eSSatish Balay       rp[ii+1] = rp[ii]; \
1813eda8832SBarry Smith       ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr); \
182ac7a638eSSatish Balay     } \
1833eda8832SBarry Smith     if (N>=_i) { ierr = PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));CHKERRQ(ierr);}  \
184ac7a638eSSatish Balay     rp[_i]                      = bcol;  \
185ac7a638eSSatish Balay     ap[bs2*_i + bs*cidx + ridx] = value;  \
186ac7a638eSSatish Balay b_noinsert:; \
187ac7a638eSSatish Balay     bilen[brow] = nrow; \
188ac7a638eSSatish Balay   }
189ac7a638eSSatish Balay 
1904a2ae208SSatish Balay #undef __FUNCT__
1914a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIBAIJ"
192b24ad042SBarry Smith PetscErrorCode MatSetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
19357b952d6SSatish Balay {
19457b952d6SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
19593fea6afSBarry Smith   MatScalar      value;
196ace3abfcSBarry Smith   PetscBool      roworiented = baij->roworiented;
197dfbe8321SBarry Smith   PetscErrorCode ierr;
198b24ad042SBarry Smith   PetscInt       i,j,row,col;
199d0f46423SBarry Smith   PetscInt       rstart_orig=mat->rmap->rstart;
200d0f46423SBarry Smith   PetscInt       rend_orig  =mat->rmap->rend,cstart_orig=mat->cmap->rstart;
201d0f46423SBarry Smith   PetscInt       cend_orig  =mat->cmap->rend,bs=mat->rmap->bs;
20257b952d6SSatish Balay 
203eada6651SSatish Balay   /* Some Variables required in the macro */
20480c1aa95SSatish Balay   Mat         A     = baij->A;
20580c1aa95SSatish Balay   Mat_SeqBAIJ *a    = (Mat_SeqBAIJ*)(A)->data;
206b24ad042SBarry Smith   PetscInt    *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j;
2073eda8832SBarry Smith   MatScalar   *aa   =a->a;
208ac7a638eSSatish Balay 
209ac7a638eSSatish Balay   Mat         B     = baij->B;
210ac7a638eSSatish Balay   Mat_SeqBAIJ *b    = (Mat_SeqBAIJ*)(B)->data;
211b24ad042SBarry Smith   PetscInt    *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j;
2123eda8832SBarry Smith   MatScalar   *ba   =b->a;
213ac7a638eSSatish Balay 
214b24ad042SBarry Smith   PetscInt  *rp,ii,nrow,_i,rmax,N,brow,bcol;
215b24ad042SBarry Smith   PetscInt  low,high,t,ridx,cidx,bs2=a->bs2;
2163eda8832SBarry Smith   MatScalar *ap,*bap;
21780c1aa95SSatish Balay 
218d64ed03dSBarry Smith   PetscFunctionBegin;
21971fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
22057b952d6SSatish Balay   for (i=0; i<m; i++) {
2215ef9f2a5SBarry Smith     if (im[i] < 0) continue;
2222515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
223e32f2f54SBarry Smith     if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1);
224639f9d9dSBarry Smith #endif
22557b952d6SSatish Balay     if (im[i] >= rstart_orig && im[i] < rend_orig) {
22657b952d6SSatish Balay       row = im[i] - rstart_orig;
22757b952d6SSatish Balay       for (j=0; j<n; j++) {
22857b952d6SSatish Balay         if (in[j] >= cstart_orig && in[j] < cend_orig) {
22957b952d6SSatish Balay           col = in[j] - cstart_orig;
230db4deed7SKarl Rupp           if (roworiented) value = v[i*n+j];
231db4deed7SKarl Rupp           else             value = v[i+j*m];
232f5e9677aSSatish Balay           MatSetValues_SeqBAIJ_A_Private(row,col,value,addv);
23380c1aa95SSatish Balay           /* ierr = MatSetValues_SeqBAIJ(baij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
23473959e64SBarry Smith         } else if (in[j] < 0) continue;
2352515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
236660746e0SBarry 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);
237639f9d9dSBarry Smith #endif
23857b952d6SSatish Balay         else {
23957b952d6SSatish Balay           if (mat->was_assembled) {
240905e6a2fSBarry Smith             if (!baij->colmap) {
241ab9863d7SBarry Smith               ierr = MatCreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
242905e6a2fSBarry Smith             }
243aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
2440f5bd95cSBarry Smith             ierr = PetscTableFind(baij->colmap,in[j]/bs + 1,&col);CHKERRQ(ierr);
245bba1ac68SSatish Balay             col  = col - 1;
24648e59246SSatish Balay #else
247bba1ac68SSatish Balay             col = baij->colmap[in[j]/bs] - 1;
24848e59246SSatish Balay #endif
249c9ef50b2SBarry Smith             if (col < 0 && !((Mat_SeqBAIJ*)(baij->B->data))->nonew) {
250ab9863d7SBarry Smith               ierr = MatDisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
2518295de27SSatish Balay               col  =  in[j];
2529bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
2539bf004c3SSatish Balay               B    = baij->B;
2549bf004c3SSatish Balay               b    = (Mat_SeqBAIJ*)(B)->data;
2559bf004c3SSatish Balay               bimax=b->imax;bi=b->i;bilen=b->ilen;bj=b->j;
2569bf004c3SSatish Balay               ba   =b->a;
257c9ef50b2SBarry 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]);
258c9ef50b2SBarry Smith             else col += in[j]%bs;
2598295de27SSatish Balay           } else col = in[j];
260db4deed7SKarl Rupp           if (roworiented) value = v[i*n+j];
261db4deed7SKarl Rupp           else             value = v[i+j*m];
26290da58bdSSatish Balay           MatSetValues_SeqBAIJ_B_Private(row,col,value,addv);
26390da58bdSSatish Balay           /* ierr = MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
26457b952d6SSatish Balay         }
26557b952d6SSatish Balay       }
266d64ed03dSBarry Smith     } else {
2674cb17eb5SBarry 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]);
26890f02eecSBarry Smith       if (!baij->donotstash) {
2695080c13bSMatthew G Knepley         mat->assembled = PETSC_FALSE;
270ff2fd236SBarry Smith         if (roworiented) {
271b400d20cSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);CHKERRQ(ierr);
272ff2fd236SBarry Smith         } else {
273b400d20cSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);CHKERRQ(ierr);
27457b952d6SSatish Balay         }
27557b952d6SSatish Balay       }
27657b952d6SSatish Balay     }
27790f02eecSBarry Smith   }
2783a40ed3dSBarry Smith   PetscFunctionReturn(0);
27957b952d6SSatish Balay }
28057b952d6SSatish Balay 
2814a2ae208SSatish Balay #undef __FUNCT__
28297e5c40aSBarry Smith #define __FUNCT__ "MatSetValuesBlocked_MPIBAIJ"
28397e5c40aSBarry Smith PetscErrorCode MatSetValuesBlocked_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
284ab26458aSBarry Smith {
285ab26458aSBarry Smith   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
286dd6ea824SBarry Smith   const PetscScalar *value;
287f15d580aSBarry Smith   MatScalar         *barray     = baij->barray;
288ace3abfcSBarry Smith   PetscBool         roworiented = baij->roworiented;
289dfbe8321SBarry Smith   PetscErrorCode    ierr;
290899cda47SBarry Smith   PetscInt          i,j,ii,jj,row,col,rstart=baij->rstartbs;
291899cda47SBarry Smith   PetscInt          rend=baij->rendbs,cstart=baij->cstartbs,stepval;
292d0f46423SBarry Smith   PetscInt          cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;
293ab26458aSBarry Smith 
294b16ae2b1SBarry Smith   PetscFunctionBegin;
29530793edcSSatish Balay   if (!barray) {
29682502324SSatish Balay     ierr         = PetscMalloc(bs2*sizeof(MatScalar),&barray);CHKERRQ(ierr);
29782502324SSatish Balay     baij->barray = barray;
29830793edcSSatish Balay   }
29930793edcSSatish Balay 
30026fbe8dcSKarl Rupp   if (roworiented) stepval = (n-1)*bs;
30126fbe8dcSKarl Rupp   else stepval = (m-1)*bs;
30226fbe8dcSKarl Rupp 
303ab26458aSBarry Smith   for (i=0; i<m; i++) {
3045ef9f2a5SBarry Smith     if (im[i] < 0) continue;
3052515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
306e32f2f54SBarry 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);
307ab26458aSBarry Smith #endif
308ab26458aSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
309ab26458aSBarry Smith       row = im[i] - rstart;
310ab26458aSBarry Smith       for (j=0; j<n; j++) {
31115b57d14SSatish Balay         /* If NumCol = 1 then a copy is not required */
31215b57d14SSatish Balay         if ((roworiented) && (n == 1)) {
313f15d580aSBarry Smith           barray = (MatScalar*)v + i*bs2;
31415b57d14SSatish Balay         } else if ((!roworiented) && (m == 1)) {
315f15d580aSBarry Smith           barray = (MatScalar*)v + j*bs2;
31615b57d14SSatish Balay         } else { /* Here a copy is required */
317ab26458aSBarry Smith           if (roworiented) {
31853ef36baSBarry Smith             value = v + (i*(stepval+bs) + j)*bs;
319ab26458aSBarry Smith           } else {
32053ef36baSBarry Smith             value = v + (j*(stepval+bs) + i)*bs;
321abef11f7SSatish Balay           }
32253ef36baSBarry Smith           for (ii=0; ii<bs; ii++,value+=bs+stepval) {
32326fbe8dcSKarl Rupp             for (jj=0; jj<bs; jj++) barray[jj] = value[jj];
32453ef36baSBarry Smith             barray += bs;
32547513183SBarry Smith           }
32630793edcSSatish Balay           barray -= bs2;
32715b57d14SSatish Balay         }
328abef11f7SSatish Balay 
329abef11f7SSatish Balay         if (in[j] >= cstart && in[j] < cend) {
330abef11f7SSatish Balay           col  = in[j] - cstart;
33197e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
33226fbe8dcSKarl Rupp         } else if (in[j] < 0) continue;
3332515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
334cb9801acSJed 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);
335ab26458aSBarry Smith #endif
336ab26458aSBarry Smith         else {
337ab26458aSBarry Smith           if (mat->was_assembled) {
338ab26458aSBarry Smith             if (!baij->colmap) {
339ab9863d7SBarry Smith               ierr = MatCreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
340ab26458aSBarry Smith             }
341a5eb4965SSatish Balay 
3422515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
343aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
344b24ad042SBarry Smith             { PetscInt data;
3450f5bd95cSBarry Smith               ierr = PetscTableFind(baij->colmap,in[j]+1,&data);CHKERRQ(ierr);
346e32f2f54SBarry Smith               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
347fa46199cSSatish Balay             }
34848e59246SSatish Balay #else
349e32f2f54SBarry Smith             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
350a5eb4965SSatish Balay #endif
35148e59246SSatish Balay #endif
352aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
3530f5bd95cSBarry Smith             ierr = PetscTableFind(baij->colmap,in[j]+1,&col);CHKERRQ(ierr);
354fa46199cSSatish Balay             col  = (col - 1)/bs;
35548e59246SSatish Balay #else
356a5eb4965SSatish Balay             col = (baij->colmap[in[j]] - 1)/bs;
35748e59246SSatish Balay #endif
3580e9bae81SBarry Smith             if (col < 0 && !((Mat_SeqBAIJ*)(baij->B->data))->nonew) {
359ab9863d7SBarry Smith               ierr = MatDisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
360ab26458aSBarry Smith               col  =  in[j];
3610e9bae81SBarry 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]);
362db4deed7SKarl Rupp           } else col = in[j];
36397e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
364ab26458aSBarry Smith         }
365ab26458aSBarry Smith       }
366d64ed03dSBarry Smith     } else {
3674cb17eb5SBarry 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]);
368ab26458aSBarry Smith       if (!baij->donotstash) {
369ff2fd236SBarry Smith         if (roworiented) {
3706fa18ffdSBarry Smith           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
371ff2fd236SBarry Smith         } else {
3726fa18ffdSBarry Smith           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
373ff2fd236SBarry Smith         }
374abef11f7SSatish Balay       }
375ab26458aSBarry Smith     }
376ab26458aSBarry Smith   }
3773a40ed3dSBarry Smith   PetscFunctionReturn(0);
378ab26458aSBarry Smith }
3796fa18ffdSBarry Smith 
3800bdbc534SSatish Balay #define HASH_KEY 0.6180339887
381b24ad042SBarry Smith #define HASH(size,key,tmp) (tmp = (key)*HASH_KEY,(PetscInt)((size)*(tmp-(PetscInt)tmp)))
382b24ad042SBarry Smith /* #define HASH(size,key) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
383b24ad042SBarry Smith /* #define HASH(size,key,tmp) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
3844a2ae208SSatish Balay #undef __FUNCT__
38597e5c40aSBarry Smith #define __FUNCT__ "MatSetValues_MPIBAIJ_HT"
38697e5c40aSBarry Smith PetscErrorCode MatSetValues_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
3870bdbc534SSatish Balay {
3880bdbc534SSatish Balay   Mat_MPIBAIJ    *baij       = (Mat_MPIBAIJ*)mat->data;
389ace3abfcSBarry Smith   PetscBool      roworiented = baij->roworiented;
390dfbe8321SBarry Smith   PetscErrorCode ierr;
391b24ad042SBarry Smith   PetscInt       i,j,row,col;
392d0f46423SBarry Smith   PetscInt       rstart_orig=mat->rmap->rstart;
393d0f46423SBarry Smith   PetscInt       rend_orig  =mat->rmap->rend,Nbs=baij->Nbs;
394d0f46423SBarry Smith   PetscInt       h1,key,size=baij->ht_size,bs=mat->rmap->bs,*HT=baij->ht,idx;
395329f5518SBarry Smith   PetscReal      tmp;
3963eda8832SBarry Smith   MatScalar      **HD = baij->hd,value;
3972515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
398b24ad042SBarry Smith   PetscInt total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
3994a15367fSSatish Balay #endif
4000bdbc534SSatish Balay 
4010bdbc534SSatish Balay   PetscFunctionBegin;
40271fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
4030bdbc534SSatish Balay   for (i=0; i<m; i++) {
4042515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
405e32f2f54SBarry Smith     if (im[i] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
406e32f2f54SBarry 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);
4070bdbc534SSatish Balay #endif
4080bdbc534SSatish Balay     row = im[i];
409c2760754SSatish Balay     if (row >= rstart_orig && row < rend_orig) {
4100bdbc534SSatish Balay       for (j=0; j<n; j++) {
4110bdbc534SSatish Balay         col = in[j];
412db4deed7SKarl Rupp         if (roworiented) value = v[i*n+j];
413db4deed7SKarl Rupp         else             value = v[i+j*m];
414b24ad042SBarry Smith         /* Look up PetscInto the Hash Table */
415c2760754SSatish Balay         key = (row/bs)*Nbs+(col/bs)+1;
416c2760754SSatish Balay         h1  = HASH(size,key,tmp);
4170bdbc534SSatish Balay 
418c2760754SSatish Balay 
419c2760754SSatish Balay         idx = h1;
4202515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
421187ce0cbSSatish Balay         insert_ct++;
422187ce0cbSSatish Balay         total_ct++;
423187ce0cbSSatish Balay         if (HT[idx] != key) {
424187ce0cbSSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++) ;
425187ce0cbSSatish Balay           if (idx == size) {
426187ce0cbSSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++) ;
427f23aa3ddSBarry Smith             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
428187ce0cbSSatish Balay           }
429187ce0cbSSatish Balay         }
430187ce0cbSSatish Balay #else
431c2760754SSatish Balay         if (HT[idx] != key) {
432c2760754SSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++) ;
433c2760754SSatish Balay           if (idx == size) {
434c2760754SSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++) ;
435f23aa3ddSBarry Smith             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
436c2760754SSatish Balay           }
437c2760754SSatish Balay         }
438187ce0cbSSatish Balay #endif
439c2760754SSatish Balay         /* A HASH table entry is found, so insert the values at the correct address */
440c2760754SSatish Balay         if (addv == ADD_VALUES) *(HD[idx]+ (col % bs)*bs + (row % bs)) += value;
441c2760754SSatish Balay         else                    *(HD[idx]+ (col % bs)*bs + (row % bs))  = value;
4420bdbc534SSatish Balay       }
44326fbe8dcSKarl Rupp     } else if (!baij->donotstash) {
444ff2fd236SBarry Smith       if (roworiented) {
445b400d20cSBarry Smith         ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);CHKERRQ(ierr);
446ff2fd236SBarry Smith       } else {
447b400d20cSBarry Smith         ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);CHKERRQ(ierr);
4480bdbc534SSatish Balay       }
4490bdbc534SSatish Balay     }
4500bdbc534SSatish Balay   }
4512515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
452187ce0cbSSatish Balay   baij->ht_total_ct  = total_ct;
453187ce0cbSSatish Balay   baij->ht_insert_ct = insert_ct;
454187ce0cbSSatish Balay #endif
4550bdbc534SSatish Balay   PetscFunctionReturn(0);
4560bdbc534SSatish Balay }
4570bdbc534SSatish Balay 
4584a2ae208SSatish Balay #undef __FUNCT__
45997e5c40aSBarry Smith #define __FUNCT__ "MatSetValuesBlocked_MPIBAIJ_HT"
46097e5c40aSBarry Smith PetscErrorCode MatSetValuesBlocked_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
4610bdbc534SSatish Balay {
4620bdbc534SSatish Balay   Mat_MPIBAIJ       *baij       = (Mat_MPIBAIJ*)mat->data;
463ace3abfcSBarry Smith   PetscBool         roworiented = baij->roworiented;
464dfbe8321SBarry Smith   PetscErrorCode    ierr;
465b24ad042SBarry Smith   PetscInt          i,j,ii,jj,row,col;
466899cda47SBarry Smith   PetscInt          rstart=baij->rstartbs;
467d0f46423SBarry Smith   PetscInt          rend  =mat->rmap->rend,stepval,bs=mat->rmap->bs,bs2=baij->bs2,nbs2=n*bs2;
468b24ad042SBarry Smith   PetscInt          h1,key,size=baij->ht_size,idx,*HT=baij->ht,Nbs=baij->Nbs;
469329f5518SBarry Smith   PetscReal         tmp;
4703eda8832SBarry Smith   MatScalar         **HD = baij->hd,*baij_a;
471dd6ea824SBarry Smith   const PetscScalar *v_t,*value;
4722515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
473b24ad042SBarry Smith   PetscInt total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
4744a15367fSSatish Balay #endif
4750bdbc534SSatish Balay 
476d0a41580SSatish Balay   PetscFunctionBegin;
47726fbe8dcSKarl Rupp   if (roworiented) stepval = (n-1)*bs;
47826fbe8dcSKarl Rupp   else stepval = (m-1)*bs;
47926fbe8dcSKarl Rupp 
4800bdbc534SSatish Balay   for (i=0; i<m; i++) {
4812515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
482e32f2f54SBarry Smith     if (im[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",im[i]);
483e32f2f54SBarry 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);
4840bdbc534SSatish Balay #endif
4850bdbc534SSatish Balay     row = im[i];
486ab715e2cSSatish Balay     v_t = v + i*nbs2;
487c2760754SSatish Balay     if (row >= rstart && row < rend) {
4880bdbc534SSatish Balay       for (j=0; j<n; j++) {
4890bdbc534SSatish Balay         col = in[j];
4900bdbc534SSatish Balay 
4910bdbc534SSatish Balay         /* Look up into the Hash Table */
492c2760754SSatish Balay         key = row*Nbs+col+1;
493c2760754SSatish Balay         h1  = HASH(size,key,tmp);
4940bdbc534SSatish Balay 
495c2760754SSatish Balay         idx = h1;
4962515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
497187ce0cbSSatish Balay         total_ct++;
498187ce0cbSSatish Balay         insert_ct++;
499187ce0cbSSatish Balay         if (HT[idx] != key) {
500187ce0cbSSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++) ;
501187ce0cbSSatish Balay           if (idx == size) {
502187ce0cbSSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++) ;
503f23aa3ddSBarry Smith             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
504187ce0cbSSatish Balay           }
505187ce0cbSSatish Balay         }
506187ce0cbSSatish Balay #else
507c2760754SSatish Balay         if (HT[idx] != key) {
508c2760754SSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++) ;
509c2760754SSatish Balay           if (idx == size) {
510c2760754SSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++) ;
511f23aa3ddSBarry Smith             if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
512c2760754SSatish Balay           }
513c2760754SSatish Balay         }
514187ce0cbSSatish Balay #endif
515c2760754SSatish Balay         baij_a = HD[idx];
5160bdbc534SSatish Balay         if (roworiented) {
517c2760754SSatish Balay           /*value = v + i*(stepval+bs)*bs + j*bs;*/
518187ce0cbSSatish Balay           /* value = v + (i*(stepval+bs)+j)*bs; */
519187ce0cbSSatish Balay           value = v_t;
520187ce0cbSSatish Balay           v_t  += bs;
521fef45726SSatish Balay           if (addv == ADD_VALUES) {
522c2760754SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval) {
523c2760754SSatish Balay               for (jj=ii; jj<bs2; jj+=bs) {
524fef45726SSatish Balay                 baij_a[jj] += *value++;
525b4cc0f5aSSatish Balay               }
526b4cc0f5aSSatish Balay             }
527fef45726SSatish Balay           } else {
528c2760754SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval) {
529c2760754SSatish Balay               for (jj=ii; jj<bs2; jj+=bs) {
530fef45726SSatish Balay                 baij_a[jj] = *value++;
531fef45726SSatish Balay               }
532fef45726SSatish Balay             }
533fef45726SSatish Balay           }
5340bdbc534SSatish Balay         } else {
5350bdbc534SSatish Balay           value = v + j*(stepval+bs)*bs + i*bs;
536fef45726SSatish Balay           if (addv == ADD_VALUES) {
537b4cc0f5aSSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
5380bdbc534SSatish Balay               for (jj=0; jj<bs; jj++) {
539fef45726SSatish Balay                 baij_a[jj] += *value++;
540fef45726SSatish Balay               }
541fef45726SSatish Balay             }
542fef45726SSatish Balay           } else {
543fef45726SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
544fef45726SSatish Balay               for (jj=0; jj<bs; jj++) {
545fef45726SSatish Balay                 baij_a[jj] = *value++;
546fef45726SSatish Balay               }
547b4cc0f5aSSatish Balay             }
5480bdbc534SSatish Balay           }
5490bdbc534SSatish Balay         }
5500bdbc534SSatish Balay       }
5510bdbc534SSatish Balay     } else {
5520bdbc534SSatish Balay       if (!baij->donotstash) {
5530bdbc534SSatish Balay         if (roworiented) {
5548798bf22SSatish Balay           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
5550bdbc534SSatish Balay         } else {
5568798bf22SSatish Balay           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
5570bdbc534SSatish Balay         }
5580bdbc534SSatish Balay       }
5590bdbc534SSatish Balay     }
5600bdbc534SSatish Balay   }
5612515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
562187ce0cbSSatish Balay   baij->ht_total_ct  = total_ct;
563187ce0cbSSatish Balay   baij->ht_insert_ct = insert_ct;
564187ce0cbSSatish Balay #endif
5650bdbc534SSatish Balay   PetscFunctionReturn(0);
5660bdbc534SSatish Balay }
567133cdb44SSatish Balay 
5684a2ae208SSatish Balay #undef __FUNCT__
5694a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIBAIJ"
570b24ad042SBarry Smith PetscErrorCode MatGetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
571d6de1c52SSatish Balay {
572d6de1c52SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
5736849ba73SBarry Smith   PetscErrorCode ierr;
574d0f46423SBarry Smith   PetscInt       bs       = mat->rmap->bs,i,j,bsrstart = mat->rmap->rstart,bsrend = mat->rmap->rend;
575d0f46423SBarry Smith   PetscInt       bscstart = mat->cmap->rstart,bscend = mat->cmap->rend,row,col,data;
576d6de1c52SSatish Balay 
577133cdb44SSatish Balay   PetscFunctionBegin;
578d6de1c52SSatish Balay   for (i=0; i<m; i++) {
579e32f2f54SBarry Smith     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
580e32f2f54SBarry 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);
581d6de1c52SSatish Balay     if (idxm[i] >= bsrstart && idxm[i] < bsrend) {
582d6de1c52SSatish Balay       row = idxm[i] - bsrstart;
583d6de1c52SSatish Balay       for (j=0; j<n; j++) {
584e32f2f54SBarry Smith         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
585e32f2f54SBarry 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);
586d6de1c52SSatish Balay         if (idxn[j] >= bscstart && idxn[j] < bscend) {
587d6de1c52SSatish Balay           col  = idxn[j] - bscstart;
58898dd23e9SBarry Smith           ierr = MatGetValues_SeqBAIJ(baij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
589d64ed03dSBarry Smith         } else {
590905e6a2fSBarry Smith           if (!baij->colmap) {
591ab9863d7SBarry Smith             ierr = MatCreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
592905e6a2fSBarry Smith           }
593aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
5940f5bd95cSBarry Smith           ierr = PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);CHKERRQ(ierr);
595fa46199cSSatish Balay           data--;
59648e59246SSatish Balay #else
59748e59246SSatish Balay           data = baij->colmap[idxn[j]/bs]-1;
59848e59246SSatish Balay #endif
59948e59246SSatish Balay           if ((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0;
600d9d09a02SSatish Balay           else {
60148e59246SSatish Balay             col  = data + idxn[j]%bs;
60298dd23e9SBarry Smith             ierr = MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
603d6de1c52SSatish Balay           }
604d6de1c52SSatish Balay         }
605d6de1c52SSatish Balay       }
606f23aa3ddSBarry Smith     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
607d6de1c52SSatish Balay   }
6083a40ed3dSBarry Smith   PetscFunctionReturn(0);
609d6de1c52SSatish Balay }
610d6de1c52SSatish Balay 
6114a2ae208SSatish Balay #undef __FUNCT__
6124a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIBAIJ"
613dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIBAIJ(Mat mat,NormType type,PetscReal *nrm)
614d6de1c52SSatish Balay {
615d6de1c52SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
616d6de1c52SSatish Balay   Mat_SeqBAIJ    *amat = (Mat_SeqBAIJ*)baij->A->data,*bmat = (Mat_SeqBAIJ*)baij->B->data;
617dfbe8321SBarry Smith   PetscErrorCode ierr;
618d0f46423SBarry Smith   PetscInt       i,j,bs2=baij->bs2,bs=baij->A->rmap->bs,nz,row,col;
619329f5518SBarry Smith   PetscReal      sum = 0.0;
6203eda8832SBarry Smith   MatScalar      *v;
621d6de1c52SSatish Balay 
622d64ed03dSBarry Smith   PetscFunctionBegin;
623d6de1c52SSatish Balay   if (baij->size == 1) {
624064f8208SBarry Smith     ierr =  MatNorm(baij->A,type,nrm);CHKERRQ(ierr);
625d6de1c52SSatish Balay   } else {
626d6de1c52SSatish Balay     if (type == NORM_FROBENIUS) {
627d6de1c52SSatish Balay       v  = amat->a;
6288a62d963SHong Zhang       nz = amat->nz*bs2;
6298a62d963SHong Zhang       for (i=0; i<nz; i++) {
630329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
631d6de1c52SSatish Balay       }
632d6de1c52SSatish Balay       v  = bmat->a;
6338a62d963SHong Zhang       nz = bmat->nz*bs2;
6348a62d963SHong Zhang       for (i=0; i<nz; i++) {
635329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
636d6de1c52SSatish Balay       }
637*ce94432eSBarry Smith       ierr = MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
6388f1a2a5eSBarry Smith       *nrm = PetscSqrtReal(*nrm);
6398a62d963SHong Zhang     } else if (type == NORM_1) { /* max column sum */
6408a62d963SHong Zhang       PetscReal *tmp,*tmp2;
641899cda47SBarry Smith       PetscInt  *jj,*garray=baij->garray,cstart=baij->rstartbs;
642fca92195SBarry Smith       ierr = PetscMalloc2(mat->cmap->N,PetscReal,&tmp,mat->cmap->N,PetscReal,&tmp2);CHKERRQ(ierr);
643d0f46423SBarry Smith       ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr);
6448a62d963SHong Zhang       v    = amat->a; jj = amat->j;
6458a62d963SHong Zhang       for (i=0; i<amat->nz; i++) {
6468a62d963SHong Zhang         for (j=0; j<bs; j++) {
6478a62d963SHong Zhang           col = bs*(cstart + *jj) + j; /* column index */
6488a62d963SHong Zhang           for (row=0; row<bs; row++) {
6498a62d963SHong Zhang             tmp[col] += PetscAbsScalar(*v);  v++;
6508a62d963SHong Zhang           }
6518a62d963SHong Zhang         }
6528a62d963SHong Zhang         jj++;
6538a62d963SHong Zhang       }
6548a62d963SHong Zhang       v = bmat->a; jj = bmat->j;
6558a62d963SHong Zhang       for (i=0; i<bmat->nz; i++) {
6568a62d963SHong Zhang         for (j=0; j<bs; j++) {
6578a62d963SHong Zhang           col = bs*garray[*jj] + j;
6588a62d963SHong Zhang           for (row=0; row<bs; row++) {
6598a62d963SHong Zhang             tmp[col] += PetscAbsScalar(*v); v++;
6608a62d963SHong Zhang           }
6618a62d963SHong Zhang         }
6628a62d963SHong Zhang         jj++;
6638a62d963SHong Zhang       }
664*ce94432eSBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
6658a62d963SHong Zhang       *nrm = 0.0;
666d0f46423SBarry Smith       for (j=0; j<mat->cmap->N; j++) {
6678a62d963SHong Zhang         if (tmp2[j] > *nrm) *nrm = tmp2[j];
6688a62d963SHong Zhang       }
669fca92195SBarry Smith       ierr = PetscFree2(tmp,tmp2);CHKERRQ(ierr);
6708a62d963SHong Zhang     } else if (type == NORM_INFINITY) { /* max row sum */
671577dd1f9SKris Buschelman       PetscReal *sums;
672cb9801acSJed Brown       ierr = PetscMalloc(bs*sizeof(PetscReal),&sums);CHKERRQ(ierr);
6738a62d963SHong Zhang       sum  = 0.0;
6748a62d963SHong Zhang       for (j=0; j<amat->mbs; j++) {
6758a62d963SHong Zhang         for (row=0; row<bs; row++) sums[row] = 0.0;
6768a62d963SHong Zhang         v  = amat->a + bs2*amat->i[j];
6778a62d963SHong Zhang         nz = amat->i[j+1]-amat->i[j];
6788a62d963SHong Zhang         for (i=0; i<nz; i++) {
6798a62d963SHong Zhang           for (col=0; col<bs; col++) {
6808a62d963SHong Zhang             for (row=0; row<bs; row++) {
6818a62d963SHong Zhang               sums[row] += PetscAbsScalar(*v); v++;
6828a62d963SHong Zhang             }
6838a62d963SHong Zhang           }
6848a62d963SHong Zhang         }
6858a62d963SHong Zhang         v  = bmat->a + bs2*bmat->i[j];
6868a62d963SHong Zhang         nz = bmat->i[j+1]-bmat->i[j];
6878a62d963SHong Zhang         for (i=0; i<nz; i++) {
6888a62d963SHong Zhang           for (col=0; col<bs; col++) {
6898a62d963SHong Zhang             for (row=0; row<bs; row++) {
6908a62d963SHong Zhang               sums[row] += PetscAbsScalar(*v); v++;
6918a62d963SHong Zhang             }
6928a62d963SHong Zhang           }
6938a62d963SHong Zhang         }
6948a62d963SHong Zhang         for (row=0; row<bs; row++) {
6958a62d963SHong Zhang           if (sums[row] > sum) sum = sums[row];
6968a62d963SHong Zhang         }
6978a62d963SHong Zhang       }
698*ce94432eSBarry Smith       ierr = MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
699577dd1f9SKris Buschelman       ierr = PetscFree(sums);CHKERRQ(ierr);
700*ce94432eSBarry Smith     } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for this norm yet");
701d64ed03dSBarry Smith   }
7023a40ed3dSBarry Smith   PetscFunctionReturn(0);
703d6de1c52SSatish Balay }
70457b952d6SSatish Balay 
705fef45726SSatish Balay /*
706fef45726SSatish Balay   Creates the hash table, and sets the table
707fef45726SSatish Balay   This table is created only once.
708fef45726SSatish Balay   If new entried need to be added to the matrix
709fef45726SSatish Balay   then the hash table has to be destroyed and
710fef45726SSatish Balay   recreated.
711fef45726SSatish Balay */
7124a2ae208SSatish Balay #undef __FUNCT__
7134a2ae208SSatish Balay #define __FUNCT__ "MatCreateHashTable_MPIBAIJ_Private"
714dfbe8321SBarry Smith PetscErrorCode MatCreateHashTable_MPIBAIJ_Private(Mat mat,PetscReal factor)
715596b8d2eSBarry Smith {
716596b8d2eSBarry Smith   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
717596b8d2eSBarry Smith   Mat            A     = baij->A,B=baij->B;
718596b8d2eSBarry Smith   Mat_SeqBAIJ    *a    = (Mat_SeqBAIJ*)A->data,*b=(Mat_SeqBAIJ*)B->data;
719b24ad042SBarry Smith   PetscInt       i,j,k,nz=a->nz+b->nz,h1,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
7206849ba73SBarry Smith   PetscErrorCode ierr;
721fca92195SBarry Smith   PetscInt       ht_size,bs2=baij->bs2,rstart=baij->rstartbs;
722899cda47SBarry Smith   PetscInt       cstart=baij->cstartbs,*garray=baij->garray,row,col,Nbs=baij->Nbs;
723b24ad042SBarry Smith   PetscInt       *HT,key;
7243eda8832SBarry Smith   MatScalar      **HD;
725329f5518SBarry Smith   PetscReal      tmp;
7266cf91177SBarry Smith #if defined(PETSC_USE_INFO)
727b24ad042SBarry Smith   PetscInt ct=0,max=0;
7284a15367fSSatish Balay #endif
729fef45726SSatish Balay 
730d64ed03dSBarry Smith   PetscFunctionBegin;
731fca92195SBarry Smith   if (baij->ht) PetscFunctionReturn(0);
732fef45726SSatish Balay 
733fca92195SBarry Smith   baij->ht_size = (PetscInt)(factor*nz);
734fca92195SBarry Smith   ht_size       = baij->ht_size;
7350bdbc534SSatish Balay 
736fef45726SSatish Balay   /* Allocate Memory for Hash Table */
737fca92195SBarry Smith   ierr = PetscMalloc2(ht_size,MatScalar*,&baij->hd,ht_size,PetscInt,&baij->ht);CHKERRQ(ierr);
738fca92195SBarry Smith   ierr = PetscMemzero(baij->hd,ht_size*sizeof(MatScalar*));CHKERRQ(ierr);
739fca92195SBarry Smith   ierr = PetscMemzero(baij->ht,ht_size*sizeof(PetscInt));CHKERRQ(ierr);
740b9e4cc15SSatish Balay   HD   = baij->hd;
741a07cd24cSSatish Balay   HT   = baij->ht;
742b9e4cc15SSatish Balay 
743596b8d2eSBarry Smith   /* Loop Over A */
7440bdbc534SSatish Balay   for (i=0; i<a->mbs; i++) {
745596b8d2eSBarry Smith     for (j=ai[i]; j<ai[i+1]; j++) {
7460bdbc534SSatish Balay       row = i+rstart;
7470bdbc534SSatish Balay       col = aj[j]+cstart;
748596b8d2eSBarry Smith 
749187ce0cbSSatish Balay       key = row*Nbs + col + 1;
750fca92195SBarry Smith       h1  = HASH(ht_size,key,tmp);
751fca92195SBarry Smith       for (k=0; k<ht_size; k++) {
752fca92195SBarry Smith         if (!HT[(h1+k)%ht_size]) {
753fca92195SBarry Smith           HT[(h1+k)%ht_size] = key;
754fca92195SBarry Smith           HD[(h1+k)%ht_size] = a->a + j*bs2;
755596b8d2eSBarry Smith           break;
7566cf91177SBarry Smith #if defined(PETSC_USE_INFO)
757187ce0cbSSatish Balay         } else {
758187ce0cbSSatish Balay           ct++;
759187ce0cbSSatish Balay #endif
760596b8d2eSBarry Smith         }
761187ce0cbSSatish Balay       }
7626cf91177SBarry Smith #if defined(PETSC_USE_INFO)
763187ce0cbSSatish Balay       if (k> max) max = k;
764187ce0cbSSatish Balay #endif
765596b8d2eSBarry Smith     }
766596b8d2eSBarry Smith   }
767596b8d2eSBarry Smith   /* Loop Over B */
7680bdbc534SSatish Balay   for (i=0; i<b->mbs; i++) {
769596b8d2eSBarry Smith     for (j=bi[i]; j<bi[i+1]; j++) {
7700bdbc534SSatish Balay       row = i+rstart;
7710bdbc534SSatish Balay       col = garray[bj[j]];
772187ce0cbSSatish Balay       key = row*Nbs + col + 1;
773fca92195SBarry Smith       h1  = HASH(ht_size,key,tmp);
774fca92195SBarry Smith       for (k=0; k<ht_size; k++) {
775fca92195SBarry Smith         if (!HT[(h1+k)%ht_size]) {
776fca92195SBarry Smith           HT[(h1+k)%ht_size] = key;
777fca92195SBarry Smith           HD[(h1+k)%ht_size] = b->a + j*bs2;
778596b8d2eSBarry Smith           break;
7796cf91177SBarry Smith #if defined(PETSC_USE_INFO)
780187ce0cbSSatish Balay         } else {
781187ce0cbSSatish Balay           ct++;
782187ce0cbSSatish Balay #endif
783596b8d2eSBarry Smith         }
784187ce0cbSSatish Balay       }
7856cf91177SBarry Smith #if defined(PETSC_USE_INFO)
786187ce0cbSSatish Balay       if (k> max) max = k;
787187ce0cbSSatish Balay #endif
788596b8d2eSBarry Smith     }
789596b8d2eSBarry Smith   }
790596b8d2eSBarry Smith 
791596b8d2eSBarry Smith   /* Print Summary */
7926cf91177SBarry Smith #if defined(PETSC_USE_INFO)
793fca92195SBarry Smith   for (i=0,j=0; i<ht_size; i++) {
79426fbe8dcSKarl Rupp     if (HT[i]) j++;
795c38d4ed2SBarry Smith   }
7961e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Average Search = %5.2f,max search = %D\n",(!j)? 0.0:((PetscReal)(ct+j))/j,max);CHKERRQ(ierr);
797187ce0cbSSatish Balay #endif
7983a40ed3dSBarry Smith   PetscFunctionReturn(0);
799596b8d2eSBarry Smith }
80057b952d6SSatish Balay 
8014a2ae208SSatish Balay #undef __FUNCT__
8024a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIBAIJ"
803dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIBAIJ(Mat mat,MatAssemblyType mode)
804bbb85fb3SSatish Balay {
805bbb85fb3SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
806dfbe8321SBarry Smith   PetscErrorCode ierr;
807b24ad042SBarry Smith   PetscInt       nstash,reallocs;
808bbb85fb3SSatish Balay   InsertMode     addv;
809bbb85fb3SSatish Balay 
810bbb85fb3SSatish Balay   PetscFunctionBegin;
81126fbe8dcSKarl Rupp   if (baij->donotstash || mat->nooffprocentries) PetscFunctionReturn(0);
812bbb85fb3SSatish Balay 
813bbb85fb3SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
814*ce94432eSBarry Smith   ierr = MPI_Allreduce((PetscEnum*)&mat->insertmode,(PetscEnum*)&addv,1,MPIU_ENUM,MPI_BOR,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
815*ce94432eSBarry Smith   if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
816bbb85fb3SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
817bbb85fb3SSatish Balay 
818d0f46423SBarry Smith   ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr);
8191e2582c4SBarry Smith   ierr = MatStashScatterBegin_Private(mat,&mat->bstash,baij->rangebs);CHKERRQ(ierr);
8208798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
8211e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Stash has %D entries,uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
82246680499SSatish Balay   ierr = MatStashGetInfo_Private(&mat->bstash,&nstash,&reallocs);CHKERRQ(ierr);
8231e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
824bbb85fb3SSatish Balay   PetscFunctionReturn(0);
825bbb85fb3SSatish Balay }
826bbb85fb3SSatish Balay 
8274a2ae208SSatish Balay #undef __FUNCT__
8284a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIBAIJ"
829dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIBAIJ(Mat mat,MatAssemblyType mode)
830bbb85fb3SSatish Balay {
831bbb85fb3SSatish Balay   Mat_MPIBAIJ    *baij=(Mat_MPIBAIJ*)mat->data;
83291c97fd4SSatish Balay   Mat_SeqBAIJ    *a   =(Mat_SeqBAIJ*)baij->A->data;
8336849ba73SBarry Smith   PetscErrorCode ierr;
834b24ad042SBarry Smith   PetscInt       i,j,rstart,ncols,flg,bs2=baij->bs2;
835e44c0bd4SBarry Smith   PetscInt       *row,*col;
836ace3abfcSBarry Smith   PetscBool      r1,r2,r3,other_disassembled;
8373eda8832SBarry Smith   MatScalar      *val;
838bbb85fb3SSatish Balay   InsertMode     addv = mat->insertmode;
839b24ad042SBarry Smith   PetscMPIInt    n;
840bbb85fb3SSatish Balay 
841bbb85fb3SSatish Balay   PetscFunctionBegin;
8425fd66863SKarl Rupp   /* do not use 'b=(Mat_SeqBAIJ*)baij->B->data' as B can be reset in disassembly */
8434cb17eb5SBarry Smith   if (!baij->donotstash && !mat->nooffprocentries) {
844a2d1c673SSatish Balay     while (1) {
8458798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
846a2d1c673SSatish Balay       if (!flg) break;
847a2d1c673SSatish Balay 
848bbb85fb3SSatish Balay       for (i=0; i<n;) {
849bbb85fb3SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
85026fbe8dcSKarl Rupp         for (j=i,rstart=row[j]; j<n; j++) {
85126fbe8dcSKarl Rupp           if (row[j] != rstart) break;
85226fbe8dcSKarl Rupp         }
853bbb85fb3SSatish Balay         if (j < n) ncols = j-i;
854bbb85fb3SSatish Balay         else       ncols = n-i;
855bbb85fb3SSatish Balay         /* Now assemble all these values with a single function call */
85697e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
857bbb85fb3SSatish Balay         i    = j;
858bbb85fb3SSatish Balay       }
859bbb85fb3SSatish Balay     }
8608798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
861a2d1c673SSatish Balay     /* Now process the block-stash. Since the values are stashed column-oriented,
862a2d1c673SSatish Balay        set the roworiented flag to column oriented, and after MatSetValues()
863a2d1c673SSatish Balay        restore the original flags */
864a2d1c673SSatish Balay     r1 = baij->roworiented;
865a2d1c673SSatish Balay     r2 = a->roworiented;
86691c97fd4SSatish Balay     r3 = ((Mat_SeqBAIJ*)baij->B->data)->roworiented;
86726fbe8dcSKarl Rupp 
8687c922b88SBarry Smith     baij->roworiented = PETSC_FALSE;
8697c922b88SBarry Smith     a->roworiented    = PETSC_FALSE;
87026fbe8dcSKarl Rupp 
87191c97fd4SSatish Balay     (((Mat_SeqBAIJ*)baij->B->data))->roworiented = PETSC_FALSE; /* b->roworiented */
872a2d1c673SSatish Balay     while (1) {
8738798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->bstash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
874a2d1c673SSatish Balay       if (!flg) break;
875a2d1c673SSatish Balay 
876a2d1c673SSatish Balay       for (i=0; i<n;) {
877a2d1c673SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
87826fbe8dcSKarl Rupp         for (j=i,rstart=row[j]; j<n; j++) {
87926fbe8dcSKarl Rupp           if (row[j] != rstart) break;
88026fbe8dcSKarl Rupp         }
881a2d1c673SSatish Balay         if (j < n) ncols = j-i;
882a2d1c673SSatish Balay         else       ncols = n-i;
88397e5c40aSBarry Smith         ierr = MatSetValuesBlocked_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i*bs2,addv);CHKERRQ(ierr);
884a2d1c673SSatish Balay         i    = j;
885a2d1c673SSatish Balay       }
886a2d1c673SSatish Balay     }
8878798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->bstash);CHKERRQ(ierr);
88826fbe8dcSKarl Rupp 
889a2d1c673SSatish Balay     baij->roworiented = r1;
890a2d1c673SSatish Balay     a->roworiented    = r2;
89126fbe8dcSKarl Rupp 
89291c97fd4SSatish Balay     ((Mat_SeqBAIJ*)baij->B->data)->roworiented = r3; /* b->roworiented */
893bbb85fb3SSatish Balay   }
894bbb85fb3SSatish Balay 
895bbb85fb3SSatish Balay   ierr = MatAssemblyBegin(baij->A,mode);CHKERRQ(ierr);
896bbb85fb3SSatish Balay   ierr = MatAssemblyEnd(baij->A,mode);CHKERRQ(ierr);
897bbb85fb3SSatish Balay 
898bbb85fb3SSatish Balay   /* determine if any processor has disassembled, if so we must
899bbb85fb3SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
900bbb85fb3SSatish Balay   /*
901bbb85fb3SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
902bbb85fb3SSatish Balay      no processor disassembled thus we can skip this stuff
903bbb85fb3SSatish Balay   */
904bbb85fb3SSatish Balay   if (!((Mat_SeqBAIJ*)baij->B->data)->nonew) {
905*ce94432eSBarry Smith     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
906bbb85fb3SSatish Balay     if (mat->was_assembled && !other_disassembled) {
907ab9863d7SBarry Smith       ierr = MatDisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
908bbb85fb3SSatish Balay     }
909bbb85fb3SSatish Balay   }
910bbb85fb3SSatish Balay 
911bbb85fb3SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
912bbb85fb3SSatish Balay     ierr = MatSetUpMultiply_MPIBAIJ(mat);CHKERRQ(ierr);
913bbb85fb3SSatish Balay   }
9144e35b6f3SSatish Balay   ierr = MatSetOption(baij->B,MAT_CHECK_COMPRESSED_ROW,PETSC_FALSE);CHKERRQ(ierr);
915bbb85fb3SSatish Balay   ierr = MatAssemblyBegin(baij->B,mode);CHKERRQ(ierr);
916bbb85fb3SSatish Balay   ierr = MatAssemblyEnd(baij->B,mode);CHKERRQ(ierr);
917bbb85fb3SSatish Balay 
9186cf91177SBarry Smith #if defined(PETSC_USE_INFO)
919bbb85fb3SSatish Balay   if (baij->ht && mode== MAT_FINAL_ASSEMBLY) {
9201e2582c4SBarry Smith     ierr = PetscInfo1(mat,"Average Hash Table Search in MatSetValues = %5.2f\n",((PetscReal)baij->ht_total_ct)/baij->ht_insert_ct);CHKERRQ(ierr);
92126fbe8dcSKarl Rupp 
922bbb85fb3SSatish Balay     baij->ht_total_ct  = 0;
923bbb85fb3SSatish Balay     baij->ht_insert_ct = 0;
924bbb85fb3SSatish Balay   }
925bbb85fb3SSatish Balay #endif
926bbb85fb3SSatish Balay   if (baij->ht_flag && !baij->ht && mode == MAT_FINAL_ASSEMBLY) {
927bbb85fb3SSatish Balay     ierr = MatCreateHashTable_MPIBAIJ_Private(mat,baij->ht_fact);CHKERRQ(ierr);
92826fbe8dcSKarl Rupp 
929bbb85fb3SSatish Balay     mat->ops->setvalues        = MatSetValues_MPIBAIJ_HT;
930bbb85fb3SSatish Balay     mat->ops->setvaluesblocked = MatSetValuesBlocked_MPIBAIJ_HT;
931bbb85fb3SSatish Balay   }
932bbb85fb3SSatish Balay 
933fca92195SBarry Smith   ierr = PetscFree2(baij->rowvalues,baij->rowindices);CHKERRQ(ierr);
93426fbe8dcSKarl Rupp 
935606d414cSSatish Balay   baij->rowvalues = 0;
936bbb85fb3SSatish Balay   PetscFunctionReturn(0);
937bbb85fb3SSatish Balay }
93857b952d6SSatish Balay 
9394a2ae208SSatish Balay #undef __FUNCT__
9404a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIBAIJ_ASCIIorDraworSocket"
9416849ba73SBarry Smith static PetscErrorCode MatView_MPIBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
94257b952d6SSatish Balay {
94357b952d6SSatish Balay   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
944dfbe8321SBarry Smith   PetscErrorCode    ierr;
945b24ad042SBarry Smith   PetscMPIInt       size = baij->size,rank = baij->rank;
946d0f46423SBarry Smith   PetscInt          bs   = mat->rmap->bs;
947ace3abfcSBarry Smith   PetscBool         iascii,isdraw;
948b0a32e0cSBarry Smith   PetscViewer       sviewer;
949f3ef73ceSBarry Smith   PetscViewerFormat format;
95057b952d6SSatish Balay 
951d64ed03dSBarry Smith   PetscFunctionBegin;
952251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
953251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
95432077d6dSBarry Smith   if (iascii) {
955b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
956456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
9574e220ebcSLois Curfman McInnes       MatInfo info;
958*ce94432eSBarry Smith       ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);CHKERRQ(ierr);
959d41123aaSBarry Smith       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
9607b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr);
96177431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D bs %D mem %D\n",
96216608c43SJed Brown                                                 rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,mat->rmap->bs,(PetscInt)info.memory);CHKERRQ(ierr);
963d132466eSBarry Smith       ierr = MatGetInfo(baij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
964e6dd01d4SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
965d132466eSBarry Smith       ierr = MatGetInfo(baij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
966e6dd01d4SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
967b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
9687b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr);
96907d81ca4SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr);
97057b952d6SSatish Balay       ierr = VecScatterView(baij->Mvctx,viewer);CHKERRQ(ierr);
9713a40ed3dSBarry Smith       PetscFunctionReturn(0);
972fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
97377431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);CHKERRQ(ierr);
9743a40ed3dSBarry Smith       PetscFunctionReturn(0);
97504929863SHong Zhang     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
97604929863SHong Zhang       PetscFunctionReturn(0);
97757b952d6SSatish Balay     }
97857b952d6SSatish Balay   }
97957b952d6SSatish Balay 
9800f5bd95cSBarry Smith   if (isdraw) {
981b0a32e0cSBarry Smith     PetscDraw draw;
982ace3abfcSBarry Smith     PetscBool isnull;
983b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
984b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
98557b952d6SSatish Balay   }
98657b952d6SSatish Balay 
98757b952d6SSatish Balay   if (size == 1) {
9887adad957SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)baij->A,((PetscObject)mat)->name);CHKERRQ(ierr);
98957b952d6SSatish Balay     ierr = MatView(baij->A,viewer);CHKERRQ(ierr);
990d64ed03dSBarry Smith   } else {
99157b952d6SSatish Balay     /* assemble the entire matrix onto first processor. */
99257b952d6SSatish Balay     Mat         A;
99357b952d6SSatish Balay     Mat_SeqBAIJ *Aloc;
994d0f46423SBarry Smith     PetscInt    M = mat->rmap->N,N = mat->cmap->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs;
9953eda8832SBarry Smith     MatScalar   *a;
99657b952d6SSatish Balay 
997f204ca49SKris Buschelman     /* Here we are creating a temporary matrix, so will assume MPIBAIJ is acceptable */
998f204ca49SKris Buschelman     /* Perhaps this should be the type of mat? */
999*ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)mat),&A);CHKERRQ(ierr);
100057b952d6SSatish Balay     if (!rank) {
1001f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr);
1002d64ed03dSBarry Smith     } else {
1003f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr);
100457b952d6SSatish Balay     }
1005f204ca49SKris Buschelman     ierr = MatSetType(A,MATMPIBAIJ);CHKERRQ(ierr);
10060298fd71SBarry Smith     ierr = MatMPIBAIJSetPreallocation(A,mat->rmap->bs,0,NULL,0,NULL);CHKERRQ(ierr);
10072b82e772SSatish Balay     ierr = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);
100852e6d16bSBarry Smith     ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr);
100957b952d6SSatish Balay 
101057b952d6SSatish Balay     /* copy over the A part */
101157b952d6SSatish Balay     Aloc = (Mat_SeqBAIJ*)baij->A->data;
101257b952d6SSatish Balay     ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1013b24ad042SBarry Smith     ierr = PetscMalloc(bs*sizeof(PetscInt),&rvals);CHKERRQ(ierr);
101457b952d6SSatish Balay 
101557b952d6SSatish Balay     for (i=0; i<mbs; i++) {
1016899cda47SBarry Smith       rvals[0] = bs*(baij->rstartbs + i);
101726fbe8dcSKarl Rupp       for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
101857b952d6SSatish Balay       for (j=ai[i]; j<ai[i+1]; j++) {
1019899cda47SBarry Smith         col = (baij->cstartbs+aj[j])*bs;
102057b952d6SSatish Balay         for (k=0; k<bs; k++) {
102197e5c40aSBarry Smith           ierr      = MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);CHKERRQ(ierr);
1022cee3aa6bSSatish Balay           col++; a += bs;
102357b952d6SSatish Balay         }
102457b952d6SSatish Balay       }
102557b952d6SSatish Balay     }
102657b952d6SSatish Balay     /* copy over the B part */
102757b952d6SSatish Balay     Aloc = (Mat_SeqBAIJ*)baij->B->data;
102857b952d6SSatish Balay     ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
102957b952d6SSatish Balay     for (i=0; i<mbs; i++) {
1030899cda47SBarry Smith       rvals[0] = bs*(baij->rstartbs + i);
103126fbe8dcSKarl Rupp       for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
103257b952d6SSatish Balay       for (j=ai[i]; j<ai[i+1]; j++) {
103357b952d6SSatish Balay         col = baij->garray[aj[j]]*bs;
103457b952d6SSatish Balay         for (k=0; k<bs; k++) {
103597e5c40aSBarry Smith           ierr      = MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);CHKERRQ(ierr);
1036cee3aa6bSSatish Balay           col++; a += bs;
103757b952d6SSatish Balay         }
103857b952d6SSatish Balay       }
103957b952d6SSatish Balay     }
1040606d414cSSatish Balay     ierr = PetscFree(rvals);CHKERRQ(ierr);
10416d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10426d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
104355843e3eSBarry Smith     /*
104455843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
1045b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
104655843e3eSBarry Smith     */
1047b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
1048f14a1c24SBarry Smith     if (!rank) {
10497adad957SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject)((Mat_MPIBAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr);
10507566de4bSShri Abhyankar       /* Set the type name to MATMPIBAIJ so that the correct type can be printed out by PetscObjectPrintClassNamePrefixType() in MatView_SeqBAIJ_ASCII()*/
10517566de4bSShri Abhyankar       PetscStrcpy(((PetscObject)((Mat_MPIBAIJ*)(A->data))->A)->type_name,MATMPIBAIJ);
10526831982aSBarry Smith       ierr = MatView(((Mat_MPIBAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
105357b952d6SSatish Balay     }
1054b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
10556bf464f9SBarry Smith     ierr = MatDestroy(&A);CHKERRQ(ierr);
105657b952d6SSatish Balay   }
10573a40ed3dSBarry Smith   PetscFunctionReturn(0);
105857b952d6SSatish Balay }
105957b952d6SSatish Balay 
10604a2ae208SSatish Balay #undef __FUNCT__
1061660746e0SBarry Smith #define __FUNCT__ "MatView_MPIBAIJ_Binary"
1062660746e0SBarry Smith static PetscErrorCode MatView_MPIBAIJ_Binary(Mat mat,PetscViewer viewer)
1063660746e0SBarry Smith {
1064660746e0SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)mat->data;
1065660746e0SBarry Smith   Mat_SeqBAIJ    *A = (Mat_SeqBAIJ*)a->A->data;
1066660746e0SBarry Smith   Mat_SeqBAIJ    *B = (Mat_SeqBAIJ*)a->B->data;
1067660746e0SBarry Smith   PetscErrorCode ierr;
10685f48b12bSBarry Smith   PetscInt       i,*row_lens,*crow_lens,bs = mat->rmap->bs,j,k,bs2=a->bs2,header[4],nz,rlen;
1069e96a6426SSatish Balay   PetscInt       *range=0,nzmax,*column_indices,cnt,col,*garray = a->garray,cstart = mat->cmap->rstart/bs,len,pcnt,l,ll;
1070660746e0SBarry Smith   int            fd;
1071660746e0SBarry Smith   PetscScalar    *column_values;
1072660746e0SBarry Smith   FILE           *file;
1073660746e0SBarry Smith   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
1074638eb2ebSBarry Smith   PetscInt       message_count,flowcontrolcount;
1075660746e0SBarry Smith 
1076660746e0SBarry Smith   PetscFunctionBegin;
1077*ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);CHKERRQ(ierr);
1078*ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr);
1079660746e0SBarry Smith   nz   = bs2*(A->nz + B->nz);
1080660746e0SBarry Smith   rlen = mat->rmap->n;
1081660746e0SBarry Smith   if (!rank) {
1082660746e0SBarry Smith     header[0] = MAT_FILE_CLASSID;
1083660746e0SBarry Smith     header[1] = mat->rmap->N;
1084660746e0SBarry Smith     header[2] = mat->cmap->N;
108526fbe8dcSKarl Rupp 
1086*ce94432eSBarry Smith     ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1087660746e0SBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
1088660746e0SBarry Smith     ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1089660746e0SBarry Smith     /* get largest number of rows any processor has */
1090660746e0SBarry Smith     range = mat->rmap->range;
1091660746e0SBarry Smith     for (i=1; i<size; i++) {
1092660746e0SBarry Smith       rlen = PetscMax(rlen,range[i+1] - range[i]);
1093660746e0SBarry Smith     }
1094660746e0SBarry Smith   } else {
1095*ce94432eSBarry Smith     ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1096660746e0SBarry Smith   }
1097660746e0SBarry Smith 
1098660746e0SBarry Smith   ierr = PetscMalloc((rlen/bs)*sizeof(PetscInt),&crow_lens);CHKERRQ(ierr);
1099660746e0SBarry Smith   /* compute lengths of each row  */
1100660746e0SBarry Smith   for (i=0; i<a->mbs; i++) {
1101660746e0SBarry Smith     crow_lens[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
1102660746e0SBarry Smith   }
1103660746e0SBarry Smith   /* store the row lengths to the file */
1104638eb2ebSBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1105660746e0SBarry Smith   if (!rank) {
1106660746e0SBarry Smith     MPI_Status status;
1107660746e0SBarry Smith     ierr = PetscMalloc(rlen*sizeof(PetscInt),&row_lens);CHKERRQ(ierr);
1108660746e0SBarry Smith     rlen = (range[1] - range[0])/bs;
1109660746e0SBarry Smith     for (i=0; i<rlen; i++) {
1110660746e0SBarry Smith       for (j=0; j<bs; j++) {
1111660746e0SBarry Smith         row_lens[i*bs+j] = bs*crow_lens[i];
1112660746e0SBarry Smith       }
1113660746e0SBarry Smith     }
1114660746e0SBarry Smith     ierr = PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1115660746e0SBarry Smith     for (i=1; i<size; i++) {
1116660746e0SBarry Smith       rlen = (range[i+1] - range[i])/bs;
1117639ff905SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
1118*ce94432eSBarry Smith       ierr = MPI_Recv(crow_lens,rlen,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr);
1119660746e0SBarry Smith       for (k=0; k<rlen; k++) {
1120660746e0SBarry Smith         for (j=0; j<bs; j++) {
1121660746e0SBarry Smith           row_lens[k*bs+j] = bs*crow_lens[k];
1122660746e0SBarry Smith         }
1123660746e0SBarry Smith       }
1124660746e0SBarry Smith       ierr = PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1125660746e0SBarry Smith     }
1126639ff905SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
1127660746e0SBarry Smith     ierr = PetscFree(row_lens);CHKERRQ(ierr);
1128660746e0SBarry Smith   } else {
1129639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
1130*ce94432eSBarry Smith     ierr = MPI_Send(crow_lens,mat->rmap->n/bs,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1131639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
1132660746e0SBarry Smith   }
1133660746e0SBarry Smith   ierr = PetscFree(crow_lens);CHKERRQ(ierr);
1134660746e0SBarry Smith 
1135660746e0SBarry 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
1136660746e0SBarry Smith      information needed to make it for each row from a block row. This does require more communication but still not more than
1137660746e0SBarry Smith      the communication needed for the nonzero values  */
1138660746e0SBarry Smith   nzmax = nz; /*  space a largest processor needs */
1139*ce94432eSBarry Smith   ierr  = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1140660746e0SBarry Smith   ierr  = PetscMalloc(nzmax*sizeof(PetscInt),&column_indices);CHKERRQ(ierr);
1141660746e0SBarry Smith   cnt   = 0;
1142660746e0SBarry Smith   for (i=0; i<a->mbs; i++) {
1143660746e0SBarry Smith     pcnt = cnt;
1144660746e0SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
1145660746e0SBarry Smith       if ((col = garray[B->j[j]]) > cstart) break;
1146660746e0SBarry Smith       for (l=0; l<bs; l++) {
1147660746e0SBarry Smith         column_indices[cnt++] = bs*col+l;
1148660746e0SBarry Smith       }
1149660746e0SBarry Smith     }
1150660746e0SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
1151660746e0SBarry Smith       for (l=0; l<bs; l++) {
1152660746e0SBarry Smith         column_indices[cnt++] = bs*(A->j[k] + cstart)+l;
1153660746e0SBarry Smith       }
1154660746e0SBarry Smith     }
1155660746e0SBarry Smith     for (; j<B->i[i+1]; j++) {
1156660746e0SBarry Smith       for (l=0; l<bs; l++) {
1157660746e0SBarry Smith         column_indices[cnt++] = bs*garray[B->j[j]]+l;
1158660746e0SBarry Smith       }
1159660746e0SBarry Smith     }
1160660746e0SBarry Smith     len = cnt - pcnt;
1161660746e0SBarry Smith     for (k=1; k<bs; k++) {
1162660746e0SBarry Smith       ierr = PetscMemcpy(&column_indices[cnt],&column_indices[pcnt],len*sizeof(PetscInt));CHKERRQ(ierr);
1163660746e0SBarry Smith       cnt += len;
1164660746e0SBarry Smith     }
1165660746e0SBarry Smith   }
1166660746e0SBarry Smith   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1167660746e0SBarry Smith 
1168660746e0SBarry Smith   /* store the columns to the file */
1169638eb2ebSBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1170660746e0SBarry Smith   if (!rank) {
1171660746e0SBarry Smith     MPI_Status status;
1172660746e0SBarry Smith     ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1173660746e0SBarry Smith     for (i=1; i<size; i++) {
1174639ff905SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
1175*ce94432eSBarry Smith       ierr = MPI_Recv(&cnt,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr);
1176*ce94432eSBarry Smith       ierr = MPI_Recv(column_indices,cnt,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr);
1177660746e0SBarry Smith       ierr = PetscBinaryWrite(fd,column_indices,cnt,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1178660746e0SBarry Smith     }
1179639ff905SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
1180660746e0SBarry Smith   } else {
1181639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
1182*ce94432eSBarry Smith     ierr = MPI_Send(&cnt,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1183*ce94432eSBarry Smith     ierr = MPI_Send(column_indices,cnt,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1184639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
1185660746e0SBarry Smith   }
1186660746e0SBarry Smith   ierr = PetscFree(column_indices);CHKERRQ(ierr);
1187660746e0SBarry Smith 
1188660746e0SBarry Smith   /* load up the numerical values */
1189660746e0SBarry Smith   ierr = PetscMalloc(nzmax*sizeof(PetscScalar),&column_values);CHKERRQ(ierr);
1190660746e0SBarry Smith   cnt  = 0;
1191660746e0SBarry Smith   for (i=0; i<a->mbs; i++) {
1192660746e0SBarry Smith     rlen = bs*(B->i[i+1] - B->i[i] + A->i[i+1] - A->i[i]);
1193660746e0SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
1194660746e0SBarry Smith       if (garray[B->j[j]] > cstart) break;
1195660746e0SBarry Smith       for (l=0; l<bs; l++) {
1196660746e0SBarry Smith         for (ll=0; ll<bs; ll++) {
1197660746e0SBarry Smith           column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1198660746e0SBarry Smith         }
1199660746e0SBarry Smith       }
1200660746e0SBarry Smith       cnt += bs;
1201660746e0SBarry Smith     }
1202660746e0SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
1203660746e0SBarry Smith       for (l=0; l<bs; l++) {
1204660746e0SBarry Smith         for (ll=0; ll<bs; ll++) {
1205660746e0SBarry Smith           column_values[cnt + l*rlen + ll] = A->a[bs2*k+l+bs*ll];
1206660746e0SBarry Smith         }
1207660746e0SBarry Smith       }
1208660746e0SBarry Smith       cnt += bs;
1209660746e0SBarry Smith     }
1210660746e0SBarry Smith     for (; j<B->i[i+1]; j++) {
1211660746e0SBarry Smith       for (l=0; l<bs; l++) {
1212660746e0SBarry Smith         for (ll=0; ll<bs; ll++) {
1213660746e0SBarry Smith           column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1214660746e0SBarry Smith         }
1215660746e0SBarry Smith       }
1216660746e0SBarry Smith       cnt += bs;
1217660746e0SBarry Smith     }
1218660746e0SBarry Smith     cnt += (bs-1)*rlen;
1219660746e0SBarry Smith   }
1220660746e0SBarry Smith   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1221660746e0SBarry Smith 
1222660746e0SBarry Smith   /* store the column values to the file */
1223638eb2ebSBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1224660746e0SBarry Smith   if (!rank) {
1225660746e0SBarry Smith     MPI_Status status;
1226660746e0SBarry Smith     ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
1227660746e0SBarry Smith     for (i=1; i<size; i++) {
1228639ff905SBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr);
1229*ce94432eSBarry Smith       ierr = MPI_Recv(&cnt,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr);
1230*ce94432eSBarry Smith       ierr = MPI_Recv(column_values,cnt,MPIU_SCALAR,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr);
1231660746e0SBarry Smith       ierr = PetscBinaryWrite(fd,column_values,cnt,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
1232660746e0SBarry Smith     }
1233639ff905SBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr);
1234660746e0SBarry Smith   } else {
1235639ff905SBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr);
1236*ce94432eSBarry Smith     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1237*ce94432eSBarry Smith     ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
1238639ff905SBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr);
1239660746e0SBarry Smith   }
1240660746e0SBarry Smith   ierr = PetscFree(column_values);CHKERRQ(ierr);
1241660746e0SBarry Smith 
1242660746e0SBarry Smith   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
1243660746e0SBarry Smith   if (file) {
1244660746e0SBarry Smith     fprintf(file,"-matload_block_size %d\n",(int)mat->rmap->bs);
1245660746e0SBarry Smith   }
1246660746e0SBarry Smith   PetscFunctionReturn(0);
1247660746e0SBarry Smith }
1248660746e0SBarry Smith 
1249660746e0SBarry Smith #undef __FUNCT__
12504a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIBAIJ"
1251dfbe8321SBarry Smith PetscErrorCode MatView_MPIBAIJ(Mat mat,PetscViewer viewer)
125257b952d6SSatish Balay {
1253dfbe8321SBarry Smith   PetscErrorCode ierr;
1254ace3abfcSBarry Smith   PetscBool      iascii,isdraw,issocket,isbinary;
125557b952d6SSatish Balay 
1256d64ed03dSBarry Smith   PetscFunctionBegin;
1257251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1258251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1259251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr);
1260251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
1261660746e0SBarry Smith   if (iascii || isdraw || issocket) {
12627b2a1423SBarry Smith     ierr = MatView_MPIBAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
1263660746e0SBarry Smith   } else if (isbinary) {
1264660746e0SBarry Smith     ierr = MatView_MPIBAIJ_Binary(mat,viewer);CHKERRQ(ierr);
126557b952d6SSatish Balay   }
12663a40ed3dSBarry Smith   PetscFunctionReturn(0);
126757b952d6SSatish Balay }
126857b952d6SSatish Balay 
12694a2ae208SSatish Balay #undef __FUNCT__
12704a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIBAIJ"
1271dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIBAIJ(Mat mat)
127279bdfe76SSatish Balay {
127379bdfe76SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
1274dfbe8321SBarry Smith   PetscErrorCode ierr;
127579bdfe76SSatish Balay 
1276d64ed03dSBarry Smith   PetscFunctionBegin;
1277aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1278d0f46423SBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%D,Cols=%D",mat->rmap->N,mat->cmap->N);
127979bdfe76SSatish Balay #endif
12808798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
12818798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->bstash);CHKERRQ(ierr);
12826bf464f9SBarry Smith   ierr = MatDestroy(&baij->A);CHKERRQ(ierr);
12836bf464f9SBarry Smith   ierr = MatDestroy(&baij->B);CHKERRQ(ierr);
1284aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
12856bc0bbbfSBarry Smith   ierr = PetscTableDestroy(&baij->colmap);CHKERRQ(ierr);
128648e59246SSatish Balay #else
128705b42c5fSBarry Smith   ierr = PetscFree(baij->colmap);CHKERRQ(ierr);
128848e59246SSatish Balay #endif
128905b42c5fSBarry Smith   ierr = PetscFree(baij->garray);CHKERRQ(ierr);
12906bf464f9SBarry Smith   ierr = VecDestroy(&baij->lvec);CHKERRQ(ierr);
12916bf464f9SBarry Smith   ierr = VecScatterDestroy(&baij->Mvctx);CHKERRQ(ierr);
1292fca92195SBarry Smith   ierr = PetscFree2(baij->rowvalues,baij->rowindices);CHKERRQ(ierr);
129305b42c5fSBarry Smith   ierr = PetscFree(baij->barray);CHKERRQ(ierr);
1294fca92195SBarry Smith   ierr = PetscFree2(baij->hd,baij->ht);CHKERRQ(ierr);
1295899cda47SBarry Smith   ierr = PetscFree(baij->rangebs);CHKERRQ(ierr);
1296bf0cc555SLisandro Dalcin   ierr = PetscFree(mat->data);CHKERRQ(ierr);
1297901853e0SKris Buschelman 
1298dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr);
12990298fd71SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",NULL);CHKERRQ(ierr);
13000298fd71SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",NULL);CHKERRQ(ierr);
13010298fd71SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",NULL);CHKERRQ(ierr);
13020298fd71SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocation_C","",NULL);CHKERRQ(ierr);
13030298fd71SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocationCSR_C","",NULL);CHKERRQ(ierr);
13040298fd71SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",NULL);CHKERRQ(ierr);
13050298fd71SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatSetHashTableFactor_C","",NULL);CHKERRQ(ierr);
13060298fd71SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_mpisbaij_C","",NULL);CHKERRQ(ierr);
13070298fd71SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_mpibstrm_C","",NULL);CHKERRQ(ierr);
13083a40ed3dSBarry Smith   PetscFunctionReturn(0);
130979bdfe76SSatish Balay }
131079bdfe76SSatish Balay 
13114a2ae208SSatish Balay #undef __FUNCT__
13124a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIBAIJ"
1313dfbe8321SBarry Smith PetscErrorCode MatMult_MPIBAIJ(Mat A,Vec xx,Vec yy)
1314cee3aa6bSSatish Balay {
1315cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1316dfbe8321SBarry Smith   PetscErrorCode ierr;
1317b24ad042SBarry Smith   PetscInt       nt;
1318cee3aa6bSSatish Balay 
1319d64ed03dSBarry Smith   PetscFunctionBegin;
1320e1311b90SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
1321e7e72b3dSBarry Smith   if (nt != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");
1322e1311b90SBarry Smith   ierr = VecGetLocalSize(yy,&nt);CHKERRQ(ierr);
1323e7e72b3dSBarry Smith   if (nt != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible parition of A and yy");
1324ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1325f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
1326ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1327f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
13283a40ed3dSBarry Smith   PetscFunctionReturn(0);
1329cee3aa6bSSatish Balay }
1330cee3aa6bSSatish Balay 
13314a2ae208SSatish Balay #undef __FUNCT__
13324a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIBAIJ"
1333dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1334cee3aa6bSSatish Balay {
1335cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1336dfbe8321SBarry Smith   PetscErrorCode ierr;
1337d64ed03dSBarry Smith 
1338d64ed03dSBarry Smith   PetscFunctionBegin;
1339ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1340f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1341ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1342f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
13433a40ed3dSBarry Smith   PetscFunctionReturn(0);
1344cee3aa6bSSatish Balay }
1345cee3aa6bSSatish Balay 
13464a2ae208SSatish Balay #undef __FUNCT__
13474a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIBAIJ"
1348dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIBAIJ(Mat A,Vec xx,Vec yy)
1349cee3aa6bSSatish Balay {
1350cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1351dfbe8321SBarry Smith   PetscErrorCode ierr;
1352ace3abfcSBarry Smith   PetscBool      merged;
1353cee3aa6bSSatish Balay 
1354d64ed03dSBarry Smith   PetscFunctionBegin;
1355a5ff213dSBarry Smith   ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr);
1356cee3aa6bSSatish Balay   /* do nondiagonal part */
13577c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1358a5ff213dSBarry Smith   if (!merged) {
1359cee3aa6bSSatish Balay     /* send it on its way */
1360ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1361cee3aa6bSSatish Balay     /* do local part */
13627c922b88SBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1363cee3aa6bSSatish Balay     /* receive remote parts: note this assumes the values are not actually */
1364a5ff213dSBarry Smith     /* inserted in yy until the next line */
1365ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1366a5ff213dSBarry Smith   } else {
1367a5ff213dSBarry Smith     /* do local part */
1368a5ff213dSBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1369a5ff213dSBarry Smith     /* send it on its way */
1370ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1371a5ff213dSBarry Smith     /* values actually were received in the Begin() but we need to call this nop */
1372ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1373a5ff213dSBarry Smith   }
13743a40ed3dSBarry Smith   PetscFunctionReturn(0);
1375cee3aa6bSSatish Balay }
1376cee3aa6bSSatish Balay 
13774a2ae208SSatish Balay #undef __FUNCT__
13784a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIBAIJ"
1379dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1380cee3aa6bSSatish Balay {
1381cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1382dfbe8321SBarry Smith   PetscErrorCode ierr;
1383cee3aa6bSSatish Balay 
1384d64ed03dSBarry Smith   PetscFunctionBegin;
1385cee3aa6bSSatish Balay   /* do nondiagonal part */
13867c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1387cee3aa6bSSatish Balay   /* send it on its way */
1388ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1389cee3aa6bSSatish Balay   /* do local part */
13907c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1391cee3aa6bSSatish Balay   /* receive remote parts: note this assumes the values are not actually */
1392cee3aa6bSSatish Balay   /* inserted in yy until the next line, which is true for my implementation*/
1393cee3aa6bSSatish Balay   /* but is not perhaps always true. */
1394ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
13953a40ed3dSBarry Smith   PetscFunctionReturn(0);
1396cee3aa6bSSatish Balay }
1397cee3aa6bSSatish Balay 
1398cee3aa6bSSatish Balay /*
1399cee3aa6bSSatish Balay   This only works correctly for square matrices where the subblock A->A is the
1400cee3aa6bSSatish Balay    diagonal block
1401cee3aa6bSSatish Balay */
14024a2ae208SSatish Balay #undef __FUNCT__
14034a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIBAIJ"
1404dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIBAIJ(Mat A,Vec v)
1405cee3aa6bSSatish Balay {
1406cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1407dfbe8321SBarry Smith   PetscErrorCode ierr;
1408d64ed03dSBarry Smith 
1409d64ed03dSBarry Smith   PetscFunctionBegin;
1410e32f2f54SBarry 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");
14113a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
14123a40ed3dSBarry Smith   PetscFunctionReturn(0);
1413cee3aa6bSSatish Balay }
1414cee3aa6bSSatish Balay 
14154a2ae208SSatish Balay #undef __FUNCT__
14164a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIBAIJ"
1417f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIBAIJ(Mat A,PetscScalar aa)
1418cee3aa6bSSatish Balay {
1419cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1420dfbe8321SBarry Smith   PetscErrorCode ierr;
1421d64ed03dSBarry Smith 
1422d64ed03dSBarry Smith   PetscFunctionBegin;
1423f4df32b1SMatthew Knepley   ierr = MatScale(a->A,aa);CHKERRQ(ierr);
1424f4df32b1SMatthew Knepley   ierr = MatScale(a->B,aa);CHKERRQ(ierr);
14253a40ed3dSBarry Smith   PetscFunctionReturn(0);
1426cee3aa6bSSatish Balay }
1427026e39d0SSatish Balay 
14284a2ae208SSatish Balay #undef __FUNCT__
14294a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIBAIJ"
1430b24ad042SBarry Smith PetscErrorCode MatGetRow_MPIBAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1431acdf5bf4SSatish Balay {
1432acdf5bf4SSatish Balay   Mat_MPIBAIJ    *mat = (Mat_MPIBAIJ*)matin->data;
143387828ca2SBarry Smith   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
14346849ba73SBarry Smith   PetscErrorCode ierr;
1435d0f46423SBarry Smith   PetscInt       bs = matin->rmap->bs,bs2 = mat->bs2,i,*cworkA,*cworkB,**pcA,**pcB;
1436d0f46423SBarry Smith   PetscInt       nztot,nzA,nzB,lrow,brstart = matin->rmap->rstart,brend = matin->rmap->rend;
1437899cda47SBarry Smith   PetscInt       *cmap,*idx_p,cstart = mat->cstartbs;
1438acdf5bf4SSatish Balay 
1439d64ed03dSBarry Smith   PetscFunctionBegin;
1440e7e72b3dSBarry Smith   if (row < brstart || row >= brend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local rows");
1441e32f2f54SBarry Smith   if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active");
1442acdf5bf4SSatish Balay   mat->getrowactive = PETSC_TRUE;
1443acdf5bf4SSatish Balay 
1444acdf5bf4SSatish Balay   if (!mat->rowvalues && (idx || v)) {
1445acdf5bf4SSatish Balay     /*
1446acdf5bf4SSatish Balay         allocate enough space to hold information from the longest row.
1447acdf5bf4SSatish Balay     */
1448acdf5bf4SSatish Balay     Mat_SeqBAIJ *Aa = (Mat_SeqBAIJ*)mat->A->data,*Ba = (Mat_SeqBAIJ*)mat->B->data;
1449b24ad042SBarry Smith     PetscInt    max = 1,mbs = mat->mbs,tmp;
1450bd16c2feSSatish Balay     for (i=0; i<mbs; i++) {
1451acdf5bf4SSatish Balay       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
145226fbe8dcSKarl Rupp       if (max < tmp) max = tmp;
1453acdf5bf4SSatish Balay     }
1454fca92195SBarry Smith     ierr = PetscMalloc2(max*bs2,PetscScalar,&mat->rowvalues,max*bs2,PetscInt,&mat->rowindices);CHKERRQ(ierr);
1455acdf5bf4SSatish Balay   }
1456d9d09a02SSatish Balay   lrow = row - brstart;
1457acdf5bf4SSatish Balay 
1458acdf5bf4SSatish Balay   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1459acdf5bf4SSatish Balay   if (!v)   {pvA = 0; pvB = 0;}
1460acdf5bf4SSatish Balay   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1461f830108cSBarry Smith   ierr  = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1462f830108cSBarry Smith   ierr  = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1463acdf5bf4SSatish Balay   nztot = nzA + nzB;
1464acdf5bf4SSatish Balay 
1465acdf5bf4SSatish Balay   cmap = mat->garray;
1466acdf5bf4SSatish Balay   if (v  || idx) {
1467acdf5bf4SSatish Balay     if (nztot) {
1468acdf5bf4SSatish Balay       /* Sort by increasing column numbers, assuming A and B already sorted */
1469b24ad042SBarry Smith       PetscInt imark = -1;
1470acdf5bf4SSatish Balay       if (v) {
1471acdf5bf4SSatish Balay         *v = v_p = mat->rowvalues;
1472acdf5bf4SSatish Balay         for (i=0; i<nzB; i++) {
1473d9d09a02SSatish Balay           if (cmap[cworkB[i]/bs] < cstart) v_p[i] = vworkB[i];
1474acdf5bf4SSatish Balay           else break;
1475acdf5bf4SSatish Balay         }
1476acdf5bf4SSatish Balay         imark = i;
1477acdf5bf4SSatish Balay         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1478acdf5bf4SSatish Balay         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1479acdf5bf4SSatish Balay       }
1480acdf5bf4SSatish Balay       if (idx) {
1481acdf5bf4SSatish Balay         *idx = idx_p = mat->rowindices;
1482acdf5bf4SSatish Balay         if (imark > -1) {
1483acdf5bf4SSatish Balay           for (i=0; i<imark; i++) {
1484bd16c2feSSatish Balay             idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1485acdf5bf4SSatish Balay           }
1486acdf5bf4SSatish Balay         } else {
1487acdf5bf4SSatish Balay           for (i=0; i<nzB; i++) {
148826fbe8dcSKarl Rupp             if (cmap[cworkB[i]/bs] < cstart) idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1489acdf5bf4SSatish Balay             else break;
1490acdf5bf4SSatish Balay           }
1491acdf5bf4SSatish Balay           imark = i;
1492acdf5bf4SSatish Balay         }
1493d9d09a02SSatish Balay         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart*bs + cworkA[i];
1494d9d09a02SSatish Balay         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1495acdf5bf4SSatish Balay       }
1496d64ed03dSBarry Smith     } else {
1497d212a18eSSatish Balay       if (idx) *idx = 0;
1498d212a18eSSatish Balay       if (v)   *v   = 0;
1499d212a18eSSatish Balay     }
1500acdf5bf4SSatish Balay   }
1501acdf5bf4SSatish Balay   *nz  = nztot;
1502f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1503f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
15043a40ed3dSBarry Smith   PetscFunctionReturn(0);
1505acdf5bf4SSatish Balay }
1506acdf5bf4SSatish Balay 
15074a2ae208SSatish Balay #undef __FUNCT__
15084a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIBAIJ"
1509b24ad042SBarry Smith PetscErrorCode MatRestoreRow_MPIBAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1510acdf5bf4SSatish Balay {
1511acdf5bf4SSatish Balay   Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1512d64ed03dSBarry Smith 
1513d64ed03dSBarry Smith   PetscFunctionBegin;
1514e7e72b3dSBarry Smith   if (!baij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
1515acdf5bf4SSatish Balay   baij->getrowactive = PETSC_FALSE;
15163a40ed3dSBarry Smith   PetscFunctionReturn(0);
1517acdf5bf4SSatish Balay }
1518acdf5bf4SSatish Balay 
15194a2ae208SSatish Balay #undef __FUNCT__
15204a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIBAIJ"
1521dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIBAIJ(Mat A)
152258667388SSatish Balay {
152358667388SSatish Balay   Mat_MPIBAIJ    *l = (Mat_MPIBAIJ*)A->data;
1524dfbe8321SBarry Smith   PetscErrorCode ierr;
1525d64ed03dSBarry Smith 
1526d64ed03dSBarry Smith   PetscFunctionBegin;
152758667388SSatish Balay   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
152858667388SSatish Balay   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
15293a40ed3dSBarry Smith   PetscFunctionReturn(0);
153058667388SSatish Balay }
15310ac07820SSatish Balay 
15324a2ae208SSatish Balay #undef __FUNCT__
15334a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIBAIJ"
1534dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIBAIJ(Mat matin,MatInfoType flag,MatInfo *info)
15350ac07820SSatish Balay {
15364e220ebcSLois Curfman McInnes   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)matin->data;
15374e220ebcSLois Curfman McInnes   Mat            A  = a->A,B = a->B;
1538dfbe8321SBarry Smith   PetscErrorCode ierr;
1539329f5518SBarry Smith   PetscReal      isend[5],irecv[5];
15400ac07820SSatish Balay 
1541d64ed03dSBarry Smith   PetscFunctionBegin;
1542d0f46423SBarry Smith   info->block_size = (PetscReal)matin->rmap->bs;
154326fbe8dcSKarl Rupp 
15444e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
154526fbe8dcSKarl Rupp 
15460e4b21beSBarry Smith   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1547de87f314SBarry Smith   isend[3] = info->memory;  isend[4] = info->mallocs;
154826fbe8dcSKarl Rupp 
15494e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
155026fbe8dcSKarl Rupp 
15510e4b21beSBarry Smith   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1552de87f314SBarry Smith   isend[3] += info->memory;  isend[4] += info->mallocs;
155326fbe8dcSKarl Rupp 
15540ac07820SSatish Balay   if (flag == MAT_LOCAL) {
15554e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
15564e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
15574e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
15584e220ebcSLois Curfman McInnes     info->memory       = isend[3];
15594e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
15600ac07820SSatish Balay   } else if (flag == MAT_GLOBAL_MAX) {
1561*ce94432eSBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));CHKERRQ(ierr);
156226fbe8dcSKarl Rupp 
15634e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
15644e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
15654e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
15664e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
15674e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
15680ac07820SSatish Balay   } else if (flag == MAT_GLOBAL_SUM) {
1569*ce94432eSBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));CHKERRQ(ierr);
157026fbe8dcSKarl Rupp 
15714e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
15724e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
15734e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
15744e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
15754e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1576*ce94432eSBarry Smith   } else SETERRQ1(PetscObjectComm((PetscObject)matin),PETSC_ERR_ARG_WRONG,"Unknown MatInfoType argument %d",(int)flag);
15774e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
15784e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
15794e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
15803a40ed3dSBarry Smith   PetscFunctionReturn(0);
15810ac07820SSatish Balay }
15820ac07820SSatish Balay 
15834a2ae208SSatish Balay #undef __FUNCT__
15844a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIBAIJ"
1585ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIBAIJ(Mat A,MatOption op,PetscBool flg)
158658667388SSatish Balay {
158758667388SSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1588dfbe8321SBarry Smith   PetscErrorCode ierr;
158958667388SSatish Balay 
1590d64ed03dSBarry Smith   PetscFunctionBegin;
159112c028f9SKris Buschelman   switch (op) {
1592512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
159312c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
159428b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1595a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
159612c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
15974e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
15984e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
159912c028f9SKris Buschelman     break;
160012c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
16014e0d8c25SBarry Smith     a->roworiented = flg;
160226fbe8dcSKarl Rupp 
16034e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
16044e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
160512c028f9SKris Buschelman     break;
16064e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1607290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
160812c028f9SKris Buschelman     break;
160912c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
16104e0d8c25SBarry Smith     a->donotstash = flg;
161112c028f9SKris Buschelman     break;
161212c028f9SKris Buschelman   case MAT_USE_HASH_TABLE:
16134e0d8c25SBarry Smith     a->ht_flag = flg;
161412c028f9SKris Buschelman     break;
161577e54ba9SKris Buschelman   case MAT_SYMMETRIC:
161677e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
16172188ac68SBarry Smith   case MAT_HERMITIAN:
16182188ac68SBarry Smith   case MAT_SYMMETRY_ETERNAL:
16194e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
162077e54ba9SKris Buschelman     break;
162112c028f9SKris Buschelman   default:
1622*ce94432eSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"unknown option %d",op);
1623d64ed03dSBarry Smith   }
16243a40ed3dSBarry Smith   PetscFunctionReturn(0);
162558667388SSatish Balay }
162658667388SSatish Balay 
16274a2ae208SSatish Balay #undef __FUNCT__
16286a719282SBarry Smith #define __FUNCT__ "MatTranspose_MPIBAIJ"
1629fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIBAIJ(Mat A,MatReuse reuse,Mat *matout)
16300ac07820SSatish Balay {
16310ac07820SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)A->data;
16320ac07820SSatish Balay   Mat_SeqBAIJ    *Aloc;
16330ac07820SSatish Balay   Mat            B;
1634dfbe8321SBarry Smith   PetscErrorCode ierr;
1635d0f46423SBarry Smith   PetscInt       M =A->rmap->N,N=A->cmap->N,*ai,*aj,i,*rvals,j,k,col;
1636d0f46423SBarry Smith   PetscInt       bs=A->rmap->bs,mbs=baij->mbs;
16373eda8832SBarry Smith   MatScalar      *a;
16380ac07820SSatish Balay 
1639d64ed03dSBarry Smith   PetscFunctionBegin;
1640*ce94432eSBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1641fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
1642*ce94432eSBarry Smith     ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
1643d0f46423SBarry Smith     ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr);
16447adad957SLisandro Dalcin     ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr);
16452e72b8d9SBarry Smith     /* Do not know preallocation information, but must set block size */
16460298fd71SBarry Smith     ierr = MatMPIBAIJSetPreallocation(B,A->rmap->bs,PETSC_DECIDE,NULL,PETSC_DECIDE,NULL);CHKERRQ(ierr);
1647fc4dec0aSBarry Smith   } else {
1648fc4dec0aSBarry Smith     B = *matout;
1649fc4dec0aSBarry Smith   }
16500ac07820SSatish Balay 
16510ac07820SSatish Balay   /* copy over the A part */
16520ac07820SSatish Balay   Aloc = (Mat_SeqBAIJ*)baij->A->data;
16530ac07820SSatish Balay   ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1654b24ad042SBarry Smith   ierr = PetscMalloc(bs*sizeof(PetscInt),&rvals);CHKERRQ(ierr);
16550ac07820SSatish Balay 
16560ac07820SSatish Balay   for (i=0; i<mbs; i++) {
1657899cda47SBarry Smith     rvals[0] = bs*(baij->rstartbs + i);
165826fbe8dcSKarl Rupp     for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
16590ac07820SSatish Balay     for (j=ai[i]; j<ai[i+1]; j++) {
1660899cda47SBarry Smith       col = (baij->cstartbs+aj[j])*bs;
16610ac07820SSatish Balay       for (k=0; k<bs; k++) {
166297e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);CHKERRQ(ierr);
166326fbe8dcSKarl Rupp 
16640ac07820SSatish Balay         col++; a += bs;
16650ac07820SSatish Balay       }
16660ac07820SSatish Balay     }
16670ac07820SSatish Balay   }
16680ac07820SSatish Balay   /* copy over the B part */
16690ac07820SSatish Balay   Aloc = (Mat_SeqBAIJ*)baij->B->data;
16700ac07820SSatish Balay   ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
16710ac07820SSatish Balay   for (i=0; i<mbs; i++) {
1672899cda47SBarry Smith     rvals[0] = bs*(baij->rstartbs + i);
167326fbe8dcSKarl Rupp     for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
16740ac07820SSatish Balay     for (j=ai[i]; j<ai[i+1]; j++) {
16750ac07820SSatish Balay       col = baij->garray[aj[j]]*bs;
16760ac07820SSatish Balay       for (k=0; k<bs; k++) {
167797e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);CHKERRQ(ierr);
167826fbe8dcSKarl Rupp         col++;
167926fbe8dcSKarl Rupp         a += bs;
16800ac07820SSatish Balay       }
16810ac07820SSatish Balay     }
16820ac07820SSatish Balay   }
1683606d414cSSatish Balay   ierr = PetscFree(rvals);CHKERRQ(ierr);
16840ac07820SSatish Balay   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16850ac07820SSatish Balay   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16860ac07820SSatish Balay 
168726fbe8dcSKarl Rupp   if (reuse == MAT_INITIAL_MATRIX || *matout != A) *matout = B;
168826fbe8dcSKarl Rupp   else {
1689eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,B);CHKERRQ(ierr);
16900ac07820SSatish Balay   }
16913a40ed3dSBarry Smith   PetscFunctionReturn(0);
16920ac07820SSatish Balay }
16930e95ebc0SSatish Balay 
16944a2ae208SSatish Balay #undef __FUNCT__
16954a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIBAIJ"
1696dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIBAIJ(Mat mat,Vec ll,Vec rr)
16970e95ebc0SSatish Balay {
169836c4a09eSSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
169936c4a09eSSatish Balay   Mat            a     = baij->A,b = baij->B;
1700dfbe8321SBarry Smith   PetscErrorCode ierr;
1701b24ad042SBarry Smith   PetscInt       s1,s2,s3;
17020e95ebc0SSatish Balay 
1703d64ed03dSBarry Smith   PetscFunctionBegin;
170436c4a09eSSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
170536c4a09eSSatish Balay   if (rr) {
170636c4a09eSSatish Balay     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
1707e32f2f54SBarry Smith     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
170836c4a09eSSatish Balay     /* Overlap communication with computation. */
1709ca9f406cSSatish Balay     ierr = VecScatterBegin(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
171036c4a09eSSatish Balay   }
17110e95ebc0SSatish Balay   if (ll) {
17120e95ebc0SSatish Balay     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
1713e32f2f54SBarry Smith     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
17140298fd71SBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,NULL);CHKERRQ(ierr);
17150e95ebc0SSatish Balay   }
171636c4a09eSSatish Balay   /* scale  the diagonal block */
171736c4a09eSSatish Balay   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
171836c4a09eSSatish Balay 
171936c4a09eSSatish Balay   if (rr) {
172036c4a09eSSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
1721ca9f406cSSatish Balay     ierr = VecScatterEnd(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
17220298fd71SBarry Smith     ierr = (*b->ops->diagonalscale)(b,NULL,baij->lvec);CHKERRQ(ierr);
172336c4a09eSSatish Balay   }
17243a40ed3dSBarry Smith   PetscFunctionReturn(0);
17250e95ebc0SSatish Balay }
17260e95ebc0SSatish Balay 
17274a2ae208SSatish Balay #undef __FUNCT__
17284a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIBAIJ"
17292b40b63fSBarry Smith PetscErrorCode MatZeroRows_MPIBAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
17300ac07820SSatish Balay {
17310ac07820SSatish Balay   Mat_MPIBAIJ       *l = (Mat_MPIBAIJ*)A->data;
17326849ba73SBarry Smith   PetscErrorCode    ierr;
1733b24ad042SBarry Smith   PetscMPIInt       imdex,size = l->size,n,rank = l->rank;
1734d0f46423SBarry Smith   PetscInt          i,*owners = A->rmap->range;
1735b24ad042SBarry Smith   PetscInt          *nprocs,j,idx,nsends,row;
1736b24ad042SBarry Smith   PetscInt          nmax,*svalues,*starts,*owner,nrecvs;
17377adad957SLisandro Dalcin   PetscInt          *rvalues,tag = ((PetscObject)A)->tag,count,base,slen,*source,lastidx = -1;
1738d0f46423SBarry Smith   PetscInt          *lens,*lrows,*values,rstart_bs=A->rmap->rstart;
1739*ce94432eSBarry Smith   MPI_Comm          comm;
17400ac07820SSatish Balay   MPI_Request       *send_waits,*recv_waits;
17410ac07820SSatish Balay   MPI_Status        recv_status,*send_status;
174297b48c8fSBarry Smith   const PetscScalar *xx;
174397b48c8fSBarry Smith   PetscScalar       *bb;
17446543fbbaSBarry Smith #if defined(PETSC_DEBUG)
1745ace3abfcSBarry Smith   PetscBool         found = PETSC_FALSE;
17466543fbbaSBarry Smith #endif
17470ac07820SSatish Balay 
1748d64ed03dSBarry Smith   PetscFunctionBegin;
1749*ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr);
17500ac07820SSatish Balay   /*  first count number of contributors to each processor */
1751b24ad042SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
1752b24ad042SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
1753b24ad042SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr);  /* see note*/
17546543fbbaSBarry Smith   j    = 0;
17550ac07820SSatish Balay   for (i=0; i<N; i++) {
17566543fbbaSBarry Smith     if (lastidx > (idx = rows[i])) j = 0;
17576543fbbaSBarry Smith     lastidx = idx;
17586543fbbaSBarry Smith     for (; j<size; j++) {
1759357c27ecSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
17606543fbbaSBarry Smith         nprocs[2*j]++;
17616543fbbaSBarry Smith         nprocs[2*j+1] = 1;
17626543fbbaSBarry Smith         owner[i]      = j;
17636543fbbaSBarry Smith #if defined(PETSC_DEBUG)
17646543fbbaSBarry Smith         found = PETSC_TRUE;
17656543fbbaSBarry Smith #endif
17666543fbbaSBarry Smith         break;
17670ac07820SSatish Balay       }
17680ac07820SSatish Balay     }
17696543fbbaSBarry Smith #if defined(PETSC_DEBUG)
1770e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
17716543fbbaSBarry Smith     found = PETSC_FALSE;
17726543fbbaSBarry Smith #endif
17730ac07820SSatish Balay   }
177426fbe8dcSKarl Rupp   nsends = 0;  for (i=0; i<size; i++) nsends += nprocs[2*i+1];
17750ac07820SSatish Balay 
17767367270fSBarry Smith   if (A->nooffproczerorows) {
17777367270fSBarry 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");
17787367270fSBarry Smith     nrecvs = nsends;
17797367270fSBarry Smith     nmax   = N;
17807367270fSBarry Smith   } else {
17810ac07820SSatish Balay     /* inform other processors of number of messages and max length*/
1782c1dc657dSBarry Smith     ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
17837367270fSBarry Smith   }
17840ac07820SSatish Balay 
17850ac07820SSatish Balay   /* post receives:   */
1786b24ad042SBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr);
1787b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
17880ac07820SSatish Balay   for (i=0; i<nrecvs; i++) {
1789b24ad042SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
17900ac07820SSatish Balay   }
17910ac07820SSatish Balay 
17920ac07820SSatish Balay   /* do sends:
17930ac07820SSatish Balay      1) starts[i] gives the starting index in svalues for stuff going to
17940ac07820SSatish Balay      the ith processor
17950ac07820SSatish Balay   */
1796b24ad042SBarry Smith   ierr      = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr);
1797b0a32e0cSBarry Smith   ierr      = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
1798b24ad042SBarry Smith   ierr      = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
17990ac07820SSatish Balay   starts[0] = 0;
180026fbe8dcSKarl Rupp   for (i=1; i<size; i++) starts[i] = starts[i-1] + nprocs[2*i-2];
18010ac07820SSatish Balay   for (i=0; i<N; i++) {
18020ac07820SSatish Balay     svalues[starts[owner[i]]++] = rows[i];
18030ac07820SSatish Balay   }
18040ac07820SSatish Balay 
18050ac07820SSatish Balay   starts[0] = 0;
180626fbe8dcSKarl Rupp   for (i=1; i<size+1; i++) starts[i] = starts[i-1] + nprocs[2*i-2];
18070ac07820SSatish Balay   count = 0;
18080ac07820SSatish Balay   for (i=0; i<size; i++) {
1809c1dc657dSBarry Smith     if (nprocs[2*i+1]) {
1810b24ad042SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
18110ac07820SSatish Balay     }
18120ac07820SSatish Balay   }
1813606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
18140ac07820SSatish Balay 
1815357c27ecSBarry Smith   base = owners[rank];
18160ac07820SSatish Balay 
18170ac07820SSatish Balay   /*  wait on receives */
1818fca92195SBarry Smith   ierr  = PetscMalloc2(nrecvs+1,PetscInt,&lens,nrecvs+1,PetscInt,&source);CHKERRQ(ierr);
1819fca92195SBarry Smith   count = nrecvs;
1820fca92195SBarry Smith   slen  = 0;
18210ac07820SSatish Balay   while (count) {
1822ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
18230ac07820SSatish Balay     /* unpack receives into our local space */
1824b24ad042SBarry Smith     ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr);
182526fbe8dcSKarl Rupp 
18260ac07820SSatish Balay     source[imdex] = recv_status.MPI_SOURCE;
18270ac07820SSatish Balay     lens[imdex]   = n;
18280ac07820SSatish Balay     slen         += n;
18290ac07820SSatish Balay     count--;
18300ac07820SSatish Balay   }
1831606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
18320ac07820SSatish Balay 
18330ac07820SSatish Balay   /* move the data into the send scatter */
1834b24ad042SBarry Smith   ierr  = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr);
18350ac07820SSatish Balay   count = 0;
18360ac07820SSatish Balay   for (i=0; i<nrecvs; i++) {
18370ac07820SSatish Balay     values = rvalues + i*nmax;
18380ac07820SSatish Balay     for (j=0; j<lens[i]; j++) {
18390ac07820SSatish Balay       lrows[count++] = values[j] - base;
18400ac07820SSatish Balay     }
18410ac07820SSatish Balay   }
1842606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
1843fca92195SBarry Smith   ierr = PetscFree2(lens,source);CHKERRQ(ierr);
1844606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
1845606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
18460ac07820SSatish Balay 
184797b48c8fSBarry Smith   /* fix right hand side if needed */
184897b48c8fSBarry Smith   if (x && b) {
184997b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
185097b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
185197b48c8fSBarry Smith     for (i=0; i<slen; i++) {
185297b48c8fSBarry Smith       bb[lrows[i]] = diag*xx[lrows[i]];
185397b48c8fSBarry Smith     }
185497b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
185597b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
185697b48c8fSBarry Smith   }
185797b48c8fSBarry Smith 
18580ac07820SSatish Balay   /* actually zap the local rows */
185972dacd9aSBarry Smith   /*
186072dacd9aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
1861a8c7a070SBarry Smith      is square and the user wishes to set the diagonal we use separate
186272dacd9aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
186372dacd9aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
186472dacd9aSBarry Smith 
186572dacd9aSBarry Smith   */
18669c957beeSSatish Balay   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
18672b40b63fSBarry Smith   ierr = MatZeroRows_SeqBAIJ(l->B,slen,lrows,0.0,0,0);CHKERRQ(ierr);
1868d0f46423SBarry Smith   if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) {
18692b40b63fSBarry Smith     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,diag,0,0);CHKERRQ(ierr);
1870f4df32b1SMatthew Knepley   } else if (diag != 0.0) {
18712b40b63fSBarry Smith     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr);
1872e7e72b3dSBarry 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\
1873512a5fc5SBarry Smith        MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
1874a07cd24cSSatish Balay     for (i=0; i<slen; i++) {
1875a07cd24cSSatish Balay       row  = lrows[i] + rstart_bs;
1876f4df32b1SMatthew Knepley       ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
1877a07cd24cSSatish Balay     }
1878a07cd24cSSatish Balay     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1879a07cd24cSSatish Balay     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18809c957beeSSatish Balay   } else {
18812b40b63fSBarry Smith     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr);
1882a07cd24cSSatish Balay   }
18839c957beeSSatish Balay 
1884606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
1885a07cd24cSSatish Balay 
18860ac07820SSatish Balay   /* wait on sends */
18870ac07820SSatish Balay   if (nsends) {
188882502324SSatish Balay     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
1889ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
1890606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
18910ac07820SSatish Balay   }
1892606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
1893606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
18943a40ed3dSBarry Smith   PetscFunctionReturn(0);
18950ac07820SSatish Balay }
189672dacd9aSBarry Smith 
18974a2ae208SSatish Balay #undef __FUNCT__
18984a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIBAIJ"
1899dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIBAIJ(Mat A)
1900bb5a7306SBarry Smith {
1901bb5a7306SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1902dfbe8321SBarry Smith   PetscErrorCode ierr;
1903d64ed03dSBarry Smith 
1904d64ed03dSBarry Smith   PetscFunctionBegin;
1905bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
19063a40ed3dSBarry Smith   PetscFunctionReturn(0);
1907bb5a7306SBarry Smith }
1908bb5a7306SBarry Smith 
19096849ba73SBarry Smith static PetscErrorCode MatDuplicate_MPIBAIJ(Mat,MatDuplicateOption,Mat*);
19100ac07820SSatish Balay 
19114a2ae208SSatish Balay #undef __FUNCT__
19124a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIBAIJ"
1913ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIBAIJ(Mat A,Mat B,PetscBool  *flag)
19147fc3c18eSBarry Smith {
19157fc3c18eSBarry Smith   Mat_MPIBAIJ    *matB = (Mat_MPIBAIJ*)B->data,*matA = (Mat_MPIBAIJ*)A->data;
19167fc3c18eSBarry Smith   Mat            a,b,c,d;
1917ace3abfcSBarry Smith   PetscBool      flg;
1918dfbe8321SBarry Smith   PetscErrorCode ierr;
19197fc3c18eSBarry Smith 
19207fc3c18eSBarry Smith   PetscFunctionBegin;
19217fc3c18eSBarry Smith   a = matA->A; b = matA->B;
19227fc3c18eSBarry Smith   c = matB->A; d = matB->B;
19237fc3c18eSBarry Smith 
19247fc3c18eSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1925abc0a331SBarry Smith   if (flg) {
19267fc3c18eSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
19277fc3c18eSBarry Smith   }
1928*ce94432eSBarry Smith   ierr = MPI_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
19297fc3c18eSBarry Smith   PetscFunctionReturn(0);
19307fc3c18eSBarry Smith }
19317fc3c18eSBarry Smith 
19323c896bc6SHong Zhang #undef __FUNCT__
19333c896bc6SHong Zhang #define __FUNCT__ "MatCopy_MPIBAIJ"
19343c896bc6SHong Zhang PetscErrorCode MatCopy_MPIBAIJ(Mat A,Mat B,MatStructure str)
19353c896bc6SHong Zhang {
19363c896bc6SHong Zhang   PetscErrorCode ierr;
19373c896bc6SHong Zhang   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
19383c896bc6SHong Zhang   Mat_MPIBAIJ    *b = (Mat_MPIBAIJ*)B->data;
19393c896bc6SHong Zhang 
19403c896bc6SHong Zhang   PetscFunctionBegin;
19413c896bc6SHong Zhang   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
19423c896bc6SHong Zhang   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
19433c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
19443c896bc6SHong Zhang   } else {
19453c896bc6SHong Zhang     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
19463c896bc6SHong Zhang     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
19473c896bc6SHong Zhang   }
19483c896bc6SHong Zhang   PetscFunctionReturn(0);
19493c896bc6SHong Zhang }
1950273d9f13SBarry Smith 
19514a2ae208SSatish Balay #undef __FUNCT__
19524994cf47SJed Brown #define __FUNCT__ "MatSetUp_MPIBAIJ"
19534994cf47SJed Brown PetscErrorCode MatSetUp_MPIBAIJ(Mat A)
1954273d9f13SBarry Smith {
1955dfbe8321SBarry Smith   PetscErrorCode ierr;
1956273d9f13SBarry Smith 
1957273d9f13SBarry Smith   PetscFunctionBegin;
1958535b19f3SBarry Smith   ierr =  MatMPIBAIJSetPreallocation(A,A->rmap->bs,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1959273d9f13SBarry Smith   PetscFunctionReturn(0);
1960273d9f13SBarry Smith }
1961273d9f13SBarry Smith 
19624fe895cdSHong Zhang #undef __FUNCT__
19634fe895cdSHong Zhang #define __FUNCT__ "MatAXPY_MPIBAIJ"
19644fe895cdSHong Zhang PetscErrorCode MatAXPY_MPIBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
19654fe895cdSHong Zhang {
19664fe895cdSHong Zhang   PetscErrorCode ierr;
19674fe895cdSHong Zhang   Mat_MPIBAIJ    *xx=(Mat_MPIBAIJ*)X->data,*yy=(Mat_MPIBAIJ*)Y->data;
19684fe895cdSHong Zhang   PetscBLASInt   bnz,one=1;
19694fe895cdSHong Zhang   Mat_SeqBAIJ    *x,*y;
19704fe895cdSHong Zhang 
19714fe895cdSHong Zhang   PetscFunctionBegin;
19724fe895cdSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
19734fe895cdSHong Zhang     PetscScalar alpha = a;
19744fe895cdSHong Zhang     x    = (Mat_SeqBAIJ*)xx->A->data;
19754fe895cdSHong Zhang     y    = (Mat_SeqBAIJ*)yy->A->data;
1976c5df96a5SBarry Smith     ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
1977a83cb05cSBarry Smith     PetscStackCall("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
19784fe895cdSHong Zhang     x    = (Mat_SeqBAIJ*)xx->B->data;
19794fe895cdSHong Zhang     y    = (Mat_SeqBAIJ*)yy->B->data;
1980c5df96a5SBarry Smith     ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr);
1981a83cb05cSBarry Smith     PetscStackCall("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
19824fe895cdSHong Zhang   } else {
19834fe895cdSHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
19844fe895cdSHong Zhang   }
19854fe895cdSHong Zhang   PetscFunctionReturn(0);
19864fe895cdSHong Zhang }
19874fe895cdSHong Zhang 
198899cafbc1SBarry Smith #undef __FUNCT__
198999cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIBAIJ"
199099cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIBAIJ(Mat A)
199199cafbc1SBarry Smith {
199299cafbc1SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
199399cafbc1SBarry Smith   PetscErrorCode ierr;
199499cafbc1SBarry Smith 
199599cafbc1SBarry Smith   PetscFunctionBegin;
199699cafbc1SBarry Smith   ierr = MatRealPart(a->A);CHKERRQ(ierr);
199799cafbc1SBarry Smith   ierr = MatRealPart(a->B);CHKERRQ(ierr);
199899cafbc1SBarry Smith   PetscFunctionReturn(0);
199999cafbc1SBarry Smith }
200099cafbc1SBarry Smith 
200199cafbc1SBarry Smith #undef __FUNCT__
200299cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIBAIJ"
200399cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIBAIJ(Mat A)
200499cafbc1SBarry Smith {
200599cafbc1SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
200699cafbc1SBarry Smith   PetscErrorCode ierr;
200799cafbc1SBarry Smith 
200899cafbc1SBarry Smith   PetscFunctionBegin;
200999cafbc1SBarry Smith   ierr = MatImaginaryPart(a->A);CHKERRQ(ierr);
201099cafbc1SBarry Smith   ierr = MatImaginaryPart(a->B);CHKERRQ(ierr);
201199cafbc1SBarry Smith   PetscFunctionReturn(0);
201299cafbc1SBarry Smith }
201399cafbc1SBarry Smith 
201482094794SBarry Smith #undef __FUNCT__
201582094794SBarry Smith #define __FUNCT__ "MatGetSubMatrix_MPIBAIJ"
20164aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIBAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
20174aa3045dSJed Brown {
20184aa3045dSJed Brown   PetscErrorCode ierr;
20194aa3045dSJed Brown   IS             iscol_local;
20204aa3045dSJed Brown   PetscInt       csize;
20214aa3045dSJed Brown 
20224aa3045dSJed Brown   PetscFunctionBegin;
20234aa3045dSJed Brown   ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr);
2024b79d0421SJed Brown   if (call == MAT_REUSE_MATRIX) {
2025b79d0421SJed Brown     ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr);
2026e32f2f54SBarry Smith     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2027b79d0421SJed Brown   } else {
20284aa3045dSJed Brown     ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr);
2029b79d0421SJed Brown   }
20304aa3045dSJed Brown   ierr = MatGetSubMatrix_MPIBAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr);
2031b79d0421SJed Brown   if (call == MAT_INITIAL_MATRIX) {
2032b79d0421SJed Brown     ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr);
20336bf464f9SBarry Smith     ierr = ISDestroy(&iscol_local);CHKERRQ(ierr);
2034b79d0421SJed Brown   }
20354aa3045dSJed Brown   PetscFunctionReturn(0);
20364aa3045dSJed Brown }
203729dcf524SDmitry Karpeev extern PetscErrorCode MatGetSubMatrices_MPIBAIJ_local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool*,PetscBool*,Mat*);
20384aa3045dSJed Brown #undef __FUNCT__
2039dd183c9eSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIBAIJ_Private"
204082094794SBarry Smith /*
204182094794SBarry Smith   Not great since it makes two copies of the submatrix, first an SeqBAIJ
204282094794SBarry Smith   in local and then by concatenating the local matrices the end result.
204382094794SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIBAIJ()
204482094794SBarry Smith */
20454aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIBAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
204682094794SBarry Smith {
204782094794SBarry Smith   PetscErrorCode ierr;
204882094794SBarry Smith   PetscMPIInt    rank,size;
204982094794SBarry Smith   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs;
205029dcf524SDmitry Karpeev   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol,nrow;
205129dcf524SDmitry Karpeev   Mat            M,Mreuse;
205282094794SBarry Smith   MatScalar      *vwork,*aa;
2053*ce94432eSBarry Smith   MPI_Comm       comm;
205429dcf524SDmitry Karpeev   IS             isrow_new, iscol_new;
205529dcf524SDmitry Karpeev   PetscBool      idflag,allrows, allcols;
205682094794SBarry Smith   Mat_SeqBAIJ    *aij;
205782094794SBarry Smith 
205882094794SBarry Smith   PetscFunctionBegin;
2059*ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr);
206082094794SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
206182094794SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
206229dcf524SDmitry Karpeev   /* The compression and expansion should be avoided. Doesn't point
206329dcf524SDmitry Karpeev      out errors, might change the indices, hence buggey */
206429dcf524SDmitry Karpeev   ierr = ISCompressIndicesGeneral(mat->rmap->N,mat->rmap->n,mat->rmap->bs,1,&isrow,&isrow_new);CHKERRQ(ierr);
206529dcf524SDmitry Karpeev   ierr = ISCompressIndicesGeneral(mat->cmap->N,mat->cmap->n,mat->cmap->bs,1,&iscol,&iscol_new);CHKERRQ(ierr);
206682094794SBarry Smith 
206729dcf524SDmitry Karpeev   /* Check for special case: each processor gets entire matrix columns */
206829dcf524SDmitry Karpeev   ierr = ISIdentity(iscol,&idflag);CHKERRQ(ierr);
206929dcf524SDmitry Karpeev   ierr = ISGetLocalSize(iscol,&ncol);CHKERRQ(ierr);
207026fbe8dcSKarl Rupp   if (idflag && ncol == mat->cmap->N) allcols = PETSC_TRUE;
207126fbe8dcSKarl Rupp   else allcols = PETSC_FALSE;
207229dcf524SDmitry Karpeev 
207329dcf524SDmitry Karpeev   ierr = ISIdentity(isrow,&idflag);CHKERRQ(ierr);
207429dcf524SDmitry Karpeev   ierr = ISGetLocalSize(isrow,&nrow);CHKERRQ(ierr);
207526fbe8dcSKarl Rupp   if (idflag && nrow == mat->rmap->N) allrows = PETSC_TRUE;
207626fbe8dcSKarl Rupp   else allrows = PETSC_FALSE;
207726fbe8dcSKarl Rupp 
207882094794SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
207982094794SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);CHKERRQ(ierr);
2080e32f2f54SBarry Smith     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
208175f6568bSJed Brown     ierr = MatGetSubMatrices_MPIBAIJ_local(mat,1,&isrow_new,&iscol_new,MAT_REUSE_MATRIX,&allrows,&allcols,&Mreuse);CHKERRQ(ierr);
208282094794SBarry Smith   } else {
208375f6568bSJed Brown     ierr = MatGetSubMatrices_MPIBAIJ_local(mat,1,&isrow_new,&iscol_new,MAT_INITIAL_MATRIX,&allrows,&allcols,&Mreuse);CHKERRQ(ierr);
208482094794SBarry Smith   }
208529dcf524SDmitry Karpeev   ierr = ISDestroy(&isrow_new);CHKERRQ(ierr);
208629dcf524SDmitry Karpeev   ierr = ISDestroy(&iscol_new);CHKERRQ(ierr);
208782094794SBarry Smith   /*
208882094794SBarry Smith       m - number of local rows
208982094794SBarry Smith       n - number of columns (same on all processors)
209082094794SBarry Smith       rstart - first row in new global matrix generated
209182094794SBarry Smith   */
209282094794SBarry Smith   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
209382094794SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
209482094794SBarry Smith   m    = m/bs;
209582094794SBarry Smith   n    = n/bs;
209682094794SBarry Smith 
209782094794SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
209882094794SBarry Smith     aij = (Mat_SeqBAIJ*)(Mreuse)->data;
209982094794SBarry Smith     ii  = aij->i;
210082094794SBarry Smith     jj  = aij->j;
210182094794SBarry Smith 
210282094794SBarry Smith     /*
210382094794SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
210482094794SBarry Smith         portions of the matrix in order to do correct preallocation
210582094794SBarry Smith     */
210682094794SBarry Smith 
210782094794SBarry Smith     /* first get start and end of "diagonal" columns */
210882094794SBarry Smith     if (csize == PETSC_DECIDE) {
210982094794SBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
211082094794SBarry Smith       if (mglobal == n*bs) { /* square matrix */
211182094794SBarry Smith         nlocal = m;
211282094794SBarry Smith       } else {
211382094794SBarry Smith         nlocal = n/size + ((n % size) > rank);
211482094794SBarry Smith       }
211582094794SBarry Smith     } else {
211682094794SBarry Smith       nlocal = csize/bs;
211782094794SBarry Smith     }
211882094794SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
211982094794SBarry Smith     rstart = rend - nlocal;
212065e19b50SBarry 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);
212182094794SBarry Smith 
212282094794SBarry Smith     /* next, compute all the lengths */
212382094794SBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr);
212482094794SBarry Smith     olens = dlens + m;
212582094794SBarry Smith     for (i=0; i<m; i++) {
212682094794SBarry Smith       jend = ii[i+1] - ii[i];
212782094794SBarry Smith       olen = 0;
212882094794SBarry Smith       dlen = 0;
212982094794SBarry Smith       for (j=0; j<jend; j++) {
213082094794SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
213182094794SBarry Smith         else dlen++;
213282094794SBarry Smith         jj++;
213382094794SBarry Smith       }
213482094794SBarry Smith       olens[i] = olen;
213582094794SBarry Smith       dlens[i] = dlen;
213682094794SBarry Smith     }
213782094794SBarry Smith     ierr = MatCreate(comm,&M);CHKERRQ(ierr);
213882094794SBarry Smith     ierr = MatSetSizes(M,bs*m,bs*nlocal,PETSC_DECIDE,bs*n);CHKERRQ(ierr);
213982094794SBarry Smith     ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr);
214082094794SBarry Smith     ierr = MatMPIBAIJSetPreallocation(M,bs,0,dlens,0,olens);CHKERRQ(ierr);
214182094794SBarry Smith     ierr = PetscFree(dlens);CHKERRQ(ierr);
214282094794SBarry Smith   } else {
214382094794SBarry Smith     PetscInt ml,nl;
214482094794SBarry Smith 
214582094794SBarry Smith     M    = *newmat;
214682094794SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
2147e32f2f54SBarry Smith     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
214882094794SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
214982094794SBarry Smith     /*
215082094794SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
215182094794SBarry Smith        rather than the slower MatSetValues().
215282094794SBarry Smith     */
215382094794SBarry Smith     M->was_assembled = PETSC_TRUE;
215482094794SBarry Smith     M->assembled     = PETSC_FALSE;
215582094794SBarry Smith   }
215682094794SBarry Smith   ierr = MatSetOption(M,MAT_ROW_ORIENTED,PETSC_FALSE);CHKERRQ(ierr);
215782094794SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
215882094794SBarry Smith   aij  = (Mat_SeqBAIJ*)(Mreuse)->data;
215982094794SBarry Smith   ii   = aij->i;
216082094794SBarry Smith   jj   = aij->j;
216182094794SBarry Smith   aa   = aij->a;
216282094794SBarry Smith   for (i=0; i<m; i++) {
216382094794SBarry Smith     row   = rstart/bs + i;
216482094794SBarry Smith     nz    = ii[i+1] - ii[i];
216582094794SBarry Smith     cwork = jj;     jj += nz;
216675f6568bSJed Brown     vwork = aa;     aa += nz*bs*bs;
216782094794SBarry Smith     ierr  = MatSetValuesBlocked_MPIBAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
216882094794SBarry Smith   }
216982094794SBarry Smith 
217082094794SBarry Smith   ierr    = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
217182094794SBarry Smith   ierr    = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
217282094794SBarry Smith   *newmat = M;
217382094794SBarry Smith 
217482094794SBarry Smith   /* save submatrix used in processor for next request */
217582094794SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
217682094794SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
217782094794SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
217882094794SBarry Smith   }
217982094794SBarry Smith   PetscFunctionReturn(0);
218082094794SBarry Smith }
218182094794SBarry Smith 
218282094794SBarry Smith #undef __FUNCT__
218382094794SBarry Smith #define __FUNCT__ "MatPermute_MPIBAIJ"
218482094794SBarry Smith PetscErrorCode MatPermute_MPIBAIJ(Mat A,IS rowp,IS colp,Mat *B)
218582094794SBarry Smith {
218682094794SBarry Smith   MPI_Comm       comm,pcomm;
218775f6568bSJed Brown   PetscInt       first,rlocal_size,clocal_size,nrows;
218882094794SBarry Smith   const PetscInt *rows;
2189dbf0e21dSBarry Smith   PetscMPIInt    size;
219075f6568bSJed Brown   IS             crowp,growp,irowp,lrowp,lcolp;
219182094794SBarry Smith   PetscErrorCode ierr;
219282094794SBarry Smith 
219382094794SBarry Smith   PetscFunctionBegin;
219482094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr);
219582094794SBarry Smith   /* make a collective version of 'rowp' */
219682094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)rowp,&pcomm);CHKERRQ(ierr);
219782094794SBarry Smith   if (pcomm==comm) {
219882094794SBarry Smith     crowp = rowp;
219982094794SBarry Smith   } else {
220082094794SBarry Smith     ierr = ISGetSize(rowp,&nrows);CHKERRQ(ierr);
220182094794SBarry Smith     ierr = ISGetIndices(rowp,&rows);CHKERRQ(ierr);
220270b3c8c7SBarry Smith     ierr = ISCreateGeneral(comm,nrows,rows,PETSC_COPY_VALUES,&crowp);CHKERRQ(ierr);
220382094794SBarry Smith     ierr = ISRestoreIndices(rowp,&rows);CHKERRQ(ierr);
220482094794SBarry Smith   }
220582094794SBarry Smith   /* collect the global row permutation and invert it */
220682094794SBarry Smith   ierr = ISAllGather(crowp,&growp);CHKERRQ(ierr);
220782094794SBarry Smith   ierr = ISSetPermutation(growp);CHKERRQ(ierr);
220882094794SBarry Smith   if (pcomm!=comm) {
22096bf464f9SBarry Smith     ierr = ISDestroy(&crowp);CHKERRQ(ierr);
221082094794SBarry Smith   }
221182094794SBarry Smith   ierr = ISInvertPermutation(growp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
221275f6568bSJed Brown   ierr = ISDestroy(&growp);CHKERRQ(ierr);
221382094794SBarry Smith   /* get the local target indices */
22140298fd71SBarry Smith   ierr = MatGetOwnershipRange(A,&first,NULL);CHKERRQ(ierr);
221575f6568bSJed Brown   ierr = MatGetLocalSize(A,&rlocal_size,&clocal_size);CHKERRQ(ierr);
221682094794SBarry Smith   ierr = ISGetIndices(irowp,&rows);CHKERRQ(ierr);
221775f6568bSJed Brown   ierr = ISCreateGeneral(MPI_COMM_SELF,rlocal_size,rows+first,PETSC_COPY_VALUES,&lrowp);CHKERRQ(ierr);
221882094794SBarry Smith   ierr = ISRestoreIndices(irowp,&rows);CHKERRQ(ierr);
22196bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
222082094794SBarry Smith   /* the column permutation is so much easier;
222182094794SBarry Smith      make a local version of 'colp' and invert it */
222282094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)colp,&pcomm);CHKERRQ(ierr);
2223dbf0e21dSBarry Smith   ierr = MPI_Comm_size(pcomm,&size);CHKERRQ(ierr);
2224dbf0e21dSBarry Smith   if (size==1) {
222582094794SBarry Smith     lcolp = colp;
222682094794SBarry Smith   } else {
222775f6568bSJed Brown     ierr = ISAllGather(colp,&lcolp);CHKERRQ(ierr);
222882094794SBarry Smith   }
2229dbf0e21dSBarry Smith   ierr = ISSetPermutation(lcolp);CHKERRQ(ierr);
223075f6568bSJed Brown   /* now we just get the submatrix */
223175f6568bSJed Brown   ierr = MatGetSubMatrix_MPIBAIJ_Private(A,lrowp,lcolp,clocal_size,MAT_INITIAL_MATRIX,B);CHKERRQ(ierr);
2232dbf0e21dSBarry Smith   if (size>1) {
22336bf464f9SBarry Smith     ierr = ISDestroy(&lcolp);CHKERRQ(ierr);
223482094794SBarry Smith   }
223582094794SBarry Smith   /* clean up */
22366bf464f9SBarry Smith   ierr = ISDestroy(&lrowp);CHKERRQ(ierr);
223782094794SBarry Smith   PetscFunctionReturn(0);
223882094794SBarry Smith }
223982094794SBarry Smith 
22408c7482ecSBarry Smith #undef __FUNCT__
22418c7482ecSBarry Smith #define __FUNCT__ "MatGetGhosts_MPIBAIJ"
22427087cfbeSBarry Smith PetscErrorCode  MatGetGhosts_MPIBAIJ(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
22438c7482ecSBarry Smith {
22448c7482ecSBarry Smith   Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*) mat->data;
22458c7482ecSBarry Smith   Mat_SeqBAIJ *B    = (Mat_SeqBAIJ*)baij->B->data;
22468c7482ecSBarry Smith 
22478c7482ecSBarry Smith   PetscFunctionBegin;
224826fbe8dcSKarl Rupp   if (nghosts) *nghosts = B->nbs;
224926fbe8dcSKarl Rupp   if (ghosts) *ghosts = baij->garray;
22508c7482ecSBarry Smith   PetscFunctionReturn(0);
22518c7482ecSBarry Smith }
22528c7482ecSBarry Smith 
2253ab9863d7SBarry Smith extern PetscErrorCode MatCreateColmap_MPIBAIJ_Private(Mat);
2254f6d58c54SBarry Smith 
2255f6d58c54SBarry Smith #undef __FUNCT__
2256f6d58c54SBarry Smith #define __FUNCT__ "MatFDColoringCreate_MPIBAIJ"
2257f6d58c54SBarry Smith /*
2258f6d58c54SBarry Smith     This routine is almost identical to MatFDColoringCreate_MPIBAIJ()!
2259f6d58c54SBarry Smith */
2260f6d58c54SBarry Smith PetscErrorCode MatFDColoringCreate_MPIBAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
2261f6d58c54SBarry Smith {
2262f6d58c54SBarry Smith   Mat_MPIBAIJ            *baij = (Mat_MPIBAIJ*)mat->data;
2263f6d58c54SBarry Smith   PetscErrorCode         ierr;
2264f6d58c54SBarry Smith   PetscMPIInt            size,*ncolsonproc,*disp,nn;
22651a83f524SJed Brown   PetscInt               bs,i,n,nrows,j,k,m,ncols,col;
2266afcb2eb5SJed Brown   const PetscInt         *is,*rows = 0,*A_ci,*A_cj,*B_ci,*B_cj,*ltog;
22671a83f524SJed Brown   PetscInt               nis = iscoloring->n,nctot,*cols;
2268f6d58c54SBarry Smith   PetscInt               *rowhit,M,cstart,cend,colb;
2269f6d58c54SBarry Smith   PetscInt               *columnsforrow,l;
2270f6d58c54SBarry Smith   IS                     *isa;
2271ace3abfcSBarry Smith   PetscBool              done,flg;
2272992144d0SBarry Smith   ISLocalToGlobalMapping map = mat->cmap->bmapping;
2273afcb2eb5SJed Brown   PetscInt               ctype=c->ctype;
2274f6d58c54SBarry Smith 
2275f6d58c54SBarry Smith   PetscFunctionBegin;
2276*ce94432eSBarry Smith   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();");
2277*ce94432eSBarry Smith   if (ctype == IS_COLORING_GHOSTED && !map) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_INCOMP,"When using ghosted differencing matrix must have local to global mapping provided with MatSetLocalToGlobalMappingBlock");
2278f6d58c54SBarry Smith 
2279afcb2eb5SJed Brown   if (map) {ierr = ISLocalToGlobalMappingGetIndices(map,&ltog);CHKERRQ(ierr);}
2280afcb2eb5SJed Brown   else     ltog = NULL;
2281f6d58c54SBarry Smith   ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr);
2282f6d58c54SBarry Smith   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
228326fbe8dcSKarl Rupp 
2284f6d58c54SBarry Smith   M         = mat->rmap->n/bs;
2285f6d58c54SBarry Smith   cstart    = mat->cmap->rstart/bs;
2286f6d58c54SBarry Smith   cend      = mat->cmap->rend/bs;
2287f6d58c54SBarry Smith   c->M      = mat->rmap->N/bs;         /* set the global rows and columns and local rows */
2288f6d58c54SBarry Smith   c->N      = mat->cmap->N/bs;
2289f6d58c54SBarry Smith   c->m      = mat->rmap->n/bs;
2290f6d58c54SBarry Smith   c->rstart = mat->rmap->rstart/bs;
2291f6d58c54SBarry Smith 
2292f6d58c54SBarry Smith   c->ncolors = nis;
2293f6d58c54SBarry Smith   ierr       = PetscMalloc(nis*sizeof(PetscInt),&c->ncolumns);CHKERRQ(ierr);
2294f6d58c54SBarry Smith   ierr       = PetscMalloc(nis*sizeof(PetscInt*),&c->columns);CHKERRQ(ierr);
2295f6d58c54SBarry Smith   ierr       = PetscMalloc(nis*sizeof(PetscInt),&c->nrows);CHKERRQ(ierr);
2296f6d58c54SBarry Smith   ierr       = PetscMalloc(nis*sizeof(PetscInt*),&c->rows);CHKERRQ(ierr);
2297f6d58c54SBarry Smith   ierr       = PetscMalloc(nis*sizeof(PetscInt*),&c->columnsforrow);CHKERRQ(ierr);
2298f6d58c54SBarry Smith   ierr       = PetscLogObjectMemory(c,5*nis*sizeof(PetscInt));CHKERRQ(ierr);
2299f6d58c54SBarry Smith 
2300f6d58c54SBarry Smith   /* Allow access to data structures of local part of matrix */
2301f6d58c54SBarry Smith   if (!baij->colmap) {
2302ab9863d7SBarry Smith     ierr = MatCreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
2303f6d58c54SBarry Smith   }
2304f6d58c54SBarry Smith   ierr = MatGetColumnIJ(baij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
2305f6d58c54SBarry Smith   ierr = MatGetColumnIJ(baij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
2306f6d58c54SBarry Smith 
2307f6d58c54SBarry Smith   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&rowhit);CHKERRQ(ierr);
2308f6d58c54SBarry Smith   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&columnsforrow);CHKERRQ(ierr);
2309f6d58c54SBarry Smith 
2310f6d58c54SBarry Smith   for (i=0; i<nis; i++) {
2311f6d58c54SBarry Smith     ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr);
2312f6d58c54SBarry Smith     ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr);
231326fbe8dcSKarl Rupp 
2314f6d58c54SBarry Smith     c->ncolumns[i] = n;
2315f6d58c54SBarry Smith     if (n) {
2316f6d58c54SBarry Smith       ierr = PetscMalloc(n*sizeof(PetscInt),&c->columns[i]);CHKERRQ(ierr);
2317f6d58c54SBarry Smith       ierr = PetscLogObjectMemory(c,n*sizeof(PetscInt));CHKERRQ(ierr);
2318f6d58c54SBarry Smith       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));CHKERRQ(ierr);
2319f6d58c54SBarry Smith     } else {
2320f6d58c54SBarry Smith       c->columns[i] = 0;
2321f6d58c54SBarry Smith     }
2322f6d58c54SBarry Smith 
2323f6d58c54SBarry Smith     if (ctype == IS_COLORING_GLOBAL) {
2324f6d58c54SBarry Smith       /* Determine the total (parallel) number of columns of this color */
2325*ce94432eSBarry Smith       ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr);
2326fca92195SBarry Smith       ierr = PetscMalloc2(size,PetscMPIInt,&ncolsonproc,size,PetscMPIInt,&disp);CHKERRQ(ierr);
2327f6d58c54SBarry Smith 
23284dc2109aSBarry Smith       ierr  = PetscMPIIntCast(n,&nn);CHKERRQ(ierr);
2329*ce94432eSBarry Smith       ierr  = MPI_Allgather(&nn,1,MPI_INT,ncolsonproc,1,MPI_INT,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
233026fbe8dcSKarl Rupp       nctot = 0; for (j=0; j<size; j++) nctot += ncolsonproc[j];
2331f6d58c54SBarry Smith       if (!nctot) {
2332f6d58c54SBarry Smith         ierr = PetscInfo(mat,"Coloring of matrix has some unneeded colors with no corresponding rows\n");CHKERRQ(ierr);
2333f6d58c54SBarry Smith       }
2334f6d58c54SBarry Smith 
2335f6d58c54SBarry Smith       disp[0] = 0;
2336f6d58c54SBarry Smith       for (j=1; j<size; j++) {
2337f6d58c54SBarry Smith         disp[j] = disp[j-1] + ncolsonproc[j-1];
2338f6d58c54SBarry Smith       }
2339f6d58c54SBarry Smith 
2340f6d58c54SBarry Smith       /* Get complete list of columns for color on each processor */
2341f6d58c54SBarry Smith       ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
2342*ce94432eSBarry Smith       ierr = MPI_Allgatherv((void*)is,n,MPIU_INT,cols,ncolsonproc,disp,MPIU_INT,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr);
2343fca92195SBarry Smith       ierr = PetscFree2(ncolsonproc,disp);CHKERRQ(ierr);
2344f6d58c54SBarry Smith     } else if (ctype == IS_COLORING_GHOSTED) {
2345f6d58c54SBarry Smith       /* Determine local number of columns of this color on this process, including ghost points */
2346f6d58c54SBarry Smith       nctot = n;
2347f6d58c54SBarry Smith       ierr  = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
2348f6d58c54SBarry Smith       ierr  = PetscMemcpy(cols,is,n*sizeof(PetscInt));CHKERRQ(ierr);
2349f23aa3ddSBarry Smith     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not provided for this MatFDColoring type");
2350f6d58c54SBarry Smith 
2351f6d58c54SBarry Smith     /*
2352f6d58c54SBarry Smith        Mark all rows affect by these columns
2353f6d58c54SBarry Smith     */
2354f6d58c54SBarry Smith     /* Temporary option to allow for debugging/testing */
2355f6d58c54SBarry Smith     flg  = PETSC_FALSE;
23560298fd71SBarry Smith     ierr = PetscOptionsGetBool(NULL,"-matfdcoloring_slow",&flg,NULL);CHKERRQ(ierr);
2357f6d58c54SBarry Smith     if (!flg) { /*-----------------------------------------------------------------------------*/
2358f6d58c54SBarry Smith       /* crude, fast version */
2359f6d58c54SBarry Smith       ierr = PetscMemzero(rowhit,M*sizeof(PetscInt));CHKERRQ(ierr);
2360f6d58c54SBarry Smith       /* loop over columns*/
2361f6d58c54SBarry Smith       for (j=0; j<nctot; j++) {
2362f6d58c54SBarry Smith         if (ctype == IS_COLORING_GHOSTED) {
2363f6d58c54SBarry Smith           col = ltog[cols[j]];
2364f6d58c54SBarry Smith         } else {
2365f6d58c54SBarry Smith           col = cols[j];
2366f6d58c54SBarry Smith         }
2367f6d58c54SBarry Smith         if (col >= cstart && col < cend) {
2368f6d58c54SBarry Smith           /* column is in diagonal block of matrix */
2369f6d58c54SBarry Smith           rows = A_cj + A_ci[col-cstart];
2370f6d58c54SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
2371f6d58c54SBarry Smith         } else {
2372f6d58c54SBarry Smith #if defined(PETSC_USE_CTABLE)
2373cb9801acSJed Brown           ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr);
2374f6d58c54SBarry Smith           colb--;
2375f6d58c54SBarry Smith #else
2376f6d58c54SBarry Smith           colb = baij->colmap[col] - 1;
2377f6d58c54SBarry Smith #endif
2378f6d58c54SBarry Smith           if (colb == -1) {
2379f6d58c54SBarry Smith             m = 0;
2380f6d58c54SBarry Smith           } else {
2381f6d58c54SBarry Smith             colb = colb/bs;
2382f6d58c54SBarry Smith             rows = B_cj + B_ci[colb];
2383f6d58c54SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
2384f6d58c54SBarry Smith           }
2385f6d58c54SBarry Smith         }
2386f6d58c54SBarry Smith         /* loop over columns marking them in rowhit */
2387f6d58c54SBarry Smith         for (k=0; k<m; k++) {
2388f6d58c54SBarry Smith           rowhit[*rows++] = col + 1;
2389f6d58c54SBarry Smith         }
2390f6d58c54SBarry Smith       }
2391f6d58c54SBarry Smith 
2392f6d58c54SBarry Smith       /* count the number of hits */
2393f6d58c54SBarry Smith       nrows = 0;
2394f6d58c54SBarry Smith       for (j=0; j<M; j++) {
2395f6d58c54SBarry Smith         if (rowhit[j]) nrows++;
2396f6d58c54SBarry Smith       }
2397f6d58c54SBarry Smith       c->nrows[i] = nrows;
2398f6d58c54SBarry Smith       ierr        = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr);
2399f6d58c54SBarry Smith       ierr        = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr);
2400f6d58c54SBarry Smith       ierr        = PetscLogObjectMemory(c,2*(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr);
2401f6d58c54SBarry Smith       nrows       = 0;
2402f6d58c54SBarry Smith       for (j=0; j<M; j++) {
2403f6d58c54SBarry Smith         if (rowhit[j]) {
2404f6d58c54SBarry Smith           c->rows[i][nrows]          = j;
2405f6d58c54SBarry Smith           c->columnsforrow[i][nrows] = rowhit[j] - 1;
2406f6d58c54SBarry Smith           nrows++;
2407f6d58c54SBarry Smith         }
2408f6d58c54SBarry Smith       }
2409f6d58c54SBarry Smith     } else { /*-------------------------------------------------------------------------------*/
2410f6d58c54SBarry Smith       /* slow version, using rowhit as a linked list */
2411f6d58c54SBarry Smith       PetscInt currentcol,fm,mfm;
2412f6d58c54SBarry Smith       rowhit[M] = M;
2413f6d58c54SBarry Smith       nrows     = 0;
2414f6d58c54SBarry Smith       /* loop over columns*/
2415f6d58c54SBarry Smith       for (j=0; j<nctot; j++) {
2416f6d58c54SBarry Smith         if (ctype == IS_COLORING_GHOSTED) {
2417f6d58c54SBarry Smith           col = ltog[cols[j]];
2418f6d58c54SBarry Smith         } else {
2419f6d58c54SBarry Smith           col = cols[j];
2420f6d58c54SBarry Smith         }
2421f6d58c54SBarry Smith         if (col >= cstart && col < cend) {
2422f6d58c54SBarry Smith           /* column is in diagonal block of matrix */
2423f6d58c54SBarry Smith           rows = A_cj + A_ci[col-cstart];
2424f6d58c54SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
2425f6d58c54SBarry Smith         } else {
2426f6d58c54SBarry Smith #if defined(PETSC_USE_CTABLE)
2427f6d58c54SBarry Smith           ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr);
2428f6d58c54SBarry Smith           colb--;
2429f6d58c54SBarry Smith #else
2430f6d58c54SBarry Smith           colb = baij->colmap[col] - 1;
2431f6d58c54SBarry Smith #endif
2432f6d58c54SBarry Smith           if (colb == -1) {
2433f6d58c54SBarry Smith             m = 0;
2434f6d58c54SBarry Smith           } else {
2435f6d58c54SBarry Smith             colb = colb/bs;
2436f6d58c54SBarry Smith             rows = B_cj + B_ci[colb];
2437f6d58c54SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
2438f6d58c54SBarry Smith           }
2439f6d58c54SBarry Smith         }
2440f6d58c54SBarry Smith 
2441f6d58c54SBarry Smith         /* loop over columns marking them in rowhit */
2442f6d58c54SBarry Smith         fm = M;    /* fm points to first entry in linked list */
2443f6d58c54SBarry Smith         for (k=0; k<m; k++) {
2444f6d58c54SBarry Smith           currentcol = *rows++;
2445f6d58c54SBarry Smith           /* is it already in the list? */
2446f6d58c54SBarry Smith           do {
2447f6d58c54SBarry Smith             mfm = fm;
2448f6d58c54SBarry Smith             fm  = rowhit[fm];
2449f6d58c54SBarry Smith           } while (fm < currentcol);
2450f6d58c54SBarry Smith           /* not in list so add it */
2451f6d58c54SBarry Smith           if (fm != currentcol) {
2452f6d58c54SBarry Smith             nrows++;
2453f6d58c54SBarry Smith             columnsforrow[currentcol] = col;
2454f6d58c54SBarry Smith             /* next three lines insert new entry into linked list */
2455f6d58c54SBarry Smith             rowhit[mfm]        = currentcol;
2456f6d58c54SBarry Smith             rowhit[currentcol] = fm;
2457f6d58c54SBarry Smith             fm                 = currentcol;
2458f6d58c54SBarry Smith             /* fm points to present position in list since we know the columns are sorted */
2459f23aa3ddSBarry Smith           } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid coloring of matrix detected");
2460f6d58c54SBarry Smith         }
2461f6d58c54SBarry Smith       }
2462f6d58c54SBarry Smith       c->nrows[i] = nrows;
2463f6d58c54SBarry Smith       ierr        = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr);
2464f6d58c54SBarry Smith       ierr        = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr);
2465f6d58c54SBarry Smith       ierr        = PetscLogObjectMemory(c,(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr);
2466f6d58c54SBarry Smith       /* now store the linked list of rows into c->rows[i] */
2467f6d58c54SBarry Smith       nrows = 0;
2468f6d58c54SBarry Smith       fm    = rowhit[M];
2469f6d58c54SBarry Smith       do {
2470f6d58c54SBarry Smith         c->rows[i][nrows]            = fm;
2471f6d58c54SBarry Smith         c->columnsforrow[i][nrows++] = columnsforrow[fm];
2472f6d58c54SBarry Smith         fm                           = rowhit[fm];
2473f6d58c54SBarry Smith       } while (fm < M);
2474f6d58c54SBarry Smith     } /* ---------------------------------------------------------------------------------------*/
2475f6d58c54SBarry Smith     ierr = PetscFree(cols);CHKERRQ(ierr);
2476f6d58c54SBarry Smith   }
2477f6d58c54SBarry Smith 
2478f6d58c54SBarry Smith   /* Optimize by adding the vscale, and scaleforrow[][] fields */
2479f6d58c54SBarry Smith   /*
2480f6d58c54SBarry Smith        vscale will contain the "diagonal" on processor scalings followed by the off processor
2481f6d58c54SBarry Smith   */
2482f6d58c54SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
2483f6d58c54SBarry Smith     PetscInt *garray;
2484f6d58c54SBarry Smith     ierr = PetscMalloc(baij->B->cmap->n*sizeof(PetscInt),&garray);CHKERRQ(ierr);
2485f6d58c54SBarry Smith     for (i=0; i<baij->B->cmap->n/bs; i++) {
2486f6d58c54SBarry Smith       for (j=0; j<bs; j++) {
2487f6d58c54SBarry Smith         garray[i*bs+j] = bs*baij->garray[i]+j;
2488f6d58c54SBarry Smith       }
2489f6d58c54SBarry Smith     }
2490*ce94432eSBarry Smith     ierr = VecCreateGhost(PetscObjectComm((PetscObject)mat),baij->A->rmap->n,PETSC_DETERMINE,baij->B->cmap->n,garray,&c->vscale);CHKERRQ(ierr);
2491f6d58c54SBarry Smith     ierr = PetscFree(garray);CHKERRQ(ierr);
2492f6d58c54SBarry Smith     CHKMEMQ;
2493f6d58c54SBarry Smith     ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr);
2494f6d58c54SBarry Smith     for (k=0; k<c->ncolors; k++) {
2495f6d58c54SBarry Smith       ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr);
2496f6d58c54SBarry Smith       for (l=0; l<c->nrows[k]; l++) {
2497f6d58c54SBarry Smith         col = c->columnsforrow[k][l];
2498f6d58c54SBarry Smith         if (col >= cstart && col < cend) {
2499f6d58c54SBarry Smith           /* column is in diagonal block of matrix */
2500f6d58c54SBarry Smith           colb = col - cstart;
2501f6d58c54SBarry Smith         } else {
2502f6d58c54SBarry Smith           /* column  is in "off-processor" part */
2503f6d58c54SBarry Smith #if defined(PETSC_USE_CTABLE)
2504f6d58c54SBarry Smith           ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr);
2505f6d58c54SBarry Smith           colb--;
2506f6d58c54SBarry Smith #else
2507f6d58c54SBarry Smith           colb = baij->colmap[col] - 1;
2508f6d58c54SBarry Smith #endif
2509f6d58c54SBarry Smith           colb  = colb/bs;
2510f6d58c54SBarry Smith           colb += cend - cstart;
2511f6d58c54SBarry Smith         }
2512f6d58c54SBarry Smith         c->vscaleforrow[k][l] = colb;
2513f6d58c54SBarry Smith       }
2514f6d58c54SBarry Smith     }
2515f6d58c54SBarry Smith   } else if (ctype == IS_COLORING_GHOSTED) {
2516f6d58c54SBarry Smith     /* Get gtol mapping */
2517afcb2eb5SJed Brown     PetscInt N = mat->cmap->N,nlocal,*gtol;
2518f6d58c54SBarry Smith     ierr = PetscMalloc((N+1)*sizeof(PetscInt),&gtol);CHKERRQ(ierr);
2519f6d58c54SBarry Smith     for (i=0; i<N; i++) gtol[i] = -1;
2520afcb2eb5SJed Brown     ierr = ISLocalToGlobalMappingGetSize(map,&nlocal);CHKERRQ(ierr);
2521afcb2eb5SJed Brown     for (i=0; i<nlocal; i++) gtol[ltog[i]] = i;
2522f6d58c54SBarry Smith 
2523f6d58c54SBarry Smith     c->vscale = 0; /* will be created in MatFDColoringApply() */
2524f6d58c54SBarry Smith     ierr      = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr);
2525f6d58c54SBarry Smith     for (k=0; k<c->ncolors; k++) {
2526f6d58c54SBarry Smith       ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr);
2527f6d58c54SBarry Smith       for (l=0; l<c->nrows[k]; l++) {
2528f6d58c54SBarry Smith         col = c->columnsforrow[k][l];      /* global column index */
252926fbe8dcSKarl Rupp 
2530f6d58c54SBarry Smith         c->vscaleforrow[k][l] = gtol[col]; /* local column index */
2531f6d58c54SBarry Smith       }
2532f6d58c54SBarry Smith     }
2533f6d58c54SBarry Smith     ierr = PetscFree(gtol);CHKERRQ(ierr);
2534f6d58c54SBarry Smith   }
2535f6d58c54SBarry Smith   ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr);
2536f6d58c54SBarry Smith 
2537f6d58c54SBarry Smith   ierr = PetscFree(rowhit);CHKERRQ(ierr);
2538f6d58c54SBarry Smith   ierr = PetscFree(columnsforrow);CHKERRQ(ierr);
2539f6d58c54SBarry Smith   ierr = MatRestoreColumnIJ(baij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
2540f6d58c54SBarry Smith   ierr = MatRestoreColumnIJ(baij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
2541afcb2eb5SJed Brown   if (map) {ierr = ISLocalToGlobalMappingRestoreIndices(map,&ltog);CHKERRQ(ierr);}
2542f6d58c54SBarry Smith   CHKMEMQ;
2543f6d58c54SBarry Smith   PetscFunctionReturn(0);
2544f6d58c54SBarry Smith }
2545f6d58c54SBarry Smith 
2546f6d58c54SBarry Smith #undef __FUNCT__
2547d1adec66SJed Brown #define __FUNCT__ "MatGetSeqNonzeroStructure_MPIBAIJ"
2548d1adec66SJed Brown PetscErrorCode MatGetSeqNonzeroStructure_MPIBAIJ(Mat A,Mat *newmat)
2549f6d58c54SBarry Smith {
2550f6d58c54SBarry Smith   Mat            B;
2551f6d58c54SBarry Smith   Mat_MPIBAIJ    *a  = (Mat_MPIBAIJ*)A->data;
2552f6d58c54SBarry Smith   Mat_SeqBAIJ    *ad = (Mat_SeqBAIJ*)a->A->data,*bd = (Mat_SeqBAIJ*)a->B->data;
2553f6d58c54SBarry Smith   Mat_SeqAIJ     *b;
2554f6d58c54SBarry Smith   PetscErrorCode ierr;
2555f6d58c54SBarry Smith   PetscMPIInt    size,rank,*recvcounts = 0,*displs = 0;
2556f6d58c54SBarry Smith   PetscInt       sendcount,i,*rstarts = A->rmap->range,n,cnt,j,bs = A->rmap->bs;
2557f6d58c54SBarry Smith   PetscInt       m,*garray = a->garray,*lens,*jsendbuf,*a_jsendbuf,*b_jsendbuf;
2558f6d58c54SBarry Smith 
2559f6d58c54SBarry Smith   PetscFunctionBegin;
2560*ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);CHKERRQ(ierr);
2561*ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);CHKERRQ(ierr);
2562f6d58c54SBarry Smith 
2563f6d58c54SBarry Smith   /* ----------------------------------------------------------------
2564f6d58c54SBarry Smith      Tell every processor the number of nonzeros per row
2565f6d58c54SBarry Smith   */
2566f6d58c54SBarry Smith   ierr = PetscMalloc((A->rmap->N/bs)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
2567f6d58c54SBarry Smith   for (i=A->rmap->rstart/bs; i<A->rmap->rend/bs; i++) {
2568f6d58c54SBarry 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];
2569f6d58c54SBarry Smith   }
2570f6d58c54SBarry Smith   sendcount = A->rmap->rend/bs - A->rmap->rstart/bs;
2571f6d58c54SBarry Smith   ierr      = PetscMalloc(2*size*sizeof(PetscMPIInt),&recvcounts);CHKERRQ(ierr);
2572f6d58c54SBarry Smith   displs    = recvcounts + size;
2573f6d58c54SBarry Smith   for (i=0; i<size; i++) {
2574f6d58c54SBarry Smith     recvcounts[i] = A->rmap->range[i+1]/bs - A->rmap->range[i]/bs;
2575f6d58c54SBarry Smith     displs[i]     = A->rmap->range[i]/bs;
2576f6d58c54SBarry Smith   }
2577f6d58c54SBarry Smith #if defined(PETSC_HAVE_MPI_IN_PLACE)
2578*ce94432eSBarry Smith   ierr = MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,lens,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
2579f6d58c54SBarry Smith #else
2580*ce94432eSBarry Smith   ierr = MPI_Allgatherv(lens+A->rmap->rstart/bs,sendcount,MPIU_INT,lens,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
2581f6d58c54SBarry Smith #endif
2582f6d58c54SBarry Smith   /* ---------------------------------------------------------------
2583f6d58c54SBarry Smith      Create the sequential matrix of the same type as the local block diagonal
2584f6d58c54SBarry Smith   */
2585f6d58c54SBarry Smith   ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr);
2586f6d58c54SBarry Smith   ierr = MatSetSizes(B,A->rmap->N/bs,A->cmap->N/bs,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
2587f6d58c54SBarry Smith   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
2588f6d58c54SBarry Smith   ierr = MatSeqAIJSetPreallocation(B,0,lens);CHKERRQ(ierr);
2589f6d58c54SBarry Smith   b    = (Mat_SeqAIJ*)B->data;
2590f6d58c54SBarry Smith 
2591f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2592f6d58c54SBarry Smith     Copy my part of matrix column indices over
2593f6d58c54SBarry Smith   */
2594f6d58c54SBarry Smith   sendcount  = ad->nz + bd->nz;
2595f6d58c54SBarry Smith   jsendbuf   = b->j + b->i[rstarts[rank]/bs];
2596f6d58c54SBarry Smith   a_jsendbuf = ad->j;
2597f6d58c54SBarry Smith   b_jsendbuf = bd->j;
2598f6d58c54SBarry Smith   n          = A->rmap->rend/bs - A->rmap->rstart/bs;
2599f6d58c54SBarry Smith   cnt        = 0;
2600f6d58c54SBarry Smith   for (i=0; i<n; i++) {
2601f6d58c54SBarry Smith 
2602f6d58c54SBarry Smith     /* put in lower diagonal portion */
2603f6d58c54SBarry Smith     m = bd->i[i+1] - bd->i[i];
2604f6d58c54SBarry Smith     while (m > 0) {
2605f6d58c54SBarry Smith       /* is it above diagonal (in bd (compressed) numbering) */
2606f6d58c54SBarry Smith       if (garray[*b_jsendbuf] > A->rmap->rstart/bs + i) break;
2607f6d58c54SBarry Smith       jsendbuf[cnt++] = garray[*b_jsendbuf++];
2608f6d58c54SBarry Smith       m--;
2609f6d58c54SBarry Smith     }
2610f6d58c54SBarry Smith 
2611f6d58c54SBarry Smith     /* put in diagonal portion */
2612f6d58c54SBarry Smith     for (j=ad->i[i]; j<ad->i[i+1]; j++) {
2613f6d58c54SBarry Smith       jsendbuf[cnt++] = A->rmap->rstart/bs + *a_jsendbuf++;
2614f6d58c54SBarry Smith     }
2615f6d58c54SBarry Smith 
2616f6d58c54SBarry Smith     /* put in upper diagonal portion */
2617f6d58c54SBarry Smith     while (m-- > 0) {
2618f6d58c54SBarry Smith       jsendbuf[cnt++] = garray[*b_jsendbuf++];
2619f6d58c54SBarry Smith     }
2620f6d58c54SBarry Smith   }
2621e32f2f54SBarry Smith   if (cnt != sendcount) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupted PETSc matrix: nz given %D actual nz %D",sendcount,cnt);
2622f6d58c54SBarry Smith 
2623f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2624f6d58c54SBarry Smith     Gather all column indices to all processors
2625f6d58c54SBarry Smith   */
2626f6d58c54SBarry Smith   for (i=0; i<size; i++) {
2627f6d58c54SBarry Smith     recvcounts[i] = 0;
2628f6d58c54SBarry Smith     for (j=A->rmap->range[i]/bs; j<A->rmap->range[i+1]/bs; j++) {
2629f6d58c54SBarry Smith       recvcounts[i] += lens[j];
2630f6d58c54SBarry Smith     }
2631f6d58c54SBarry Smith   }
2632f6d58c54SBarry Smith   displs[0] = 0;
2633f6d58c54SBarry Smith   for (i=1; i<size; i++) {
2634f6d58c54SBarry Smith     displs[i] = displs[i-1] + recvcounts[i-1];
2635f6d58c54SBarry Smith   }
2636f6d58c54SBarry Smith #if defined(PETSC_HAVE_MPI_IN_PLACE)
2637*ce94432eSBarry Smith   ierr = MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,b->j,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
2638f6d58c54SBarry Smith #else
2639*ce94432eSBarry Smith   ierr = MPI_Allgatherv(jsendbuf,sendcount,MPIU_INT,b->j,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));CHKERRQ(ierr);
2640f6d58c54SBarry Smith #endif
2641f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2642f6d58c54SBarry Smith     Assemble the matrix into useable form (note numerical values not yet set)
2643f6d58c54SBarry Smith   */
2644f6d58c54SBarry Smith   /* set the b->ilen (length of each row) values */
2645f6d58c54SBarry Smith   ierr = PetscMemcpy(b->ilen,lens,(A->rmap->N/bs)*sizeof(PetscInt));CHKERRQ(ierr);
2646f6d58c54SBarry Smith   /* set the b->i indices */
2647f6d58c54SBarry Smith   b->i[0] = 0;
2648f6d58c54SBarry Smith   for (i=1; i<=A->rmap->N/bs; i++) {
2649f6d58c54SBarry Smith     b->i[i] = b->i[i-1] + lens[i-1];
2650f6d58c54SBarry Smith   }
2651f6d58c54SBarry Smith   ierr = PetscFree(lens);CHKERRQ(ierr);
2652f6d58c54SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2653f6d58c54SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2654f6d58c54SBarry Smith   ierr = PetscFree(recvcounts);CHKERRQ(ierr);
2655f6d58c54SBarry Smith 
2656f6d58c54SBarry Smith   if (A->symmetric) {
2657f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
2658f6d58c54SBarry Smith   } else if (A->hermitian) {
2659f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
2660f6d58c54SBarry Smith   } else if (A->structurally_symmetric) {
2661f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
2662f6d58c54SBarry Smith   }
2663f6d58c54SBarry Smith   *newmat = B;
2664f6d58c54SBarry Smith   PetscFunctionReturn(0);
2665f6d58c54SBarry Smith }
2666f6d58c54SBarry Smith 
2667b1a666ecSBarry Smith #undef __FUNCT__
2668b1a666ecSBarry Smith #define __FUNCT__ "MatSOR_MPIBAIJ"
2669b1a666ecSBarry Smith PetscErrorCode MatSOR_MPIBAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
2670b1a666ecSBarry Smith {
2671b1a666ecSBarry Smith   Mat_MPIBAIJ    *mat = (Mat_MPIBAIJ*)matin->data;
2672b1a666ecSBarry Smith   PetscErrorCode ierr;
2673b1a666ecSBarry Smith   Vec            bb1 = 0;
2674b1a666ecSBarry Smith 
2675b1a666ecSBarry Smith   PetscFunctionBegin;
2676b1a666ecSBarry Smith   if (flag == SOR_APPLY_UPPER) {
2677b1a666ecSBarry Smith     ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2678b1a666ecSBarry Smith     PetscFunctionReturn(0);
2679b1a666ecSBarry Smith   }
2680b1a666ecSBarry Smith 
26814e980039SJed Brown   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS) {
26824e980039SJed Brown     ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
26834e980039SJed Brown   }
26844e980039SJed Brown 
2685b1a666ecSBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
2686b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2687b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2688b1a666ecSBarry Smith       its--;
2689b1a666ecSBarry Smith     }
2690b1a666ecSBarry Smith 
2691b1a666ecSBarry Smith     while (its--) {
2692b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2693b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2694b1a666ecSBarry Smith 
2695b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2696b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2697b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2698b1a666ecSBarry Smith 
2699b1a666ecSBarry Smith       /* local sweep */
2700b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2701b1a666ecSBarry Smith     }
2702b1a666ecSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
2703b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2704b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2705b1a666ecSBarry Smith       its--;
2706b1a666ecSBarry Smith     }
2707b1a666ecSBarry Smith     while (its--) {
2708b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2709b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2710b1a666ecSBarry Smith 
2711b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2712b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2713b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2714b1a666ecSBarry Smith 
2715b1a666ecSBarry Smith       /* local sweep */
2716b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2717b1a666ecSBarry Smith     }
2718b1a666ecSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
2719b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2720b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2721b1a666ecSBarry Smith       its--;
2722b1a666ecSBarry Smith     }
2723b1a666ecSBarry Smith     while (its--) {
2724b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2725b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2726b1a666ecSBarry Smith 
2727b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2728b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2729b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2730b1a666ecSBarry Smith 
2731b1a666ecSBarry Smith       /* local sweep */
2732b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2733b1a666ecSBarry Smith     }
2734*ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)matin),PETSC_ERR_SUP,"Parallel version of SOR requested not supported");
2735b1a666ecSBarry Smith 
27366bf464f9SBarry Smith   ierr = VecDestroy(&bb1);CHKERRQ(ierr);
2737b1a666ecSBarry Smith   PetscFunctionReturn(0);
2738b1a666ecSBarry Smith }
2739b1a666ecSBarry Smith 
27407087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_BAIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2741f6d58c54SBarry Smith 
2742bbead8a2SBarry Smith #undef __FUNCT__
2743bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_MPIBAIJ"
2744713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_MPIBAIJ(Mat A,const PetscScalar **values)
2745bbead8a2SBarry Smith {
2746bbead8a2SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*) A->data;
2747bbead8a2SBarry Smith   PetscErrorCode ierr;
2748bbead8a2SBarry Smith 
2749bbead8a2SBarry Smith   PetscFunctionBegin;
2750bbead8a2SBarry Smith   ierr = MatInvertBlockDiagonal(a->A,values);CHKERRQ(ierr);
2751bbead8a2SBarry Smith   PetscFunctionReturn(0);
2752bbead8a2SBarry Smith }
2753bbead8a2SBarry Smith 
27548c7482ecSBarry Smith 
275579bdfe76SSatish Balay /* -------------------------------------------------------------------*/
2756cc2dc46cSBarry Smith static struct _MatOps MatOps_Values = {
2757cc2dc46cSBarry Smith        MatSetValues_MPIBAIJ,
2758cc2dc46cSBarry Smith        MatGetRow_MPIBAIJ,
2759cc2dc46cSBarry Smith        MatRestoreRow_MPIBAIJ,
2760cc2dc46cSBarry Smith        MatMult_MPIBAIJ,
276197304618SKris Buschelman /* 4*/ MatMultAdd_MPIBAIJ,
27627c922b88SBarry Smith        MatMultTranspose_MPIBAIJ,
27637c922b88SBarry Smith        MatMultTransposeAdd_MPIBAIJ,
2764cc2dc46cSBarry Smith        0,
2765cc2dc46cSBarry Smith        0,
2766cc2dc46cSBarry Smith        0,
276797304618SKris Buschelman /*10*/ 0,
2768cc2dc46cSBarry Smith        0,
2769cc2dc46cSBarry Smith        0,
2770b1a666ecSBarry Smith        MatSOR_MPIBAIJ,
2771cc2dc46cSBarry Smith        MatTranspose_MPIBAIJ,
277297304618SKris Buschelman /*15*/ MatGetInfo_MPIBAIJ,
27737fc3c18eSBarry Smith        MatEqual_MPIBAIJ,
2774cc2dc46cSBarry Smith        MatGetDiagonal_MPIBAIJ,
2775cc2dc46cSBarry Smith        MatDiagonalScale_MPIBAIJ,
2776cc2dc46cSBarry Smith        MatNorm_MPIBAIJ,
277797304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIBAIJ,
2778cc2dc46cSBarry Smith        MatAssemblyEnd_MPIBAIJ,
2779cc2dc46cSBarry Smith        MatSetOption_MPIBAIJ,
2780cc2dc46cSBarry Smith        MatZeroEntries_MPIBAIJ,
2781d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIBAIJ,
2782cc2dc46cSBarry Smith        0,
2783cc2dc46cSBarry Smith        0,
2784cc2dc46cSBarry Smith        0,
2785cc2dc46cSBarry Smith        0,
27864994cf47SJed Brown /*29*/ MatSetUp_MPIBAIJ,
2787273d9f13SBarry Smith        0,
2788cc2dc46cSBarry Smith        0,
2789cc2dc46cSBarry Smith        0,
2790cc2dc46cSBarry Smith        0,
2791d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIBAIJ,
2792cc2dc46cSBarry Smith        0,
2793cc2dc46cSBarry Smith        0,
2794cc2dc46cSBarry Smith        0,
2795cc2dc46cSBarry Smith        0,
2796d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIBAIJ,
2797cc2dc46cSBarry Smith        MatGetSubMatrices_MPIBAIJ,
2798cc2dc46cSBarry Smith        MatIncreaseOverlap_MPIBAIJ,
2799cc2dc46cSBarry Smith        MatGetValues_MPIBAIJ,
28003c896bc6SHong Zhang        MatCopy_MPIBAIJ,
2801d519adbfSMatthew Knepley /*44*/ 0,
2802cc2dc46cSBarry Smith        MatScale_MPIBAIJ,
2803cc2dc46cSBarry Smith        0,
2804cc2dc46cSBarry Smith        0,
2805cc2dc46cSBarry Smith        0,
2806f73d5cc4SBarry Smith /*49*/ 0,
2807cc2dc46cSBarry Smith        0,
2808cc2dc46cSBarry Smith        0,
2809cc2dc46cSBarry Smith        0,
2810cc2dc46cSBarry Smith        0,
2811f6d58c54SBarry Smith /*54*/ MatFDColoringCreate_MPIBAIJ,
2812cc2dc46cSBarry Smith        0,
2813cc2dc46cSBarry Smith        MatSetUnfactored_MPIBAIJ,
281482094794SBarry Smith        MatPermute_MPIBAIJ,
2815cc2dc46cSBarry Smith        MatSetValuesBlocked_MPIBAIJ,
2816d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIBAIJ,
2817f14a1c24SBarry Smith        MatDestroy_MPIBAIJ,
2818f14a1c24SBarry Smith        MatView_MPIBAIJ,
2819357abbc8SBarry Smith        0,
28207843d17aSBarry Smith        0,
2821d519adbfSMatthew Knepley /*64*/ 0,
28227843d17aSBarry Smith        0,
28237843d17aSBarry Smith        0,
28247843d17aSBarry Smith        0,
28257843d17aSBarry Smith        0,
2826d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIBAIJ,
28277843d17aSBarry Smith        0,
282897304618SKris Buschelman        0,
282997304618SKris Buschelman        0,
283097304618SKris Buschelman        0,
2831d519adbfSMatthew Knepley /*74*/ 0,
2832f6d58c54SBarry Smith        MatFDColoringApply_BAIJ,
283397304618SKris Buschelman        0,
283497304618SKris Buschelman        0,
283597304618SKris Buschelman        0,
2836d519adbfSMatthew Knepley /*79*/ 0,
283797304618SKris Buschelman        0,
283897304618SKris Buschelman        0,
283997304618SKris Buschelman        0,
28405bba2384SShri Abhyankar        MatLoad_MPIBAIJ,
2841d519adbfSMatthew Knepley /*84*/ 0,
2842865e5f61SKris Buschelman        0,
2843865e5f61SKris Buschelman        0,
2844865e5f61SKris Buschelman        0,
2845865e5f61SKris Buschelman        0,
2846d519adbfSMatthew Knepley /*89*/ 0,
2847865e5f61SKris Buschelman        0,
2848865e5f61SKris Buschelman        0,
2849865e5f61SKris Buschelman        0,
2850865e5f61SKris Buschelman        0,
2851d519adbfSMatthew Knepley /*94*/ 0,
2852865e5f61SKris Buschelman        0,
2853865e5f61SKris Buschelman        0,
285499cafbc1SBarry Smith        0,
285599cafbc1SBarry Smith        0,
2856d519adbfSMatthew Knepley /*99*/ 0,
285799cafbc1SBarry Smith        0,
285899cafbc1SBarry Smith        0,
285999cafbc1SBarry Smith        0,
286099cafbc1SBarry Smith        0,
2861d519adbfSMatthew Knepley /*104*/0,
286299cafbc1SBarry Smith        MatRealPart_MPIBAIJ,
28638c7482ecSBarry Smith        MatImaginaryPart_MPIBAIJ,
28648c7482ecSBarry Smith        0,
28658c7482ecSBarry Smith        0,
2866d519adbfSMatthew Knepley /*109*/0,
28678c7482ecSBarry Smith        0,
28688c7482ecSBarry Smith        0,
28698c7482ecSBarry Smith        0,
28708c7482ecSBarry Smith        0,
2871d1adec66SJed Brown /*114*/MatGetSeqNonzeroStructure_MPIBAIJ,
28728c7482ecSBarry Smith        0,
28734683f7a4SShri Abhyankar        MatGetGhosts_MPIBAIJ,
28744683f7a4SShri Abhyankar        0,
28754683f7a4SShri Abhyankar        0,
28764683f7a4SShri Abhyankar /*119*/0,
28774683f7a4SShri Abhyankar        0,
28784683f7a4SShri Abhyankar        0,
2879bbead8a2SBarry Smith        0,
2880bbead8a2SBarry Smith        0,
2881bbead8a2SBarry Smith /*124*/0,
2882bbead8a2SBarry Smith        0,
2883bbead8a2SBarry Smith        MatInvertBlockDiagonal_MPIBAIJ
28848c7482ecSBarry Smith };
288579bdfe76SSatish Balay 
2886e18c124aSSatish Balay EXTERN_C_BEGIN
28874a2ae208SSatish Balay #undef __FUNCT__
28884a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonalBlock_MPIBAIJ"
288911bd1e4dSLisandro Dalcin PetscErrorCode  MatGetDiagonalBlock_MPIBAIJ(Mat A,Mat *a)
28905ef9f2a5SBarry Smith {
28915ef9f2a5SBarry Smith   PetscFunctionBegin;
28925ef9f2a5SBarry Smith   *a = ((Mat_MPIBAIJ*)A->data)->A;
28935ef9f2a5SBarry Smith   PetscFunctionReturn(0);
28945ef9f2a5SBarry Smith }
2895e18c124aSSatish Balay EXTERN_C_END
289679bdfe76SSatish Balay 
2897273d9f13SBarry Smith EXTERN_C_BEGIN
28987087cfbeSBarry Smith extern PetscErrorCode  MatConvert_MPIBAIJ_MPISBAIJ(Mat, MatType,MatReuse,Mat*);
2899d94109b8SHong Zhang EXTERN_C_END
2900d94109b8SHong Zhang 
2901b8d659d7SLisandro Dalcin EXTERN_C_BEGIN
2902aac34f13SBarry Smith #undef __FUNCT__
2903aac34f13SBarry Smith #define __FUNCT__ "MatMPIBAIJSetPreallocationCSR_MPIBAIJ"
2904cf12db73SBarry Smith PetscErrorCode MatMPIBAIJSetPreallocationCSR_MPIBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
2905aac34f13SBarry Smith {
2906b8d659d7SLisandro Dalcin   PetscInt       m,rstart,cstart,cend;
2907b8d659d7SLisandro Dalcin   PetscInt       i,j,d,nz,nz_max=0,*d_nnz=0,*o_nnz=0;
2908b8d659d7SLisandro Dalcin   const PetscInt *JJ    =0;
2909b8d659d7SLisandro Dalcin   PetscScalar    *values=0;
2910aac34f13SBarry Smith   PetscErrorCode ierr;
2911aac34f13SBarry Smith 
2912aac34f13SBarry Smith   PetscFunctionBegin;
291326283091SBarry Smith   ierr   = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
291426283091SBarry Smith   ierr   = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
291526283091SBarry Smith   ierr   = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
291626283091SBarry Smith   ierr   = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2917e02043d6SBarry Smith   ierr   = PetscLayoutGetBlockSize(B->rmap,&bs);CHKERRQ(ierr);
2918d0f46423SBarry Smith   m      = B->rmap->n/bs;
2919d0f46423SBarry Smith   rstart = B->rmap->rstart/bs;
2920d0f46423SBarry Smith   cstart = B->cmap->rstart/bs;
2921d0f46423SBarry Smith   cend   = B->cmap->rend/bs;
2922b8d659d7SLisandro Dalcin 
2923e32f2f54SBarry Smith   if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
2924fca92195SBarry Smith   ierr = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr);
2925aac34f13SBarry Smith   for (i=0; i<m; i++) {
2926cf12db73SBarry Smith     nz = ii[i+1] - ii[i];
2927e32f2f54SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative number of columns %D",i,nz);
2928b8d659d7SLisandro Dalcin     nz_max = PetscMax(nz_max,nz);
2929cf12db73SBarry Smith     JJ     = jj + ii[i];
2930b8d659d7SLisandro Dalcin     for (j=0; j<nz; j++) {
2931aac34f13SBarry Smith       if (*JJ >= cstart) break;
2932aac34f13SBarry Smith       JJ++;
2933aac34f13SBarry Smith     }
2934aac34f13SBarry Smith     d = 0;
2935b8d659d7SLisandro Dalcin     for (; j<nz; j++) {
2936aac34f13SBarry Smith       if (*JJ++ >= cend) break;
2937aac34f13SBarry Smith       d++;
2938aac34f13SBarry Smith     }
2939aac34f13SBarry Smith     d_nnz[i] = d;
2940b8d659d7SLisandro Dalcin     o_nnz[i] = nz - d;
2941aac34f13SBarry Smith   }
2942aac34f13SBarry Smith   ierr = MatMPIBAIJSetPreallocation(B,bs,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
2943fca92195SBarry Smith   ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr);
2944aac34f13SBarry Smith 
2945b8d659d7SLisandro Dalcin   values = (PetscScalar*)V;
2946b8d659d7SLisandro Dalcin   if (!values) {
2947fca92195SBarry Smith     ierr = PetscMalloc(bs*bs*nz_max*sizeof(PetscScalar),&values);CHKERRQ(ierr);
2948b8d659d7SLisandro Dalcin     ierr = PetscMemzero(values,bs*bs*nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
2949b8d659d7SLisandro Dalcin   }
2950b8d659d7SLisandro Dalcin   for (i=0; i<m; i++) {
2951b8d659d7SLisandro Dalcin     PetscInt          row    = i + rstart;
2952cf12db73SBarry Smith     PetscInt          ncols  = ii[i+1] - ii[i];
2953cf12db73SBarry Smith     const PetscInt    *icols = jj + ii[i];
2954cf12db73SBarry Smith     const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
2955b8d659d7SLisandro Dalcin     ierr = MatSetValuesBlocked_MPIBAIJ(B,1,&row,ncols,icols,svals,INSERT_VALUES);CHKERRQ(ierr);
2956aac34f13SBarry Smith   }
2957aac34f13SBarry Smith 
2958b8d659d7SLisandro Dalcin   if (!V) { ierr = PetscFree(values);CHKERRQ(ierr); }
2959aac34f13SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2960aac34f13SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
29617827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
2962aac34f13SBarry Smith   PetscFunctionReturn(0);
2963aac34f13SBarry Smith }
2964b8d659d7SLisandro Dalcin EXTERN_C_END
2965aac34f13SBarry Smith 
2966aac34f13SBarry Smith #undef __FUNCT__
2967aac34f13SBarry Smith #define __FUNCT__ "MatMPIBAIJSetPreallocationCSR"
2968aac34f13SBarry Smith /*@C
2969dfb205c3SBarry Smith    MatMPIBAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in BAIJ format
2970aac34f13SBarry Smith    (the default parallel PETSc format).
2971aac34f13SBarry Smith 
2972aac34f13SBarry Smith    Collective on MPI_Comm
2973aac34f13SBarry Smith 
2974aac34f13SBarry Smith    Input Parameters:
2975aac34f13SBarry Smith +  A - the matrix
2976dfb205c3SBarry Smith .  bs - the block size
2977aac34f13SBarry Smith .  i - the indices into j for the start of each local row (starts with zero)
2978aac34f13SBarry Smith .  j - the column indices for each local row (starts with zero) these must be sorted for each row
2979aac34f13SBarry Smith -  v - optional values in the matrix
2980aac34f13SBarry Smith 
2981aac34f13SBarry Smith    Level: developer
2982aac34f13SBarry Smith 
2983aac34f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2984aac34f13SBarry Smith 
298569b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIBAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ
2986aac34f13SBarry Smith @*/
29877087cfbeSBarry Smith PetscErrorCode  MatMPIBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2988aac34f13SBarry Smith {
29894ac538c5SBarry Smith   PetscErrorCode ierr;
2990aac34f13SBarry Smith 
2991aac34f13SBarry Smith   PetscFunctionBegin;
29926ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
29936ba663aaSJed Brown   PetscValidType(B,1);
29946ba663aaSJed Brown   PetscValidLogicalCollectiveInt(B,bs,2);
29954ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatMPIBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));CHKERRQ(ierr);
2996aac34f13SBarry Smith   PetscFunctionReturn(0);
2997aac34f13SBarry Smith }
2998aac34f13SBarry Smith 
2999d94109b8SHong Zhang EXTERN_C_BEGIN
30004a2ae208SSatish Balay #undef __FUNCT__
3001a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIBAIJSetPreallocation_MPIBAIJ"
30027087cfbeSBarry Smith PetscErrorCode  MatMPIBAIJSetPreallocation_MPIBAIJ(Mat B,PetscInt bs,PetscInt d_nz,PetscInt *d_nnz,PetscInt o_nz,PetscInt *o_nnz)
3003a23d5eceSKris Buschelman {
3004a23d5eceSKris Buschelman   Mat_MPIBAIJ    *b;
3005dfbe8321SBarry Smith   PetscErrorCode ierr;
3006535b19f3SBarry Smith   PetscInt       i;
30072576faa2SJed Brown   PetscBool      d_realalloc = PETSC_FALSE,o_realalloc = PETSC_FALSE;
3008a23d5eceSKris Buschelman 
3009a23d5eceSKris Buschelman   PetscFunctionBegin;
30102576faa2SJed Brown   if (d_nz >= 0 || d_nnz) d_realalloc = PETSC_TRUE;
30112576faa2SJed Brown   if (o_nz >= 0 || o_nnz) o_realalloc = PETSC_TRUE;
3012db4efbfdSBarry Smith 
3013a23d5eceSKris Buschelman   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
3014a23d5eceSKris Buschelman   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
3015e32f2f54SBarry Smith   if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz);
3016e32f2f54SBarry Smith   if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz);
3017899cda47SBarry Smith 
301826283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
301926283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
302026283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
302126283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3022e02043d6SBarry Smith   ierr = PetscLayoutGetBlockSize(B->rmap,&bs);CHKERRQ(ierr);
3023899cda47SBarry Smith 
3024a23d5eceSKris Buschelman   if (d_nnz) {
3025d0f46423SBarry Smith     for (i=0; i<B->rmap->n/bs; i++) {
3026e32f2f54SBarry 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]);
3027a23d5eceSKris Buschelman     }
3028a23d5eceSKris Buschelman   }
3029a23d5eceSKris Buschelman   if (o_nnz) {
3030d0f46423SBarry Smith     for (i=0; i<B->rmap->n/bs; i++) {
3031e32f2f54SBarry 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]);
3032a23d5eceSKris Buschelman     }
3033a23d5eceSKris Buschelman   }
3034a23d5eceSKris Buschelman 
3035a23d5eceSKris Buschelman   b      = (Mat_MPIBAIJ*)B->data;
3036a23d5eceSKris Buschelman   b->bs2 = bs*bs;
3037d0f46423SBarry Smith   b->mbs = B->rmap->n/bs;
3038d0f46423SBarry Smith   b->nbs = B->cmap->n/bs;
3039d0f46423SBarry Smith   b->Mbs = B->rmap->N/bs;
3040d0f46423SBarry Smith   b->Nbs = B->cmap->N/bs;
3041a23d5eceSKris Buschelman 
3042a23d5eceSKris Buschelman   for (i=0; i<=b->size; i++) {
3043d0f46423SBarry Smith     b->rangebs[i] = B->rmap->range[i]/bs;
3044a23d5eceSKris Buschelman   }
3045d0f46423SBarry Smith   b->rstartbs = B->rmap->rstart/bs;
3046d0f46423SBarry Smith   b->rendbs   = B->rmap->rend/bs;
3047d0f46423SBarry Smith   b->cstartbs = B->cmap->rstart/bs;
3048d0f46423SBarry Smith   b->cendbs   = B->cmap->rend/bs;
3049a23d5eceSKris Buschelman 
3050526dfc15SBarry Smith   if (!B->preallocated) {
3051f69a0ea3SMatthew Knepley     ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr);
3052d0f46423SBarry Smith     ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr);
30539c097c71SKris Buschelman     ierr = MatSetType(b->A,MATSEQBAIJ);CHKERRQ(ierr);
305452e6d16bSBarry Smith     ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr);
3055f69a0ea3SMatthew Knepley     ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr);
3056d0f46423SBarry Smith     ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr);
30579c097c71SKris Buschelman     ierr = MatSetType(b->B,MATSEQBAIJ);CHKERRQ(ierr);
305852e6d16bSBarry Smith     ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr);
3059*ce94432eSBarry Smith     ierr = MatStashCreate_Private(PetscObjectComm((PetscObject)B),bs,&B->bstash);CHKERRQ(ierr);
3060526dfc15SBarry Smith   }
3061a23d5eceSKris Buschelman 
3062526dfc15SBarry Smith   ierr = MatSeqBAIJSetPreallocation(b->A,bs,d_nz,d_nnz);CHKERRQ(ierr);
3063526dfc15SBarry Smith   ierr = MatSeqBAIJSetPreallocation(b->B,bs,o_nz,o_nnz);CHKERRQ(ierr);
30642576faa2SJed Brown   /* Do not error if the user did not give real preallocation information. Ugly because this would overwrite a previous user call to MatSetOption(). */
30652576faa2SJed Brown   if (!d_realalloc) {ierr = MatSetOption(b->A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);}
30662576faa2SJed Brown   if (!o_realalloc) {ierr = MatSetOption(b->B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);}
3067526dfc15SBarry Smith   B->preallocated = PETSC_TRUE;
3068a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3069a23d5eceSKris Buschelman }
3070a23d5eceSKris Buschelman EXTERN_C_END
3071a23d5eceSKris Buschelman 
3072a23d5eceSKris Buschelman EXTERN_C_BEGIN
30737087cfbeSBarry Smith extern PetscErrorCode  MatDiagonalScaleLocal_MPIBAIJ(Mat,Vec);
30747087cfbeSBarry Smith extern PetscErrorCode  MatSetHashTableFactor_MPIBAIJ(Mat,PetscReal);
307592b32695SKris Buschelman EXTERN_C_END
30765bf65638SKris Buschelman 
307782094794SBarry Smith 
307882094794SBarry Smith EXTERN_C_BEGIN
307982094794SBarry Smith #undef __FUNCT__
308082094794SBarry Smith #define __FUNCT__ "MatConvert_MPIBAIJ_MPIAdj"
308119fd82e9SBarry Smith PetscErrorCode  MatConvert_MPIBAIJ_MPIAdj(Mat B, MatType newtype,MatReuse reuse,Mat *adj)
308282094794SBarry Smith {
308382094794SBarry Smith   Mat_MPIBAIJ    *b = (Mat_MPIBAIJ*)B->data;
308482094794SBarry Smith   PetscErrorCode ierr;
308582094794SBarry Smith   Mat_SeqBAIJ    *d  = (Mat_SeqBAIJ*) b->A->data,*o = (Mat_SeqBAIJ*) b->B->data;
308682094794SBarry Smith   PetscInt       M   = B->rmap->n/B->rmap->bs,i,*ii,*jj,cnt,j,k,rstart = B->rmap->rstart/B->rmap->bs;
308782094794SBarry Smith   const PetscInt *id = d->i, *jd = d->j, *io = o->i, *jo = o->j, *garray = b->garray;
308882094794SBarry Smith 
308982094794SBarry Smith   PetscFunctionBegin;
309082094794SBarry Smith   ierr  = PetscMalloc((M+1)*sizeof(PetscInt),&ii);CHKERRQ(ierr);
309182094794SBarry Smith   ii[0] = 0;
309282094794SBarry Smith   CHKMEMQ;
309382094794SBarry Smith   for (i=0; i<M; i++) {
3094e32f2f54SBarry 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]);
3095e32f2f54SBarry 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]);
309682094794SBarry Smith     ii[i+1] = ii[i] + id[i+1] - id[i] + io[i+1] - io[i];
30975ee9ba1cSJed Brown     /* remove one from count of matrix has diagonal */
30985ee9ba1cSJed Brown     for (j=id[i]; j<id[i+1]; j++) {
30995ee9ba1cSJed Brown       if (jd[j] == i) {ii[i+1]--;break;}
31005ee9ba1cSJed Brown     }
310182094794SBarry Smith     CHKMEMQ;
310282094794SBarry Smith   }
310382094794SBarry Smith   ierr = PetscMalloc(ii[M]*sizeof(PetscInt),&jj);CHKERRQ(ierr);
310482094794SBarry Smith   cnt  = 0;
310582094794SBarry Smith   for (i=0; i<M; i++) {
310682094794SBarry Smith     for (j=io[i]; j<io[i+1]; j++) {
310782094794SBarry Smith       if (garray[jo[j]] > rstart) break;
310882094794SBarry Smith       jj[cnt++] = garray[jo[j]];
310982094794SBarry Smith       CHKMEMQ;
311082094794SBarry Smith     }
311182094794SBarry Smith     for (k=id[i]; k<id[i+1]; k++) {
31125ee9ba1cSJed Brown       if (jd[k] != i) {
311382094794SBarry Smith         jj[cnt++] = rstart + jd[k];
311482094794SBarry Smith         CHKMEMQ;
311582094794SBarry Smith       }
31165ee9ba1cSJed Brown     }
311782094794SBarry Smith     for (; j<io[i+1]; j++) {
311882094794SBarry Smith       jj[cnt++] = garray[jo[j]];
311982094794SBarry Smith       CHKMEMQ;
312082094794SBarry Smith     }
312182094794SBarry Smith   }
3122*ce94432eSBarry Smith   ierr = MatCreateMPIAdj(PetscObjectComm((PetscObject)B),M,B->cmap->N/B->rmap->bs,ii,jj,NULL,adj);CHKERRQ(ierr);
312382094794SBarry Smith   PetscFunctionReturn(0);
312482094794SBarry Smith }
3125dbf0e21dSBarry Smith EXTERN_C_END
312682094794SBarry Smith 
3127c6db04a5SJed Brown #include <../src/mat/impls/aij/mpi/mpiaij.h>
31281e66c38aSShri Abhyankar EXTERN_C_BEGIN
312919fd82e9SBarry Smith PetscErrorCode  MatConvert_SeqBAIJ_SeqAIJ(Mat,MatType,MatReuse,Mat*);
31301e66c38aSShri Abhyankar EXTERN_C_END
313162471d69SBarry Smith 
313262471d69SBarry Smith EXTERN_C_BEGIN
313362471d69SBarry Smith #undef __FUNCT__
313462471d69SBarry Smith #define __FUNCT__ "MatConvert_MPIBAIJ_MPIAIJ"
313519fd82e9SBarry Smith PetscErrorCode  MatConvert_MPIBAIJ_MPIAIJ(Mat A,MatType newtype,MatReuse reuse,Mat *newmat)
313662471d69SBarry Smith {
313762471d69SBarry Smith   PetscErrorCode ierr;
313862471d69SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
313962471d69SBarry Smith   Mat            B;
314085a69837SSatish Balay   Mat_MPIAIJ     *b;
314162471d69SBarry Smith 
314262471d69SBarry Smith   PetscFunctionBegin;
3143*ce94432eSBarry Smith   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix must be assembled");
314462471d69SBarry Smith 
3145*ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
314662471d69SBarry Smith   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
31476d0a4a0eSHong Zhang   ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr);
31480298fd71SBarry Smith   ierr = MatSeqAIJSetPreallocation(B,0,NULL);CHKERRQ(ierr);
31490298fd71SBarry Smith   ierr = MatMPIAIJSetPreallocation(B,0,NULL,0,NULL);CHKERRQ(ierr);
315062471d69SBarry Smith   b    = (Mat_MPIAIJ*) B->data;
315162471d69SBarry Smith 
31526bf464f9SBarry Smith   ierr = MatDestroy(&b->A);CHKERRQ(ierr);
31536bf464f9SBarry Smith   ierr = MatDestroy(&b->B);CHKERRQ(ierr);
3154ab9863d7SBarry Smith   ierr = MatDisAssemble_MPIBAIJ(A);CHKERRQ(ierr);
315562471d69SBarry Smith   ierr = MatConvert_SeqBAIJ_SeqAIJ(a->A, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->A);CHKERRQ(ierr);
315662471d69SBarry Smith   ierr = MatConvert_SeqBAIJ_SeqAIJ(a->B, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->B);CHKERRQ(ierr);
315762471d69SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
315862471d69SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
31596a719282SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
31606a719282SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
316162471d69SBarry Smith   if (reuse == MAT_REUSE_MATRIX) {
316262471d69SBarry Smith     ierr = MatHeaderReplace(A,B);CHKERRQ(ierr);
316362471d69SBarry Smith   } else {
316462471d69SBarry Smith    *newmat = B;
316562471d69SBarry Smith   }
316662471d69SBarry Smith   PetscFunctionReturn(0);
316762471d69SBarry Smith }
316862471d69SBarry Smith EXTERN_C_END
316962471d69SBarry Smith 
3170450b117fSShri Abhyankar EXTERN_C_BEGIN
3171450b117fSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
3172bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_baij_mumps(Mat,MatFactorType,Mat*);
3173450b117fSShri Abhyankar #endif
3174450b117fSShri Abhyankar EXTERN_C_END
3175450b117fSShri Abhyankar 
31760bad9183SKris Buschelman /*MC
3177fafad747SKris Buschelman    MATMPIBAIJ - MATMPIBAIJ = "mpibaij" - A matrix type to be used for distributed block sparse matrices.
31780bad9183SKris Buschelman 
31790bad9183SKris Buschelman    Options Database Keys:
31808c07d4e3SBarry Smith + -mat_type mpibaij - sets the matrix type to "mpibaij" during a call to MatSetFromOptions()
31818c07d4e3SBarry Smith . -mat_block_size <bs> - set the blocksize used to store the matrix
31828c07d4e3SBarry Smith - -mat_use_hash_table <fact>
31830bad9183SKris Buschelman 
31840bad9183SKris Buschelman   Level: beginner
31850bad9183SKris Buschelman 
31860bad9183SKris Buschelman .seealso: MatCreateMPIBAIJ
31870bad9183SKris Buschelman M*/
31880bad9183SKris Buschelman 
318992b32695SKris Buschelman EXTERN_C_BEGIN
319019fd82e9SBarry Smith extern PetscErrorCode MatConvert_MPIBAIJ_MPIBSTRM(Mat,MatType,MatReuse,Mat*);
3191c0cdd4a1SDahai Guo EXTERN_C_END
3192c0cdd4a1SDahai Guo 
3193c0cdd4a1SDahai Guo EXTERN_C_BEGIN
3194a23d5eceSKris Buschelman #undef __FUNCT__
31954a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIBAIJ"
31967087cfbeSBarry Smith PetscErrorCode  MatCreate_MPIBAIJ(Mat B)
3197273d9f13SBarry Smith {
3198273d9f13SBarry Smith   Mat_MPIBAIJ    *b;
3199dfbe8321SBarry Smith   PetscErrorCode ierr;
3200ace3abfcSBarry Smith   PetscBool      flg;
3201273d9f13SBarry Smith 
3202273d9f13SBarry Smith   PetscFunctionBegin;
320338f2d2fdSLisandro Dalcin   ierr    = PetscNewLog(B,Mat_MPIBAIJ,&b);CHKERRQ(ierr);
320482502324SSatish Balay   B->data = (void*)b;
320582502324SSatish Balay 
3206273d9f13SBarry Smith   ierr         = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
3207273d9f13SBarry Smith   B->assembled = PETSC_FALSE;
3208273d9f13SBarry Smith 
3209273d9f13SBarry Smith   B->insertmode = NOT_SET_VALUES;
3210*ce94432eSBarry Smith   ierr          = MPI_Comm_rank(PetscObjectComm((PetscObject)B),&b->rank);CHKERRQ(ierr);
3211*ce94432eSBarry Smith   ierr          = MPI_Comm_size(PetscObjectComm((PetscObject)B),&b->size);CHKERRQ(ierr);
3212273d9f13SBarry Smith 
3213273d9f13SBarry Smith   /* build local table of row and column ownerships */
3214899cda47SBarry Smith   ierr = PetscMalloc((b->size+1)*sizeof(PetscInt),&b->rangebs);CHKERRQ(ierr);
3215273d9f13SBarry Smith 
3216273d9f13SBarry Smith   /* build cache for off array entries formed */
3217*ce94432eSBarry Smith   ierr = MatStashCreate_Private(PetscObjectComm((PetscObject)B),1,&B->stash);CHKERRQ(ierr);
321826fbe8dcSKarl Rupp 
3219273d9f13SBarry Smith   b->donotstash  = PETSC_FALSE;
32200298fd71SBarry Smith   b->colmap      = NULL;
32210298fd71SBarry Smith   b->garray      = NULL;
3222273d9f13SBarry Smith   b->roworiented = PETSC_TRUE;
3223273d9f13SBarry Smith 
3224273d9f13SBarry Smith   /* stuff used in block assembly */
3225273d9f13SBarry Smith   b->barray = 0;
3226273d9f13SBarry Smith 
3227273d9f13SBarry Smith   /* stuff used for matrix vector multiply */
3228273d9f13SBarry Smith   b->lvec  = 0;
3229273d9f13SBarry Smith   b->Mvctx = 0;
3230273d9f13SBarry Smith 
3231273d9f13SBarry Smith   /* stuff for MatGetRow() */
3232273d9f13SBarry Smith   b->rowindices   = 0;
3233273d9f13SBarry Smith   b->rowvalues    = 0;
3234273d9f13SBarry Smith   b->getrowactive = PETSC_FALSE;
3235273d9f13SBarry Smith 
3236273d9f13SBarry Smith   /* hash table stuff */
3237273d9f13SBarry Smith   b->ht           = 0;
3238273d9f13SBarry Smith   b->hd           = 0;
3239273d9f13SBarry Smith   b->ht_size      = 0;
3240273d9f13SBarry Smith   b->ht_flag      = PETSC_FALSE;
3241273d9f13SBarry Smith   b->ht_fact      = 0;
3242273d9f13SBarry Smith   b->ht_total_ct  = 0;
3243273d9f13SBarry Smith   b->ht_insert_ct = 0;
3244273d9f13SBarry Smith 
32457a868f3eSHong Zhang   /* stuff for MatGetSubMatrices_MPIBAIJ_local() */
32467a868f3eSHong Zhang   b->ijonly = PETSC_FALSE;
32477a868f3eSHong Zhang 
3248*ce94432eSBarry Smith   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)B),NULL,"Options for loading MPIBAIJ matrix 1","Mat");CHKERRQ(ierr);
32490298fd71SBarry Smith   ierr = PetscOptionsBool("-mat_use_hash_table","Use hash table to save memory in constructing matrix","MatSetOption",PETSC_FALSE,&flg,NULL);CHKERRQ(ierr);
3250273d9f13SBarry Smith   if (flg) {
3251f6275e2eSBarry Smith     PetscReal fact = 1.39;
32524e0d8c25SBarry Smith     ierr = MatSetOption(B,MAT_USE_HASH_TABLE,PETSC_TRUE);CHKERRQ(ierr);
32530298fd71SBarry Smith     ierr = PetscOptionsReal("-mat_use_hash_table","Use hash table factor","MatMPIBAIJSetHashTableFactor",fact,&fact,NULL);CHKERRQ(ierr);
3254273d9f13SBarry Smith     if (fact <= 1.0) fact = 1.39;
3255273d9f13SBarry Smith     ierr = MatMPIBAIJSetHashTableFactor(B,fact);CHKERRQ(ierr);
32561e2582c4SBarry Smith     ierr = PetscInfo1(B,"Hash table Factor used %5.2f\n",fact);CHKERRQ(ierr);
3257273d9f13SBarry Smith   }
32588c07d4e3SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
32598c07d4e3SBarry Smith 
3260450b117fSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
3261bccb9932SShri Abhyankar   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", "MatGetFactor_baij_mumps",MatGetFactor_baij_mumps);CHKERRQ(ierr);
3262450b117fSShri Abhyankar #endif
326382094794SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpibaij_mpiadj_C",
326482094794SBarry Smith                                            "MatConvert_MPIBAIJ_MPIAdj",
326582094794SBarry Smith                                            MatConvert_MPIBAIJ_MPIAdj);CHKERRQ(ierr);
326662471d69SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpibaij_mpiaij_C",
326762471d69SBarry Smith                                            "MatConvert_MPIBAIJ_MPIAIJ",
326862471d69SBarry Smith                                            MatConvert_MPIBAIJ_MPIAIJ);CHKERRQ(ierr);
3269ad5247fdSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpibaij_mpisbaij_C",
3270ad5247fdSHong Zhang                                            "MatConvert_MPIBAIJ_MPISBAIJ",
3271ad5247fdSHong Zhang                                            MatConvert_MPIBAIJ_MPISBAIJ);CHKERRQ(ierr);
3272273d9f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3273273d9f13SBarry Smith                                            "MatStoreValues_MPIBAIJ",
3274273d9f13SBarry Smith                                            MatStoreValues_MPIBAIJ);CHKERRQ(ierr);
3275273d9f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3276273d9f13SBarry Smith                                            "MatRetrieveValues_MPIBAIJ",
3277273d9f13SBarry Smith                                            MatRetrieveValues_MPIBAIJ);CHKERRQ(ierr);
3278273d9f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
3279273d9f13SBarry Smith                                            "MatGetDiagonalBlock_MPIBAIJ",
3280273d9f13SBarry Smith                                            MatGetDiagonalBlock_MPIBAIJ);CHKERRQ(ierr);
3281a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIBAIJSetPreallocation_C",
3282a23d5eceSKris Buschelman                                            "MatMPIBAIJSetPreallocation_MPIBAIJ",
3283a23d5eceSKris Buschelman                                            MatMPIBAIJSetPreallocation_MPIBAIJ);CHKERRQ(ierr);
3284aac34f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIBAIJSetPreallocationCSR_C",
328544ec7894SLisandro Dalcin                                            "MatMPIBAIJSetPreallocationCSR_MPIBAIJ",
3286aac34f13SBarry Smith                                            MatMPIBAIJSetPreallocationCSR_MPIBAIJ);CHKERRQ(ierr);
328792b32695SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
328892b32695SKris Buschelman                                            "MatDiagonalScaleLocal_MPIBAIJ",
328992b32695SKris Buschelman                                            MatDiagonalScaleLocal_MPIBAIJ);CHKERRQ(ierr);
32905bf65638SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSetHashTableFactor_C",
32915bf65638SKris Buschelman                                            "MatSetHashTableFactor_MPIBAIJ",
32925bf65638SKris Buschelman                                            MatSetHashTableFactor_MPIBAIJ);CHKERRQ(ierr);
3293c0cdd4a1SDahai Guo   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpibaij_mpibstrm_C",
3294c0cdd4a1SDahai Guo                                            "MatConvert_MPIBAIJ_MPIBSTRM",
3295c0cdd4a1SDahai Guo                                            MatConvert_MPIBAIJ_MPIBSTRM);CHKERRQ(ierr);
329617667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIBAIJ);CHKERRQ(ierr);
3297273d9f13SBarry Smith   PetscFunctionReturn(0);
3298273d9f13SBarry Smith }
3299273d9f13SBarry Smith EXTERN_C_END
3300273d9f13SBarry Smith 
3301209238afSKris Buschelman /*MC
3302002d173eSKris Buschelman    MATBAIJ - MATBAIJ = "baij" - A matrix type to be used for block sparse matrices.
3303209238afSKris Buschelman 
3304209238afSKris Buschelman    This matrix type is identical to MATSEQBAIJ when constructed with a single process communicator,
3305209238afSKris Buschelman    and MATMPIBAIJ otherwise.
3306209238afSKris Buschelman 
3307209238afSKris Buschelman    Options Database Keys:
3308209238afSKris Buschelman . -mat_type baij - sets the matrix type to "baij" during a call to MatSetFromOptions()
3309209238afSKris Buschelman 
3310209238afSKris Buschelman   Level: beginner
3311209238afSKris Buschelman 
331269b1f4b7SBarry Smith .seealso: MatCreateBAIJ(),MATSEQBAIJ,MATMPIBAIJ, MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
3313209238afSKris Buschelman M*/
3314209238afSKris Buschelman 
33154a2ae208SSatish Balay #undef __FUNCT__
33164a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJSetPreallocation"
3317273d9f13SBarry Smith /*@C
3318aac34f13SBarry Smith    MatMPIBAIJSetPreallocation - Allocates memory for a sparse parallel matrix in block AIJ format
3319273d9f13SBarry Smith    (block compressed row).  For good matrix assembly performance
3320273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
3321273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
3322273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
3323273d9f13SBarry Smith 
3324273d9f13SBarry Smith    Collective on Mat
3325273d9f13SBarry Smith 
3326273d9f13SBarry Smith    Input Parameters:
3327273d9f13SBarry Smith +  A - the matrix
3328273d9f13SBarry Smith .  bs   - size of blockk
3329273d9f13SBarry Smith .  d_nz  - number of block nonzeros per block row in diagonal portion of local
3330273d9f13SBarry Smith            submatrix  (same for all local rows)
3331273d9f13SBarry Smith .  d_nnz - array containing the number of block nonzeros in the various block rows
3332273d9f13SBarry Smith            of the in diagonal portion of the local (possibly different for each block
33330298fd71SBarry Smith            row) or NULL.  If you plan to factor the matrix you must leave room for the diagonal entry and
333495742e49SBarry Smith            set it even if it is zero.
3335273d9f13SBarry Smith .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
3336273d9f13SBarry Smith            submatrix (same for all local rows).
3337273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various block rows of the
3338273d9f13SBarry Smith            off-diagonal portion of the local submatrix (possibly different for
33390298fd71SBarry Smith            each block row) or NULL.
3340273d9f13SBarry Smith 
334149a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
3342273d9f13SBarry Smith 
3343273d9f13SBarry Smith    Options Database Keys:
33448c07d4e3SBarry Smith +   -mat_block_size - size of the blocks to use
33458c07d4e3SBarry Smith -   -mat_use_hash_table <fact>
3346273d9f13SBarry Smith 
3347273d9f13SBarry Smith    Notes:
3348273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one processor
3349273d9f13SBarry Smith    than it must be used on all processors that share the object for that argument.
3350273d9f13SBarry Smith 
3351273d9f13SBarry Smith    Storage Information:
3352273d9f13SBarry Smith    For a square global matrix we define each processor's diagonal portion
3353273d9f13SBarry Smith    to be its local rows and the corresponding columns (a square submatrix);
3354273d9f13SBarry Smith    each processor's off-diagonal portion encompasses the remainder of the
3355273d9f13SBarry Smith    local matrix (a rectangular submatrix).
3356273d9f13SBarry Smith 
3357273d9f13SBarry Smith    The user can specify preallocated storage for the diagonal part of
3358273d9f13SBarry Smith    the local submatrix with either d_nz or d_nnz (not both).  Set
33590298fd71SBarry Smith    d_nz=PETSC_DEFAULT and d_nnz=NULL for PETSc to control dynamic
3360273d9f13SBarry Smith    memory allocation.  Likewise, specify preallocated storage for the
3361273d9f13SBarry Smith    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
3362273d9f13SBarry Smith 
3363273d9f13SBarry Smith    Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
3364273d9f13SBarry Smith    the figure below we depict these three local rows and all columns (0-11).
3365273d9f13SBarry Smith 
3366273d9f13SBarry Smith .vb
3367273d9f13SBarry Smith            0 1 2 3 4 5 6 7 8 9 10 11
3368273d9f13SBarry Smith           -------------------
3369273d9f13SBarry Smith    row 3  |  o o o d d d o o o o o o
3370273d9f13SBarry Smith    row 4  |  o o o d d d o o o o o o
3371273d9f13SBarry Smith    row 5  |  o o o d d d o o o o o o
3372273d9f13SBarry Smith           -------------------
3373273d9f13SBarry Smith .ve
3374273d9f13SBarry Smith 
3375273d9f13SBarry Smith    Thus, any entries in the d locations are stored in the d (diagonal)
3376273d9f13SBarry Smith    submatrix, and any entries in the o locations are stored in the
3377273d9f13SBarry Smith    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
3378273d9f13SBarry Smith    stored simply in the MATSEQBAIJ format for compressed row storage.
3379273d9f13SBarry Smith 
3380273d9f13SBarry Smith    Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3381273d9f13SBarry Smith    and o_nz should indicate the number of block nonzeros per row in the o matrix.
3382273d9f13SBarry Smith    In general, for PDE problems in which most nonzeros are near the diagonal,
3383273d9f13SBarry Smith    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
3384273d9f13SBarry Smith    or you will get TERRIBLE performance; see the users' manual chapter on
3385273d9f13SBarry Smith    matrices.
3386273d9f13SBarry Smith 
3387aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3388aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3389aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3390aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3391aa95bbe8SBarry Smith 
3392273d9f13SBarry Smith    Level: intermediate
3393273d9f13SBarry Smith 
3394273d9f13SBarry Smith .keywords: matrix, block, aij, compressed row, sparse, parallel
3395273d9f13SBarry Smith 
3396ab978733SBarry Smith .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateBAIJ(), MatMPIBAIJSetPreallocationCSR(), PetscSplitOwnership()
3397273d9f13SBarry Smith @*/
33987087cfbeSBarry Smith PetscErrorCode  MatMPIBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3399273d9f13SBarry Smith {
34004ac538c5SBarry Smith   PetscErrorCode ierr;
3401273d9f13SBarry Smith 
3402273d9f13SBarry Smith   PetscFunctionBegin;
34036ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
34046ba663aaSJed Brown   PetscValidType(B,1);
34056ba663aaSJed Brown   PetscValidLogicalCollectiveInt(B,bs,2);
34064ac538c5SBarry 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);
3407273d9f13SBarry Smith   PetscFunctionReturn(0);
3408273d9f13SBarry Smith }
3409273d9f13SBarry Smith 
34104a2ae208SSatish Balay #undef __FUNCT__
341169b1f4b7SBarry Smith #define __FUNCT__ "MatCreateBAIJ"
341279bdfe76SSatish Balay /*@C
341369b1f4b7SBarry Smith    MatCreateBAIJ - Creates a sparse parallel matrix in block AIJ format
341479bdfe76SSatish Balay    (block compressed row).  For good matrix assembly performance
341579bdfe76SSatish Balay    the user should preallocate the matrix storage by setting the parameters
341679bdfe76SSatish Balay    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
341779bdfe76SSatish Balay    performance can be increased by more than a factor of 50.
341879bdfe76SSatish Balay 
3419db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3420db81eaa0SLois Curfman McInnes 
342179bdfe76SSatish Balay    Input Parameters:
3422db81eaa0SLois Curfman McInnes +  comm - MPI communicator
342379bdfe76SSatish Balay .  bs   - size of blockk
342479bdfe76SSatish Balay .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
342592e8d321SLois Curfman McInnes            This value should be the same as the local size used in creating the
342692e8d321SLois Curfman McInnes            y vector for the matrix-vector product y = Ax.
342792e8d321SLois Curfman McInnes .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
342892e8d321SLois Curfman McInnes            This value should be the same as the local size used in creating the
342992e8d321SLois Curfman McInnes            x vector for the matrix-vector product y = Ax.
3430be79a94dSBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3431be79a94dSBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
343247a75d0bSBarry Smith .  d_nz  - number of nonzero blocks per block row in diagonal portion of local
343379bdfe76SSatish Balay            submatrix  (same for all local rows)
343447a75d0bSBarry Smith .  d_nnz - array containing the number of nonzero blocks in the various block rows
343592e8d321SLois Curfman McInnes            of the in diagonal portion of the local (possibly different for each block
34360298fd71SBarry Smith            row) or NULL.  If you plan to factor the matrix you must leave room for the diagonal entry
343795742e49SBarry Smith            and set it even if it is zero.
343847a75d0bSBarry Smith .  o_nz  - number of nonzero blocks per block row in the off-diagonal portion of local
343979bdfe76SSatish Balay            submatrix (same for all local rows).
344047a75d0bSBarry Smith -  o_nnz - array containing the number of nonzero blocks in the various block rows of the
344192e8d321SLois Curfman McInnes            off-diagonal portion of the local submatrix (possibly different for
34420298fd71SBarry Smith            each block row) or NULL.
344379bdfe76SSatish Balay 
344479bdfe76SSatish Balay    Output Parameter:
344579bdfe76SSatish Balay .  A - the matrix
344679bdfe76SSatish Balay 
3447db81eaa0SLois Curfman McInnes    Options Database Keys:
34488c07d4e3SBarry Smith +   -mat_block_size - size of the blocks to use
34498c07d4e3SBarry Smith -   -mat_use_hash_table <fact>
34503ffaccefSLois Curfman McInnes 
3451175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3452ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3453175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3454175b88e8SBarry Smith 
3455b259b22eSLois Curfman McInnes    Notes:
345649a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
345749a6f317SBarry Smith 
345847a75d0bSBarry Smith    A nonzero block is any block that as 1 or more nonzeros in it
345947a75d0bSBarry Smith 
346079bdfe76SSatish Balay    The user MUST specify either the local or global matrix dimensions
346179bdfe76SSatish Balay    (possibly both).
346279bdfe76SSatish Balay 
3463be79a94dSBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one processor
3464be79a94dSBarry Smith    than it must be used on all processors that share the object for that argument.
3465be79a94dSBarry Smith 
346679bdfe76SSatish Balay    Storage Information:
346779bdfe76SSatish Balay    For a square global matrix we define each processor's diagonal portion
346879bdfe76SSatish Balay    to be its local rows and the corresponding columns (a square submatrix);
346979bdfe76SSatish Balay    each processor's off-diagonal portion encompasses the remainder of the
347079bdfe76SSatish Balay    local matrix (a rectangular submatrix).
347179bdfe76SSatish Balay 
347279bdfe76SSatish Balay    The user can specify preallocated storage for the diagonal part of
347379bdfe76SSatish Balay    the local submatrix with either d_nz or d_nnz (not both).  Set
34740298fd71SBarry Smith    d_nz=PETSC_DEFAULT and d_nnz=NULL for PETSc to control dynamic
347579bdfe76SSatish Balay    memory allocation.  Likewise, specify preallocated storage for the
347679bdfe76SSatish Balay    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
347779bdfe76SSatish Balay 
347879bdfe76SSatish Balay    Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
347979bdfe76SSatish Balay    the figure below we depict these three local rows and all columns (0-11).
348079bdfe76SSatish Balay 
3481db81eaa0SLois Curfman McInnes .vb
3482db81eaa0SLois Curfman McInnes            0 1 2 3 4 5 6 7 8 9 10 11
3483db81eaa0SLois Curfman McInnes           -------------------
3484db81eaa0SLois Curfman McInnes    row 3  |  o o o d d d o o o o o o
3485db81eaa0SLois Curfman McInnes    row 4  |  o o o d d d o o o o o o
3486db81eaa0SLois Curfman McInnes    row 5  |  o o o d d d o o o o o o
3487db81eaa0SLois Curfman McInnes           -------------------
3488db81eaa0SLois Curfman McInnes .ve
348979bdfe76SSatish Balay 
349079bdfe76SSatish Balay    Thus, any entries in the d locations are stored in the d (diagonal)
349179bdfe76SSatish Balay    submatrix, and any entries in the o locations are stored in the
349279bdfe76SSatish Balay    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
349357b952d6SSatish Balay    stored simply in the MATSEQBAIJ format for compressed row storage.
349479bdfe76SSatish Balay 
3495d64ed03dSBarry Smith    Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3496d64ed03dSBarry Smith    and o_nz should indicate the number of block nonzeros per row in the o matrix.
349779bdfe76SSatish Balay    In general, for PDE problems in which most nonzeros are near the diagonal,
349892e8d321SLois Curfman McInnes    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
349992e8d321SLois Curfman McInnes    or you will get TERRIBLE performance; see the users' manual chapter on
35006da5968aSLois Curfman McInnes    matrices.
350179bdfe76SSatish Balay 
3502027ccd11SLois Curfman McInnes    Level: intermediate
3503027ccd11SLois Curfman McInnes 
350492e8d321SLois Curfman McInnes .keywords: matrix, block, aij, compressed row, sparse, parallel
350579bdfe76SSatish Balay 
350669b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateBAIJ(), MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
350779bdfe76SSatish Balay @*/
350869b1f4b7SBarry 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)
350979bdfe76SSatish Balay {
35106849ba73SBarry Smith   PetscErrorCode ierr;
3511b24ad042SBarry Smith   PetscMPIInt    size;
351279bdfe76SSatish Balay 
3513d64ed03dSBarry Smith   PetscFunctionBegin;
3514f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3515f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr);
3516d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3517273d9f13SBarry Smith   if (size > 1) {
3518273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIBAIJ);CHKERRQ(ierr);
3519273d9f13SBarry Smith     ierr = MatMPIBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
3520273d9f13SBarry Smith   } else {
3521273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQBAIJ);CHKERRQ(ierr);
3522273d9f13SBarry Smith     ierr = MatSeqBAIJSetPreallocation(*A,bs,d_nz,d_nnz);CHKERRQ(ierr);
35233914022bSBarry Smith   }
35243a40ed3dSBarry Smith   PetscFunctionReturn(0);
352579bdfe76SSatish Balay }
3526026e39d0SSatish Balay 
35274a2ae208SSatish Balay #undef __FUNCT__
35284a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIBAIJ"
35296849ba73SBarry Smith static PetscErrorCode MatDuplicate_MPIBAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
35300ac07820SSatish Balay {
35310ac07820SSatish Balay   Mat            mat;
35320ac07820SSatish Balay   Mat_MPIBAIJ    *a,*oldmat = (Mat_MPIBAIJ*)matin->data;
3533dfbe8321SBarry Smith   PetscErrorCode ierr;
3534b24ad042SBarry Smith   PetscInt       len=0;
35350ac07820SSatish Balay 
3536d64ed03dSBarry Smith   PetscFunctionBegin;
35370ac07820SSatish Balay   *newmat = 0;
3538*ce94432eSBarry Smith   ierr    = MatCreate(PetscObjectComm((PetscObject)matin),&mat);CHKERRQ(ierr);
3539d0f46423SBarry Smith   ierr    = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr);
35407adad957SLisandro Dalcin   ierr    = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr);
35411d5dac46SHong Zhang   ierr    = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
35427fff6886SHong Zhang 
3543d5f3da31SBarry Smith   mat->factortype   = matin->factortype;
3544273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
35450ac07820SSatish Balay   mat->assembled    = PETSC_TRUE;
35467fff6886SHong Zhang   mat->insertmode   = NOT_SET_VALUES;
35477fff6886SHong Zhang 
3548273d9f13SBarry Smith   a             = (Mat_MPIBAIJ*)mat->data;
3549d0f46423SBarry Smith   mat->rmap->bs = matin->rmap->bs;
35500ac07820SSatish Balay   a->bs2        = oldmat->bs2;
35510ac07820SSatish Balay   a->mbs        = oldmat->mbs;
35520ac07820SSatish Balay   a->nbs        = oldmat->nbs;
35530ac07820SSatish Balay   a->Mbs        = oldmat->Mbs;
35540ac07820SSatish Balay   a->Nbs        = oldmat->Nbs;
35550ac07820SSatish Balay 
35561e1e43feSBarry Smith   ierr = PetscLayoutReference(matin->rmap,&mat->rmap);CHKERRQ(ierr);
35571e1e43feSBarry Smith   ierr = PetscLayoutReference(matin->cmap,&mat->cmap);CHKERRQ(ierr);
3558899cda47SBarry Smith 
35590ac07820SSatish Balay   a->size         = oldmat->size;
35600ac07820SSatish Balay   a->rank         = oldmat->rank;
3561aef5e8e0SSatish Balay   a->donotstash   = oldmat->donotstash;
3562aef5e8e0SSatish Balay   a->roworiented  = oldmat->roworiented;
3563aef5e8e0SSatish Balay   a->rowindices   = 0;
35640ac07820SSatish Balay   a->rowvalues    = 0;
35650ac07820SSatish Balay   a->getrowactive = PETSC_FALSE;
356630793edcSSatish Balay   a->barray       = 0;
3567899cda47SBarry Smith   a->rstartbs     = oldmat->rstartbs;
3568899cda47SBarry Smith   a->rendbs       = oldmat->rendbs;
3569899cda47SBarry Smith   a->cstartbs     = oldmat->cstartbs;
3570899cda47SBarry Smith   a->cendbs       = oldmat->cendbs;
35710ac07820SSatish Balay 
3572133cdb44SSatish Balay   /* hash table stuff */
3573133cdb44SSatish Balay   a->ht           = 0;
3574133cdb44SSatish Balay   a->hd           = 0;
3575133cdb44SSatish Balay   a->ht_size      = 0;
3576133cdb44SSatish Balay   a->ht_flag      = oldmat->ht_flag;
357725fdafccSSatish Balay   a->ht_fact      = oldmat->ht_fact;
3578133cdb44SSatish Balay   a->ht_total_ct  = 0;
3579133cdb44SSatish Balay   a->ht_insert_ct = 0;
3580133cdb44SSatish Balay 
3581899cda47SBarry Smith   ierr = PetscMemcpy(a->rangebs,oldmat->rangebs,(a->size+1)*sizeof(PetscInt));CHKERRQ(ierr);
35820ac07820SSatish Balay   if (oldmat->colmap) {
3583aa482453SBarry Smith #if defined(PETSC_USE_CTABLE)
35840f5bd95cSBarry Smith     ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
358548e59246SSatish Balay #else
3586b24ad042SBarry Smith     ierr = PetscMalloc((a->Nbs)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr);
358752e6d16bSBarry Smith     ierr = PetscLogObjectMemory(mat,(a->Nbs)*sizeof(PetscInt));CHKERRQ(ierr);
3588b24ad042SBarry Smith     ierr = PetscMemcpy(a->colmap,oldmat->colmap,(a->Nbs)*sizeof(PetscInt));CHKERRQ(ierr);
358948e59246SSatish Balay #endif
35900ac07820SSatish Balay   } else a->colmap = 0;
35914beb1cfeSHong Zhang 
35920ac07820SSatish Balay   if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) {
3593b24ad042SBarry Smith     ierr = PetscMalloc(len*sizeof(PetscInt),&a->garray);CHKERRQ(ierr);
359452e6d16bSBarry Smith     ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr);
3595b24ad042SBarry Smith     ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr);
35960ac07820SSatish Balay   } else a->garray = 0;
35970ac07820SSatish Balay 
3598*ce94432eSBarry Smith   ierr = MatStashCreate_Private(PetscObjectComm((PetscObject)matin),matin->rmap->bs,&mat->bstash);CHKERRQ(ierr);
35990ac07820SSatish Balay   ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
360052e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr);
36010ac07820SSatish Balay   ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
360252e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr);
36037fff6886SHong Zhang 
36042e8a6d31SBarry Smith   ierr    = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
360552e6d16bSBarry Smith   ierr    = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr);
36062e8a6d31SBarry Smith   ierr    = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
360752e6d16bSBarry Smith   ierr    = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr);
3608140e18c1SBarry Smith   ierr    = PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr);
36090ac07820SSatish Balay   *newmat = mat;
36103a40ed3dSBarry Smith   PetscFunctionReturn(0);
36110ac07820SSatish Balay }
361257b952d6SSatish Balay 
36134a2ae208SSatish Balay #undef __FUNCT__
36145bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIBAIJ"
3615112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIBAIJ(Mat newmat,PetscViewer viewer)
36164683f7a4SShri Abhyankar {
36174683f7a4SShri Abhyankar   PetscErrorCode ierr;
36184683f7a4SShri Abhyankar   int            fd;
36194683f7a4SShri Abhyankar   PetscInt       i,nz,j,rstart,rend;
36204683f7a4SShri Abhyankar   PetscScalar    *vals,*buf;
3621*ce94432eSBarry Smith   MPI_Comm       comm;
36224683f7a4SShri Abhyankar   MPI_Status     status;
36234683f7a4SShri Abhyankar   PetscMPIInt    rank,size,maxnz;
36244683f7a4SShri Abhyankar   PetscInt       header[4],*rowlengths = 0,M,N,m,*rowners,*cols;
36250298fd71SBarry Smith   PetscInt       *locrowlens = NULL,*procsnz = NULL,*browners = NULL;
36264683f7a4SShri Abhyankar   PetscInt       jj,*mycols,*ibuf,bs=1,Mbs,mbs,extra_rows,mmax;
36274683f7a4SShri Abhyankar   PetscMPIInt    tag    = ((PetscObject)viewer)->tag;
36280298fd71SBarry Smith   PetscInt       *dlens = NULL,*odlens = NULL,*mask = NULL,*masked1 = NULL,*masked2 = NULL,rowcount,odcount;
36294683f7a4SShri Abhyankar   PetscInt       dcount,kmax,k,nzcount,tmp,mend,sizesset=1,grows,gcols;
36304683f7a4SShri Abhyankar 
36314683f7a4SShri Abhyankar   PetscFunctionBegin;
3632*ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
36330298fd71SBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"Options for loading MPIBAIJ matrix 2","Mat");CHKERRQ(ierr);
36340298fd71SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr);
36354683f7a4SShri Abhyankar   ierr = PetscOptionsEnd();CHKERRQ(ierr);
36364683f7a4SShri Abhyankar 
36374683f7a4SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
36384683f7a4SShri Abhyankar   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
36394683f7a4SShri Abhyankar   if (!rank) {
36404683f7a4SShri Abhyankar     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
36414683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,(char*)header,4,PETSC_INT);CHKERRQ(ierr);
36424683f7a4SShri Abhyankar     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
36434683f7a4SShri Abhyankar   }
36444683f7a4SShri Abhyankar 
36454683f7a4SShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) sizesset = 0;
36464683f7a4SShri Abhyankar 
36474683f7a4SShri Abhyankar   ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr);
36484683f7a4SShri Abhyankar   M    = header[1]; N = header[2];
36494683f7a4SShri Abhyankar 
36504683f7a4SShri Abhyankar   /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */
36514683f7a4SShri Abhyankar   if (sizesset && newmat->rmap->N < 0) newmat->rmap->N = M;
36524683f7a4SShri Abhyankar   if (sizesset && newmat->cmap->N < 0) newmat->cmap->N = N;
36534683f7a4SShri Abhyankar 
36544683f7a4SShri Abhyankar   /* If global sizes are set, check if they are consistent with that given in the file */
36554683f7a4SShri Abhyankar   if (sizesset) {
36564683f7a4SShri Abhyankar     ierr = MatGetSize(newmat,&grows,&gcols);CHKERRQ(ierr);
36574683f7a4SShri Abhyankar   }
3658abd38a8fSBarry 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);
3659abd38a8fSBarry 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);
36604683f7a4SShri Abhyankar 
3661*ce94432eSBarry Smith   if (M != N) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Can only do square matrices");
36624683f7a4SShri Abhyankar 
36634683f7a4SShri Abhyankar   /*
36644683f7a4SShri Abhyankar      This code adds extra rows to make sure the number of rows is
36654683f7a4SShri Abhyankar      divisible by the blocksize
36664683f7a4SShri Abhyankar   */
36674683f7a4SShri Abhyankar   Mbs        = M/bs;
36684683f7a4SShri Abhyankar   extra_rows = bs - M + bs*Mbs;
36694683f7a4SShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
36704683f7a4SShri Abhyankar   else                  Mbs++;
36714683f7a4SShri Abhyankar   if (extra_rows && !rank) {
36724683f7a4SShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
36734683f7a4SShri Abhyankar   }
36744683f7a4SShri Abhyankar 
36754683f7a4SShri Abhyankar   /* determine ownership of all rows */
36764683f7a4SShri Abhyankar   if (newmat->rmap->n < 0) { /* PETSC_DECIDE */
36774683f7a4SShri Abhyankar     mbs = Mbs/size + ((Mbs % size) > rank);
36784683f7a4SShri Abhyankar     m   = mbs*bs;
36794683f7a4SShri Abhyankar   } else { /* User set */
36804683f7a4SShri Abhyankar     m   = newmat->rmap->n;
36814683f7a4SShri Abhyankar     mbs = m/bs;
36824683f7a4SShri Abhyankar   }
36834683f7a4SShri Abhyankar   ierr = PetscMalloc2(size+1,PetscInt,&rowners,size+1,PetscInt,&browners);CHKERRQ(ierr);
36844683f7a4SShri Abhyankar   ierr = MPI_Allgather(&mbs,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
36854683f7a4SShri Abhyankar 
36864683f7a4SShri Abhyankar   /* process 0 needs enough room for process with most rows */
36874683f7a4SShri Abhyankar   if (!rank) {
36884683f7a4SShri Abhyankar     mmax = rowners[1];
36891251c579SMatthew G Knepley     for (i=2; i<=size; i++) {
36904683f7a4SShri Abhyankar       mmax = PetscMax(mmax,rowners[i]);
36914683f7a4SShri Abhyankar     }
36924683f7a4SShri Abhyankar     mmax*=bs;
36934683f7a4SShri Abhyankar   } else mmax = m;
36944683f7a4SShri Abhyankar 
36954683f7a4SShri Abhyankar   rowners[0] = 0;
36964683f7a4SShri Abhyankar   for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
36974683f7a4SShri Abhyankar   for (i=0; i<=size; i++) browners[i] = rowners[i]*bs;
36984683f7a4SShri Abhyankar   rstart = rowners[rank];
36994683f7a4SShri Abhyankar   rend   = rowners[rank+1];
37004683f7a4SShri Abhyankar 
37014683f7a4SShri Abhyankar   /* distribute row lengths to all processors */
37024683f7a4SShri Abhyankar   ierr = PetscMalloc((mmax+1)*sizeof(PetscInt),&locrowlens);CHKERRQ(ierr);
37034683f7a4SShri Abhyankar   if (!rank) {
37044683f7a4SShri Abhyankar     mend = m;
37054683f7a4SShri Abhyankar     if (size == 1) mend = mend - extra_rows;
37064683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,locrowlens,mend,PETSC_INT);CHKERRQ(ierr);
37074683f7a4SShri Abhyankar     for (j=mend; j<m; j++) locrowlens[j] = 1;
37084683f7a4SShri Abhyankar     ierr = PetscMalloc(m*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
37094683f7a4SShri Abhyankar     ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr);
37104683f7a4SShri Abhyankar     ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr);
37114683f7a4SShri Abhyankar     for (j=0; j<m; j++) {
37124683f7a4SShri Abhyankar       procsnz[0] += locrowlens[j];
37134683f7a4SShri Abhyankar     }
37144683f7a4SShri Abhyankar     for (i=1; i<size; i++) {
37154683f7a4SShri Abhyankar       mend = browners[i+1] - browners[i];
37164683f7a4SShri Abhyankar       if (i == size-1) mend = mend - extra_rows;
37174683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,rowlengths,mend,PETSC_INT);CHKERRQ(ierr);
37184683f7a4SShri Abhyankar       for (j=mend; j<browners[i+1] - browners[i]; j++) rowlengths[j] = 1;
37194683f7a4SShri Abhyankar       /* calculate the number of nonzeros on each processor */
37204683f7a4SShri Abhyankar       for (j=0; j<browners[i+1]-browners[i]; j++) {
37214683f7a4SShri Abhyankar         procsnz[i] += rowlengths[j];
37224683f7a4SShri Abhyankar       }
37234683f7a4SShri Abhyankar       ierr = MPI_Send(rowlengths,browners[i+1]-browners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
37244683f7a4SShri Abhyankar     }
37254683f7a4SShri Abhyankar     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
37264683f7a4SShri Abhyankar   } else {
37274683f7a4SShri Abhyankar     ierr = MPI_Recv(locrowlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
37284683f7a4SShri Abhyankar   }
37294683f7a4SShri Abhyankar 
37304683f7a4SShri Abhyankar   if (!rank) {
37314683f7a4SShri Abhyankar     /* determine max buffer needed and allocate it */
37324683f7a4SShri Abhyankar     maxnz = procsnz[0];
37334683f7a4SShri Abhyankar     for (i=1; i<size; i++) {
37344683f7a4SShri Abhyankar       maxnz = PetscMax(maxnz,procsnz[i]);
37354683f7a4SShri Abhyankar     }
37364683f7a4SShri Abhyankar     ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr);
37374683f7a4SShri Abhyankar 
37384683f7a4SShri Abhyankar     /* read in my part of the matrix column indices  */
37394683f7a4SShri Abhyankar     nz     = procsnz[0];
37404683f7a4SShri Abhyankar     ierr   = PetscMalloc((nz+1)*sizeof(PetscInt),&ibuf);CHKERRQ(ierr);
37414683f7a4SShri Abhyankar     mycols = ibuf;
37424683f7a4SShri Abhyankar     if (size == 1) nz -= extra_rows;
37434683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
374426fbe8dcSKarl Rupp     if (size == 1) {
374526fbe8dcSKarl Rupp       for (i=0; i< extra_rows; i++) mycols[nz+i] = M+i;
374626fbe8dcSKarl Rupp     }
37474683f7a4SShri Abhyankar 
37484683f7a4SShri Abhyankar     /* read in every ones (except the last) and ship off */
37494683f7a4SShri Abhyankar     for (i=1; i<size-1; i++) {
37504683f7a4SShri Abhyankar       nz   = procsnz[i];
37514683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
37524683f7a4SShri Abhyankar       ierr = MPI_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
37534683f7a4SShri Abhyankar     }
37544683f7a4SShri Abhyankar     /* read in the stuff for the last proc */
37554683f7a4SShri Abhyankar     if (size != 1) {
37564683f7a4SShri Abhyankar       nz   = procsnz[size-1] - extra_rows;  /* the extra rows are not on the disk */
37574683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
37584683f7a4SShri Abhyankar       for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
37594683f7a4SShri Abhyankar       ierr = MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);CHKERRQ(ierr);
37604683f7a4SShri Abhyankar     }
37614683f7a4SShri Abhyankar     ierr = PetscFree(cols);CHKERRQ(ierr);
37624683f7a4SShri Abhyankar   } else {
37634683f7a4SShri Abhyankar     /* determine buffer space needed for message */
37644683f7a4SShri Abhyankar     nz = 0;
37654683f7a4SShri Abhyankar     for (i=0; i<m; i++) {
37664683f7a4SShri Abhyankar       nz += locrowlens[i];
37674683f7a4SShri Abhyankar     }
37684683f7a4SShri Abhyankar     ierr   = PetscMalloc((nz+1)*sizeof(PetscInt),&ibuf);CHKERRQ(ierr);
37694683f7a4SShri Abhyankar     mycols = ibuf;
37704683f7a4SShri Abhyankar     /* receive message of column indices*/
37714683f7a4SShri Abhyankar     ierr = MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
37724683f7a4SShri Abhyankar     ierr = MPI_Get_count(&status,MPIU_INT,&maxnz);CHKERRQ(ierr);
37734683f7a4SShri Abhyankar     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
37744683f7a4SShri Abhyankar   }
37754683f7a4SShri Abhyankar 
37764683f7a4SShri Abhyankar   /* loop over local rows, determining number of off diagonal entries */
37774683f7a4SShri Abhyankar   ierr     = PetscMalloc2(rend-rstart,PetscInt,&dlens,rend-rstart,PetscInt,&odlens);CHKERRQ(ierr);
37784683f7a4SShri Abhyankar   ierr     = PetscMalloc3(Mbs,PetscInt,&mask,Mbs,PetscInt,&masked1,Mbs,PetscInt,&masked2);CHKERRQ(ierr);
37794683f7a4SShri Abhyankar   ierr     = PetscMemzero(mask,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
37804683f7a4SShri Abhyankar   ierr     = PetscMemzero(masked1,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
37814683f7a4SShri Abhyankar   ierr     = PetscMemzero(masked2,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
37824683f7a4SShri Abhyankar   rowcount = 0; nzcount = 0;
37834683f7a4SShri Abhyankar   for (i=0; i<mbs; i++) {
37844683f7a4SShri Abhyankar     dcount  = 0;
37854683f7a4SShri Abhyankar     odcount = 0;
37864683f7a4SShri Abhyankar     for (j=0; j<bs; j++) {
37874683f7a4SShri Abhyankar       kmax = locrowlens[rowcount];
37884683f7a4SShri Abhyankar       for (k=0; k<kmax; k++) {
37894683f7a4SShri Abhyankar         tmp = mycols[nzcount++]/bs;
37904683f7a4SShri Abhyankar         if (!mask[tmp]) {
37914683f7a4SShri Abhyankar           mask[tmp] = 1;
37924683f7a4SShri Abhyankar           if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp;
37934683f7a4SShri Abhyankar           else masked1[dcount++] = tmp;
37944683f7a4SShri Abhyankar         }
37954683f7a4SShri Abhyankar       }
37964683f7a4SShri Abhyankar       rowcount++;
37974683f7a4SShri Abhyankar     }
37984683f7a4SShri Abhyankar 
37994683f7a4SShri Abhyankar     dlens[i]  = dcount;
38004683f7a4SShri Abhyankar     odlens[i] = odcount;
38014683f7a4SShri Abhyankar 
38024683f7a4SShri Abhyankar     /* zero out the mask elements we set */
38034683f7a4SShri Abhyankar     for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
38044683f7a4SShri Abhyankar     for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
38054683f7a4SShri Abhyankar   }
38064683f7a4SShri Abhyankar 
38074683f7a4SShri Abhyankar 
38084683f7a4SShri Abhyankar   if (!sizesset) {
38094683f7a4SShri Abhyankar     ierr = MatSetSizes(newmat,m,m,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
38104683f7a4SShri Abhyankar   }
38114683f7a4SShri Abhyankar   ierr = MatMPIBAIJSetPreallocation(newmat,bs,0,dlens,0,odlens);CHKERRQ(ierr);
38124683f7a4SShri Abhyankar 
38134683f7a4SShri Abhyankar   if (!rank) {
38144683f7a4SShri Abhyankar     ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&buf);CHKERRQ(ierr);
38154683f7a4SShri Abhyankar     /* read in my part of the matrix numerical values  */
38164683f7a4SShri Abhyankar     nz     = procsnz[0];
38174683f7a4SShri Abhyankar     vals   = buf;
38184683f7a4SShri Abhyankar     mycols = ibuf;
38194683f7a4SShri Abhyankar     if (size == 1) nz -= extra_rows;
38204683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
382126fbe8dcSKarl Rupp     if (size == 1) {
382226fbe8dcSKarl Rupp       for (i=0; i< extra_rows; i++) vals[nz+i] = 1.0;
382326fbe8dcSKarl Rupp     }
38244683f7a4SShri Abhyankar 
38254683f7a4SShri Abhyankar     /* insert into matrix */
38264683f7a4SShri Abhyankar     jj = rstart*bs;
38274683f7a4SShri Abhyankar     for (i=0; i<m; i++) {
38284683f7a4SShri Abhyankar       ierr    = MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);CHKERRQ(ierr);
38294683f7a4SShri Abhyankar       mycols += locrowlens[i];
38304683f7a4SShri Abhyankar       vals   += locrowlens[i];
38314683f7a4SShri Abhyankar       jj++;
38324683f7a4SShri Abhyankar     }
38334683f7a4SShri Abhyankar     /* read in other processors (except the last one) and ship out */
38344683f7a4SShri Abhyankar     for (i=1; i<size-1; i++) {
38354683f7a4SShri Abhyankar       nz   = procsnz[i];
38364683f7a4SShri Abhyankar       vals = buf;
38374683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
3838479e424cSMichael Lange       ierr = MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newmat)->tag,comm);CHKERRQ(ierr);
38394683f7a4SShri Abhyankar     }
38404683f7a4SShri Abhyankar     /* the last proc */
38414683f7a4SShri Abhyankar     if (size != 1) {
38424683f7a4SShri Abhyankar       nz   = procsnz[i] - extra_rows;
38434683f7a4SShri Abhyankar       vals = buf;
38444683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
38454683f7a4SShri Abhyankar       for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
3846479e424cSMichael Lange       ierr = MPIULong_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,((PetscObject)newmat)->tag,comm);CHKERRQ(ierr);
38474683f7a4SShri Abhyankar     }
38484683f7a4SShri Abhyankar     ierr = PetscFree(procsnz);CHKERRQ(ierr);
38494683f7a4SShri Abhyankar   } else {
38504683f7a4SShri Abhyankar     /* receive numeric values */
38514683f7a4SShri Abhyankar     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&buf);CHKERRQ(ierr);
38524683f7a4SShri Abhyankar 
38534683f7a4SShri Abhyankar     /* receive message of values*/
38544683f7a4SShri Abhyankar     vals   = buf;
38554683f7a4SShri Abhyankar     mycols = ibuf;
3856479e424cSMichael Lange     ierr   = MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newmat)->tag,comm);CHKERRQ(ierr);
38574683f7a4SShri Abhyankar 
38584683f7a4SShri Abhyankar     /* insert into matrix */
38594683f7a4SShri Abhyankar     jj = rstart*bs;
38604683f7a4SShri Abhyankar     for (i=0; i<m; i++) {
38614683f7a4SShri Abhyankar       ierr    = MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);CHKERRQ(ierr);
38624683f7a4SShri Abhyankar       mycols += locrowlens[i];
38634683f7a4SShri Abhyankar       vals   += locrowlens[i];
38644683f7a4SShri Abhyankar       jj++;
38654683f7a4SShri Abhyankar     }
38664683f7a4SShri Abhyankar   }
38674683f7a4SShri Abhyankar   ierr = PetscFree(locrowlens);CHKERRQ(ierr);
38684683f7a4SShri Abhyankar   ierr = PetscFree(buf);CHKERRQ(ierr);
38694683f7a4SShri Abhyankar   ierr = PetscFree(ibuf);CHKERRQ(ierr);
38704683f7a4SShri Abhyankar   ierr = PetscFree2(rowners,browners);CHKERRQ(ierr);
38714683f7a4SShri Abhyankar   ierr = PetscFree2(dlens,odlens);CHKERRQ(ierr);
38724683f7a4SShri Abhyankar   ierr = PetscFree3(mask,masked1,masked2);CHKERRQ(ierr);
38734683f7a4SShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
38744683f7a4SShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
38754683f7a4SShri Abhyankar   PetscFunctionReturn(0);
38764683f7a4SShri Abhyankar }
38774683f7a4SShri Abhyankar 
38784683f7a4SShri Abhyankar #undef __FUNCT__
38794a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJSetHashTableFactor"
3880133cdb44SSatish Balay /*@
3881133cdb44SSatish Balay    MatMPIBAIJSetHashTableFactor - Sets the factor required to compute the size of the HashTable.
3882133cdb44SSatish Balay 
3883133cdb44SSatish Balay    Input Parameters:
3884133cdb44SSatish Balay .  mat  - the matrix
3885133cdb44SSatish Balay .  fact - factor
3886133cdb44SSatish Balay 
3887c5eb9154SBarry Smith    Not Collective, each process can use a different factor
3888fee21e36SBarry Smith 
38898c890885SBarry Smith    Level: advanced
38908c890885SBarry Smith 
3891133cdb44SSatish Balay   Notes:
38928c07d4e3SBarry Smith    This can also be set by the command line option: -mat_use_hash_table <fact>
3893133cdb44SSatish Balay 
3894133cdb44SSatish Balay .keywords: matrix, hashtable, factor, HT
3895133cdb44SSatish Balay 
3896133cdb44SSatish Balay .seealso: MatSetOption()
3897133cdb44SSatish Balay @*/
38987087cfbeSBarry Smith PetscErrorCode  MatMPIBAIJSetHashTableFactor(Mat mat,PetscReal fact)
3899133cdb44SSatish Balay {
39004ac538c5SBarry Smith   PetscErrorCode ierr;
39015bf65638SKris Buschelman 
39025bf65638SKris Buschelman   PetscFunctionBegin;
39034ac538c5SBarry Smith   ierr = PetscTryMethod(mat,"MatSetHashTableFactor_C",(Mat,PetscReal),(mat,fact));CHKERRQ(ierr);
39045bf65638SKris Buschelman   PetscFunctionReturn(0);
39055bf65638SKris Buschelman }
39065bf65638SKris Buschelman 
3907be1d678aSKris Buschelman EXTERN_C_BEGIN
39085bf65638SKris Buschelman #undef __FUNCT__
39095bf65638SKris Buschelman #define __FUNCT__ "MatSetHashTableFactor_MPIBAIJ"
39107087cfbeSBarry Smith PetscErrorCode  MatSetHashTableFactor_MPIBAIJ(Mat mat,PetscReal fact)
39115bf65638SKris Buschelman {
391225fdafccSSatish Balay   Mat_MPIBAIJ *baij;
3913133cdb44SSatish Balay 
3914133cdb44SSatish Balay   PetscFunctionBegin;
3915133cdb44SSatish Balay   baij          = (Mat_MPIBAIJ*)mat->data;
3916133cdb44SSatish Balay   baij->ht_fact = fact;
3917133cdb44SSatish Balay   PetscFunctionReturn(0);
3918133cdb44SSatish Balay }
3919be1d678aSKris Buschelman EXTERN_C_END
3920f2a5309cSSatish Balay 
39214a2ae208SSatish Balay #undef __FUNCT__
39224a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJGetSeqBAIJ"
39239230625dSJed Brown PetscErrorCode  MatMPIBAIJGetSeqBAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[])
3924f2a5309cSSatish Balay {
3925f2a5309cSSatish Balay   Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
39265fd66863SKarl Rupp 
3927f2a5309cSSatish Balay   PetscFunctionBegin;
3928f2a5309cSSatish Balay   *Ad     = a->A;
3929f2a5309cSSatish Balay   *Ao     = a->B;
3930195d93cdSBarry Smith   *colmap = a->garray;
3931f2a5309cSSatish Balay   PetscFunctionReturn(0);
3932f2a5309cSSatish Balay }
393385535b8eSBarry Smith 
393485535b8eSBarry Smith /*
393585535b8eSBarry Smith     Special version for direct calls from Fortran (to eliminate two function call overheads
393685535b8eSBarry Smith */
393785535b8eSBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
393885535b8eSBarry Smith #define matmpibaijsetvaluesblocked_ MATMPIBAIJSETVALUESBLOCKED
393985535b8eSBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
394085535b8eSBarry Smith #define matmpibaijsetvaluesblocked_ matmpibaijsetvaluesblocked
394185535b8eSBarry Smith #endif
394285535b8eSBarry Smith 
394385535b8eSBarry Smith #undef __FUNCT__
394485535b8eSBarry Smith #define __FUNCT__ "matmpibiajsetvaluesblocked"
394585535b8eSBarry Smith /*@C
394685535b8eSBarry Smith   MatMPIBAIJSetValuesBlocked - Direct Fortran call to replace call to MatSetValuesBlocked()
394785535b8eSBarry Smith 
394885535b8eSBarry Smith   Collective on Mat
394985535b8eSBarry Smith 
395085535b8eSBarry Smith   Input Parameters:
395185535b8eSBarry Smith + mat - the matrix
395285535b8eSBarry Smith . min - number of input rows
395385535b8eSBarry Smith . im - input rows
395485535b8eSBarry Smith . nin - number of input columns
395585535b8eSBarry Smith . in - input columns
395685535b8eSBarry Smith . v - numerical values input
395785535b8eSBarry Smith - addvin - INSERT_VALUES or ADD_VALUES
395885535b8eSBarry Smith 
395985535b8eSBarry Smith   Notes: This has a complete copy of MatSetValuesBlocked_MPIBAIJ() which is terrible code un-reuse.
396085535b8eSBarry Smith 
396185535b8eSBarry Smith   Level: advanced
396285535b8eSBarry Smith 
396385535b8eSBarry Smith .seealso:   MatSetValuesBlocked()
396485535b8eSBarry Smith @*/
396585535b8eSBarry Smith PetscErrorCode matmpibaijsetvaluesblocked_(Mat *matin,PetscInt *min,const PetscInt im[],PetscInt *nin,const PetscInt in[],const MatScalar v[],InsertMode *addvin)
396685535b8eSBarry Smith {
396785535b8eSBarry Smith   /* convert input arguments to C version */
396885535b8eSBarry Smith   Mat        mat  = *matin;
396985535b8eSBarry Smith   PetscInt   m    = *min, n = *nin;
397085535b8eSBarry Smith   InsertMode addv = *addvin;
397185535b8eSBarry Smith 
397285535b8eSBarry Smith   Mat_MPIBAIJ     *baij = (Mat_MPIBAIJ*)mat->data;
397385535b8eSBarry Smith   const MatScalar *value;
397485535b8eSBarry Smith   MatScalar       *barray     = baij->barray;
3975ace3abfcSBarry Smith   PetscBool       roworiented = baij->roworiented;
397685535b8eSBarry Smith   PetscErrorCode  ierr;
397785535b8eSBarry Smith   PetscInt        i,j,ii,jj,row,col,rstart=baij->rstartbs;
397885535b8eSBarry Smith   PetscInt        rend=baij->rendbs,cstart=baij->cstartbs,stepval;
3979d0f46423SBarry Smith   PetscInt        cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;
398085535b8eSBarry Smith 
398185535b8eSBarry Smith   PetscFunctionBegin;
398285535b8eSBarry Smith   /* tasks normally handled by MatSetValuesBlocked() */
398326fbe8dcSKarl Rupp   if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv;
398485535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
3985e7e72b3dSBarry Smith   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
3986e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
398785535b8eSBarry Smith #endif
398885535b8eSBarry Smith   if (mat->assembled) {
398985535b8eSBarry Smith     mat->was_assembled = PETSC_TRUE;
399085535b8eSBarry Smith     mat->assembled     = PETSC_FALSE;
399185535b8eSBarry Smith   }
399285535b8eSBarry Smith   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
399385535b8eSBarry Smith 
399485535b8eSBarry Smith 
399585535b8eSBarry Smith   if (!barray) {
399685535b8eSBarry Smith     ierr         = PetscMalloc(bs2*sizeof(MatScalar),&barray);CHKERRQ(ierr);
399785535b8eSBarry Smith     baij->barray = barray;
399885535b8eSBarry Smith   }
399985535b8eSBarry Smith 
400026fbe8dcSKarl Rupp   if (roworiented) stepval = (n-1)*bs;
400126fbe8dcSKarl Rupp   else stepval = (m-1)*bs;
400226fbe8dcSKarl Rupp 
400385535b8eSBarry Smith   for (i=0; i<m; i++) {
400485535b8eSBarry Smith     if (im[i] < 0) continue;
400585535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
4006e32f2f54SBarry 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);
400785535b8eSBarry Smith #endif
400885535b8eSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
400985535b8eSBarry Smith       row = im[i] - rstart;
401085535b8eSBarry Smith       for (j=0; j<n; j++) {
401185535b8eSBarry Smith         /* If NumCol = 1 then a copy is not required */
401285535b8eSBarry Smith         if ((roworiented) && (n == 1)) {
401385535b8eSBarry Smith           barray = (MatScalar*)v + i*bs2;
401485535b8eSBarry Smith         } else if ((!roworiented) && (m == 1)) {
401585535b8eSBarry Smith           barray = (MatScalar*)v + j*bs2;
401685535b8eSBarry Smith         } else { /* Here a copy is required */
401785535b8eSBarry Smith           if (roworiented) {
401885535b8eSBarry Smith             value = v + i*(stepval+bs)*bs + j*bs;
401985535b8eSBarry Smith           } else {
402085535b8eSBarry Smith             value = v + j*(stepval+bs)*bs + i*bs;
402185535b8eSBarry Smith           }
402285535b8eSBarry Smith           for (ii=0; ii<bs; ii++,value+=stepval) {
402385535b8eSBarry Smith             for (jj=0; jj<bs; jj++) {
402485535b8eSBarry Smith               *barray++ = *value++;
402585535b8eSBarry Smith             }
402685535b8eSBarry Smith           }
402785535b8eSBarry Smith           barray -=bs2;
402885535b8eSBarry Smith         }
402985535b8eSBarry Smith 
403085535b8eSBarry Smith         if (in[j] >= cstart && in[j] < cend) {
403185535b8eSBarry Smith           col  = in[j] - cstart;
403297e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
403326fbe8dcSKarl Rupp         } else if (in[j] < 0) continue;
403485535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
4035cb9801acSJed 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);
403685535b8eSBarry Smith #endif
403785535b8eSBarry Smith         else {
403885535b8eSBarry Smith           if (mat->was_assembled) {
403985535b8eSBarry Smith             if (!baij->colmap) {
4040ab9863d7SBarry Smith               ierr = MatCreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
404185535b8eSBarry Smith             }
404285535b8eSBarry Smith 
404385535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
404485535b8eSBarry Smith #if defined(PETSC_USE_CTABLE)
404585535b8eSBarry Smith             { PetscInt data;
404685535b8eSBarry Smith               ierr = PetscTableFind(baij->colmap,in[j]+1,&data);CHKERRQ(ierr);
4047e32f2f54SBarry Smith               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
404885535b8eSBarry Smith             }
404985535b8eSBarry Smith #else
4050e32f2f54SBarry Smith             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
405185535b8eSBarry Smith #endif
405285535b8eSBarry Smith #endif
405385535b8eSBarry Smith #if defined(PETSC_USE_CTABLE)
405485535b8eSBarry Smith             ierr = PetscTableFind(baij->colmap,in[j]+1,&col);CHKERRQ(ierr);
405585535b8eSBarry Smith             col  = (col - 1)/bs;
405685535b8eSBarry Smith #else
405785535b8eSBarry Smith             col = (baij->colmap[in[j]] - 1)/bs;
405885535b8eSBarry Smith #endif
405985535b8eSBarry Smith             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
4060ab9863d7SBarry Smith               ierr = MatDisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
406185535b8eSBarry Smith               col  =  in[j];
406285535b8eSBarry Smith             }
406326fbe8dcSKarl Rupp           } else col = in[j];
406497e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
406585535b8eSBarry Smith         }
406685535b8eSBarry Smith       }
406785535b8eSBarry Smith     } else {
406885535b8eSBarry Smith       if (!baij->donotstash) {
406985535b8eSBarry Smith         if (roworiented) {
407085535b8eSBarry Smith           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
407185535b8eSBarry Smith         } else {
407285535b8eSBarry Smith           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
407385535b8eSBarry Smith         }
407485535b8eSBarry Smith       }
407585535b8eSBarry Smith     }
407685535b8eSBarry Smith   }
407785535b8eSBarry Smith 
407885535b8eSBarry Smith   /* task normally handled by MatSetValuesBlocked() */
407985535b8eSBarry Smith   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
408085535b8eSBarry Smith   PetscFunctionReturn(0);
408185535b8eSBarry Smith }
4082dfb205c3SBarry Smith 
4083dfb205c3SBarry Smith #undef __FUNCT__
4084dfb205c3SBarry Smith #define __FUNCT__ "MatCreateMPIBAIJWithArrays"
4085dfb205c3SBarry Smith /*@
4086dfb205c3SBarry Smith      MatCreateMPIBAIJWithArrays - creates a MPI BAIJ matrix using arrays that contain in standard
4087dfb205c3SBarry Smith          CSR format the local rows.
4088dfb205c3SBarry Smith 
4089dfb205c3SBarry Smith    Collective on MPI_Comm
4090dfb205c3SBarry Smith 
4091dfb205c3SBarry Smith    Input Parameters:
4092dfb205c3SBarry Smith +  comm - MPI communicator
4093dfb205c3SBarry Smith .  bs - the block size, only a block size of 1 is supported
4094dfb205c3SBarry Smith .  m - number of local rows (Cannot be PETSC_DECIDE)
4095dfb205c3SBarry Smith .  n - This value should be the same as the local size used in creating the
4096dfb205c3SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4097dfb205c3SBarry Smith        calculated if N is given) For square matrices n is almost always m.
4098dfb205c3SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4099dfb205c3SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4100dfb205c3SBarry Smith .   i - row indices
4101dfb205c3SBarry Smith .   j - column indices
4102dfb205c3SBarry Smith -   a - matrix values
4103dfb205c3SBarry Smith 
4104dfb205c3SBarry Smith    Output Parameter:
4105dfb205c3SBarry Smith .   mat - the matrix
4106dfb205c3SBarry Smith 
4107dfb205c3SBarry Smith    Level: intermediate
4108dfb205c3SBarry Smith 
4109dfb205c3SBarry Smith    Notes:
4110dfb205c3SBarry Smith        The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
4111dfb205c3SBarry Smith      thus you CANNOT change the matrix entries by changing the values of a[] after you have
4112dfb205c3SBarry Smith      called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
4113dfb205c3SBarry Smith 
4114dfb205c3SBarry Smith        The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
4115dfb205c3SBarry Smith 
4116dfb205c3SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
4117dfb205c3SBarry Smith 
4118dfb205c3SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
411969b1f4b7SBarry Smith           MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
4120dfb205c3SBarry Smith @*/
41217087cfbeSBarry 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)
4122dfb205c3SBarry Smith {
4123dfb205c3SBarry Smith   PetscErrorCode ierr;
4124dfb205c3SBarry Smith 
4125dfb205c3SBarry Smith   PetscFunctionBegin;
4126f23aa3ddSBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4127dfb205c3SBarry Smith   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
4128dfb205c3SBarry Smith   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4129dfb205c3SBarry Smith   ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr);
4130dfb205c3SBarry Smith   ierr = MatSetType(*mat,MATMPISBAIJ);CHKERRQ(ierr);
4131dfb205c3SBarry Smith   ierr = MatMPIBAIJSetPreallocationCSR(*mat,bs,i,j,a);CHKERRQ(ierr);
4132dfb205c3SBarry Smith   PetscFunctionReturn(0);
4133dfb205c3SBarry Smith }
4134