xref: /petsc/src/mat/impls/baij/mpi/mpibaij.c (revision 4994cf479fe3cab9bbc99db3d00c8198cfe9191f)
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);
609573ac7SBarry Smith extern PetscErrorCode DisAssemble_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) {
28192daf7cSBarry Smith     for (i=0; i<A->rmap->n; i++) {if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;}
29985db425SBarry Smith   }
307843d17aSBarry Smith 
31d0f46423SBarry Smith   ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr);
32d0f46423SBarry Smith   if (idx) {ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr);}
33985db425SBarry Smith   ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr);
341ebc52fbSHong Zhang   ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr);
357843d17aSBarry Smith 
36d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++){
37d0f46423SBarry Smith     if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {va[i] = vb[i]; if (idx) idx[i] = A->cmap->bs*a->garray[idxb[i]/A->cmap->bs] + (idxb[i] % A->cmap->bs);}
387843d17aSBarry Smith   }
397843d17aSBarry Smith 
401ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&va);CHKERRQ(ierr);
411ebc52fbSHong Zhang   ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr);
42c31cb41cSBarry Smith   ierr = PetscFree(idxb);CHKERRQ(ierr);
436bf464f9SBarry Smith   ierr = VecDestroy(&vtmp);CHKERRQ(ierr);
447843d17aSBarry Smith   PetscFunctionReturn(0);
457843d17aSBarry Smith }
467843d17aSBarry Smith 
477fc3c18eSBarry Smith EXTERN_C_BEGIN
484a2ae208SSatish Balay #undef __FUNCT__
494a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIBAIJ"
507087cfbeSBarry Smith PetscErrorCode  MatStoreValues_MPIBAIJ(Mat mat)
517fc3c18eSBarry Smith {
527fc3c18eSBarry Smith   Mat_MPIBAIJ    *aij = (Mat_MPIBAIJ *)mat->data;
53dfbe8321SBarry Smith   PetscErrorCode ierr;
547fc3c18eSBarry Smith 
557fc3c18eSBarry Smith   PetscFunctionBegin;
567fc3c18eSBarry Smith   ierr = MatStoreValues(aij->A);CHKERRQ(ierr);
577fc3c18eSBarry Smith   ierr = MatStoreValues(aij->B);CHKERRQ(ierr);
587fc3c18eSBarry Smith   PetscFunctionReturn(0);
597fc3c18eSBarry Smith }
607fc3c18eSBarry Smith EXTERN_C_END
617fc3c18eSBarry Smith 
627fc3c18eSBarry Smith EXTERN_C_BEGIN
634a2ae208SSatish Balay #undef __FUNCT__
644a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIBAIJ"
657087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_MPIBAIJ(Mat mat)
667fc3c18eSBarry Smith {
677fc3c18eSBarry Smith   Mat_MPIBAIJ    *aij = (Mat_MPIBAIJ *)mat->data;
68dfbe8321SBarry Smith   PetscErrorCode ierr;
697fc3c18eSBarry Smith 
707fc3c18eSBarry Smith   PetscFunctionBegin;
717fc3c18eSBarry Smith   ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr);
727fc3c18eSBarry Smith   ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr);
737fc3c18eSBarry Smith   PetscFunctionReturn(0);
747fc3c18eSBarry Smith }
757fc3c18eSBarry Smith EXTERN_C_END
767fc3c18eSBarry Smith 
77537820f0SBarry Smith /*
78537820f0SBarry Smith      Local utility routine that creates a mapping from the global column
7957b952d6SSatish Balay    number to the local number in the off-diagonal part of the local
80e06f6af7SJed Brown    storage of the matrix.  This is done in a non scalable way since the
8157b952d6SSatish Balay    length of colmap equals the global matrix length.
8257b952d6SSatish Balay */
834a2ae208SSatish Balay #undef __FUNCT__
844a2ae208SSatish Balay #define __FUNCT__ "CreateColmap_MPIBAIJ_Private"
85dfbe8321SBarry Smith PetscErrorCode CreateColmap_MPIBAIJ_Private(Mat mat)
8657b952d6SSatish Balay {
8757b952d6SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
8857b952d6SSatish Balay   Mat_SeqBAIJ    *B = (Mat_SeqBAIJ*)baij->B->data;
896849ba73SBarry Smith   PetscErrorCode ierr;
90d0f46423SBarry Smith   PetscInt       nbs = B->nbs,i,bs=mat->rmap->bs;
9157b952d6SSatish Balay 
92d64ed03dSBarry Smith   PetscFunctionBegin;
93aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
94e23dfa41SBarry Smith   ierr = PetscTableCreate(baij->nbs,baij->Nbs+1,&baij->colmap);CHKERRQ(ierr);
9548e59246SSatish Balay   for (i=0; i<nbs; i++){
963861aac3SJed Brown     ierr = PetscTableAdd(baij->colmap,baij->garray[i]+1,i*bs+1,INSERT_VALUES);CHKERRQ(ierr);
9748e59246SSatish Balay   }
9848e59246SSatish Balay #else
99b24ad042SBarry Smith   ierr = PetscMalloc((baij->Nbs+1)*sizeof(PetscInt),&baij->colmap);CHKERRQ(ierr);
10052e6d16bSBarry Smith   ierr = PetscLogObjectMemory(mat,baij->Nbs*sizeof(PetscInt));CHKERRQ(ierr);
101b24ad042SBarry Smith   ierr = PetscMemzero(baij->colmap,baij->Nbs*sizeof(PetscInt));CHKERRQ(ierr);
102928fc39bSSatish Balay   for (i=0; i<nbs; i++) baij->colmap[baij->garray[i]] = i*bs+1;
10348e59246SSatish Balay #endif
1043a40ed3dSBarry Smith   PetscFunctionReturn(0);
10557b952d6SSatish Balay }
10657b952d6SSatish Balay 
107f5e9677aSSatish Balay #define  MatSetValues_SeqBAIJ_A_Private(row,col,value,addv) \
10880c1aa95SSatish Balay { \
10980c1aa95SSatish Balay  \
11080c1aa95SSatish Balay     brow = row/bs;  \
11180c1aa95SSatish Balay     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow]; \
112ac7a638eSSatish Balay     rmax = aimax[brow]; nrow = ailen[brow]; \
11380c1aa95SSatish Balay       bcol = col/bs; \
11480c1aa95SSatish Balay       ridx = row % bs; cidx = col % bs; \
115ab26458aSBarry Smith       low = 0; high = nrow; \
116ab26458aSBarry Smith       while (high-low > 3) { \
117ab26458aSBarry Smith         t = (low+high)/2; \
118ab26458aSBarry Smith         if (rp[t] > bcol) high = t; \
119ab26458aSBarry Smith         else              low  = t; \
120ab26458aSBarry Smith       } \
121ab26458aSBarry Smith       for (_i=low; _i<high; _i++) { \
12280c1aa95SSatish Balay         if (rp[_i] > bcol) break; \
12380c1aa95SSatish Balay         if (rp[_i] == bcol) { \
12480c1aa95SSatish Balay           bap  = ap +  bs2*_i + bs*cidx + ridx; \
125eada6651SSatish Balay           if (addv == ADD_VALUES) *bap += value;  \
126eada6651SSatish Balay           else                    *bap  = value;  \
127ac7a638eSSatish Balay           goto a_noinsert; \
12880c1aa95SSatish Balay         } \
12980c1aa95SSatish Balay       } \
13089280ab3SLois Curfman McInnes       if (a->nonew == 1) goto a_noinsert; \
131e32f2f54SBarry Smith       if (a->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
132fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,aimax,a->nonew,MatScalar); \
13380c1aa95SSatish Balay       N = nrow++ - 1;  \
13480c1aa95SSatish Balay       /* shift up all the later entries in this row */ \
13580c1aa95SSatish Balay       for (ii=N; ii>=_i; ii--) { \
13680c1aa95SSatish Balay         rp[ii+1] = rp[ii]; \
1373eda8832SBarry Smith         ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr); \
13880c1aa95SSatish Balay       } \
1393eda8832SBarry Smith       if (N>=_i) { ierr = PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));CHKERRQ(ierr); }  \
14080c1aa95SSatish Balay       rp[_i]                      = bcol;  \
14180c1aa95SSatish Balay       ap[bs2*_i + bs*cidx + ridx] = value;  \
142ac7a638eSSatish Balay       a_noinsert:; \
14380c1aa95SSatish Balay     ailen[brow] = nrow; \
14480c1aa95SSatish Balay }
14557b952d6SSatish Balay 
146ac7a638eSSatish Balay #define  MatSetValues_SeqBAIJ_B_Private(row,col,value,addv) \
147ac7a638eSSatish Balay { \
148ac7a638eSSatish Balay     brow = row/bs;  \
149ac7a638eSSatish Balay     rp   = bj + bi[brow]; ap = ba + bs2*bi[brow]; \
150ac7a638eSSatish Balay     rmax = bimax[brow]; nrow = bilen[brow]; \
151ac7a638eSSatish Balay       bcol = col/bs; \
152ac7a638eSSatish Balay       ridx = row % bs; cidx = col % bs; \
153ac7a638eSSatish Balay       low = 0; high = nrow; \
154ac7a638eSSatish Balay       while (high-low > 3) { \
155ac7a638eSSatish Balay         t = (low+high)/2; \
156ac7a638eSSatish Balay         if (rp[t] > bcol) high = t; \
157ac7a638eSSatish Balay         else              low  = t; \
158ac7a638eSSatish Balay       } \
159ac7a638eSSatish Balay       for (_i=low; _i<high; _i++) { \
160ac7a638eSSatish Balay         if (rp[_i] > bcol) break; \
161ac7a638eSSatish Balay         if (rp[_i] == bcol) { \
162ac7a638eSSatish Balay           bap  = ap +  bs2*_i + bs*cidx + ridx; \
163ac7a638eSSatish Balay           if (addv == ADD_VALUES) *bap += value;  \
164ac7a638eSSatish Balay           else                    *bap  = value;  \
165ac7a638eSSatish Balay           goto b_noinsert; \
166ac7a638eSSatish Balay         } \
167ac7a638eSSatish Balay       } \
16889280ab3SLois Curfman McInnes       if (b->nonew == 1) goto b_noinsert; \
169e32f2f54SBarry Smith       if (b->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
170fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(B,b->mbs,bs2,nrow,brow,bcol,rmax,ba,bi,bj,rp,ap,bimax,b->nonew,MatScalar); \
171085a36d4SBarry Smith       CHKMEMQ;\
172ac7a638eSSatish Balay       N = nrow++ - 1;  \
173ac7a638eSSatish Balay       /* shift up all the later entries in this row */ \
174ac7a638eSSatish Balay       for (ii=N; ii>=_i; ii--) { \
175ac7a638eSSatish Balay         rp[ii+1] = rp[ii]; \
1763eda8832SBarry Smith         ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr); \
177ac7a638eSSatish Balay       } \
1783eda8832SBarry Smith       if (N>=_i) { ierr = PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));CHKERRQ(ierr);}  \
179ac7a638eSSatish Balay       rp[_i]                      = bcol;  \
180ac7a638eSSatish Balay       ap[bs2*_i + bs*cidx + ridx] = value;  \
181ac7a638eSSatish Balay       b_noinsert:; \
182ac7a638eSSatish Balay     bilen[brow] = nrow; \
183ac7a638eSSatish Balay }
184ac7a638eSSatish Balay 
1854a2ae208SSatish Balay #undef __FUNCT__
1864a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIBAIJ"
187b24ad042SBarry Smith PetscErrorCode MatSetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
18857b952d6SSatish Balay {
18957b952d6SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
19093fea6afSBarry Smith   MatScalar      value;
191ace3abfcSBarry Smith   PetscBool      roworiented = baij->roworiented;
192dfbe8321SBarry Smith   PetscErrorCode ierr;
193b24ad042SBarry Smith   PetscInt       i,j,row,col;
194d0f46423SBarry Smith   PetscInt       rstart_orig=mat->rmap->rstart;
195d0f46423SBarry Smith   PetscInt       rend_orig=mat->rmap->rend,cstart_orig=mat->cmap->rstart;
196d0f46423SBarry Smith   PetscInt       cend_orig=mat->cmap->rend,bs=mat->rmap->bs;
19757b952d6SSatish Balay 
198eada6651SSatish Balay   /* Some Variables required in the macro */
19980c1aa95SSatish Balay   Mat            A = baij->A;
20080c1aa95SSatish Balay   Mat_SeqBAIJ    *a = (Mat_SeqBAIJ*)(A)->data;
201b24ad042SBarry Smith   PetscInt       *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j;
2023eda8832SBarry Smith   MatScalar      *aa=a->a;
203ac7a638eSSatish Balay 
204ac7a638eSSatish Balay   Mat            B = baij->B;
205ac7a638eSSatish Balay   Mat_SeqBAIJ    *b = (Mat_SeqBAIJ*)(B)->data;
206b24ad042SBarry Smith   PetscInt       *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j;
2073eda8832SBarry Smith   MatScalar      *ba=b->a;
208ac7a638eSSatish Balay 
209b24ad042SBarry Smith   PetscInt       *rp,ii,nrow,_i,rmax,N,brow,bcol;
210b24ad042SBarry Smith   PetscInt       low,high,t,ridx,cidx,bs2=a->bs2;
2113eda8832SBarry Smith   MatScalar      *ap,*bap;
21280c1aa95SSatish Balay 
213d64ed03dSBarry Smith   PetscFunctionBegin;
21471fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
21557b952d6SSatish Balay   for (i=0; i<m; i++) {
2165ef9f2a5SBarry Smith     if (im[i] < 0) continue;
2172515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
218e32f2f54SBarry Smith     if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1);
219639f9d9dSBarry Smith #endif
22057b952d6SSatish Balay     if (im[i] >= rstart_orig && im[i] < rend_orig) {
22157b952d6SSatish Balay       row = im[i] - rstart_orig;
22257b952d6SSatish Balay       for (j=0; j<n; j++) {
22357b952d6SSatish Balay         if (in[j] >= cstart_orig && in[j] < cend_orig){
22457b952d6SSatish Balay           col = in[j] - cstart_orig;
22557b952d6SSatish Balay           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
226f5e9677aSSatish Balay           MatSetValues_SeqBAIJ_A_Private(row,col,value,addv);
22780c1aa95SSatish Balay           /* ierr = MatSetValues_SeqBAIJ(baij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
22873959e64SBarry Smith         } else if (in[j] < 0) continue;
2292515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
230660746e0SBarry 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);
231639f9d9dSBarry Smith #endif
23257b952d6SSatish Balay         else {
23357b952d6SSatish Balay           if (mat->was_assembled) {
234905e6a2fSBarry Smith             if (!baij->colmap) {
235905e6a2fSBarry Smith               ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
236905e6a2fSBarry Smith             }
237aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
2380f5bd95cSBarry Smith             ierr = PetscTableFind(baij->colmap,in[j]/bs + 1,&col);CHKERRQ(ierr);
239bba1ac68SSatish Balay             col  = col - 1;
24048e59246SSatish Balay #else
241bba1ac68SSatish Balay             col = baij->colmap[in[j]/bs] - 1;
24248e59246SSatish Balay #endif
24357b952d6SSatish Balay             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
24457b952d6SSatish Balay               ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
2458295de27SSatish Balay               col =  in[j];
2469bf004c3SSatish Balay               /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
2479bf004c3SSatish Balay               B = baij->B;
2489bf004c3SSatish Balay               b = (Mat_SeqBAIJ*)(B)->data;
2499bf004c3SSatish Balay               bimax=b->imax;bi=b->i;bilen=b->ilen;bj=b->j;
2509bf004c3SSatish Balay               ba=b->a;
251bba1ac68SSatish Balay             } else col += in[j]%bs;
2528295de27SSatish Balay           } else col = in[j];
25357b952d6SSatish Balay           if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
25490da58bdSSatish Balay           MatSetValues_SeqBAIJ_B_Private(row,col,value,addv);
25590da58bdSSatish Balay           /* ierr = MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */
25657b952d6SSatish Balay         }
25757b952d6SSatish Balay       }
258d64ed03dSBarry Smith     } else {
2594cb17eb5SBarry 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]);
26090f02eecSBarry Smith       if (!baij->donotstash) {
261ff2fd236SBarry Smith         if (roworiented) {
262b400d20cSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);CHKERRQ(ierr);
263ff2fd236SBarry Smith         } else {
264b400d20cSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);CHKERRQ(ierr);
26557b952d6SSatish Balay         }
26657b952d6SSatish Balay       }
26757b952d6SSatish Balay     }
26890f02eecSBarry Smith   }
2693a40ed3dSBarry Smith   PetscFunctionReturn(0);
27057b952d6SSatish Balay }
27157b952d6SSatish Balay 
2724a2ae208SSatish Balay #undef __FUNCT__
27397e5c40aSBarry Smith #define __FUNCT__ "MatSetValuesBlocked_MPIBAIJ"
27497e5c40aSBarry Smith PetscErrorCode MatSetValuesBlocked_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
275ab26458aSBarry Smith {
276ab26458aSBarry Smith   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
277dd6ea824SBarry Smith   const PetscScalar *value;
278f15d580aSBarry Smith   MatScalar         *barray=baij->barray;
279ace3abfcSBarry Smith   PetscBool         roworiented = baij->roworiented;
280dfbe8321SBarry Smith   PetscErrorCode    ierr;
281899cda47SBarry Smith   PetscInt          i,j,ii,jj,row,col,rstart=baij->rstartbs;
282899cda47SBarry Smith   PetscInt          rend=baij->rendbs,cstart=baij->cstartbs,stepval;
283d0f46423SBarry Smith   PetscInt          cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;
284ab26458aSBarry Smith 
285b16ae2b1SBarry Smith   PetscFunctionBegin;
28630793edcSSatish Balay   if(!barray) {
28782502324SSatish Balay     ierr         = PetscMalloc(bs2*sizeof(MatScalar),&barray);CHKERRQ(ierr);
28882502324SSatish Balay     baij->barray = barray;
28930793edcSSatish Balay   }
29030793edcSSatish Balay 
291ab26458aSBarry Smith   if (roworiented) {
292ab26458aSBarry Smith     stepval = (n-1)*bs;
293ab26458aSBarry Smith   } else {
294ab26458aSBarry Smith     stepval = (m-1)*bs;
295ab26458aSBarry Smith   }
296ab26458aSBarry Smith   for (i=0; i<m; i++) {
2975ef9f2a5SBarry Smith     if (im[i] < 0) continue;
2982515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
299e32f2f54SBarry 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);
300ab26458aSBarry Smith #endif
301ab26458aSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
302ab26458aSBarry Smith       row = im[i] - rstart;
303ab26458aSBarry Smith       for (j=0; j<n; j++) {
30415b57d14SSatish Balay         /* If NumCol = 1 then a copy is not required */
30515b57d14SSatish Balay         if ((roworiented) && (n == 1)) {
306f15d580aSBarry Smith           barray = (MatScalar*)v + i*bs2;
30715b57d14SSatish Balay         } else if((!roworiented) && (m == 1)) {
308f15d580aSBarry Smith           barray = (MatScalar*)v + j*bs2;
30915b57d14SSatish Balay         } else { /* Here a copy is required */
310ab26458aSBarry Smith           if (roworiented) {
31153ef36baSBarry Smith             value = v + (i*(stepval+bs) + j)*bs;
312ab26458aSBarry Smith           } else {
31353ef36baSBarry Smith 	    value = v + (j*(stepval+bs) + i)*bs;
314abef11f7SSatish Balay           }
31553ef36baSBarry Smith           for (ii=0; ii<bs; ii++,value+=bs+stepval) {
31647513183SBarry Smith             for (jj=0; jj<bs; jj++) {
31753ef36baSBarry Smith               barray[jj]  = value[jj];
31847513183SBarry Smith             }
31953ef36baSBarry Smith             barray += bs;
32047513183SBarry Smith           }
32130793edcSSatish Balay           barray -= bs2;
32215b57d14SSatish Balay         }
323abef11f7SSatish Balay 
324abef11f7SSatish Balay         if (in[j] >= cstart && in[j] < cend){
325abef11f7SSatish Balay           col  = in[j] - cstart;
32697e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
327ab26458aSBarry Smith         }
3285ef9f2a5SBarry Smith         else if (in[j] < 0) continue;
3292515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
330cb9801acSJed 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);
331ab26458aSBarry Smith #endif
332ab26458aSBarry Smith         else {
333ab26458aSBarry Smith           if (mat->was_assembled) {
334ab26458aSBarry Smith             if (!baij->colmap) {
335ab26458aSBarry Smith               ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
336ab26458aSBarry Smith             }
337a5eb4965SSatish Balay 
3382515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
339aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
340b24ad042SBarry Smith             { PetscInt data;
3410f5bd95cSBarry Smith               ierr = PetscTableFind(baij->colmap,in[j]+1,&data);CHKERRQ(ierr);
342e32f2f54SBarry Smith               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
343fa46199cSSatish Balay             }
34448e59246SSatish Balay #else
345e32f2f54SBarry Smith             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
346a5eb4965SSatish Balay #endif
34748e59246SSatish Balay #endif
348aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
3490f5bd95cSBarry Smith 	    ierr = PetscTableFind(baij->colmap,in[j]+1,&col);CHKERRQ(ierr);
350fa46199cSSatish Balay             col  = (col - 1)/bs;
35148e59246SSatish Balay #else
352a5eb4965SSatish Balay             col = (baij->colmap[in[j]] - 1)/bs;
35348e59246SSatish Balay #endif
354ab26458aSBarry Smith             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
355ab26458aSBarry Smith               ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
356ab26458aSBarry Smith               col =  in[j];
357ab26458aSBarry Smith             }
358ab26458aSBarry Smith           }
359ab26458aSBarry Smith           else col = in[j];
36097e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
361ab26458aSBarry Smith         }
362ab26458aSBarry Smith       }
363d64ed03dSBarry Smith     } else {
3644cb17eb5SBarry 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]);
365ab26458aSBarry Smith       if (!baij->donotstash) {
366ff2fd236SBarry Smith         if (roworiented) {
3676fa18ffdSBarry Smith           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
368ff2fd236SBarry Smith         } else {
3696fa18ffdSBarry Smith           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
370ff2fd236SBarry Smith         }
371abef11f7SSatish Balay       }
372ab26458aSBarry Smith     }
373ab26458aSBarry Smith   }
3743a40ed3dSBarry Smith   PetscFunctionReturn(0);
375ab26458aSBarry Smith }
3766fa18ffdSBarry Smith 
3770bdbc534SSatish Balay #define HASH_KEY 0.6180339887
378b24ad042SBarry Smith #define HASH(size,key,tmp) (tmp = (key)*HASH_KEY,(PetscInt)((size)*(tmp-(PetscInt)tmp)))
379b24ad042SBarry Smith /* #define HASH(size,key) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
380b24ad042SBarry Smith /* #define HASH(size,key,tmp) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
3814a2ae208SSatish Balay #undef __FUNCT__
38297e5c40aSBarry Smith #define __FUNCT__ "MatSetValues_MPIBAIJ_HT"
38397e5c40aSBarry Smith PetscErrorCode MatSetValues_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
3840bdbc534SSatish Balay {
3850bdbc534SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
386ace3abfcSBarry Smith   PetscBool      roworiented = baij->roworiented;
387dfbe8321SBarry Smith   PetscErrorCode ierr;
388b24ad042SBarry Smith   PetscInt       i,j,row,col;
389d0f46423SBarry Smith   PetscInt       rstart_orig=mat->rmap->rstart;
390d0f46423SBarry Smith   PetscInt       rend_orig=mat->rmap->rend,Nbs=baij->Nbs;
391d0f46423SBarry Smith   PetscInt       h1,key,size=baij->ht_size,bs=mat->rmap->bs,*HT=baij->ht,idx;
392329f5518SBarry Smith   PetscReal      tmp;
3933eda8832SBarry Smith   MatScalar      **HD = baij->hd,value;
3942515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
395b24ad042SBarry Smith   PetscInt       total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
3964a15367fSSatish Balay #endif
3970bdbc534SSatish Balay 
3980bdbc534SSatish Balay   PetscFunctionBegin;
39971fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
4000bdbc534SSatish Balay   for (i=0; i<m; i++) {
4012515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
402e32f2f54SBarry Smith     if (im[i] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
403e32f2f54SBarry Smith     if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1);
4040bdbc534SSatish Balay #endif
4050bdbc534SSatish Balay       row = im[i];
406c2760754SSatish Balay     if (row >= rstart_orig && row < rend_orig) {
4070bdbc534SSatish Balay       for (j=0; j<n; j++) {
4080bdbc534SSatish Balay         col = in[j];
4096fa18ffdSBarry Smith         if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
410b24ad042SBarry Smith         /* Look up PetscInto the Hash Table */
411c2760754SSatish Balay         key = (row/bs)*Nbs+(col/bs)+1;
412c2760754SSatish Balay         h1  = HASH(size,key,tmp);
4130bdbc534SSatish Balay 
414c2760754SSatish Balay 
415c2760754SSatish Balay         idx = h1;
4162515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
417187ce0cbSSatish Balay         insert_ct++;
418187ce0cbSSatish Balay         total_ct++;
419187ce0cbSSatish Balay         if (HT[idx] != key) {
420187ce0cbSSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++);
421187ce0cbSSatish Balay           if (idx == size) {
422187ce0cbSSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++);
423187ce0cbSSatish Balay             if (idx == h1) {
424e32f2f54SBarry Smith               SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
425187ce0cbSSatish Balay             }
426187ce0cbSSatish Balay           }
427187ce0cbSSatish Balay         }
428187ce0cbSSatish Balay #else
429c2760754SSatish Balay         if (HT[idx] != key) {
430c2760754SSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++);
431c2760754SSatish Balay           if (idx == size) {
432c2760754SSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++);
433c2760754SSatish Balay             if (idx == h1) {
434e32f2f54SBarry Smith               SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
435c2760754SSatish Balay             }
436c2760754SSatish Balay           }
437c2760754SSatish Balay         }
438187ce0cbSSatish Balay #endif
439c2760754SSatish Balay         /* A HASH table entry is found, so insert the values at the correct address */
440c2760754SSatish Balay         if (addv == ADD_VALUES) *(HD[idx]+ (col % bs)*bs + (row % bs)) += value;
441c2760754SSatish Balay         else                    *(HD[idx]+ (col % bs)*bs + (row % bs))  = value;
4420bdbc534SSatish Balay       }
4430bdbc534SSatish Balay     } else {
4440bdbc534SSatish Balay       if (!baij->donotstash) {
445ff2fd236SBarry Smith         if (roworiented) {
446b400d20cSBarry Smith           ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);CHKERRQ(ierr);
447ff2fd236SBarry Smith         } else {
448b400d20cSBarry Smith           ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);CHKERRQ(ierr);
4490bdbc534SSatish Balay         }
4500bdbc534SSatish Balay       }
4510bdbc534SSatish Balay     }
4520bdbc534SSatish Balay   }
4532515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
454187ce0cbSSatish Balay   baij->ht_total_ct = total_ct;
455187ce0cbSSatish Balay   baij->ht_insert_ct = insert_ct;
456187ce0cbSSatish Balay #endif
4570bdbc534SSatish Balay   PetscFunctionReturn(0);
4580bdbc534SSatish Balay }
4590bdbc534SSatish Balay 
4604a2ae208SSatish Balay #undef __FUNCT__
46197e5c40aSBarry Smith #define __FUNCT__ "MatSetValuesBlocked_MPIBAIJ_HT"
46297e5c40aSBarry Smith PetscErrorCode MatSetValuesBlocked_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
4630bdbc534SSatish Balay {
4640bdbc534SSatish Balay   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
465ace3abfcSBarry Smith   PetscBool         roworiented = baij->roworiented;
466dfbe8321SBarry Smith   PetscErrorCode    ierr;
467b24ad042SBarry Smith   PetscInt          i,j,ii,jj,row,col;
468899cda47SBarry Smith   PetscInt          rstart=baij->rstartbs;
469d0f46423SBarry Smith   PetscInt          rend=mat->rmap->rend,stepval,bs=mat->rmap->bs,bs2=baij->bs2,nbs2=n*bs2;
470b24ad042SBarry Smith   PetscInt          h1,key,size=baij->ht_size,idx,*HT=baij->ht,Nbs=baij->Nbs;
471329f5518SBarry Smith   PetscReal         tmp;
4723eda8832SBarry Smith   MatScalar         **HD = baij->hd,*baij_a;
473dd6ea824SBarry Smith   const PetscScalar *v_t,*value;
4742515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
475b24ad042SBarry Smith   PetscInt          total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
4764a15367fSSatish Balay #endif
4770bdbc534SSatish Balay 
478d0a41580SSatish Balay   PetscFunctionBegin;
479d0a41580SSatish Balay 
4800bdbc534SSatish Balay   if (roworiented) {
4810bdbc534SSatish Balay     stepval = (n-1)*bs;
4820bdbc534SSatish Balay   } else {
4830bdbc534SSatish Balay     stepval = (m-1)*bs;
4840bdbc534SSatish Balay   }
4850bdbc534SSatish Balay   for (i=0; i<m; i++) {
4862515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
487e32f2f54SBarry Smith     if (im[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",im[i]);
488e32f2f54SBarry Smith     if (im[i] >= baij->Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],baij->Mbs-1);
4890bdbc534SSatish Balay #endif
4900bdbc534SSatish Balay     row   = im[i];
491ab715e2cSSatish Balay     v_t   = v + i*nbs2;
492c2760754SSatish Balay     if (row >= rstart && row < rend) {
4930bdbc534SSatish Balay       for (j=0; j<n; j++) {
4940bdbc534SSatish Balay         col = in[j];
4950bdbc534SSatish Balay 
4960bdbc534SSatish Balay         /* Look up into the Hash Table */
497c2760754SSatish Balay         key = row*Nbs+col+1;
498c2760754SSatish Balay         h1  = HASH(size,key,tmp);
4990bdbc534SSatish Balay 
500c2760754SSatish Balay         idx = h1;
5012515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
502187ce0cbSSatish Balay         total_ct++;
503187ce0cbSSatish Balay         insert_ct++;
504187ce0cbSSatish Balay        if (HT[idx] != key) {
505187ce0cbSSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++);
506187ce0cbSSatish Balay           if (idx == size) {
507187ce0cbSSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++);
508187ce0cbSSatish Balay             if (idx == h1) {
509e32f2f54SBarry Smith               SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
510187ce0cbSSatish Balay             }
511187ce0cbSSatish Balay           }
512187ce0cbSSatish Balay         }
513187ce0cbSSatish Balay #else
514c2760754SSatish Balay         if (HT[idx] != key) {
515c2760754SSatish Balay           for (idx=h1; (idx<size) && (HT[idx]!=key); idx++);
516c2760754SSatish Balay           if (idx == size) {
517c2760754SSatish Balay             for (idx=0; (idx<h1) && (HT[idx]!=key); idx++);
518c2760754SSatish Balay             if (idx == h1) {
519e32f2f54SBarry Smith               SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
520c2760754SSatish Balay             }
521c2760754SSatish Balay           }
522c2760754SSatish Balay         }
523187ce0cbSSatish Balay #endif
524c2760754SSatish Balay         baij_a = HD[idx];
5250bdbc534SSatish Balay         if (roworiented) {
526c2760754SSatish Balay           /*value = v + i*(stepval+bs)*bs + j*bs;*/
527187ce0cbSSatish Balay           /* value = v + (i*(stepval+bs)+j)*bs; */
528187ce0cbSSatish Balay           value = v_t;
529187ce0cbSSatish Balay           v_t  += bs;
530fef45726SSatish Balay           if (addv == ADD_VALUES) {
531c2760754SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval) {
532c2760754SSatish Balay               for (jj=ii; jj<bs2; jj+=bs) {
533fef45726SSatish Balay                 baij_a[jj]  += *value++;
534b4cc0f5aSSatish Balay               }
535b4cc0f5aSSatish Balay             }
536fef45726SSatish Balay           } else {
537c2760754SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval) {
538c2760754SSatish Balay               for (jj=ii; jj<bs2; jj+=bs) {
539fef45726SSatish Balay                 baij_a[jj]  = *value++;
540fef45726SSatish Balay               }
541fef45726SSatish Balay             }
542fef45726SSatish Balay           }
5430bdbc534SSatish Balay         } else {
5440bdbc534SSatish Balay           value = v + j*(stepval+bs)*bs + i*bs;
545fef45726SSatish Balay           if (addv == ADD_VALUES) {
546b4cc0f5aSSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
5470bdbc534SSatish Balay               for (jj=0; jj<bs; jj++) {
548fef45726SSatish Balay                 baij_a[jj]  += *value++;
549fef45726SSatish Balay               }
550fef45726SSatish Balay             }
551fef45726SSatish Balay           } else {
552fef45726SSatish Balay             for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
553fef45726SSatish Balay               for (jj=0; jj<bs; jj++) {
554fef45726SSatish Balay                 baij_a[jj]  = *value++;
555fef45726SSatish Balay               }
556b4cc0f5aSSatish Balay             }
5570bdbc534SSatish Balay           }
5580bdbc534SSatish Balay         }
5590bdbc534SSatish Balay       }
5600bdbc534SSatish Balay     } else {
5610bdbc534SSatish Balay       if (!baij->donotstash) {
5620bdbc534SSatish Balay         if (roworiented) {
5638798bf22SSatish Balay           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
5640bdbc534SSatish Balay         } else {
5658798bf22SSatish Balay           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
5660bdbc534SSatish Balay         }
5670bdbc534SSatish Balay       }
5680bdbc534SSatish Balay     }
5690bdbc534SSatish Balay   }
5702515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
571187ce0cbSSatish Balay   baij->ht_total_ct = total_ct;
572187ce0cbSSatish Balay   baij->ht_insert_ct = insert_ct;
573187ce0cbSSatish Balay #endif
5740bdbc534SSatish Balay   PetscFunctionReturn(0);
5750bdbc534SSatish Balay }
576133cdb44SSatish Balay 
5774a2ae208SSatish Balay #undef __FUNCT__
5784a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIBAIJ"
579b24ad042SBarry Smith PetscErrorCode MatGetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
580d6de1c52SSatish Balay {
581d6de1c52SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
5826849ba73SBarry Smith   PetscErrorCode ierr;
583d0f46423SBarry Smith   PetscInt       bs=mat->rmap->bs,i,j,bsrstart = mat->rmap->rstart,bsrend = mat->rmap->rend;
584d0f46423SBarry Smith   PetscInt       bscstart = mat->cmap->rstart,bscend = mat->cmap->rend,row,col,data;
585d6de1c52SSatish Balay 
586133cdb44SSatish Balay   PetscFunctionBegin;
587d6de1c52SSatish Balay   for (i=0; i<m; i++) {
588e32f2f54SBarry Smith     if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
589e32f2f54SBarry Smith     if (idxm[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",idxm[i],mat->rmap->N-1);
590d6de1c52SSatish Balay     if (idxm[i] >= bsrstart && idxm[i] < bsrend) {
591d6de1c52SSatish Balay       row = idxm[i] - bsrstart;
592d6de1c52SSatish Balay       for (j=0; j<n; j++) {
593e32f2f54SBarry Smith         if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
594e32f2f54SBarry Smith         if (idxn[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",idxn[j],mat->cmap->N-1);
595d6de1c52SSatish Balay         if (idxn[j] >= bscstart && idxn[j] < bscend){
596d6de1c52SSatish Balay           col = idxn[j] - bscstart;
59798dd23e9SBarry Smith           ierr = MatGetValues_SeqBAIJ(baij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
598d64ed03dSBarry Smith         } else {
599905e6a2fSBarry Smith           if (!baij->colmap) {
600905e6a2fSBarry Smith             ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
601905e6a2fSBarry Smith           }
602aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
6030f5bd95cSBarry Smith           ierr = PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);CHKERRQ(ierr);
604fa46199cSSatish Balay           data --;
60548e59246SSatish Balay #else
60648e59246SSatish Balay           data = baij->colmap[idxn[j]/bs]-1;
60748e59246SSatish Balay #endif
60848e59246SSatish Balay           if((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0;
609d9d09a02SSatish Balay           else {
61048e59246SSatish Balay             col  = data + idxn[j]%bs;
61198dd23e9SBarry Smith             ierr = MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr);
612d6de1c52SSatish Balay           }
613d6de1c52SSatish Balay         }
614d6de1c52SSatish Balay       }
615d64ed03dSBarry Smith     } else {
616e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
617d6de1c52SSatish Balay     }
618d6de1c52SSatish Balay   }
6193a40ed3dSBarry Smith  PetscFunctionReturn(0);
620d6de1c52SSatish Balay }
621d6de1c52SSatish Balay 
6224a2ae208SSatish Balay #undef __FUNCT__
6234a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIBAIJ"
624dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIBAIJ(Mat mat,NormType type,PetscReal *nrm)
625d6de1c52SSatish Balay {
626d6de1c52SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
627d6de1c52SSatish Balay   Mat_SeqBAIJ    *amat = (Mat_SeqBAIJ*)baij->A->data,*bmat = (Mat_SeqBAIJ*)baij->B->data;
628dfbe8321SBarry Smith   PetscErrorCode ierr;
629d0f46423SBarry Smith   PetscInt       i,j,bs2=baij->bs2,bs=baij->A->rmap->bs,nz,row,col;
630329f5518SBarry Smith   PetscReal      sum = 0.0;
6313eda8832SBarry Smith   MatScalar      *v;
632d6de1c52SSatish Balay 
633d64ed03dSBarry Smith   PetscFunctionBegin;
634d6de1c52SSatish Balay   if (baij->size == 1) {
635064f8208SBarry Smith     ierr =  MatNorm(baij->A,type,nrm);CHKERRQ(ierr);
636d6de1c52SSatish Balay   } else {
637d6de1c52SSatish Balay     if (type == NORM_FROBENIUS) {
638d6de1c52SSatish Balay       v = amat->a;
6398a62d963SHong Zhang       nz = amat->nz*bs2;
6408a62d963SHong Zhang       for (i=0; i<nz; i++) {
641aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
642329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
643d6de1c52SSatish Balay #else
644d6de1c52SSatish Balay         sum += (*v)*(*v); v++;
645d6de1c52SSatish Balay #endif
646d6de1c52SSatish Balay       }
647d6de1c52SSatish Balay       v = bmat->a;
6488a62d963SHong Zhang       nz = bmat->nz*bs2;
6498a62d963SHong Zhang       for (i=0; i<nz; i++) {
650aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
651329f5518SBarry Smith         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
652d6de1c52SSatish Balay #else
653d6de1c52SSatish Balay         sum += (*v)*(*v); v++;
654d6de1c52SSatish Balay #endif
655d6de1c52SSatish Balay       }
656d9822059SBarry Smith       ierr = MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr);
6578f1a2a5eSBarry Smith       *nrm = PetscSqrtReal(*nrm);
6588a62d963SHong Zhang     } else if (type == NORM_1) { /* max column sum */
6598a62d963SHong Zhang       PetscReal *tmp,*tmp2;
660899cda47SBarry Smith       PetscInt  *jj,*garray=baij->garray,cstart=baij->rstartbs;
661fca92195SBarry Smith       ierr = PetscMalloc2(mat->cmap->N,PetscReal,&tmp,mat->cmap->N,PetscReal,&tmp2);CHKERRQ(ierr);
662d0f46423SBarry Smith       ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr);
6638a62d963SHong Zhang       v = amat->a; jj = amat->j;
6648a62d963SHong Zhang       for (i=0; i<amat->nz; i++) {
6658a62d963SHong Zhang         for (j=0; j<bs; j++){
6668a62d963SHong Zhang           col = bs*(cstart + *jj) + j; /* column index */
6678a62d963SHong Zhang           for (row=0; row<bs; row++){
6688a62d963SHong Zhang             tmp[col] += PetscAbsScalar(*v);  v++;
6698a62d963SHong Zhang           }
6708a62d963SHong Zhang         }
6718a62d963SHong Zhang         jj++;
6728a62d963SHong Zhang       }
6738a62d963SHong Zhang       v = bmat->a; jj = bmat->j;
6748a62d963SHong Zhang       for (i=0; i<bmat->nz; i++) {
6758a62d963SHong Zhang         for (j=0; j<bs; j++){
6768a62d963SHong Zhang           col = bs*garray[*jj] + j;
6778a62d963SHong Zhang           for (row=0; row<bs; row++){
6788a62d963SHong Zhang             tmp[col] += PetscAbsScalar(*v); v++;
6798a62d963SHong Zhang           }
6808a62d963SHong Zhang         }
6818a62d963SHong Zhang         jj++;
6828a62d963SHong Zhang       }
683d9822059SBarry Smith       ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr);
6848a62d963SHong Zhang       *nrm = 0.0;
685d0f46423SBarry Smith       for (j=0; j<mat->cmap->N; j++) {
6868a62d963SHong Zhang         if (tmp2[j] > *nrm) *nrm = tmp2[j];
6878a62d963SHong Zhang       }
688fca92195SBarry Smith       ierr = PetscFree2(tmp,tmp2);CHKERRQ(ierr);
6898a62d963SHong Zhang     } else if (type == NORM_INFINITY) { /* max row sum */
690577dd1f9SKris Buschelman       PetscReal *sums;
691cb9801acSJed Brown       ierr = PetscMalloc(bs*sizeof(PetscReal),&sums);CHKERRQ(ierr);
6928a62d963SHong Zhang       sum = 0.0;
6938a62d963SHong Zhang       for (j=0; j<amat->mbs; j++) {
6948a62d963SHong Zhang         for (row=0; row<bs; row++) sums[row] = 0.0;
6958a62d963SHong Zhang         v = amat->a + bs2*amat->i[j];
6968a62d963SHong Zhang         nz = amat->i[j+1]-amat->i[j];
6978a62d963SHong Zhang         for (i=0; i<nz; i++) {
6988a62d963SHong Zhang           for (col=0; col<bs; col++){
6998a62d963SHong Zhang             for (row=0; row<bs; row++){
7008a62d963SHong Zhang               sums[row] += PetscAbsScalar(*v); v++;
7018a62d963SHong Zhang             }
7028a62d963SHong Zhang           }
7038a62d963SHong Zhang         }
7048a62d963SHong Zhang         v = bmat->a + bs2*bmat->i[j];
7058a62d963SHong Zhang         nz = bmat->i[j+1]-bmat->i[j];
7068a62d963SHong Zhang         for (i=0; i<nz; i++) {
7078a62d963SHong Zhang           for (col=0; col<bs; col++){
7088a62d963SHong Zhang             for (row=0; row<bs; row++){
7098a62d963SHong Zhang               sums[row] += PetscAbsScalar(*v); v++;
7108a62d963SHong Zhang             }
7118a62d963SHong Zhang           }
7128a62d963SHong Zhang         }
7138a62d963SHong Zhang         for (row=0; row<bs; row++){
7148a62d963SHong Zhang           if (sums[row] > sum) sum = sums[row];
7158a62d963SHong Zhang         }
7168a62d963SHong Zhang       }
717d9822059SBarry Smith       ierr = MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_MAX,((PetscObject)mat)->comm);CHKERRQ(ierr);
718577dd1f9SKris Buschelman       ierr = PetscFree(sums);CHKERRQ(ierr);
719e7e72b3dSBarry Smith     } else SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"No support for this norm yet");
720d64ed03dSBarry Smith   }
7213a40ed3dSBarry Smith   PetscFunctionReturn(0);
722d6de1c52SSatish Balay }
72357b952d6SSatish Balay 
724fef45726SSatish Balay /*
725fef45726SSatish Balay   Creates the hash table, and sets the table
726fef45726SSatish Balay   This table is created only once.
727fef45726SSatish Balay   If new entried need to be added to the matrix
728fef45726SSatish Balay   then the hash table has to be destroyed and
729fef45726SSatish Balay   recreated.
730fef45726SSatish Balay */
7314a2ae208SSatish Balay #undef __FUNCT__
7324a2ae208SSatish Balay #define __FUNCT__ "MatCreateHashTable_MPIBAIJ_Private"
733dfbe8321SBarry Smith PetscErrorCode MatCreateHashTable_MPIBAIJ_Private(Mat mat,PetscReal factor)
734596b8d2eSBarry Smith {
735596b8d2eSBarry Smith   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
736596b8d2eSBarry Smith   Mat            A = baij->A,B=baij->B;
737596b8d2eSBarry Smith   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ *)A->data,*b=(Mat_SeqBAIJ *)B->data;
738b24ad042SBarry Smith   PetscInt       i,j,k,nz=a->nz+b->nz,h1,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
7396849ba73SBarry Smith   PetscErrorCode ierr;
740fca92195SBarry Smith   PetscInt       ht_size,bs2=baij->bs2,rstart=baij->rstartbs;
741899cda47SBarry Smith   PetscInt       cstart=baij->cstartbs,*garray=baij->garray,row,col,Nbs=baij->Nbs;
742b24ad042SBarry Smith   PetscInt       *HT,key;
7433eda8832SBarry Smith   MatScalar      **HD;
744329f5518SBarry Smith   PetscReal      tmp;
7456cf91177SBarry Smith #if defined(PETSC_USE_INFO)
746b24ad042SBarry Smith   PetscInt       ct=0,max=0;
7474a15367fSSatish Balay #endif
748fef45726SSatish Balay 
749d64ed03dSBarry Smith   PetscFunctionBegin;
750fca92195SBarry Smith   if (baij->ht) PetscFunctionReturn(0);
751fef45726SSatish Balay 
752fca92195SBarry Smith   baij->ht_size = (PetscInt)(factor*nz);
753fca92195SBarry Smith   ht_size       = baij->ht_size;
7540bdbc534SSatish Balay 
755fef45726SSatish Balay   /* Allocate Memory for Hash Table */
756fca92195SBarry Smith   ierr = PetscMalloc2(ht_size,MatScalar*,&baij->hd,ht_size,PetscInt,&baij->ht);CHKERRQ(ierr);
757fca92195SBarry Smith   ierr = PetscMemzero(baij->hd,ht_size*sizeof(MatScalar*));CHKERRQ(ierr);
758fca92195SBarry Smith   ierr = PetscMemzero(baij->ht,ht_size*sizeof(PetscInt));CHKERRQ(ierr);
759b9e4cc15SSatish Balay   HD   = baij->hd;
760a07cd24cSSatish Balay   HT   = baij->ht;
761b9e4cc15SSatish Balay 
762596b8d2eSBarry Smith   /* Loop Over A */
7630bdbc534SSatish Balay   for (i=0; i<a->mbs; i++) {
764596b8d2eSBarry Smith     for (j=ai[i]; j<ai[i+1]; j++) {
7650bdbc534SSatish Balay       row = i+rstart;
7660bdbc534SSatish Balay       col = aj[j]+cstart;
767596b8d2eSBarry Smith 
768187ce0cbSSatish Balay       key = row*Nbs + col + 1;
769fca92195SBarry Smith       h1  = HASH(ht_size,key,tmp);
770fca92195SBarry Smith       for (k=0; k<ht_size; k++){
771fca92195SBarry Smith         if (!HT[(h1+k)%ht_size]) {
772fca92195SBarry Smith           HT[(h1+k)%ht_size] = key;
773fca92195SBarry Smith           HD[(h1+k)%ht_size] = a->a + j*bs2;
774596b8d2eSBarry Smith           break;
7756cf91177SBarry Smith #if defined(PETSC_USE_INFO)
776187ce0cbSSatish Balay         } else {
777187ce0cbSSatish Balay           ct++;
778187ce0cbSSatish Balay #endif
779596b8d2eSBarry Smith         }
780187ce0cbSSatish Balay       }
7816cf91177SBarry Smith #if defined(PETSC_USE_INFO)
782187ce0cbSSatish Balay       if (k> max) max = k;
783187ce0cbSSatish Balay #endif
784596b8d2eSBarry Smith     }
785596b8d2eSBarry Smith   }
786596b8d2eSBarry Smith   /* Loop Over B */
7870bdbc534SSatish Balay   for (i=0; i<b->mbs; i++) {
788596b8d2eSBarry Smith     for (j=bi[i]; j<bi[i+1]; j++) {
7890bdbc534SSatish Balay       row = i+rstart;
7900bdbc534SSatish Balay       col = garray[bj[j]];
791187ce0cbSSatish Balay       key = row*Nbs + col + 1;
792fca92195SBarry Smith       h1  = HASH(ht_size,key,tmp);
793fca92195SBarry Smith       for (k=0; k<ht_size; k++){
794fca92195SBarry Smith         if (!HT[(h1+k)%ht_size]) {
795fca92195SBarry Smith           HT[(h1+k)%ht_size] = key;
796fca92195SBarry Smith           HD[(h1+k)%ht_size] = b->a + j*bs2;
797596b8d2eSBarry Smith           break;
7986cf91177SBarry Smith #if defined(PETSC_USE_INFO)
799187ce0cbSSatish Balay         } else {
800187ce0cbSSatish Balay           ct++;
801187ce0cbSSatish Balay #endif
802596b8d2eSBarry Smith         }
803187ce0cbSSatish Balay       }
8046cf91177SBarry Smith #if defined(PETSC_USE_INFO)
805187ce0cbSSatish Balay       if (k> max) max = k;
806187ce0cbSSatish Balay #endif
807596b8d2eSBarry Smith     }
808596b8d2eSBarry Smith   }
809596b8d2eSBarry Smith 
810596b8d2eSBarry Smith   /* Print Summary */
8116cf91177SBarry Smith #if defined(PETSC_USE_INFO)
812fca92195SBarry Smith   for (i=0,j=0; i<ht_size; i++) {
813596b8d2eSBarry Smith     if (HT[i]) {j++;}
814c38d4ed2SBarry Smith   }
8151e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Average Search = %5.2f,max search = %D\n",(!j)? 0.0:((PetscReal)(ct+j))/j,max);CHKERRQ(ierr);
816187ce0cbSSatish Balay #endif
8173a40ed3dSBarry Smith   PetscFunctionReturn(0);
818596b8d2eSBarry Smith }
81957b952d6SSatish Balay 
8204a2ae208SSatish Balay #undef __FUNCT__
8214a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIBAIJ"
822dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIBAIJ(Mat mat,MatAssemblyType mode)
823bbb85fb3SSatish Balay {
824bbb85fb3SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
825dfbe8321SBarry Smith   PetscErrorCode ierr;
826b24ad042SBarry Smith   PetscInt       nstash,reallocs;
827bbb85fb3SSatish Balay   InsertMode     addv;
828bbb85fb3SSatish Balay 
829bbb85fb3SSatish Balay   PetscFunctionBegin;
8304cb17eb5SBarry Smith   if (baij->donotstash || mat->nooffprocentries) {
831bbb85fb3SSatish Balay     PetscFunctionReturn(0);
832bbb85fb3SSatish Balay   }
833bbb85fb3SSatish Balay 
834bbb85fb3SSatish Balay   /* make sure all processors are either in INSERTMODE or ADDMODE */
8357adad957SLisandro Dalcin   ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,((PetscObject)mat)->comm);CHKERRQ(ierr);
836e7e72b3dSBarry Smith   if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
837bbb85fb3SSatish Balay   mat->insertmode = addv; /* in case this processor had no cache */
838bbb85fb3SSatish Balay 
839d0f46423SBarry Smith   ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr);
8401e2582c4SBarry Smith   ierr = MatStashScatterBegin_Private(mat,&mat->bstash,baij->rangebs);CHKERRQ(ierr);
8418798bf22SSatish Balay   ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr);
8421e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Stash has %D entries,uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
84346680499SSatish Balay   ierr = MatStashGetInfo_Private(&mat->bstash,&nstash,&reallocs);CHKERRQ(ierr);
8441e2582c4SBarry Smith   ierr = PetscInfo2(mat,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr);
845bbb85fb3SSatish Balay   PetscFunctionReturn(0);
846bbb85fb3SSatish Balay }
847bbb85fb3SSatish Balay 
8484a2ae208SSatish Balay #undef __FUNCT__
8494a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIBAIJ"
850dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIBAIJ(Mat mat,MatAssemblyType mode)
851bbb85fb3SSatish Balay {
852bbb85fb3SSatish Balay   Mat_MPIBAIJ    *baij=(Mat_MPIBAIJ*)mat->data;
85391c97fd4SSatish Balay   Mat_SeqBAIJ    *a=(Mat_SeqBAIJ*)baij->A->data;
8546849ba73SBarry Smith   PetscErrorCode ierr;
855b24ad042SBarry Smith   PetscInt       i,j,rstart,ncols,flg,bs2=baij->bs2;
856e44c0bd4SBarry Smith   PetscInt       *row,*col;
857ace3abfcSBarry Smith   PetscBool      r1,r2,r3,other_disassembled;
8583eda8832SBarry Smith   MatScalar      *val;
859bbb85fb3SSatish Balay   InsertMode     addv = mat->insertmode;
860b24ad042SBarry Smith   PetscMPIInt    n;
861bbb85fb3SSatish Balay 
86291c97fd4SSatish Balay   /* do not use 'b=(Mat_SeqBAIJ*)baij->B->data' as B can be reset in disassembly */
863bbb85fb3SSatish Balay   PetscFunctionBegin;
8644cb17eb5SBarry Smith   if (!baij->donotstash && !mat->nooffprocentries) {
865a2d1c673SSatish Balay     while (1) {
8668798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
867a2d1c673SSatish Balay       if (!flg) break;
868a2d1c673SSatish Balay 
869bbb85fb3SSatish Balay       for (i=0; i<n;) {
870bbb85fb3SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
871bbb85fb3SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
872bbb85fb3SSatish Balay         if (j < n) ncols = j-i;
873bbb85fb3SSatish Balay         else       ncols = n-i;
874bbb85fb3SSatish Balay         /* Now assemble all these values with a single function call */
87597e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr);
876bbb85fb3SSatish Balay         i = j;
877bbb85fb3SSatish Balay       }
878bbb85fb3SSatish Balay     }
8798798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr);
880a2d1c673SSatish Balay     /* Now process the block-stash. Since the values are stashed column-oriented,
881a2d1c673SSatish Balay        set the roworiented flag to column oriented, and after MatSetValues()
882a2d1c673SSatish Balay        restore the original flags */
883a2d1c673SSatish Balay     r1 = baij->roworiented;
884a2d1c673SSatish Balay     r2 = a->roworiented;
88591c97fd4SSatish Balay     r3 = ((Mat_SeqBAIJ*)baij->B->data)->roworiented;
8867c922b88SBarry Smith     baij->roworiented = PETSC_FALSE;
8877c922b88SBarry Smith     a->roworiented    = PETSC_FALSE;
88891c97fd4SSatish Balay     (((Mat_SeqBAIJ*)baij->B->data))->roworiented    = PETSC_FALSE; /* b->roworiented */
889a2d1c673SSatish Balay     while (1) {
8908798bf22SSatish Balay       ierr = MatStashScatterGetMesg_Private(&mat->bstash,&n,&row,&col,&val,&flg);CHKERRQ(ierr);
891a2d1c673SSatish Balay       if (!flg) break;
892a2d1c673SSatish Balay 
893a2d1c673SSatish Balay       for (i=0; i<n;) {
894a2d1c673SSatish Balay         /* Now identify the consecutive vals belonging to the same row */
895a2d1c673SSatish Balay         for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
896a2d1c673SSatish Balay         if (j < n) ncols = j-i;
897a2d1c673SSatish Balay         else       ncols = n-i;
89897e5c40aSBarry Smith         ierr = MatSetValuesBlocked_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i*bs2,addv);CHKERRQ(ierr);
899a2d1c673SSatish Balay         i = j;
900a2d1c673SSatish Balay       }
901a2d1c673SSatish Balay     }
9028798bf22SSatish Balay     ierr = MatStashScatterEnd_Private(&mat->bstash);CHKERRQ(ierr);
903a2d1c673SSatish Balay     baij->roworiented = r1;
904a2d1c673SSatish Balay     a->roworiented    = r2;
90591c97fd4SSatish Balay     ((Mat_SeqBAIJ*)baij->B->data)->roworiented    = r3; /* b->roworiented */
906bbb85fb3SSatish Balay   }
907bbb85fb3SSatish Balay 
908bbb85fb3SSatish Balay   ierr = MatAssemblyBegin(baij->A,mode);CHKERRQ(ierr);
909bbb85fb3SSatish Balay   ierr = MatAssemblyEnd(baij->A,mode);CHKERRQ(ierr);
910bbb85fb3SSatish Balay 
911bbb85fb3SSatish Balay   /* determine if any processor has disassembled, if so we must
912bbb85fb3SSatish Balay      also disassemble ourselfs, in order that we may reassemble. */
913bbb85fb3SSatish Balay   /*
914bbb85fb3SSatish Balay      if nonzero structure of submatrix B cannot change then we know that
915bbb85fb3SSatish Balay      no processor disassembled thus we can skip this stuff
916bbb85fb3SSatish Balay   */
917bbb85fb3SSatish Balay   if (!((Mat_SeqBAIJ*)baij->B->data)->nonew)  {
9187adad957SLisandro Dalcin     ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,((PetscObject)mat)->comm);CHKERRQ(ierr);
919bbb85fb3SSatish Balay     if (mat->was_assembled && !other_disassembled) {
920bbb85fb3SSatish Balay       ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
921bbb85fb3SSatish Balay     }
922bbb85fb3SSatish Balay   }
923bbb85fb3SSatish Balay 
924bbb85fb3SSatish Balay   if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
925bbb85fb3SSatish Balay     ierr = MatSetUpMultiply_MPIBAIJ(mat);CHKERRQ(ierr);
926bbb85fb3SSatish Balay   }
9274e35b6f3SSatish Balay   ierr = MatSetOption(baij->B,MAT_CHECK_COMPRESSED_ROW,PETSC_FALSE);CHKERRQ(ierr);
928bbb85fb3SSatish Balay   ierr = MatAssemblyBegin(baij->B,mode);CHKERRQ(ierr);
929bbb85fb3SSatish Balay   ierr = MatAssemblyEnd(baij->B,mode);CHKERRQ(ierr);
930bbb85fb3SSatish Balay 
9316cf91177SBarry Smith #if defined(PETSC_USE_INFO)
932bbb85fb3SSatish Balay   if (baij->ht && mode== MAT_FINAL_ASSEMBLY) {
9331e2582c4SBarry Smith     ierr = PetscInfo1(mat,"Average Hash Table Search in MatSetValues = %5.2f\n",((PetscReal)baij->ht_total_ct)/baij->ht_insert_ct);CHKERRQ(ierr);
934bbb85fb3SSatish Balay     baij->ht_total_ct  = 0;
935bbb85fb3SSatish Balay     baij->ht_insert_ct = 0;
936bbb85fb3SSatish Balay   }
937bbb85fb3SSatish Balay #endif
938bbb85fb3SSatish Balay   if (baij->ht_flag && !baij->ht && mode == MAT_FINAL_ASSEMBLY) {
939bbb85fb3SSatish Balay     ierr = MatCreateHashTable_MPIBAIJ_Private(mat,baij->ht_fact);CHKERRQ(ierr);
940bbb85fb3SSatish Balay     mat->ops->setvalues        = MatSetValues_MPIBAIJ_HT;
941bbb85fb3SSatish Balay     mat->ops->setvaluesblocked = MatSetValuesBlocked_MPIBAIJ_HT;
942bbb85fb3SSatish Balay   }
943bbb85fb3SSatish Balay 
944fca92195SBarry Smith   ierr = PetscFree2(baij->rowvalues,baij->rowindices);CHKERRQ(ierr);
945606d414cSSatish Balay   baij->rowvalues = 0;
946bbb85fb3SSatish Balay   PetscFunctionReturn(0);
947bbb85fb3SSatish Balay }
94857b952d6SSatish Balay 
9494a2ae208SSatish Balay #undef __FUNCT__
9504a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIBAIJ_ASCIIorDraworSocket"
9516849ba73SBarry Smith static PetscErrorCode MatView_MPIBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
95257b952d6SSatish Balay {
95357b952d6SSatish Balay   Mat_MPIBAIJ       *baij = (Mat_MPIBAIJ*)mat->data;
954dfbe8321SBarry Smith   PetscErrorCode    ierr;
955b24ad042SBarry Smith   PetscMPIInt       size = baij->size,rank = baij->rank;
956d0f46423SBarry Smith   PetscInt          bs = mat->rmap->bs;
957ace3abfcSBarry Smith   PetscBool         iascii,isdraw;
958b0a32e0cSBarry Smith   PetscViewer       sviewer;
959f3ef73ceSBarry Smith   PetscViewerFormat format;
96057b952d6SSatish Balay 
961d64ed03dSBarry Smith   PetscFunctionBegin;
9622692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
9632692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
96432077d6dSBarry Smith   if (iascii) {
965b0a32e0cSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
966456192e2SBarry Smith     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
9674e220ebcSLois Curfman McInnes       MatInfo info;
9687adad957SLisandro Dalcin       ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr);
969d41123aaSBarry Smith       ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr);
9707b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr);
97177431f27SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D bs %D mem %D\n",
97216608c43SJed Brown              rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,mat->rmap->bs,(PetscInt)info.memory);CHKERRQ(ierr);
973d132466eSBarry Smith       ierr = MatGetInfo(baij->A,MAT_LOCAL,&info);CHKERRQ(ierr);
974e6dd01d4SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
975d132466eSBarry Smith       ierr = MatGetInfo(baij->B,MAT_LOCAL,&info);CHKERRQ(ierr);
976e6dd01d4SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr);
977b0a32e0cSBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
9787b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr);
97907d81ca4SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr);
98057b952d6SSatish Balay       ierr = VecScatterView(baij->Mvctx,viewer);CHKERRQ(ierr);
9813a40ed3dSBarry Smith       PetscFunctionReturn(0);
982fb9695e5SSatish Balay     } else if (format == PETSC_VIEWER_ASCII_INFO) {
98377431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);CHKERRQ(ierr);
9843a40ed3dSBarry Smith       PetscFunctionReturn(0);
98504929863SHong Zhang     } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
98604929863SHong Zhang       PetscFunctionReturn(0);
98757b952d6SSatish Balay     }
98857b952d6SSatish Balay   }
98957b952d6SSatish Balay 
9900f5bd95cSBarry Smith   if (isdraw) {
991b0a32e0cSBarry Smith     PetscDraw       draw;
992ace3abfcSBarry Smith     PetscBool  isnull;
993b0a32e0cSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
994b0a32e0cSBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
99557b952d6SSatish Balay   }
99657b952d6SSatish Balay 
99757b952d6SSatish Balay   if (size == 1) {
9987adad957SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)baij->A,((PetscObject)mat)->name);CHKERRQ(ierr);
99957b952d6SSatish Balay     ierr = MatView(baij->A,viewer);CHKERRQ(ierr);
1000d64ed03dSBarry Smith   } else {
100157b952d6SSatish Balay     /* assemble the entire matrix onto first processor. */
100257b952d6SSatish Balay     Mat         A;
100357b952d6SSatish Balay     Mat_SeqBAIJ *Aloc;
1004d0f46423SBarry Smith     PetscInt    M = mat->rmap->N,N = mat->cmap->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs;
10053eda8832SBarry Smith     MatScalar   *a;
100657b952d6SSatish Balay 
1007f204ca49SKris Buschelman     /* Here we are creating a temporary matrix, so will assume MPIBAIJ is acceptable */
1008f204ca49SKris Buschelman     /* Perhaps this should be the type of mat? */
10097adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)mat)->comm,&A);CHKERRQ(ierr);
101057b952d6SSatish Balay     if (!rank) {
1011f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr);
1012d64ed03dSBarry Smith     } else {
1013f69a0ea3SMatthew Knepley       ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr);
101457b952d6SSatish Balay     }
1015f204ca49SKris Buschelman     ierr = MatSetType(A,MATMPIBAIJ);CHKERRQ(ierr);
1016d0f46423SBarry Smith     ierr = MatMPIBAIJSetPreallocation(A,mat->rmap->bs,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
10172b82e772SSatish Balay     ierr = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);
101852e6d16bSBarry Smith     ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr);
101957b952d6SSatish Balay 
102057b952d6SSatish Balay     /* copy over the A part */
102157b952d6SSatish Balay     Aloc = (Mat_SeqBAIJ*)baij->A->data;
102257b952d6SSatish Balay     ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1023b24ad042SBarry Smith     ierr = PetscMalloc(bs*sizeof(PetscInt),&rvals);CHKERRQ(ierr);
102457b952d6SSatish Balay 
102557b952d6SSatish Balay     for (i=0; i<mbs; i++) {
1026899cda47SBarry Smith       rvals[0] = bs*(baij->rstartbs + i);
102757b952d6SSatish Balay       for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
102857b952d6SSatish Balay       for (j=ai[i]; j<ai[i+1]; j++) {
1029899cda47SBarry Smith         col = (baij->cstartbs+aj[j])*bs;
103057b952d6SSatish Balay         for (k=0; k<bs; k++) {
103197e5c40aSBarry Smith           ierr = MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);CHKERRQ(ierr);
1032cee3aa6bSSatish Balay           col++; a += bs;
103357b952d6SSatish Balay         }
103457b952d6SSatish Balay       }
103557b952d6SSatish Balay     }
103657b952d6SSatish Balay     /* copy over the B part */
103757b952d6SSatish Balay     Aloc = (Mat_SeqBAIJ*)baij->B->data;
103857b952d6SSatish Balay     ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
103957b952d6SSatish Balay     for (i=0; i<mbs; i++) {
1040899cda47SBarry Smith       rvals[0] = bs*(baij->rstartbs + i);
104157b952d6SSatish Balay       for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
104257b952d6SSatish Balay       for (j=ai[i]; j<ai[i+1]; j++) {
104357b952d6SSatish Balay         col = baij->garray[aj[j]]*bs;
104457b952d6SSatish Balay         for (k=0; k<bs; k++) {
104597e5c40aSBarry Smith           ierr = MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);CHKERRQ(ierr);
1046cee3aa6bSSatish Balay           col++; a += bs;
104757b952d6SSatish Balay         }
104857b952d6SSatish Balay       }
104957b952d6SSatish Balay     }
1050606d414cSSatish Balay     ierr = PetscFree(rvals);CHKERRQ(ierr);
10516d4a8577SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10526d4a8577SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
105355843e3eSBarry Smith     /*
105455843e3eSBarry Smith        Everyone has to call to draw the matrix since the graphics waits are
1055b0a32e0cSBarry Smith        synchronized across all processors that share the PetscDraw object
105655843e3eSBarry Smith     */
1057b0a32e0cSBarry Smith     ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr);
1058f14a1c24SBarry Smith     if (!rank) {
10597adad957SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject)((Mat_MPIBAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr);
10607566de4bSShri Abhyankar     /* Set the type name to MATMPIBAIJ so that the correct type can be printed out by PetscObjectPrintClassNamePrefixType() in MatView_SeqBAIJ_ASCII()*/
10617566de4bSShri Abhyankar       PetscStrcpy(((PetscObject)((Mat_MPIBAIJ*)(A->data))->A)->type_name,MATMPIBAIJ);
10626831982aSBarry Smith       ierr = MatView(((Mat_MPIBAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr);
106357b952d6SSatish Balay     }
1064b0a32e0cSBarry Smith     ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr);
10656bf464f9SBarry Smith     ierr = MatDestroy(&A);CHKERRQ(ierr);
106657b952d6SSatish Balay   }
10673a40ed3dSBarry Smith   PetscFunctionReturn(0);
106857b952d6SSatish Balay }
106957b952d6SSatish Balay 
10704a2ae208SSatish Balay #undef __FUNCT__
1071660746e0SBarry Smith #define __FUNCT__ "MatView_MPIBAIJ_Binary"
1072660746e0SBarry Smith static PetscErrorCode MatView_MPIBAIJ_Binary(Mat mat,PetscViewer viewer)
1073660746e0SBarry Smith {
1074660746e0SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)mat->data;
1075660746e0SBarry Smith   Mat_SeqBAIJ*   A = (Mat_SeqBAIJ*)a->A->data;
1076660746e0SBarry Smith   Mat_SeqBAIJ*   B = (Mat_SeqBAIJ*)a->B->data;
1077660746e0SBarry Smith   PetscErrorCode ierr;
10785f48b12bSBarry Smith   PetscInt       i,*row_lens,*crow_lens,bs = mat->rmap->bs,j,k,bs2=a->bs2,header[4],nz,rlen;
1079e96a6426SSatish Balay   PetscInt       *range=0,nzmax,*column_indices,cnt,col,*garray = a->garray,cstart = mat->cmap->rstart/bs,len,pcnt,l,ll;
1080660746e0SBarry Smith   int            fd;
1081660746e0SBarry Smith   PetscScalar    *column_values;
1082660746e0SBarry Smith   FILE           *file;
1083660746e0SBarry Smith   PetscMPIInt    rank,size,tag = ((PetscObject)viewer)->tag;
1084638eb2ebSBarry Smith   PetscInt       message_count,flowcontrolcount;
1085660746e0SBarry Smith 
1086660746e0SBarry Smith   PetscFunctionBegin;
1087660746e0SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr);
1088660746e0SBarry Smith   ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
1089660746e0SBarry Smith   nz   = bs2*(A->nz + B->nz);
1090660746e0SBarry Smith   rlen = mat->rmap->n;
1091660746e0SBarry Smith   if (!rank) {
1092660746e0SBarry Smith     header[0] = MAT_FILE_CLASSID;
1093660746e0SBarry Smith     header[1] = mat->rmap->N;
1094660746e0SBarry Smith     header[2] = mat->cmap->N;
1095660746e0SBarry Smith     ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr);
1096660746e0SBarry Smith     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
1097660746e0SBarry Smith     ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1098660746e0SBarry Smith     /* get largest number of rows any processor has */
1099660746e0SBarry Smith     range = mat->rmap->range;
1100660746e0SBarry Smith     for (i=1; i<size; i++) {
1101660746e0SBarry Smith       rlen = PetscMax(rlen,range[i+1] - range[i]);
1102660746e0SBarry Smith     }
1103660746e0SBarry Smith   } else {
1104660746e0SBarry Smith     ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr);
1105660746e0SBarry Smith   }
1106660746e0SBarry Smith 
1107660746e0SBarry Smith   ierr  = PetscMalloc((rlen/bs)*sizeof(PetscInt),&crow_lens);CHKERRQ(ierr);
1108660746e0SBarry Smith   /* compute lengths of each row  */
1109660746e0SBarry Smith   for (i=0; i<a->mbs; i++) {
1110660746e0SBarry Smith     crow_lens[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
1111660746e0SBarry Smith   }
1112660746e0SBarry Smith   /* store the row lengths to the file */
1113638eb2ebSBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1114660746e0SBarry Smith   if (!rank) {
1115660746e0SBarry Smith     MPI_Status status;
1116660746e0SBarry Smith     ierr  = PetscMalloc(rlen*sizeof(PetscInt),&row_lens);CHKERRQ(ierr);
1117660746e0SBarry Smith     rlen  = (range[1] - range[0])/bs;
1118660746e0SBarry Smith     for (i=0; i<rlen; i++) {
1119660746e0SBarry Smith       for (j=0; j<bs; j++) {
1120660746e0SBarry Smith         row_lens[i*bs+j] = bs*crow_lens[i];
1121660746e0SBarry Smith       }
1122660746e0SBarry Smith     }
1123660746e0SBarry Smith     ierr = PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1124660746e0SBarry Smith     for (i=1; i<size; i++) {
1125660746e0SBarry Smith       rlen = (range[i+1] - range[i])/bs;
1126638eb2ebSBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr);
1127660746e0SBarry Smith       ierr = MPI_Recv(crow_lens,rlen,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr);
1128660746e0SBarry Smith       for (k=0; k<rlen; k++) {
1129660746e0SBarry Smith 	for (j=0; j<bs; j++) {
1130660746e0SBarry Smith 	  row_lens[k*bs+j] = bs*crow_lens[k];
1131660746e0SBarry Smith 	}
1132660746e0SBarry Smith       }
1133660746e0SBarry Smith       ierr = PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1134660746e0SBarry Smith     }
1135638eb2ebSBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr);
1136660746e0SBarry Smith     ierr = PetscFree(row_lens);CHKERRQ(ierr);
1137660746e0SBarry Smith   } else {
1138638eb2ebSBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr);
1139660746e0SBarry Smith     ierr = MPI_Send(crow_lens,mat->rmap->n/bs,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
1140638eb2ebSBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr);
1141660746e0SBarry Smith   }
1142660746e0SBarry Smith   ierr = PetscFree(crow_lens);CHKERRQ(ierr);
1143660746e0SBarry Smith 
1144660746e0SBarry 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
1145660746e0SBarry Smith      information needed to make it for each row from a block row. This does require more communication but still not more than
1146660746e0SBarry Smith      the communication needed for the nonzero values  */
1147660746e0SBarry Smith   nzmax = nz; /*  space a largest processor needs */
1148660746e0SBarry Smith   ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,((PetscObject)mat)->comm);CHKERRQ(ierr);
1149660746e0SBarry Smith   ierr = PetscMalloc(nzmax*sizeof(PetscInt),&column_indices);CHKERRQ(ierr);
1150660746e0SBarry Smith   cnt  = 0;
1151660746e0SBarry Smith   for (i=0; i<a->mbs; i++) {
1152660746e0SBarry Smith     pcnt = cnt;
1153660746e0SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
1154660746e0SBarry Smith       if ( (col = garray[B->j[j]]) > cstart) break;
1155660746e0SBarry Smith       for (l=0; l<bs; l++) {
1156660746e0SBarry Smith 	column_indices[cnt++] = bs*col+l;
1157660746e0SBarry Smith       }
1158660746e0SBarry Smith     }
1159660746e0SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
1160660746e0SBarry Smith       for (l=0; l<bs; l++) {
1161660746e0SBarry Smith         column_indices[cnt++] = bs*(A->j[k] + cstart)+l;
1162660746e0SBarry Smith       }
1163660746e0SBarry Smith     }
1164660746e0SBarry Smith     for (; j<B->i[i+1]; j++) {
1165660746e0SBarry Smith       for (l=0; l<bs; l++) {
1166660746e0SBarry Smith         column_indices[cnt++] = bs*garray[B->j[j]]+l;
1167660746e0SBarry Smith       }
1168660746e0SBarry Smith     }
1169660746e0SBarry Smith     len = cnt - pcnt;
1170660746e0SBarry Smith     for (k=1; k<bs; k++) {
1171660746e0SBarry Smith       ierr = PetscMemcpy(&column_indices[cnt],&column_indices[pcnt],len*sizeof(PetscInt));CHKERRQ(ierr);
1172660746e0SBarry Smith       cnt += len;
1173660746e0SBarry Smith     }
1174660746e0SBarry Smith   }
1175660746e0SBarry Smith   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1176660746e0SBarry Smith 
1177660746e0SBarry Smith   /* store the columns to the file */
1178638eb2ebSBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1179660746e0SBarry Smith   if (!rank) {
1180660746e0SBarry Smith     MPI_Status status;
1181660746e0SBarry Smith     ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1182660746e0SBarry Smith     for (i=1; i<size; i++) {
1183638eb2ebSBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr);
1184660746e0SBarry Smith       ierr = MPI_Recv(&cnt,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr);
1185660746e0SBarry Smith       ierr = MPI_Recv(column_indices,cnt,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr);
1186660746e0SBarry Smith       ierr = PetscBinaryWrite(fd,column_indices,cnt,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
1187660746e0SBarry Smith     }
1188638eb2ebSBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr);
1189660746e0SBarry Smith   } else {
1190638eb2ebSBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr);
1191660746e0SBarry Smith     ierr = MPI_Send(&cnt,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
1192660746e0SBarry Smith     ierr = MPI_Send(column_indices,cnt,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
1193638eb2ebSBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr);
1194660746e0SBarry Smith   }
1195660746e0SBarry Smith   ierr = PetscFree(column_indices);CHKERRQ(ierr);
1196660746e0SBarry Smith 
1197660746e0SBarry Smith   /* load up the numerical values */
1198660746e0SBarry Smith   ierr = PetscMalloc(nzmax*sizeof(PetscScalar),&column_values);CHKERRQ(ierr);
1199660746e0SBarry Smith   cnt = 0;
1200660746e0SBarry Smith   for (i=0; i<a->mbs; i++) {
1201660746e0SBarry Smith     rlen = bs*(B->i[i+1] - B->i[i] + A->i[i+1] - A->i[i]);
1202660746e0SBarry Smith     for (j=B->i[i]; j<B->i[i+1]; j++) {
1203660746e0SBarry Smith       if ( garray[B->j[j]] > cstart) break;
1204660746e0SBarry Smith       for (l=0; l<bs; l++) {
1205660746e0SBarry Smith         for (ll=0; ll<bs; ll++) {
1206660746e0SBarry Smith 	  column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1207660746e0SBarry Smith         }
1208660746e0SBarry Smith       }
1209660746e0SBarry Smith       cnt += bs;
1210660746e0SBarry Smith     }
1211660746e0SBarry Smith     for (k=A->i[i]; k<A->i[i+1]; k++) {
1212660746e0SBarry Smith       for (l=0; l<bs; l++) {
1213660746e0SBarry Smith         for (ll=0; ll<bs; ll++) {
1214660746e0SBarry Smith           column_values[cnt + l*rlen + ll] = A->a[bs2*k+l+bs*ll];
1215660746e0SBarry Smith         }
1216660746e0SBarry Smith       }
1217660746e0SBarry Smith       cnt += bs;
1218660746e0SBarry Smith     }
1219660746e0SBarry Smith     for (; j<B->i[i+1]; j++) {
1220660746e0SBarry Smith       for (l=0; l<bs; l++) {
1221660746e0SBarry Smith         for (ll=0; ll<bs; ll++) {
1222660746e0SBarry Smith 	  column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1223660746e0SBarry Smith         }
1224660746e0SBarry Smith       }
1225660746e0SBarry Smith       cnt += bs;
1226660746e0SBarry Smith     }
1227660746e0SBarry Smith     cnt += (bs-1)*rlen;
1228660746e0SBarry Smith   }
1229660746e0SBarry Smith   if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1230660746e0SBarry Smith 
1231660746e0SBarry Smith   /* store the column values to the file */
1232638eb2ebSBarry Smith   ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr);
1233660746e0SBarry Smith   if (!rank) {
1234660746e0SBarry Smith     MPI_Status status;
1235660746e0SBarry Smith     ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
1236660746e0SBarry Smith     for (i=1; i<size; i++) {
1237638eb2ebSBarry Smith       ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr);
1238660746e0SBarry Smith       ierr = MPI_Recv(&cnt,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr);
1239660746e0SBarry Smith       ierr = MPI_Recv(column_values,cnt,MPIU_SCALAR,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr);
1240660746e0SBarry Smith       ierr = PetscBinaryWrite(fd,column_values,cnt,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr);
1241660746e0SBarry Smith     }
1242638eb2ebSBarry Smith     ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr);
1243660746e0SBarry Smith   } else {
1244638eb2ebSBarry Smith     ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr);
1245660746e0SBarry Smith     ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
1246660746e0SBarry Smith     ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr);
1247638eb2ebSBarry Smith     ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr);
1248660746e0SBarry Smith   }
1249660746e0SBarry Smith   ierr = PetscFree(column_values);CHKERRQ(ierr);
1250660746e0SBarry Smith 
1251660746e0SBarry Smith   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
1252660746e0SBarry Smith   if (file) {
1253660746e0SBarry Smith     fprintf(file,"-matload_block_size %d\n",(int)mat->rmap->bs);
1254660746e0SBarry Smith   }
1255660746e0SBarry Smith   PetscFunctionReturn(0);
1256660746e0SBarry Smith }
1257660746e0SBarry Smith 
1258660746e0SBarry Smith #undef __FUNCT__
12594a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIBAIJ"
1260dfbe8321SBarry Smith PetscErrorCode MatView_MPIBAIJ(Mat mat,PetscViewer viewer)
126157b952d6SSatish Balay {
1262dfbe8321SBarry Smith   PetscErrorCode ierr;
1263ace3abfcSBarry Smith   PetscBool      iascii,isdraw,issocket,isbinary;
126457b952d6SSatish Balay 
1265d64ed03dSBarry Smith   PetscFunctionBegin;
12662692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
12672692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
12682692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr);
12692692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
1270660746e0SBarry Smith   if (iascii || isdraw || issocket) {
12717b2a1423SBarry Smith     ierr = MatView_MPIBAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr);
1272660746e0SBarry Smith   } else if (isbinary) {
1273660746e0SBarry Smith     ierr = MatView_MPIBAIJ_Binary(mat,viewer);CHKERRQ(ierr);
12745cd90555SBarry Smith   } else {
127565e19b50SBarry Smith     SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Viewer type %s not supported by MPIBAIJ matrices",((PetscObject)viewer)->type_name);
127657b952d6SSatish Balay   }
12773a40ed3dSBarry Smith   PetscFunctionReturn(0);
127857b952d6SSatish Balay }
127957b952d6SSatish Balay 
12804a2ae208SSatish Balay #undef __FUNCT__
12814a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIBAIJ"
1282dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIBAIJ(Mat mat)
128379bdfe76SSatish Balay {
128479bdfe76SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
1285dfbe8321SBarry Smith   PetscErrorCode ierr;
128679bdfe76SSatish Balay 
1287d64ed03dSBarry Smith   PetscFunctionBegin;
1288aa482453SBarry Smith #if defined(PETSC_USE_LOG)
1289d0f46423SBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%D,Cols=%D",mat->rmap->N,mat->cmap->N);
129079bdfe76SSatish Balay #endif
12918798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr);
12928798bf22SSatish Balay   ierr = MatStashDestroy_Private(&mat->bstash);CHKERRQ(ierr);
12936bf464f9SBarry Smith   ierr = MatDestroy(&baij->A);CHKERRQ(ierr);
12946bf464f9SBarry Smith   ierr = MatDestroy(&baij->B);CHKERRQ(ierr);
1295aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
12966bc0bbbfSBarry Smith   ierr = PetscTableDestroy(&baij->colmap);CHKERRQ(ierr);
129748e59246SSatish Balay #else
129805b42c5fSBarry Smith   ierr = PetscFree(baij->colmap);CHKERRQ(ierr);
129948e59246SSatish Balay #endif
130005b42c5fSBarry Smith   ierr = PetscFree(baij->garray);CHKERRQ(ierr);
13016bf464f9SBarry Smith   ierr = VecDestroy(&baij->lvec);CHKERRQ(ierr);
13026bf464f9SBarry Smith   ierr = VecScatterDestroy(&baij->Mvctx);CHKERRQ(ierr);
1303fca92195SBarry Smith   ierr = PetscFree2(baij->rowvalues,baij->rowindices);CHKERRQ(ierr);
130405b42c5fSBarry Smith   ierr = PetscFree(baij->barray);CHKERRQ(ierr);
1305fca92195SBarry Smith   ierr = PetscFree2(baij->hd,baij->ht);CHKERRQ(ierr);
1306899cda47SBarry Smith   ierr = PetscFree(baij->rangebs);CHKERRQ(ierr);
1307bf0cc555SLisandro Dalcin   ierr = PetscFree(mat->data);CHKERRQ(ierr);
1308901853e0SKris Buschelman 
1309dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr);
1310901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
1311901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
1312901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);CHKERRQ(ierr);
1313901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
1314aac34f13SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
1315901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);CHKERRQ(ierr);
1316901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatSetHashTableFactor_C","",PETSC_NULL);CHKERRQ(ierr);
1317ad5247fdSHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_mpisbaij_C","",PETSC_NULL);CHKERRQ(ierr);
1318c0cdd4a1SDahai Guo   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_mpibstrm_C","",PETSC_NULL);CHKERRQ(ierr);
13193a40ed3dSBarry Smith   PetscFunctionReturn(0);
132079bdfe76SSatish Balay }
132179bdfe76SSatish Balay 
13224a2ae208SSatish Balay #undef __FUNCT__
13234a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIBAIJ"
1324dfbe8321SBarry Smith PetscErrorCode MatMult_MPIBAIJ(Mat A,Vec xx,Vec yy)
1325cee3aa6bSSatish Balay {
1326cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1327dfbe8321SBarry Smith   PetscErrorCode ierr;
1328b24ad042SBarry Smith   PetscInt       nt;
1329cee3aa6bSSatish Balay 
1330d64ed03dSBarry Smith   PetscFunctionBegin;
1331e1311b90SBarry Smith   ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr);
1332e7e72b3dSBarry Smith   if (nt != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");
1333e1311b90SBarry Smith   ierr = VecGetLocalSize(yy,&nt);CHKERRQ(ierr);
1334e7e72b3dSBarry Smith   if (nt != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible parition of A and yy");
1335ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1336f830108cSBarry Smith   ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr);
1337ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1338f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr);
13393a40ed3dSBarry Smith   PetscFunctionReturn(0);
1340cee3aa6bSSatish Balay }
1341cee3aa6bSSatish Balay 
13424a2ae208SSatish Balay #undef __FUNCT__
13434a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIBAIJ"
1344dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1345cee3aa6bSSatish Balay {
1346cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1347dfbe8321SBarry Smith   PetscErrorCode ierr;
1348d64ed03dSBarry Smith 
1349d64ed03dSBarry Smith   PetscFunctionBegin;
1350ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1351f830108cSBarry Smith   ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1352ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1353f830108cSBarry Smith   ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr);
13543a40ed3dSBarry Smith   PetscFunctionReturn(0);
1355cee3aa6bSSatish Balay }
1356cee3aa6bSSatish Balay 
13574a2ae208SSatish Balay #undef __FUNCT__
13584a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIBAIJ"
1359dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIBAIJ(Mat A,Vec xx,Vec yy)
1360cee3aa6bSSatish Balay {
1361cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1362dfbe8321SBarry Smith   PetscErrorCode ierr;
1363ace3abfcSBarry Smith   PetscBool      merged;
1364cee3aa6bSSatish Balay 
1365d64ed03dSBarry Smith   PetscFunctionBegin;
1366a5ff213dSBarry Smith   ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr);
1367cee3aa6bSSatish Balay   /* do nondiagonal part */
13687c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1369a5ff213dSBarry Smith   if (!merged) {
1370cee3aa6bSSatish Balay     /* send it on its way */
1371ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1372cee3aa6bSSatish Balay     /* do local part */
13737c922b88SBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1374cee3aa6bSSatish Balay     /* receive remote parts: note this assumes the values are not actually */
1375a5ff213dSBarry Smith     /* inserted in yy until the next line */
1376ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1377a5ff213dSBarry Smith   } else {
1378a5ff213dSBarry Smith     /* do local part */
1379a5ff213dSBarry Smith     ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr);
1380a5ff213dSBarry Smith     /* send it on its way */
1381ca9f406cSSatish Balay     ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1382a5ff213dSBarry Smith     /* values actually were received in the Begin() but we need to call this nop */
1383ca9f406cSSatish Balay     ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1384a5ff213dSBarry Smith   }
13853a40ed3dSBarry Smith   PetscFunctionReturn(0);
1386cee3aa6bSSatish Balay }
1387cee3aa6bSSatish Balay 
13884a2ae208SSatish Balay #undef __FUNCT__
13894a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIBAIJ"
1390dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1391cee3aa6bSSatish Balay {
1392cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1393dfbe8321SBarry Smith   PetscErrorCode ierr;
1394cee3aa6bSSatish Balay 
1395d64ed03dSBarry Smith   PetscFunctionBegin;
1396cee3aa6bSSatish Balay   /* do nondiagonal part */
13977c922b88SBarry Smith   ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr);
1398cee3aa6bSSatish Balay   /* send it on its way */
1399ca9f406cSSatish Balay   ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
1400cee3aa6bSSatish Balay   /* do local part */
14017c922b88SBarry Smith   ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr);
1402cee3aa6bSSatish Balay   /* receive remote parts: note this assumes the values are not actually */
1403cee3aa6bSSatish Balay   /* inserted in yy until the next line, which is true for my implementation*/
1404cee3aa6bSSatish Balay   /* but is not perhaps always true. */
1405ca9f406cSSatish Balay   ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
14063a40ed3dSBarry Smith   PetscFunctionReturn(0);
1407cee3aa6bSSatish Balay }
1408cee3aa6bSSatish Balay 
1409cee3aa6bSSatish Balay /*
1410cee3aa6bSSatish Balay   This only works correctly for square matrices where the subblock A->A is the
1411cee3aa6bSSatish Balay    diagonal block
1412cee3aa6bSSatish Balay */
14134a2ae208SSatish Balay #undef __FUNCT__
14144a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIBAIJ"
1415dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIBAIJ(Mat A,Vec v)
1416cee3aa6bSSatish Balay {
1417cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1418dfbe8321SBarry Smith   PetscErrorCode ierr;
1419d64ed03dSBarry Smith 
1420d64ed03dSBarry Smith   PetscFunctionBegin;
1421e32f2f54SBarry 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");
14223a40ed3dSBarry Smith   ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr);
14233a40ed3dSBarry Smith   PetscFunctionReturn(0);
1424cee3aa6bSSatish Balay }
1425cee3aa6bSSatish Balay 
14264a2ae208SSatish Balay #undef __FUNCT__
14274a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIBAIJ"
1428f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIBAIJ(Mat A,PetscScalar aa)
1429cee3aa6bSSatish Balay {
1430cee3aa6bSSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1431dfbe8321SBarry Smith   PetscErrorCode ierr;
1432d64ed03dSBarry Smith 
1433d64ed03dSBarry Smith   PetscFunctionBegin;
1434f4df32b1SMatthew Knepley   ierr = MatScale(a->A,aa);CHKERRQ(ierr);
1435f4df32b1SMatthew Knepley   ierr = MatScale(a->B,aa);CHKERRQ(ierr);
14363a40ed3dSBarry Smith   PetscFunctionReturn(0);
1437cee3aa6bSSatish Balay }
1438026e39d0SSatish Balay 
14394a2ae208SSatish Balay #undef __FUNCT__
14404a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIBAIJ"
1441b24ad042SBarry Smith PetscErrorCode MatGetRow_MPIBAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1442acdf5bf4SSatish Balay {
1443acdf5bf4SSatish Balay   Mat_MPIBAIJ    *mat = (Mat_MPIBAIJ*)matin->data;
144487828ca2SBarry Smith   PetscScalar    *vworkA,*vworkB,**pvA,**pvB,*v_p;
14456849ba73SBarry Smith   PetscErrorCode ierr;
1446d0f46423SBarry Smith   PetscInt       bs = matin->rmap->bs,bs2 = mat->bs2,i,*cworkA,*cworkB,**pcA,**pcB;
1447d0f46423SBarry Smith   PetscInt       nztot,nzA,nzB,lrow,brstart = matin->rmap->rstart,brend = matin->rmap->rend;
1448899cda47SBarry Smith   PetscInt       *cmap,*idx_p,cstart = mat->cstartbs;
1449acdf5bf4SSatish Balay 
1450d64ed03dSBarry Smith   PetscFunctionBegin;
1451e7e72b3dSBarry Smith   if (row < brstart || row >= brend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local rows");
1452e32f2f54SBarry Smith   if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active");
1453acdf5bf4SSatish Balay   mat->getrowactive = PETSC_TRUE;
1454acdf5bf4SSatish Balay 
1455acdf5bf4SSatish Balay   if (!mat->rowvalues && (idx || v)) {
1456acdf5bf4SSatish Balay     /*
1457acdf5bf4SSatish Balay         allocate enough space to hold information from the longest row.
1458acdf5bf4SSatish Balay     */
1459acdf5bf4SSatish Balay     Mat_SeqBAIJ *Aa = (Mat_SeqBAIJ*)mat->A->data,*Ba = (Mat_SeqBAIJ*)mat->B->data;
1460b24ad042SBarry Smith     PetscInt     max = 1,mbs = mat->mbs,tmp;
1461bd16c2feSSatish Balay     for (i=0; i<mbs; i++) {
1462acdf5bf4SSatish Balay       tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1463acdf5bf4SSatish Balay       if (max < tmp) { max = tmp; }
1464acdf5bf4SSatish Balay     }
1465fca92195SBarry Smith     ierr = PetscMalloc2(max*bs2,PetscScalar,&mat->rowvalues,max*bs2,PetscInt,&mat->rowindices);CHKERRQ(ierr);
1466acdf5bf4SSatish Balay   }
1467d9d09a02SSatish Balay   lrow = row - brstart;
1468acdf5bf4SSatish Balay 
1469acdf5bf4SSatish Balay   pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1470acdf5bf4SSatish Balay   if (!v)   {pvA = 0; pvB = 0;}
1471acdf5bf4SSatish Balay   if (!idx) {pcA = 0; if (!v) pcB = 0;}
1472f830108cSBarry Smith   ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1473f830108cSBarry Smith   ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
1474acdf5bf4SSatish Balay   nztot = nzA + nzB;
1475acdf5bf4SSatish Balay 
1476acdf5bf4SSatish Balay   cmap  = mat->garray;
1477acdf5bf4SSatish Balay   if (v  || idx) {
1478acdf5bf4SSatish Balay     if (nztot) {
1479acdf5bf4SSatish Balay       /* Sort by increasing column numbers, assuming A and B already sorted */
1480b24ad042SBarry Smith       PetscInt imark = -1;
1481acdf5bf4SSatish Balay       if (v) {
1482acdf5bf4SSatish Balay         *v = v_p = mat->rowvalues;
1483acdf5bf4SSatish Balay         for (i=0; i<nzB; i++) {
1484d9d09a02SSatish Balay           if (cmap[cworkB[i]/bs] < cstart)   v_p[i] = vworkB[i];
1485acdf5bf4SSatish Balay           else break;
1486acdf5bf4SSatish Balay         }
1487acdf5bf4SSatish Balay         imark = i;
1488acdf5bf4SSatish Balay         for (i=0; i<nzA; i++)     v_p[imark+i] = vworkA[i];
1489acdf5bf4SSatish Balay         for (i=imark; i<nzB; i++) v_p[nzA+i]   = vworkB[i];
1490acdf5bf4SSatish Balay       }
1491acdf5bf4SSatish Balay       if (idx) {
1492acdf5bf4SSatish Balay         *idx = idx_p = mat->rowindices;
1493acdf5bf4SSatish Balay         if (imark > -1) {
1494acdf5bf4SSatish Balay           for (i=0; i<imark; i++) {
1495bd16c2feSSatish Balay             idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1496acdf5bf4SSatish Balay           }
1497acdf5bf4SSatish Balay         } else {
1498acdf5bf4SSatish Balay           for (i=0; i<nzB; i++) {
1499d9d09a02SSatish Balay             if (cmap[cworkB[i]/bs] < cstart)
1500d9d09a02SSatish Balay               idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1501acdf5bf4SSatish Balay             else break;
1502acdf5bf4SSatish Balay           }
1503acdf5bf4SSatish Balay           imark = i;
1504acdf5bf4SSatish Balay         }
1505d9d09a02SSatish Balay         for (i=0; i<nzA; i++)     idx_p[imark+i] = cstart*bs + cworkA[i];
1506d9d09a02SSatish Balay         for (i=imark; i<nzB; i++) idx_p[nzA+i]   = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1507acdf5bf4SSatish Balay       }
1508d64ed03dSBarry Smith     } else {
1509d212a18eSSatish Balay       if (idx) *idx = 0;
1510d212a18eSSatish Balay       if (v)   *v   = 0;
1511d212a18eSSatish Balay     }
1512acdf5bf4SSatish Balay   }
1513acdf5bf4SSatish Balay   *nz = nztot;
1514f830108cSBarry Smith   ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr);
1515f830108cSBarry Smith   ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr);
15163a40ed3dSBarry Smith   PetscFunctionReturn(0);
1517acdf5bf4SSatish Balay }
1518acdf5bf4SSatish Balay 
15194a2ae208SSatish Balay #undef __FUNCT__
15204a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIBAIJ"
1521b24ad042SBarry Smith PetscErrorCode MatRestoreRow_MPIBAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1522acdf5bf4SSatish Balay {
1523acdf5bf4SSatish Balay   Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1524d64ed03dSBarry Smith 
1525d64ed03dSBarry Smith   PetscFunctionBegin;
1526e7e72b3dSBarry Smith   if (!baij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
1527acdf5bf4SSatish Balay   baij->getrowactive = PETSC_FALSE;
15283a40ed3dSBarry Smith   PetscFunctionReturn(0);
1529acdf5bf4SSatish Balay }
1530acdf5bf4SSatish Balay 
15314a2ae208SSatish Balay #undef __FUNCT__
15324a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIBAIJ"
1533dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIBAIJ(Mat A)
153458667388SSatish Balay {
153558667388SSatish Balay   Mat_MPIBAIJ    *l = (Mat_MPIBAIJ*)A->data;
1536dfbe8321SBarry Smith   PetscErrorCode ierr;
1537d64ed03dSBarry Smith 
1538d64ed03dSBarry Smith   PetscFunctionBegin;
153958667388SSatish Balay   ierr = MatZeroEntries(l->A);CHKERRQ(ierr);
154058667388SSatish Balay   ierr = MatZeroEntries(l->B);CHKERRQ(ierr);
15413a40ed3dSBarry Smith   PetscFunctionReturn(0);
154258667388SSatish Balay }
15430ac07820SSatish Balay 
15444a2ae208SSatish Balay #undef __FUNCT__
15454a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIBAIJ"
1546dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIBAIJ(Mat matin,MatInfoType flag,MatInfo *info)
15470ac07820SSatish Balay {
15484e220ebcSLois Curfman McInnes   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)matin->data;
15494e220ebcSLois Curfman McInnes   Mat            A = a->A,B = a->B;
1550dfbe8321SBarry Smith   PetscErrorCode ierr;
1551329f5518SBarry Smith   PetscReal      isend[5],irecv[5];
15520ac07820SSatish Balay 
1553d64ed03dSBarry Smith   PetscFunctionBegin;
1554d0f46423SBarry Smith   info->block_size     = (PetscReal)matin->rmap->bs;
15554e220ebcSLois Curfman McInnes   ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr);
15560e4b21beSBarry Smith   isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1557de87f314SBarry Smith   isend[3] = info->memory;  isend[4] = info->mallocs;
15584e220ebcSLois Curfman McInnes   ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr);
15590e4b21beSBarry Smith   isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1560de87f314SBarry Smith   isend[3] += info->memory;  isend[4] += info->mallocs;
15610ac07820SSatish Balay   if (flag == MAT_LOCAL) {
15624e220ebcSLois Curfman McInnes     info->nz_used      = isend[0];
15634e220ebcSLois Curfman McInnes     info->nz_allocated = isend[1];
15644e220ebcSLois Curfman McInnes     info->nz_unneeded  = isend[2];
15654e220ebcSLois Curfman McInnes     info->memory       = isend[3];
15664e220ebcSLois Curfman McInnes     info->mallocs      = isend[4];
15670ac07820SSatish Balay   } else if (flag == MAT_GLOBAL_MAX) {
1568d9822059SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,((PetscObject)matin)->comm);CHKERRQ(ierr);
15694e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
15704e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
15714e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
15724e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
15734e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
15740ac07820SSatish Balay   } else if (flag == MAT_GLOBAL_SUM) {
1575d9822059SBarry Smith     ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,((PetscObject)matin)->comm);CHKERRQ(ierr);
15764e220ebcSLois Curfman McInnes     info->nz_used      = irecv[0];
15774e220ebcSLois Curfman McInnes     info->nz_allocated = irecv[1];
15784e220ebcSLois Curfman McInnes     info->nz_unneeded  = irecv[2];
15794e220ebcSLois Curfman McInnes     info->memory       = irecv[3];
15804e220ebcSLois Curfman McInnes     info->mallocs      = irecv[4];
1581d41123aaSBarry Smith   } else {
158265e19b50SBarry Smith     SETERRQ1(((PetscObject)matin)->comm,PETSC_ERR_ARG_WRONG,"Unknown MatInfoType argument %d",(int)flag);
15830ac07820SSatish Balay   }
15844e220ebcSLois Curfman McInnes   info->fill_ratio_given  = 0; /* no parallel LU/ILU/Cholesky */
15854e220ebcSLois Curfman McInnes   info->fill_ratio_needed = 0;
15864e220ebcSLois Curfman McInnes   info->factor_mallocs    = 0;
15873a40ed3dSBarry Smith   PetscFunctionReturn(0);
15880ac07820SSatish Balay }
15890ac07820SSatish Balay 
15904a2ae208SSatish Balay #undef __FUNCT__
15914a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIBAIJ"
1592ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIBAIJ(Mat A,MatOption op,PetscBool  flg)
159358667388SSatish Balay {
159458667388SSatish Balay   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
1595dfbe8321SBarry Smith   PetscErrorCode ierr;
159658667388SSatish Balay 
1597d64ed03dSBarry Smith   PetscFunctionBegin;
159812c028f9SKris Buschelman   switch (op) {
1599512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
160012c028f9SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
160128b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1602a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
160312c028f9SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
16044e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
16054e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
160612c028f9SKris Buschelman     break;
160712c028f9SKris Buschelman   case MAT_ROW_ORIENTED:
16084e0d8c25SBarry Smith     a->roworiented = flg;
16094e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
16104e0d8c25SBarry Smith     ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr);
161112c028f9SKris Buschelman     break;
16124e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1613290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
161412c028f9SKris Buschelman     break;
161512c028f9SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
16164e0d8c25SBarry Smith     a->donotstash = flg;
161712c028f9SKris Buschelman     break;
161812c028f9SKris Buschelman   case MAT_USE_HASH_TABLE:
16194e0d8c25SBarry Smith     a->ht_flag = flg;
162012c028f9SKris Buschelman     break;
162177e54ba9SKris Buschelman   case MAT_SYMMETRIC:
162277e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
16232188ac68SBarry Smith   case MAT_HERMITIAN:
16242188ac68SBarry Smith   case MAT_SYMMETRY_ETERNAL:
16254e0d8c25SBarry Smith     ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr);
162677e54ba9SKris Buschelman     break;
162712c028f9SKris Buschelman   default:
162865e19b50SBarry Smith     SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"unknown option %d",op);
1629d64ed03dSBarry Smith   }
16303a40ed3dSBarry Smith   PetscFunctionReturn(0);
163158667388SSatish Balay }
163258667388SSatish Balay 
16334a2ae208SSatish Balay #undef __FUNCT__
16346a719282SBarry Smith #define __FUNCT__ "MatTranspose_MPIBAIJ"
1635fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIBAIJ(Mat A,MatReuse reuse,Mat *matout)
16360ac07820SSatish Balay {
16370ac07820SSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)A->data;
16380ac07820SSatish Balay   Mat_SeqBAIJ    *Aloc;
16390ac07820SSatish Balay   Mat            B;
1640dfbe8321SBarry Smith   PetscErrorCode ierr;
1641d0f46423SBarry Smith   PetscInt       M=A->rmap->N,N=A->cmap->N,*ai,*aj,i,*rvals,j,k,col;
1642d0f46423SBarry Smith   PetscInt       bs=A->rmap->bs,mbs=baij->mbs;
16433eda8832SBarry Smith   MatScalar      *a;
16440ac07820SSatish Balay 
1645d64ed03dSBarry Smith   PetscFunctionBegin;
1646e7e72b3dSBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1647fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
16487adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr);
1649d0f46423SBarry Smith     ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr);
16507adad957SLisandro Dalcin     ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr);
16512e72b8d9SBarry Smith     /* Do not know preallocation information, but must set block size */
16522e72b8d9SBarry Smith     ierr = MatMPIBAIJSetPreallocation(B,A->rmap->bs,PETSC_DECIDE,PETSC_NULL,PETSC_DECIDE,PETSC_NULL);CHKERRQ(ierr);
1653fc4dec0aSBarry Smith   } else {
1654fc4dec0aSBarry Smith     B = *matout;
1655fc4dec0aSBarry Smith   }
16560ac07820SSatish Balay 
16570ac07820SSatish Balay   /* copy over the A part */
16580ac07820SSatish Balay   Aloc = (Mat_SeqBAIJ*)baij->A->data;
16590ac07820SSatish Balay   ai   = Aloc->i; aj = Aloc->j; a = Aloc->a;
1660b24ad042SBarry Smith   ierr = PetscMalloc(bs*sizeof(PetscInt),&rvals);CHKERRQ(ierr);
16610ac07820SSatish Balay 
16620ac07820SSatish Balay   for (i=0; i<mbs; i++) {
1663899cda47SBarry Smith     rvals[0] = bs*(baij->rstartbs + i);
16640ac07820SSatish Balay     for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
16650ac07820SSatish Balay     for (j=ai[i]; j<ai[i+1]; j++) {
1666899cda47SBarry Smith       col = (baij->cstartbs+aj[j])*bs;
16670ac07820SSatish Balay       for (k=0; k<bs; k++) {
166897e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);CHKERRQ(ierr);
16690ac07820SSatish Balay         col++; a += bs;
16700ac07820SSatish Balay       }
16710ac07820SSatish Balay     }
16720ac07820SSatish Balay   }
16730ac07820SSatish Balay   /* copy over the B part */
16740ac07820SSatish Balay   Aloc = (Mat_SeqBAIJ*)baij->B->data;
16750ac07820SSatish Balay   ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
16760ac07820SSatish Balay   for (i=0; i<mbs; i++) {
1677899cda47SBarry Smith     rvals[0] = bs*(baij->rstartbs + i);
16780ac07820SSatish Balay     for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
16790ac07820SSatish Balay     for (j=ai[i]; j<ai[i+1]; j++) {
16800ac07820SSatish Balay       col = baij->garray[aj[j]]*bs;
16810ac07820SSatish Balay       for (k=0; k<bs; k++) {
168297e5c40aSBarry Smith         ierr = MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);CHKERRQ(ierr);
16830ac07820SSatish Balay         col++; a += bs;
16840ac07820SSatish Balay       }
16850ac07820SSatish Balay     }
16860ac07820SSatish Balay   }
1687606d414cSSatish Balay   ierr = PetscFree(rvals);CHKERRQ(ierr);
16880ac07820SSatish Balay   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16890ac07820SSatish Balay   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16900ac07820SSatish Balay 
1691815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *matout != A) {
16920ac07820SSatish Balay     *matout = B;
16930ac07820SSatish Balay   } else {
1694eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,B);CHKERRQ(ierr);
16950ac07820SSatish Balay   }
16963a40ed3dSBarry Smith   PetscFunctionReturn(0);
16970ac07820SSatish Balay }
16980e95ebc0SSatish Balay 
16994a2ae208SSatish Balay #undef __FUNCT__
17004a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIBAIJ"
1701dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIBAIJ(Mat mat,Vec ll,Vec rr)
17020e95ebc0SSatish Balay {
170336c4a09eSSatish Balay   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*)mat->data;
170436c4a09eSSatish Balay   Mat            a = baij->A,b = baij->B;
1705dfbe8321SBarry Smith   PetscErrorCode ierr;
1706b24ad042SBarry Smith   PetscInt       s1,s2,s3;
17070e95ebc0SSatish Balay 
1708d64ed03dSBarry Smith   PetscFunctionBegin;
170936c4a09eSSatish Balay   ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr);
171036c4a09eSSatish Balay   if (rr) {
171136c4a09eSSatish Balay     ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr);
1712e32f2f54SBarry Smith     if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
171336c4a09eSSatish Balay     /* Overlap communication with computation. */
1714ca9f406cSSatish Balay     ierr = VecScatterBegin(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
171536c4a09eSSatish Balay   }
17160e95ebc0SSatish Balay   if (ll) {
17170e95ebc0SSatish Balay     ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr);
1718e32f2f54SBarry Smith     if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1719a21fb8cbSBarry Smith     ierr = (*b->ops->diagonalscale)(b,ll,PETSC_NULL);CHKERRQ(ierr);
17200e95ebc0SSatish Balay   }
172136c4a09eSSatish Balay   /* scale  the diagonal block */
172236c4a09eSSatish Balay   ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr);
172336c4a09eSSatish Balay 
172436c4a09eSSatish Balay   if (rr) {
172536c4a09eSSatish Balay     /* Do a scatter end and then right scale the off-diagonal block */
1726ca9f406cSSatish Balay     ierr = VecScatterEnd(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1727a21fb8cbSBarry Smith     ierr = (*b->ops->diagonalscale)(b,PETSC_NULL,baij->lvec);CHKERRQ(ierr);
172836c4a09eSSatish Balay   }
172936c4a09eSSatish Balay 
17303a40ed3dSBarry Smith   PetscFunctionReturn(0);
17310e95ebc0SSatish Balay }
17320e95ebc0SSatish Balay 
17334a2ae208SSatish Balay #undef __FUNCT__
17344a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIBAIJ"
17352b40b63fSBarry Smith PetscErrorCode MatZeroRows_MPIBAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
17360ac07820SSatish Balay {
17370ac07820SSatish Balay   Mat_MPIBAIJ       *l = (Mat_MPIBAIJ*)A->data;
17386849ba73SBarry Smith   PetscErrorCode    ierr;
1739b24ad042SBarry Smith   PetscMPIInt       imdex,size = l->size,n,rank = l->rank;
1740d0f46423SBarry Smith   PetscInt          i,*owners = A->rmap->range;
1741b24ad042SBarry Smith   PetscInt          *nprocs,j,idx,nsends,row;
1742b24ad042SBarry Smith   PetscInt          nmax,*svalues,*starts,*owner,nrecvs;
17437adad957SLisandro Dalcin   PetscInt          *rvalues,tag = ((PetscObject)A)->tag,count,base,slen,*source,lastidx = -1;
1744d0f46423SBarry Smith   PetscInt          *lens,*lrows,*values,rstart_bs=A->rmap->rstart;
17457adad957SLisandro Dalcin   MPI_Comm          comm = ((PetscObject)A)->comm;
17460ac07820SSatish Balay   MPI_Request       *send_waits,*recv_waits;
17470ac07820SSatish Balay   MPI_Status        recv_status,*send_status;
174897b48c8fSBarry Smith   const PetscScalar *xx;
174997b48c8fSBarry Smith   PetscScalar       *bb;
17506543fbbaSBarry Smith #if defined(PETSC_DEBUG)
1751ace3abfcSBarry Smith   PetscBool         found = PETSC_FALSE;
17526543fbbaSBarry Smith #endif
17530ac07820SSatish Balay 
1754d64ed03dSBarry Smith   PetscFunctionBegin;
17550ac07820SSatish Balay   /*  first count number of contributors to each processor */
1756b24ad042SBarry Smith   ierr  = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
1757b24ad042SBarry Smith   ierr  = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
1758b24ad042SBarry Smith   ierr  = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/
17596543fbbaSBarry Smith   j = 0;
17600ac07820SSatish Balay   for (i=0; i<N; i++) {
17616543fbbaSBarry Smith     if (lastidx > (idx = rows[i])) j = 0;
17626543fbbaSBarry Smith     lastidx = idx;
17636543fbbaSBarry Smith     for (; j<size; j++) {
1764357c27ecSBarry Smith       if (idx >= owners[j] && idx < owners[j+1]) {
17656543fbbaSBarry Smith         nprocs[2*j]++;
17666543fbbaSBarry Smith         nprocs[2*j+1] = 1;
17676543fbbaSBarry Smith         owner[i] = j;
17686543fbbaSBarry Smith #if defined(PETSC_DEBUG)
17696543fbbaSBarry Smith         found = PETSC_TRUE;
17706543fbbaSBarry Smith #endif
17716543fbbaSBarry Smith         break;
17720ac07820SSatish Balay       }
17730ac07820SSatish Balay     }
17746543fbbaSBarry Smith #if defined(PETSC_DEBUG)
1775e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
17766543fbbaSBarry Smith     found = PETSC_FALSE;
17776543fbbaSBarry Smith #endif
17780ac07820SSatish Balay   }
1779c1dc657dSBarry Smith   nsends = 0;  for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
17800ac07820SSatish Balay 
17817367270fSBarry Smith   if (A->nooffproczerorows) {
17827367270fSBarry 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");
17837367270fSBarry Smith     nrecvs = nsends;
17847367270fSBarry Smith     nmax   = N;
17857367270fSBarry Smith   } else {
17860ac07820SSatish Balay     /* inform other processors of number of messages and max length*/
1787c1dc657dSBarry Smith     ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
17887367270fSBarry Smith   }
17890ac07820SSatish Balay 
17900ac07820SSatish Balay   /* post receives:   */
1791b24ad042SBarry Smith   ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr);
1792b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
17930ac07820SSatish Balay   for (i=0; i<nrecvs; i++) {
1794b24ad042SBarry Smith     ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr);
17950ac07820SSatish Balay   }
17960ac07820SSatish Balay 
17970ac07820SSatish Balay   /* do sends:
17980ac07820SSatish Balay      1) starts[i] gives the starting index in svalues for stuff going to
17990ac07820SSatish Balay      the ith processor
18000ac07820SSatish Balay   */
1801b24ad042SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr);
1802b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
1803b24ad042SBarry Smith   ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
18040ac07820SSatish Balay   starts[0]  = 0;
1805c1dc657dSBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
18060ac07820SSatish Balay   for (i=0; i<N; i++) {
18070ac07820SSatish Balay     svalues[starts[owner[i]]++] = rows[i];
18080ac07820SSatish Balay   }
18090ac07820SSatish Balay 
18100ac07820SSatish Balay   starts[0] = 0;
1811c1dc657dSBarry Smith   for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
18120ac07820SSatish Balay   count = 0;
18130ac07820SSatish Balay   for (i=0; i<size; i++) {
1814c1dc657dSBarry Smith     if (nprocs[2*i+1]) {
1815b24ad042SBarry Smith       ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr);
18160ac07820SSatish Balay     }
18170ac07820SSatish Balay   }
1818606d414cSSatish Balay   ierr = PetscFree(starts);CHKERRQ(ierr);
18190ac07820SSatish Balay 
1820357c27ecSBarry Smith   base = owners[rank];
18210ac07820SSatish Balay 
18220ac07820SSatish Balay   /*  wait on receives */
1823fca92195SBarry Smith   ierr   = PetscMalloc2(nrecvs+1,PetscInt,&lens,nrecvs+1,PetscInt,&source);CHKERRQ(ierr);
1824fca92195SBarry Smith   count  = nrecvs;
1825fca92195SBarry Smith   slen = 0;
18260ac07820SSatish Balay   while (count) {
1827ca161407SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
18280ac07820SSatish Balay     /* unpack receives into our local space */
1829b24ad042SBarry Smith     ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr);
18300ac07820SSatish Balay     source[imdex]  = recv_status.MPI_SOURCE;
18310ac07820SSatish Balay     lens[imdex]    = n;
18320ac07820SSatish Balay     slen          += n;
18330ac07820SSatish Balay     count--;
18340ac07820SSatish Balay   }
1835606d414cSSatish Balay   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
18360ac07820SSatish Balay 
18370ac07820SSatish Balay   /* move the data into the send scatter */
1838b24ad042SBarry Smith   ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr);
18390ac07820SSatish Balay   count = 0;
18400ac07820SSatish Balay   for (i=0; i<nrecvs; i++) {
18410ac07820SSatish Balay     values = rvalues + i*nmax;
18420ac07820SSatish Balay     for (j=0; j<lens[i]; j++) {
18430ac07820SSatish Balay       lrows[count++] = values[j] - base;
18440ac07820SSatish Balay     }
18450ac07820SSatish Balay   }
1846606d414cSSatish Balay   ierr = PetscFree(rvalues);CHKERRQ(ierr);
1847fca92195SBarry Smith   ierr = PetscFree2(lens,source);CHKERRQ(ierr);
1848606d414cSSatish Balay   ierr = PetscFree(owner);CHKERRQ(ierr);
1849606d414cSSatish Balay   ierr = PetscFree(nprocs);CHKERRQ(ierr);
18500ac07820SSatish Balay 
185197b48c8fSBarry Smith   /* fix right hand side if needed */
185297b48c8fSBarry Smith   if (x && b) {
185397b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
185497b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
185597b48c8fSBarry Smith     for (i=0; i<slen; i++) {
185697b48c8fSBarry Smith       bb[lrows[i]] = diag*xx[lrows[i]];
185797b48c8fSBarry Smith     }
185897b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
185997b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
186097b48c8fSBarry Smith   }
186197b48c8fSBarry Smith 
18620ac07820SSatish Balay   /* actually zap the local rows */
186372dacd9aSBarry Smith   /*
186472dacd9aSBarry Smith         Zero the required rows. If the "diagonal block" of the matrix
1865a8c7a070SBarry Smith      is square and the user wishes to set the diagonal we use separate
186672dacd9aSBarry Smith      code so that MatSetValues() is not called for each diagonal allocating
186772dacd9aSBarry Smith      new memory, thus calling lots of mallocs and slowing things down.
186872dacd9aSBarry Smith 
186972dacd9aSBarry Smith   */
18709c957beeSSatish Balay   /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
18712b40b63fSBarry Smith   ierr = MatZeroRows_SeqBAIJ(l->B,slen,lrows,0.0,0,0);CHKERRQ(ierr);
1872d0f46423SBarry Smith   if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) {
18732b40b63fSBarry Smith     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,diag,0,0);CHKERRQ(ierr);
1874f4df32b1SMatthew Knepley   } else if (diag != 0.0) {
18752b40b63fSBarry Smith     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr);
1876e7e72b3dSBarry 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\
1877512a5fc5SBarry Smith        MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
1878a07cd24cSSatish Balay     for (i=0; i<slen; i++) {
1879a07cd24cSSatish Balay       row  = lrows[i] + rstart_bs;
1880f4df32b1SMatthew Knepley       ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
1881a07cd24cSSatish Balay     }
1882a07cd24cSSatish Balay     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1883a07cd24cSSatish Balay     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18849c957beeSSatish Balay   } else {
18852b40b63fSBarry Smith     ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr);
1886a07cd24cSSatish Balay   }
18879c957beeSSatish Balay 
1888606d414cSSatish Balay   ierr = PetscFree(lrows);CHKERRQ(ierr);
1889a07cd24cSSatish Balay 
18900ac07820SSatish Balay   /* wait on sends */
18910ac07820SSatish Balay   if (nsends) {
189282502324SSatish Balay     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
1893ca161407SBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
1894606d414cSSatish Balay     ierr = PetscFree(send_status);CHKERRQ(ierr);
18950ac07820SSatish Balay   }
1896606d414cSSatish Balay   ierr = PetscFree(send_waits);CHKERRQ(ierr);
1897606d414cSSatish Balay   ierr = PetscFree(svalues);CHKERRQ(ierr);
18980ac07820SSatish Balay 
18993a40ed3dSBarry Smith   PetscFunctionReturn(0);
19000ac07820SSatish Balay }
190172dacd9aSBarry Smith 
19024a2ae208SSatish Balay #undef __FUNCT__
19034a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIBAIJ"
1904dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIBAIJ(Mat A)
1905bb5a7306SBarry Smith {
1906bb5a7306SBarry Smith   Mat_MPIBAIJ    *a   = (Mat_MPIBAIJ*)A->data;
1907dfbe8321SBarry Smith   PetscErrorCode ierr;
1908d64ed03dSBarry Smith 
1909d64ed03dSBarry Smith   PetscFunctionBegin;
1910bb5a7306SBarry Smith   ierr = MatSetUnfactored(a->A);CHKERRQ(ierr);
19113a40ed3dSBarry Smith   PetscFunctionReturn(0);
1912bb5a7306SBarry Smith }
1913bb5a7306SBarry Smith 
19146849ba73SBarry Smith static PetscErrorCode MatDuplicate_MPIBAIJ(Mat,MatDuplicateOption,Mat *);
19150ac07820SSatish Balay 
19164a2ae208SSatish Balay #undef __FUNCT__
19174a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIBAIJ"
1918ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIBAIJ(Mat A,Mat B,PetscBool  *flag)
19197fc3c18eSBarry Smith {
19207fc3c18eSBarry Smith   Mat_MPIBAIJ    *matB = (Mat_MPIBAIJ*)B->data,*matA = (Mat_MPIBAIJ*)A->data;
19217fc3c18eSBarry Smith   Mat            a,b,c,d;
1922ace3abfcSBarry Smith   PetscBool      flg;
1923dfbe8321SBarry Smith   PetscErrorCode ierr;
19247fc3c18eSBarry Smith 
19257fc3c18eSBarry Smith   PetscFunctionBegin;
19267fc3c18eSBarry Smith   a = matA->A; b = matA->B;
19277fc3c18eSBarry Smith   c = matB->A; d = matB->B;
19287fc3c18eSBarry Smith 
19297fc3c18eSBarry Smith   ierr = MatEqual(a,c,&flg);CHKERRQ(ierr);
1930abc0a331SBarry Smith   if (flg) {
19317fc3c18eSBarry Smith     ierr = MatEqual(b,d,&flg);CHKERRQ(ierr);
19327fc3c18eSBarry Smith   }
19337adad957SLisandro Dalcin   ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,((PetscObject)A)->comm);CHKERRQ(ierr);
19347fc3c18eSBarry Smith   PetscFunctionReturn(0);
19357fc3c18eSBarry Smith }
19367fc3c18eSBarry Smith 
19373c896bc6SHong Zhang #undef __FUNCT__
19383c896bc6SHong Zhang #define __FUNCT__ "MatCopy_MPIBAIJ"
19393c896bc6SHong Zhang PetscErrorCode MatCopy_MPIBAIJ(Mat A,Mat B,MatStructure str)
19403c896bc6SHong Zhang {
19413c896bc6SHong Zhang   PetscErrorCode ierr;
19423c896bc6SHong Zhang   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ *)A->data;
19433c896bc6SHong Zhang   Mat_MPIBAIJ    *b = (Mat_MPIBAIJ *)B->data;
19443c896bc6SHong Zhang 
19453c896bc6SHong Zhang   PetscFunctionBegin;
19463c896bc6SHong Zhang   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
19473c896bc6SHong Zhang   if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
19483c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
19493c896bc6SHong Zhang   } else {
19503c896bc6SHong Zhang     ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr);
19513c896bc6SHong Zhang     ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr);
19523c896bc6SHong Zhang   }
19533c896bc6SHong Zhang   PetscFunctionReturn(0);
19543c896bc6SHong Zhang }
1955273d9f13SBarry Smith 
19564a2ae208SSatish Balay #undef __FUNCT__
1957*4994cf47SJed Brown #define __FUNCT__ "MatSetUp_MPIBAIJ"
1958*4994cf47SJed Brown PetscErrorCode MatSetUp_MPIBAIJ(Mat A)
1959273d9f13SBarry Smith {
1960dfbe8321SBarry Smith   PetscErrorCode ierr;
1961273d9f13SBarry Smith 
1962273d9f13SBarry Smith   PetscFunctionBegin;
1963db4efbfdSBarry Smith   ierr =  MatMPIBAIJSetPreallocation(A,-PetscMax(A->rmap->bs,1),PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr);
1964273d9f13SBarry Smith   PetscFunctionReturn(0);
1965273d9f13SBarry Smith }
1966273d9f13SBarry Smith 
19674fe895cdSHong Zhang #undef __FUNCT__
19684fe895cdSHong Zhang #define __FUNCT__ "MatAXPY_MPIBAIJ"
19694fe895cdSHong Zhang PetscErrorCode MatAXPY_MPIBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
19704fe895cdSHong Zhang {
19714fe895cdSHong Zhang   PetscErrorCode ierr;
19724fe895cdSHong Zhang   Mat_MPIBAIJ    *xx=(Mat_MPIBAIJ *)X->data,*yy=(Mat_MPIBAIJ *)Y->data;
19734fe895cdSHong Zhang   PetscBLASInt   bnz,one=1;
19744fe895cdSHong Zhang   Mat_SeqBAIJ    *x,*y;
19754fe895cdSHong Zhang 
19764fe895cdSHong Zhang   PetscFunctionBegin;
19774fe895cdSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
19784fe895cdSHong Zhang     PetscScalar alpha = a;
19794fe895cdSHong Zhang     x = (Mat_SeqBAIJ *)xx->A->data;
19804fe895cdSHong Zhang     y = (Mat_SeqBAIJ *)yy->A->data;
19810805154bSBarry Smith     bnz = PetscBLASIntCast(x->nz);
19824fe895cdSHong Zhang     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
19834fe895cdSHong Zhang     x = (Mat_SeqBAIJ *)xx->B->data;
19844fe895cdSHong Zhang     y = (Mat_SeqBAIJ *)yy->B->data;
19850805154bSBarry Smith     bnz = PetscBLASIntCast(x->nz);
19864fe895cdSHong Zhang     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
19874fe895cdSHong Zhang   } else {
19884fe895cdSHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
19894fe895cdSHong Zhang   }
19904fe895cdSHong Zhang   PetscFunctionReturn(0);
19914fe895cdSHong Zhang }
19924fe895cdSHong Zhang 
199399cafbc1SBarry Smith #undef __FUNCT__
199441c166b1SJed Brown #define __FUNCT__ "MatSetBlockSize_MPIBAIJ"
199541c166b1SJed Brown PetscErrorCode MatSetBlockSize_MPIBAIJ(Mat A,PetscInt bs)
199641c166b1SJed Brown {
199741c166b1SJed Brown   Mat_MPIBAIJ    *a   = (Mat_MPIBAIJ*)A->data;
1998829b6ff0SJed Brown   PetscInt rbs,cbs;
199941c166b1SJed Brown   PetscErrorCode ierr;
200041c166b1SJed Brown 
200141c166b1SJed Brown   PetscFunctionBegin;
200241c166b1SJed Brown   ierr = MatSetBlockSize(a->A,bs);CHKERRQ(ierr);
200341c166b1SJed Brown   ierr = MatSetBlockSize(a->B,bs);CHKERRQ(ierr);
2004829b6ff0SJed Brown   ierr = PetscLayoutGetBlockSize(A->rmap,&rbs);CHKERRQ(ierr);
2005829b6ff0SJed Brown   ierr = PetscLayoutGetBlockSize(A->cmap,&cbs);CHKERRQ(ierr);
2006e32f2f54SBarry Smith   if (rbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with BAIJ %d",bs,rbs);
2007e32f2f54SBarry Smith   if (cbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with BAIJ %d",bs,cbs);
200841c166b1SJed Brown   PetscFunctionReturn(0);
200941c166b1SJed Brown }
201041c166b1SJed Brown 
201141c166b1SJed Brown #undef __FUNCT__
201299cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIBAIJ"
201399cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIBAIJ(Mat A)
201499cafbc1SBarry Smith {
201599cafbc1SBarry Smith   Mat_MPIBAIJ   *a = (Mat_MPIBAIJ*)A->data;
201699cafbc1SBarry Smith   PetscErrorCode ierr;
201799cafbc1SBarry Smith 
201899cafbc1SBarry Smith   PetscFunctionBegin;
201999cafbc1SBarry Smith   ierr = MatRealPart(a->A);CHKERRQ(ierr);
202099cafbc1SBarry Smith   ierr = MatRealPart(a->B);CHKERRQ(ierr);
202199cafbc1SBarry Smith   PetscFunctionReturn(0);
202299cafbc1SBarry Smith }
202399cafbc1SBarry Smith 
202499cafbc1SBarry Smith #undef __FUNCT__
202599cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIBAIJ"
202699cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIBAIJ(Mat A)
202799cafbc1SBarry Smith {
202899cafbc1SBarry Smith   Mat_MPIBAIJ   *a = (Mat_MPIBAIJ*)A->data;
202999cafbc1SBarry Smith   PetscErrorCode ierr;
203099cafbc1SBarry Smith 
203199cafbc1SBarry Smith   PetscFunctionBegin;
203299cafbc1SBarry Smith   ierr = MatImaginaryPart(a->A);CHKERRQ(ierr);
203399cafbc1SBarry Smith   ierr = MatImaginaryPart(a->B);CHKERRQ(ierr);
203499cafbc1SBarry Smith   PetscFunctionReturn(0);
203599cafbc1SBarry Smith }
203699cafbc1SBarry Smith 
203782094794SBarry Smith #undef __FUNCT__
203882094794SBarry Smith #define __FUNCT__ "MatGetSubMatrix_MPIBAIJ"
20394aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIBAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
20404aa3045dSJed Brown {
20414aa3045dSJed Brown   PetscErrorCode ierr;
20424aa3045dSJed Brown   IS             iscol_local;
20434aa3045dSJed Brown   PetscInt       csize;
20444aa3045dSJed Brown 
20454aa3045dSJed Brown   PetscFunctionBegin;
20464aa3045dSJed Brown   ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr);
2047b79d0421SJed Brown   if (call == MAT_REUSE_MATRIX) {
2048b79d0421SJed Brown     ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr);
2049e32f2f54SBarry Smith     if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2050b79d0421SJed Brown   } else {
20514aa3045dSJed Brown     ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr);
2052b79d0421SJed Brown   }
20534aa3045dSJed Brown   ierr = MatGetSubMatrix_MPIBAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr);
2054b79d0421SJed Brown   if (call == MAT_INITIAL_MATRIX) {
2055b79d0421SJed Brown     ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr);
20566bf464f9SBarry Smith     ierr = ISDestroy(&iscol_local);CHKERRQ(ierr);
2057b79d0421SJed Brown   }
20584aa3045dSJed Brown   PetscFunctionReturn(0);
20594aa3045dSJed Brown }
20604aa3045dSJed Brown 
20614aa3045dSJed Brown #undef __FUNCT__
2062dd183c9eSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIBAIJ_Private"
206382094794SBarry Smith /*
206482094794SBarry Smith     Not great since it makes two copies of the submatrix, first an SeqBAIJ
206582094794SBarry Smith   in local and then by concatenating the local matrices the end result.
206682094794SBarry Smith   Writing it directly would be much like MatGetSubMatrices_MPIBAIJ()
206782094794SBarry Smith */
20684aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIBAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
206982094794SBarry Smith {
207082094794SBarry Smith   PetscErrorCode ierr;
207182094794SBarry Smith   PetscMPIInt    rank,size;
207282094794SBarry Smith   PetscInt       i,m,n,rstart,row,rend,nz,*cwork,j,bs;
207382094794SBarry Smith   PetscInt       *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
207482094794SBarry Smith   Mat            *local,M,Mreuse;
207582094794SBarry Smith   MatScalar      *vwork,*aa;
207682094794SBarry Smith   MPI_Comm       comm = ((PetscObject)mat)->comm;
207782094794SBarry Smith   Mat_SeqBAIJ    *aij;
207882094794SBarry Smith 
207982094794SBarry Smith 
208082094794SBarry Smith   PetscFunctionBegin;
208182094794SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
208282094794SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
208382094794SBarry Smith 
208482094794SBarry Smith   if (call ==  MAT_REUSE_MATRIX) {
208582094794SBarry Smith     ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr);
2086e32f2f54SBarry Smith     if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
208782094794SBarry Smith     local = &Mreuse;
208882094794SBarry Smith     ierr  = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr);
208982094794SBarry Smith   } else {
209082094794SBarry Smith     ierr   = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
209182094794SBarry Smith     Mreuse = *local;
209282094794SBarry Smith     ierr   = PetscFree(local);CHKERRQ(ierr);
209382094794SBarry Smith   }
209482094794SBarry Smith 
209582094794SBarry Smith   /*
209682094794SBarry Smith       m - number of local rows
209782094794SBarry Smith       n - number of columns (same on all processors)
209882094794SBarry Smith       rstart - first row in new global matrix generated
209982094794SBarry Smith   */
210082094794SBarry Smith   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
210182094794SBarry Smith   ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr);
210282094794SBarry Smith   m    = m/bs;
210382094794SBarry Smith   n    = n/bs;
210482094794SBarry Smith 
210582094794SBarry Smith   if (call == MAT_INITIAL_MATRIX) {
210682094794SBarry Smith     aij = (Mat_SeqBAIJ*)(Mreuse)->data;
210782094794SBarry Smith     ii  = aij->i;
210882094794SBarry Smith     jj  = aij->j;
210982094794SBarry Smith 
211082094794SBarry Smith     /*
211182094794SBarry Smith         Determine the number of non-zeros in the diagonal and off-diagonal
211282094794SBarry Smith         portions of the matrix in order to do correct preallocation
211382094794SBarry Smith     */
211482094794SBarry Smith 
211582094794SBarry Smith     /* first get start and end of "diagonal" columns */
211682094794SBarry Smith     if (csize == PETSC_DECIDE) {
211782094794SBarry Smith       ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr);
211882094794SBarry Smith       if (mglobal == n*bs) { /* square matrix */
211982094794SBarry Smith 	nlocal = m;
212082094794SBarry Smith       } else {
212182094794SBarry Smith         nlocal = n/size + ((n % size) > rank);
212282094794SBarry Smith       }
212382094794SBarry Smith     } else {
212482094794SBarry Smith       nlocal = csize/bs;
212582094794SBarry Smith     }
212682094794SBarry Smith     ierr   = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr);
212782094794SBarry Smith     rstart = rend - nlocal;
212865e19b50SBarry 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);
212982094794SBarry Smith 
213082094794SBarry Smith     /* next, compute all the lengths */
213182094794SBarry Smith     ierr  = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr);
213282094794SBarry Smith     olens = dlens + m;
213382094794SBarry Smith     for (i=0; i<m; i++) {
213482094794SBarry Smith       jend = ii[i+1] - ii[i];
213582094794SBarry Smith       olen = 0;
213682094794SBarry Smith       dlen = 0;
213782094794SBarry Smith       for (j=0; j<jend; j++) {
213882094794SBarry Smith         if (*jj < rstart || *jj >= rend) olen++;
213982094794SBarry Smith         else dlen++;
214082094794SBarry Smith         jj++;
214182094794SBarry Smith       }
214282094794SBarry Smith       olens[i] = olen;
214382094794SBarry Smith       dlens[i] = dlen;
214482094794SBarry Smith     }
214582094794SBarry Smith     ierr = MatCreate(comm,&M);CHKERRQ(ierr);
214682094794SBarry Smith     ierr = MatSetSizes(M,bs*m,bs*nlocal,PETSC_DECIDE,bs*n);CHKERRQ(ierr);
214782094794SBarry Smith     ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr);
214882094794SBarry Smith     ierr = MatMPIBAIJSetPreallocation(M,bs,0,dlens,0,olens);CHKERRQ(ierr);
214982094794SBarry Smith     ierr = PetscFree(dlens);CHKERRQ(ierr);
215082094794SBarry Smith   } else {
215182094794SBarry Smith     PetscInt ml,nl;
215282094794SBarry Smith 
215382094794SBarry Smith     M = *newmat;
215482094794SBarry Smith     ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr);
2155e32f2f54SBarry Smith     if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
215682094794SBarry Smith     ierr = MatZeroEntries(M);CHKERRQ(ierr);
215782094794SBarry Smith     /*
215882094794SBarry Smith          The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
215982094794SBarry Smith        rather than the slower MatSetValues().
216082094794SBarry Smith     */
216182094794SBarry Smith     M->was_assembled = PETSC_TRUE;
216282094794SBarry Smith     M->assembled     = PETSC_FALSE;
216382094794SBarry Smith   }
216482094794SBarry Smith   ierr = MatSetOption(M,MAT_ROW_ORIENTED,PETSC_FALSE);CHKERRQ(ierr);
216582094794SBarry Smith   ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr);
216682094794SBarry Smith   aij = (Mat_SeqBAIJ*)(Mreuse)->data;
216782094794SBarry Smith   ii  = aij->i;
216882094794SBarry Smith   jj  = aij->j;
216982094794SBarry Smith   aa  = aij->a;
217082094794SBarry Smith   for (i=0; i<m; i++) {
217182094794SBarry Smith     row   = rstart/bs + i;
217282094794SBarry Smith     nz    = ii[i+1] - ii[i];
217382094794SBarry Smith     cwork = jj;     jj += nz;
217482094794SBarry Smith     vwork = aa;     aa += nz;
217582094794SBarry Smith     ierr = MatSetValuesBlocked_MPIBAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
217682094794SBarry Smith   }
217782094794SBarry Smith 
217882094794SBarry Smith   ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
217982094794SBarry Smith   ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
218082094794SBarry Smith   *newmat = M;
218182094794SBarry Smith 
218282094794SBarry Smith   /* save submatrix used in processor for next request */
218382094794SBarry Smith   if (call ==  MAT_INITIAL_MATRIX) {
218482094794SBarry Smith     ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr);
218582094794SBarry Smith     ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr);
218682094794SBarry Smith   }
218782094794SBarry Smith 
218882094794SBarry Smith   PetscFunctionReturn(0);
218982094794SBarry Smith }
219082094794SBarry Smith 
219182094794SBarry Smith #undef __FUNCT__
219282094794SBarry Smith #define __FUNCT__ "MatPermute_MPIBAIJ"
219382094794SBarry Smith PetscErrorCode MatPermute_MPIBAIJ(Mat A,IS rowp,IS colp,Mat *B)
219482094794SBarry Smith {
219582094794SBarry Smith   MPI_Comm       comm,pcomm;
219682094794SBarry Smith   PetscInt       first,local_size,nrows;
219782094794SBarry Smith   const PetscInt *rows;
2198dbf0e21dSBarry Smith   PetscMPIInt    size;
219982094794SBarry Smith   IS             crowp,growp,irowp,lrowp,lcolp,icolp;
220082094794SBarry Smith   PetscErrorCode ierr;
220182094794SBarry Smith 
220282094794SBarry Smith   PetscFunctionBegin;
220382094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr);
220482094794SBarry Smith   /* make a collective version of 'rowp' */
220582094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)rowp,&pcomm);CHKERRQ(ierr);
220682094794SBarry Smith   if (pcomm==comm) {
220782094794SBarry Smith     crowp = rowp;
220882094794SBarry Smith   } else {
220982094794SBarry Smith     ierr = ISGetSize(rowp,&nrows);CHKERRQ(ierr);
221082094794SBarry Smith     ierr = ISGetIndices(rowp,&rows);CHKERRQ(ierr);
221170b3c8c7SBarry Smith     ierr = ISCreateGeneral(comm,nrows,rows,PETSC_COPY_VALUES,&crowp);CHKERRQ(ierr);
221282094794SBarry Smith     ierr = ISRestoreIndices(rowp,&rows);CHKERRQ(ierr);
221382094794SBarry Smith   }
221482094794SBarry Smith   /* collect the global row permutation and invert it */
221582094794SBarry Smith   ierr = ISAllGather(crowp,&growp);CHKERRQ(ierr);
221682094794SBarry Smith   ierr = ISSetPermutation(growp);CHKERRQ(ierr);
221782094794SBarry Smith   if (pcomm!=comm) {
22186bf464f9SBarry Smith     ierr = ISDestroy(&crowp);CHKERRQ(ierr);
221982094794SBarry Smith   }
222082094794SBarry Smith   ierr = ISInvertPermutation(growp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
222182094794SBarry Smith   /* get the local target indices */
222282094794SBarry Smith   ierr = MatGetOwnershipRange(A,&first,PETSC_NULL);CHKERRQ(ierr);
222382094794SBarry Smith   ierr = MatGetLocalSize(A,&local_size,PETSC_NULL);CHKERRQ(ierr);
222482094794SBarry Smith   ierr = ISGetIndices(irowp,&rows);CHKERRQ(ierr);
222570b3c8c7SBarry Smith   ierr = ISCreateGeneral(MPI_COMM_SELF,local_size,rows+first,PETSC_COPY_VALUES,&lrowp);CHKERRQ(ierr);
222682094794SBarry Smith   ierr = ISRestoreIndices(irowp,&rows);CHKERRQ(ierr);
22276bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
222882094794SBarry Smith   /* the column permutation is so much easier;
222982094794SBarry Smith      make a local version of 'colp' and invert it */
223082094794SBarry Smith   ierr = PetscObjectGetComm((PetscObject)colp,&pcomm);CHKERRQ(ierr);
2231dbf0e21dSBarry Smith   ierr = MPI_Comm_size(pcomm,&size);CHKERRQ(ierr);
2232dbf0e21dSBarry Smith   if (size==1) {
223382094794SBarry Smith     lcolp = colp;
223482094794SBarry Smith   } else {
223582094794SBarry Smith     ierr = ISGetSize(colp,&nrows);CHKERRQ(ierr);
223682094794SBarry Smith     ierr = ISGetIndices(colp,&rows);CHKERRQ(ierr);
223770b3c8c7SBarry Smith     ierr = ISCreateGeneral(MPI_COMM_SELF,nrows,rows,PETSC_COPY_VALUES,&lcolp);CHKERRQ(ierr);
223882094794SBarry Smith   }
2239dbf0e21dSBarry Smith   ierr = ISSetPermutation(lcolp);CHKERRQ(ierr);
224082094794SBarry Smith   ierr = ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
22414aa3045dSJed Brown   ierr = ISSetPermutation(icolp);CHKERRQ(ierr);
2242dbf0e21dSBarry Smith   if (size>1) {
224382094794SBarry Smith     ierr = ISRestoreIndices(colp,&rows);CHKERRQ(ierr);
22446bf464f9SBarry Smith     ierr = ISDestroy(&lcolp);CHKERRQ(ierr);
224582094794SBarry Smith   }
224682094794SBarry Smith   /* now we just get the submatrix */
22474aa3045dSJed Brown   ierr = MatGetSubMatrix_MPIBAIJ_Private(A,lrowp,icolp,local_size,MAT_INITIAL_MATRIX,B);CHKERRQ(ierr);
224882094794SBarry Smith   /* clean up */
22496bf464f9SBarry Smith   ierr = ISDestroy(&lrowp);CHKERRQ(ierr);
22506bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
225182094794SBarry Smith   PetscFunctionReturn(0);
225282094794SBarry Smith }
225382094794SBarry Smith 
22548c7482ecSBarry Smith #undef __FUNCT__
22558c7482ecSBarry Smith #define __FUNCT__ "MatGetGhosts_MPIBAIJ"
22567087cfbeSBarry Smith PetscErrorCode  MatGetGhosts_MPIBAIJ(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
22578c7482ecSBarry Smith {
22588c7482ecSBarry Smith   Mat_MPIBAIJ    *baij = (Mat_MPIBAIJ*) mat->data;
22598c7482ecSBarry Smith   Mat_SeqBAIJ    *B = (Mat_SeqBAIJ*)baij->B->data;
22608c7482ecSBarry Smith 
22618c7482ecSBarry Smith   PetscFunctionBegin;
22628c7482ecSBarry Smith   if (nghosts) { *nghosts = B->nbs;}
22638c7482ecSBarry Smith   if (ghosts) {*ghosts = baij->garray;}
22648c7482ecSBarry Smith   PetscFunctionReturn(0);
22658c7482ecSBarry Smith }
22668c7482ecSBarry Smith 
226709573ac7SBarry Smith extern PetscErrorCode CreateColmap_MPIBAIJ_Private(Mat);
2268f6d58c54SBarry Smith 
2269f6d58c54SBarry Smith #undef __FUNCT__
2270f6d58c54SBarry Smith #define __FUNCT__ "MatFDColoringCreate_MPIBAIJ"
2271f6d58c54SBarry Smith /*
2272f6d58c54SBarry Smith     This routine is almost identical to MatFDColoringCreate_MPIBAIJ()!
2273f6d58c54SBarry Smith */
2274f6d58c54SBarry Smith PetscErrorCode MatFDColoringCreate_MPIBAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
2275f6d58c54SBarry Smith {
2276f6d58c54SBarry Smith   Mat_MPIBAIJ            *baij = (Mat_MPIBAIJ*)mat->data;
2277f6d58c54SBarry Smith   PetscErrorCode        ierr;
2278f6d58c54SBarry Smith   PetscMPIInt           size,*ncolsonproc,*disp,nn;
2279f6d58c54SBarry Smith   PetscInt              bs,i,n,nrows,j,k,m,*rows = 0,*A_ci,*A_cj,ncols,col;
2280f6d58c54SBarry Smith   const PetscInt        *is;
2281f6d58c54SBarry Smith   PetscInt              nis = iscoloring->n,nctot,*cols,*B_ci,*B_cj;
2282f6d58c54SBarry Smith   PetscInt              *rowhit,M,cstart,cend,colb;
2283f6d58c54SBarry Smith   PetscInt              *columnsforrow,l;
2284f6d58c54SBarry Smith   IS                    *isa;
2285ace3abfcSBarry Smith   PetscBool              done,flg;
2286992144d0SBarry Smith   ISLocalToGlobalMapping map = mat->cmap->bmapping;
2287f6d58c54SBarry Smith   PetscInt               *ltog = (map ? map->indices : (PetscInt*) PETSC_NULL) ,ctype=c->ctype;
2288f6d58c54SBarry Smith 
2289f6d58c54SBarry Smith   PetscFunctionBegin;
2290e7e72b3dSBarry Smith   if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();");
2291e7e72b3dSBarry Smith   if (ctype == IS_COLORING_GHOSTED && !map) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_INCOMP,"When using ghosted differencing matrix must have local to global mapping provided with MatSetLocalToGlobalMappingBlock");
2292f6d58c54SBarry Smith 
2293f6d58c54SBarry Smith   ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr);
2294f6d58c54SBarry Smith   ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr);
2295f6d58c54SBarry Smith   M                = mat->rmap->n/bs;
2296f6d58c54SBarry Smith   cstart           = mat->cmap->rstart/bs;
2297f6d58c54SBarry Smith   cend             = mat->cmap->rend/bs;
2298f6d58c54SBarry Smith   c->M             = mat->rmap->N/bs;  /* set the global rows and columns and local rows */
2299f6d58c54SBarry Smith   c->N             = mat->cmap->N/bs;
2300f6d58c54SBarry Smith   c->m             = mat->rmap->n/bs;
2301f6d58c54SBarry Smith   c->rstart        = mat->rmap->rstart/bs;
2302f6d58c54SBarry Smith 
2303f6d58c54SBarry Smith   c->ncolors       = nis;
2304f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt),&c->ncolumns);CHKERRQ(ierr);
2305f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->columns);CHKERRQ(ierr);
2306f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt),&c->nrows);CHKERRQ(ierr);
2307f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->rows);CHKERRQ(ierr);
2308f6d58c54SBarry Smith   ierr             = PetscMalloc(nis*sizeof(PetscInt*),&c->columnsforrow);CHKERRQ(ierr);
2309f6d58c54SBarry Smith   ierr = PetscLogObjectMemory(c,5*nis*sizeof(PetscInt));CHKERRQ(ierr);
2310f6d58c54SBarry Smith 
2311f6d58c54SBarry Smith   /* Allow access to data structures of local part of matrix */
2312f6d58c54SBarry Smith   if (!baij->colmap) {
2313f6d58c54SBarry Smith     ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
2314f6d58c54SBarry Smith   }
2315f6d58c54SBarry Smith   ierr = MatGetColumnIJ(baij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
2316f6d58c54SBarry Smith   ierr = MatGetColumnIJ(baij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
2317f6d58c54SBarry Smith 
2318f6d58c54SBarry Smith   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&rowhit);CHKERRQ(ierr);
2319f6d58c54SBarry Smith   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&columnsforrow);CHKERRQ(ierr);
2320f6d58c54SBarry Smith 
2321f6d58c54SBarry Smith   for (i=0; i<nis; i++) {
2322f6d58c54SBarry Smith     ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr);
2323f6d58c54SBarry Smith     ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr);
2324f6d58c54SBarry Smith     c->ncolumns[i] = n;
2325f6d58c54SBarry Smith     if (n) {
2326f6d58c54SBarry Smith       ierr = PetscMalloc(n*sizeof(PetscInt),&c->columns[i]);CHKERRQ(ierr);
2327f6d58c54SBarry Smith       ierr = PetscLogObjectMemory(c,n*sizeof(PetscInt));CHKERRQ(ierr);
2328f6d58c54SBarry Smith       ierr = PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));CHKERRQ(ierr);
2329f6d58c54SBarry Smith     } else {
2330f6d58c54SBarry Smith       c->columns[i]  = 0;
2331f6d58c54SBarry Smith     }
2332f6d58c54SBarry Smith 
2333f6d58c54SBarry Smith     if (ctype == IS_COLORING_GLOBAL){
2334f6d58c54SBarry Smith       /* Determine the total (parallel) number of columns of this color */
2335f6d58c54SBarry Smith       ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr);
2336fca92195SBarry Smith       ierr = PetscMalloc2(size,PetscMPIInt,&ncolsonproc,size,PetscMPIInt,&disp);CHKERRQ(ierr);
2337f6d58c54SBarry Smith 
2338f6d58c54SBarry Smith       nn   = PetscMPIIntCast(n);
2339f6d58c54SBarry Smith       ierr = MPI_Allgather(&nn,1,MPI_INT,ncolsonproc,1,MPI_INT,((PetscObject)mat)->comm);CHKERRQ(ierr);
2340f6d58c54SBarry Smith       nctot = 0; for (j=0; j<size; j++) {nctot += ncolsonproc[j];}
2341f6d58c54SBarry Smith       if (!nctot) {
2342f6d58c54SBarry Smith         ierr = PetscInfo(mat,"Coloring of matrix has some unneeded colors with no corresponding rows\n");CHKERRQ(ierr);
2343f6d58c54SBarry Smith       }
2344f6d58c54SBarry Smith 
2345f6d58c54SBarry Smith       disp[0] = 0;
2346f6d58c54SBarry Smith       for (j=1; j<size; j++) {
2347f6d58c54SBarry Smith         disp[j] = disp[j-1] + ncolsonproc[j-1];
2348f6d58c54SBarry Smith       }
2349f6d58c54SBarry Smith 
2350f6d58c54SBarry Smith       /* Get complete list of columns for color on each processor */
2351f6d58c54SBarry Smith       ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
2352f6d58c54SBarry Smith       ierr = MPI_Allgatherv((void*)is,n,MPIU_INT,cols,ncolsonproc,disp,MPIU_INT,((PetscObject)mat)->comm);CHKERRQ(ierr);
2353fca92195SBarry Smith       ierr = PetscFree2(ncolsonproc,disp);CHKERRQ(ierr);
2354f6d58c54SBarry Smith     } else if (ctype == IS_COLORING_GHOSTED){
2355f6d58c54SBarry Smith       /* Determine local number of columns of this color on this process, including ghost points */
2356f6d58c54SBarry Smith       nctot = n;
2357f6d58c54SBarry Smith       ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr);
2358f6d58c54SBarry Smith       ierr = PetscMemcpy(cols,is,n*sizeof(PetscInt));CHKERRQ(ierr);
2359f6d58c54SBarry Smith     } else {
2360e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not provided for this MatFDColoring type");
2361f6d58c54SBarry Smith     }
2362f6d58c54SBarry Smith 
2363f6d58c54SBarry Smith     /*
2364f6d58c54SBarry Smith        Mark all rows affect by these columns
2365f6d58c54SBarry Smith     */
2366f6d58c54SBarry Smith     /* Temporary option to allow for debugging/testing */
2367f6d58c54SBarry Smith     flg  = PETSC_FALSE;
2368acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(PETSC_NULL,"-matfdcoloring_slow",&flg,PETSC_NULL);CHKERRQ(ierr);
2369f6d58c54SBarry Smith     if (!flg) {/*-----------------------------------------------------------------------------*/
2370f6d58c54SBarry Smith       /* crude, fast version */
2371f6d58c54SBarry Smith       ierr = PetscMemzero(rowhit,M*sizeof(PetscInt));CHKERRQ(ierr);
2372f6d58c54SBarry Smith       /* loop over columns*/
2373f6d58c54SBarry Smith       for (j=0; j<nctot; j++) {
2374f6d58c54SBarry Smith         if (ctype == IS_COLORING_GHOSTED) {
2375f6d58c54SBarry Smith           col = ltog[cols[j]];
2376f6d58c54SBarry Smith         } else {
2377f6d58c54SBarry Smith           col  = cols[j];
2378f6d58c54SBarry Smith         }
2379f6d58c54SBarry Smith         if (col >= cstart && col < cend) {
2380f6d58c54SBarry Smith           /* column is in diagonal block of matrix */
2381f6d58c54SBarry Smith           rows = A_cj + A_ci[col-cstart];
2382f6d58c54SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
2383f6d58c54SBarry Smith         } else {
2384f6d58c54SBarry Smith #if defined (PETSC_USE_CTABLE)
2385cb9801acSJed Brown           ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr);
2386f6d58c54SBarry Smith 	  colb --;
2387f6d58c54SBarry Smith #else
2388f6d58c54SBarry Smith           colb = baij->colmap[col] - 1;
2389f6d58c54SBarry Smith #endif
2390f6d58c54SBarry Smith           if (colb == -1) {
2391f6d58c54SBarry Smith             m = 0;
2392f6d58c54SBarry Smith           } else {
2393f6d58c54SBarry Smith             colb = colb/bs;
2394f6d58c54SBarry Smith             rows = B_cj + B_ci[colb];
2395f6d58c54SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
2396f6d58c54SBarry Smith           }
2397f6d58c54SBarry Smith         }
2398f6d58c54SBarry Smith         /* loop over columns marking them in rowhit */
2399f6d58c54SBarry Smith         for (k=0; k<m; k++) {
2400f6d58c54SBarry Smith           rowhit[*rows++] = col + 1;
2401f6d58c54SBarry Smith         }
2402f6d58c54SBarry Smith       }
2403f6d58c54SBarry Smith 
2404f6d58c54SBarry Smith       /* count the number of hits */
2405f6d58c54SBarry Smith       nrows = 0;
2406f6d58c54SBarry Smith       for (j=0; j<M; j++) {
2407f6d58c54SBarry Smith         if (rowhit[j]) nrows++;
2408f6d58c54SBarry Smith       }
2409f6d58c54SBarry Smith       c->nrows[i]         = nrows;
2410f6d58c54SBarry Smith       ierr                = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr);
2411f6d58c54SBarry Smith       ierr                = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr);
2412f6d58c54SBarry Smith       ierr = PetscLogObjectMemory(c,2*(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr);
2413f6d58c54SBarry Smith       nrows = 0;
2414f6d58c54SBarry Smith       for (j=0; j<M; j++) {
2415f6d58c54SBarry Smith         if (rowhit[j]) {
2416f6d58c54SBarry Smith           c->rows[i][nrows]           = j;
2417f6d58c54SBarry Smith           c->columnsforrow[i][nrows] = rowhit[j] - 1;
2418f6d58c54SBarry Smith           nrows++;
2419f6d58c54SBarry Smith         }
2420f6d58c54SBarry Smith       }
2421f6d58c54SBarry Smith     } else {/*-------------------------------------------------------------------------------*/
2422f6d58c54SBarry Smith       /* slow version, using rowhit as a linked list */
2423f6d58c54SBarry Smith       PetscInt currentcol,fm,mfm;
2424f6d58c54SBarry Smith       rowhit[M] = M;
2425f6d58c54SBarry Smith       nrows     = 0;
2426f6d58c54SBarry Smith       /* loop over columns*/
2427f6d58c54SBarry Smith       for (j=0; j<nctot; j++) {
2428f6d58c54SBarry Smith         if (ctype == IS_COLORING_GHOSTED) {
2429f6d58c54SBarry Smith           col = ltog[cols[j]];
2430f6d58c54SBarry Smith         } else {
2431f6d58c54SBarry Smith           col  = cols[j];
2432f6d58c54SBarry Smith         }
2433f6d58c54SBarry Smith         if (col >= cstart && col < cend) {
2434f6d58c54SBarry Smith           /* column is in diagonal block of matrix */
2435f6d58c54SBarry Smith           rows = A_cj + A_ci[col-cstart];
2436f6d58c54SBarry Smith           m    = A_ci[col-cstart+1] - A_ci[col-cstart];
2437f6d58c54SBarry Smith         } else {
2438f6d58c54SBarry Smith #if defined (PETSC_USE_CTABLE)
2439f6d58c54SBarry Smith           ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr);
2440f6d58c54SBarry Smith           colb --;
2441f6d58c54SBarry Smith #else
2442f6d58c54SBarry Smith           colb = baij->colmap[col] - 1;
2443f6d58c54SBarry Smith #endif
2444f6d58c54SBarry Smith           if (colb == -1) {
2445f6d58c54SBarry Smith             m = 0;
2446f6d58c54SBarry Smith           } else {
2447f6d58c54SBarry Smith             colb = colb/bs;
2448f6d58c54SBarry Smith             rows = B_cj + B_ci[colb];
2449f6d58c54SBarry Smith             m    = B_ci[colb+1] - B_ci[colb];
2450f6d58c54SBarry Smith           }
2451f6d58c54SBarry Smith         }
2452f6d58c54SBarry Smith 
2453f6d58c54SBarry Smith         /* loop over columns marking them in rowhit */
2454f6d58c54SBarry Smith         fm    = M; /* fm points to first entry in linked list */
2455f6d58c54SBarry Smith         for (k=0; k<m; k++) {
2456f6d58c54SBarry Smith           currentcol = *rows++;
2457f6d58c54SBarry Smith 	  /* is it already in the list? */
2458f6d58c54SBarry Smith           do {
2459f6d58c54SBarry Smith             mfm  = fm;
2460f6d58c54SBarry Smith             fm   = rowhit[fm];
2461f6d58c54SBarry Smith           } while (fm < currentcol);
2462f6d58c54SBarry Smith           /* not in list so add it */
2463f6d58c54SBarry Smith           if (fm != currentcol) {
2464f6d58c54SBarry Smith             nrows++;
2465f6d58c54SBarry Smith             columnsforrow[currentcol] = col;
2466f6d58c54SBarry Smith             /* next three lines insert new entry into linked list */
2467f6d58c54SBarry Smith             rowhit[mfm]               = currentcol;
2468f6d58c54SBarry Smith             rowhit[currentcol]        = fm;
2469f6d58c54SBarry Smith             fm                        = currentcol;
2470f6d58c54SBarry Smith             /* fm points to present position in list since we know the columns are sorted */
2471f6d58c54SBarry Smith           } else {
2472e32f2f54SBarry Smith             SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid coloring of matrix detected");
2473f6d58c54SBarry Smith           }
2474f6d58c54SBarry Smith         }
2475f6d58c54SBarry Smith       }
2476f6d58c54SBarry Smith       c->nrows[i]         = nrows;
2477f6d58c54SBarry Smith       ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr);
2478f6d58c54SBarry Smith       ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr);
2479f6d58c54SBarry Smith       ierr = PetscLogObjectMemory(c,(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr);
2480f6d58c54SBarry Smith       /* now store the linked list of rows into c->rows[i] */
2481f6d58c54SBarry Smith       nrows = 0;
2482f6d58c54SBarry Smith       fm    = rowhit[M];
2483f6d58c54SBarry Smith       do {
2484f6d58c54SBarry Smith         c->rows[i][nrows]            = fm;
2485f6d58c54SBarry Smith         c->columnsforrow[i][nrows++] = columnsforrow[fm];
2486f6d58c54SBarry Smith         fm                           = rowhit[fm];
2487f6d58c54SBarry Smith       } while (fm < M);
2488f6d58c54SBarry Smith     } /* ---------------------------------------------------------------------------------------*/
2489f6d58c54SBarry Smith     ierr = PetscFree(cols);CHKERRQ(ierr);
2490f6d58c54SBarry Smith   }
2491f6d58c54SBarry Smith 
2492f6d58c54SBarry Smith   /* Optimize by adding the vscale, and scaleforrow[][] fields */
2493f6d58c54SBarry Smith   /*
2494f6d58c54SBarry Smith        vscale will contain the "diagonal" on processor scalings followed by the off processor
2495f6d58c54SBarry Smith   */
2496f6d58c54SBarry Smith   if (ctype == IS_COLORING_GLOBAL) {
2497f6d58c54SBarry Smith     PetscInt *garray;
2498f6d58c54SBarry Smith     ierr = PetscMalloc(baij->B->cmap->n*sizeof(PetscInt),&garray);CHKERRQ(ierr);
2499f6d58c54SBarry Smith     for (i=0; i<baij->B->cmap->n/bs; i++) {
2500f6d58c54SBarry Smith       for (j=0; j<bs; j++) {
2501f6d58c54SBarry Smith         garray[i*bs+j] = bs*baij->garray[i]+j;
2502f6d58c54SBarry Smith       }
2503f6d58c54SBarry Smith     }
2504f6d58c54SBarry Smith     ierr = VecCreateGhost(((PetscObject)mat)->comm,baij->A->rmap->n,PETSC_DETERMINE,baij->B->cmap->n,garray,&c->vscale);CHKERRQ(ierr);
2505f6d58c54SBarry Smith     ierr = PetscFree(garray);CHKERRQ(ierr);
2506f6d58c54SBarry Smith     CHKMEMQ;
2507f6d58c54SBarry Smith     ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr);
2508f6d58c54SBarry Smith     for (k=0; k<c->ncolors; k++) {
2509f6d58c54SBarry Smith       ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr);
2510f6d58c54SBarry Smith       for (l=0; l<c->nrows[k]; l++) {
2511f6d58c54SBarry Smith         col = c->columnsforrow[k][l];
2512f6d58c54SBarry Smith         if (col >= cstart && col < cend) {
2513f6d58c54SBarry Smith           /* column is in diagonal block of matrix */
2514f6d58c54SBarry Smith           colb = col - cstart;
2515f6d58c54SBarry Smith         } else {
2516f6d58c54SBarry Smith           /* column  is in "off-processor" part */
2517f6d58c54SBarry Smith #if defined (PETSC_USE_CTABLE)
2518f6d58c54SBarry Smith           ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr);
2519f6d58c54SBarry Smith           colb --;
2520f6d58c54SBarry Smith #else
2521f6d58c54SBarry Smith           colb = baij->colmap[col] - 1;
2522f6d58c54SBarry Smith #endif
2523f6d58c54SBarry Smith           colb = colb/bs;
2524f6d58c54SBarry Smith           colb += cend - cstart;
2525f6d58c54SBarry Smith         }
2526f6d58c54SBarry Smith         c->vscaleforrow[k][l] = colb;
2527f6d58c54SBarry Smith       }
2528f6d58c54SBarry Smith     }
2529f6d58c54SBarry Smith   } else if (ctype == IS_COLORING_GHOSTED) {
2530f6d58c54SBarry Smith     /* Get gtol mapping */
2531f6d58c54SBarry Smith     PetscInt N = mat->cmap->N, *gtol;
2532f6d58c54SBarry Smith     ierr = PetscMalloc((N+1)*sizeof(PetscInt),&gtol);CHKERRQ(ierr);
2533f6d58c54SBarry Smith     for (i=0; i<N; i++) gtol[i] = -1;
2534f6d58c54SBarry Smith     for (i=0; i<map->n; i++) gtol[ltog[i]] = i;
2535f6d58c54SBarry Smith 
2536f6d58c54SBarry Smith     c->vscale = 0; /* will be created in MatFDColoringApply() */
2537f6d58c54SBarry Smith     ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr);
2538f6d58c54SBarry Smith     for (k=0; k<c->ncolors; k++) {
2539f6d58c54SBarry Smith       ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr);
2540f6d58c54SBarry Smith       for (l=0; l<c->nrows[k]; l++) {
2541f6d58c54SBarry Smith         col = c->columnsforrow[k][l];      /* global column index */
2542f6d58c54SBarry Smith         c->vscaleforrow[k][l] = gtol[col]; /* local column index */
2543f6d58c54SBarry Smith       }
2544f6d58c54SBarry Smith     }
2545f6d58c54SBarry Smith     ierr = PetscFree(gtol);CHKERRQ(ierr);
2546f6d58c54SBarry Smith   }
2547f6d58c54SBarry Smith   ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr);
2548f6d58c54SBarry Smith 
2549f6d58c54SBarry Smith   ierr = PetscFree(rowhit);CHKERRQ(ierr);
2550f6d58c54SBarry Smith   ierr = PetscFree(columnsforrow);CHKERRQ(ierr);
2551f6d58c54SBarry Smith   ierr = MatRestoreColumnIJ(baij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr);
2552f6d58c54SBarry Smith   ierr = MatRestoreColumnIJ(baij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr);
2553f6d58c54SBarry Smith     CHKMEMQ;
2554f6d58c54SBarry Smith   PetscFunctionReturn(0);
2555f6d58c54SBarry Smith }
2556f6d58c54SBarry Smith 
2557f6d58c54SBarry Smith #undef __FUNCT__
2558d1adec66SJed Brown #define __FUNCT__ "MatGetSeqNonzeroStructure_MPIBAIJ"
2559d1adec66SJed Brown PetscErrorCode MatGetSeqNonzeroStructure_MPIBAIJ(Mat A,Mat *newmat)
2560f6d58c54SBarry Smith {
2561f6d58c54SBarry Smith   Mat            B;
2562f6d58c54SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ *)A->data;
2563f6d58c54SBarry Smith   Mat_SeqBAIJ    *ad = (Mat_SeqBAIJ*)a->A->data,*bd = (Mat_SeqBAIJ*)a->B->data;
2564f6d58c54SBarry Smith   Mat_SeqAIJ     *b;
2565f6d58c54SBarry Smith   PetscErrorCode ierr;
2566f6d58c54SBarry Smith   PetscMPIInt    size,rank,*recvcounts = 0,*displs = 0;
2567f6d58c54SBarry Smith   PetscInt       sendcount,i,*rstarts = A->rmap->range,n,cnt,j,bs = A->rmap->bs;
2568f6d58c54SBarry Smith   PetscInt       m,*garray = a->garray,*lens,*jsendbuf,*a_jsendbuf,*b_jsendbuf;
2569f6d58c54SBarry Smith 
2570f6d58c54SBarry Smith   PetscFunctionBegin;
2571f6d58c54SBarry Smith   ierr = MPI_Comm_size(((PetscObject)A)->comm,&size);CHKERRQ(ierr);
2572f6d58c54SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)A)->comm,&rank);CHKERRQ(ierr);
2573f6d58c54SBarry Smith 
2574f6d58c54SBarry Smith   /* ----------------------------------------------------------------
2575f6d58c54SBarry Smith      Tell every processor the number of nonzeros per row
2576f6d58c54SBarry Smith   */
2577f6d58c54SBarry Smith   ierr = PetscMalloc((A->rmap->N/bs)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
2578f6d58c54SBarry Smith   for (i=A->rmap->rstart/bs; i<A->rmap->rend/bs; i++) {
2579f6d58c54SBarry 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];
2580f6d58c54SBarry Smith   }
2581f6d58c54SBarry Smith   sendcount = A->rmap->rend/bs - A->rmap->rstart/bs;
2582f6d58c54SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscMPIInt),&recvcounts);CHKERRQ(ierr);
2583f6d58c54SBarry Smith   displs     = recvcounts + size;
2584f6d58c54SBarry Smith   for (i=0; i<size; i++) {
2585f6d58c54SBarry Smith     recvcounts[i] = A->rmap->range[i+1]/bs - A->rmap->range[i]/bs;
2586f6d58c54SBarry Smith     displs[i]     = A->rmap->range[i]/bs;
2587f6d58c54SBarry Smith   }
2588f6d58c54SBarry Smith #if defined(PETSC_HAVE_MPI_IN_PLACE)
2589f6d58c54SBarry Smith   ierr  = MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,lens,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr);
2590f6d58c54SBarry Smith #else
2591f6d58c54SBarry Smith   ierr  = MPI_Allgatherv(lens+A->rmap->rstart/bs,sendcount,MPIU_INT,lens,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr);
2592f6d58c54SBarry Smith #endif
2593f6d58c54SBarry Smith   /* ---------------------------------------------------------------
2594f6d58c54SBarry Smith      Create the sequential matrix of the same type as the local block diagonal
2595f6d58c54SBarry Smith   */
2596f6d58c54SBarry Smith   ierr  = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr);
2597f6d58c54SBarry Smith   ierr  = MatSetSizes(B,A->rmap->N/bs,A->cmap->N/bs,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
2598f6d58c54SBarry Smith   ierr  = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
2599f6d58c54SBarry Smith   ierr  = MatSeqAIJSetPreallocation(B,0,lens);CHKERRQ(ierr);
2600f6d58c54SBarry Smith   b = (Mat_SeqAIJ *)B->data;
2601f6d58c54SBarry Smith 
2602f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2603f6d58c54SBarry Smith     Copy my part of matrix column indices over
2604f6d58c54SBarry Smith   */
2605f6d58c54SBarry Smith   sendcount  = ad->nz + bd->nz;
2606f6d58c54SBarry Smith   jsendbuf   = b->j + b->i[rstarts[rank]/bs];
2607f6d58c54SBarry Smith   a_jsendbuf = ad->j;
2608f6d58c54SBarry Smith   b_jsendbuf = bd->j;
2609f6d58c54SBarry Smith   n          = A->rmap->rend/bs - A->rmap->rstart/bs;
2610f6d58c54SBarry Smith   cnt        = 0;
2611f6d58c54SBarry Smith   for (i=0; i<n; i++) {
2612f6d58c54SBarry Smith 
2613f6d58c54SBarry Smith     /* put in lower diagonal portion */
2614f6d58c54SBarry Smith     m = bd->i[i+1] - bd->i[i];
2615f6d58c54SBarry Smith     while (m > 0) {
2616f6d58c54SBarry Smith       /* is it above diagonal (in bd (compressed) numbering) */
2617f6d58c54SBarry Smith       if (garray[*b_jsendbuf] > A->rmap->rstart/bs + i) break;
2618f6d58c54SBarry Smith       jsendbuf[cnt++] = garray[*b_jsendbuf++];
2619f6d58c54SBarry Smith       m--;
2620f6d58c54SBarry Smith     }
2621f6d58c54SBarry Smith 
2622f6d58c54SBarry Smith     /* put in diagonal portion */
2623f6d58c54SBarry Smith     for (j=ad->i[i]; j<ad->i[i+1]; j++) {
2624f6d58c54SBarry Smith       jsendbuf[cnt++] = A->rmap->rstart/bs + *a_jsendbuf++;
2625f6d58c54SBarry Smith     }
2626f6d58c54SBarry Smith 
2627f6d58c54SBarry Smith     /* put in upper diagonal portion */
2628f6d58c54SBarry Smith     while (m-- > 0) {
2629f6d58c54SBarry Smith       jsendbuf[cnt++] = garray[*b_jsendbuf++];
2630f6d58c54SBarry Smith     }
2631f6d58c54SBarry Smith   }
2632e32f2f54SBarry Smith   if (cnt != sendcount) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupted PETSc matrix: nz given %D actual nz %D",sendcount,cnt);
2633f6d58c54SBarry Smith 
2634f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2635f6d58c54SBarry Smith     Gather all column indices to all processors
2636f6d58c54SBarry Smith   */
2637f6d58c54SBarry Smith   for (i=0; i<size; i++) {
2638f6d58c54SBarry Smith     recvcounts[i] = 0;
2639f6d58c54SBarry Smith     for (j=A->rmap->range[i]/bs; j<A->rmap->range[i+1]/bs; j++) {
2640f6d58c54SBarry Smith       recvcounts[i] += lens[j];
2641f6d58c54SBarry Smith     }
2642f6d58c54SBarry Smith   }
2643f6d58c54SBarry Smith   displs[0]  = 0;
2644f6d58c54SBarry Smith   for (i=1; i<size; i++) {
2645f6d58c54SBarry Smith     displs[i] = displs[i-1] + recvcounts[i-1];
2646f6d58c54SBarry Smith   }
2647f6d58c54SBarry Smith #if defined(PETSC_HAVE_MPI_IN_PLACE)
2648f6d58c54SBarry Smith   ierr = MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,b->j,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr);
2649f6d58c54SBarry Smith #else
2650f6d58c54SBarry Smith   ierr = MPI_Allgatherv(jsendbuf,sendcount,MPIU_INT,b->j,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr);
2651f6d58c54SBarry Smith #endif
2652f6d58c54SBarry Smith   /*--------------------------------------------------------------------
2653f6d58c54SBarry Smith     Assemble the matrix into useable form (note numerical values not yet set)
2654f6d58c54SBarry Smith   */
2655f6d58c54SBarry Smith   /* set the b->ilen (length of each row) values */
2656f6d58c54SBarry Smith   ierr = PetscMemcpy(b->ilen,lens,(A->rmap->N/bs)*sizeof(PetscInt));CHKERRQ(ierr);
2657f6d58c54SBarry Smith   /* set the b->i indices */
2658f6d58c54SBarry Smith   b->i[0] = 0;
2659f6d58c54SBarry Smith   for (i=1; i<=A->rmap->N/bs; i++) {
2660f6d58c54SBarry Smith     b->i[i] = b->i[i-1] + lens[i-1];
2661f6d58c54SBarry Smith   }
2662f6d58c54SBarry Smith   ierr = PetscFree(lens);CHKERRQ(ierr);
2663f6d58c54SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2664f6d58c54SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2665f6d58c54SBarry Smith   ierr = PetscFree(recvcounts);CHKERRQ(ierr);
2666f6d58c54SBarry Smith 
2667f6d58c54SBarry Smith   if (A->symmetric){
2668f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
2669f6d58c54SBarry Smith   } else if (A->hermitian) {
2670f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr);
2671f6d58c54SBarry Smith   } else if (A->structurally_symmetric) {
2672f6d58c54SBarry Smith     ierr = MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
2673f6d58c54SBarry Smith   }
2674f6d58c54SBarry Smith   *newmat = B;
2675f6d58c54SBarry Smith   PetscFunctionReturn(0);
2676f6d58c54SBarry Smith }
2677f6d58c54SBarry Smith 
2678b1a666ecSBarry Smith #undef __FUNCT__
2679b1a666ecSBarry Smith #define __FUNCT__ "MatSOR_MPIBAIJ"
2680b1a666ecSBarry Smith PetscErrorCode MatSOR_MPIBAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
2681b1a666ecSBarry Smith {
2682b1a666ecSBarry Smith   Mat_MPIBAIJ    *mat = (Mat_MPIBAIJ*)matin->data;
2683b1a666ecSBarry Smith   PetscErrorCode ierr;
2684b1a666ecSBarry Smith   Vec            bb1 = 0;
2685b1a666ecSBarry Smith 
2686b1a666ecSBarry Smith   PetscFunctionBegin;
2687b1a666ecSBarry Smith   if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS) {
2688b1a666ecSBarry Smith     ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr);
2689b1a666ecSBarry Smith   }
2690b1a666ecSBarry Smith 
2691b1a666ecSBarry Smith   if (flag == SOR_APPLY_UPPER) {
2692b1a666ecSBarry Smith     ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2693b1a666ecSBarry Smith     PetscFunctionReturn(0);
2694b1a666ecSBarry Smith   }
2695b1a666ecSBarry Smith 
2696b1a666ecSBarry Smith   if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
2697b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2698b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2699b1a666ecSBarry Smith       its--;
2700b1a666ecSBarry Smith     }
2701b1a666ecSBarry Smith 
2702b1a666ecSBarry Smith     while (its--) {
2703b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2704b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2705b1a666ecSBarry Smith 
2706b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2707b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2708b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2709b1a666ecSBarry Smith 
2710b1a666ecSBarry Smith       /* local sweep */
2711b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2712b1a666ecSBarry Smith     }
2713b1a666ecSBarry Smith   } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
2714b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2715b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2716b1a666ecSBarry Smith       its--;
2717b1a666ecSBarry Smith     }
2718b1a666ecSBarry Smith     while (its--) {
2719b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2720b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2721b1a666ecSBarry Smith 
2722b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2723b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2724b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2725b1a666ecSBarry Smith 
2726b1a666ecSBarry Smith       /* local sweep */
2727b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2728b1a666ecSBarry Smith     }
2729b1a666ecSBarry Smith   } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
2730b1a666ecSBarry Smith     if (flag & SOR_ZERO_INITIAL_GUESS) {
2731b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr);
2732b1a666ecSBarry Smith       its--;
2733b1a666ecSBarry Smith     }
2734b1a666ecSBarry Smith     while (its--) {
2735b1a666ecSBarry Smith       ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2736b1a666ecSBarry Smith       ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2737b1a666ecSBarry Smith 
2738b1a666ecSBarry Smith       /* update rhs: bb1 = bb - B*x */
2739b1a666ecSBarry Smith       ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr);
2740b1a666ecSBarry Smith       ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr);
2741b1a666ecSBarry Smith 
2742b1a666ecSBarry Smith       /* local sweep */
2743b1a666ecSBarry Smith       ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr);
2744b1a666ecSBarry Smith     }
2745e7e72b3dSBarry Smith   } else SETERRQ(((PetscObject)matin)->comm,PETSC_ERR_SUP,"Parallel version of SOR requested not supported");
2746b1a666ecSBarry Smith 
27476bf464f9SBarry Smith   ierr = VecDestroy(&bb1);CHKERRQ(ierr);
2748b1a666ecSBarry Smith   PetscFunctionReturn(0);
2749b1a666ecSBarry Smith }
2750b1a666ecSBarry Smith 
27517087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_BAIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2752f6d58c54SBarry Smith 
2753bbead8a2SBarry Smith #undef __FUNCT__
2754bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_MPIBAIJ"
2755bbead8a2SBarry Smith PetscErrorCode  MatInvertBlockDiagonal_MPIBAIJ(Mat A,PetscScalar **values)
2756bbead8a2SBarry Smith {
2757bbead8a2SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*) A->data;
2758bbead8a2SBarry Smith   PetscErrorCode ierr;
2759bbead8a2SBarry Smith 
2760bbead8a2SBarry Smith   PetscFunctionBegin;
2761bbead8a2SBarry Smith   ierr = MatInvertBlockDiagonal(a->A,values);CHKERRQ(ierr);
2762bbead8a2SBarry Smith   PetscFunctionReturn(0);
2763bbead8a2SBarry Smith }
2764bbead8a2SBarry Smith 
27658c7482ecSBarry Smith 
276679bdfe76SSatish Balay /* -------------------------------------------------------------------*/
2767cc2dc46cSBarry Smith static struct _MatOps MatOps_Values = {
2768cc2dc46cSBarry Smith        MatSetValues_MPIBAIJ,
2769cc2dc46cSBarry Smith        MatGetRow_MPIBAIJ,
2770cc2dc46cSBarry Smith        MatRestoreRow_MPIBAIJ,
2771cc2dc46cSBarry Smith        MatMult_MPIBAIJ,
277297304618SKris Buschelman /* 4*/ MatMultAdd_MPIBAIJ,
27737c922b88SBarry Smith        MatMultTranspose_MPIBAIJ,
27747c922b88SBarry Smith        MatMultTransposeAdd_MPIBAIJ,
2775cc2dc46cSBarry Smith        0,
2776cc2dc46cSBarry Smith        0,
2777cc2dc46cSBarry Smith        0,
277897304618SKris Buschelman /*10*/ 0,
2779cc2dc46cSBarry Smith        0,
2780cc2dc46cSBarry Smith        0,
2781b1a666ecSBarry Smith        MatSOR_MPIBAIJ,
2782cc2dc46cSBarry Smith        MatTranspose_MPIBAIJ,
278397304618SKris Buschelman /*15*/ MatGetInfo_MPIBAIJ,
27847fc3c18eSBarry Smith        MatEqual_MPIBAIJ,
2785cc2dc46cSBarry Smith        MatGetDiagonal_MPIBAIJ,
2786cc2dc46cSBarry Smith        MatDiagonalScale_MPIBAIJ,
2787cc2dc46cSBarry Smith        MatNorm_MPIBAIJ,
278897304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIBAIJ,
2789cc2dc46cSBarry Smith        MatAssemblyEnd_MPIBAIJ,
2790cc2dc46cSBarry Smith        MatSetOption_MPIBAIJ,
2791cc2dc46cSBarry Smith        MatZeroEntries_MPIBAIJ,
2792d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIBAIJ,
2793cc2dc46cSBarry Smith        0,
2794cc2dc46cSBarry Smith        0,
2795cc2dc46cSBarry Smith        0,
2796cc2dc46cSBarry Smith        0,
2797*4994cf47SJed Brown /*29*/ MatSetUp_MPIBAIJ,
2798273d9f13SBarry Smith        0,
2799cc2dc46cSBarry Smith        0,
2800cc2dc46cSBarry Smith        0,
2801cc2dc46cSBarry Smith        0,
2802d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIBAIJ,
2803cc2dc46cSBarry Smith        0,
2804cc2dc46cSBarry Smith        0,
2805cc2dc46cSBarry Smith        0,
2806cc2dc46cSBarry Smith        0,
2807d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIBAIJ,
2808cc2dc46cSBarry Smith        MatGetSubMatrices_MPIBAIJ,
2809cc2dc46cSBarry Smith        MatIncreaseOverlap_MPIBAIJ,
2810cc2dc46cSBarry Smith        MatGetValues_MPIBAIJ,
28113c896bc6SHong Zhang        MatCopy_MPIBAIJ,
2812d519adbfSMatthew Knepley /*44*/ 0,
2813cc2dc46cSBarry Smith        MatScale_MPIBAIJ,
2814cc2dc46cSBarry Smith        0,
2815cc2dc46cSBarry Smith        0,
2816cc2dc46cSBarry Smith        0,
281741c166b1SJed Brown /*49*/ MatSetBlockSize_MPIBAIJ,
2818cc2dc46cSBarry Smith        0,
2819cc2dc46cSBarry Smith        0,
2820cc2dc46cSBarry Smith        0,
2821cc2dc46cSBarry Smith        0,
2822f6d58c54SBarry Smith /*54*/ MatFDColoringCreate_MPIBAIJ,
2823cc2dc46cSBarry Smith        0,
2824cc2dc46cSBarry Smith        MatSetUnfactored_MPIBAIJ,
282582094794SBarry Smith        MatPermute_MPIBAIJ,
2826cc2dc46cSBarry Smith        MatSetValuesBlocked_MPIBAIJ,
2827d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIBAIJ,
2828f14a1c24SBarry Smith        MatDestroy_MPIBAIJ,
2829f14a1c24SBarry Smith        MatView_MPIBAIJ,
2830357abbc8SBarry Smith        0,
28317843d17aSBarry Smith        0,
2832d519adbfSMatthew Knepley /*64*/ 0,
28337843d17aSBarry Smith        0,
28347843d17aSBarry Smith        0,
28357843d17aSBarry Smith        0,
28367843d17aSBarry Smith        0,
2837d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIBAIJ,
28387843d17aSBarry Smith        0,
283997304618SKris Buschelman        0,
284097304618SKris Buschelman        0,
284197304618SKris Buschelman        0,
2842d519adbfSMatthew Knepley /*74*/ 0,
2843f6d58c54SBarry Smith        MatFDColoringApply_BAIJ,
284497304618SKris Buschelman        0,
284597304618SKris Buschelman        0,
284697304618SKris Buschelman        0,
2847d519adbfSMatthew Knepley /*79*/ 0,
284897304618SKris Buschelman        0,
284997304618SKris Buschelman        0,
285097304618SKris Buschelman        0,
28515bba2384SShri Abhyankar        MatLoad_MPIBAIJ,
2852d519adbfSMatthew Knepley /*84*/ 0,
2853865e5f61SKris Buschelman        0,
2854865e5f61SKris Buschelman        0,
2855865e5f61SKris Buschelman        0,
2856865e5f61SKris Buschelman        0,
2857d519adbfSMatthew Knepley /*89*/ 0,
2858865e5f61SKris Buschelman        0,
2859865e5f61SKris Buschelman        0,
2860865e5f61SKris Buschelman        0,
2861865e5f61SKris Buschelman        0,
2862d519adbfSMatthew Knepley /*94*/ 0,
2863865e5f61SKris Buschelman        0,
2864865e5f61SKris Buschelman        0,
286599cafbc1SBarry Smith        0,
286699cafbc1SBarry Smith        0,
2867d519adbfSMatthew Knepley /*99*/ 0,
286899cafbc1SBarry Smith        0,
286999cafbc1SBarry Smith        0,
287099cafbc1SBarry Smith        0,
287199cafbc1SBarry Smith        0,
2872d519adbfSMatthew Knepley /*104*/0,
287399cafbc1SBarry Smith        MatRealPart_MPIBAIJ,
28748c7482ecSBarry Smith        MatImaginaryPart_MPIBAIJ,
28758c7482ecSBarry Smith        0,
28768c7482ecSBarry Smith        0,
2877d519adbfSMatthew Knepley /*109*/0,
28788c7482ecSBarry Smith        0,
28798c7482ecSBarry Smith        0,
28808c7482ecSBarry Smith        0,
28818c7482ecSBarry Smith        0,
2882d1adec66SJed Brown /*114*/MatGetSeqNonzeroStructure_MPIBAIJ,
28838c7482ecSBarry Smith        0,
28844683f7a4SShri Abhyankar        MatGetGhosts_MPIBAIJ,
28854683f7a4SShri Abhyankar        0,
28864683f7a4SShri Abhyankar        0,
28874683f7a4SShri Abhyankar /*119*/0,
28884683f7a4SShri Abhyankar        0,
28894683f7a4SShri Abhyankar        0,
2890bbead8a2SBarry Smith        0,
2891bbead8a2SBarry Smith        0,
2892bbead8a2SBarry Smith /*124*/0,
2893bbead8a2SBarry Smith        0,
2894bbead8a2SBarry Smith        MatInvertBlockDiagonal_MPIBAIJ
28958c7482ecSBarry Smith };
289679bdfe76SSatish Balay 
2897e18c124aSSatish Balay EXTERN_C_BEGIN
28984a2ae208SSatish Balay #undef __FUNCT__
28994a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonalBlock_MPIBAIJ"
290011bd1e4dSLisandro Dalcin PetscErrorCode  MatGetDiagonalBlock_MPIBAIJ(Mat A,Mat *a)
29015ef9f2a5SBarry Smith {
29025ef9f2a5SBarry Smith   PetscFunctionBegin;
29035ef9f2a5SBarry Smith   *a = ((Mat_MPIBAIJ *)A->data)->A;
29045ef9f2a5SBarry Smith   PetscFunctionReturn(0);
29055ef9f2a5SBarry Smith }
2906e18c124aSSatish Balay EXTERN_C_END
290779bdfe76SSatish Balay 
2908273d9f13SBarry Smith EXTERN_C_BEGIN
29097087cfbeSBarry Smith extern PetscErrorCode  MatConvert_MPIBAIJ_MPISBAIJ(Mat, MatType,MatReuse,Mat*);
2910d94109b8SHong Zhang EXTERN_C_END
2911d94109b8SHong Zhang 
2912b8d659d7SLisandro Dalcin EXTERN_C_BEGIN
2913aac34f13SBarry Smith #undef __FUNCT__
2914aac34f13SBarry Smith #define __FUNCT__ "MatMPIBAIJSetPreallocationCSR_MPIBAIJ"
2915cf12db73SBarry Smith PetscErrorCode MatMPIBAIJSetPreallocationCSR_MPIBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
2916aac34f13SBarry Smith {
2917b8d659d7SLisandro Dalcin   PetscInt       m,rstart,cstart,cend;
2918b8d659d7SLisandro Dalcin   PetscInt       i,j,d,nz,nz_max=0,*d_nnz=0,*o_nnz=0;
2919b8d659d7SLisandro Dalcin   const PetscInt *JJ=0;
2920b8d659d7SLisandro Dalcin   PetscScalar    *values=0;
2921aac34f13SBarry Smith   PetscErrorCode ierr;
2922aac34f13SBarry Smith 
2923aac34f13SBarry Smith   PetscFunctionBegin;
2924b8d659d7SLisandro Dalcin 
292565e19b50SBarry Smith   if (bs < 1) SETERRQ1(((PetscObject)B)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
292626283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
292726283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
292826283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
292926283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2930d0f46423SBarry Smith   m      = B->rmap->n/bs;
2931d0f46423SBarry Smith   rstart = B->rmap->rstart/bs;
2932d0f46423SBarry Smith   cstart = B->cmap->rstart/bs;
2933d0f46423SBarry Smith   cend   = B->cmap->rend/bs;
2934b8d659d7SLisandro Dalcin 
2935e32f2f54SBarry Smith   if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
2936fca92195SBarry Smith   ierr  = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr);
2937aac34f13SBarry Smith   for (i=0; i<m; i++) {
2938cf12db73SBarry Smith     nz = ii[i+1] - ii[i];
2939e32f2f54SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative number of columns %D",i,nz);
2940b8d659d7SLisandro Dalcin     nz_max = PetscMax(nz_max,nz);
2941cf12db73SBarry Smith     JJ  = jj + ii[i];
2942b8d659d7SLisandro Dalcin     for (j=0; j<nz; j++) {
2943aac34f13SBarry Smith       if (*JJ >= cstart) break;
2944aac34f13SBarry Smith       JJ++;
2945aac34f13SBarry Smith     }
2946aac34f13SBarry Smith     d = 0;
2947b8d659d7SLisandro Dalcin     for (; j<nz; j++) {
2948aac34f13SBarry Smith       if (*JJ++ >= cend) break;
2949aac34f13SBarry Smith       d++;
2950aac34f13SBarry Smith     }
2951aac34f13SBarry Smith     d_nnz[i] = d;
2952b8d659d7SLisandro Dalcin     o_nnz[i] = nz - d;
2953aac34f13SBarry Smith   }
2954aac34f13SBarry Smith   ierr = MatMPIBAIJSetPreallocation(B,bs,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
2955fca92195SBarry Smith   ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr);
2956aac34f13SBarry Smith 
2957b8d659d7SLisandro Dalcin   values = (PetscScalar*)V;
2958b8d659d7SLisandro Dalcin   if (!values) {
2959fca92195SBarry Smith     ierr = PetscMalloc(bs*bs*nz_max*sizeof(PetscScalar),&values);CHKERRQ(ierr);
2960b8d659d7SLisandro Dalcin     ierr = PetscMemzero(values,bs*bs*nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
2961b8d659d7SLisandro Dalcin   }
2962b8d659d7SLisandro Dalcin   for (i=0; i<m; i++) {
2963b8d659d7SLisandro Dalcin     PetscInt          row    = i + rstart;
2964cf12db73SBarry Smith     PetscInt          ncols  = ii[i+1] - ii[i];
2965cf12db73SBarry Smith     const PetscInt    *icols = jj + ii[i];
2966cf12db73SBarry Smith     const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
2967b8d659d7SLisandro Dalcin     ierr = MatSetValuesBlocked_MPIBAIJ(B,1,&row,ncols,icols,svals,INSERT_VALUES);CHKERRQ(ierr);
2968aac34f13SBarry Smith   }
2969aac34f13SBarry Smith 
2970b8d659d7SLisandro Dalcin   if (!V) { ierr = PetscFree(values);CHKERRQ(ierr); }
2971aac34f13SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2972aac34f13SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
29737827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
2974aac34f13SBarry Smith   PetscFunctionReturn(0);
2975aac34f13SBarry Smith }
2976b8d659d7SLisandro Dalcin EXTERN_C_END
2977aac34f13SBarry Smith 
2978aac34f13SBarry Smith #undef __FUNCT__
2979aac34f13SBarry Smith #define __FUNCT__ "MatMPIBAIJSetPreallocationCSR"
2980aac34f13SBarry Smith /*@C
2981dfb205c3SBarry Smith    MatMPIBAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in BAIJ format
2982aac34f13SBarry Smith    (the default parallel PETSc format).
2983aac34f13SBarry Smith 
2984aac34f13SBarry Smith    Collective on MPI_Comm
2985aac34f13SBarry Smith 
2986aac34f13SBarry Smith    Input Parameters:
2987aac34f13SBarry Smith +  A - the matrix
2988dfb205c3SBarry Smith .  bs - the block size
2989aac34f13SBarry Smith .  i - the indices into j for the start of each local row (starts with zero)
2990aac34f13SBarry Smith .  j - the column indices for each local row (starts with zero) these must be sorted for each row
2991aac34f13SBarry Smith -  v - optional values in the matrix
2992aac34f13SBarry Smith 
2993aac34f13SBarry Smith    Level: developer
2994aac34f13SBarry Smith 
2995aac34f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
2996aac34f13SBarry Smith 
2997aac34f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIBAIJSetPreallocation(), MatCreateMPIAIJ(), MPIAIJ
2998aac34f13SBarry Smith @*/
29997087cfbeSBarry Smith PetscErrorCode  MatMPIBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3000aac34f13SBarry Smith {
30014ac538c5SBarry Smith   PetscErrorCode ierr;
3002aac34f13SBarry Smith 
3003aac34f13SBarry Smith   PetscFunctionBegin;
30046ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
30056ba663aaSJed Brown   PetscValidType(B,1);
30066ba663aaSJed Brown   PetscValidLogicalCollectiveInt(B,bs,2);
30074ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatMPIBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));CHKERRQ(ierr);
3008aac34f13SBarry Smith   PetscFunctionReturn(0);
3009aac34f13SBarry Smith }
3010aac34f13SBarry Smith 
3011d94109b8SHong Zhang EXTERN_C_BEGIN
30124a2ae208SSatish Balay #undef __FUNCT__
3013a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIBAIJSetPreallocation_MPIBAIJ"
30147087cfbeSBarry Smith PetscErrorCode  MatMPIBAIJSetPreallocation_MPIBAIJ(Mat B,PetscInt bs,PetscInt d_nz,PetscInt *d_nnz,PetscInt o_nz,PetscInt *o_nnz)
3015a23d5eceSKris Buschelman {
3016a23d5eceSKris Buschelman   Mat_MPIBAIJ    *b;
3017dfbe8321SBarry Smith   PetscErrorCode ierr;
3018db4efbfdSBarry Smith   PetscInt       i, newbs = PetscAbs(bs);
30192576faa2SJed Brown   PetscBool      d_realalloc = PETSC_FALSE,o_realalloc = PETSC_FALSE;
3020a23d5eceSKris Buschelman 
3021a23d5eceSKris Buschelman   PetscFunctionBegin;
30222576faa2SJed Brown   if (d_nz >= 0 || d_nnz) d_realalloc = PETSC_TRUE;
30232576faa2SJed Brown   if (o_nz >= 0 || o_nnz) o_realalloc = PETSC_TRUE;
3024db4efbfdSBarry Smith   if (bs < 0) {
30257adad957SLisandro Dalcin     ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Options for MPIBAIJ matrix","Mat");CHKERRQ(ierr);
3026db4efbfdSBarry Smith       ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatMPIBAIJSetPreallocation",newbs,&newbs,PETSC_NULL);CHKERRQ(ierr);
30278c07d4e3SBarry Smith     ierr = PetscOptionsEnd();CHKERRQ(ierr);
3028db4efbfdSBarry Smith     bs   = PetscAbs(bs);
3029db4efbfdSBarry Smith   }
3030e7e72b3dSBarry Smith   if ((d_nnz || o_nnz) && newbs != bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot change blocksize from command line if setting d_nnz or o_nnz");
3031db4efbfdSBarry Smith   bs = newbs;
3032db4efbfdSBarry Smith 
3033a23d5eceSKris Buschelman 
3034e7e72b3dSBarry Smith   if (bs < 1) SETERRQ(((PetscObject)B)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive");
3035a23d5eceSKris Buschelman   if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
3036a23d5eceSKris Buschelman   if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
3037e32f2f54SBarry Smith   if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz);
3038e32f2f54SBarry Smith   if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz);
3039899cda47SBarry Smith 
304026283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
304126283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
304226283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
304326283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3044899cda47SBarry Smith 
3045a23d5eceSKris Buschelman   if (d_nnz) {
3046d0f46423SBarry Smith     for (i=0; i<B->rmap->n/bs; i++) {
3047e32f2f54SBarry 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]);
3048a23d5eceSKris Buschelman     }
3049a23d5eceSKris Buschelman   }
3050a23d5eceSKris Buschelman   if (o_nnz) {
3051d0f46423SBarry Smith     for (i=0; i<B->rmap->n/bs; i++) {
3052e32f2f54SBarry 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]);
3053a23d5eceSKris Buschelman     }
3054a23d5eceSKris Buschelman   }
3055a23d5eceSKris Buschelman 
3056a23d5eceSKris Buschelman   b = (Mat_MPIBAIJ*)B->data;
3057a23d5eceSKris Buschelman   b->bs2 = bs*bs;
3058d0f46423SBarry Smith   b->mbs = B->rmap->n/bs;
3059d0f46423SBarry Smith   b->nbs = B->cmap->n/bs;
3060d0f46423SBarry Smith   b->Mbs = B->rmap->N/bs;
3061d0f46423SBarry Smith   b->Nbs = B->cmap->N/bs;
3062a23d5eceSKris Buschelman 
3063a23d5eceSKris Buschelman   for (i=0; i<=b->size; i++) {
3064d0f46423SBarry Smith     b->rangebs[i] = B->rmap->range[i]/bs;
3065a23d5eceSKris Buschelman   }
3066d0f46423SBarry Smith   b->rstartbs = B->rmap->rstart/bs;
3067d0f46423SBarry Smith   b->rendbs   = B->rmap->rend/bs;
3068d0f46423SBarry Smith   b->cstartbs = B->cmap->rstart/bs;
3069d0f46423SBarry Smith   b->cendbs   = B->cmap->rend/bs;
3070a23d5eceSKris Buschelman 
3071526dfc15SBarry Smith   if (!B->preallocated) {
3072f69a0ea3SMatthew Knepley     ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr);
3073d0f46423SBarry Smith     ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr);
30749c097c71SKris Buschelman     ierr = MatSetType(b->A,MATSEQBAIJ);CHKERRQ(ierr);
307552e6d16bSBarry Smith     ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr);
3076f69a0ea3SMatthew Knepley     ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr);
3077d0f46423SBarry Smith     ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr);
30789c097c71SKris Buschelman     ierr = MatSetType(b->B,MATSEQBAIJ);CHKERRQ(ierr);
307952e6d16bSBarry Smith     ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr);
30807adad957SLisandro Dalcin     ierr = MatStashCreate_Private(((PetscObject)B)->comm,bs,&B->bstash);CHKERRQ(ierr);
3081526dfc15SBarry Smith   }
3082a23d5eceSKris Buschelman 
3083526dfc15SBarry Smith   ierr = MatSeqBAIJSetPreallocation(b->A,bs,d_nz,d_nnz);CHKERRQ(ierr);
3084526dfc15SBarry Smith   ierr = MatSeqBAIJSetPreallocation(b->B,bs,o_nz,o_nnz);CHKERRQ(ierr);
30852576faa2SJed Brown   /* Do not error if the user did not give real preallocation information. Ugly because this would overwrite a previous user call to MatSetOption(). */
30862576faa2SJed Brown   if (!d_realalloc) {ierr = MatSetOption(b->A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);}
30872576faa2SJed Brown   if (!o_realalloc) {ierr = MatSetOption(b->B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);}
3088526dfc15SBarry Smith   B->preallocated = PETSC_TRUE;
3089a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3090a23d5eceSKris Buschelman }
3091a23d5eceSKris Buschelman EXTERN_C_END
3092a23d5eceSKris Buschelman 
3093a23d5eceSKris Buschelman EXTERN_C_BEGIN
30947087cfbeSBarry Smith extern PetscErrorCode  MatDiagonalScaleLocal_MPIBAIJ(Mat,Vec);
30957087cfbeSBarry Smith extern PetscErrorCode  MatSetHashTableFactor_MPIBAIJ(Mat,PetscReal);
309692b32695SKris Buschelman EXTERN_C_END
30975bf65638SKris Buschelman 
309882094794SBarry Smith 
309982094794SBarry Smith EXTERN_C_BEGIN
310082094794SBarry Smith #undef __FUNCT__
310182094794SBarry Smith #define __FUNCT__ "MatConvert_MPIBAIJ_MPIAdj"
31027087cfbeSBarry Smith PetscErrorCode  MatConvert_MPIBAIJ_MPIAdj(Mat B, const MatType newtype,MatReuse reuse,Mat *adj)
310382094794SBarry Smith {
310482094794SBarry Smith   Mat_MPIBAIJ    *b = (Mat_MPIBAIJ*)B->data;
310582094794SBarry Smith   PetscErrorCode ierr;
310682094794SBarry Smith   Mat_SeqBAIJ    *d = (Mat_SeqBAIJ*) b->A->data,*o = (Mat_SeqBAIJ*) b->B->data;
310782094794SBarry Smith   PetscInt       M = B->rmap->n/B->rmap->bs,i,*ii,*jj,cnt,j,k,rstart = B->rmap->rstart/B->rmap->bs;
310882094794SBarry Smith   const PetscInt *id = d->i, *jd = d->j, *io = o->i, *jo = o->j, *garray = b->garray;
310982094794SBarry Smith 
311082094794SBarry Smith   PetscFunctionBegin;
311182094794SBarry Smith   ierr = PetscMalloc((M+1)*sizeof(PetscInt),&ii);CHKERRQ(ierr);
311282094794SBarry Smith   ii[0] = 0;
311382094794SBarry Smith   CHKMEMQ;
311482094794SBarry Smith   for (i=0; i<M; i++) {
3115e32f2f54SBarry 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]);
3116e32f2f54SBarry 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]);
311782094794SBarry Smith     ii[i+1] = ii[i] + id[i+1] - id[i] + io[i+1] - io[i];
311882094794SBarry Smith     /* remove one from count of matrix has diagonal */
311982094794SBarry Smith     for (j=id[i]; j<id[i+1]; j++) {
312082094794SBarry Smith       if (jd[j] == i) {ii[i+1]--;break;}
312182094794SBarry Smith     }
312282094794SBarry Smith   CHKMEMQ;
312382094794SBarry Smith   }
312482094794SBarry Smith   ierr = PetscMalloc(ii[M]*sizeof(PetscInt),&jj);CHKERRQ(ierr);
312582094794SBarry Smith   cnt = 0;
312682094794SBarry Smith   for (i=0; i<M; i++) {
312782094794SBarry Smith     for (j=io[i]; j<io[i+1]; j++) {
312882094794SBarry Smith       if (garray[jo[j]] > rstart) break;
312982094794SBarry Smith       jj[cnt++] = garray[jo[j]];
313082094794SBarry Smith   CHKMEMQ;
313182094794SBarry Smith     }
313282094794SBarry Smith     for (k=id[i]; k<id[i+1]; k++) {
313382094794SBarry Smith       if (jd[k] != i) {
313482094794SBarry Smith         jj[cnt++] = rstart + jd[k];
313582094794SBarry Smith   CHKMEMQ;
313682094794SBarry Smith       }
313782094794SBarry Smith     }
313882094794SBarry Smith     for (;j<io[i+1]; j++) {
313982094794SBarry Smith       jj[cnt++] = garray[jo[j]];
314082094794SBarry Smith   CHKMEMQ;
314182094794SBarry Smith     }
314282094794SBarry Smith   }
314382094794SBarry Smith   ierr = MatCreateMPIAdj(((PetscObject)B)->comm,M,B->cmap->N/B->rmap->bs,ii,jj,PETSC_NULL,adj);CHKERRQ(ierr);
314482094794SBarry Smith   PetscFunctionReturn(0);
314582094794SBarry Smith }
3146dbf0e21dSBarry Smith EXTERN_C_END
314782094794SBarry Smith 
3148c6db04a5SJed Brown #include <../src/mat/impls/aij/mpi/mpiaij.h>
31491e66c38aSShri Abhyankar EXTERN_C_BEGIN
31507087cfbeSBarry Smith PetscErrorCode  MatConvert_SeqBAIJ_SeqAIJ(Mat,const MatType,MatReuse,Mat*);
31511e66c38aSShri Abhyankar EXTERN_C_END
315262471d69SBarry Smith 
315362471d69SBarry Smith EXTERN_C_BEGIN
315462471d69SBarry Smith #undef __FUNCT__
315562471d69SBarry Smith #define __FUNCT__ "MatConvert_MPIBAIJ_MPIAIJ"
31567087cfbeSBarry Smith PetscErrorCode  MatConvert_MPIBAIJ_MPIAIJ(Mat A,const MatType newtype,MatReuse reuse,Mat *newmat)
315762471d69SBarry Smith {
315862471d69SBarry Smith   PetscErrorCode ierr;
315962471d69SBarry Smith   Mat_MPIBAIJ    *a = (Mat_MPIBAIJ*)A->data;
316062471d69SBarry Smith   Mat            B;
316185a69837SSatish Balay   Mat_MPIAIJ     *b;
316262471d69SBarry Smith 
316362471d69SBarry Smith   PetscFunctionBegin;
316462471d69SBarry Smith   if (!A->assembled) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Matrix must be assembled");
316562471d69SBarry Smith 
316662471d69SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr);
316762471d69SBarry Smith   ierr = MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);CHKERRQ(ierr);
31686d0a4a0eSHong Zhang   ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr);
31696d0a4a0eSHong Zhang   ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr);
317062471d69SBarry Smith   ierr = MatMPIAIJSetPreallocation(B,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr);
317162471d69SBarry Smith   b = (Mat_MPIAIJ*) B->data;
317262471d69SBarry Smith 
31736bf464f9SBarry Smith   ierr = MatDestroy(&b->A);CHKERRQ(ierr);
31746bf464f9SBarry Smith   ierr = MatDestroy(&b->B);CHKERRQ(ierr);
31756a719282SBarry Smith   ierr = DisAssemble_MPIBAIJ(A);CHKERRQ(ierr);
317662471d69SBarry Smith   ierr = MatConvert_SeqBAIJ_SeqAIJ(a->A, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->A);CHKERRQ(ierr);
317762471d69SBarry Smith   ierr = MatConvert_SeqBAIJ_SeqAIJ(a->B, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->B);CHKERRQ(ierr);
317862471d69SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
317962471d69SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
31806a719282SBarry Smith   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
31816a719282SBarry Smith   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
318262471d69SBarry Smith   if (reuse == MAT_REUSE_MATRIX) {
318362471d69SBarry Smith     ierr = MatHeaderReplace(A,B);CHKERRQ(ierr);
318462471d69SBarry Smith   } else {
318562471d69SBarry Smith    *newmat = B;
318662471d69SBarry Smith   }
318762471d69SBarry Smith   PetscFunctionReturn(0);
318862471d69SBarry Smith }
318962471d69SBarry Smith EXTERN_C_END
319062471d69SBarry Smith 
3191450b117fSShri Abhyankar EXTERN_C_BEGIN
3192450b117fSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
3193bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_baij_mumps(Mat,MatFactorType,Mat*);
3194450b117fSShri Abhyankar #endif
3195450b117fSShri Abhyankar EXTERN_C_END
3196450b117fSShri Abhyankar 
31970bad9183SKris Buschelman /*MC
3198fafad747SKris Buschelman    MATMPIBAIJ - MATMPIBAIJ = "mpibaij" - A matrix type to be used for distributed block sparse matrices.
31990bad9183SKris Buschelman 
32000bad9183SKris Buschelman    Options Database Keys:
32018c07d4e3SBarry Smith + -mat_type mpibaij - sets the matrix type to "mpibaij" during a call to MatSetFromOptions()
32028c07d4e3SBarry Smith . -mat_block_size <bs> - set the blocksize used to store the matrix
32038c07d4e3SBarry Smith - -mat_use_hash_table <fact>
32040bad9183SKris Buschelman 
32050bad9183SKris Buschelman   Level: beginner
32060bad9183SKris Buschelman 
32070bad9183SKris Buschelman .seealso: MatCreateMPIBAIJ
32080bad9183SKris Buschelman M*/
32090bad9183SKris Buschelman 
321092b32695SKris Buschelman EXTERN_C_BEGIN
3211c0cdd4a1SDahai Guo extern PetscErrorCode MatConvert_MPIBAIJ_MPIBSTRM(Mat,const MatType,MatReuse,Mat*);
3212c0cdd4a1SDahai Guo EXTERN_C_END
3213c0cdd4a1SDahai Guo 
3214c0cdd4a1SDahai Guo EXTERN_C_BEGIN
3215a23d5eceSKris Buschelman #undef __FUNCT__
32164a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIBAIJ"
32177087cfbeSBarry Smith PetscErrorCode  MatCreate_MPIBAIJ(Mat B)
3218273d9f13SBarry Smith {
3219273d9f13SBarry Smith   Mat_MPIBAIJ    *b;
3220dfbe8321SBarry Smith   PetscErrorCode ierr;
3221ace3abfcSBarry Smith   PetscBool      flg;
3222273d9f13SBarry Smith 
3223273d9f13SBarry Smith   PetscFunctionBegin;
322438f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_MPIBAIJ,&b);CHKERRQ(ierr);
322582502324SSatish Balay   B->data = (void*)b;
322682502324SSatish Balay 
3227273d9f13SBarry Smith   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
3228273d9f13SBarry Smith   B->assembled  = PETSC_FALSE;
3229273d9f13SBarry Smith 
3230273d9f13SBarry Smith   B->insertmode = NOT_SET_VALUES;
32317adad957SLisandro Dalcin   ierr = MPI_Comm_rank(((PetscObject)B)->comm,&b->rank);CHKERRQ(ierr);
32327adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&b->size);CHKERRQ(ierr);
3233273d9f13SBarry Smith 
3234273d9f13SBarry Smith   /* build local table of row and column ownerships */
3235899cda47SBarry Smith   ierr = PetscMalloc((b->size+1)*sizeof(PetscInt),&b->rangebs);CHKERRQ(ierr);
3236273d9f13SBarry Smith 
3237273d9f13SBarry Smith   /* build cache for off array entries formed */
32387adad957SLisandro Dalcin   ierr = MatStashCreate_Private(((PetscObject)B)->comm,1,&B->stash);CHKERRQ(ierr);
3239273d9f13SBarry Smith   b->donotstash  = PETSC_FALSE;
3240273d9f13SBarry Smith   b->colmap      = PETSC_NULL;
3241273d9f13SBarry Smith   b->garray      = PETSC_NULL;
3242273d9f13SBarry Smith   b->roworiented = PETSC_TRUE;
3243273d9f13SBarry Smith 
3244273d9f13SBarry Smith   /* stuff used in block assembly */
3245273d9f13SBarry Smith   b->barray       = 0;
3246273d9f13SBarry Smith 
3247273d9f13SBarry Smith   /* stuff used for matrix vector multiply */
3248273d9f13SBarry Smith   b->lvec         = 0;
3249273d9f13SBarry Smith   b->Mvctx        = 0;
3250273d9f13SBarry Smith 
3251273d9f13SBarry Smith   /* stuff for MatGetRow() */
3252273d9f13SBarry Smith   b->rowindices   = 0;
3253273d9f13SBarry Smith   b->rowvalues    = 0;
3254273d9f13SBarry Smith   b->getrowactive = PETSC_FALSE;
3255273d9f13SBarry Smith 
3256273d9f13SBarry Smith   /* hash table stuff */
3257273d9f13SBarry Smith   b->ht           = 0;
3258273d9f13SBarry Smith   b->hd           = 0;
3259273d9f13SBarry Smith   b->ht_size      = 0;
3260273d9f13SBarry Smith   b->ht_flag      = PETSC_FALSE;
3261273d9f13SBarry Smith   b->ht_fact      = 0;
3262273d9f13SBarry Smith   b->ht_total_ct  = 0;
3263273d9f13SBarry Smith   b->ht_insert_ct = 0;
3264273d9f13SBarry Smith 
32657a868f3eSHong Zhang   /* stuff for MatGetSubMatrices_MPIBAIJ_local() */
32667a868f3eSHong Zhang   b->ijonly       = PETSC_FALSE;
32677a868f3eSHong Zhang 
32687adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)B)->comm,PETSC_NULL,"Options for loading MPIBAIJ matrix 1","Mat");CHKERRQ(ierr);
3269acfcf0e5SJed Brown     ierr = PetscOptionsBool("-mat_use_hash_table","Use hash table to save memory in constructing matrix","MatSetOption",PETSC_FALSE,&flg,PETSC_NULL);CHKERRQ(ierr);
3270273d9f13SBarry Smith     if (flg) {
3271f6275e2eSBarry Smith       PetscReal fact = 1.39;
32724e0d8c25SBarry Smith       ierr = MatSetOption(B,MAT_USE_HASH_TABLE,PETSC_TRUE);CHKERRQ(ierr);
32738c07d4e3SBarry Smith       ierr = PetscOptionsReal("-mat_use_hash_table","Use hash table factor","MatMPIBAIJSetHashTableFactor",fact,&fact,PETSC_NULL);CHKERRQ(ierr);
3274273d9f13SBarry Smith       if (fact <= 1.0) fact = 1.39;
3275273d9f13SBarry Smith       ierr = MatMPIBAIJSetHashTableFactor(B,fact);CHKERRQ(ierr);
32761e2582c4SBarry Smith       ierr = PetscInfo1(B,"Hash table Factor used %5.2f\n",fact);CHKERRQ(ierr);
3277273d9f13SBarry Smith     }
32788c07d4e3SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
32798c07d4e3SBarry Smith 
3280450b117fSShri Abhyankar #if defined(PETSC_HAVE_MUMPS)
3281bccb9932SShri Abhyankar   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", "MatGetFactor_baij_mumps",MatGetFactor_baij_mumps);CHKERRQ(ierr);
3282450b117fSShri Abhyankar #endif
328382094794SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpibaij_mpiadj_C",
328482094794SBarry Smith                                      "MatConvert_MPIBAIJ_MPIAdj",
328582094794SBarry Smith                                       MatConvert_MPIBAIJ_MPIAdj);CHKERRQ(ierr);
328662471d69SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpibaij_mpiaij_C",
328762471d69SBarry Smith                                      "MatConvert_MPIBAIJ_MPIAIJ",
328862471d69SBarry Smith                                       MatConvert_MPIBAIJ_MPIAIJ);CHKERRQ(ierr);
3289ad5247fdSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpibaij_mpisbaij_C",
3290ad5247fdSHong Zhang                                      "MatConvert_MPIBAIJ_MPISBAIJ",
3291ad5247fdSHong Zhang                                       MatConvert_MPIBAIJ_MPISBAIJ);CHKERRQ(ierr);
3292273d9f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3293273d9f13SBarry Smith                                      "MatStoreValues_MPIBAIJ",
3294273d9f13SBarry Smith                                      MatStoreValues_MPIBAIJ);CHKERRQ(ierr);
3295273d9f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3296273d9f13SBarry Smith                                      "MatRetrieveValues_MPIBAIJ",
3297273d9f13SBarry Smith                                      MatRetrieveValues_MPIBAIJ);CHKERRQ(ierr);
3298273d9f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
3299273d9f13SBarry Smith                                      "MatGetDiagonalBlock_MPIBAIJ",
3300273d9f13SBarry Smith                                      MatGetDiagonalBlock_MPIBAIJ);CHKERRQ(ierr);
3301a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIBAIJSetPreallocation_C",
3302a23d5eceSKris Buschelman                                      "MatMPIBAIJSetPreallocation_MPIBAIJ",
3303a23d5eceSKris Buschelman                                      MatMPIBAIJSetPreallocation_MPIBAIJ);CHKERRQ(ierr);
3304aac34f13SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIBAIJSetPreallocationCSR_C",
330544ec7894SLisandro Dalcin 				     "MatMPIBAIJSetPreallocationCSR_MPIBAIJ",
3306aac34f13SBarry Smith 				     MatMPIBAIJSetPreallocationCSR_MPIBAIJ);CHKERRQ(ierr);
330792b32695SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
330892b32695SKris Buschelman                                      "MatDiagonalScaleLocal_MPIBAIJ",
330992b32695SKris Buschelman                                      MatDiagonalScaleLocal_MPIBAIJ);CHKERRQ(ierr);
33105bf65638SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSetHashTableFactor_C",
33115bf65638SKris Buschelman                                      "MatSetHashTableFactor_MPIBAIJ",
33125bf65638SKris Buschelman                                      MatSetHashTableFactor_MPIBAIJ);CHKERRQ(ierr);
3313c0cdd4a1SDahai Guo   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpibaij_mpibstrm_C",
3314c0cdd4a1SDahai Guo                                      "MatConvert_MPIBAIJ_MPIBSTRM",
3315c0cdd4a1SDahai Guo                                       MatConvert_MPIBAIJ_MPIBSTRM);CHKERRQ(ierr);
331617667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIBAIJ);CHKERRQ(ierr);
3317273d9f13SBarry Smith   PetscFunctionReturn(0);
3318273d9f13SBarry Smith }
3319273d9f13SBarry Smith EXTERN_C_END
3320273d9f13SBarry Smith 
3321209238afSKris Buschelman /*MC
3322002d173eSKris Buschelman    MATBAIJ - MATBAIJ = "baij" - A matrix type to be used for block sparse matrices.
3323209238afSKris Buschelman 
3324209238afSKris Buschelman    This matrix type is identical to MATSEQBAIJ when constructed with a single process communicator,
3325209238afSKris Buschelman    and MATMPIBAIJ otherwise.
3326209238afSKris Buschelman 
3327209238afSKris Buschelman    Options Database Keys:
3328209238afSKris Buschelman . -mat_type baij - sets the matrix type to "baij" during a call to MatSetFromOptions()
3329209238afSKris Buschelman 
3330209238afSKris Buschelman   Level: beginner
3331209238afSKris Buschelman 
3332aac34f13SBarry Smith .seealso: MatCreateMPIBAIJ(),MATSEQBAIJ,MATMPIBAIJ, MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
3333209238afSKris Buschelman M*/
3334209238afSKris Buschelman 
33354a2ae208SSatish Balay #undef __FUNCT__
33364a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJSetPreallocation"
3337273d9f13SBarry Smith /*@C
3338aac34f13SBarry Smith    MatMPIBAIJSetPreallocation - Allocates memory for a sparse parallel matrix in block AIJ format
3339273d9f13SBarry Smith    (block compressed row).  For good matrix assembly performance
3340273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameters
3341273d9f13SBarry Smith    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
3342273d9f13SBarry Smith    performance can be increased by more than a factor of 50.
3343273d9f13SBarry Smith 
3344273d9f13SBarry Smith    Collective on Mat
3345273d9f13SBarry Smith 
3346273d9f13SBarry Smith    Input Parameters:
3347273d9f13SBarry Smith +  A - the matrix
3348273d9f13SBarry Smith .  bs   - size of blockk
3349273d9f13SBarry Smith .  d_nz  - number of block nonzeros per block row in diagonal portion of local
3350273d9f13SBarry Smith            submatrix  (same for all local rows)
3351273d9f13SBarry Smith .  d_nnz - array containing the number of block nonzeros in the various block rows
3352273d9f13SBarry Smith            of the in diagonal portion of the local (possibly different for each block
335395742e49SBarry Smith            row) or PETSC_NULL.  If you plan to factor the matrix you must leave room for the diagonal entry and
335495742e49SBarry Smith            set it even if it is zero.
3355273d9f13SBarry Smith .  o_nz  - number of block nonzeros per block row in the off-diagonal portion of local
3356273d9f13SBarry Smith            submatrix (same for all local rows).
3357273d9f13SBarry Smith -  o_nnz - array containing the number of nonzeros in the various block rows of the
3358273d9f13SBarry Smith            off-diagonal portion of the local submatrix (possibly different for
3359273d9f13SBarry Smith            each block row) or PETSC_NULL.
3360273d9f13SBarry Smith 
336149a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
3362273d9f13SBarry Smith 
3363273d9f13SBarry Smith    Options Database Keys:
33648c07d4e3SBarry Smith +   -mat_block_size - size of the blocks to use
33658c07d4e3SBarry Smith -   -mat_use_hash_table <fact>
3366273d9f13SBarry Smith 
3367273d9f13SBarry Smith    Notes:
3368273d9f13SBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one processor
3369273d9f13SBarry Smith    than it must be used on all processors that share the object for that argument.
3370273d9f13SBarry Smith 
3371273d9f13SBarry Smith    Storage Information:
3372273d9f13SBarry Smith    For a square global matrix we define each processor's diagonal portion
3373273d9f13SBarry Smith    to be its local rows and the corresponding columns (a square submatrix);
3374273d9f13SBarry Smith    each processor's off-diagonal portion encompasses the remainder of the
3375273d9f13SBarry Smith    local matrix (a rectangular submatrix).
3376273d9f13SBarry Smith 
3377273d9f13SBarry Smith    The user can specify preallocated storage for the diagonal part of
3378273d9f13SBarry Smith    the local submatrix with either d_nz or d_nnz (not both).  Set
3379273d9f13SBarry Smith    d_nz=PETSC_DEFAULT and d_nnz=PETSC_NULL for PETSc to control dynamic
3380273d9f13SBarry Smith    memory allocation.  Likewise, specify preallocated storage for the
3381273d9f13SBarry Smith    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
3382273d9f13SBarry Smith 
3383273d9f13SBarry Smith    Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
3384273d9f13SBarry Smith    the figure below we depict these three local rows and all columns (0-11).
3385273d9f13SBarry Smith 
3386273d9f13SBarry Smith .vb
3387273d9f13SBarry Smith            0 1 2 3 4 5 6 7 8 9 10 11
3388273d9f13SBarry Smith           -------------------
3389273d9f13SBarry Smith    row 3  |  o o o d d d o o o o o o
3390273d9f13SBarry Smith    row 4  |  o o o d d d o o o o o o
3391273d9f13SBarry Smith    row 5  |  o o o d d d o o o o o o
3392273d9f13SBarry Smith           -------------------
3393273d9f13SBarry Smith .ve
3394273d9f13SBarry Smith 
3395273d9f13SBarry Smith    Thus, any entries in the d locations are stored in the d (diagonal)
3396273d9f13SBarry Smith    submatrix, and any entries in the o locations are stored in the
3397273d9f13SBarry Smith    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
3398273d9f13SBarry Smith    stored simply in the MATSEQBAIJ format for compressed row storage.
3399273d9f13SBarry Smith 
3400273d9f13SBarry Smith    Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3401273d9f13SBarry Smith    and o_nz should indicate the number of block nonzeros per row in the o matrix.
3402273d9f13SBarry Smith    In general, for PDE problems in which most nonzeros are near the diagonal,
3403273d9f13SBarry Smith    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
3404273d9f13SBarry Smith    or you will get TERRIBLE performance; see the users' manual chapter on
3405273d9f13SBarry Smith    matrices.
3406273d9f13SBarry Smith 
3407aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3408aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3409aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3410aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3411aa95bbe8SBarry Smith 
3412273d9f13SBarry Smith    Level: intermediate
3413273d9f13SBarry Smith 
3414273d9f13SBarry Smith .keywords: matrix, block, aij, compressed row, sparse, parallel
3415273d9f13SBarry Smith 
3416aac34f13SBarry Smith .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateMPIBAIJ(), MatMPIBAIJSetPreallocationCSR()
3417273d9f13SBarry Smith @*/
34187087cfbeSBarry Smith PetscErrorCode  MatMPIBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3419273d9f13SBarry Smith {
34204ac538c5SBarry Smith   PetscErrorCode ierr;
3421273d9f13SBarry Smith 
3422273d9f13SBarry Smith   PetscFunctionBegin;
34236ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
34246ba663aaSJed Brown   PetscValidType(B,1);
34256ba663aaSJed Brown   PetscValidLogicalCollectiveInt(B,bs,2);
34264ac538c5SBarry 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);
3427273d9f13SBarry Smith   PetscFunctionReturn(0);
3428273d9f13SBarry Smith }
3429273d9f13SBarry Smith 
34304a2ae208SSatish Balay #undef __FUNCT__
34314a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIBAIJ"
343279bdfe76SSatish Balay /*@C
343379bdfe76SSatish Balay    MatCreateMPIBAIJ - Creates a sparse parallel matrix in block AIJ format
343479bdfe76SSatish Balay    (block compressed row).  For good matrix assembly performance
343579bdfe76SSatish Balay    the user should preallocate the matrix storage by setting the parameters
343679bdfe76SSatish Balay    d_nz (or d_nnz) and o_nz (or o_nnz).  By setting these parameters accurately,
343779bdfe76SSatish Balay    performance can be increased by more than a factor of 50.
343879bdfe76SSatish Balay 
3439db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3440db81eaa0SLois Curfman McInnes 
344179bdfe76SSatish Balay    Input Parameters:
3442db81eaa0SLois Curfman McInnes +  comm - MPI communicator
344379bdfe76SSatish Balay .  bs   - size of blockk
344479bdfe76SSatish Balay .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
344592e8d321SLois Curfman McInnes            This value should be the same as the local size used in creating the
344692e8d321SLois Curfman McInnes            y vector for the matrix-vector product y = Ax.
344792e8d321SLois Curfman McInnes .  n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
344892e8d321SLois Curfman McInnes            This value should be the same as the local size used in creating the
344992e8d321SLois Curfman McInnes            x vector for the matrix-vector product y = Ax.
3450be79a94dSBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3451be79a94dSBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
345247a75d0bSBarry Smith .  d_nz  - number of nonzero blocks per block row in diagonal portion of local
345379bdfe76SSatish Balay            submatrix  (same for all local rows)
345447a75d0bSBarry Smith .  d_nnz - array containing the number of nonzero blocks in the various block rows
345592e8d321SLois Curfman McInnes            of the in diagonal portion of the local (possibly different for each block
345695742e49SBarry Smith            row) or PETSC_NULL.  If you plan to factor the matrix you must leave room for the diagonal entry
345795742e49SBarry Smith            and set it even if it is zero.
345847a75d0bSBarry Smith .  o_nz  - number of nonzero blocks per block row in the off-diagonal portion of local
345979bdfe76SSatish Balay            submatrix (same for all local rows).
346047a75d0bSBarry Smith -  o_nnz - array containing the number of nonzero blocks in the various block rows of the
346192e8d321SLois Curfman McInnes            off-diagonal portion of the local submatrix (possibly different for
346292e8d321SLois Curfman McInnes            each block row) or PETSC_NULL.
346379bdfe76SSatish Balay 
346479bdfe76SSatish Balay    Output Parameter:
346579bdfe76SSatish Balay .  A - the matrix
346679bdfe76SSatish Balay 
3467db81eaa0SLois Curfman McInnes    Options Database Keys:
34688c07d4e3SBarry Smith +   -mat_block_size - size of the blocks to use
34698c07d4e3SBarry Smith -   -mat_use_hash_table <fact>
34703ffaccefSLois Curfman McInnes 
3471175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3472ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3473175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3474175b88e8SBarry Smith 
3475b259b22eSLois Curfman McInnes    Notes:
347649a6f317SBarry Smith    If the *_nnz parameter is given then the *_nz parameter is ignored
347749a6f317SBarry Smith 
347847a75d0bSBarry Smith    A nonzero block is any block that as 1 or more nonzeros in it
347947a75d0bSBarry Smith 
348079bdfe76SSatish Balay    The user MUST specify either the local or global matrix dimensions
348179bdfe76SSatish Balay    (possibly both).
348279bdfe76SSatish Balay 
3483be79a94dSBarry Smith    If PETSC_DECIDE or  PETSC_DETERMINE is used for a particular argument on one processor
3484be79a94dSBarry Smith    than it must be used on all processors that share the object for that argument.
3485be79a94dSBarry Smith 
348679bdfe76SSatish Balay    Storage Information:
348779bdfe76SSatish Balay    For a square global matrix we define each processor's diagonal portion
348879bdfe76SSatish Balay    to be its local rows and the corresponding columns (a square submatrix);
348979bdfe76SSatish Balay    each processor's off-diagonal portion encompasses the remainder of the
349079bdfe76SSatish Balay    local matrix (a rectangular submatrix).
349179bdfe76SSatish Balay 
349279bdfe76SSatish Balay    The user can specify preallocated storage for the diagonal part of
349379bdfe76SSatish Balay    the local submatrix with either d_nz or d_nnz (not both).  Set
349479bdfe76SSatish Balay    d_nz=PETSC_DEFAULT and d_nnz=PETSC_NULL for PETSc to control dynamic
349579bdfe76SSatish Balay    memory allocation.  Likewise, specify preallocated storage for the
349679bdfe76SSatish Balay    off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
349779bdfe76SSatish Balay 
349879bdfe76SSatish Balay    Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
349979bdfe76SSatish Balay    the figure below we depict these three local rows and all columns (0-11).
350079bdfe76SSatish Balay 
3501db81eaa0SLois Curfman McInnes .vb
3502db81eaa0SLois Curfman McInnes            0 1 2 3 4 5 6 7 8 9 10 11
3503db81eaa0SLois Curfman McInnes           -------------------
3504db81eaa0SLois Curfman McInnes    row 3  |  o o o d d d o o o o o o
3505db81eaa0SLois Curfman McInnes    row 4  |  o o o d d d o o o o o o
3506db81eaa0SLois Curfman McInnes    row 5  |  o o o d d d o o o o o o
3507db81eaa0SLois Curfman McInnes           -------------------
3508db81eaa0SLois Curfman McInnes .ve
350979bdfe76SSatish Balay 
351079bdfe76SSatish Balay    Thus, any entries in the d locations are stored in the d (diagonal)
351179bdfe76SSatish Balay    submatrix, and any entries in the o locations are stored in the
351279bdfe76SSatish Balay    o (off-diagonal) submatrix.  Note that the d and the o submatrices are
351357b952d6SSatish Balay    stored simply in the MATSEQBAIJ format for compressed row storage.
351479bdfe76SSatish Balay 
3515d64ed03dSBarry Smith    Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3516d64ed03dSBarry Smith    and o_nz should indicate the number of block nonzeros per row in the o matrix.
351779bdfe76SSatish Balay    In general, for PDE problems in which most nonzeros are near the diagonal,
351892e8d321SLois Curfman McInnes    one expects d_nz >> o_nz.   For large problems you MUST preallocate memory
351992e8d321SLois Curfman McInnes    or you will get TERRIBLE performance; see the users' manual chapter on
35206da5968aSLois Curfman McInnes    matrices.
352179bdfe76SSatish Balay 
3522027ccd11SLois Curfman McInnes    Level: intermediate
3523027ccd11SLois Curfman McInnes 
352492e8d321SLois Curfman McInnes .keywords: matrix, block, aij, compressed row, sparse, parallel
352579bdfe76SSatish Balay 
3526aac34f13SBarry Smith .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateMPIBAIJ(), MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
352779bdfe76SSatish Balay @*/
35287087cfbeSBarry Smith PetscErrorCode  MatCreateMPIBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A)
352979bdfe76SSatish Balay {
35306849ba73SBarry Smith   PetscErrorCode ierr;
3531b24ad042SBarry Smith   PetscMPIInt    size;
353279bdfe76SSatish Balay 
3533d64ed03dSBarry Smith   PetscFunctionBegin;
3534f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3535f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr);
3536d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
3537273d9f13SBarry Smith   if (size > 1) {
3538273d9f13SBarry Smith     ierr = MatSetType(*A,MATMPIBAIJ);CHKERRQ(ierr);
3539273d9f13SBarry Smith     ierr = MatMPIBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr);
3540273d9f13SBarry Smith   } else {
3541273d9f13SBarry Smith     ierr = MatSetType(*A,MATSEQBAIJ);CHKERRQ(ierr);
3542273d9f13SBarry Smith     ierr = MatSeqBAIJSetPreallocation(*A,bs,d_nz,d_nnz);CHKERRQ(ierr);
35433914022bSBarry Smith   }
35443a40ed3dSBarry Smith   PetscFunctionReturn(0);
354579bdfe76SSatish Balay }
3546026e39d0SSatish Balay 
35474a2ae208SSatish Balay #undef __FUNCT__
35484a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIBAIJ"
35496849ba73SBarry Smith static PetscErrorCode MatDuplicate_MPIBAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
35500ac07820SSatish Balay {
35510ac07820SSatish Balay   Mat            mat;
35520ac07820SSatish Balay   Mat_MPIBAIJ    *a,*oldmat = (Mat_MPIBAIJ*)matin->data;
3553dfbe8321SBarry Smith   PetscErrorCode ierr;
3554b24ad042SBarry Smith   PetscInt       len=0;
35550ac07820SSatish Balay 
3556d64ed03dSBarry Smith   PetscFunctionBegin;
35570ac07820SSatish Balay   *newmat       = 0;
35587adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)matin)->comm,&mat);CHKERRQ(ierr);
3559d0f46423SBarry Smith   ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr);
35607adad957SLisandro Dalcin   ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr);
35611d5dac46SHong Zhang   ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
35627fff6886SHong Zhang 
3563d5f3da31SBarry Smith   mat->factortype   = matin->factortype;
3564273d9f13SBarry Smith   mat->preallocated = PETSC_TRUE;
35650ac07820SSatish Balay   mat->assembled    = PETSC_TRUE;
35667fff6886SHong Zhang   mat->insertmode   = NOT_SET_VALUES;
35677fff6886SHong Zhang 
3568273d9f13SBarry Smith   a      = (Mat_MPIBAIJ*)mat->data;
3569d0f46423SBarry Smith   mat->rmap->bs  = matin->rmap->bs;
35700ac07820SSatish Balay   a->bs2   = oldmat->bs2;
35710ac07820SSatish Balay   a->mbs   = oldmat->mbs;
35720ac07820SSatish Balay   a->nbs   = oldmat->nbs;
35730ac07820SSatish Balay   a->Mbs   = oldmat->Mbs;
35740ac07820SSatish Balay   a->Nbs   = oldmat->Nbs;
35750ac07820SSatish Balay 
35761e1e43feSBarry Smith   ierr = PetscLayoutReference(matin->rmap,&mat->rmap);CHKERRQ(ierr);
35771e1e43feSBarry Smith   ierr = PetscLayoutReference(matin->cmap,&mat->cmap);CHKERRQ(ierr);
3578899cda47SBarry Smith 
35790ac07820SSatish Balay   a->size         = oldmat->size;
35800ac07820SSatish Balay   a->rank         = oldmat->rank;
3581aef5e8e0SSatish Balay   a->donotstash   = oldmat->donotstash;
3582aef5e8e0SSatish Balay   a->roworiented  = oldmat->roworiented;
3583aef5e8e0SSatish Balay   a->rowindices   = 0;
35840ac07820SSatish Balay   a->rowvalues    = 0;
35850ac07820SSatish Balay   a->getrowactive = PETSC_FALSE;
358630793edcSSatish Balay   a->barray       = 0;
3587899cda47SBarry Smith   a->rstartbs     = oldmat->rstartbs;
3588899cda47SBarry Smith   a->rendbs       = oldmat->rendbs;
3589899cda47SBarry Smith   a->cstartbs     = oldmat->cstartbs;
3590899cda47SBarry Smith   a->cendbs       = oldmat->cendbs;
35910ac07820SSatish Balay 
3592133cdb44SSatish Balay   /* hash table stuff */
3593133cdb44SSatish Balay   a->ht           = 0;
3594133cdb44SSatish Balay   a->hd           = 0;
3595133cdb44SSatish Balay   a->ht_size      = 0;
3596133cdb44SSatish Balay   a->ht_flag      = oldmat->ht_flag;
359725fdafccSSatish Balay   a->ht_fact      = oldmat->ht_fact;
3598133cdb44SSatish Balay   a->ht_total_ct  = 0;
3599133cdb44SSatish Balay   a->ht_insert_ct = 0;
3600133cdb44SSatish Balay 
3601899cda47SBarry Smith   ierr = PetscMemcpy(a->rangebs,oldmat->rangebs,(a->size+1)*sizeof(PetscInt));CHKERRQ(ierr);
36020ac07820SSatish Balay   if (oldmat->colmap) {
3603aa482453SBarry Smith #if defined (PETSC_USE_CTABLE)
36040f5bd95cSBarry Smith   ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr);
360548e59246SSatish Balay #else
3606b24ad042SBarry Smith   ierr = PetscMalloc((a->Nbs)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr);
360752e6d16bSBarry Smith   ierr = PetscLogObjectMemory(mat,(a->Nbs)*sizeof(PetscInt));CHKERRQ(ierr);
3608b24ad042SBarry Smith   ierr = PetscMemcpy(a->colmap,oldmat->colmap,(a->Nbs)*sizeof(PetscInt));CHKERRQ(ierr);
360948e59246SSatish Balay #endif
36100ac07820SSatish Balay   } else a->colmap = 0;
36114beb1cfeSHong Zhang 
36120ac07820SSatish Balay   if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) {
3613b24ad042SBarry Smith     ierr = PetscMalloc(len*sizeof(PetscInt),&a->garray);CHKERRQ(ierr);
361452e6d16bSBarry Smith     ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr);
3615b24ad042SBarry Smith     ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr);
36160ac07820SSatish Balay   } else a->garray = 0;
36170ac07820SSatish Balay 
3618533163c2SBarry Smith   ierr = MatStashCreate_Private(((PetscObject)matin)->comm,matin->rmap->bs,&mat->bstash);CHKERRQ(ierr);
36190ac07820SSatish Balay   ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr);
362052e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr);
36210ac07820SSatish Balay   ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr);
362252e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr);
36237fff6886SHong Zhang 
36242e8a6d31SBarry Smith   ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr);
362552e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr);
36262e8a6d31SBarry Smith   ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr);
362752e6d16bSBarry Smith   ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr);
36287adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr);
36290ac07820SSatish Balay   *newmat = mat;
36304beb1cfeSHong Zhang 
36313a40ed3dSBarry Smith   PetscFunctionReturn(0);
36320ac07820SSatish Balay }
363357b952d6SSatish Balay 
36344a2ae208SSatish Balay #undef __FUNCT__
36355bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIBAIJ"
3636112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIBAIJ(Mat newmat,PetscViewer viewer)
36374683f7a4SShri Abhyankar {
36384683f7a4SShri Abhyankar   PetscErrorCode ierr;
36394683f7a4SShri Abhyankar   int            fd;
36404683f7a4SShri Abhyankar   PetscInt       i,nz,j,rstart,rend;
36414683f7a4SShri Abhyankar   PetscScalar    *vals,*buf;
36424683f7a4SShri Abhyankar   MPI_Comm       comm = ((PetscObject)viewer)->comm;
36434683f7a4SShri Abhyankar   MPI_Status     status;
36444683f7a4SShri Abhyankar   PetscMPIInt    rank,size,maxnz;
36454683f7a4SShri Abhyankar   PetscInt       header[4],*rowlengths = 0,M,N,m,*rowners,*cols;
36464683f7a4SShri Abhyankar   PetscInt       *locrowlens = PETSC_NULL,*procsnz = PETSC_NULL,*browners = PETSC_NULL;
36474683f7a4SShri Abhyankar   PetscInt       jj,*mycols,*ibuf,bs=1,Mbs,mbs,extra_rows,mmax;
36484683f7a4SShri Abhyankar   PetscMPIInt    tag = ((PetscObject)viewer)->tag;
36494683f7a4SShri Abhyankar   PetscInt       *dlens = PETSC_NULL,*odlens = PETSC_NULL,*mask = PETSC_NULL,*masked1 = PETSC_NULL,*masked2 = PETSC_NULL,rowcount,odcount;
36504683f7a4SShri Abhyankar   PetscInt       dcount,kmax,k,nzcount,tmp,mend,sizesset=1,grows,gcols;
36514683f7a4SShri Abhyankar 
36524683f7a4SShri Abhyankar   PetscFunctionBegin;
36534683f7a4SShri Abhyankar   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading MPIBAIJ matrix 2","Mat");CHKERRQ(ierr);
36544683f7a4SShri Abhyankar     ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
36554683f7a4SShri Abhyankar   ierr = PetscOptionsEnd();CHKERRQ(ierr);
36564683f7a4SShri Abhyankar 
36574683f7a4SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
36584683f7a4SShri Abhyankar   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
36594683f7a4SShri Abhyankar   if (!rank) {
36604683f7a4SShri Abhyankar     ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
36614683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr);
36624683f7a4SShri Abhyankar     if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
36634683f7a4SShri Abhyankar   }
36644683f7a4SShri Abhyankar 
36654683f7a4SShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) sizesset = 0;
36664683f7a4SShri Abhyankar 
36674683f7a4SShri Abhyankar   ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr);
36684683f7a4SShri Abhyankar   M = header[1]; N = header[2];
36694683f7a4SShri Abhyankar 
36704683f7a4SShri Abhyankar   /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */
36714683f7a4SShri Abhyankar   if (sizesset && newmat->rmap->N < 0) newmat->rmap->N = M;
36724683f7a4SShri Abhyankar   if (sizesset && newmat->cmap->N < 0) newmat->cmap->N = N;
36734683f7a4SShri Abhyankar 
36744683f7a4SShri Abhyankar   /* If global sizes are set, check if they are consistent with that given in the file */
36754683f7a4SShri Abhyankar   if (sizesset) {
36764683f7a4SShri Abhyankar     ierr = MatGetSize(newmat,&grows,&gcols);CHKERRQ(ierr);
36774683f7a4SShri Abhyankar   }
3678abd38a8fSBarry 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);
3679abd38a8fSBarry 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);
36804683f7a4SShri Abhyankar 
36814683f7a4SShri Abhyankar   if (M != N) SETERRQ(((PetscObject)viewer)->comm,PETSC_ERR_SUP,"Can only do square matrices");
36824683f7a4SShri Abhyankar 
36834683f7a4SShri Abhyankar   /*
36844683f7a4SShri Abhyankar      This code adds extra rows to make sure the number of rows is
36854683f7a4SShri Abhyankar      divisible by the blocksize
36864683f7a4SShri Abhyankar   */
36874683f7a4SShri Abhyankar   Mbs        = M/bs;
36884683f7a4SShri Abhyankar   extra_rows = bs - M + bs*Mbs;
36894683f7a4SShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
36904683f7a4SShri Abhyankar   else                  Mbs++;
36914683f7a4SShri Abhyankar   if (extra_rows && !rank) {
36924683f7a4SShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
36934683f7a4SShri Abhyankar   }
36944683f7a4SShri Abhyankar 
36954683f7a4SShri Abhyankar   /* determine ownership of all rows */
36964683f7a4SShri Abhyankar   if (newmat->rmap->n < 0) { /* PETSC_DECIDE */
36974683f7a4SShri Abhyankar     mbs        = Mbs/size + ((Mbs % size) > rank);
36984683f7a4SShri Abhyankar     m          = mbs*bs;
36994683f7a4SShri Abhyankar   } else { /* User set */
37004683f7a4SShri Abhyankar     m          = newmat->rmap->n;
37014683f7a4SShri Abhyankar     mbs        = m/bs;
37024683f7a4SShri Abhyankar   }
37034683f7a4SShri Abhyankar   ierr       = PetscMalloc2(size+1,PetscInt,&rowners,size+1,PetscInt,&browners);CHKERRQ(ierr);
37044683f7a4SShri Abhyankar   ierr       = MPI_Allgather(&mbs,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr);
37054683f7a4SShri Abhyankar 
37064683f7a4SShri Abhyankar   /* process 0 needs enough room for process with most rows */
37074683f7a4SShri Abhyankar   if (!rank) {
37084683f7a4SShri Abhyankar     mmax = rowners[1];
37094683f7a4SShri Abhyankar     for (i=2; i<size; i++) {
37104683f7a4SShri Abhyankar       mmax = PetscMax(mmax,rowners[i]);
37114683f7a4SShri Abhyankar     }
37124683f7a4SShri Abhyankar     mmax*=bs;
37134683f7a4SShri Abhyankar   } else mmax = m;
37144683f7a4SShri Abhyankar 
37154683f7a4SShri Abhyankar   rowners[0] = 0;
37164683f7a4SShri Abhyankar   for (i=2; i<=size; i++)  rowners[i] += rowners[i-1];
37174683f7a4SShri Abhyankar   for (i=0; i<=size;  i++) browners[i] = rowners[i]*bs;
37184683f7a4SShri Abhyankar   rstart = rowners[rank];
37194683f7a4SShri Abhyankar   rend   = rowners[rank+1];
37204683f7a4SShri Abhyankar 
37214683f7a4SShri Abhyankar   /* distribute row lengths to all processors */
37224683f7a4SShri Abhyankar   ierr = PetscMalloc((mmax+1)*sizeof(PetscInt),&locrowlens);CHKERRQ(ierr);
37234683f7a4SShri Abhyankar   if (!rank) {
37244683f7a4SShri Abhyankar     mend = m;
37254683f7a4SShri Abhyankar     if (size == 1) mend = mend - extra_rows;
37264683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,locrowlens,mend,PETSC_INT);CHKERRQ(ierr);
37274683f7a4SShri Abhyankar     for (j=mend; j<m; j++) locrowlens[j] = 1;
37284683f7a4SShri Abhyankar     ierr = PetscMalloc(m*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
37294683f7a4SShri Abhyankar     ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr);
37304683f7a4SShri Abhyankar     ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr);
37314683f7a4SShri Abhyankar     for (j=0; j<m; j++) {
37324683f7a4SShri Abhyankar       procsnz[0] += locrowlens[j];
37334683f7a4SShri Abhyankar     }
37344683f7a4SShri Abhyankar     for (i=1; i<size; i++) {
37354683f7a4SShri Abhyankar       mend = browners[i+1] - browners[i];
37364683f7a4SShri Abhyankar       if (i == size-1) mend = mend - extra_rows;
37374683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,rowlengths,mend,PETSC_INT);CHKERRQ(ierr);
37384683f7a4SShri Abhyankar       for (j=mend; j<browners[i+1] - browners[i]; j++) rowlengths[j] = 1;
37394683f7a4SShri Abhyankar       /* calculate the number of nonzeros on each processor */
37404683f7a4SShri Abhyankar       for (j=0; j<browners[i+1]-browners[i]; j++) {
37414683f7a4SShri Abhyankar         procsnz[i] += rowlengths[j];
37424683f7a4SShri Abhyankar       }
37434683f7a4SShri Abhyankar       ierr = MPI_Send(rowlengths,browners[i+1]-browners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr);
37444683f7a4SShri Abhyankar     }
37454683f7a4SShri Abhyankar     ierr = PetscFree(rowlengths);CHKERRQ(ierr);
37464683f7a4SShri Abhyankar   } else {
37474683f7a4SShri Abhyankar     ierr = MPI_Recv(locrowlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
37484683f7a4SShri Abhyankar   }
37494683f7a4SShri Abhyankar 
37504683f7a4SShri Abhyankar   if (!rank) {
37514683f7a4SShri Abhyankar     /* determine max buffer needed and allocate it */
37524683f7a4SShri Abhyankar     maxnz = procsnz[0];
37534683f7a4SShri Abhyankar     for (i=1; i<size; i++) {
37544683f7a4SShri Abhyankar       maxnz = PetscMax(maxnz,procsnz[i]);
37554683f7a4SShri Abhyankar     }
37564683f7a4SShri Abhyankar     ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr);
37574683f7a4SShri Abhyankar 
37584683f7a4SShri Abhyankar     /* read in my part of the matrix column indices  */
37594683f7a4SShri Abhyankar     nz     = procsnz[0];
37604683f7a4SShri Abhyankar     ierr   = PetscMalloc((nz+1)*sizeof(PetscInt),&ibuf);CHKERRQ(ierr);
37614683f7a4SShri Abhyankar     mycols = ibuf;
37624683f7a4SShri Abhyankar     if (size == 1)  nz -= extra_rows;
37634683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr);
37644683f7a4SShri Abhyankar     if (size == 1)  for (i=0; i< extra_rows; i++) { mycols[nz+i] = M+i; }
37654683f7a4SShri Abhyankar 
37664683f7a4SShri Abhyankar     /* read in every ones (except the last) and ship off */
37674683f7a4SShri Abhyankar     for (i=1; i<size-1; i++) {
37684683f7a4SShri Abhyankar       nz   = procsnz[i];
37694683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
37704683f7a4SShri Abhyankar       ierr = MPI_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr);
37714683f7a4SShri Abhyankar     }
37724683f7a4SShri Abhyankar     /* read in the stuff for the last proc */
37734683f7a4SShri Abhyankar     if (size != 1) {
37744683f7a4SShri Abhyankar       nz   = procsnz[size-1] - extra_rows;  /* the extra rows are not on the disk */
37754683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr);
37764683f7a4SShri Abhyankar       for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
37774683f7a4SShri Abhyankar       ierr = MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);CHKERRQ(ierr);
37784683f7a4SShri Abhyankar     }
37794683f7a4SShri Abhyankar     ierr = PetscFree(cols);CHKERRQ(ierr);
37804683f7a4SShri Abhyankar   } else {
37814683f7a4SShri Abhyankar     /* determine buffer space needed for message */
37824683f7a4SShri Abhyankar     nz = 0;
37834683f7a4SShri Abhyankar     for (i=0; i<m; i++) {
37844683f7a4SShri Abhyankar       nz += locrowlens[i];
37854683f7a4SShri Abhyankar     }
37864683f7a4SShri Abhyankar     ierr   = PetscMalloc((nz+1)*sizeof(PetscInt),&ibuf);CHKERRQ(ierr);
37874683f7a4SShri Abhyankar     mycols = ibuf;
37884683f7a4SShri Abhyankar     /* receive message of column indices*/
37894683f7a4SShri Abhyankar     ierr = MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr);
37904683f7a4SShri Abhyankar     ierr = MPI_Get_count(&status,MPIU_INT,&maxnz);CHKERRQ(ierr);
37914683f7a4SShri Abhyankar     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
37924683f7a4SShri Abhyankar   }
37934683f7a4SShri Abhyankar 
37944683f7a4SShri Abhyankar   /* loop over local rows, determining number of off diagonal entries */
37954683f7a4SShri Abhyankar   ierr     = PetscMalloc2(rend-rstart,PetscInt,&dlens,rend-rstart,PetscInt,&odlens);CHKERRQ(ierr);
37964683f7a4SShri Abhyankar   ierr     = PetscMalloc3(Mbs,PetscInt,&mask,Mbs,PetscInt,&masked1,Mbs,PetscInt,&masked2);CHKERRQ(ierr);
37974683f7a4SShri Abhyankar   ierr     = PetscMemzero(mask,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
37984683f7a4SShri Abhyankar   ierr     = PetscMemzero(masked1,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
37994683f7a4SShri Abhyankar   ierr     = PetscMemzero(masked2,Mbs*sizeof(PetscInt));CHKERRQ(ierr);
38004683f7a4SShri Abhyankar   rowcount = 0; nzcount = 0;
38014683f7a4SShri Abhyankar   for (i=0; i<mbs; i++) {
38024683f7a4SShri Abhyankar     dcount  = 0;
38034683f7a4SShri Abhyankar     odcount = 0;
38044683f7a4SShri Abhyankar     for (j=0; j<bs; j++) {
38054683f7a4SShri Abhyankar       kmax = locrowlens[rowcount];
38064683f7a4SShri Abhyankar       for (k=0; k<kmax; k++) {
38074683f7a4SShri Abhyankar         tmp = mycols[nzcount++]/bs;
38084683f7a4SShri Abhyankar         if (!mask[tmp]) {
38094683f7a4SShri Abhyankar           mask[tmp] = 1;
38104683f7a4SShri Abhyankar           if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp;
38114683f7a4SShri Abhyankar           else masked1[dcount++] = tmp;
38124683f7a4SShri Abhyankar         }
38134683f7a4SShri Abhyankar       }
38144683f7a4SShri Abhyankar       rowcount++;
38154683f7a4SShri Abhyankar     }
38164683f7a4SShri Abhyankar 
38174683f7a4SShri Abhyankar     dlens[i]  = dcount;
38184683f7a4SShri Abhyankar     odlens[i] = odcount;
38194683f7a4SShri Abhyankar 
38204683f7a4SShri Abhyankar     /* zero out the mask elements we set */
38214683f7a4SShri Abhyankar     for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
38224683f7a4SShri Abhyankar     for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
38234683f7a4SShri Abhyankar   }
38244683f7a4SShri Abhyankar 
38254683f7a4SShri Abhyankar 
38264683f7a4SShri Abhyankar   if (!sizesset) {
38274683f7a4SShri Abhyankar     ierr = MatSetSizes(newmat,m,m,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
38284683f7a4SShri Abhyankar   }
38294683f7a4SShri Abhyankar   ierr = MatMPIBAIJSetPreallocation(newmat,bs,0,dlens,0,odlens);CHKERRQ(ierr);
38304683f7a4SShri Abhyankar 
38314683f7a4SShri Abhyankar   if (!rank) {
38324683f7a4SShri Abhyankar     ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&buf);CHKERRQ(ierr);
38334683f7a4SShri Abhyankar     /* read in my part of the matrix numerical values  */
38344683f7a4SShri Abhyankar     nz = procsnz[0];
38354683f7a4SShri Abhyankar     vals = buf;
38364683f7a4SShri Abhyankar     mycols = ibuf;
38374683f7a4SShri Abhyankar     if (size == 1)  nz -= extra_rows;
38384683f7a4SShri Abhyankar     ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
38394683f7a4SShri Abhyankar     if (size == 1)  for (i=0; i< extra_rows; i++) { vals[nz+i] = 1.0; }
38404683f7a4SShri Abhyankar 
38414683f7a4SShri Abhyankar     /* insert into matrix */
38424683f7a4SShri Abhyankar     jj      = rstart*bs;
38434683f7a4SShri Abhyankar     for (i=0; i<m; i++) {
38444683f7a4SShri Abhyankar       ierr = MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);CHKERRQ(ierr);
38454683f7a4SShri Abhyankar       mycols += locrowlens[i];
38464683f7a4SShri Abhyankar       vals   += locrowlens[i];
38474683f7a4SShri Abhyankar       jj++;
38484683f7a4SShri Abhyankar     }
38494683f7a4SShri Abhyankar     /* read in other processors (except the last one) and ship out */
38504683f7a4SShri Abhyankar     for (i=1; i<size-1; i++) {
38514683f7a4SShri Abhyankar       nz   = procsnz[i];
38524683f7a4SShri Abhyankar       vals = buf;
38534683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
38544683f7a4SShri Abhyankar       ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newmat)->tag,comm);CHKERRQ(ierr);
38554683f7a4SShri Abhyankar     }
38564683f7a4SShri Abhyankar     /* the last proc */
38574683f7a4SShri Abhyankar     if (size != 1){
38584683f7a4SShri Abhyankar       nz   = procsnz[i] - extra_rows;
38594683f7a4SShri Abhyankar       vals = buf;
38604683f7a4SShri Abhyankar       ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr);
38614683f7a4SShri Abhyankar       for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
38624683f7a4SShri Abhyankar       ierr = MPI_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,((PetscObject)newmat)->tag,comm);CHKERRQ(ierr);
38634683f7a4SShri Abhyankar     }
38644683f7a4SShri Abhyankar     ierr = PetscFree(procsnz);CHKERRQ(ierr);
38654683f7a4SShri Abhyankar   } else {
38664683f7a4SShri Abhyankar     /* receive numeric values */
38674683f7a4SShri Abhyankar     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&buf);CHKERRQ(ierr);
38684683f7a4SShri Abhyankar 
38694683f7a4SShri Abhyankar     /* receive message of values*/
38704683f7a4SShri Abhyankar     vals   = buf;
38714683f7a4SShri Abhyankar     mycols = ibuf;
38724683f7a4SShri Abhyankar     ierr   = MPI_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newmat)->tag,comm,&status);CHKERRQ(ierr);
38734683f7a4SShri Abhyankar     ierr   = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr);
38744683f7a4SShri Abhyankar     if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
38754683f7a4SShri Abhyankar 
38764683f7a4SShri Abhyankar     /* insert into matrix */
38774683f7a4SShri Abhyankar     jj      = rstart*bs;
38784683f7a4SShri Abhyankar     for (i=0; i<m; i++) {
38794683f7a4SShri Abhyankar       ierr    = MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);CHKERRQ(ierr);
38804683f7a4SShri Abhyankar       mycols += locrowlens[i];
38814683f7a4SShri Abhyankar       vals   += locrowlens[i];
38824683f7a4SShri Abhyankar       jj++;
38834683f7a4SShri Abhyankar     }
38844683f7a4SShri Abhyankar   }
38854683f7a4SShri Abhyankar   ierr = PetscFree(locrowlens);CHKERRQ(ierr);
38864683f7a4SShri Abhyankar   ierr = PetscFree(buf);CHKERRQ(ierr);
38874683f7a4SShri Abhyankar   ierr = PetscFree(ibuf);CHKERRQ(ierr);
38884683f7a4SShri Abhyankar   ierr = PetscFree2(rowners,browners);CHKERRQ(ierr);
38894683f7a4SShri Abhyankar   ierr = PetscFree2(dlens,odlens);CHKERRQ(ierr);
38904683f7a4SShri Abhyankar   ierr = PetscFree3(mask,masked1,masked2);CHKERRQ(ierr);
38914683f7a4SShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
38924683f7a4SShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
38934683f7a4SShri Abhyankar 
38944683f7a4SShri Abhyankar   PetscFunctionReturn(0);
38954683f7a4SShri Abhyankar }
38964683f7a4SShri Abhyankar 
38974683f7a4SShri Abhyankar #undef __FUNCT__
38984a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJSetHashTableFactor"
3899133cdb44SSatish Balay /*@
3900133cdb44SSatish Balay    MatMPIBAIJSetHashTableFactor - Sets the factor required to compute the size of the HashTable.
3901133cdb44SSatish Balay 
3902133cdb44SSatish Balay    Input Parameters:
3903133cdb44SSatish Balay .  mat  - the matrix
3904133cdb44SSatish Balay .  fact - factor
3905133cdb44SSatish Balay 
3906c5eb9154SBarry Smith    Not Collective, each process can use a different factor
3907fee21e36SBarry Smith 
39088c890885SBarry Smith    Level: advanced
39098c890885SBarry Smith 
3910133cdb44SSatish Balay   Notes:
39118c07d4e3SBarry Smith    This can also be set by the command line option: -mat_use_hash_table <fact>
3912133cdb44SSatish Balay 
3913133cdb44SSatish Balay .keywords: matrix, hashtable, factor, HT
3914133cdb44SSatish Balay 
3915133cdb44SSatish Balay .seealso: MatSetOption()
3916133cdb44SSatish Balay @*/
39177087cfbeSBarry Smith PetscErrorCode  MatMPIBAIJSetHashTableFactor(Mat mat,PetscReal fact)
3918133cdb44SSatish Balay {
39194ac538c5SBarry Smith   PetscErrorCode ierr;
39205bf65638SKris Buschelman 
39215bf65638SKris Buschelman   PetscFunctionBegin;
39224ac538c5SBarry Smith   ierr = PetscTryMethod(mat,"MatSetHashTableFactor_C",(Mat,PetscReal),(mat,fact));CHKERRQ(ierr);
39235bf65638SKris Buschelman   PetscFunctionReturn(0);
39245bf65638SKris Buschelman }
39255bf65638SKris Buschelman 
3926be1d678aSKris Buschelman EXTERN_C_BEGIN
39275bf65638SKris Buschelman #undef __FUNCT__
39285bf65638SKris Buschelman #define __FUNCT__ "MatSetHashTableFactor_MPIBAIJ"
39297087cfbeSBarry Smith PetscErrorCode  MatSetHashTableFactor_MPIBAIJ(Mat mat,PetscReal fact)
39305bf65638SKris Buschelman {
393125fdafccSSatish Balay   Mat_MPIBAIJ *baij;
3932133cdb44SSatish Balay 
3933133cdb44SSatish Balay   PetscFunctionBegin;
3934133cdb44SSatish Balay   baij = (Mat_MPIBAIJ*)mat->data;
3935133cdb44SSatish Balay   baij->ht_fact = fact;
3936133cdb44SSatish Balay   PetscFunctionReturn(0);
3937133cdb44SSatish Balay }
3938be1d678aSKris Buschelman EXTERN_C_END
3939f2a5309cSSatish Balay 
39404a2ae208SSatish Balay #undef __FUNCT__
39414a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJGetSeqBAIJ"
39427087cfbeSBarry Smith PetscErrorCode  MatMPIBAIJGetSeqBAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[])
3943f2a5309cSSatish Balay {
3944f2a5309cSSatish Balay   Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
3945f2a5309cSSatish Balay   PetscFunctionBegin;
3946f2a5309cSSatish Balay   *Ad     = a->A;
3947f2a5309cSSatish Balay   *Ao     = a->B;
3948195d93cdSBarry Smith   *colmap = a->garray;
3949f2a5309cSSatish Balay   PetscFunctionReturn(0);
3950f2a5309cSSatish Balay }
395185535b8eSBarry Smith 
395285535b8eSBarry Smith /*
395385535b8eSBarry Smith     Special version for direct calls from Fortran (to eliminate two function call overheads
395485535b8eSBarry Smith */
395585535b8eSBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
395685535b8eSBarry Smith #define matmpibaijsetvaluesblocked_ MATMPIBAIJSETVALUESBLOCKED
395785535b8eSBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
395885535b8eSBarry Smith #define matmpibaijsetvaluesblocked_ matmpibaijsetvaluesblocked
395985535b8eSBarry Smith #endif
396085535b8eSBarry Smith 
396185535b8eSBarry Smith #undef __FUNCT__
396285535b8eSBarry Smith #define __FUNCT__ "matmpibiajsetvaluesblocked"
396385535b8eSBarry Smith /*@C
396485535b8eSBarry Smith   MatMPIBAIJSetValuesBlocked - Direct Fortran call to replace call to MatSetValuesBlocked()
396585535b8eSBarry Smith 
396685535b8eSBarry Smith   Collective on Mat
396785535b8eSBarry Smith 
396885535b8eSBarry Smith   Input Parameters:
396985535b8eSBarry Smith + mat - the matrix
397085535b8eSBarry Smith . min - number of input rows
397185535b8eSBarry Smith . im - input rows
397285535b8eSBarry Smith . nin - number of input columns
397385535b8eSBarry Smith . in - input columns
397485535b8eSBarry Smith . v - numerical values input
397585535b8eSBarry Smith - addvin - INSERT_VALUES or ADD_VALUES
397685535b8eSBarry Smith 
397785535b8eSBarry Smith   Notes: This has a complete copy of MatSetValuesBlocked_MPIBAIJ() which is terrible code un-reuse.
397885535b8eSBarry Smith 
397985535b8eSBarry Smith   Level: advanced
398085535b8eSBarry Smith 
398185535b8eSBarry Smith .seealso:   MatSetValuesBlocked()
398285535b8eSBarry Smith @*/
398385535b8eSBarry Smith PetscErrorCode matmpibaijsetvaluesblocked_(Mat *matin,PetscInt *min,const PetscInt im[],PetscInt *nin,const PetscInt in[],const MatScalar v[],InsertMode *addvin)
398485535b8eSBarry Smith {
398585535b8eSBarry Smith   /* convert input arguments to C version */
398685535b8eSBarry Smith   Mat             mat = *matin;
398785535b8eSBarry Smith   PetscInt        m = *min, n = *nin;
398885535b8eSBarry Smith   InsertMode      addv = *addvin;
398985535b8eSBarry Smith 
399085535b8eSBarry Smith   Mat_MPIBAIJ     *baij = (Mat_MPIBAIJ*)mat->data;
399185535b8eSBarry Smith   const MatScalar *value;
399285535b8eSBarry Smith   MatScalar       *barray=baij->barray;
3993ace3abfcSBarry Smith   PetscBool       roworiented = baij->roworiented;
399485535b8eSBarry Smith   PetscErrorCode  ierr;
399585535b8eSBarry Smith   PetscInt        i,j,ii,jj,row,col,rstart=baij->rstartbs;
399685535b8eSBarry Smith   PetscInt        rend=baij->rendbs,cstart=baij->cstartbs,stepval;
3997d0f46423SBarry Smith   PetscInt        cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;
399885535b8eSBarry Smith 
399985535b8eSBarry Smith   PetscFunctionBegin;
400085535b8eSBarry Smith   /* tasks normally handled by MatSetValuesBlocked() */
400185535b8eSBarry Smith   if (mat->insertmode == NOT_SET_VALUES) {
400285535b8eSBarry Smith     mat->insertmode = addv;
400385535b8eSBarry Smith   }
400485535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
4005e7e72b3dSBarry Smith   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
4006e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
400785535b8eSBarry Smith #endif
400885535b8eSBarry Smith   if (mat->assembled) {
400985535b8eSBarry Smith     mat->was_assembled = PETSC_TRUE;
401085535b8eSBarry Smith     mat->assembled     = PETSC_FALSE;
401185535b8eSBarry Smith   }
401285535b8eSBarry Smith   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
401385535b8eSBarry Smith 
401485535b8eSBarry Smith 
401585535b8eSBarry Smith   if(!barray) {
401685535b8eSBarry Smith     ierr         = PetscMalloc(bs2*sizeof(MatScalar),&barray);CHKERRQ(ierr);
401785535b8eSBarry Smith     baij->barray = barray;
401885535b8eSBarry Smith   }
401985535b8eSBarry Smith 
402085535b8eSBarry Smith   if (roworiented) {
402185535b8eSBarry Smith     stepval = (n-1)*bs;
402285535b8eSBarry Smith   } else {
402385535b8eSBarry Smith     stepval = (m-1)*bs;
402485535b8eSBarry Smith   }
402585535b8eSBarry Smith   for (i=0; i<m; i++) {
402685535b8eSBarry Smith     if (im[i] < 0) continue;
402785535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
4028e32f2f54SBarry 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);
402985535b8eSBarry Smith #endif
403085535b8eSBarry Smith     if (im[i] >= rstart && im[i] < rend) {
403185535b8eSBarry Smith       row = im[i] - rstart;
403285535b8eSBarry Smith       for (j=0; j<n; j++) {
403385535b8eSBarry Smith         /* If NumCol = 1 then a copy is not required */
403485535b8eSBarry Smith         if ((roworiented) && (n == 1)) {
403585535b8eSBarry Smith           barray = (MatScalar*)v + i*bs2;
403685535b8eSBarry Smith         } else if((!roworiented) && (m == 1)) {
403785535b8eSBarry Smith           barray = (MatScalar*)v + j*bs2;
403885535b8eSBarry Smith         } else { /* Here a copy is required */
403985535b8eSBarry Smith           if (roworiented) {
404085535b8eSBarry Smith             value = v + i*(stepval+bs)*bs + j*bs;
404185535b8eSBarry Smith           } else {
404285535b8eSBarry Smith             value = v + j*(stepval+bs)*bs + i*bs;
404385535b8eSBarry Smith           }
404485535b8eSBarry Smith           for (ii=0; ii<bs; ii++,value+=stepval) {
404585535b8eSBarry Smith             for (jj=0; jj<bs; jj++) {
404685535b8eSBarry Smith               *barray++  = *value++;
404785535b8eSBarry Smith             }
404885535b8eSBarry Smith           }
404985535b8eSBarry Smith           barray -=bs2;
405085535b8eSBarry Smith         }
405185535b8eSBarry Smith 
405285535b8eSBarry Smith         if (in[j] >= cstart && in[j] < cend){
405385535b8eSBarry Smith           col  = in[j] - cstart;
405497e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
405585535b8eSBarry Smith         }
405685535b8eSBarry Smith         else if (in[j] < 0) continue;
405785535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
4058cb9801acSJed 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);
405985535b8eSBarry Smith #endif
406085535b8eSBarry Smith         else {
406185535b8eSBarry Smith           if (mat->was_assembled) {
406285535b8eSBarry Smith             if (!baij->colmap) {
406385535b8eSBarry Smith               ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr);
406485535b8eSBarry Smith             }
406585535b8eSBarry Smith 
406685535b8eSBarry Smith #if defined(PETSC_USE_DEBUG)
406785535b8eSBarry Smith #if defined (PETSC_USE_CTABLE)
406885535b8eSBarry Smith             { PetscInt data;
406985535b8eSBarry Smith               ierr = PetscTableFind(baij->colmap,in[j]+1,&data);CHKERRQ(ierr);
4070e32f2f54SBarry Smith               if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
407185535b8eSBarry Smith             }
407285535b8eSBarry Smith #else
4073e32f2f54SBarry Smith             if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
407485535b8eSBarry Smith #endif
407585535b8eSBarry Smith #endif
407685535b8eSBarry Smith #if defined (PETSC_USE_CTABLE)
407785535b8eSBarry Smith 	    ierr = PetscTableFind(baij->colmap,in[j]+1,&col);CHKERRQ(ierr);
407885535b8eSBarry Smith             col  = (col - 1)/bs;
407985535b8eSBarry Smith #else
408085535b8eSBarry Smith             col = (baij->colmap[in[j]] - 1)/bs;
408185535b8eSBarry Smith #endif
408285535b8eSBarry Smith             if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
408385535b8eSBarry Smith               ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr);
408485535b8eSBarry Smith               col =  in[j];
408585535b8eSBarry Smith             }
408685535b8eSBarry Smith           }
408785535b8eSBarry Smith           else col = in[j];
408897e5c40aSBarry Smith           ierr = MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);CHKERRQ(ierr);
408985535b8eSBarry Smith         }
409085535b8eSBarry Smith       }
409185535b8eSBarry Smith     } else {
409285535b8eSBarry Smith       if (!baij->donotstash) {
409385535b8eSBarry Smith         if (roworiented) {
409485535b8eSBarry Smith           ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
409585535b8eSBarry Smith         } else {
409685535b8eSBarry Smith           ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr);
409785535b8eSBarry Smith         }
409885535b8eSBarry Smith       }
409985535b8eSBarry Smith     }
410085535b8eSBarry Smith   }
410185535b8eSBarry Smith 
410285535b8eSBarry Smith   /* task normally handled by MatSetValuesBlocked() */
410385535b8eSBarry Smith   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
410485535b8eSBarry Smith   PetscFunctionReturn(0);
410585535b8eSBarry Smith }
4106dfb205c3SBarry Smith 
4107dfb205c3SBarry Smith #undef __FUNCT__
4108dfb205c3SBarry Smith #define __FUNCT__ "MatCreateMPIBAIJWithArrays"
4109dfb205c3SBarry Smith /*@
4110dfb205c3SBarry Smith      MatCreateMPIBAIJWithArrays - creates a MPI BAIJ matrix using arrays that contain in standard
4111dfb205c3SBarry Smith          CSR format the local rows.
4112dfb205c3SBarry Smith 
4113dfb205c3SBarry Smith    Collective on MPI_Comm
4114dfb205c3SBarry Smith 
4115dfb205c3SBarry Smith    Input Parameters:
4116dfb205c3SBarry Smith +  comm - MPI communicator
4117dfb205c3SBarry Smith .  bs - the block size, only a block size of 1 is supported
4118dfb205c3SBarry Smith .  m - number of local rows (Cannot be PETSC_DECIDE)
4119dfb205c3SBarry Smith .  n - This value should be the same as the local size used in creating the
4120dfb205c3SBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4121dfb205c3SBarry Smith        calculated if N is given) For square matrices n is almost always m.
4122dfb205c3SBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4123dfb205c3SBarry Smith .  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4124dfb205c3SBarry Smith .   i - row indices
4125dfb205c3SBarry Smith .   j - column indices
4126dfb205c3SBarry Smith -   a - matrix values
4127dfb205c3SBarry Smith 
4128dfb205c3SBarry Smith    Output Parameter:
4129dfb205c3SBarry Smith .   mat - the matrix
4130dfb205c3SBarry Smith 
4131dfb205c3SBarry Smith    Level: intermediate
4132dfb205c3SBarry Smith 
4133dfb205c3SBarry Smith    Notes:
4134dfb205c3SBarry Smith        The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
4135dfb205c3SBarry Smith      thus you CANNOT change the matrix entries by changing the values of a[] after you have
4136dfb205c3SBarry Smith      called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
4137dfb205c3SBarry Smith 
4138dfb205c3SBarry Smith        The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
4139dfb205c3SBarry Smith 
4140dfb205c3SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel
4141dfb205c3SBarry Smith 
4142dfb205c3SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
4143dfb205c3SBarry Smith           MPIAIJ, MatCreateMPIAIJ(), MatCreateMPIAIJWithSplitArrays()
4144dfb205c3SBarry Smith @*/
41457087cfbeSBarry 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)
4146dfb205c3SBarry Smith {
4147dfb205c3SBarry Smith   PetscErrorCode ierr;
4148dfb205c3SBarry Smith 
4149dfb205c3SBarry Smith 
4150dfb205c3SBarry Smith  PetscFunctionBegin;
4151dfb205c3SBarry Smith   if (i[0]) {
4152dfb205c3SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4153dfb205c3SBarry Smith   }
4154dfb205c3SBarry Smith   if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
4155dfb205c3SBarry Smith   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4156dfb205c3SBarry Smith   ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr);
4157dfb205c3SBarry Smith   ierr = MatSetType(*mat,MATMPISBAIJ);CHKERRQ(ierr);
4158dfb205c3SBarry Smith   ierr = MatMPIBAIJSetPreallocationCSR(*mat,bs,i,j,a);CHKERRQ(ierr);
4159dfb205c3SBarry Smith   PetscFunctionReturn(0);
4160dfb205c3SBarry Smith }
4161