1be1d678aSKris Buschelman #define PETSCMAT_DLL 279bdfe76SSatish Balay 37c4f633dSBarry Smith #include "../src/mat/impls/baij/mpi/mpibaij.h" /*I "petscmat.h" I*/ 4f3da1532SBarry Smith #include "petscblaslapack.h" 579bdfe76SSatish Balay 6dfbe8321SBarry Smith EXTERN PetscErrorCode MatSetUpMultiply_MPIBAIJ(Mat); 7dfbe8321SBarry Smith EXTERN PetscErrorCode DisAssemble_MPIBAIJ(Mat); 8b24ad042SBarry Smith EXTERN PetscErrorCode MatIncreaseOverlap_MPIBAIJ(Mat,PetscInt,IS[],PetscInt); 9b24ad042SBarry Smith EXTERN PetscErrorCode MatGetSubMatrices_MPIBAIJ(Mat,PetscInt,const IS[],const IS[],MatReuse,Mat *[]); 10b24ad042SBarry Smith EXTERN PetscErrorCode MatGetValues_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt [],PetscScalar []); 11b24ad042SBarry Smith EXTERN PetscErrorCode MatSetValues_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt [],const PetscScalar [],InsertMode); 12b24ad042SBarry Smith EXTERN PetscErrorCode MatSetValuesBlocked_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode); 13b24ad042SBarry Smith EXTERN PetscErrorCode MatGetRow_SeqBAIJ(Mat,PetscInt,PetscInt*,PetscInt*[],PetscScalar*[]); 14b24ad042SBarry Smith EXTERN PetscErrorCode MatRestoreRow_SeqBAIJ(Mat,PetscInt,PetscInt*,PetscInt*[],PetscScalar*[]); 15f4df32b1SMatthew Knepley EXTERN PetscErrorCode MatZeroRows_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscScalar); 1693fea6afSBarry Smith 174a2ae208SSatish Balay #undef __FUNCT__ 18985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_MPIBAIJ" 19985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_MPIBAIJ(Mat A,Vec v,PetscInt idx[]) 207843d17aSBarry Smith { 217843d17aSBarry Smith Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data; 22dfbe8321SBarry Smith PetscErrorCode ierr; 23985db425SBarry Smith PetscInt i,*idxb = 0; 2487828ca2SBarry Smith PetscScalar *va,*vb; 257843d17aSBarry Smith Vec vtmp; 267843d17aSBarry Smith 277843d17aSBarry Smith PetscFunctionBegin; 28985db425SBarry Smith ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr); 291ebc52fbSHong Zhang ierr = VecGetArray(v,&va);CHKERRQ(ierr); 30985db425SBarry Smith if (idx) { 31192daf7cSBarry Smith for (i=0; i<A->rmap->n; i++) {if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;} 32985db425SBarry Smith } 337843d17aSBarry Smith 34d0f46423SBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 35d0f46423SBarry Smith if (idx) {ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr);} 36985db425SBarry Smith ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 371ebc52fbSHong Zhang ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 387843d17aSBarry Smith 39d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++){ 40d0f46423SBarry Smith if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {va[i] = vb[i]; if (idx) idx[i] = A->cmap->bs*a->garray[idxb[i]/A->cmap->bs] + (idxb[i] % A->cmap->bs);} 417843d17aSBarry Smith } 427843d17aSBarry Smith 431ebc52fbSHong Zhang ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 441ebc52fbSHong Zhang ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 45985db425SBarry Smith if (idxb) {ierr = PetscFree(idxb);CHKERRQ(ierr);} 46ac355199SBarry Smith ierr = VecDestroy(vtmp);CHKERRQ(ierr); 477843d17aSBarry Smith PetscFunctionReturn(0); 487843d17aSBarry Smith } 497843d17aSBarry Smith 507fc3c18eSBarry Smith EXTERN_C_BEGIN 514a2ae208SSatish Balay #undef __FUNCT__ 524a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIBAIJ" 53be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_MPIBAIJ(Mat mat) 547fc3c18eSBarry Smith { 557fc3c18eSBarry Smith Mat_MPIBAIJ *aij = (Mat_MPIBAIJ *)mat->data; 56dfbe8321SBarry Smith PetscErrorCode ierr; 577fc3c18eSBarry Smith 587fc3c18eSBarry Smith PetscFunctionBegin; 597fc3c18eSBarry Smith ierr = MatStoreValues(aij->A);CHKERRQ(ierr); 607fc3c18eSBarry Smith ierr = MatStoreValues(aij->B);CHKERRQ(ierr); 617fc3c18eSBarry Smith PetscFunctionReturn(0); 627fc3c18eSBarry Smith } 637fc3c18eSBarry Smith EXTERN_C_END 647fc3c18eSBarry Smith 657fc3c18eSBarry Smith EXTERN_C_BEGIN 664a2ae208SSatish Balay #undef __FUNCT__ 674a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIBAIJ" 68be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_MPIBAIJ(Mat mat) 697fc3c18eSBarry Smith { 707fc3c18eSBarry Smith Mat_MPIBAIJ *aij = (Mat_MPIBAIJ *)mat->data; 71dfbe8321SBarry Smith PetscErrorCode ierr; 727fc3c18eSBarry Smith 737fc3c18eSBarry Smith PetscFunctionBegin; 747fc3c18eSBarry Smith ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr); 757fc3c18eSBarry Smith ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr); 767fc3c18eSBarry Smith PetscFunctionReturn(0); 777fc3c18eSBarry Smith } 787fc3c18eSBarry Smith EXTERN_C_END 797fc3c18eSBarry Smith 80537820f0SBarry Smith /* 81537820f0SBarry Smith Local utility routine that creates a mapping from the global column 8257b952d6SSatish Balay number to the local number in the off-diagonal part of the local 83e06f6af7SJed Brown storage of the matrix. This is done in a non scalable way since the 8457b952d6SSatish Balay length of colmap equals the global matrix length. 8557b952d6SSatish Balay */ 864a2ae208SSatish Balay #undef __FUNCT__ 874a2ae208SSatish Balay #define __FUNCT__ "CreateColmap_MPIBAIJ_Private" 88dfbe8321SBarry Smith PetscErrorCode CreateColmap_MPIBAIJ_Private(Mat mat) 8957b952d6SSatish Balay { 9057b952d6SSatish Balay Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 9157b952d6SSatish Balay Mat_SeqBAIJ *B = (Mat_SeqBAIJ*)baij->B->data; 926849ba73SBarry Smith PetscErrorCode ierr; 93d0f46423SBarry Smith PetscInt nbs = B->nbs,i,bs=mat->rmap->bs; 9457b952d6SSatish Balay 95d64ed03dSBarry Smith PetscFunctionBegin; 96aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 97f14a1c24SBarry Smith ierr = PetscTableCreate(baij->nbs,&baij->colmap);CHKERRQ(ierr); 9848e59246SSatish Balay for (i=0; i<nbs; i++){ 990f5bd95cSBarry Smith ierr = PetscTableAdd(baij->colmap,baij->garray[i]+1,i*bs+1);CHKERRQ(ierr); 10048e59246SSatish Balay } 10148e59246SSatish Balay #else 102b24ad042SBarry Smith ierr = PetscMalloc((baij->Nbs+1)*sizeof(PetscInt),&baij->colmap);CHKERRQ(ierr); 10352e6d16bSBarry Smith ierr = PetscLogObjectMemory(mat,baij->Nbs*sizeof(PetscInt));CHKERRQ(ierr); 104b24ad042SBarry Smith ierr = PetscMemzero(baij->colmap,baij->Nbs*sizeof(PetscInt));CHKERRQ(ierr); 105928fc39bSSatish Balay for (i=0; i<nbs; i++) baij->colmap[baij->garray[i]] = i*bs+1; 10648e59246SSatish Balay #endif 1073a40ed3dSBarry Smith PetscFunctionReturn(0); 10857b952d6SSatish Balay } 10957b952d6SSatish Balay 110f5e9677aSSatish Balay #define MatSetValues_SeqBAIJ_A_Private(row,col,value,addv) \ 11180c1aa95SSatish Balay { \ 11280c1aa95SSatish Balay \ 11380c1aa95SSatish Balay brow = row/bs; \ 11480c1aa95SSatish Balay rp = aj + ai[brow]; ap = aa + bs2*ai[brow]; \ 115ac7a638eSSatish Balay rmax = aimax[brow]; nrow = ailen[brow]; \ 11680c1aa95SSatish Balay bcol = col/bs; \ 11780c1aa95SSatish Balay ridx = row % bs; cidx = col % bs; \ 118ab26458aSBarry Smith low = 0; high = nrow; \ 119ab26458aSBarry Smith while (high-low > 3) { \ 120ab26458aSBarry Smith t = (low+high)/2; \ 121ab26458aSBarry Smith if (rp[t] > bcol) high = t; \ 122ab26458aSBarry Smith else low = t; \ 123ab26458aSBarry Smith } \ 124ab26458aSBarry Smith for (_i=low; _i<high; _i++) { \ 12580c1aa95SSatish Balay if (rp[_i] > bcol) break; \ 12680c1aa95SSatish Balay if (rp[_i] == bcol) { \ 12780c1aa95SSatish Balay bap = ap + bs2*_i + bs*cidx + ridx; \ 128eada6651SSatish Balay if (addv == ADD_VALUES) *bap += value; \ 129eada6651SSatish Balay else *bap = value; \ 130ac7a638eSSatish Balay goto a_noinsert; \ 13180c1aa95SSatish Balay } \ 13280c1aa95SSatish Balay } \ 13389280ab3SLois Curfman McInnes if (a->nonew == 1) goto a_noinsert; \ 134e32f2f54SBarry Smith if (a->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \ 135fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,aimax,a->nonew,MatScalar); \ 13680c1aa95SSatish Balay N = nrow++ - 1; \ 13780c1aa95SSatish Balay /* shift up all the later entries in this row */ \ 13880c1aa95SSatish Balay for (ii=N; ii>=_i; ii--) { \ 13980c1aa95SSatish Balay rp[ii+1] = rp[ii]; \ 1403eda8832SBarry Smith ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr); \ 14180c1aa95SSatish Balay } \ 1423eda8832SBarry Smith if (N>=_i) { ierr = PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));CHKERRQ(ierr); } \ 14380c1aa95SSatish Balay rp[_i] = bcol; \ 14480c1aa95SSatish Balay ap[bs2*_i + bs*cidx + ridx] = value; \ 145ac7a638eSSatish Balay a_noinsert:; \ 14680c1aa95SSatish Balay ailen[brow] = nrow; \ 14780c1aa95SSatish Balay } 14857b952d6SSatish Balay 149ac7a638eSSatish Balay #define MatSetValues_SeqBAIJ_B_Private(row,col,value,addv) \ 150ac7a638eSSatish Balay { \ 151ac7a638eSSatish Balay brow = row/bs; \ 152ac7a638eSSatish Balay rp = bj + bi[brow]; ap = ba + bs2*bi[brow]; \ 153ac7a638eSSatish Balay rmax = bimax[brow]; nrow = bilen[brow]; \ 154ac7a638eSSatish Balay bcol = col/bs; \ 155ac7a638eSSatish Balay ridx = row % bs; cidx = col % bs; \ 156ac7a638eSSatish Balay low = 0; high = nrow; \ 157ac7a638eSSatish Balay while (high-low > 3) { \ 158ac7a638eSSatish Balay t = (low+high)/2; \ 159ac7a638eSSatish Balay if (rp[t] > bcol) high = t; \ 160ac7a638eSSatish Balay else low = t; \ 161ac7a638eSSatish Balay } \ 162ac7a638eSSatish Balay for (_i=low; _i<high; _i++) { \ 163ac7a638eSSatish Balay if (rp[_i] > bcol) break; \ 164ac7a638eSSatish Balay if (rp[_i] == bcol) { \ 165ac7a638eSSatish Balay bap = ap + bs2*_i + bs*cidx + ridx; \ 166ac7a638eSSatish Balay if (addv == ADD_VALUES) *bap += value; \ 167ac7a638eSSatish Balay else *bap = value; \ 168ac7a638eSSatish Balay goto b_noinsert; \ 169ac7a638eSSatish Balay } \ 170ac7a638eSSatish Balay } \ 17189280ab3SLois Curfman McInnes if (b->nonew == 1) goto b_noinsert; \ 172e32f2f54SBarry Smith if (b->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \ 173fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(B,b->mbs,bs2,nrow,brow,bcol,rmax,ba,bi,bj,rp,ap,bimax,b->nonew,MatScalar); \ 174085a36d4SBarry Smith CHKMEMQ;\ 175ac7a638eSSatish Balay N = nrow++ - 1; \ 176ac7a638eSSatish Balay /* shift up all the later entries in this row */ \ 177ac7a638eSSatish Balay for (ii=N; ii>=_i; ii--) { \ 178ac7a638eSSatish Balay rp[ii+1] = rp[ii]; \ 1793eda8832SBarry Smith ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr); \ 180ac7a638eSSatish Balay } \ 1813eda8832SBarry Smith if (N>=_i) { ierr = PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));CHKERRQ(ierr);} \ 182ac7a638eSSatish Balay rp[_i] = bcol; \ 183ac7a638eSSatish Balay ap[bs2*_i + bs*cidx + ridx] = value; \ 184ac7a638eSSatish Balay b_noinsert:; \ 185ac7a638eSSatish Balay bilen[brow] = nrow; \ 186ac7a638eSSatish Balay } 187ac7a638eSSatish Balay 1884a2ae208SSatish Balay #undef __FUNCT__ 1894a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIBAIJ" 190b24ad042SBarry Smith PetscErrorCode MatSetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv) 19157b952d6SSatish Balay { 19257b952d6SSatish Balay Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 19393fea6afSBarry Smith MatScalar value; 194ace3abfcSBarry Smith PetscBool roworiented = baij->roworiented; 195dfbe8321SBarry Smith PetscErrorCode ierr; 196b24ad042SBarry Smith PetscInt i,j,row,col; 197d0f46423SBarry Smith PetscInt rstart_orig=mat->rmap->rstart; 198d0f46423SBarry Smith PetscInt rend_orig=mat->rmap->rend,cstart_orig=mat->cmap->rstart; 199d0f46423SBarry Smith PetscInt cend_orig=mat->cmap->rend,bs=mat->rmap->bs; 20057b952d6SSatish Balay 201eada6651SSatish Balay /* Some Variables required in the macro */ 20280c1aa95SSatish Balay Mat A = baij->A; 20380c1aa95SSatish Balay Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)(A)->data; 204b24ad042SBarry Smith PetscInt *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j; 2053eda8832SBarry Smith MatScalar *aa=a->a; 206ac7a638eSSatish Balay 207ac7a638eSSatish Balay Mat B = baij->B; 208ac7a638eSSatish Balay Mat_SeqBAIJ *b = (Mat_SeqBAIJ*)(B)->data; 209b24ad042SBarry Smith PetscInt *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j; 2103eda8832SBarry Smith MatScalar *ba=b->a; 211ac7a638eSSatish Balay 212b24ad042SBarry Smith PetscInt *rp,ii,nrow,_i,rmax,N,brow,bcol; 213b24ad042SBarry Smith PetscInt low,high,t,ridx,cidx,bs2=a->bs2; 2143eda8832SBarry Smith MatScalar *ap,*bap; 21580c1aa95SSatish Balay 216d64ed03dSBarry Smith PetscFunctionBegin; 21771fd2e92SBarry Smith if (v) PetscValidScalarPointer(v,6); 21857b952d6SSatish Balay for (i=0; i<m; i++) { 2195ef9f2a5SBarry Smith if (im[i] < 0) continue; 2202515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 221e32f2f54SBarry Smith if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1); 222639f9d9dSBarry Smith #endif 22357b952d6SSatish Balay if (im[i] >= rstart_orig && im[i] < rend_orig) { 22457b952d6SSatish Balay row = im[i] - rstart_orig; 22557b952d6SSatish Balay for (j=0; j<n; j++) { 22657b952d6SSatish Balay if (in[j] >= cstart_orig && in[j] < cend_orig){ 22757b952d6SSatish Balay col = in[j] - cstart_orig; 22857b952d6SSatish Balay if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 229f5e9677aSSatish Balay MatSetValues_SeqBAIJ_A_Private(row,col,value,addv); 23080c1aa95SSatish Balay /* ierr = MatSetValues_SeqBAIJ(baij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */ 23173959e64SBarry Smith } else if (in[j] < 0) continue; 2322515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 233660746e0SBarry 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); 234639f9d9dSBarry Smith #endif 23557b952d6SSatish Balay else { 23657b952d6SSatish Balay if (mat->was_assembled) { 237905e6a2fSBarry Smith if (!baij->colmap) { 238905e6a2fSBarry Smith ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr); 239905e6a2fSBarry Smith } 240aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 2410f5bd95cSBarry Smith ierr = PetscTableFind(baij->colmap,in[j]/bs + 1,&col);CHKERRQ(ierr); 242bba1ac68SSatish Balay col = col - 1; 24348e59246SSatish Balay #else 244bba1ac68SSatish Balay col = baij->colmap[in[j]/bs] - 1; 24548e59246SSatish Balay #endif 24657b952d6SSatish Balay if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) { 24757b952d6SSatish Balay ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr); 2488295de27SSatish Balay col = in[j]; 2499bf004c3SSatish Balay /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */ 2509bf004c3SSatish Balay B = baij->B; 2519bf004c3SSatish Balay b = (Mat_SeqBAIJ*)(B)->data; 2529bf004c3SSatish Balay bimax=b->imax;bi=b->i;bilen=b->ilen;bj=b->j; 2539bf004c3SSatish Balay ba=b->a; 254bba1ac68SSatish Balay } else col += in[j]%bs; 2558295de27SSatish Balay } else col = in[j]; 25657b952d6SSatish Balay if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 25790da58bdSSatish Balay MatSetValues_SeqBAIJ_B_Private(row,col,value,addv); 25890da58bdSSatish Balay /* ierr = MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */ 25957b952d6SSatish Balay } 26057b952d6SSatish Balay } 261d64ed03dSBarry Smith } else { 2624cb17eb5SBarry Smith if (mat->nooffprocentries) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Setting off process row %D even though MatSetOption(,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) was set",im[i]); 26390f02eecSBarry Smith if (!baij->donotstash) { 264ff2fd236SBarry Smith if (roworiented) { 265b400d20cSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);CHKERRQ(ierr); 266ff2fd236SBarry Smith } else { 267b400d20cSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);CHKERRQ(ierr); 26857b952d6SSatish Balay } 26957b952d6SSatish Balay } 27057b952d6SSatish Balay } 27190f02eecSBarry Smith } 2723a40ed3dSBarry Smith PetscFunctionReturn(0); 27357b952d6SSatish Balay } 27457b952d6SSatish Balay 2754a2ae208SSatish Balay #undef __FUNCT__ 27697e5c40aSBarry Smith #define __FUNCT__ "MatSetValuesBlocked_MPIBAIJ" 27797e5c40aSBarry Smith PetscErrorCode MatSetValuesBlocked_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv) 278ab26458aSBarry Smith { 279ab26458aSBarry Smith Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 280dd6ea824SBarry Smith const PetscScalar *value; 281f15d580aSBarry Smith MatScalar *barray=baij->barray; 282ace3abfcSBarry Smith PetscBool roworiented = baij->roworiented; 283dfbe8321SBarry Smith PetscErrorCode ierr; 284899cda47SBarry Smith PetscInt i,j,ii,jj,row,col,rstart=baij->rstartbs; 285899cda47SBarry Smith PetscInt rend=baij->rendbs,cstart=baij->cstartbs,stepval; 286d0f46423SBarry Smith PetscInt cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2; 287ab26458aSBarry Smith 288b16ae2b1SBarry Smith PetscFunctionBegin; 28930793edcSSatish Balay if(!barray) { 29082502324SSatish Balay ierr = PetscMalloc(bs2*sizeof(MatScalar),&barray);CHKERRQ(ierr); 29182502324SSatish Balay baij->barray = barray; 29230793edcSSatish Balay } 29330793edcSSatish Balay 294ab26458aSBarry Smith if (roworiented) { 295ab26458aSBarry Smith stepval = (n-1)*bs; 296ab26458aSBarry Smith } else { 297ab26458aSBarry Smith stepval = (m-1)*bs; 298ab26458aSBarry Smith } 299ab26458aSBarry Smith for (i=0; i<m; i++) { 3005ef9f2a5SBarry Smith if (im[i] < 0) continue; 3012515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 302e32f2f54SBarry 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); 303ab26458aSBarry Smith #endif 304ab26458aSBarry Smith if (im[i] >= rstart && im[i] < rend) { 305ab26458aSBarry Smith row = im[i] - rstart; 306ab26458aSBarry Smith for (j=0; j<n; j++) { 30715b57d14SSatish Balay /* If NumCol = 1 then a copy is not required */ 30815b57d14SSatish Balay if ((roworiented) && (n == 1)) { 309f15d580aSBarry Smith barray = (MatScalar*)v + i*bs2; 31015b57d14SSatish Balay } else if((!roworiented) && (m == 1)) { 311f15d580aSBarry Smith barray = (MatScalar*)v + j*bs2; 31215b57d14SSatish Balay } else { /* Here a copy is required */ 313ab26458aSBarry Smith if (roworiented) { 31453ef36baSBarry Smith value = v + (i*(stepval+bs) + j)*bs; 315ab26458aSBarry Smith } else { 31653ef36baSBarry Smith value = v + (j*(stepval+bs) + i)*bs; 317abef11f7SSatish Balay } 31853ef36baSBarry Smith for (ii=0; ii<bs; ii++,value+=bs+stepval) { 31947513183SBarry Smith for (jj=0; jj<bs; jj++) { 32053ef36baSBarry Smith barray[jj] = value[jj]; 32147513183SBarry Smith } 32253ef36baSBarry Smith barray += bs; 32347513183SBarry Smith } 32430793edcSSatish Balay barray -= bs2; 32515b57d14SSatish Balay } 326abef11f7SSatish Balay 327abef11f7SSatish Balay if (in[j] >= cstart && in[j] < cend){ 328abef11f7SSatish Balay col = in[j] - cstart; 32997e5c40aSBarry Smith ierr = MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);CHKERRQ(ierr); 330ab26458aSBarry Smith } 3315ef9f2a5SBarry Smith else if (in[j] < 0) continue; 3322515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 333cb9801acSJed 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); 334ab26458aSBarry Smith #endif 335ab26458aSBarry Smith else { 336ab26458aSBarry Smith if (mat->was_assembled) { 337ab26458aSBarry Smith if (!baij->colmap) { 338ab26458aSBarry Smith ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr); 339ab26458aSBarry Smith } 340a5eb4965SSatish Balay 3412515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 342aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 343b24ad042SBarry Smith { PetscInt data; 3440f5bd95cSBarry Smith ierr = PetscTableFind(baij->colmap,in[j]+1,&data);CHKERRQ(ierr); 345e32f2f54SBarry Smith if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap"); 346fa46199cSSatish Balay } 34748e59246SSatish Balay #else 348e32f2f54SBarry Smith if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap"); 349a5eb4965SSatish Balay #endif 35048e59246SSatish Balay #endif 351aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 3520f5bd95cSBarry Smith ierr = PetscTableFind(baij->colmap,in[j]+1,&col);CHKERRQ(ierr); 353fa46199cSSatish Balay col = (col - 1)/bs; 35448e59246SSatish Balay #else 355a5eb4965SSatish Balay col = (baij->colmap[in[j]] - 1)/bs; 35648e59246SSatish Balay #endif 357ab26458aSBarry Smith if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) { 358ab26458aSBarry Smith ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr); 359ab26458aSBarry Smith col = in[j]; 360ab26458aSBarry Smith } 361ab26458aSBarry Smith } 362ab26458aSBarry Smith else col = in[j]; 36397e5c40aSBarry Smith ierr = MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);CHKERRQ(ierr); 364ab26458aSBarry Smith } 365ab26458aSBarry Smith } 366d64ed03dSBarry Smith } else { 3674cb17eb5SBarry Smith if (mat->nooffprocentries) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Setting off process row %D even though MatSetOption(,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) was set",im[i]); 368ab26458aSBarry Smith if (!baij->donotstash) { 369ff2fd236SBarry Smith if (roworiented) { 3706fa18ffdSBarry Smith ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr); 371ff2fd236SBarry Smith } else { 3726fa18ffdSBarry Smith ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr); 373ff2fd236SBarry Smith } 374abef11f7SSatish Balay } 375ab26458aSBarry Smith } 376ab26458aSBarry Smith } 3773a40ed3dSBarry Smith PetscFunctionReturn(0); 378ab26458aSBarry Smith } 3796fa18ffdSBarry Smith 3800bdbc534SSatish Balay #define HASH_KEY 0.6180339887 381b24ad042SBarry Smith #define HASH(size,key,tmp) (tmp = (key)*HASH_KEY,(PetscInt)((size)*(tmp-(PetscInt)tmp))) 382b24ad042SBarry Smith /* #define HASH(size,key) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */ 383b24ad042SBarry Smith /* #define HASH(size,key,tmp) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */ 3844a2ae208SSatish Balay #undef __FUNCT__ 38597e5c40aSBarry Smith #define __FUNCT__ "MatSetValues_MPIBAIJ_HT" 38697e5c40aSBarry Smith PetscErrorCode MatSetValues_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv) 3870bdbc534SSatish Balay { 3880bdbc534SSatish Balay Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 389ace3abfcSBarry Smith PetscBool roworiented = baij->roworiented; 390dfbe8321SBarry Smith PetscErrorCode ierr; 391b24ad042SBarry Smith PetscInt i,j,row,col; 392d0f46423SBarry Smith PetscInt rstart_orig=mat->rmap->rstart; 393d0f46423SBarry Smith PetscInt rend_orig=mat->rmap->rend,Nbs=baij->Nbs; 394d0f46423SBarry Smith PetscInt h1,key,size=baij->ht_size,bs=mat->rmap->bs,*HT=baij->ht,idx; 395329f5518SBarry Smith PetscReal tmp; 3963eda8832SBarry Smith MatScalar **HD = baij->hd,value; 3972515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 398b24ad042SBarry Smith PetscInt total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct; 3994a15367fSSatish Balay #endif 4000bdbc534SSatish Balay 4010bdbc534SSatish Balay PetscFunctionBegin; 40271fd2e92SBarry Smith if (v) PetscValidScalarPointer(v,6); 4030bdbc534SSatish Balay for (i=0; i<m; i++) { 4042515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 405e32f2f54SBarry Smith if (im[i] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row"); 406e32f2f54SBarry Smith if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1); 4070bdbc534SSatish Balay #endif 4080bdbc534SSatish Balay row = im[i]; 409c2760754SSatish Balay if (row >= rstart_orig && row < rend_orig) { 4100bdbc534SSatish Balay for (j=0; j<n; j++) { 4110bdbc534SSatish Balay col = in[j]; 4126fa18ffdSBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 413b24ad042SBarry Smith /* Look up PetscInto the Hash Table */ 414c2760754SSatish Balay key = (row/bs)*Nbs+(col/bs)+1; 415c2760754SSatish Balay h1 = HASH(size,key,tmp); 4160bdbc534SSatish Balay 417c2760754SSatish Balay 418c2760754SSatish Balay idx = h1; 4192515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 420187ce0cbSSatish Balay insert_ct++; 421187ce0cbSSatish Balay total_ct++; 422187ce0cbSSatish Balay if (HT[idx] != key) { 423187ce0cbSSatish Balay for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++); 424187ce0cbSSatish Balay if (idx == size) { 425187ce0cbSSatish Balay for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++); 426187ce0cbSSatish Balay if (idx == h1) { 427e32f2f54SBarry Smith SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col); 428187ce0cbSSatish Balay } 429187ce0cbSSatish Balay } 430187ce0cbSSatish Balay } 431187ce0cbSSatish Balay #else 432c2760754SSatish Balay if (HT[idx] != key) { 433c2760754SSatish Balay for (idx=h1; (idx<size) && (HT[idx]!=key); idx++); 434c2760754SSatish Balay if (idx == size) { 435c2760754SSatish Balay for (idx=0; (idx<h1) && (HT[idx]!=key); idx++); 436c2760754SSatish Balay if (idx == h1) { 437e32f2f54SBarry Smith SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col); 438c2760754SSatish Balay } 439c2760754SSatish Balay } 440c2760754SSatish Balay } 441187ce0cbSSatish Balay #endif 442c2760754SSatish Balay /* A HASH table entry is found, so insert the values at the correct address */ 443c2760754SSatish Balay if (addv == ADD_VALUES) *(HD[idx]+ (col % bs)*bs + (row % bs)) += value; 444c2760754SSatish Balay else *(HD[idx]+ (col % bs)*bs + (row % bs)) = value; 4450bdbc534SSatish Balay } 4460bdbc534SSatish Balay } else { 4470bdbc534SSatish Balay if (!baij->donotstash) { 448ff2fd236SBarry Smith if (roworiented) { 449b400d20cSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);CHKERRQ(ierr); 450ff2fd236SBarry Smith } else { 451b400d20cSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);CHKERRQ(ierr); 4520bdbc534SSatish Balay } 4530bdbc534SSatish Balay } 4540bdbc534SSatish Balay } 4550bdbc534SSatish Balay } 4562515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 457187ce0cbSSatish Balay baij->ht_total_ct = total_ct; 458187ce0cbSSatish Balay baij->ht_insert_ct = insert_ct; 459187ce0cbSSatish Balay #endif 4600bdbc534SSatish Balay PetscFunctionReturn(0); 4610bdbc534SSatish Balay } 4620bdbc534SSatish Balay 4634a2ae208SSatish Balay #undef __FUNCT__ 46497e5c40aSBarry Smith #define __FUNCT__ "MatSetValuesBlocked_MPIBAIJ_HT" 46597e5c40aSBarry Smith PetscErrorCode MatSetValuesBlocked_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv) 4660bdbc534SSatish Balay { 4670bdbc534SSatish Balay Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 468ace3abfcSBarry Smith PetscBool roworiented = baij->roworiented; 469dfbe8321SBarry Smith PetscErrorCode ierr; 470b24ad042SBarry Smith PetscInt i,j,ii,jj,row,col; 471899cda47SBarry Smith PetscInt rstart=baij->rstartbs; 472d0f46423SBarry Smith PetscInt rend=mat->rmap->rend,stepval,bs=mat->rmap->bs,bs2=baij->bs2,nbs2=n*bs2; 473b24ad042SBarry Smith PetscInt h1,key,size=baij->ht_size,idx,*HT=baij->ht,Nbs=baij->Nbs; 474329f5518SBarry Smith PetscReal tmp; 4753eda8832SBarry Smith MatScalar **HD = baij->hd,*baij_a; 476dd6ea824SBarry Smith const PetscScalar *v_t,*value; 4772515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 478b24ad042SBarry Smith PetscInt total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct; 4794a15367fSSatish Balay #endif 4800bdbc534SSatish Balay 481d0a41580SSatish Balay PetscFunctionBegin; 482d0a41580SSatish Balay 4830bdbc534SSatish Balay if (roworiented) { 4840bdbc534SSatish Balay stepval = (n-1)*bs; 4850bdbc534SSatish Balay } else { 4860bdbc534SSatish Balay stepval = (m-1)*bs; 4870bdbc534SSatish Balay } 4880bdbc534SSatish Balay for (i=0; i<m; i++) { 4892515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 490e32f2f54SBarry Smith if (im[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",im[i]); 491e32f2f54SBarry 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); 4920bdbc534SSatish Balay #endif 4930bdbc534SSatish Balay row = im[i]; 494ab715e2cSSatish Balay v_t = v + i*nbs2; 495c2760754SSatish Balay if (row >= rstart && row < rend) { 4960bdbc534SSatish Balay for (j=0; j<n; j++) { 4970bdbc534SSatish Balay col = in[j]; 4980bdbc534SSatish Balay 4990bdbc534SSatish Balay /* Look up into the Hash Table */ 500c2760754SSatish Balay key = row*Nbs+col+1; 501c2760754SSatish Balay h1 = HASH(size,key,tmp); 5020bdbc534SSatish Balay 503c2760754SSatish Balay idx = h1; 5042515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 505187ce0cbSSatish Balay total_ct++; 506187ce0cbSSatish Balay insert_ct++; 507187ce0cbSSatish Balay if (HT[idx] != key) { 508187ce0cbSSatish Balay for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++); 509187ce0cbSSatish Balay if (idx == size) { 510187ce0cbSSatish Balay for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++); 511187ce0cbSSatish Balay if (idx == h1) { 512e32f2f54SBarry Smith SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col); 513187ce0cbSSatish Balay } 514187ce0cbSSatish Balay } 515187ce0cbSSatish Balay } 516187ce0cbSSatish Balay #else 517c2760754SSatish Balay if (HT[idx] != key) { 518c2760754SSatish Balay for (idx=h1; (idx<size) && (HT[idx]!=key); idx++); 519c2760754SSatish Balay if (idx == size) { 520c2760754SSatish Balay for (idx=0; (idx<h1) && (HT[idx]!=key); idx++); 521c2760754SSatish Balay if (idx == h1) { 522e32f2f54SBarry Smith SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col); 523c2760754SSatish Balay } 524c2760754SSatish Balay } 525c2760754SSatish Balay } 526187ce0cbSSatish Balay #endif 527c2760754SSatish Balay baij_a = HD[idx]; 5280bdbc534SSatish Balay if (roworiented) { 529c2760754SSatish Balay /*value = v + i*(stepval+bs)*bs + j*bs;*/ 530187ce0cbSSatish Balay /* value = v + (i*(stepval+bs)+j)*bs; */ 531187ce0cbSSatish Balay value = v_t; 532187ce0cbSSatish Balay v_t += bs; 533fef45726SSatish Balay if (addv == ADD_VALUES) { 534c2760754SSatish Balay for (ii=0; ii<bs; ii++,value+=stepval) { 535c2760754SSatish Balay for (jj=ii; jj<bs2; jj+=bs) { 536fef45726SSatish Balay baij_a[jj] += *value++; 537b4cc0f5aSSatish Balay } 538b4cc0f5aSSatish Balay } 539fef45726SSatish Balay } else { 540c2760754SSatish Balay for (ii=0; ii<bs; ii++,value+=stepval) { 541c2760754SSatish Balay for (jj=ii; jj<bs2; jj+=bs) { 542fef45726SSatish Balay baij_a[jj] = *value++; 543fef45726SSatish Balay } 544fef45726SSatish Balay } 545fef45726SSatish Balay } 5460bdbc534SSatish Balay } else { 5470bdbc534SSatish Balay value = v + j*(stepval+bs)*bs + i*bs; 548fef45726SSatish Balay if (addv == ADD_VALUES) { 549b4cc0f5aSSatish Balay for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) { 5500bdbc534SSatish Balay for (jj=0; jj<bs; jj++) { 551fef45726SSatish Balay baij_a[jj] += *value++; 552fef45726SSatish Balay } 553fef45726SSatish Balay } 554fef45726SSatish Balay } else { 555fef45726SSatish Balay for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) { 556fef45726SSatish Balay for (jj=0; jj<bs; jj++) { 557fef45726SSatish Balay baij_a[jj] = *value++; 558fef45726SSatish Balay } 559b4cc0f5aSSatish Balay } 5600bdbc534SSatish Balay } 5610bdbc534SSatish Balay } 5620bdbc534SSatish Balay } 5630bdbc534SSatish Balay } else { 5640bdbc534SSatish Balay if (!baij->donotstash) { 5650bdbc534SSatish Balay if (roworiented) { 5668798bf22SSatish Balay ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr); 5670bdbc534SSatish Balay } else { 5688798bf22SSatish Balay ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr); 5690bdbc534SSatish Balay } 5700bdbc534SSatish Balay } 5710bdbc534SSatish Balay } 5720bdbc534SSatish Balay } 5732515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 574187ce0cbSSatish Balay baij->ht_total_ct = total_ct; 575187ce0cbSSatish Balay baij->ht_insert_ct = insert_ct; 576187ce0cbSSatish Balay #endif 5770bdbc534SSatish Balay PetscFunctionReturn(0); 5780bdbc534SSatish Balay } 579133cdb44SSatish Balay 5804a2ae208SSatish Balay #undef __FUNCT__ 5814a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIBAIJ" 582b24ad042SBarry Smith PetscErrorCode MatGetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[]) 583d6de1c52SSatish Balay { 584d6de1c52SSatish Balay Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 5856849ba73SBarry Smith PetscErrorCode ierr; 586d0f46423SBarry Smith PetscInt bs=mat->rmap->bs,i,j,bsrstart = mat->rmap->rstart,bsrend = mat->rmap->rend; 587d0f46423SBarry Smith PetscInt bscstart = mat->cmap->rstart,bscend = mat->cmap->rend,row,col,data; 588d6de1c52SSatish Balay 589133cdb44SSatish Balay PetscFunctionBegin; 590d6de1c52SSatish Balay for (i=0; i<m; i++) { 591e32f2f54SBarry Smith if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/ 592e32f2f54SBarry 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); 593d6de1c52SSatish Balay if (idxm[i] >= bsrstart && idxm[i] < bsrend) { 594d6de1c52SSatish Balay row = idxm[i] - bsrstart; 595d6de1c52SSatish Balay for (j=0; j<n; j++) { 596e32f2f54SBarry Smith if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */ 597e32f2f54SBarry 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); 598d6de1c52SSatish Balay if (idxn[j] >= bscstart && idxn[j] < bscend){ 599d6de1c52SSatish Balay col = idxn[j] - bscstart; 60098dd23e9SBarry Smith ierr = MatGetValues_SeqBAIJ(baij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 601d64ed03dSBarry Smith } else { 602905e6a2fSBarry Smith if (!baij->colmap) { 603905e6a2fSBarry Smith ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr); 604905e6a2fSBarry Smith } 605aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 6060f5bd95cSBarry Smith ierr = PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);CHKERRQ(ierr); 607fa46199cSSatish Balay data --; 60848e59246SSatish Balay #else 60948e59246SSatish Balay data = baij->colmap[idxn[j]/bs]-1; 61048e59246SSatish Balay #endif 61148e59246SSatish Balay if((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0; 612d9d09a02SSatish Balay else { 61348e59246SSatish Balay col = data + idxn[j]%bs; 61498dd23e9SBarry Smith ierr = MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 615d6de1c52SSatish Balay } 616d6de1c52SSatish Balay } 617d6de1c52SSatish Balay } 618d64ed03dSBarry Smith } else { 619e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported"); 620d6de1c52SSatish Balay } 621d6de1c52SSatish Balay } 6223a40ed3dSBarry Smith PetscFunctionReturn(0); 623d6de1c52SSatish Balay } 624d6de1c52SSatish Balay 6254a2ae208SSatish Balay #undef __FUNCT__ 6264a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIBAIJ" 627dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIBAIJ(Mat mat,NormType type,PetscReal *nrm) 628d6de1c52SSatish Balay { 629d6de1c52SSatish Balay Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 630d6de1c52SSatish Balay Mat_SeqBAIJ *amat = (Mat_SeqBAIJ*)baij->A->data,*bmat = (Mat_SeqBAIJ*)baij->B->data; 631dfbe8321SBarry Smith PetscErrorCode ierr; 632d0f46423SBarry Smith PetscInt i,j,bs2=baij->bs2,bs=baij->A->rmap->bs,nz,row,col; 633329f5518SBarry Smith PetscReal sum = 0.0; 6343eda8832SBarry Smith MatScalar *v; 635d6de1c52SSatish Balay 636d64ed03dSBarry Smith PetscFunctionBegin; 637d6de1c52SSatish Balay if (baij->size == 1) { 638064f8208SBarry Smith ierr = MatNorm(baij->A,type,nrm);CHKERRQ(ierr); 639d6de1c52SSatish Balay } else { 640d6de1c52SSatish Balay if (type == NORM_FROBENIUS) { 641d6de1c52SSatish Balay v = amat->a; 6428a62d963SHong Zhang nz = amat->nz*bs2; 6438a62d963SHong Zhang for (i=0; i<nz; i++) { 644aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 645329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 646d6de1c52SSatish Balay #else 647d6de1c52SSatish Balay sum += (*v)*(*v); v++; 648d6de1c52SSatish Balay #endif 649d6de1c52SSatish Balay } 650d6de1c52SSatish Balay v = bmat->a; 6518a62d963SHong Zhang nz = bmat->nz*bs2; 6528a62d963SHong Zhang for (i=0; i<nz; i++) { 653aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 654329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 655d6de1c52SSatish Balay #else 656d6de1c52SSatish Balay sum += (*v)*(*v); v++; 657d6de1c52SSatish Balay #endif 658d6de1c52SSatish Balay } 6597adad957SLisandro Dalcin ierr = MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr); 660064f8208SBarry Smith *nrm = sqrt(*nrm); 6618a62d963SHong Zhang } else if (type == NORM_1) { /* max column sum */ 6628a62d963SHong Zhang PetscReal *tmp,*tmp2; 663899cda47SBarry Smith PetscInt *jj,*garray=baij->garray,cstart=baij->rstartbs; 664fca92195SBarry Smith ierr = PetscMalloc2(mat->cmap->N,PetscReal,&tmp,mat->cmap->N,PetscReal,&tmp2);CHKERRQ(ierr); 665d0f46423SBarry Smith ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr); 6668a62d963SHong Zhang v = amat->a; jj = amat->j; 6678a62d963SHong Zhang for (i=0; i<amat->nz; i++) { 6688a62d963SHong Zhang for (j=0; j<bs; j++){ 6698a62d963SHong Zhang col = bs*(cstart + *jj) + j; /* column index */ 6708a62d963SHong Zhang for (row=0; row<bs; row++){ 6718a62d963SHong Zhang tmp[col] += PetscAbsScalar(*v); v++; 6728a62d963SHong Zhang } 6738a62d963SHong Zhang } 6748a62d963SHong Zhang jj++; 6758a62d963SHong Zhang } 6768a62d963SHong Zhang v = bmat->a; jj = bmat->j; 6778a62d963SHong Zhang for (i=0; i<bmat->nz; i++) { 6788a62d963SHong Zhang for (j=0; j<bs; j++){ 6798a62d963SHong Zhang col = bs*garray[*jj] + j; 6808a62d963SHong Zhang for (row=0; row<bs; row++){ 6818a62d963SHong Zhang tmp[col] += PetscAbsScalar(*v); v++; 6828a62d963SHong Zhang } 6838a62d963SHong Zhang } 6848a62d963SHong Zhang jj++; 6858a62d963SHong Zhang } 686d0f46423SBarry Smith ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr); 6878a62d963SHong Zhang *nrm = 0.0; 688d0f46423SBarry Smith for (j=0; j<mat->cmap->N; j++) { 6898a62d963SHong Zhang if (tmp2[j] > *nrm) *nrm = tmp2[j]; 6908a62d963SHong Zhang } 691fca92195SBarry Smith ierr = PetscFree2(tmp,tmp2);CHKERRQ(ierr); 6928a62d963SHong Zhang } else if (type == NORM_INFINITY) { /* max row sum */ 693577dd1f9SKris Buschelman PetscReal *sums; 694cb9801acSJed Brown ierr = PetscMalloc(bs*sizeof(PetscReal),&sums);CHKERRQ(ierr); 6958a62d963SHong Zhang sum = 0.0; 6968a62d963SHong Zhang for (j=0; j<amat->mbs; j++) { 6978a62d963SHong Zhang for (row=0; row<bs; row++) sums[row] = 0.0; 6988a62d963SHong Zhang v = amat->a + bs2*amat->i[j]; 6998a62d963SHong Zhang nz = amat->i[j+1]-amat->i[j]; 7008a62d963SHong Zhang for (i=0; i<nz; i++) { 7018a62d963SHong Zhang for (col=0; col<bs; col++){ 7028a62d963SHong Zhang for (row=0; row<bs; row++){ 7038a62d963SHong Zhang sums[row] += PetscAbsScalar(*v); v++; 7048a62d963SHong Zhang } 7058a62d963SHong Zhang } 7068a62d963SHong Zhang } 7078a62d963SHong Zhang v = bmat->a + bs2*bmat->i[j]; 7088a62d963SHong Zhang nz = bmat->i[j+1]-bmat->i[j]; 7098a62d963SHong Zhang for (i=0; i<nz; i++) { 7108a62d963SHong Zhang for (col=0; col<bs; col++){ 7118a62d963SHong Zhang for (row=0; row<bs; row++){ 7128a62d963SHong Zhang sums[row] += PetscAbsScalar(*v); v++; 7138a62d963SHong Zhang } 7148a62d963SHong Zhang } 7158a62d963SHong Zhang } 7168a62d963SHong Zhang for (row=0; row<bs; row++){ 7178a62d963SHong Zhang if (sums[row] > sum) sum = sums[row]; 7188a62d963SHong Zhang } 7198a62d963SHong Zhang } 7207adad957SLisandro Dalcin ierr = MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPI_MAX,((PetscObject)mat)->comm);CHKERRQ(ierr); 721577dd1f9SKris Buschelman ierr = PetscFree(sums);CHKERRQ(ierr); 722e7e72b3dSBarry Smith } else SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"No support for this norm yet"); 723d64ed03dSBarry Smith } 7243a40ed3dSBarry Smith PetscFunctionReturn(0); 725d6de1c52SSatish Balay } 72657b952d6SSatish Balay 727fef45726SSatish Balay /* 728fef45726SSatish Balay Creates the hash table, and sets the table 729fef45726SSatish Balay This table is created only once. 730fef45726SSatish Balay If new entried need to be added to the matrix 731fef45726SSatish Balay then the hash table has to be destroyed and 732fef45726SSatish Balay recreated. 733fef45726SSatish Balay */ 7344a2ae208SSatish Balay #undef __FUNCT__ 7354a2ae208SSatish Balay #define __FUNCT__ "MatCreateHashTable_MPIBAIJ_Private" 736dfbe8321SBarry Smith PetscErrorCode MatCreateHashTable_MPIBAIJ_Private(Mat mat,PetscReal factor) 737596b8d2eSBarry Smith { 738596b8d2eSBarry Smith Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 739596b8d2eSBarry Smith Mat A = baij->A,B=baij->B; 740596b8d2eSBarry Smith Mat_SeqBAIJ *a=(Mat_SeqBAIJ *)A->data,*b=(Mat_SeqBAIJ *)B->data; 741b24ad042SBarry Smith PetscInt i,j,k,nz=a->nz+b->nz,h1,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j; 7426849ba73SBarry Smith PetscErrorCode ierr; 743fca92195SBarry Smith PetscInt ht_size,bs2=baij->bs2,rstart=baij->rstartbs; 744899cda47SBarry Smith PetscInt cstart=baij->cstartbs,*garray=baij->garray,row,col,Nbs=baij->Nbs; 745b24ad042SBarry Smith PetscInt *HT,key; 7463eda8832SBarry Smith MatScalar **HD; 747329f5518SBarry Smith PetscReal tmp; 7486cf91177SBarry Smith #if defined(PETSC_USE_INFO) 749b24ad042SBarry Smith PetscInt ct=0,max=0; 7504a15367fSSatish Balay #endif 751fef45726SSatish Balay 752d64ed03dSBarry Smith PetscFunctionBegin; 753fca92195SBarry Smith if (baij->ht) PetscFunctionReturn(0); 754fef45726SSatish Balay 755fca92195SBarry Smith baij->ht_size = (PetscInt)(factor*nz); 756fca92195SBarry Smith ht_size = baij->ht_size; 7570bdbc534SSatish Balay 758fef45726SSatish Balay /* Allocate Memory for Hash Table */ 759fca92195SBarry Smith ierr = PetscMalloc2(ht_size,MatScalar*,&baij->hd,ht_size,PetscInt,&baij->ht);CHKERRQ(ierr); 760fca92195SBarry Smith ierr = PetscMemzero(baij->hd,ht_size*sizeof(MatScalar*));CHKERRQ(ierr); 761fca92195SBarry Smith ierr = PetscMemzero(baij->ht,ht_size*sizeof(PetscInt));CHKERRQ(ierr); 762b9e4cc15SSatish Balay HD = baij->hd; 763a07cd24cSSatish Balay HT = baij->ht; 764b9e4cc15SSatish Balay 765596b8d2eSBarry Smith /* Loop Over A */ 7660bdbc534SSatish Balay for (i=0; i<a->mbs; i++) { 767596b8d2eSBarry Smith for (j=ai[i]; j<ai[i+1]; j++) { 7680bdbc534SSatish Balay row = i+rstart; 7690bdbc534SSatish Balay col = aj[j]+cstart; 770596b8d2eSBarry Smith 771187ce0cbSSatish Balay key = row*Nbs + col + 1; 772fca92195SBarry Smith h1 = HASH(ht_size,key,tmp); 773fca92195SBarry Smith for (k=0; k<ht_size; k++){ 774fca92195SBarry Smith if (!HT[(h1+k)%ht_size]) { 775fca92195SBarry Smith HT[(h1+k)%ht_size] = key; 776fca92195SBarry Smith HD[(h1+k)%ht_size] = a->a + j*bs2; 777596b8d2eSBarry Smith break; 7786cf91177SBarry Smith #if defined(PETSC_USE_INFO) 779187ce0cbSSatish Balay } else { 780187ce0cbSSatish Balay ct++; 781187ce0cbSSatish Balay #endif 782596b8d2eSBarry Smith } 783187ce0cbSSatish Balay } 7846cf91177SBarry Smith #if defined(PETSC_USE_INFO) 785187ce0cbSSatish Balay if (k> max) max = k; 786187ce0cbSSatish Balay #endif 787596b8d2eSBarry Smith } 788596b8d2eSBarry Smith } 789596b8d2eSBarry Smith /* Loop Over B */ 7900bdbc534SSatish Balay for (i=0; i<b->mbs; i++) { 791596b8d2eSBarry Smith for (j=bi[i]; j<bi[i+1]; j++) { 7920bdbc534SSatish Balay row = i+rstart; 7930bdbc534SSatish Balay col = garray[bj[j]]; 794187ce0cbSSatish Balay key = row*Nbs + col + 1; 795fca92195SBarry Smith h1 = HASH(ht_size,key,tmp); 796fca92195SBarry Smith for (k=0; k<ht_size; k++){ 797fca92195SBarry Smith if (!HT[(h1+k)%ht_size]) { 798fca92195SBarry Smith HT[(h1+k)%ht_size] = key; 799fca92195SBarry Smith HD[(h1+k)%ht_size] = b->a + j*bs2; 800596b8d2eSBarry Smith break; 8016cf91177SBarry Smith #if defined(PETSC_USE_INFO) 802187ce0cbSSatish Balay } else { 803187ce0cbSSatish Balay ct++; 804187ce0cbSSatish Balay #endif 805596b8d2eSBarry Smith } 806187ce0cbSSatish Balay } 8076cf91177SBarry Smith #if defined(PETSC_USE_INFO) 808187ce0cbSSatish Balay if (k> max) max = k; 809187ce0cbSSatish Balay #endif 810596b8d2eSBarry Smith } 811596b8d2eSBarry Smith } 812596b8d2eSBarry Smith 813596b8d2eSBarry Smith /* Print Summary */ 8146cf91177SBarry Smith #if defined(PETSC_USE_INFO) 815fca92195SBarry Smith for (i=0,j=0; i<ht_size; i++) { 816596b8d2eSBarry Smith if (HT[i]) {j++;} 817c38d4ed2SBarry Smith } 8181e2582c4SBarry Smith ierr = PetscInfo2(mat,"Average Search = %5.2f,max search = %D\n",(!j)? 0.0:((PetscReal)(ct+j))/j,max);CHKERRQ(ierr); 819187ce0cbSSatish Balay #endif 8203a40ed3dSBarry Smith PetscFunctionReturn(0); 821596b8d2eSBarry Smith } 82257b952d6SSatish Balay 8234a2ae208SSatish Balay #undef __FUNCT__ 8244a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIBAIJ" 825dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIBAIJ(Mat mat,MatAssemblyType mode) 826bbb85fb3SSatish Balay { 827bbb85fb3SSatish Balay Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 828dfbe8321SBarry Smith PetscErrorCode ierr; 829b24ad042SBarry Smith PetscInt nstash,reallocs; 830bbb85fb3SSatish Balay InsertMode addv; 831bbb85fb3SSatish Balay 832bbb85fb3SSatish Balay PetscFunctionBegin; 8334cb17eb5SBarry Smith if (baij->donotstash || mat->nooffprocentries) { 834bbb85fb3SSatish Balay PetscFunctionReturn(0); 835bbb85fb3SSatish Balay } 836bbb85fb3SSatish Balay 837bbb85fb3SSatish Balay /* make sure all processors are either in INSERTMODE or ADDMODE */ 8387adad957SLisandro Dalcin ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,((PetscObject)mat)->comm);CHKERRQ(ierr); 839e7e72b3dSBarry Smith if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added"); 840bbb85fb3SSatish Balay mat->insertmode = addv; /* in case this processor had no cache */ 841bbb85fb3SSatish Balay 842d0f46423SBarry Smith ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr); 8431e2582c4SBarry Smith ierr = MatStashScatterBegin_Private(mat,&mat->bstash,baij->rangebs);CHKERRQ(ierr); 8448798bf22SSatish Balay ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr); 8451e2582c4SBarry Smith ierr = PetscInfo2(mat,"Stash has %D entries,uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr); 84646680499SSatish Balay ierr = MatStashGetInfo_Private(&mat->bstash,&nstash,&reallocs);CHKERRQ(ierr); 8471e2582c4SBarry Smith ierr = PetscInfo2(mat,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr); 848bbb85fb3SSatish Balay PetscFunctionReturn(0); 849bbb85fb3SSatish Balay } 850bbb85fb3SSatish Balay 8514a2ae208SSatish Balay #undef __FUNCT__ 8524a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIBAIJ" 853dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIBAIJ(Mat mat,MatAssemblyType mode) 854bbb85fb3SSatish Balay { 855bbb85fb3SSatish Balay Mat_MPIBAIJ *baij=(Mat_MPIBAIJ*)mat->data; 85691c97fd4SSatish Balay Mat_SeqBAIJ *a=(Mat_SeqBAIJ*)baij->A->data; 8576849ba73SBarry Smith PetscErrorCode ierr; 858b24ad042SBarry Smith PetscInt i,j,rstart,ncols,flg,bs2=baij->bs2; 859e44c0bd4SBarry Smith PetscInt *row,*col; 860ace3abfcSBarry Smith PetscBool r1,r2,r3,other_disassembled; 8613eda8832SBarry Smith MatScalar *val; 862bbb85fb3SSatish Balay InsertMode addv = mat->insertmode; 863b24ad042SBarry Smith PetscMPIInt n; 864bbb85fb3SSatish Balay 86591c97fd4SSatish Balay /* do not use 'b=(Mat_SeqBAIJ*)baij->B->data' as B can be reset in disassembly */ 866bbb85fb3SSatish Balay PetscFunctionBegin; 8674cb17eb5SBarry Smith if (!baij->donotstash && !mat->nooffprocentries) { 868a2d1c673SSatish Balay while (1) { 8698798bf22SSatish Balay ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr); 870a2d1c673SSatish Balay if (!flg) break; 871a2d1c673SSatish Balay 872bbb85fb3SSatish Balay for (i=0; i<n;) { 873bbb85fb3SSatish Balay /* Now identify the consecutive vals belonging to the same row */ 874bbb85fb3SSatish Balay for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; } 875bbb85fb3SSatish Balay if (j < n) ncols = j-i; 876bbb85fb3SSatish Balay else ncols = n-i; 877bbb85fb3SSatish Balay /* Now assemble all these values with a single function call */ 87897e5c40aSBarry Smith ierr = MatSetValues_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr); 879bbb85fb3SSatish Balay i = j; 880bbb85fb3SSatish Balay } 881bbb85fb3SSatish Balay } 8828798bf22SSatish Balay ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr); 883a2d1c673SSatish Balay /* Now process the block-stash. Since the values are stashed column-oriented, 884a2d1c673SSatish Balay set the roworiented flag to column oriented, and after MatSetValues() 885a2d1c673SSatish Balay restore the original flags */ 886a2d1c673SSatish Balay r1 = baij->roworiented; 887a2d1c673SSatish Balay r2 = a->roworiented; 88891c97fd4SSatish Balay r3 = ((Mat_SeqBAIJ*)baij->B->data)->roworiented; 8897c922b88SBarry Smith baij->roworiented = PETSC_FALSE; 8907c922b88SBarry Smith a->roworiented = PETSC_FALSE; 89191c97fd4SSatish Balay (((Mat_SeqBAIJ*)baij->B->data))->roworiented = PETSC_FALSE; /* b->roworiented */ 892a2d1c673SSatish Balay while (1) { 8938798bf22SSatish Balay ierr = MatStashScatterGetMesg_Private(&mat->bstash,&n,&row,&col,&val,&flg);CHKERRQ(ierr); 894a2d1c673SSatish Balay if (!flg) break; 895a2d1c673SSatish Balay 896a2d1c673SSatish Balay for (i=0; i<n;) { 897a2d1c673SSatish Balay /* Now identify the consecutive vals belonging to the same row */ 898a2d1c673SSatish Balay for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; } 899a2d1c673SSatish Balay if (j < n) ncols = j-i; 900a2d1c673SSatish Balay else ncols = n-i; 90197e5c40aSBarry Smith ierr = MatSetValuesBlocked_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i*bs2,addv);CHKERRQ(ierr); 902a2d1c673SSatish Balay i = j; 903a2d1c673SSatish Balay } 904a2d1c673SSatish Balay } 9058798bf22SSatish Balay ierr = MatStashScatterEnd_Private(&mat->bstash);CHKERRQ(ierr); 906a2d1c673SSatish Balay baij->roworiented = r1; 907a2d1c673SSatish Balay a->roworiented = r2; 90891c97fd4SSatish Balay ((Mat_SeqBAIJ*)baij->B->data)->roworiented = r3; /* b->roworiented */ 909bbb85fb3SSatish Balay } 910bbb85fb3SSatish Balay 911bbb85fb3SSatish Balay ierr = MatAssemblyBegin(baij->A,mode);CHKERRQ(ierr); 912bbb85fb3SSatish Balay ierr = MatAssemblyEnd(baij->A,mode);CHKERRQ(ierr); 913bbb85fb3SSatish Balay 914bbb85fb3SSatish Balay /* determine if any processor has disassembled, if so we must 915bbb85fb3SSatish Balay also disassemble ourselfs, in order that we may reassemble. */ 916bbb85fb3SSatish Balay /* 917bbb85fb3SSatish Balay if nonzero structure of submatrix B cannot change then we know that 918bbb85fb3SSatish Balay no processor disassembled thus we can skip this stuff 919bbb85fb3SSatish Balay */ 920bbb85fb3SSatish Balay if (!((Mat_SeqBAIJ*)baij->B->data)->nonew) { 9217adad957SLisandro Dalcin ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,((PetscObject)mat)->comm);CHKERRQ(ierr); 922bbb85fb3SSatish Balay if (mat->was_assembled && !other_disassembled) { 923bbb85fb3SSatish Balay ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr); 924bbb85fb3SSatish Balay } 925bbb85fb3SSatish Balay } 926bbb85fb3SSatish Balay 927bbb85fb3SSatish Balay if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) { 928bbb85fb3SSatish Balay ierr = MatSetUpMultiply_MPIBAIJ(mat);CHKERRQ(ierr); 929bbb85fb3SSatish Balay } 93091c97fd4SSatish Balay ((Mat_SeqBAIJ*)baij->B->data)->compressedrow.use = PETSC_TRUE; /* b->compressedrow.use */ 931bbb85fb3SSatish Balay ierr = MatAssemblyBegin(baij->B,mode);CHKERRQ(ierr); 932bbb85fb3SSatish Balay ierr = MatAssemblyEnd(baij->B,mode);CHKERRQ(ierr); 933bbb85fb3SSatish Balay 9346cf91177SBarry Smith #if defined(PETSC_USE_INFO) 935bbb85fb3SSatish Balay if (baij->ht && mode== MAT_FINAL_ASSEMBLY) { 9361e2582c4SBarry Smith ierr = PetscInfo1(mat,"Average Hash Table Search in MatSetValues = %5.2f\n",((PetscReal)baij->ht_total_ct)/baij->ht_insert_ct);CHKERRQ(ierr); 937bbb85fb3SSatish Balay baij->ht_total_ct = 0; 938bbb85fb3SSatish Balay baij->ht_insert_ct = 0; 939bbb85fb3SSatish Balay } 940bbb85fb3SSatish Balay #endif 941bbb85fb3SSatish Balay if (baij->ht_flag && !baij->ht && mode == MAT_FINAL_ASSEMBLY) { 942bbb85fb3SSatish Balay ierr = MatCreateHashTable_MPIBAIJ_Private(mat,baij->ht_fact);CHKERRQ(ierr); 943bbb85fb3SSatish Balay mat->ops->setvalues = MatSetValues_MPIBAIJ_HT; 944bbb85fb3SSatish Balay mat->ops->setvaluesblocked = MatSetValuesBlocked_MPIBAIJ_HT; 945bbb85fb3SSatish Balay } 946bbb85fb3SSatish Balay 947fca92195SBarry Smith ierr = PetscFree2(baij->rowvalues,baij->rowindices);CHKERRQ(ierr); 948606d414cSSatish Balay baij->rowvalues = 0; 949bbb85fb3SSatish Balay PetscFunctionReturn(0); 950bbb85fb3SSatish Balay } 95157b952d6SSatish Balay 9524a2ae208SSatish Balay #undef __FUNCT__ 9534a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIBAIJ_ASCIIorDraworSocket" 9546849ba73SBarry Smith static PetscErrorCode MatView_MPIBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer) 95557b952d6SSatish Balay { 95657b952d6SSatish Balay Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 957dfbe8321SBarry Smith PetscErrorCode ierr; 958b24ad042SBarry Smith PetscMPIInt size = baij->size,rank = baij->rank; 959d0f46423SBarry Smith PetscInt bs = mat->rmap->bs; 960ace3abfcSBarry Smith PetscBool iascii,isdraw; 961b0a32e0cSBarry Smith PetscViewer sviewer; 962f3ef73ceSBarry Smith PetscViewerFormat format; 96357b952d6SSatish Balay 964d64ed03dSBarry Smith PetscFunctionBegin; 9652692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 9662692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 96732077d6dSBarry Smith if (iascii) { 968b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 969456192e2SBarry Smith if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 9704e220ebcSLois Curfman McInnes MatInfo info; 9717adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr); 972d41123aaSBarry Smith ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr); 97377431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D bs %D mem %D\n", 97416608c43SJed Brown rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,mat->rmap->bs,(PetscInt)info.memory);CHKERRQ(ierr); 975d132466eSBarry Smith ierr = MatGetInfo(baij->A,MAT_LOCAL,&info);CHKERRQ(ierr); 976e6dd01d4SJed Brown ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 977d132466eSBarry Smith ierr = MatGetInfo(baij->B,MAT_LOCAL,&info);CHKERRQ(ierr); 978e6dd01d4SJed Brown ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 979b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 98007d81ca4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr); 98157b952d6SSatish Balay ierr = VecScatterView(baij->Mvctx,viewer);CHKERRQ(ierr); 9823a40ed3dSBarry Smith PetscFunctionReturn(0); 983fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_INFO) { 98477431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," block size is %D\n",bs);CHKERRQ(ierr); 9853a40ed3dSBarry Smith PetscFunctionReturn(0); 98604929863SHong Zhang } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) { 98704929863SHong Zhang PetscFunctionReturn(0); 98857b952d6SSatish Balay } 98957b952d6SSatish Balay } 99057b952d6SSatish Balay 9910f5bd95cSBarry Smith if (isdraw) { 992b0a32e0cSBarry Smith PetscDraw draw; 993ace3abfcSBarry Smith PetscBool isnull; 994b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 995b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 99657b952d6SSatish Balay } 99757b952d6SSatish Balay 99857b952d6SSatish Balay if (size == 1) { 9997adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)baij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 100057b952d6SSatish Balay ierr = MatView(baij->A,viewer);CHKERRQ(ierr); 1001d64ed03dSBarry Smith } else { 100257b952d6SSatish Balay /* assemble the entire matrix onto first processor. */ 100357b952d6SSatish Balay Mat A; 100457b952d6SSatish Balay Mat_SeqBAIJ *Aloc; 1005d0f46423SBarry Smith PetscInt M = mat->rmap->N,N = mat->cmap->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs; 10063eda8832SBarry Smith MatScalar *a; 100757b952d6SSatish Balay 1008f204ca49SKris Buschelman /* Here we are creating a temporary matrix, so will assume MPIBAIJ is acceptable */ 1009f204ca49SKris Buschelman /* Perhaps this should be the type of mat? */ 10107adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)mat)->comm,&A);CHKERRQ(ierr); 101157b952d6SSatish Balay if (!rank) { 1012f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr); 1013d64ed03dSBarry Smith } else { 1014f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr); 101557b952d6SSatish Balay } 1016f204ca49SKris Buschelman ierr = MatSetType(A,MATMPIBAIJ);CHKERRQ(ierr); 1017d0f46423SBarry Smith ierr = MatMPIBAIJSetPreallocation(A,mat->rmap->bs,0,PETSC_NULL,0,PETSC_NULL);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); 106557b952d6SSatish Balay 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; 1078660746e0SBarry Smith PetscInt i,*row_lens,*crow_lens,bs = mat->rmap->bs,count,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; 1084*638eb2ebSBarry 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 count = 0; 1110660746e0SBarry Smith for (i=0; i<a->mbs; i++) { 1111660746e0SBarry Smith crow_lens[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i]; 1112660746e0SBarry Smith } 1113660746e0SBarry Smith /* store the row lengths to the file */ 1114*638eb2ebSBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1115660746e0SBarry Smith if (!rank) { 1116660746e0SBarry Smith MPI_Status status; 1117660746e0SBarry Smith ierr = PetscMalloc(rlen*sizeof(PetscInt),&row_lens);CHKERRQ(ierr); 1118660746e0SBarry Smith rlen = (range[1] - range[0])/bs; 1119660746e0SBarry Smith for (i=0; i<rlen; i++) { 1120660746e0SBarry Smith for (j=0; j<bs; j++) { 1121660746e0SBarry Smith row_lens[i*bs+j] = bs*crow_lens[i]; 1122660746e0SBarry Smith } 1123660746e0SBarry Smith } 1124660746e0SBarry Smith ierr = PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 1125660746e0SBarry Smith for (i=1; i<size; i++) { 1126660746e0SBarry Smith rlen = (range[i+1] - range[i])/bs; 1127*638eb2ebSBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 1128660746e0SBarry Smith ierr = MPI_Recv(crow_lens,rlen,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1129660746e0SBarry Smith for (k=0; k<rlen; k++) { 1130660746e0SBarry Smith for (j=0; j<bs; j++) { 1131660746e0SBarry Smith row_lens[k*bs+j] = bs*crow_lens[k]; 1132660746e0SBarry Smith } 1133660746e0SBarry Smith } 1134660746e0SBarry Smith ierr = PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 1135660746e0SBarry Smith } 1136*638eb2ebSBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 1137660746e0SBarry Smith ierr = PetscFree(row_lens);CHKERRQ(ierr); 1138660746e0SBarry Smith } else { 1139*638eb2ebSBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 1140660746e0SBarry Smith ierr = MPI_Send(crow_lens,mat->rmap->n/bs,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 1141*638eb2ebSBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 1142660746e0SBarry Smith } 1143660746e0SBarry Smith ierr = PetscFree(crow_lens);CHKERRQ(ierr); 1144660746e0SBarry Smith 1145660746e0SBarry 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 1146660746e0SBarry Smith information needed to make it for each row from a block row. This does require more communication but still not more than 1147660746e0SBarry Smith the communication needed for the nonzero values */ 1148660746e0SBarry Smith nzmax = nz; /* space a largest processor needs */ 1149660746e0SBarry Smith ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 1150660746e0SBarry Smith ierr = PetscMalloc(nzmax*sizeof(PetscInt),&column_indices);CHKERRQ(ierr); 1151660746e0SBarry Smith cnt = 0; 1152660746e0SBarry Smith for (i=0; i<a->mbs; i++) { 1153660746e0SBarry Smith pcnt = cnt; 1154660746e0SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 1155660746e0SBarry Smith if ( (col = garray[B->j[j]]) > cstart) break; 1156660746e0SBarry Smith for (l=0; l<bs; l++) { 1157660746e0SBarry Smith column_indices[cnt++] = bs*col+l; 1158660746e0SBarry Smith } 1159660746e0SBarry Smith } 1160660746e0SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 1161660746e0SBarry Smith for (l=0; l<bs; l++) { 1162660746e0SBarry Smith column_indices[cnt++] = bs*(A->j[k] + cstart)+l; 1163660746e0SBarry Smith } 1164660746e0SBarry Smith } 1165660746e0SBarry Smith for (; j<B->i[i+1]; j++) { 1166660746e0SBarry Smith for (l=0; l<bs; l++) { 1167660746e0SBarry Smith column_indices[cnt++] = bs*garray[B->j[j]]+l; 1168660746e0SBarry Smith } 1169660746e0SBarry Smith } 1170660746e0SBarry Smith len = cnt - pcnt; 1171660746e0SBarry Smith for (k=1; k<bs; k++) { 1172660746e0SBarry Smith ierr = PetscMemcpy(&column_indices[cnt],&column_indices[pcnt],len*sizeof(PetscInt));CHKERRQ(ierr); 1173660746e0SBarry Smith cnt += len; 1174660746e0SBarry Smith } 1175660746e0SBarry Smith } 1176660746e0SBarry Smith if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz); 1177660746e0SBarry Smith 1178660746e0SBarry Smith /* store the columns to the file */ 1179*638eb2ebSBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1180660746e0SBarry Smith if (!rank) { 1181660746e0SBarry Smith MPI_Status status; 1182660746e0SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 1183660746e0SBarry Smith for (i=1; i<size; i++) { 1184*638eb2ebSBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 1185660746e0SBarry Smith ierr = MPI_Recv(&cnt,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1186660746e0SBarry Smith ierr = MPI_Recv(column_indices,cnt,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1187660746e0SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,cnt,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 1188660746e0SBarry Smith } 1189*638eb2ebSBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 1190660746e0SBarry Smith } else { 1191*638eb2ebSBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 1192660746e0SBarry Smith ierr = MPI_Send(&cnt,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 1193660746e0SBarry Smith ierr = MPI_Send(column_indices,cnt,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 1194*638eb2ebSBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 1195660746e0SBarry Smith } 1196660746e0SBarry Smith ierr = PetscFree(column_indices);CHKERRQ(ierr); 1197660746e0SBarry Smith 1198660746e0SBarry Smith /* load up the numerical values */ 1199660746e0SBarry Smith ierr = PetscMalloc(nzmax*sizeof(PetscScalar),&column_values);CHKERRQ(ierr); 1200660746e0SBarry Smith cnt = 0; 1201660746e0SBarry Smith for (i=0; i<a->mbs; i++) { 1202660746e0SBarry Smith rlen = bs*(B->i[i+1] - B->i[i] + A->i[i+1] - A->i[i]); 1203660746e0SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 1204660746e0SBarry Smith if ( garray[B->j[j]] > cstart) break; 1205660746e0SBarry Smith for (l=0; l<bs; l++) { 1206660746e0SBarry Smith for (ll=0; ll<bs; ll++) { 1207660746e0SBarry Smith column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll]; 1208660746e0SBarry Smith } 1209660746e0SBarry Smith } 1210660746e0SBarry Smith cnt += bs; 1211660746e0SBarry Smith } 1212660746e0SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 1213660746e0SBarry Smith for (l=0; l<bs; l++) { 1214660746e0SBarry Smith for (ll=0; ll<bs; ll++) { 1215660746e0SBarry Smith column_values[cnt + l*rlen + ll] = A->a[bs2*k+l+bs*ll]; 1216660746e0SBarry Smith } 1217660746e0SBarry Smith } 1218660746e0SBarry Smith cnt += bs; 1219660746e0SBarry Smith } 1220660746e0SBarry Smith for (; j<B->i[i+1]; j++) { 1221660746e0SBarry Smith for (l=0; l<bs; l++) { 1222660746e0SBarry Smith for (ll=0; ll<bs; ll++) { 1223660746e0SBarry Smith column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll]; 1224660746e0SBarry Smith } 1225660746e0SBarry Smith } 1226660746e0SBarry Smith cnt += bs; 1227660746e0SBarry Smith } 1228660746e0SBarry Smith cnt += (bs-1)*rlen; 1229660746e0SBarry Smith } 1230660746e0SBarry Smith if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz); 1231660746e0SBarry Smith 1232660746e0SBarry Smith /* store the column values to the file */ 1233*638eb2ebSBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1234660746e0SBarry Smith if (!rank) { 1235660746e0SBarry Smith MPI_Status status; 1236660746e0SBarry Smith ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 1237660746e0SBarry Smith for (i=1; i<size; i++) { 1238*638eb2ebSBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 1239660746e0SBarry Smith ierr = MPI_Recv(&cnt,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1240660746e0SBarry Smith ierr = MPI_Recv(column_values,cnt,MPIU_SCALAR,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1241660746e0SBarry Smith ierr = PetscBinaryWrite(fd,column_values,cnt,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 1242660746e0SBarry Smith } 1243*638eb2ebSBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 1244660746e0SBarry Smith } else { 1245*638eb2ebSBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 1246660746e0SBarry Smith ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 1247660746e0SBarry Smith ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 1248*638eb2ebSBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 1249660746e0SBarry Smith } 1250660746e0SBarry Smith ierr = PetscFree(column_values);CHKERRQ(ierr); 1251660746e0SBarry Smith 1252660746e0SBarry Smith ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr); 1253660746e0SBarry Smith if (file) { 1254660746e0SBarry Smith fprintf(file,"-matload_block_size %d\n",(int)mat->rmap->bs); 1255660746e0SBarry Smith } 1256660746e0SBarry Smith PetscFunctionReturn(0); 1257660746e0SBarry Smith } 1258660746e0SBarry Smith 1259660746e0SBarry Smith #undef __FUNCT__ 12604a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIBAIJ" 1261dfbe8321SBarry Smith PetscErrorCode MatView_MPIBAIJ(Mat mat,PetscViewer viewer) 126257b952d6SSatish Balay { 1263dfbe8321SBarry Smith PetscErrorCode ierr; 1264ace3abfcSBarry Smith PetscBool iascii,isdraw,issocket,isbinary; 126557b952d6SSatish Balay 1266d64ed03dSBarry Smith PetscFunctionBegin; 12672692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 12682692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 12692692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr); 12702692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 1271660746e0SBarry Smith if (iascii || isdraw || issocket) { 12727b2a1423SBarry Smith ierr = MatView_MPIBAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr); 1273660746e0SBarry Smith } else if (isbinary) { 1274660746e0SBarry Smith ierr = MatView_MPIBAIJ_Binary(mat,viewer);CHKERRQ(ierr); 12755cd90555SBarry Smith } else { 127665e19b50SBarry Smith SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Viewer type %s not supported by MPIBAIJ matrices",((PetscObject)viewer)->type_name); 127757b952d6SSatish Balay } 12783a40ed3dSBarry Smith PetscFunctionReturn(0); 127957b952d6SSatish Balay } 128057b952d6SSatish Balay 12814a2ae208SSatish Balay #undef __FUNCT__ 12824a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIBAIJ" 1283dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIBAIJ(Mat mat) 128479bdfe76SSatish Balay { 128579bdfe76SSatish Balay Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 1286dfbe8321SBarry Smith PetscErrorCode ierr; 128779bdfe76SSatish Balay 1288d64ed03dSBarry Smith PetscFunctionBegin; 1289aa482453SBarry Smith #if defined(PETSC_USE_LOG) 1290d0f46423SBarry Smith PetscLogObjectState((PetscObject)mat,"Rows=%D,Cols=%D",mat->rmap->N,mat->cmap->N); 129179bdfe76SSatish Balay #endif 12928798bf22SSatish Balay ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr); 12938798bf22SSatish Balay ierr = MatStashDestroy_Private(&mat->bstash);CHKERRQ(ierr); 129413ea16ffSHong Zhang if (baij->A){ierr = MatDestroy(baij->A);CHKERRQ(ierr);} 129513ea16ffSHong Zhang if (baij->B){ierr = MatDestroy(baij->B);CHKERRQ(ierr);} 1296aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 12979c666560SBarry Smith if (baij->colmap) {ierr = PetscTableDestroy(baij->colmap);CHKERRQ(ierr);} 129848e59246SSatish Balay #else 129905b42c5fSBarry Smith ierr = PetscFree(baij->colmap);CHKERRQ(ierr); 130048e59246SSatish Balay #endif 130105b42c5fSBarry Smith ierr = PetscFree(baij->garray);CHKERRQ(ierr); 1302606d414cSSatish Balay if (baij->lvec) {ierr = VecDestroy(baij->lvec);CHKERRQ(ierr);} 1303606d414cSSatish Balay if (baij->Mvctx) {ierr = VecScatterDestroy(baij->Mvctx);CHKERRQ(ierr);} 1304fca92195SBarry Smith ierr = PetscFree2(baij->rowvalues,baij->rowindices);CHKERRQ(ierr); 130505b42c5fSBarry Smith ierr = PetscFree(baij->barray);CHKERRQ(ierr); 1306fca92195SBarry Smith ierr = PetscFree2(baij->hd,baij->ht);CHKERRQ(ierr); 1307899cda47SBarry Smith ierr = PetscFree(baij->rangebs);CHKERRQ(ierr); 1308606d414cSSatish Balay ierr = PetscFree(baij);CHKERRQ(ierr); 1309901853e0SKris Buschelman 1310dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr); 1311901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr); 1312901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr); 1313901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);CHKERRQ(ierr); 1314901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr); 1315aac34f13SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr); 1316901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);CHKERRQ(ierr); 1317901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatSetHashTableFactor_C","",PETSC_NULL);CHKERRQ(ierr); 13183a40ed3dSBarry Smith PetscFunctionReturn(0); 131979bdfe76SSatish Balay } 132079bdfe76SSatish Balay 13214a2ae208SSatish Balay #undef __FUNCT__ 13224a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIBAIJ" 1323dfbe8321SBarry Smith PetscErrorCode MatMult_MPIBAIJ(Mat A,Vec xx,Vec yy) 1324cee3aa6bSSatish Balay { 1325cee3aa6bSSatish Balay Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data; 1326dfbe8321SBarry Smith PetscErrorCode ierr; 1327b24ad042SBarry Smith PetscInt nt; 1328cee3aa6bSSatish Balay 1329d64ed03dSBarry Smith PetscFunctionBegin; 1330e1311b90SBarry Smith ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr); 1331e7e72b3dSBarry Smith if (nt != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx"); 1332e1311b90SBarry Smith ierr = VecGetLocalSize(yy,&nt);CHKERRQ(ierr); 1333e7e72b3dSBarry Smith if (nt != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible parition of A and yy"); 1334ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1335f830108cSBarry Smith ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr); 1336ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1337f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr); 13383a40ed3dSBarry Smith PetscFunctionReturn(0); 1339cee3aa6bSSatish Balay } 1340cee3aa6bSSatish Balay 13414a2ae208SSatish Balay #undef __FUNCT__ 13424a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIBAIJ" 1343dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1344cee3aa6bSSatish Balay { 1345cee3aa6bSSatish Balay Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data; 1346dfbe8321SBarry Smith PetscErrorCode ierr; 1347d64ed03dSBarry Smith 1348d64ed03dSBarry Smith PetscFunctionBegin; 1349ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1350f830108cSBarry Smith ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 1351ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1352f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr); 13533a40ed3dSBarry Smith PetscFunctionReturn(0); 1354cee3aa6bSSatish Balay } 1355cee3aa6bSSatish Balay 13564a2ae208SSatish Balay #undef __FUNCT__ 13574a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIBAIJ" 1358dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIBAIJ(Mat A,Vec xx,Vec yy) 1359cee3aa6bSSatish Balay { 1360cee3aa6bSSatish Balay Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data; 1361dfbe8321SBarry Smith PetscErrorCode ierr; 1362ace3abfcSBarry Smith PetscBool merged; 1363cee3aa6bSSatish Balay 1364d64ed03dSBarry Smith PetscFunctionBegin; 1365a5ff213dSBarry Smith ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr); 1366cee3aa6bSSatish Balay /* do nondiagonal part */ 13677c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 1368a5ff213dSBarry Smith if (!merged) { 1369cee3aa6bSSatish Balay /* send it on its way */ 1370ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1371cee3aa6bSSatish Balay /* do local part */ 13727c922b88SBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 1373cee3aa6bSSatish Balay /* receive remote parts: note this assumes the values are not actually */ 1374a5ff213dSBarry Smith /* inserted in yy until the next line */ 1375ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1376a5ff213dSBarry Smith } else { 1377a5ff213dSBarry Smith /* do local part */ 1378a5ff213dSBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 1379a5ff213dSBarry Smith /* send it on its way */ 1380ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1381a5ff213dSBarry Smith /* values actually were received in the Begin() but we need to call this nop */ 1382ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1383a5ff213dSBarry Smith } 13843a40ed3dSBarry Smith PetscFunctionReturn(0); 1385cee3aa6bSSatish Balay } 1386cee3aa6bSSatish Balay 13874a2ae208SSatish Balay #undef __FUNCT__ 13884a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIBAIJ" 1389dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1390cee3aa6bSSatish Balay { 1391cee3aa6bSSatish Balay Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data; 1392dfbe8321SBarry Smith PetscErrorCode ierr; 1393cee3aa6bSSatish Balay 1394d64ed03dSBarry Smith PetscFunctionBegin; 1395cee3aa6bSSatish Balay /* do nondiagonal part */ 13967c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 1397cee3aa6bSSatish Balay /* send it on its way */ 1398ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1399cee3aa6bSSatish Balay /* do local part */ 14007c922b88SBarry Smith ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 1401cee3aa6bSSatish Balay /* receive remote parts: note this assumes the values are not actually */ 1402cee3aa6bSSatish Balay /* inserted in yy until the next line, which is true for my implementation*/ 1403cee3aa6bSSatish Balay /* but is not perhaps always true. */ 1404ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 14053a40ed3dSBarry Smith PetscFunctionReturn(0); 1406cee3aa6bSSatish Balay } 1407cee3aa6bSSatish Balay 1408cee3aa6bSSatish Balay /* 1409cee3aa6bSSatish Balay This only works correctly for square matrices where the subblock A->A is the 1410cee3aa6bSSatish Balay diagonal block 1411cee3aa6bSSatish Balay */ 14124a2ae208SSatish Balay #undef __FUNCT__ 14134a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIBAIJ" 1414dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIBAIJ(Mat A,Vec v) 1415cee3aa6bSSatish Balay { 1416cee3aa6bSSatish Balay Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data; 1417dfbe8321SBarry Smith PetscErrorCode ierr; 1418d64ed03dSBarry Smith 1419d64ed03dSBarry Smith PetscFunctionBegin; 1420e32f2f54SBarry 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"); 14213a40ed3dSBarry Smith ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr); 14223a40ed3dSBarry Smith PetscFunctionReturn(0); 1423cee3aa6bSSatish Balay } 1424cee3aa6bSSatish Balay 14254a2ae208SSatish Balay #undef __FUNCT__ 14264a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIBAIJ" 1427f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIBAIJ(Mat A,PetscScalar aa) 1428cee3aa6bSSatish Balay { 1429cee3aa6bSSatish Balay Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data; 1430dfbe8321SBarry Smith PetscErrorCode ierr; 1431d64ed03dSBarry Smith 1432d64ed03dSBarry Smith PetscFunctionBegin; 1433f4df32b1SMatthew Knepley ierr = MatScale(a->A,aa);CHKERRQ(ierr); 1434f4df32b1SMatthew Knepley ierr = MatScale(a->B,aa);CHKERRQ(ierr); 14353a40ed3dSBarry Smith PetscFunctionReturn(0); 1436cee3aa6bSSatish Balay } 1437026e39d0SSatish Balay 14384a2ae208SSatish Balay #undef __FUNCT__ 14394a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIBAIJ" 1440b24ad042SBarry Smith PetscErrorCode MatGetRow_MPIBAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 1441acdf5bf4SSatish Balay { 1442acdf5bf4SSatish Balay Mat_MPIBAIJ *mat = (Mat_MPIBAIJ*)matin->data; 144387828ca2SBarry Smith PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p; 14446849ba73SBarry Smith PetscErrorCode ierr; 1445d0f46423SBarry Smith PetscInt bs = matin->rmap->bs,bs2 = mat->bs2,i,*cworkA,*cworkB,**pcA,**pcB; 1446d0f46423SBarry Smith PetscInt nztot,nzA,nzB,lrow,brstart = matin->rmap->rstart,brend = matin->rmap->rend; 1447899cda47SBarry Smith PetscInt *cmap,*idx_p,cstart = mat->cstartbs; 1448acdf5bf4SSatish Balay 1449d64ed03dSBarry Smith PetscFunctionBegin; 1450e7e72b3dSBarry Smith if (row < brstart || row >= brend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local rows"); 1451e32f2f54SBarry Smith if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active"); 1452acdf5bf4SSatish Balay mat->getrowactive = PETSC_TRUE; 1453acdf5bf4SSatish Balay 1454acdf5bf4SSatish Balay if (!mat->rowvalues && (idx || v)) { 1455acdf5bf4SSatish Balay /* 1456acdf5bf4SSatish Balay allocate enough space to hold information from the longest row. 1457acdf5bf4SSatish Balay */ 1458acdf5bf4SSatish Balay Mat_SeqBAIJ *Aa = (Mat_SeqBAIJ*)mat->A->data,*Ba = (Mat_SeqBAIJ*)mat->B->data; 1459b24ad042SBarry Smith PetscInt max = 1,mbs = mat->mbs,tmp; 1460bd16c2feSSatish Balay for (i=0; i<mbs; i++) { 1461acdf5bf4SSatish Balay tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; 1462acdf5bf4SSatish Balay if (max < tmp) { max = tmp; } 1463acdf5bf4SSatish Balay } 1464fca92195SBarry Smith ierr = PetscMalloc2(max*bs2,PetscScalar,&mat->rowvalues,max*bs2,PetscInt,&mat->rowindices);CHKERRQ(ierr); 1465acdf5bf4SSatish Balay } 1466d9d09a02SSatish Balay lrow = row - brstart; 1467acdf5bf4SSatish Balay 1468acdf5bf4SSatish Balay pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB; 1469acdf5bf4SSatish Balay if (!v) {pvA = 0; pvB = 0;} 1470acdf5bf4SSatish Balay if (!idx) {pcA = 0; if (!v) pcB = 0;} 1471f830108cSBarry Smith ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1472f830108cSBarry Smith ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 1473acdf5bf4SSatish Balay nztot = nzA + nzB; 1474acdf5bf4SSatish Balay 1475acdf5bf4SSatish Balay cmap = mat->garray; 1476acdf5bf4SSatish Balay if (v || idx) { 1477acdf5bf4SSatish Balay if (nztot) { 1478acdf5bf4SSatish Balay /* Sort by increasing column numbers, assuming A and B already sorted */ 1479b24ad042SBarry Smith PetscInt imark = -1; 1480acdf5bf4SSatish Balay if (v) { 1481acdf5bf4SSatish Balay *v = v_p = mat->rowvalues; 1482acdf5bf4SSatish Balay for (i=0; i<nzB; i++) { 1483d9d09a02SSatish Balay if (cmap[cworkB[i]/bs] < cstart) v_p[i] = vworkB[i]; 1484acdf5bf4SSatish Balay else break; 1485acdf5bf4SSatish Balay } 1486acdf5bf4SSatish Balay imark = i; 1487acdf5bf4SSatish Balay for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i]; 1488acdf5bf4SSatish Balay for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i]; 1489acdf5bf4SSatish Balay } 1490acdf5bf4SSatish Balay if (idx) { 1491acdf5bf4SSatish Balay *idx = idx_p = mat->rowindices; 1492acdf5bf4SSatish Balay if (imark > -1) { 1493acdf5bf4SSatish Balay for (i=0; i<imark; i++) { 1494bd16c2feSSatish Balay idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs; 1495acdf5bf4SSatish Balay } 1496acdf5bf4SSatish Balay } else { 1497acdf5bf4SSatish Balay for (i=0; i<nzB; i++) { 1498d9d09a02SSatish Balay if (cmap[cworkB[i]/bs] < cstart) 1499d9d09a02SSatish Balay idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ; 1500acdf5bf4SSatish Balay else break; 1501acdf5bf4SSatish Balay } 1502acdf5bf4SSatish Balay imark = i; 1503acdf5bf4SSatish Balay } 1504d9d09a02SSatish Balay for (i=0; i<nzA; i++) idx_p[imark+i] = cstart*bs + cworkA[i]; 1505d9d09a02SSatish Balay for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ; 1506acdf5bf4SSatish Balay } 1507d64ed03dSBarry Smith } else { 1508d212a18eSSatish Balay if (idx) *idx = 0; 1509d212a18eSSatish Balay if (v) *v = 0; 1510d212a18eSSatish Balay } 1511acdf5bf4SSatish Balay } 1512acdf5bf4SSatish Balay *nz = nztot; 1513f830108cSBarry Smith ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1514f830108cSBarry Smith ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 15153a40ed3dSBarry Smith PetscFunctionReturn(0); 1516acdf5bf4SSatish Balay } 1517acdf5bf4SSatish Balay 15184a2ae208SSatish Balay #undef __FUNCT__ 15194a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIBAIJ" 1520b24ad042SBarry Smith PetscErrorCode MatRestoreRow_MPIBAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 1521acdf5bf4SSatish Balay { 1522acdf5bf4SSatish Balay Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 1523d64ed03dSBarry Smith 1524d64ed03dSBarry Smith PetscFunctionBegin; 1525e7e72b3dSBarry Smith if (!baij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called"); 1526acdf5bf4SSatish Balay baij->getrowactive = PETSC_FALSE; 15273a40ed3dSBarry Smith PetscFunctionReturn(0); 1528acdf5bf4SSatish Balay } 1529acdf5bf4SSatish Balay 15304a2ae208SSatish Balay #undef __FUNCT__ 15314a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIBAIJ" 1532dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIBAIJ(Mat A) 153358667388SSatish Balay { 153458667388SSatish Balay Mat_MPIBAIJ *l = (Mat_MPIBAIJ*)A->data; 1535dfbe8321SBarry Smith PetscErrorCode ierr; 1536d64ed03dSBarry Smith 1537d64ed03dSBarry Smith PetscFunctionBegin; 153858667388SSatish Balay ierr = MatZeroEntries(l->A);CHKERRQ(ierr); 153958667388SSatish Balay ierr = MatZeroEntries(l->B);CHKERRQ(ierr); 15403a40ed3dSBarry Smith PetscFunctionReturn(0); 154158667388SSatish Balay } 15420ac07820SSatish Balay 15434a2ae208SSatish Balay #undef __FUNCT__ 15444a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIBAIJ" 1545dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIBAIJ(Mat matin,MatInfoType flag,MatInfo *info) 15460ac07820SSatish Balay { 15474e220ebcSLois Curfman McInnes Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)matin->data; 15484e220ebcSLois Curfman McInnes Mat A = a->A,B = a->B; 1549dfbe8321SBarry Smith PetscErrorCode ierr; 1550329f5518SBarry Smith PetscReal isend[5],irecv[5]; 15510ac07820SSatish Balay 1552d64ed03dSBarry Smith PetscFunctionBegin; 1553d0f46423SBarry Smith info->block_size = (PetscReal)matin->rmap->bs; 15544e220ebcSLois Curfman McInnes ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr); 15550e4b21beSBarry Smith isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded; 1556de87f314SBarry Smith isend[3] = info->memory; isend[4] = info->mallocs; 15574e220ebcSLois Curfman McInnes ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr); 15580e4b21beSBarry Smith isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded; 1559de87f314SBarry Smith isend[3] += info->memory; isend[4] += info->mallocs; 15600ac07820SSatish Balay if (flag == MAT_LOCAL) { 15614e220ebcSLois Curfman McInnes info->nz_used = isend[0]; 15624e220ebcSLois Curfman McInnes info->nz_allocated = isend[1]; 15634e220ebcSLois Curfman McInnes info->nz_unneeded = isend[2]; 15644e220ebcSLois Curfman McInnes info->memory = isend[3]; 15654e220ebcSLois Curfman McInnes info->mallocs = isend[4]; 15660ac07820SSatish Balay } else if (flag == MAT_GLOBAL_MAX) { 15677adad957SLisandro Dalcin ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,((PetscObject)matin)->comm);CHKERRQ(ierr); 15684e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 15694e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 15704e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 15714e220ebcSLois Curfman McInnes info->memory = irecv[3]; 15724e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 15730ac07820SSatish Balay } else if (flag == MAT_GLOBAL_SUM) { 15747adad957SLisandro Dalcin ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,((PetscObject)matin)->comm);CHKERRQ(ierr); 15754e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 15764e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 15774e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 15784e220ebcSLois Curfman McInnes info->memory = irecv[3]; 15794e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1580d41123aaSBarry Smith } else { 158165e19b50SBarry Smith SETERRQ1(((PetscObject)matin)->comm,PETSC_ERR_ARG_WRONG,"Unknown MatInfoType argument %d",(int)flag); 15820ac07820SSatish Balay } 15834e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */ 15844e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 15854e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 15863a40ed3dSBarry Smith PetscFunctionReturn(0); 15870ac07820SSatish Balay } 15880ac07820SSatish Balay 15894a2ae208SSatish Balay #undef __FUNCT__ 15904a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIBAIJ" 1591ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIBAIJ(Mat A,MatOption op,PetscBool flg) 159258667388SSatish Balay { 159358667388SSatish Balay Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data; 1594dfbe8321SBarry Smith PetscErrorCode ierr; 159558667388SSatish Balay 1596d64ed03dSBarry Smith PetscFunctionBegin; 159712c028f9SKris Buschelman switch (op) { 1598512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 159912c028f9SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 160028b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 1601a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 160212c028f9SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 16034e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 16044e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 160512c028f9SKris Buschelman break; 160612c028f9SKris Buschelman case MAT_ROW_ORIENTED: 16074e0d8c25SBarry Smith a->roworiented = flg; 16084e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 16094e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 161012c028f9SKris Buschelman break; 16114e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 1612290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 161312c028f9SKris Buschelman break; 161412c028f9SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 16154e0d8c25SBarry Smith a->donotstash = flg; 161612c028f9SKris Buschelman break; 161712c028f9SKris Buschelman case MAT_USE_HASH_TABLE: 16184e0d8c25SBarry Smith a->ht_flag = flg; 161912c028f9SKris Buschelman break; 162077e54ba9SKris Buschelman case MAT_SYMMETRIC: 162177e54ba9SKris Buschelman case MAT_STRUCTURALLY_SYMMETRIC: 16222188ac68SBarry Smith case MAT_HERMITIAN: 16232188ac68SBarry Smith case MAT_SYMMETRY_ETERNAL: 16244e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 162577e54ba9SKris Buschelman break; 162612c028f9SKris Buschelman default: 162765e19b50SBarry Smith SETERRQ1(((PetscObject)A)->comm,PETSC_ERR_SUP,"unknown option %d",op); 1628d64ed03dSBarry Smith } 16293a40ed3dSBarry Smith PetscFunctionReturn(0); 163058667388SSatish Balay } 163158667388SSatish Balay 16324a2ae208SSatish Balay #undef __FUNCT__ 16334a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIBAIJ(" 1634fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIBAIJ(Mat A,MatReuse reuse,Mat *matout) 16350ac07820SSatish Balay { 16360ac07820SSatish Balay Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)A->data; 16370ac07820SSatish Balay Mat_SeqBAIJ *Aloc; 16380ac07820SSatish Balay Mat B; 1639dfbe8321SBarry Smith PetscErrorCode ierr; 1640d0f46423SBarry Smith PetscInt M=A->rmap->N,N=A->cmap->N,*ai,*aj,i,*rvals,j,k,col; 1641d0f46423SBarry Smith PetscInt bs=A->rmap->bs,mbs=baij->mbs; 16423eda8832SBarry Smith MatScalar *a; 16430ac07820SSatish Balay 1644d64ed03dSBarry Smith PetscFunctionBegin; 1645e7e72b3dSBarry Smith if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1646fc4dec0aSBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout == A) { 16477adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr); 1648d0f46423SBarry Smith ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr); 16497adad957SLisandro Dalcin ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr); 1650d0f46423SBarry Smith ierr = MatMPIBAIJSetPreallocation(B,A->rmap->bs,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr); 1651fc4dec0aSBarry Smith } else { 1652fc4dec0aSBarry Smith B = *matout; 1653fc4dec0aSBarry Smith } 16540ac07820SSatish Balay 16550ac07820SSatish Balay /* copy over the A part */ 16560ac07820SSatish Balay Aloc = (Mat_SeqBAIJ*)baij->A->data; 16570ac07820SSatish Balay ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1658b24ad042SBarry Smith ierr = PetscMalloc(bs*sizeof(PetscInt),&rvals);CHKERRQ(ierr); 16590ac07820SSatish Balay 16600ac07820SSatish Balay for (i=0; i<mbs; i++) { 1661899cda47SBarry Smith rvals[0] = bs*(baij->rstartbs + i); 16620ac07820SSatish Balay for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; } 16630ac07820SSatish Balay for (j=ai[i]; j<ai[i+1]; j++) { 1664899cda47SBarry Smith col = (baij->cstartbs+aj[j])*bs; 16650ac07820SSatish Balay for (k=0; k<bs; k++) { 166697e5c40aSBarry Smith ierr = MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);CHKERRQ(ierr); 16670ac07820SSatish Balay col++; a += bs; 16680ac07820SSatish Balay } 16690ac07820SSatish Balay } 16700ac07820SSatish Balay } 16710ac07820SSatish Balay /* copy over the B part */ 16720ac07820SSatish Balay Aloc = (Mat_SeqBAIJ*)baij->B->data; 16730ac07820SSatish Balay ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 16740ac07820SSatish Balay for (i=0; i<mbs; i++) { 1675899cda47SBarry Smith rvals[0] = bs*(baij->rstartbs + i); 16760ac07820SSatish Balay for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; } 16770ac07820SSatish Balay for (j=ai[i]; j<ai[i+1]; j++) { 16780ac07820SSatish Balay col = baij->garray[aj[j]]*bs; 16790ac07820SSatish Balay for (k=0; k<bs; k++) { 168097e5c40aSBarry Smith ierr = MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);CHKERRQ(ierr); 16810ac07820SSatish Balay col++; a += bs; 16820ac07820SSatish Balay } 16830ac07820SSatish Balay } 16840ac07820SSatish Balay } 1685606d414cSSatish Balay ierr = PetscFree(rvals);CHKERRQ(ierr); 16860ac07820SSatish Balay ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 16870ac07820SSatish Balay ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 16880ac07820SSatish Balay 1689815cbec1SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout != A) { 16900ac07820SSatish Balay *matout = B; 16910ac07820SSatish Balay } else { 1692eb6b5d47SBarry Smith ierr = MatHeaderMerge(A,B);CHKERRQ(ierr); 16930ac07820SSatish Balay } 16943a40ed3dSBarry Smith PetscFunctionReturn(0); 16950ac07820SSatish Balay } 16960e95ebc0SSatish Balay 16974a2ae208SSatish Balay #undef __FUNCT__ 16984a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIBAIJ" 1699dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIBAIJ(Mat mat,Vec ll,Vec rr) 17000e95ebc0SSatish Balay { 170136c4a09eSSatish Balay Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 170236c4a09eSSatish Balay Mat a = baij->A,b = baij->B; 1703dfbe8321SBarry Smith PetscErrorCode ierr; 1704b24ad042SBarry Smith PetscInt s1,s2,s3; 17050e95ebc0SSatish Balay 1706d64ed03dSBarry Smith PetscFunctionBegin; 170736c4a09eSSatish Balay ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr); 170836c4a09eSSatish Balay if (rr) { 170936c4a09eSSatish Balay ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr); 1710e32f2f54SBarry Smith if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size"); 171136c4a09eSSatish Balay /* Overlap communication with computation. */ 1712ca9f406cSSatish Balay ierr = VecScatterBegin(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 171336c4a09eSSatish Balay } 17140e95ebc0SSatish Balay if (ll) { 17150e95ebc0SSatish Balay ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr); 1716e32f2f54SBarry Smith if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size"); 1717a21fb8cbSBarry Smith ierr = (*b->ops->diagonalscale)(b,ll,PETSC_NULL);CHKERRQ(ierr); 17180e95ebc0SSatish Balay } 171936c4a09eSSatish Balay /* scale the diagonal block */ 172036c4a09eSSatish Balay ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr); 172136c4a09eSSatish Balay 172236c4a09eSSatish Balay if (rr) { 172336c4a09eSSatish Balay /* Do a scatter end and then right scale the off-diagonal block */ 1724ca9f406cSSatish Balay ierr = VecScatterEnd(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1725a21fb8cbSBarry Smith ierr = (*b->ops->diagonalscale)(b,PETSC_NULL,baij->lvec);CHKERRQ(ierr); 172636c4a09eSSatish Balay } 172736c4a09eSSatish Balay 17283a40ed3dSBarry Smith PetscFunctionReturn(0); 17290e95ebc0SSatish Balay } 17300e95ebc0SSatish Balay 17314a2ae208SSatish Balay #undef __FUNCT__ 17324a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIBAIJ" 1733f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_MPIBAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag) 17340ac07820SSatish Balay { 17350ac07820SSatish Balay Mat_MPIBAIJ *l = (Mat_MPIBAIJ*)A->data; 17366849ba73SBarry Smith PetscErrorCode ierr; 1737b24ad042SBarry Smith PetscMPIInt imdex,size = l->size,n,rank = l->rank; 1738d0f46423SBarry Smith PetscInt i,*owners = A->rmap->range; 1739b24ad042SBarry Smith PetscInt *nprocs,j,idx,nsends,row; 1740b24ad042SBarry Smith PetscInt nmax,*svalues,*starts,*owner,nrecvs; 17417adad957SLisandro Dalcin PetscInt *rvalues,tag = ((PetscObject)A)->tag,count,base,slen,*source,lastidx = -1; 1742d0f46423SBarry Smith PetscInt *lens,*lrows,*values,rstart_bs=A->rmap->rstart; 17437adad957SLisandro Dalcin MPI_Comm comm = ((PetscObject)A)->comm; 17440ac07820SSatish Balay MPI_Request *send_waits,*recv_waits; 17450ac07820SSatish Balay MPI_Status recv_status,*send_status; 17466543fbbaSBarry Smith #if defined(PETSC_DEBUG) 1747ace3abfcSBarry Smith PetscBool found = PETSC_FALSE; 17486543fbbaSBarry Smith #endif 17490ac07820SSatish Balay 1750d64ed03dSBarry Smith PetscFunctionBegin; 17510ac07820SSatish Balay /* first count number of contributors to each processor */ 1752b24ad042SBarry Smith ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); 1753b24ad042SBarry Smith ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr); 1754b24ad042SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/ 17556543fbbaSBarry Smith j = 0; 17560ac07820SSatish Balay for (i=0; i<N; i++) { 17576543fbbaSBarry Smith if (lastidx > (idx = rows[i])) j = 0; 17586543fbbaSBarry Smith lastidx = idx; 17596543fbbaSBarry Smith for (; j<size; j++) { 1760357c27ecSBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 17616543fbbaSBarry Smith nprocs[2*j]++; 17626543fbbaSBarry Smith nprocs[2*j+1] = 1; 17636543fbbaSBarry Smith owner[i] = j; 17646543fbbaSBarry Smith #if defined(PETSC_DEBUG) 17656543fbbaSBarry Smith found = PETSC_TRUE; 17666543fbbaSBarry Smith #endif 17676543fbbaSBarry Smith break; 17680ac07820SSatish Balay } 17690ac07820SSatish Balay } 17706543fbbaSBarry Smith #if defined(PETSC_DEBUG) 1771e32f2f54SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range"); 17726543fbbaSBarry Smith found = PETSC_FALSE; 17736543fbbaSBarry Smith #endif 17740ac07820SSatish Balay } 1775c1dc657dSBarry Smith nsends = 0; for (i=0; i<size; i++) { nsends += nprocs[2*i+1];} 17760ac07820SSatish Balay 17777367270fSBarry Smith if (A->nooffproczerorows) { 17787367270fSBarry 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"); 17797367270fSBarry Smith nrecvs = nsends; 17807367270fSBarry Smith nmax = N; 17817367270fSBarry Smith } else { 17820ac07820SSatish Balay /* inform other processors of number of messages and max length*/ 1783c1dc657dSBarry Smith ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr); 17847367270fSBarry Smith } 17850ac07820SSatish Balay 17860ac07820SSatish Balay /* post receives: */ 1787b24ad042SBarry Smith ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr); 1788b0a32e0cSBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 17890ac07820SSatish Balay for (i=0; i<nrecvs; i++) { 1790b24ad042SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 17910ac07820SSatish Balay } 17920ac07820SSatish Balay 17930ac07820SSatish Balay /* do sends: 17940ac07820SSatish Balay 1) starts[i] gives the starting index in svalues for stuff going to 17950ac07820SSatish Balay the ith processor 17960ac07820SSatish Balay */ 1797b24ad042SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr); 1798b0a32e0cSBarry Smith ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr); 1799b24ad042SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr); 18000ac07820SSatish Balay starts[0] = 0; 1801c1dc657dSBarry Smith for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 18020ac07820SSatish Balay for (i=0; i<N; i++) { 18030ac07820SSatish Balay svalues[starts[owner[i]]++] = rows[i]; 18040ac07820SSatish Balay } 18050ac07820SSatish Balay 18060ac07820SSatish Balay starts[0] = 0; 1807c1dc657dSBarry Smith for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 18080ac07820SSatish Balay count = 0; 18090ac07820SSatish Balay for (i=0; i<size; i++) { 1810c1dc657dSBarry Smith if (nprocs[2*i+1]) { 1811b24ad042SBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 18120ac07820SSatish Balay } 18130ac07820SSatish Balay } 1814606d414cSSatish Balay ierr = PetscFree(starts);CHKERRQ(ierr); 18150ac07820SSatish Balay 1816357c27ecSBarry Smith base = owners[rank]; 18170ac07820SSatish Balay 18180ac07820SSatish Balay /* wait on receives */ 1819fca92195SBarry Smith ierr = PetscMalloc2(nrecvs+1,PetscInt,&lens,nrecvs+1,PetscInt,&source);CHKERRQ(ierr); 1820fca92195SBarry Smith count = nrecvs; 1821fca92195SBarry Smith slen = 0; 18220ac07820SSatish Balay while (count) { 1823ca161407SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 18240ac07820SSatish Balay /* unpack receives into our local space */ 1825b24ad042SBarry Smith ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr); 18260ac07820SSatish Balay source[imdex] = recv_status.MPI_SOURCE; 18270ac07820SSatish Balay lens[imdex] = n; 18280ac07820SSatish Balay slen += n; 18290ac07820SSatish Balay count--; 18300ac07820SSatish Balay } 1831606d414cSSatish Balay ierr = PetscFree(recv_waits);CHKERRQ(ierr); 18320ac07820SSatish Balay 18330ac07820SSatish Balay /* move the data into the send scatter */ 1834b24ad042SBarry Smith ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr); 18350ac07820SSatish Balay count = 0; 18360ac07820SSatish Balay for (i=0; i<nrecvs; i++) { 18370ac07820SSatish Balay values = rvalues + i*nmax; 18380ac07820SSatish Balay for (j=0; j<lens[i]; j++) { 18390ac07820SSatish Balay lrows[count++] = values[j] - base; 18400ac07820SSatish Balay } 18410ac07820SSatish Balay } 1842606d414cSSatish Balay ierr = PetscFree(rvalues);CHKERRQ(ierr); 1843fca92195SBarry Smith ierr = PetscFree2(lens,source);CHKERRQ(ierr); 1844606d414cSSatish Balay ierr = PetscFree(owner);CHKERRQ(ierr); 1845606d414cSSatish Balay ierr = PetscFree(nprocs);CHKERRQ(ierr); 18460ac07820SSatish Balay 18470ac07820SSatish Balay /* actually zap the local rows */ 184872dacd9aSBarry Smith /* 184972dacd9aSBarry Smith Zero the required rows. If the "diagonal block" of the matrix 1850a8c7a070SBarry Smith is square and the user wishes to set the diagonal we use separate 185172dacd9aSBarry Smith code so that MatSetValues() is not called for each diagonal allocating 185272dacd9aSBarry Smith new memory, thus calling lots of mallocs and slowing things down. 185372dacd9aSBarry Smith 185472dacd9aSBarry Smith */ 18559c957beeSSatish Balay /* must zero l->B before l->A because the (diag) case below may put values into l->B*/ 1856f4df32b1SMatthew Knepley ierr = MatZeroRows_SeqBAIJ(l->B,slen,lrows,0.0);CHKERRQ(ierr); 1857d0f46423SBarry Smith if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) { 1858f4df32b1SMatthew Knepley ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,diag);CHKERRQ(ierr); 1859f4df32b1SMatthew Knepley } else if (diag != 0.0) { 1860f4df32b1SMatthew Knepley ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,0.0);CHKERRQ(ierr); 1861e7e72b3dSBarry 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\ 1862512a5fc5SBarry Smith MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR"); 1863a07cd24cSSatish Balay for (i=0; i<slen; i++) { 1864a07cd24cSSatish Balay row = lrows[i] + rstart_bs; 1865f4df32b1SMatthew Knepley ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr); 1866a07cd24cSSatish Balay } 1867a07cd24cSSatish Balay ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1868a07cd24cSSatish Balay ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 18699c957beeSSatish Balay } else { 1870f4df32b1SMatthew Knepley ierr = MatZeroRows_SeqBAIJ(l->A,slen,lrows,0.0);CHKERRQ(ierr); 1871a07cd24cSSatish Balay } 18729c957beeSSatish Balay 1873606d414cSSatish Balay ierr = PetscFree(lrows);CHKERRQ(ierr); 1874a07cd24cSSatish Balay 18750ac07820SSatish Balay /* wait on sends */ 18760ac07820SSatish Balay if (nsends) { 187782502324SSatish Balay ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 1878ca161407SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 1879606d414cSSatish Balay ierr = PetscFree(send_status);CHKERRQ(ierr); 18800ac07820SSatish Balay } 1881606d414cSSatish Balay ierr = PetscFree(send_waits);CHKERRQ(ierr); 1882606d414cSSatish Balay ierr = PetscFree(svalues);CHKERRQ(ierr); 18830ac07820SSatish Balay 18843a40ed3dSBarry Smith PetscFunctionReturn(0); 18850ac07820SSatish Balay } 188672dacd9aSBarry Smith 18874a2ae208SSatish Balay #undef __FUNCT__ 18884a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIBAIJ" 1889dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIBAIJ(Mat A) 1890bb5a7306SBarry Smith { 1891bb5a7306SBarry Smith Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data; 1892dfbe8321SBarry Smith PetscErrorCode ierr; 1893d64ed03dSBarry Smith 1894d64ed03dSBarry Smith PetscFunctionBegin; 1895bb5a7306SBarry Smith ierr = MatSetUnfactored(a->A);CHKERRQ(ierr); 18963a40ed3dSBarry Smith PetscFunctionReturn(0); 1897bb5a7306SBarry Smith } 1898bb5a7306SBarry Smith 18996849ba73SBarry Smith static PetscErrorCode MatDuplicate_MPIBAIJ(Mat,MatDuplicateOption,Mat *); 19000ac07820SSatish Balay 19014a2ae208SSatish Balay #undef __FUNCT__ 19024a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIBAIJ" 1903ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIBAIJ(Mat A,Mat B,PetscBool *flag) 19047fc3c18eSBarry Smith { 19057fc3c18eSBarry Smith Mat_MPIBAIJ *matB = (Mat_MPIBAIJ*)B->data,*matA = (Mat_MPIBAIJ*)A->data; 19067fc3c18eSBarry Smith Mat a,b,c,d; 1907ace3abfcSBarry Smith PetscBool flg; 1908dfbe8321SBarry Smith PetscErrorCode ierr; 19097fc3c18eSBarry Smith 19107fc3c18eSBarry Smith PetscFunctionBegin; 19117fc3c18eSBarry Smith a = matA->A; b = matA->B; 19127fc3c18eSBarry Smith c = matB->A; d = matB->B; 19137fc3c18eSBarry Smith 19147fc3c18eSBarry Smith ierr = MatEqual(a,c,&flg);CHKERRQ(ierr); 1915abc0a331SBarry Smith if (flg) { 19167fc3c18eSBarry Smith ierr = MatEqual(b,d,&flg);CHKERRQ(ierr); 19177fc3c18eSBarry Smith } 19187adad957SLisandro Dalcin ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,((PetscObject)A)->comm);CHKERRQ(ierr); 19197fc3c18eSBarry Smith PetscFunctionReturn(0); 19207fc3c18eSBarry Smith } 19217fc3c18eSBarry Smith 19223c896bc6SHong Zhang #undef __FUNCT__ 19233c896bc6SHong Zhang #define __FUNCT__ "MatCopy_MPIBAIJ" 19243c896bc6SHong Zhang PetscErrorCode MatCopy_MPIBAIJ(Mat A,Mat B,MatStructure str) 19253c896bc6SHong Zhang { 19263c896bc6SHong Zhang PetscErrorCode ierr; 19273c896bc6SHong Zhang Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data; 19283c896bc6SHong Zhang Mat_MPIBAIJ *b = (Mat_MPIBAIJ *)B->data; 19293c896bc6SHong Zhang 19303c896bc6SHong Zhang PetscFunctionBegin; 19313c896bc6SHong Zhang /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */ 19323c896bc6SHong Zhang if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) { 19333c896bc6SHong Zhang ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 19343c896bc6SHong Zhang } else { 19353c896bc6SHong Zhang ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr); 19363c896bc6SHong Zhang ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr); 19373c896bc6SHong Zhang } 19383c896bc6SHong Zhang PetscFunctionReturn(0); 19393c896bc6SHong Zhang } 1940273d9f13SBarry Smith 19414a2ae208SSatish Balay #undef __FUNCT__ 19424a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIBAIJ" 1943dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_MPIBAIJ(Mat A) 1944273d9f13SBarry Smith { 1945dfbe8321SBarry Smith PetscErrorCode ierr; 1946273d9f13SBarry Smith 1947273d9f13SBarry Smith PetscFunctionBegin; 1948db4efbfdSBarry Smith ierr = MatMPIBAIJSetPreallocation(A,-PetscMax(A->rmap->bs,1),PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr); 1949273d9f13SBarry Smith PetscFunctionReturn(0); 1950273d9f13SBarry Smith } 1951273d9f13SBarry Smith 19524fe895cdSHong Zhang #undef __FUNCT__ 19534fe895cdSHong Zhang #define __FUNCT__ "MatAXPY_MPIBAIJ" 19544fe895cdSHong Zhang PetscErrorCode MatAXPY_MPIBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 19554fe895cdSHong Zhang { 19564fe895cdSHong Zhang PetscErrorCode ierr; 19574fe895cdSHong Zhang Mat_MPIBAIJ *xx=(Mat_MPIBAIJ *)X->data,*yy=(Mat_MPIBAIJ *)Y->data; 19584fe895cdSHong Zhang PetscBLASInt bnz,one=1; 19594fe895cdSHong Zhang Mat_SeqBAIJ *x,*y; 19604fe895cdSHong Zhang 19614fe895cdSHong Zhang PetscFunctionBegin; 19624fe895cdSHong Zhang if (str == SAME_NONZERO_PATTERN) { 19634fe895cdSHong Zhang PetscScalar alpha = a; 19644fe895cdSHong Zhang x = (Mat_SeqBAIJ *)xx->A->data; 19654fe895cdSHong Zhang y = (Mat_SeqBAIJ *)yy->A->data; 19660805154bSBarry Smith bnz = PetscBLASIntCast(x->nz); 19674fe895cdSHong Zhang BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 19684fe895cdSHong Zhang x = (Mat_SeqBAIJ *)xx->B->data; 19694fe895cdSHong Zhang y = (Mat_SeqBAIJ *)yy->B->data; 19700805154bSBarry Smith bnz = PetscBLASIntCast(x->nz); 19714fe895cdSHong Zhang BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 19724fe895cdSHong Zhang } else { 19734fe895cdSHong Zhang ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr); 19744fe895cdSHong Zhang } 19754fe895cdSHong Zhang PetscFunctionReturn(0); 19764fe895cdSHong Zhang } 19774fe895cdSHong Zhang 197899cafbc1SBarry Smith #undef __FUNCT__ 197941c166b1SJed Brown #define __FUNCT__ "MatSetBlockSize_MPIBAIJ" 198041c166b1SJed Brown PetscErrorCode MatSetBlockSize_MPIBAIJ(Mat A,PetscInt bs) 198141c166b1SJed Brown { 198241c166b1SJed Brown Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data; 1983829b6ff0SJed Brown PetscInt rbs,cbs; 198441c166b1SJed Brown PetscErrorCode ierr; 198541c166b1SJed Brown 198641c166b1SJed Brown PetscFunctionBegin; 198741c166b1SJed Brown ierr = MatSetBlockSize(a->A,bs);CHKERRQ(ierr); 198841c166b1SJed Brown ierr = MatSetBlockSize(a->B,bs);CHKERRQ(ierr); 1989829b6ff0SJed Brown ierr = PetscLayoutGetBlockSize(A->rmap,&rbs);CHKERRQ(ierr); 1990829b6ff0SJed Brown ierr = PetscLayoutGetBlockSize(A->cmap,&cbs);CHKERRQ(ierr); 1991e32f2f54SBarry Smith if (rbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with BAIJ %d",bs,rbs); 1992e32f2f54SBarry Smith if (cbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with BAIJ %d",bs,cbs); 199341c166b1SJed Brown PetscFunctionReturn(0); 199441c166b1SJed Brown } 199541c166b1SJed Brown 199641c166b1SJed Brown #undef __FUNCT__ 199799cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIBAIJ" 199899cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIBAIJ(Mat A) 199999cafbc1SBarry Smith { 200099cafbc1SBarry Smith Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data; 200199cafbc1SBarry Smith PetscErrorCode ierr; 200299cafbc1SBarry Smith 200399cafbc1SBarry Smith PetscFunctionBegin; 200499cafbc1SBarry Smith ierr = MatRealPart(a->A);CHKERRQ(ierr); 200599cafbc1SBarry Smith ierr = MatRealPart(a->B);CHKERRQ(ierr); 200699cafbc1SBarry Smith PetscFunctionReturn(0); 200799cafbc1SBarry Smith } 200899cafbc1SBarry Smith 200999cafbc1SBarry Smith #undef __FUNCT__ 201099cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIBAIJ" 201199cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIBAIJ(Mat A) 201299cafbc1SBarry Smith { 201399cafbc1SBarry Smith Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data; 201499cafbc1SBarry Smith PetscErrorCode ierr; 201599cafbc1SBarry Smith 201699cafbc1SBarry Smith PetscFunctionBegin; 201799cafbc1SBarry Smith ierr = MatImaginaryPart(a->A);CHKERRQ(ierr); 201899cafbc1SBarry Smith ierr = MatImaginaryPart(a->B);CHKERRQ(ierr); 201999cafbc1SBarry Smith PetscFunctionReturn(0); 202099cafbc1SBarry Smith } 202199cafbc1SBarry Smith 202282094794SBarry Smith #undef __FUNCT__ 202382094794SBarry Smith #define __FUNCT__ "MatGetSubMatrix_MPIBAIJ" 20244aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIBAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat) 20254aa3045dSJed Brown { 20264aa3045dSJed Brown PetscErrorCode ierr; 20274aa3045dSJed Brown IS iscol_local; 20284aa3045dSJed Brown PetscInt csize; 20294aa3045dSJed Brown 20304aa3045dSJed Brown PetscFunctionBegin; 20314aa3045dSJed Brown ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr); 2032b79d0421SJed Brown if (call == MAT_REUSE_MATRIX) { 2033b79d0421SJed Brown ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr); 2034e32f2f54SBarry Smith if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 2035b79d0421SJed Brown } else { 20364aa3045dSJed Brown ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr); 2037b79d0421SJed Brown } 20384aa3045dSJed Brown ierr = MatGetSubMatrix_MPIBAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr); 2039b79d0421SJed Brown if (call == MAT_INITIAL_MATRIX) { 2040b79d0421SJed Brown ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr); 20414aa3045dSJed Brown ierr = ISDestroy(iscol_local);CHKERRQ(ierr); 2042b79d0421SJed Brown } 20434aa3045dSJed Brown PetscFunctionReturn(0); 20444aa3045dSJed Brown } 20454aa3045dSJed Brown 20464aa3045dSJed Brown #undef __FUNCT__ 2047dd183c9eSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIBAIJ_Private" 204882094794SBarry Smith /* 204982094794SBarry Smith Not great since it makes two copies of the submatrix, first an SeqBAIJ 205082094794SBarry Smith in local and then by concatenating the local matrices the end result. 205182094794SBarry Smith Writing it directly would be much like MatGetSubMatrices_MPIBAIJ() 205282094794SBarry Smith */ 20534aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIBAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat) 205482094794SBarry Smith { 205582094794SBarry Smith PetscErrorCode ierr; 205682094794SBarry Smith PetscMPIInt rank,size; 205782094794SBarry Smith PetscInt i,m,n,rstart,row,rend,nz,*cwork,j,bs; 205882094794SBarry Smith PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal; 205982094794SBarry Smith Mat *local,M,Mreuse; 206082094794SBarry Smith MatScalar *vwork,*aa; 206182094794SBarry Smith MPI_Comm comm = ((PetscObject)mat)->comm; 206282094794SBarry Smith Mat_SeqBAIJ *aij; 206382094794SBarry Smith 206482094794SBarry Smith 206582094794SBarry Smith PetscFunctionBegin; 206682094794SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 206782094794SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 206882094794SBarry Smith 206982094794SBarry Smith if (call == MAT_REUSE_MATRIX) { 207082094794SBarry Smith ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr); 2071e32f2f54SBarry Smith if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 207282094794SBarry Smith local = &Mreuse; 207382094794SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr); 207482094794SBarry Smith } else { 207582094794SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 207682094794SBarry Smith Mreuse = *local; 207782094794SBarry Smith ierr = PetscFree(local);CHKERRQ(ierr); 207882094794SBarry Smith } 207982094794SBarry Smith 208082094794SBarry Smith /* 208182094794SBarry Smith m - number of local rows 208282094794SBarry Smith n - number of columns (same on all processors) 208382094794SBarry Smith rstart - first row in new global matrix generated 208482094794SBarry Smith */ 208582094794SBarry Smith ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr); 208682094794SBarry Smith ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr); 208782094794SBarry Smith m = m/bs; 208882094794SBarry Smith n = n/bs; 208982094794SBarry Smith 209082094794SBarry Smith if (call == MAT_INITIAL_MATRIX) { 209182094794SBarry Smith aij = (Mat_SeqBAIJ*)(Mreuse)->data; 209282094794SBarry Smith ii = aij->i; 209382094794SBarry Smith jj = aij->j; 209482094794SBarry Smith 209582094794SBarry Smith /* 209682094794SBarry Smith Determine the number of non-zeros in the diagonal and off-diagonal 209782094794SBarry Smith portions of the matrix in order to do correct preallocation 209882094794SBarry Smith */ 209982094794SBarry Smith 210082094794SBarry Smith /* first get start and end of "diagonal" columns */ 210182094794SBarry Smith if (csize == PETSC_DECIDE) { 210282094794SBarry Smith ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr); 210382094794SBarry Smith if (mglobal == n*bs) { /* square matrix */ 210482094794SBarry Smith nlocal = m; 210582094794SBarry Smith } else { 210682094794SBarry Smith nlocal = n/size + ((n % size) > rank); 210782094794SBarry Smith } 210882094794SBarry Smith } else { 210982094794SBarry Smith nlocal = csize/bs; 211082094794SBarry Smith } 211182094794SBarry Smith ierr = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 211282094794SBarry Smith rstart = rend - nlocal; 211365e19b50SBarry 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); 211482094794SBarry Smith 211582094794SBarry Smith /* next, compute all the lengths */ 211682094794SBarry Smith ierr = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr); 211782094794SBarry Smith olens = dlens + m; 211882094794SBarry Smith for (i=0; i<m; i++) { 211982094794SBarry Smith jend = ii[i+1] - ii[i]; 212082094794SBarry Smith olen = 0; 212182094794SBarry Smith dlen = 0; 212282094794SBarry Smith for (j=0; j<jend; j++) { 212382094794SBarry Smith if (*jj < rstart || *jj >= rend) olen++; 212482094794SBarry Smith else dlen++; 212582094794SBarry Smith jj++; 212682094794SBarry Smith } 212782094794SBarry Smith olens[i] = olen; 212882094794SBarry Smith dlens[i] = dlen; 212982094794SBarry Smith } 213082094794SBarry Smith ierr = MatCreate(comm,&M);CHKERRQ(ierr); 213182094794SBarry Smith ierr = MatSetSizes(M,bs*m,bs*nlocal,PETSC_DECIDE,bs*n);CHKERRQ(ierr); 213282094794SBarry Smith ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr); 213382094794SBarry Smith ierr = MatMPIBAIJSetPreallocation(M,bs,0,dlens,0,olens);CHKERRQ(ierr); 213482094794SBarry Smith ierr = PetscFree(dlens);CHKERRQ(ierr); 213582094794SBarry Smith } else { 213682094794SBarry Smith PetscInt ml,nl; 213782094794SBarry Smith 213882094794SBarry Smith M = *newmat; 213982094794SBarry Smith ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr); 2140e32f2f54SBarry Smith if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request"); 214182094794SBarry Smith ierr = MatZeroEntries(M);CHKERRQ(ierr); 214282094794SBarry Smith /* 214382094794SBarry Smith The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly, 214482094794SBarry Smith rather than the slower MatSetValues(). 214582094794SBarry Smith */ 214682094794SBarry Smith M->was_assembled = PETSC_TRUE; 214782094794SBarry Smith M->assembled = PETSC_FALSE; 214882094794SBarry Smith } 214982094794SBarry Smith ierr = MatSetOption(M,MAT_ROW_ORIENTED,PETSC_FALSE);CHKERRQ(ierr); 215082094794SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr); 215182094794SBarry Smith aij = (Mat_SeqBAIJ*)(Mreuse)->data; 215282094794SBarry Smith ii = aij->i; 215382094794SBarry Smith jj = aij->j; 215482094794SBarry Smith aa = aij->a; 215582094794SBarry Smith for (i=0; i<m; i++) { 215682094794SBarry Smith row = rstart/bs + i; 215782094794SBarry Smith nz = ii[i+1] - ii[i]; 215882094794SBarry Smith cwork = jj; jj += nz; 215982094794SBarry Smith vwork = aa; aa += nz; 216082094794SBarry Smith ierr = MatSetValuesBlocked_MPIBAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 216182094794SBarry Smith } 216282094794SBarry Smith 216382094794SBarry Smith ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 216482094794SBarry Smith ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 216582094794SBarry Smith *newmat = M; 216682094794SBarry Smith 216782094794SBarry Smith /* save submatrix used in processor for next request */ 216882094794SBarry Smith if (call == MAT_INITIAL_MATRIX) { 216982094794SBarry Smith ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr); 217082094794SBarry Smith ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr); 217182094794SBarry Smith } 217282094794SBarry Smith 217382094794SBarry Smith PetscFunctionReturn(0); 217482094794SBarry Smith } 217582094794SBarry Smith 217682094794SBarry Smith #undef __FUNCT__ 217782094794SBarry Smith #define __FUNCT__ "MatPermute_MPIBAIJ" 217882094794SBarry Smith PetscErrorCode MatPermute_MPIBAIJ(Mat A,IS rowp,IS colp,Mat *B) 217982094794SBarry Smith { 218082094794SBarry Smith MPI_Comm comm,pcomm; 218182094794SBarry Smith PetscInt first,local_size,nrows; 218282094794SBarry Smith const PetscInt *rows; 2183dbf0e21dSBarry Smith PetscMPIInt size; 218482094794SBarry Smith IS crowp,growp,irowp,lrowp,lcolp,icolp; 218582094794SBarry Smith PetscErrorCode ierr; 218682094794SBarry Smith 218782094794SBarry Smith PetscFunctionBegin; 218882094794SBarry Smith ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 218982094794SBarry Smith /* make a collective version of 'rowp' */ 219082094794SBarry Smith ierr = PetscObjectGetComm((PetscObject)rowp,&pcomm);CHKERRQ(ierr); 219182094794SBarry Smith if (pcomm==comm) { 219282094794SBarry Smith crowp = rowp; 219382094794SBarry Smith } else { 219482094794SBarry Smith ierr = ISGetSize(rowp,&nrows);CHKERRQ(ierr); 219582094794SBarry Smith ierr = ISGetIndices(rowp,&rows);CHKERRQ(ierr); 219670b3c8c7SBarry Smith ierr = ISCreateGeneral(comm,nrows,rows,PETSC_COPY_VALUES,&crowp);CHKERRQ(ierr); 219782094794SBarry Smith ierr = ISRestoreIndices(rowp,&rows);CHKERRQ(ierr); 219882094794SBarry Smith } 219982094794SBarry Smith /* collect the global row permutation and invert it */ 220082094794SBarry Smith ierr = ISAllGather(crowp,&growp);CHKERRQ(ierr); 220182094794SBarry Smith ierr = ISSetPermutation(growp);CHKERRQ(ierr); 220282094794SBarry Smith if (pcomm!=comm) { 220382094794SBarry Smith ierr = ISDestroy(crowp);CHKERRQ(ierr); 220482094794SBarry Smith } 220582094794SBarry Smith ierr = ISInvertPermutation(growp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 220682094794SBarry Smith /* get the local target indices */ 220782094794SBarry Smith ierr = MatGetOwnershipRange(A,&first,PETSC_NULL);CHKERRQ(ierr); 220882094794SBarry Smith ierr = MatGetLocalSize(A,&local_size,PETSC_NULL);CHKERRQ(ierr); 220982094794SBarry Smith ierr = ISGetIndices(irowp,&rows);CHKERRQ(ierr); 221070b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,local_size,rows+first,PETSC_COPY_VALUES,&lrowp);CHKERRQ(ierr); 221182094794SBarry Smith ierr = ISRestoreIndices(irowp,&rows);CHKERRQ(ierr); 221282094794SBarry Smith ierr = ISDestroy(irowp);CHKERRQ(ierr); 221382094794SBarry Smith /* the column permutation is so much easier; 221482094794SBarry Smith make a local version of 'colp' and invert it */ 221582094794SBarry Smith ierr = PetscObjectGetComm((PetscObject)colp,&pcomm);CHKERRQ(ierr); 2216dbf0e21dSBarry Smith ierr = MPI_Comm_size(pcomm,&size);CHKERRQ(ierr); 2217dbf0e21dSBarry Smith if (size==1) { 221882094794SBarry Smith lcolp = colp; 221982094794SBarry Smith } else { 222082094794SBarry Smith ierr = ISGetSize(colp,&nrows);CHKERRQ(ierr); 222182094794SBarry Smith ierr = ISGetIndices(colp,&rows);CHKERRQ(ierr); 222270b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,nrows,rows,PETSC_COPY_VALUES,&lcolp);CHKERRQ(ierr); 222382094794SBarry Smith } 2224dbf0e21dSBarry Smith ierr = ISSetPermutation(lcolp);CHKERRQ(ierr); 222582094794SBarry Smith ierr = ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 22264aa3045dSJed Brown ierr = ISSetPermutation(icolp);CHKERRQ(ierr); 2227dbf0e21dSBarry Smith if (size>1) { 222882094794SBarry Smith ierr = ISRestoreIndices(colp,&rows);CHKERRQ(ierr); 222982094794SBarry Smith ierr = ISDestroy(lcolp);CHKERRQ(ierr); 223082094794SBarry Smith } 223182094794SBarry Smith /* now we just get the submatrix */ 22324aa3045dSJed Brown ierr = MatGetSubMatrix_MPIBAIJ_Private(A,lrowp,icolp,local_size,MAT_INITIAL_MATRIX,B);CHKERRQ(ierr); 223382094794SBarry Smith /* clean up */ 223482094794SBarry Smith ierr = ISDestroy(lrowp);CHKERRQ(ierr); 223582094794SBarry Smith ierr = ISDestroy(icolp);CHKERRQ(ierr); 223682094794SBarry Smith PetscFunctionReturn(0); 223782094794SBarry Smith } 223882094794SBarry Smith 22398c7482ecSBarry Smith #undef __FUNCT__ 22408c7482ecSBarry Smith #define __FUNCT__ "MatGetGhosts_MPIBAIJ" 22418c7482ecSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatGetGhosts_MPIBAIJ(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[]) 22428c7482ecSBarry Smith { 22438c7482ecSBarry Smith Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*) mat->data; 22448c7482ecSBarry Smith Mat_SeqBAIJ *B = (Mat_SeqBAIJ*)baij->B->data; 22458c7482ecSBarry Smith 22468c7482ecSBarry Smith PetscFunctionBegin; 22478c7482ecSBarry Smith if (nghosts) { *nghosts = B->nbs;} 22488c7482ecSBarry Smith if (ghosts) {*ghosts = baij->garray;} 22498c7482ecSBarry Smith PetscFunctionReturn(0); 22508c7482ecSBarry Smith } 22518c7482ecSBarry Smith 2252f6d58c54SBarry Smith EXTERN PetscErrorCode CreateColmap_MPIBAIJ_Private(Mat); 2253f6d58c54SBarry Smith 2254f6d58c54SBarry Smith #undef __FUNCT__ 2255f6d58c54SBarry Smith #define __FUNCT__ "MatFDColoringCreate_MPIBAIJ" 2256f6d58c54SBarry Smith /* 2257f6d58c54SBarry Smith This routine is almost identical to MatFDColoringCreate_MPIBAIJ()! 2258f6d58c54SBarry Smith */ 2259f6d58c54SBarry Smith PetscErrorCode MatFDColoringCreate_MPIBAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c) 2260f6d58c54SBarry Smith { 2261f6d58c54SBarry Smith Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 2262f6d58c54SBarry Smith PetscErrorCode ierr; 2263f6d58c54SBarry Smith PetscMPIInt size,*ncolsonproc,*disp,nn; 2264f6d58c54SBarry Smith PetscInt bs,i,n,nrows,j,k,m,*rows = 0,*A_ci,*A_cj,ncols,col; 2265f6d58c54SBarry Smith const PetscInt *is; 2266f6d58c54SBarry Smith PetscInt nis = iscoloring->n,nctot,*cols,*B_ci,*B_cj; 2267f6d58c54SBarry Smith PetscInt *rowhit,M,cstart,cend,colb; 2268f6d58c54SBarry Smith PetscInt *columnsforrow,l; 2269f6d58c54SBarry Smith IS *isa; 2270ace3abfcSBarry Smith PetscBool done,flg; 2271f6d58c54SBarry Smith ISLocalToGlobalMapping map = mat->bmapping; 2272f6d58c54SBarry Smith PetscInt *ltog = (map ? map->indices : (PetscInt*) PETSC_NULL) ,ctype=c->ctype; 2273f6d58c54SBarry Smith 2274f6d58c54SBarry Smith PetscFunctionBegin; 2275e7e72b3dSBarry Smith if (!mat->assembled) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();"); 2276e7e72b3dSBarry 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"); 2277f6d58c54SBarry Smith 2278f6d58c54SBarry Smith ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr); 2279f6d58c54SBarry Smith ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr); 2280f6d58c54SBarry Smith M = mat->rmap->n/bs; 2281f6d58c54SBarry Smith cstart = mat->cmap->rstart/bs; 2282f6d58c54SBarry Smith cend = mat->cmap->rend/bs; 2283f6d58c54SBarry Smith c->M = mat->rmap->N/bs; /* set the global rows and columns and local rows */ 2284f6d58c54SBarry Smith c->N = mat->cmap->N/bs; 2285f6d58c54SBarry Smith c->m = mat->rmap->n/bs; 2286f6d58c54SBarry Smith c->rstart = mat->rmap->rstart/bs; 2287f6d58c54SBarry Smith 2288f6d58c54SBarry Smith c->ncolors = nis; 2289f6d58c54SBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt),&c->ncolumns);CHKERRQ(ierr); 2290f6d58c54SBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt*),&c->columns);CHKERRQ(ierr); 2291f6d58c54SBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt),&c->nrows);CHKERRQ(ierr); 2292f6d58c54SBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt*),&c->rows);CHKERRQ(ierr); 2293f6d58c54SBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt*),&c->columnsforrow);CHKERRQ(ierr); 2294f6d58c54SBarry Smith ierr = PetscLogObjectMemory(c,5*nis*sizeof(PetscInt));CHKERRQ(ierr); 2295f6d58c54SBarry Smith 2296f6d58c54SBarry Smith /* Allow access to data structures of local part of matrix */ 2297f6d58c54SBarry Smith if (!baij->colmap) { 2298f6d58c54SBarry Smith ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr); 2299f6d58c54SBarry Smith } 2300f6d58c54SBarry Smith ierr = MatGetColumnIJ(baij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr); 2301f6d58c54SBarry Smith ierr = MatGetColumnIJ(baij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr); 2302f6d58c54SBarry Smith 2303f6d58c54SBarry Smith ierr = PetscMalloc((M+1)*sizeof(PetscInt),&rowhit);CHKERRQ(ierr); 2304f6d58c54SBarry Smith ierr = PetscMalloc((M+1)*sizeof(PetscInt),&columnsforrow);CHKERRQ(ierr); 2305f6d58c54SBarry Smith 2306f6d58c54SBarry Smith for (i=0; i<nis; i++) { 2307f6d58c54SBarry Smith ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr); 2308f6d58c54SBarry Smith ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr); 2309f6d58c54SBarry Smith c->ncolumns[i] = n; 2310f6d58c54SBarry Smith if (n) { 2311f6d58c54SBarry Smith ierr = PetscMalloc(n*sizeof(PetscInt),&c->columns[i]);CHKERRQ(ierr); 2312f6d58c54SBarry Smith ierr = PetscLogObjectMemory(c,n*sizeof(PetscInt));CHKERRQ(ierr); 2313f6d58c54SBarry Smith ierr = PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));CHKERRQ(ierr); 2314f6d58c54SBarry Smith } else { 2315f6d58c54SBarry Smith c->columns[i] = 0; 2316f6d58c54SBarry Smith } 2317f6d58c54SBarry Smith 2318f6d58c54SBarry Smith if (ctype == IS_COLORING_GLOBAL){ 2319f6d58c54SBarry Smith /* Determine the total (parallel) number of columns of this color */ 2320f6d58c54SBarry Smith ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr); 2321fca92195SBarry Smith ierr = PetscMalloc2(size,PetscMPIInt,&ncolsonproc,size,PetscMPIInt,&disp);CHKERRQ(ierr); 2322f6d58c54SBarry Smith 2323f6d58c54SBarry Smith nn = PetscMPIIntCast(n); 2324f6d58c54SBarry Smith ierr = MPI_Allgather(&nn,1,MPI_INT,ncolsonproc,1,MPI_INT,((PetscObject)mat)->comm);CHKERRQ(ierr); 2325f6d58c54SBarry Smith nctot = 0; for (j=0; j<size; j++) {nctot += ncolsonproc[j];} 2326f6d58c54SBarry Smith if (!nctot) { 2327f6d58c54SBarry Smith ierr = PetscInfo(mat,"Coloring of matrix has some unneeded colors with no corresponding rows\n");CHKERRQ(ierr); 2328f6d58c54SBarry Smith } 2329f6d58c54SBarry Smith 2330f6d58c54SBarry Smith disp[0] = 0; 2331f6d58c54SBarry Smith for (j=1; j<size; j++) { 2332f6d58c54SBarry Smith disp[j] = disp[j-1] + ncolsonproc[j-1]; 2333f6d58c54SBarry Smith } 2334f6d58c54SBarry Smith 2335f6d58c54SBarry Smith /* Get complete list of columns for color on each processor */ 2336f6d58c54SBarry Smith ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr); 2337f6d58c54SBarry Smith ierr = MPI_Allgatherv((void*)is,n,MPIU_INT,cols,ncolsonproc,disp,MPIU_INT,((PetscObject)mat)->comm);CHKERRQ(ierr); 2338fca92195SBarry Smith ierr = PetscFree2(ncolsonproc,disp);CHKERRQ(ierr); 2339f6d58c54SBarry Smith } else if (ctype == IS_COLORING_GHOSTED){ 2340f6d58c54SBarry Smith /* Determine local number of columns of this color on this process, including ghost points */ 2341f6d58c54SBarry Smith nctot = n; 2342f6d58c54SBarry Smith ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr); 2343f6d58c54SBarry Smith ierr = PetscMemcpy(cols,is,n*sizeof(PetscInt));CHKERRQ(ierr); 2344f6d58c54SBarry Smith } else { 2345e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not provided for this MatFDColoring type"); 2346f6d58c54SBarry Smith } 2347f6d58c54SBarry Smith 2348f6d58c54SBarry Smith /* 2349f6d58c54SBarry Smith Mark all rows affect by these columns 2350f6d58c54SBarry Smith */ 2351f6d58c54SBarry Smith /* Temporary option to allow for debugging/testing */ 2352f6d58c54SBarry Smith flg = PETSC_FALSE; 2353acfcf0e5SJed Brown ierr = PetscOptionsGetBool(PETSC_NULL,"-matfdcoloring_slow",&flg,PETSC_NULL);CHKERRQ(ierr); 2354f6d58c54SBarry Smith if (!flg) {/*-----------------------------------------------------------------------------*/ 2355f6d58c54SBarry Smith /* crude, fast version */ 2356f6d58c54SBarry Smith ierr = PetscMemzero(rowhit,M*sizeof(PetscInt));CHKERRQ(ierr); 2357f6d58c54SBarry Smith /* loop over columns*/ 2358f6d58c54SBarry Smith for (j=0; j<nctot; j++) { 2359f6d58c54SBarry Smith if (ctype == IS_COLORING_GHOSTED) { 2360f6d58c54SBarry Smith col = ltog[cols[j]]; 2361f6d58c54SBarry Smith } else { 2362f6d58c54SBarry Smith col = cols[j]; 2363f6d58c54SBarry Smith } 2364f6d58c54SBarry Smith if (col >= cstart && col < cend) { 2365f6d58c54SBarry Smith /* column is in diagonal block of matrix */ 2366f6d58c54SBarry Smith rows = A_cj + A_ci[col-cstart]; 2367f6d58c54SBarry Smith m = A_ci[col-cstart+1] - A_ci[col-cstart]; 2368f6d58c54SBarry Smith } else { 2369f6d58c54SBarry Smith #if defined (PETSC_USE_CTABLE) 2370cb9801acSJed Brown ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr); 2371f6d58c54SBarry Smith colb --; 2372f6d58c54SBarry Smith #else 2373f6d58c54SBarry Smith colb = baij->colmap[col] - 1; 2374f6d58c54SBarry Smith #endif 2375f6d58c54SBarry Smith if (colb == -1) { 2376f6d58c54SBarry Smith m = 0; 2377f6d58c54SBarry Smith } else { 2378f6d58c54SBarry Smith colb = colb/bs; 2379f6d58c54SBarry Smith rows = B_cj + B_ci[colb]; 2380f6d58c54SBarry Smith m = B_ci[colb+1] - B_ci[colb]; 2381f6d58c54SBarry Smith } 2382f6d58c54SBarry Smith } 2383f6d58c54SBarry Smith /* loop over columns marking them in rowhit */ 2384f6d58c54SBarry Smith for (k=0; k<m; k++) { 2385f6d58c54SBarry Smith rowhit[*rows++] = col + 1; 2386f6d58c54SBarry Smith } 2387f6d58c54SBarry Smith } 2388f6d58c54SBarry Smith 2389f6d58c54SBarry Smith /* count the number of hits */ 2390f6d58c54SBarry Smith nrows = 0; 2391f6d58c54SBarry Smith for (j=0; j<M; j++) { 2392f6d58c54SBarry Smith if (rowhit[j]) nrows++; 2393f6d58c54SBarry Smith } 2394f6d58c54SBarry Smith c->nrows[i] = nrows; 2395f6d58c54SBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr); 2396f6d58c54SBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr); 2397f6d58c54SBarry Smith ierr = PetscLogObjectMemory(c,2*(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr); 2398f6d58c54SBarry Smith nrows = 0; 2399f6d58c54SBarry Smith for (j=0; j<M; j++) { 2400f6d58c54SBarry Smith if (rowhit[j]) { 2401f6d58c54SBarry Smith c->rows[i][nrows] = j; 2402f6d58c54SBarry Smith c->columnsforrow[i][nrows] = rowhit[j] - 1; 2403f6d58c54SBarry Smith nrows++; 2404f6d58c54SBarry Smith } 2405f6d58c54SBarry Smith } 2406f6d58c54SBarry Smith } else {/*-------------------------------------------------------------------------------*/ 2407f6d58c54SBarry Smith /* slow version, using rowhit as a linked list */ 2408f6d58c54SBarry Smith PetscInt currentcol,fm,mfm; 2409f6d58c54SBarry Smith rowhit[M] = M; 2410f6d58c54SBarry Smith nrows = 0; 2411f6d58c54SBarry Smith /* loop over columns*/ 2412f6d58c54SBarry Smith for (j=0; j<nctot; j++) { 2413f6d58c54SBarry Smith if (ctype == IS_COLORING_GHOSTED) { 2414f6d58c54SBarry Smith col = ltog[cols[j]]; 2415f6d58c54SBarry Smith } else { 2416f6d58c54SBarry Smith col = cols[j]; 2417f6d58c54SBarry Smith } 2418f6d58c54SBarry Smith if (col >= cstart && col < cend) { 2419f6d58c54SBarry Smith /* column is in diagonal block of matrix */ 2420f6d58c54SBarry Smith rows = A_cj + A_ci[col-cstart]; 2421f6d58c54SBarry Smith m = A_ci[col-cstart+1] - A_ci[col-cstart]; 2422f6d58c54SBarry Smith } else { 2423f6d58c54SBarry Smith #if defined (PETSC_USE_CTABLE) 2424f6d58c54SBarry Smith ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr); 2425f6d58c54SBarry Smith colb --; 2426f6d58c54SBarry Smith #else 2427f6d58c54SBarry Smith colb = baij->colmap[col] - 1; 2428f6d58c54SBarry Smith #endif 2429f6d58c54SBarry Smith if (colb == -1) { 2430f6d58c54SBarry Smith m = 0; 2431f6d58c54SBarry Smith } else { 2432f6d58c54SBarry Smith colb = colb/bs; 2433f6d58c54SBarry Smith rows = B_cj + B_ci[colb]; 2434f6d58c54SBarry Smith m = B_ci[colb+1] - B_ci[colb]; 2435f6d58c54SBarry Smith } 2436f6d58c54SBarry Smith } 2437f6d58c54SBarry Smith 2438f6d58c54SBarry Smith /* loop over columns marking them in rowhit */ 2439f6d58c54SBarry Smith fm = M; /* fm points to first entry in linked list */ 2440f6d58c54SBarry Smith for (k=0; k<m; k++) { 2441f6d58c54SBarry Smith currentcol = *rows++; 2442f6d58c54SBarry Smith /* is it already in the list? */ 2443f6d58c54SBarry Smith do { 2444f6d58c54SBarry Smith mfm = fm; 2445f6d58c54SBarry Smith fm = rowhit[fm]; 2446f6d58c54SBarry Smith } while (fm < currentcol); 2447f6d58c54SBarry Smith /* not in list so add it */ 2448f6d58c54SBarry Smith if (fm != currentcol) { 2449f6d58c54SBarry Smith nrows++; 2450f6d58c54SBarry Smith columnsforrow[currentcol] = col; 2451f6d58c54SBarry Smith /* next three lines insert new entry into linked list */ 2452f6d58c54SBarry Smith rowhit[mfm] = currentcol; 2453f6d58c54SBarry Smith rowhit[currentcol] = fm; 2454f6d58c54SBarry Smith fm = currentcol; 2455f6d58c54SBarry Smith /* fm points to present position in list since we know the columns are sorted */ 2456f6d58c54SBarry Smith } else { 2457e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid coloring of matrix detected"); 2458f6d58c54SBarry Smith } 2459f6d58c54SBarry Smith } 2460f6d58c54SBarry Smith } 2461f6d58c54SBarry Smith c->nrows[i] = nrows; 2462f6d58c54SBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr); 2463f6d58c54SBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr); 2464f6d58c54SBarry Smith ierr = PetscLogObjectMemory(c,(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr); 2465f6d58c54SBarry Smith /* now store the linked list of rows into c->rows[i] */ 2466f6d58c54SBarry Smith nrows = 0; 2467f6d58c54SBarry Smith fm = rowhit[M]; 2468f6d58c54SBarry Smith do { 2469f6d58c54SBarry Smith c->rows[i][nrows] = fm; 2470f6d58c54SBarry Smith c->columnsforrow[i][nrows++] = columnsforrow[fm]; 2471f6d58c54SBarry Smith fm = rowhit[fm]; 2472f6d58c54SBarry Smith } while (fm < M); 2473f6d58c54SBarry Smith } /* ---------------------------------------------------------------------------------------*/ 2474f6d58c54SBarry Smith ierr = PetscFree(cols);CHKERRQ(ierr); 2475f6d58c54SBarry Smith } 2476f6d58c54SBarry Smith 2477f6d58c54SBarry Smith /* Optimize by adding the vscale, and scaleforrow[][] fields */ 2478f6d58c54SBarry Smith /* 2479f6d58c54SBarry Smith vscale will contain the "diagonal" on processor scalings followed by the off processor 2480f6d58c54SBarry Smith */ 2481f6d58c54SBarry Smith if (ctype == IS_COLORING_GLOBAL) { 2482f6d58c54SBarry Smith PetscInt *garray; 2483f6d58c54SBarry Smith ierr = PetscMalloc(baij->B->cmap->n*sizeof(PetscInt),&garray);CHKERRQ(ierr); 2484f6d58c54SBarry Smith for (i=0; i<baij->B->cmap->n/bs; i++) { 2485f6d58c54SBarry Smith for (j=0; j<bs; j++) { 2486f6d58c54SBarry Smith garray[i*bs+j] = bs*baij->garray[i]+j; 2487f6d58c54SBarry Smith } 2488f6d58c54SBarry Smith } 2489f6d58c54SBarry Smith ierr = VecCreateGhost(((PetscObject)mat)->comm,baij->A->rmap->n,PETSC_DETERMINE,baij->B->cmap->n,garray,&c->vscale);CHKERRQ(ierr); 2490f6d58c54SBarry Smith ierr = PetscFree(garray);CHKERRQ(ierr); 2491f6d58c54SBarry Smith CHKMEMQ; 2492f6d58c54SBarry Smith ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr); 2493f6d58c54SBarry Smith for (k=0; k<c->ncolors; k++) { 2494f6d58c54SBarry Smith ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr); 2495f6d58c54SBarry Smith for (l=0; l<c->nrows[k]; l++) { 2496f6d58c54SBarry Smith col = c->columnsforrow[k][l]; 2497f6d58c54SBarry Smith if (col >= cstart && col < cend) { 2498f6d58c54SBarry Smith /* column is in diagonal block of matrix */ 2499f6d58c54SBarry Smith colb = col - cstart; 2500f6d58c54SBarry Smith } else { 2501f6d58c54SBarry Smith /* column is in "off-processor" part */ 2502f6d58c54SBarry Smith #if defined (PETSC_USE_CTABLE) 2503f6d58c54SBarry Smith ierr = PetscTableFind(baij->colmap,col+1,&colb);CHKERRQ(ierr); 2504f6d58c54SBarry Smith colb --; 2505f6d58c54SBarry Smith #else 2506f6d58c54SBarry Smith colb = baij->colmap[col] - 1; 2507f6d58c54SBarry Smith #endif 2508f6d58c54SBarry Smith colb = colb/bs; 2509f6d58c54SBarry Smith colb += cend - cstart; 2510f6d58c54SBarry Smith } 2511f6d58c54SBarry Smith c->vscaleforrow[k][l] = colb; 2512f6d58c54SBarry Smith } 2513f6d58c54SBarry Smith } 2514f6d58c54SBarry Smith } else if (ctype == IS_COLORING_GHOSTED) { 2515f6d58c54SBarry Smith /* Get gtol mapping */ 2516f6d58c54SBarry Smith PetscInt N = mat->cmap->N, *gtol; 2517f6d58c54SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),>ol);CHKERRQ(ierr); 2518f6d58c54SBarry Smith for (i=0; i<N; i++) gtol[i] = -1; 2519f6d58c54SBarry Smith for (i=0; i<map->n; i++) gtol[ltog[i]] = i; 2520f6d58c54SBarry Smith 2521f6d58c54SBarry Smith c->vscale = 0; /* will be created in MatFDColoringApply() */ 2522f6d58c54SBarry Smith ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr); 2523f6d58c54SBarry Smith for (k=0; k<c->ncolors; k++) { 2524f6d58c54SBarry Smith ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr); 2525f6d58c54SBarry Smith for (l=0; l<c->nrows[k]; l++) { 2526f6d58c54SBarry Smith col = c->columnsforrow[k][l]; /* global column index */ 2527f6d58c54SBarry Smith c->vscaleforrow[k][l] = gtol[col]; /* local column index */ 2528f6d58c54SBarry Smith } 2529f6d58c54SBarry Smith } 2530f6d58c54SBarry Smith ierr = PetscFree(gtol);CHKERRQ(ierr); 2531f6d58c54SBarry Smith } 2532f6d58c54SBarry Smith ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr); 2533f6d58c54SBarry Smith 2534f6d58c54SBarry Smith ierr = PetscFree(rowhit);CHKERRQ(ierr); 2535f6d58c54SBarry Smith ierr = PetscFree(columnsforrow);CHKERRQ(ierr); 2536f6d58c54SBarry Smith ierr = MatRestoreColumnIJ(baij->A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr); 2537f6d58c54SBarry Smith ierr = MatRestoreColumnIJ(baij->B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr); 2538f6d58c54SBarry Smith CHKMEMQ; 2539f6d58c54SBarry Smith PetscFunctionReturn(0); 2540f6d58c54SBarry Smith } 2541f6d58c54SBarry Smith 2542f6d58c54SBarry Smith #undef __FUNCT__ 2543f6d58c54SBarry Smith #define __FUNCT__ "MatGetSeqNonzerostructure_MPIBAIJ" 2544f6d58c54SBarry Smith PetscErrorCode MatGetSeqNonzerostructure_MPIBAIJ(Mat A,Mat *newmat) 2545f6d58c54SBarry Smith { 2546f6d58c54SBarry Smith Mat B; 2547f6d58c54SBarry Smith Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data; 2548f6d58c54SBarry Smith Mat_SeqBAIJ *ad = (Mat_SeqBAIJ*)a->A->data,*bd = (Mat_SeqBAIJ*)a->B->data; 2549f6d58c54SBarry Smith Mat_SeqAIJ *b; 2550f6d58c54SBarry Smith PetscErrorCode ierr; 2551f6d58c54SBarry Smith PetscMPIInt size,rank,*recvcounts = 0,*displs = 0; 2552f6d58c54SBarry Smith PetscInt sendcount,i,*rstarts = A->rmap->range,n,cnt,j,bs = A->rmap->bs; 2553f6d58c54SBarry Smith PetscInt m,*garray = a->garray,*lens,*jsendbuf,*a_jsendbuf,*b_jsendbuf; 2554f6d58c54SBarry Smith 2555f6d58c54SBarry Smith PetscFunctionBegin; 2556f6d58c54SBarry Smith ierr = MPI_Comm_size(((PetscObject)A)->comm,&size);CHKERRQ(ierr); 2557f6d58c54SBarry Smith ierr = MPI_Comm_rank(((PetscObject)A)->comm,&rank);CHKERRQ(ierr); 2558f6d58c54SBarry Smith 2559f6d58c54SBarry Smith /* ---------------------------------------------------------------- 2560f6d58c54SBarry Smith Tell every processor the number of nonzeros per row 2561f6d58c54SBarry Smith */ 2562f6d58c54SBarry Smith ierr = PetscMalloc((A->rmap->N/bs)*sizeof(PetscInt),&lens);CHKERRQ(ierr); 2563f6d58c54SBarry Smith for (i=A->rmap->rstart/bs; i<A->rmap->rend/bs; i++) { 2564f6d58c54SBarry 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]; 2565f6d58c54SBarry Smith } 2566f6d58c54SBarry Smith sendcount = A->rmap->rend/bs - A->rmap->rstart/bs; 2567f6d58c54SBarry Smith ierr = PetscMalloc(2*size*sizeof(PetscMPIInt),&recvcounts);CHKERRQ(ierr); 2568f6d58c54SBarry Smith displs = recvcounts + size; 2569f6d58c54SBarry Smith for (i=0; i<size; i++) { 2570f6d58c54SBarry Smith recvcounts[i] = A->rmap->range[i+1]/bs - A->rmap->range[i]/bs; 2571f6d58c54SBarry Smith displs[i] = A->rmap->range[i]/bs; 2572f6d58c54SBarry Smith } 2573f6d58c54SBarry Smith #if defined(PETSC_HAVE_MPI_IN_PLACE) 2574f6d58c54SBarry Smith ierr = MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,lens,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr); 2575f6d58c54SBarry Smith #else 2576f6d58c54SBarry Smith ierr = MPI_Allgatherv(lens+A->rmap->rstart/bs,sendcount,MPIU_INT,lens,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr); 2577f6d58c54SBarry Smith #endif 2578f6d58c54SBarry Smith /* --------------------------------------------------------------- 2579f6d58c54SBarry Smith Create the sequential matrix of the same type as the local block diagonal 2580f6d58c54SBarry Smith */ 2581f6d58c54SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr); 2582f6d58c54SBarry Smith ierr = MatSetSizes(B,A->rmap->N/bs,A->cmap->N/bs,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 2583f6d58c54SBarry Smith ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 2584f6d58c54SBarry Smith ierr = MatSeqAIJSetPreallocation(B,0,lens);CHKERRQ(ierr); 2585f6d58c54SBarry Smith b = (Mat_SeqAIJ *)B->data; 2586f6d58c54SBarry Smith 2587f6d58c54SBarry Smith /*-------------------------------------------------------------------- 2588f6d58c54SBarry Smith Copy my part of matrix column indices over 2589f6d58c54SBarry Smith */ 2590f6d58c54SBarry Smith sendcount = ad->nz + bd->nz; 2591f6d58c54SBarry Smith jsendbuf = b->j + b->i[rstarts[rank]/bs]; 2592f6d58c54SBarry Smith a_jsendbuf = ad->j; 2593f6d58c54SBarry Smith b_jsendbuf = bd->j; 2594f6d58c54SBarry Smith n = A->rmap->rend/bs - A->rmap->rstart/bs; 2595f6d58c54SBarry Smith cnt = 0; 2596f6d58c54SBarry Smith for (i=0; i<n; i++) { 2597f6d58c54SBarry Smith 2598f6d58c54SBarry Smith /* put in lower diagonal portion */ 2599f6d58c54SBarry Smith m = bd->i[i+1] - bd->i[i]; 2600f6d58c54SBarry Smith while (m > 0) { 2601f6d58c54SBarry Smith /* is it above diagonal (in bd (compressed) numbering) */ 2602f6d58c54SBarry Smith if (garray[*b_jsendbuf] > A->rmap->rstart/bs + i) break; 2603f6d58c54SBarry Smith jsendbuf[cnt++] = garray[*b_jsendbuf++]; 2604f6d58c54SBarry Smith m--; 2605f6d58c54SBarry Smith } 2606f6d58c54SBarry Smith 2607f6d58c54SBarry Smith /* put in diagonal portion */ 2608f6d58c54SBarry Smith for (j=ad->i[i]; j<ad->i[i+1]; j++) { 2609f6d58c54SBarry Smith jsendbuf[cnt++] = A->rmap->rstart/bs + *a_jsendbuf++; 2610f6d58c54SBarry Smith } 2611f6d58c54SBarry Smith 2612f6d58c54SBarry Smith /* put in upper diagonal portion */ 2613f6d58c54SBarry Smith while (m-- > 0) { 2614f6d58c54SBarry Smith jsendbuf[cnt++] = garray[*b_jsendbuf++]; 2615f6d58c54SBarry Smith } 2616f6d58c54SBarry Smith } 2617e32f2f54SBarry Smith if (cnt != sendcount) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupted PETSc matrix: nz given %D actual nz %D",sendcount,cnt); 2618f6d58c54SBarry Smith 2619f6d58c54SBarry Smith /*-------------------------------------------------------------------- 2620f6d58c54SBarry Smith Gather all column indices to all processors 2621f6d58c54SBarry Smith */ 2622f6d58c54SBarry Smith for (i=0; i<size; i++) { 2623f6d58c54SBarry Smith recvcounts[i] = 0; 2624f6d58c54SBarry Smith for (j=A->rmap->range[i]/bs; j<A->rmap->range[i+1]/bs; j++) { 2625f6d58c54SBarry Smith recvcounts[i] += lens[j]; 2626f6d58c54SBarry Smith } 2627f6d58c54SBarry Smith } 2628f6d58c54SBarry Smith displs[0] = 0; 2629f6d58c54SBarry Smith for (i=1; i<size; i++) { 2630f6d58c54SBarry Smith displs[i] = displs[i-1] + recvcounts[i-1]; 2631f6d58c54SBarry Smith } 2632f6d58c54SBarry Smith #if defined(PETSC_HAVE_MPI_IN_PLACE) 2633f6d58c54SBarry Smith ierr = MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,b->j,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr); 2634f6d58c54SBarry Smith #else 2635f6d58c54SBarry Smith ierr = MPI_Allgatherv(jsendbuf,sendcount,MPIU_INT,b->j,recvcounts,displs,MPIU_INT,((PetscObject)A)->comm);CHKERRQ(ierr); 2636f6d58c54SBarry Smith #endif 2637f6d58c54SBarry Smith /*-------------------------------------------------------------------- 2638f6d58c54SBarry Smith Assemble the matrix into useable form (note numerical values not yet set) 2639f6d58c54SBarry Smith */ 2640f6d58c54SBarry Smith /* set the b->ilen (length of each row) values */ 2641f6d58c54SBarry Smith ierr = PetscMemcpy(b->ilen,lens,(A->rmap->N/bs)*sizeof(PetscInt));CHKERRQ(ierr); 2642f6d58c54SBarry Smith /* set the b->i indices */ 2643f6d58c54SBarry Smith b->i[0] = 0; 2644f6d58c54SBarry Smith for (i=1; i<=A->rmap->N/bs; i++) { 2645f6d58c54SBarry Smith b->i[i] = b->i[i-1] + lens[i-1]; 2646f6d58c54SBarry Smith } 2647f6d58c54SBarry Smith ierr = PetscFree(lens);CHKERRQ(ierr); 2648f6d58c54SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2649f6d58c54SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2650f6d58c54SBarry Smith ierr = PetscFree(recvcounts);CHKERRQ(ierr); 2651f6d58c54SBarry Smith 2652f6d58c54SBarry Smith if (A->symmetric){ 2653f6d58c54SBarry Smith ierr = MatSetOption(B,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 2654f6d58c54SBarry Smith } else if (A->hermitian) { 2655f6d58c54SBarry Smith ierr = MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr); 2656f6d58c54SBarry Smith } else if (A->structurally_symmetric) { 2657f6d58c54SBarry Smith ierr = MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 2658f6d58c54SBarry Smith } 2659f6d58c54SBarry Smith *newmat = B; 2660f6d58c54SBarry Smith PetscFunctionReturn(0); 2661f6d58c54SBarry Smith } 2662f6d58c54SBarry Smith 2663b1a666ecSBarry Smith #undef __FUNCT__ 2664b1a666ecSBarry Smith #define __FUNCT__ "MatSOR_MPIBAIJ" 2665b1a666ecSBarry Smith PetscErrorCode MatSOR_MPIBAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 2666b1a666ecSBarry Smith { 2667b1a666ecSBarry Smith Mat_MPIBAIJ *mat = (Mat_MPIBAIJ*)matin->data; 2668b1a666ecSBarry Smith PetscErrorCode ierr; 2669b1a666ecSBarry Smith Vec bb1 = 0; 2670b1a666ecSBarry Smith 2671b1a666ecSBarry Smith PetscFunctionBegin; 2672b1a666ecSBarry Smith if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS) { 2673b1a666ecSBarry Smith ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr); 2674b1a666ecSBarry Smith } 2675b1a666ecSBarry Smith 2676b1a666ecSBarry Smith if (flag == SOR_APPLY_UPPER) { 2677b1a666ecSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 2678b1a666ecSBarry Smith PetscFunctionReturn(0); 2679b1a666ecSBarry Smith } 2680b1a666ecSBarry Smith 2681b1a666ecSBarry Smith if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){ 2682b1a666ecSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 2683b1a666ecSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 2684b1a666ecSBarry Smith its--; 2685b1a666ecSBarry Smith } 2686b1a666ecSBarry Smith 2687b1a666ecSBarry Smith while (its--) { 2688b1a666ecSBarry Smith ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2689b1a666ecSBarry Smith ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2690b1a666ecSBarry Smith 2691b1a666ecSBarry Smith /* update rhs: bb1 = bb - B*x */ 2692b1a666ecSBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 2693b1a666ecSBarry Smith ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 2694b1a666ecSBarry Smith 2695b1a666ecSBarry Smith /* local sweep */ 2696b1a666ecSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 2697b1a666ecSBarry Smith } 2698b1a666ecSBarry Smith } else if (flag & SOR_LOCAL_FORWARD_SWEEP){ 2699b1a666ecSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 2700b1a666ecSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 2701b1a666ecSBarry Smith its--; 2702b1a666ecSBarry Smith } 2703b1a666ecSBarry Smith while (its--) { 2704b1a666ecSBarry Smith ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2705b1a666ecSBarry Smith ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2706b1a666ecSBarry Smith 2707b1a666ecSBarry Smith /* update rhs: bb1 = bb - B*x */ 2708b1a666ecSBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 2709b1a666ecSBarry Smith ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 2710b1a666ecSBarry Smith 2711b1a666ecSBarry Smith /* local sweep */ 2712b1a666ecSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 2713b1a666ecSBarry Smith } 2714b1a666ecSBarry Smith } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){ 2715b1a666ecSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 2716b1a666ecSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 2717b1a666ecSBarry Smith its--; 2718b1a666ecSBarry Smith } 2719b1a666ecSBarry Smith while (its--) { 2720b1a666ecSBarry Smith ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2721b1a666ecSBarry Smith ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2722b1a666ecSBarry Smith 2723b1a666ecSBarry Smith /* update rhs: bb1 = bb - B*x */ 2724b1a666ecSBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 2725b1a666ecSBarry Smith ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 2726b1a666ecSBarry Smith 2727b1a666ecSBarry Smith /* local sweep */ 2728b1a666ecSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 2729b1a666ecSBarry Smith } 2730e7e72b3dSBarry Smith } else SETERRQ(((PetscObject)matin)->comm,PETSC_ERR_SUP,"Parallel version of SOR requested not supported"); 2731b1a666ecSBarry Smith 2732b1a666ecSBarry Smith if (bb1) {ierr = VecDestroy(bb1);CHKERRQ(ierr);} 2733b1a666ecSBarry Smith PetscFunctionReturn(0); 2734b1a666ecSBarry Smith } 2735b1a666ecSBarry Smith 2736f6d58c54SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_BAIJ(Mat,MatFDColoring,Vec,MatStructure*,void*); 2737f6d58c54SBarry Smith 27388c7482ecSBarry Smith 273979bdfe76SSatish Balay /* -------------------------------------------------------------------*/ 2740cc2dc46cSBarry Smith static struct _MatOps MatOps_Values = { 2741cc2dc46cSBarry Smith MatSetValues_MPIBAIJ, 2742cc2dc46cSBarry Smith MatGetRow_MPIBAIJ, 2743cc2dc46cSBarry Smith MatRestoreRow_MPIBAIJ, 2744cc2dc46cSBarry Smith MatMult_MPIBAIJ, 274597304618SKris Buschelman /* 4*/ MatMultAdd_MPIBAIJ, 27467c922b88SBarry Smith MatMultTranspose_MPIBAIJ, 27477c922b88SBarry Smith MatMultTransposeAdd_MPIBAIJ, 2748cc2dc46cSBarry Smith 0, 2749cc2dc46cSBarry Smith 0, 2750cc2dc46cSBarry Smith 0, 275197304618SKris Buschelman /*10*/ 0, 2752cc2dc46cSBarry Smith 0, 2753cc2dc46cSBarry Smith 0, 2754b1a666ecSBarry Smith MatSOR_MPIBAIJ, 2755cc2dc46cSBarry Smith MatTranspose_MPIBAIJ, 275697304618SKris Buschelman /*15*/ MatGetInfo_MPIBAIJ, 27577fc3c18eSBarry Smith MatEqual_MPIBAIJ, 2758cc2dc46cSBarry Smith MatGetDiagonal_MPIBAIJ, 2759cc2dc46cSBarry Smith MatDiagonalScale_MPIBAIJ, 2760cc2dc46cSBarry Smith MatNorm_MPIBAIJ, 276197304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIBAIJ, 2762cc2dc46cSBarry Smith MatAssemblyEnd_MPIBAIJ, 2763cc2dc46cSBarry Smith MatSetOption_MPIBAIJ, 2764cc2dc46cSBarry Smith MatZeroEntries_MPIBAIJ, 2765d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIBAIJ, 2766cc2dc46cSBarry Smith 0, 2767cc2dc46cSBarry Smith 0, 2768cc2dc46cSBarry Smith 0, 2769cc2dc46cSBarry Smith 0, 2770d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_MPIBAIJ, 2771273d9f13SBarry Smith 0, 2772cc2dc46cSBarry Smith 0, 2773cc2dc46cSBarry Smith 0, 2774cc2dc46cSBarry Smith 0, 2775d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIBAIJ, 2776cc2dc46cSBarry Smith 0, 2777cc2dc46cSBarry Smith 0, 2778cc2dc46cSBarry Smith 0, 2779cc2dc46cSBarry Smith 0, 2780d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIBAIJ, 2781cc2dc46cSBarry Smith MatGetSubMatrices_MPIBAIJ, 2782cc2dc46cSBarry Smith MatIncreaseOverlap_MPIBAIJ, 2783cc2dc46cSBarry Smith MatGetValues_MPIBAIJ, 27843c896bc6SHong Zhang MatCopy_MPIBAIJ, 2785d519adbfSMatthew Knepley /*44*/ 0, 2786cc2dc46cSBarry Smith MatScale_MPIBAIJ, 2787cc2dc46cSBarry Smith 0, 2788cc2dc46cSBarry Smith 0, 2789cc2dc46cSBarry Smith 0, 279041c166b1SJed Brown /*49*/ MatSetBlockSize_MPIBAIJ, 2791cc2dc46cSBarry Smith 0, 2792cc2dc46cSBarry Smith 0, 2793cc2dc46cSBarry Smith 0, 2794cc2dc46cSBarry Smith 0, 2795f6d58c54SBarry Smith /*54*/ MatFDColoringCreate_MPIBAIJ, 2796cc2dc46cSBarry Smith 0, 2797cc2dc46cSBarry Smith MatSetUnfactored_MPIBAIJ, 279882094794SBarry Smith MatPermute_MPIBAIJ, 2799cc2dc46cSBarry Smith MatSetValuesBlocked_MPIBAIJ, 2800d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIBAIJ, 2801f14a1c24SBarry Smith MatDestroy_MPIBAIJ, 2802f14a1c24SBarry Smith MatView_MPIBAIJ, 2803357abbc8SBarry Smith 0, 28047843d17aSBarry Smith 0, 2805d519adbfSMatthew Knepley /*64*/ 0, 28067843d17aSBarry Smith 0, 28077843d17aSBarry Smith 0, 28087843d17aSBarry Smith 0, 28097843d17aSBarry Smith 0, 2810d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIBAIJ, 28117843d17aSBarry Smith 0, 281297304618SKris Buschelman 0, 281397304618SKris Buschelman 0, 281497304618SKris Buschelman 0, 2815d519adbfSMatthew Knepley /*74*/ 0, 2816f6d58c54SBarry Smith MatFDColoringApply_BAIJ, 281797304618SKris Buschelman 0, 281897304618SKris Buschelman 0, 281997304618SKris Buschelman 0, 2820d519adbfSMatthew Knepley /*79*/ 0, 282197304618SKris Buschelman 0, 282297304618SKris Buschelman 0, 282397304618SKris Buschelman 0, 28245bba2384SShri Abhyankar MatLoad_MPIBAIJ, 2825d519adbfSMatthew Knepley /*84*/ 0, 2826865e5f61SKris Buschelman 0, 2827865e5f61SKris Buschelman 0, 2828865e5f61SKris Buschelman 0, 2829865e5f61SKris Buschelman 0, 2830d519adbfSMatthew Knepley /*89*/ 0, 2831865e5f61SKris Buschelman 0, 2832865e5f61SKris Buschelman 0, 2833865e5f61SKris Buschelman 0, 2834865e5f61SKris Buschelman 0, 2835d519adbfSMatthew Knepley /*94*/ 0, 2836865e5f61SKris Buschelman 0, 2837865e5f61SKris Buschelman 0, 283899cafbc1SBarry Smith 0, 283999cafbc1SBarry Smith 0, 2840d519adbfSMatthew Knepley /*99*/ 0, 284199cafbc1SBarry Smith 0, 284299cafbc1SBarry Smith 0, 284399cafbc1SBarry Smith 0, 284499cafbc1SBarry Smith 0, 2845d519adbfSMatthew Knepley /*104*/0, 284699cafbc1SBarry Smith MatRealPart_MPIBAIJ, 28478c7482ecSBarry Smith MatImaginaryPart_MPIBAIJ, 28488c7482ecSBarry Smith 0, 28498c7482ecSBarry Smith 0, 2850d519adbfSMatthew Knepley /*109*/0, 28518c7482ecSBarry Smith 0, 28528c7482ecSBarry Smith 0, 28538c7482ecSBarry Smith 0, 28548c7482ecSBarry Smith 0, 2855f6d58c54SBarry Smith /*114*/MatGetSeqNonzerostructure_MPIBAIJ, 28568c7482ecSBarry Smith 0, 28574683f7a4SShri Abhyankar MatGetGhosts_MPIBAIJ, 28584683f7a4SShri Abhyankar 0, 28594683f7a4SShri Abhyankar 0, 28604683f7a4SShri Abhyankar /*119*/0, 28614683f7a4SShri Abhyankar 0, 28624683f7a4SShri Abhyankar 0, 28635bba2384SShri Abhyankar 0 28648c7482ecSBarry Smith }; 286579bdfe76SSatish Balay 2866e18c124aSSatish Balay EXTERN_C_BEGIN 28674a2ae208SSatish Balay #undef __FUNCT__ 28684a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonalBlock_MPIBAIJ" 2869ace3abfcSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatGetDiagonalBlock_MPIBAIJ(Mat A,PetscBool *iscopy,MatReuse reuse,Mat *a) 28705ef9f2a5SBarry Smith { 28715ef9f2a5SBarry Smith PetscFunctionBegin; 28725ef9f2a5SBarry Smith *a = ((Mat_MPIBAIJ *)A->data)->A; 28735ef9f2a5SBarry Smith *iscopy = PETSC_FALSE; 28745ef9f2a5SBarry Smith PetscFunctionReturn(0); 28755ef9f2a5SBarry Smith } 2876e18c124aSSatish Balay EXTERN_C_END 287779bdfe76SSatish Balay 2878273d9f13SBarry Smith EXTERN_C_BEGIN 2879f69a0ea3SMatthew Knepley extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIBAIJ_MPISBAIJ(Mat, MatType,MatReuse,Mat*); 2880d94109b8SHong Zhang EXTERN_C_END 2881d94109b8SHong Zhang 2882b8d659d7SLisandro Dalcin EXTERN_C_BEGIN 2883aac34f13SBarry Smith #undef __FUNCT__ 2884aac34f13SBarry Smith #define __FUNCT__ "MatMPIBAIJSetPreallocationCSR_MPIBAIJ" 2885cf12db73SBarry Smith PetscErrorCode MatMPIBAIJSetPreallocationCSR_MPIBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[]) 2886aac34f13SBarry Smith { 2887b8d659d7SLisandro Dalcin PetscInt m,rstart,cstart,cend; 2888b8d659d7SLisandro Dalcin PetscInt i,j,d,nz,nz_max=0,*d_nnz=0,*o_nnz=0; 2889b8d659d7SLisandro Dalcin const PetscInt *JJ=0; 2890b8d659d7SLisandro Dalcin PetscScalar *values=0; 2891aac34f13SBarry Smith PetscErrorCode ierr; 2892aac34f13SBarry Smith 2893aac34f13SBarry Smith PetscFunctionBegin; 2894b8d659d7SLisandro Dalcin 289565e19b50SBarry Smith if (bs < 1) SETERRQ1(((PetscObject)B)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs); 289626283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr); 289726283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr); 289826283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 289926283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 2900d0f46423SBarry Smith m = B->rmap->n/bs; 2901d0f46423SBarry Smith rstart = B->rmap->rstart/bs; 2902d0f46423SBarry Smith cstart = B->cmap->rstart/bs; 2903d0f46423SBarry Smith cend = B->cmap->rend/bs; 2904b8d659d7SLisandro Dalcin 2905e32f2f54SBarry Smith if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]); 2906fca92195SBarry Smith ierr = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr); 2907aac34f13SBarry Smith for (i=0; i<m; i++) { 2908cf12db73SBarry Smith nz = ii[i+1] - ii[i]; 2909e32f2f54SBarry Smith if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative number of columns %D",i,nz); 2910b8d659d7SLisandro Dalcin nz_max = PetscMax(nz_max,nz); 2911cf12db73SBarry Smith JJ = jj + ii[i]; 2912b8d659d7SLisandro Dalcin for (j=0; j<nz; j++) { 2913aac34f13SBarry Smith if (*JJ >= cstart) break; 2914aac34f13SBarry Smith JJ++; 2915aac34f13SBarry Smith } 2916aac34f13SBarry Smith d = 0; 2917b8d659d7SLisandro Dalcin for (; j<nz; j++) { 2918aac34f13SBarry Smith if (*JJ++ >= cend) break; 2919aac34f13SBarry Smith d++; 2920aac34f13SBarry Smith } 2921aac34f13SBarry Smith d_nnz[i] = d; 2922b8d659d7SLisandro Dalcin o_nnz[i] = nz - d; 2923aac34f13SBarry Smith } 2924aac34f13SBarry Smith ierr = MatMPIBAIJSetPreallocation(B,bs,0,d_nnz,0,o_nnz);CHKERRQ(ierr); 2925fca92195SBarry Smith ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr); 2926aac34f13SBarry Smith 2927b8d659d7SLisandro Dalcin values = (PetscScalar*)V; 2928b8d659d7SLisandro Dalcin if (!values) { 2929fca92195SBarry Smith ierr = PetscMalloc(bs*bs*nz_max*sizeof(PetscScalar),&values);CHKERRQ(ierr); 2930b8d659d7SLisandro Dalcin ierr = PetscMemzero(values,bs*bs*nz_max*sizeof(PetscScalar));CHKERRQ(ierr); 2931b8d659d7SLisandro Dalcin } 2932b8d659d7SLisandro Dalcin for (i=0; i<m; i++) { 2933b8d659d7SLisandro Dalcin PetscInt row = i + rstart; 2934cf12db73SBarry Smith PetscInt ncols = ii[i+1] - ii[i]; 2935cf12db73SBarry Smith const PetscInt *icols = jj + ii[i]; 2936cf12db73SBarry Smith const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0); 2937b8d659d7SLisandro Dalcin ierr = MatSetValuesBlocked_MPIBAIJ(B,1,&row,ncols,icols,svals,INSERT_VALUES);CHKERRQ(ierr); 2938aac34f13SBarry Smith } 2939aac34f13SBarry Smith 2940b8d659d7SLisandro Dalcin if (!V) { ierr = PetscFree(values);CHKERRQ(ierr); } 2941aac34f13SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2942aac34f13SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2943aac34f13SBarry Smith 2944aac34f13SBarry Smith PetscFunctionReturn(0); 2945aac34f13SBarry Smith } 2946b8d659d7SLisandro Dalcin EXTERN_C_END 2947aac34f13SBarry Smith 2948aac34f13SBarry Smith #undef __FUNCT__ 2949aac34f13SBarry Smith #define __FUNCT__ "MatMPIBAIJSetPreallocationCSR" 2950aac34f13SBarry Smith /*@C 2951dfb205c3SBarry Smith MatMPIBAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in BAIJ format 2952aac34f13SBarry Smith (the default parallel PETSc format). 2953aac34f13SBarry Smith 2954aac34f13SBarry Smith Collective on MPI_Comm 2955aac34f13SBarry Smith 2956aac34f13SBarry Smith Input Parameters: 2957aac34f13SBarry Smith + A - the matrix 2958dfb205c3SBarry Smith . bs - the block size 2959aac34f13SBarry Smith . i - the indices into j for the start of each local row (starts with zero) 2960aac34f13SBarry Smith . j - the column indices for each local row (starts with zero) these must be sorted for each row 2961aac34f13SBarry Smith - v - optional values in the matrix 2962aac34f13SBarry Smith 2963aac34f13SBarry Smith Level: developer 2964aac34f13SBarry Smith 2965aac34f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 2966aac34f13SBarry Smith 2967aac34f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIBAIJSetPreallocation(), MatCreateMPIAIJ(), MPIAIJ 2968aac34f13SBarry Smith @*/ 2969be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[]) 2970aac34f13SBarry Smith { 29714ac538c5SBarry Smith PetscErrorCode ierr; 2972aac34f13SBarry Smith 2973aac34f13SBarry Smith PetscFunctionBegin; 29744ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));CHKERRQ(ierr); 2975aac34f13SBarry Smith PetscFunctionReturn(0); 2976aac34f13SBarry Smith } 2977aac34f13SBarry Smith 2978d94109b8SHong Zhang EXTERN_C_BEGIN 29794a2ae208SSatish Balay #undef __FUNCT__ 2980a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIBAIJSetPreallocation_MPIBAIJ" 2981be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJSetPreallocation_MPIBAIJ(Mat B,PetscInt bs,PetscInt d_nz,PetscInt *d_nnz,PetscInt o_nz,PetscInt *o_nnz) 2982a23d5eceSKris Buschelman { 2983a23d5eceSKris Buschelman Mat_MPIBAIJ *b; 2984dfbe8321SBarry Smith PetscErrorCode ierr; 2985db4efbfdSBarry Smith PetscInt i, newbs = PetscAbs(bs); 2986a23d5eceSKris Buschelman 2987a23d5eceSKris Buschelman PetscFunctionBegin; 2988db4efbfdSBarry Smith if (bs < 0) { 29897adad957SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Options for MPIBAIJ matrix","Mat");CHKERRQ(ierr); 2990db4efbfdSBarry Smith ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatMPIBAIJSetPreallocation",newbs,&newbs,PETSC_NULL);CHKERRQ(ierr); 29918c07d4e3SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 2992db4efbfdSBarry Smith bs = PetscAbs(bs); 2993db4efbfdSBarry Smith } 2994e7e72b3dSBarry 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"); 2995db4efbfdSBarry Smith bs = newbs; 2996db4efbfdSBarry Smith 2997a23d5eceSKris Buschelman 2998e7e72b3dSBarry Smith if (bs < 1) SETERRQ(((PetscObject)B)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive"); 2999a23d5eceSKris Buschelman if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5; 3000a23d5eceSKris Buschelman if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2; 3001e32f2f54SBarry Smith if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz); 3002e32f2f54SBarry Smith if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz); 3003899cda47SBarry Smith 300426283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr); 300526283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr); 300626283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 300726283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3008899cda47SBarry Smith 3009a23d5eceSKris Buschelman if (d_nnz) { 3010d0f46423SBarry Smith for (i=0; i<B->rmap->n/bs; i++) { 3011e32f2f54SBarry 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]); 3012a23d5eceSKris Buschelman } 3013a23d5eceSKris Buschelman } 3014a23d5eceSKris Buschelman if (o_nnz) { 3015d0f46423SBarry Smith for (i=0; i<B->rmap->n/bs; i++) { 3016e32f2f54SBarry 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]); 3017a23d5eceSKris Buschelman } 3018a23d5eceSKris Buschelman } 3019a23d5eceSKris Buschelman 3020a23d5eceSKris Buschelman b = (Mat_MPIBAIJ*)B->data; 3021a23d5eceSKris Buschelman b->bs2 = bs*bs; 3022d0f46423SBarry Smith b->mbs = B->rmap->n/bs; 3023d0f46423SBarry Smith b->nbs = B->cmap->n/bs; 3024d0f46423SBarry Smith b->Mbs = B->rmap->N/bs; 3025d0f46423SBarry Smith b->Nbs = B->cmap->N/bs; 3026a23d5eceSKris Buschelman 3027a23d5eceSKris Buschelman for (i=0; i<=b->size; i++) { 3028d0f46423SBarry Smith b->rangebs[i] = B->rmap->range[i]/bs; 3029a23d5eceSKris Buschelman } 3030d0f46423SBarry Smith b->rstartbs = B->rmap->rstart/bs; 3031d0f46423SBarry Smith b->rendbs = B->rmap->rend/bs; 3032d0f46423SBarry Smith b->cstartbs = B->cmap->rstart/bs; 3033d0f46423SBarry Smith b->cendbs = B->cmap->rend/bs; 3034a23d5eceSKris Buschelman 3035526dfc15SBarry Smith if (!B->preallocated) { 3036f69a0ea3SMatthew Knepley ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr); 3037d0f46423SBarry Smith ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr); 30389c097c71SKris Buschelman ierr = MatSetType(b->A,MATSEQBAIJ);CHKERRQ(ierr); 303952e6d16bSBarry Smith ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr); 3040f69a0ea3SMatthew Knepley ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr); 3041d0f46423SBarry Smith ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr); 30429c097c71SKris Buschelman ierr = MatSetType(b->B,MATSEQBAIJ);CHKERRQ(ierr); 304352e6d16bSBarry Smith ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr); 30447adad957SLisandro Dalcin ierr = MatStashCreate_Private(((PetscObject)B)->comm,bs,&B->bstash);CHKERRQ(ierr); 3045526dfc15SBarry Smith } 3046a23d5eceSKris Buschelman 3047526dfc15SBarry Smith ierr = MatSeqBAIJSetPreallocation(b->A,bs,d_nz,d_nnz);CHKERRQ(ierr); 3048526dfc15SBarry Smith ierr = MatSeqBAIJSetPreallocation(b->B,bs,o_nz,o_nnz);CHKERRQ(ierr); 3049526dfc15SBarry Smith B->preallocated = PETSC_TRUE; 3050a23d5eceSKris Buschelman PetscFunctionReturn(0); 3051a23d5eceSKris Buschelman } 3052a23d5eceSKris Buschelman EXTERN_C_END 3053a23d5eceSKris Buschelman 3054a23d5eceSKris Buschelman EXTERN_C_BEGIN 3055be1d678aSKris Buschelman EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalScaleLocal_MPIBAIJ(Mat,Vec); 3056be1d678aSKris Buschelman EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatSetHashTableFactor_MPIBAIJ(Mat,PetscReal); 305792b32695SKris Buschelman EXTERN_C_END 30585bf65638SKris Buschelman 305982094794SBarry Smith 306082094794SBarry Smith EXTERN_C_BEGIN 306182094794SBarry Smith #undef __FUNCT__ 306282094794SBarry Smith #define __FUNCT__ "MatConvert_MPIBAIJ_MPIAdj" 306382094794SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIBAIJ_MPIAdj(Mat B, const MatType newtype,MatReuse reuse,Mat *adj) 306482094794SBarry Smith { 306582094794SBarry Smith Mat_MPIBAIJ *b = (Mat_MPIBAIJ*)B->data; 306682094794SBarry Smith PetscErrorCode ierr; 306782094794SBarry Smith Mat_SeqBAIJ *d = (Mat_SeqBAIJ*) b->A->data,*o = (Mat_SeqBAIJ*) b->B->data; 306882094794SBarry Smith PetscInt M = B->rmap->n/B->rmap->bs,i,*ii,*jj,cnt,j,k,rstart = B->rmap->rstart/B->rmap->bs; 306982094794SBarry Smith const PetscInt *id = d->i, *jd = d->j, *io = o->i, *jo = o->j, *garray = b->garray; 307082094794SBarry Smith 307182094794SBarry Smith PetscFunctionBegin; 307282094794SBarry Smith ierr = PetscMalloc((M+1)*sizeof(PetscInt),&ii);CHKERRQ(ierr); 307382094794SBarry Smith ii[0] = 0; 307482094794SBarry Smith CHKMEMQ; 307582094794SBarry Smith for (i=0; i<M; i++) { 3076e32f2f54SBarry 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]); 3077e32f2f54SBarry 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]); 307882094794SBarry Smith ii[i+1] = ii[i] + id[i+1] - id[i] + io[i+1] - io[i]; 307982094794SBarry Smith /* remove one from count of matrix has diagonal */ 308082094794SBarry Smith for (j=id[i]; j<id[i+1]; j++) { 308182094794SBarry Smith if (jd[j] == i) {ii[i+1]--;break;} 308282094794SBarry Smith } 308382094794SBarry Smith CHKMEMQ; 308482094794SBarry Smith } 308582094794SBarry Smith ierr = PetscMalloc(ii[M]*sizeof(PetscInt),&jj);CHKERRQ(ierr); 308682094794SBarry Smith cnt = 0; 308782094794SBarry Smith for (i=0; i<M; i++) { 308882094794SBarry Smith for (j=io[i]; j<io[i+1]; j++) { 308982094794SBarry Smith if (garray[jo[j]] > rstart) break; 309082094794SBarry Smith jj[cnt++] = garray[jo[j]]; 309182094794SBarry Smith CHKMEMQ; 309282094794SBarry Smith } 309382094794SBarry Smith for (k=id[i]; k<id[i+1]; k++) { 309482094794SBarry Smith if (jd[k] != i) { 309582094794SBarry Smith jj[cnt++] = rstart + jd[k]; 309682094794SBarry Smith CHKMEMQ; 309782094794SBarry Smith } 309882094794SBarry Smith } 309982094794SBarry Smith for (;j<io[i+1]; j++) { 310082094794SBarry Smith jj[cnt++] = garray[jo[j]]; 310182094794SBarry Smith CHKMEMQ; 310282094794SBarry Smith } 310382094794SBarry Smith } 310482094794SBarry Smith ierr = MatCreateMPIAdj(((PetscObject)B)->comm,M,B->cmap->N/B->rmap->bs,ii,jj,PETSC_NULL,adj);CHKERRQ(ierr); 310582094794SBarry Smith PetscFunctionReturn(0); 310682094794SBarry Smith } 3107dbf0e21dSBarry Smith EXTERN_C_END 310882094794SBarry Smith 3109450b117fSShri Abhyankar EXTERN_C_BEGIN 3110450b117fSShri Abhyankar #if defined(PETSC_HAVE_MUMPS) 3111bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_baij_mumps(Mat,MatFactorType,Mat*); 3112450b117fSShri Abhyankar #endif 3113450b117fSShri Abhyankar EXTERN_C_END 3114450b117fSShri Abhyankar 31150bad9183SKris Buschelman /*MC 3116fafad747SKris Buschelman MATMPIBAIJ - MATMPIBAIJ = "mpibaij" - A matrix type to be used for distributed block sparse matrices. 31170bad9183SKris Buschelman 31180bad9183SKris Buschelman Options Database Keys: 31198c07d4e3SBarry Smith + -mat_type mpibaij - sets the matrix type to "mpibaij" during a call to MatSetFromOptions() 31208c07d4e3SBarry Smith . -mat_block_size <bs> - set the blocksize used to store the matrix 31218c07d4e3SBarry Smith - -mat_use_hash_table <fact> 31220bad9183SKris Buschelman 31230bad9183SKris Buschelman Level: beginner 31240bad9183SKris Buschelman 31250bad9183SKris Buschelman .seealso: MatCreateMPIBAIJ 31260bad9183SKris Buschelman M*/ 31270bad9183SKris Buschelman 312892b32695SKris Buschelman EXTERN_C_BEGIN 3129a23d5eceSKris Buschelman #undef __FUNCT__ 31304a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIBAIJ" 3131be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MPIBAIJ(Mat B) 3132273d9f13SBarry Smith { 3133273d9f13SBarry Smith Mat_MPIBAIJ *b; 3134dfbe8321SBarry Smith PetscErrorCode ierr; 3135ace3abfcSBarry Smith PetscBool flg; 3136273d9f13SBarry Smith 3137273d9f13SBarry Smith PetscFunctionBegin; 313838f2d2fdSLisandro Dalcin ierr = PetscNewLog(B,Mat_MPIBAIJ,&b);CHKERRQ(ierr); 313982502324SSatish Balay B->data = (void*)b; 314082502324SSatish Balay 3141085a36d4SBarry Smith 3142273d9f13SBarry Smith ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 3143273d9f13SBarry Smith B->mapping = 0; 3144273d9f13SBarry Smith B->assembled = PETSC_FALSE; 3145273d9f13SBarry Smith 3146273d9f13SBarry Smith B->insertmode = NOT_SET_VALUES; 31477adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)B)->comm,&b->rank);CHKERRQ(ierr); 31487adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)B)->comm,&b->size);CHKERRQ(ierr); 3149273d9f13SBarry Smith 3150273d9f13SBarry Smith /* build local table of row and column ownerships */ 3151899cda47SBarry Smith ierr = PetscMalloc((b->size+1)*sizeof(PetscInt),&b->rangebs);CHKERRQ(ierr); 3152273d9f13SBarry Smith 3153273d9f13SBarry Smith /* build cache for off array entries formed */ 31547adad957SLisandro Dalcin ierr = MatStashCreate_Private(((PetscObject)B)->comm,1,&B->stash);CHKERRQ(ierr); 3155273d9f13SBarry Smith b->donotstash = PETSC_FALSE; 3156273d9f13SBarry Smith b->colmap = PETSC_NULL; 3157273d9f13SBarry Smith b->garray = PETSC_NULL; 3158273d9f13SBarry Smith b->roworiented = PETSC_TRUE; 3159273d9f13SBarry Smith 3160273d9f13SBarry Smith /* stuff used in block assembly */ 3161273d9f13SBarry Smith b->barray = 0; 3162273d9f13SBarry Smith 3163273d9f13SBarry Smith /* stuff used for matrix vector multiply */ 3164273d9f13SBarry Smith b->lvec = 0; 3165273d9f13SBarry Smith b->Mvctx = 0; 3166273d9f13SBarry Smith 3167273d9f13SBarry Smith /* stuff for MatGetRow() */ 3168273d9f13SBarry Smith b->rowindices = 0; 3169273d9f13SBarry Smith b->rowvalues = 0; 3170273d9f13SBarry Smith b->getrowactive = PETSC_FALSE; 3171273d9f13SBarry Smith 3172273d9f13SBarry Smith /* hash table stuff */ 3173273d9f13SBarry Smith b->ht = 0; 3174273d9f13SBarry Smith b->hd = 0; 3175273d9f13SBarry Smith b->ht_size = 0; 3176273d9f13SBarry Smith b->ht_flag = PETSC_FALSE; 3177273d9f13SBarry Smith b->ht_fact = 0; 3178273d9f13SBarry Smith b->ht_total_ct = 0; 3179273d9f13SBarry Smith b->ht_insert_ct = 0; 3180273d9f13SBarry Smith 31817adad957SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)B)->comm,PETSC_NULL,"Options for loading MPIBAIJ matrix 1","Mat");CHKERRQ(ierr); 3182acfcf0e5SJed Brown ierr = PetscOptionsBool("-mat_use_hash_table","Use hash table to save memory in constructing matrix","MatSetOption",PETSC_FALSE,&flg,PETSC_NULL);CHKERRQ(ierr); 3183273d9f13SBarry Smith if (flg) { 3184f6275e2eSBarry Smith PetscReal fact = 1.39; 31854e0d8c25SBarry Smith ierr = MatSetOption(B,MAT_USE_HASH_TABLE,PETSC_TRUE);CHKERRQ(ierr); 31868c07d4e3SBarry Smith ierr = PetscOptionsReal("-mat_use_hash_table","Use hash table factor","MatMPIBAIJSetHashTableFactor",fact,&fact,PETSC_NULL);CHKERRQ(ierr); 3187273d9f13SBarry Smith if (fact <= 1.0) fact = 1.39; 3188273d9f13SBarry Smith ierr = MatMPIBAIJSetHashTableFactor(B,fact);CHKERRQ(ierr); 31891e2582c4SBarry Smith ierr = PetscInfo1(B,"Hash table Factor used %5.2f\n",fact);CHKERRQ(ierr); 3190273d9f13SBarry Smith } 31918c07d4e3SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 31928c07d4e3SBarry Smith 3193450b117fSShri Abhyankar #if defined(PETSC_HAVE_MUMPS) 3194bccb9932SShri Abhyankar ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", "MatGetFactor_baij_mumps",MatGetFactor_baij_mumps);CHKERRQ(ierr); 3195450b117fSShri Abhyankar #endif 319682094794SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpibaij_mpiadj_C", 319782094794SBarry Smith "MatConvert_MPIBAIJ_MPIAdj", 319882094794SBarry Smith MatConvert_MPIBAIJ_MPIAdj);CHKERRQ(ierr); 3199273d9f13SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 3200273d9f13SBarry Smith "MatStoreValues_MPIBAIJ", 3201273d9f13SBarry Smith MatStoreValues_MPIBAIJ);CHKERRQ(ierr); 3202273d9f13SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 3203273d9f13SBarry Smith "MatRetrieveValues_MPIBAIJ", 3204273d9f13SBarry Smith MatRetrieveValues_MPIBAIJ);CHKERRQ(ierr); 3205273d9f13SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C", 3206273d9f13SBarry Smith "MatGetDiagonalBlock_MPIBAIJ", 3207273d9f13SBarry Smith MatGetDiagonalBlock_MPIBAIJ);CHKERRQ(ierr); 3208a23d5eceSKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIBAIJSetPreallocation_C", 3209a23d5eceSKris Buschelman "MatMPIBAIJSetPreallocation_MPIBAIJ", 3210a23d5eceSKris Buschelman MatMPIBAIJSetPreallocation_MPIBAIJ);CHKERRQ(ierr); 3211aac34f13SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIBAIJSetPreallocationCSR_C", 321244ec7894SLisandro Dalcin "MatMPIBAIJSetPreallocationCSR_MPIBAIJ", 3213aac34f13SBarry Smith MatMPIBAIJSetPreallocationCSR_MPIBAIJ);CHKERRQ(ierr); 321492b32695SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C", 321592b32695SKris Buschelman "MatDiagonalScaleLocal_MPIBAIJ", 321692b32695SKris Buschelman MatDiagonalScaleLocal_MPIBAIJ);CHKERRQ(ierr); 32175bf65638SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSetHashTableFactor_C", 32185bf65638SKris Buschelman "MatSetHashTableFactor_MPIBAIJ", 32195bf65638SKris Buschelman MatSetHashTableFactor_MPIBAIJ);CHKERRQ(ierr); 322017667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIBAIJ);CHKERRQ(ierr); 3221273d9f13SBarry Smith PetscFunctionReturn(0); 3222273d9f13SBarry Smith } 3223273d9f13SBarry Smith EXTERN_C_END 3224273d9f13SBarry Smith 3225209238afSKris Buschelman /*MC 3226002d173eSKris Buschelman MATBAIJ - MATBAIJ = "baij" - A matrix type to be used for block sparse matrices. 3227209238afSKris Buschelman 3228209238afSKris Buschelman This matrix type is identical to MATSEQBAIJ when constructed with a single process communicator, 3229209238afSKris Buschelman and MATMPIBAIJ otherwise. 3230209238afSKris Buschelman 3231209238afSKris Buschelman Options Database Keys: 3232209238afSKris Buschelman . -mat_type baij - sets the matrix type to "baij" during a call to MatSetFromOptions() 3233209238afSKris Buschelman 3234209238afSKris Buschelman Level: beginner 3235209238afSKris Buschelman 3236aac34f13SBarry Smith .seealso: MatCreateMPIBAIJ(),MATSEQBAIJ,MATMPIBAIJ, MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR() 3237209238afSKris Buschelman M*/ 3238209238afSKris Buschelman 3239209238afSKris Buschelman EXTERN_C_BEGIN 3240209238afSKris Buschelman #undef __FUNCT__ 3241209238afSKris Buschelman #define __FUNCT__ "MatCreate_BAIJ" 3242be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_BAIJ(Mat A) 3243dfbe8321SBarry Smith { 32446849ba73SBarry Smith PetscErrorCode ierr; 3245b24ad042SBarry Smith PetscMPIInt size; 3246209238afSKris Buschelman 3247209238afSKris Buschelman PetscFunctionBegin; 32487adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)A)->comm,&size);CHKERRQ(ierr); 3249209238afSKris Buschelman if (size == 1) { 3250209238afSKris Buschelman ierr = MatSetType(A,MATSEQBAIJ);CHKERRQ(ierr); 3251209238afSKris Buschelman } else { 3252209238afSKris Buschelman ierr = MatSetType(A,MATMPIBAIJ);CHKERRQ(ierr); 3253209238afSKris Buschelman } 3254209238afSKris Buschelman PetscFunctionReturn(0); 3255209238afSKris Buschelman } 3256209238afSKris Buschelman EXTERN_C_END 3257209238afSKris Buschelman 32584a2ae208SSatish Balay #undef __FUNCT__ 32594a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJSetPreallocation" 3260273d9f13SBarry Smith /*@C 3261aac34f13SBarry Smith MatMPIBAIJSetPreallocation - Allocates memory for a sparse parallel matrix in block AIJ format 3262273d9f13SBarry Smith (block compressed row). For good matrix assembly performance 3263273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3264273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3265273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3266273d9f13SBarry Smith 3267273d9f13SBarry Smith Collective on Mat 3268273d9f13SBarry Smith 3269273d9f13SBarry Smith Input Parameters: 3270273d9f13SBarry Smith + A - the matrix 3271273d9f13SBarry Smith . bs - size of blockk 3272273d9f13SBarry Smith . d_nz - number of block nonzeros per block row in diagonal portion of local 3273273d9f13SBarry Smith submatrix (same for all local rows) 3274273d9f13SBarry Smith . d_nnz - array containing the number of block nonzeros in the various block rows 3275273d9f13SBarry Smith of the in diagonal portion of the local (possibly different for each block 3276273d9f13SBarry Smith row) or PETSC_NULL. You must leave room for the diagonal entry even if it is zero. 3277273d9f13SBarry Smith . o_nz - number of block nonzeros per block row in the off-diagonal portion of local 3278273d9f13SBarry Smith submatrix (same for all local rows). 3279273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various block rows of the 3280273d9f13SBarry Smith off-diagonal portion of the local submatrix (possibly different for 3281273d9f13SBarry Smith each block row) or PETSC_NULL. 3282273d9f13SBarry Smith 328349a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 3284273d9f13SBarry Smith 3285273d9f13SBarry Smith Options Database Keys: 32868c07d4e3SBarry Smith + -mat_block_size - size of the blocks to use 32878c07d4e3SBarry Smith - -mat_use_hash_table <fact> 3288273d9f13SBarry Smith 3289273d9f13SBarry Smith Notes: 3290273d9f13SBarry Smith If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one processor 3291273d9f13SBarry Smith than it must be used on all processors that share the object for that argument. 3292273d9f13SBarry Smith 3293273d9f13SBarry Smith Storage Information: 3294273d9f13SBarry Smith For a square global matrix we define each processor's diagonal portion 3295273d9f13SBarry Smith to be its local rows and the corresponding columns (a square submatrix); 3296273d9f13SBarry Smith each processor's off-diagonal portion encompasses the remainder of the 3297273d9f13SBarry Smith local matrix (a rectangular submatrix). 3298273d9f13SBarry Smith 3299273d9f13SBarry Smith The user can specify preallocated storage for the diagonal part of 3300273d9f13SBarry Smith the local submatrix with either d_nz or d_nnz (not both). Set 3301273d9f13SBarry Smith d_nz=PETSC_DEFAULT and d_nnz=PETSC_NULL for PETSc to control dynamic 3302273d9f13SBarry Smith memory allocation. Likewise, specify preallocated storage for the 3303273d9f13SBarry Smith off-diagonal part of the local submatrix with o_nz or o_nnz (not both). 3304273d9f13SBarry Smith 3305273d9f13SBarry Smith Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In 3306273d9f13SBarry Smith the figure below we depict these three local rows and all columns (0-11). 3307273d9f13SBarry Smith 3308273d9f13SBarry Smith .vb 3309273d9f13SBarry Smith 0 1 2 3 4 5 6 7 8 9 10 11 3310273d9f13SBarry Smith ------------------- 3311273d9f13SBarry Smith row 3 | o o o d d d o o o o o o 3312273d9f13SBarry Smith row 4 | o o o d d d o o o o o o 3313273d9f13SBarry Smith row 5 | o o o d d d o o o o o o 3314273d9f13SBarry Smith ------------------- 3315273d9f13SBarry Smith .ve 3316273d9f13SBarry Smith 3317273d9f13SBarry Smith Thus, any entries in the d locations are stored in the d (diagonal) 3318273d9f13SBarry Smith submatrix, and any entries in the o locations are stored in the 3319273d9f13SBarry Smith o (off-diagonal) submatrix. Note that the d and the o submatrices are 3320273d9f13SBarry Smith stored simply in the MATSEQBAIJ format for compressed row storage. 3321273d9f13SBarry Smith 3322273d9f13SBarry Smith Now d_nz should indicate the number of block nonzeros per row in the d matrix, 3323273d9f13SBarry Smith and o_nz should indicate the number of block nonzeros per row in the o matrix. 3324273d9f13SBarry Smith In general, for PDE problems in which most nonzeros are near the diagonal, 3325273d9f13SBarry Smith one expects d_nz >> o_nz. For large problems you MUST preallocate memory 3326273d9f13SBarry Smith or you will get TERRIBLE performance; see the users' manual chapter on 3327273d9f13SBarry Smith matrices. 3328273d9f13SBarry Smith 3329aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 3330aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 3331aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 3332aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 3333aa95bbe8SBarry Smith 3334273d9f13SBarry Smith Level: intermediate 3335273d9f13SBarry Smith 3336273d9f13SBarry Smith .keywords: matrix, block, aij, compressed row, sparse, parallel 3337273d9f13SBarry Smith 3338aac34f13SBarry Smith .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateMPIBAIJ(), MatMPIBAIJSetPreallocationCSR() 3339273d9f13SBarry Smith @*/ 3340be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 3341273d9f13SBarry Smith { 33424ac538c5SBarry Smith PetscErrorCode ierr; 3343273d9f13SBarry Smith 3344273d9f13SBarry Smith PetscFunctionBegin; 33454ac538c5SBarry 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); 3346273d9f13SBarry Smith PetscFunctionReturn(0); 3347273d9f13SBarry Smith } 3348273d9f13SBarry Smith 33494a2ae208SSatish Balay #undef __FUNCT__ 33504a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIBAIJ" 335179bdfe76SSatish Balay /*@C 335279bdfe76SSatish Balay MatCreateMPIBAIJ - Creates a sparse parallel matrix in block AIJ format 335379bdfe76SSatish Balay (block compressed row). For good matrix assembly performance 335479bdfe76SSatish Balay the user should preallocate the matrix storage by setting the parameters 335579bdfe76SSatish Balay d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 335679bdfe76SSatish Balay performance can be increased by more than a factor of 50. 335779bdfe76SSatish Balay 3358db81eaa0SLois Curfman McInnes Collective on MPI_Comm 3359db81eaa0SLois Curfman McInnes 336079bdfe76SSatish Balay Input Parameters: 3361db81eaa0SLois Curfman McInnes + comm - MPI communicator 336279bdfe76SSatish Balay . bs - size of blockk 336379bdfe76SSatish Balay . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 336492e8d321SLois Curfman McInnes This value should be the same as the local size used in creating the 336592e8d321SLois Curfman McInnes y vector for the matrix-vector product y = Ax. 336692e8d321SLois Curfman McInnes . n - number of local columns (or PETSC_DECIDE to have calculated if N is given) 336792e8d321SLois Curfman McInnes This value should be the same as the local size used in creating the 336892e8d321SLois Curfman McInnes x vector for the matrix-vector product y = Ax. 3369be79a94dSBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 3370be79a94dSBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 337147a75d0bSBarry Smith . d_nz - number of nonzero blocks per block row in diagonal portion of local 337279bdfe76SSatish Balay submatrix (same for all local rows) 337347a75d0bSBarry Smith . d_nnz - array containing the number of nonzero blocks in the various block rows 337492e8d321SLois Curfman McInnes of the in diagonal portion of the local (possibly different for each block 3375db81eaa0SLois Curfman McInnes row) or PETSC_NULL. You must leave room for the diagonal entry even if it is zero. 337647a75d0bSBarry Smith . o_nz - number of nonzero blocks per block row in the off-diagonal portion of local 337779bdfe76SSatish Balay submatrix (same for all local rows). 337847a75d0bSBarry Smith - o_nnz - array containing the number of nonzero blocks in the various block rows of the 337992e8d321SLois Curfman McInnes off-diagonal portion of the local submatrix (possibly different for 338092e8d321SLois Curfman McInnes each block row) or PETSC_NULL. 338179bdfe76SSatish Balay 338279bdfe76SSatish Balay Output Parameter: 338379bdfe76SSatish Balay . A - the matrix 338479bdfe76SSatish Balay 3385db81eaa0SLois Curfman McInnes Options Database Keys: 33868c07d4e3SBarry Smith + -mat_block_size - size of the blocks to use 33878c07d4e3SBarry Smith - -mat_use_hash_table <fact> 33883ffaccefSLois Curfman McInnes 3389175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 3390ae1d86c5SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. 3391175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 3392175b88e8SBarry Smith 3393b259b22eSLois Curfman McInnes Notes: 339449a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 339549a6f317SBarry Smith 339647a75d0bSBarry Smith A nonzero block is any block that as 1 or more nonzeros in it 339747a75d0bSBarry Smith 339879bdfe76SSatish Balay The user MUST specify either the local or global matrix dimensions 339979bdfe76SSatish Balay (possibly both). 340079bdfe76SSatish Balay 3401be79a94dSBarry Smith If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one processor 3402be79a94dSBarry Smith than it must be used on all processors that share the object for that argument. 3403be79a94dSBarry Smith 340479bdfe76SSatish Balay Storage Information: 340579bdfe76SSatish Balay For a square global matrix we define each processor's diagonal portion 340679bdfe76SSatish Balay to be its local rows and the corresponding columns (a square submatrix); 340779bdfe76SSatish Balay each processor's off-diagonal portion encompasses the remainder of the 340879bdfe76SSatish Balay local matrix (a rectangular submatrix). 340979bdfe76SSatish Balay 341079bdfe76SSatish Balay The user can specify preallocated storage for the diagonal part of 341179bdfe76SSatish Balay the local submatrix with either d_nz or d_nnz (not both). Set 341279bdfe76SSatish Balay d_nz=PETSC_DEFAULT and d_nnz=PETSC_NULL for PETSc to control dynamic 341379bdfe76SSatish Balay memory allocation. Likewise, specify preallocated storage for the 341479bdfe76SSatish Balay off-diagonal part of the local submatrix with o_nz or o_nnz (not both). 341579bdfe76SSatish Balay 341679bdfe76SSatish Balay Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In 341779bdfe76SSatish Balay the figure below we depict these three local rows and all columns (0-11). 341879bdfe76SSatish Balay 3419db81eaa0SLois Curfman McInnes .vb 3420db81eaa0SLois Curfman McInnes 0 1 2 3 4 5 6 7 8 9 10 11 3421db81eaa0SLois Curfman McInnes ------------------- 3422db81eaa0SLois Curfman McInnes row 3 | o o o d d d o o o o o o 3423db81eaa0SLois Curfman McInnes row 4 | o o o d d d o o o o o o 3424db81eaa0SLois Curfman McInnes row 5 | o o o d d d o o o o o o 3425db81eaa0SLois Curfman McInnes ------------------- 3426db81eaa0SLois Curfman McInnes .ve 342779bdfe76SSatish Balay 342879bdfe76SSatish Balay Thus, any entries in the d locations are stored in the d (diagonal) 342979bdfe76SSatish Balay submatrix, and any entries in the o locations are stored in the 343079bdfe76SSatish Balay o (off-diagonal) submatrix. Note that the d and the o submatrices are 343157b952d6SSatish Balay stored simply in the MATSEQBAIJ format for compressed row storage. 343279bdfe76SSatish Balay 3433d64ed03dSBarry Smith Now d_nz should indicate the number of block nonzeros per row in the d matrix, 3434d64ed03dSBarry Smith and o_nz should indicate the number of block nonzeros per row in the o matrix. 343579bdfe76SSatish Balay In general, for PDE problems in which most nonzeros are near the diagonal, 343692e8d321SLois Curfman McInnes one expects d_nz >> o_nz. For large problems you MUST preallocate memory 343792e8d321SLois Curfman McInnes or you will get TERRIBLE performance; see the users' manual chapter on 34386da5968aSLois Curfman McInnes matrices. 343979bdfe76SSatish Balay 3440027ccd11SLois Curfman McInnes Level: intermediate 3441027ccd11SLois Curfman McInnes 344292e8d321SLois Curfman McInnes .keywords: matrix, block, aij, compressed row, sparse, parallel 344379bdfe76SSatish Balay 3444aac34f13SBarry Smith .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateMPIBAIJ(), MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR() 344579bdfe76SSatish Balay @*/ 3446be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMPIBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A) 344779bdfe76SSatish Balay { 34486849ba73SBarry Smith PetscErrorCode ierr; 3449b24ad042SBarry Smith PetscMPIInt size; 345079bdfe76SSatish Balay 3451d64ed03dSBarry Smith PetscFunctionBegin; 3452f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 3453f69a0ea3SMatthew Knepley ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr); 3454d132466eSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 3455273d9f13SBarry Smith if (size > 1) { 3456273d9f13SBarry Smith ierr = MatSetType(*A,MATMPIBAIJ);CHKERRQ(ierr); 3457273d9f13SBarry Smith ierr = MatMPIBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr); 3458273d9f13SBarry Smith } else { 3459273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQBAIJ);CHKERRQ(ierr); 3460273d9f13SBarry Smith ierr = MatSeqBAIJSetPreallocation(*A,bs,d_nz,d_nnz);CHKERRQ(ierr); 34613914022bSBarry Smith } 34623a40ed3dSBarry Smith PetscFunctionReturn(0); 346379bdfe76SSatish Balay } 3464026e39d0SSatish Balay 34654a2ae208SSatish Balay #undef __FUNCT__ 34664a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIBAIJ" 34676849ba73SBarry Smith static PetscErrorCode MatDuplicate_MPIBAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat) 34680ac07820SSatish Balay { 34690ac07820SSatish Balay Mat mat; 34700ac07820SSatish Balay Mat_MPIBAIJ *a,*oldmat = (Mat_MPIBAIJ*)matin->data; 3471dfbe8321SBarry Smith PetscErrorCode ierr; 3472b24ad042SBarry Smith PetscInt len=0; 34730ac07820SSatish Balay 3474d64ed03dSBarry Smith PetscFunctionBegin; 34750ac07820SSatish Balay *newmat = 0; 34767adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)matin)->comm,&mat);CHKERRQ(ierr); 3477d0f46423SBarry Smith ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr); 34787adad957SLisandro Dalcin ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr); 34791d5dac46SHong Zhang ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr); 34807fff6886SHong Zhang 3481d5f3da31SBarry Smith mat->factortype = matin->factortype; 3482273d9f13SBarry Smith mat->preallocated = PETSC_TRUE; 34830ac07820SSatish Balay mat->assembled = PETSC_TRUE; 34847fff6886SHong Zhang mat->insertmode = NOT_SET_VALUES; 34857fff6886SHong Zhang 3486273d9f13SBarry Smith a = (Mat_MPIBAIJ*)mat->data; 3487d0f46423SBarry Smith mat->rmap->bs = matin->rmap->bs; 34880ac07820SSatish Balay a->bs2 = oldmat->bs2; 34890ac07820SSatish Balay a->mbs = oldmat->mbs; 34900ac07820SSatish Balay a->nbs = oldmat->nbs; 34910ac07820SSatish Balay a->Mbs = oldmat->Mbs; 34920ac07820SSatish Balay a->Nbs = oldmat->Nbs; 34930ac07820SSatish Balay 349426283091SBarry Smith ierr = PetscLayoutCopy(matin->rmap,&mat->rmap);CHKERRQ(ierr); 349526283091SBarry Smith ierr = PetscLayoutCopy(matin->cmap,&mat->cmap);CHKERRQ(ierr); 3496899cda47SBarry Smith 34970ac07820SSatish Balay a->size = oldmat->size; 34980ac07820SSatish Balay a->rank = oldmat->rank; 3499aef5e8e0SSatish Balay a->donotstash = oldmat->donotstash; 3500aef5e8e0SSatish Balay a->roworiented = oldmat->roworiented; 3501aef5e8e0SSatish Balay a->rowindices = 0; 35020ac07820SSatish Balay a->rowvalues = 0; 35030ac07820SSatish Balay a->getrowactive = PETSC_FALSE; 350430793edcSSatish Balay a->barray = 0; 3505899cda47SBarry Smith a->rstartbs = oldmat->rstartbs; 3506899cda47SBarry Smith a->rendbs = oldmat->rendbs; 3507899cda47SBarry Smith a->cstartbs = oldmat->cstartbs; 3508899cda47SBarry Smith a->cendbs = oldmat->cendbs; 35090ac07820SSatish Balay 3510133cdb44SSatish Balay /* hash table stuff */ 3511133cdb44SSatish Balay a->ht = 0; 3512133cdb44SSatish Balay a->hd = 0; 3513133cdb44SSatish Balay a->ht_size = 0; 3514133cdb44SSatish Balay a->ht_flag = oldmat->ht_flag; 351525fdafccSSatish Balay a->ht_fact = oldmat->ht_fact; 3516133cdb44SSatish Balay a->ht_total_ct = 0; 3517133cdb44SSatish Balay a->ht_insert_ct = 0; 3518133cdb44SSatish Balay 3519899cda47SBarry Smith ierr = PetscMemcpy(a->rangebs,oldmat->rangebs,(a->size+1)*sizeof(PetscInt));CHKERRQ(ierr); 35200ac07820SSatish Balay if (oldmat->colmap) { 3521aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 35220f5bd95cSBarry Smith ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr); 352348e59246SSatish Balay #else 3524b24ad042SBarry Smith ierr = PetscMalloc((a->Nbs)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr); 352552e6d16bSBarry Smith ierr = PetscLogObjectMemory(mat,(a->Nbs)*sizeof(PetscInt));CHKERRQ(ierr); 3526b24ad042SBarry Smith ierr = PetscMemcpy(a->colmap,oldmat->colmap,(a->Nbs)*sizeof(PetscInt));CHKERRQ(ierr); 352748e59246SSatish Balay #endif 35280ac07820SSatish Balay } else a->colmap = 0; 35294beb1cfeSHong Zhang 35300ac07820SSatish Balay if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) { 3531b24ad042SBarry Smith ierr = PetscMalloc(len*sizeof(PetscInt),&a->garray);CHKERRQ(ierr); 353252e6d16bSBarry Smith ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr); 3533b24ad042SBarry Smith ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); 35340ac07820SSatish Balay } else a->garray = 0; 35350ac07820SSatish Balay 3536533163c2SBarry Smith ierr = MatStashCreate_Private(((PetscObject)matin)->comm,matin->rmap->bs,&mat->bstash);CHKERRQ(ierr); 35370ac07820SSatish Balay ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr); 353852e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr); 35390ac07820SSatish Balay ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr); 354052e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr); 35417fff6886SHong Zhang 35422e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr); 354352e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr); 35442e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr); 354552e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr); 35467adad957SLisandro Dalcin ierr = PetscFListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr); 35470ac07820SSatish Balay *newmat = mat; 35484beb1cfeSHong Zhang 35493a40ed3dSBarry Smith PetscFunctionReturn(0); 35500ac07820SSatish Balay } 355157b952d6SSatish Balay 35524a2ae208SSatish Balay #undef __FUNCT__ 35535bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIBAIJ" 3554112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIBAIJ(Mat newmat,PetscViewer viewer) 35554683f7a4SShri Abhyankar { 35564683f7a4SShri Abhyankar PetscErrorCode ierr; 35574683f7a4SShri Abhyankar int fd; 35584683f7a4SShri Abhyankar PetscInt i,nz,j,rstart,rend; 35594683f7a4SShri Abhyankar PetscScalar *vals,*buf; 35604683f7a4SShri Abhyankar MPI_Comm comm = ((PetscObject)viewer)->comm; 35614683f7a4SShri Abhyankar MPI_Status status; 35624683f7a4SShri Abhyankar PetscMPIInt rank,size,maxnz; 35634683f7a4SShri Abhyankar PetscInt header[4],*rowlengths = 0,M,N,m,*rowners,*cols; 35644683f7a4SShri Abhyankar PetscInt *locrowlens = PETSC_NULL,*procsnz = PETSC_NULL,*browners = PETSC_NULL; 35654683f7a4SShri Abhyankar PetscInt jj,*mycols,*ibuf,bs=1,Mbs,mbs,extra_rows,mmax; 35664683f7a4SShri Abhyankar PetscMPIInt tag = ((PetscObject)viewer)->tag; 35674683f7a4SShri Abhyankar PetscInt *dlens = PETSC_NULL,*odlens = PETSC_NULL,*mask = PETSC_NULL,*masked1 = PETSC_NULL,*masked2 = PETSC_NULL,rowcount,odcount; 35684683f7a4SShri Abhyankar PetscInt dcount,kmax,k,nzcount,tmp,mend,sizesset=1,grows,gcols; 35694683f7a4SShri Abhyankar 35704683f7a4SShri Abhyankar PetscFunctionBegin; 35714683f7a4SShri Abhyankar ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading MPIBAIJ matrix 2","Mat");CHKERRQ(ierr); 35724683f7a4SShri Abhyankar ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr); 35734683f7a4SShri Abhyankar ierr = PetscOptionsEnd();CHKERRQ(ierr); 35744683f7a4SShri Abhyankar 35754683f7a4SShri Abhyankar ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 35764683f7a4SShri Abhyankar ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 35774683f7a4SShri Abhyankar if (!rank) { 35784683f7a4SShri Abhyankar ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 35794683f7a4SShri Abhyankar ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr); 35804683f7a4SShri Abhyankar if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object"); 35814683f7a4SShri Abhyankar } 35824683f7a4SShri Abhyankar 35834683f7a4SShri Abhyankar if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) sizesset = 0; 35844683f7a4SShri Abhyankar 35854683f7a4SShri Abhyankar ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr); 35864683f7a4SShri Abhyankar M = header[1]; N = header[2]; 35874683f7a4SShri Abhyankar 35884683f7a4SShri Abhyankar /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */ 35894683f7a4SShri Abhyankar if (sizesset && newmat->rmap->N < 0) newmat->rmap->N = M; 35904683f7a4SShri Abhyankar if (sizesset && newmat->cmap->N < 0) newmat->cmap->N = N; 35914683f7a4SShri Abhyankar 35924683f7a4SShri Abhyankar /* If global sizes are set, check if they are consistent with that given in the file */ 35934683f7a4SShri Abhyankar if (sizesset) { 35944683f7a4SShri Abhyankar ierr = MatGetSize(newmat,&grows,&gcols);CHKERRQ(ierr); 35954683f7a4SShri Abhyankar } 3596abd38a8fSBarry 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); 3597abd38a8fSBarry 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); 35984683f7a4SShri Abhyankar 35994683f7a4SShri Abhyankar if (M != N) SETERRQ(((PetscObject)viewer)->comm,PETSC_ERR_SUP,"Can only do square matrices"); 36004683f7a4SShri Abhyankar 36014683f7a4SShri Abhyankar /* 36024683f7a4SShri Abhyankar This code adds extra rows to make sure the number of rows is 36034683f7a4SShri Abhyankar divisible by the blocksize 36044683f7a4SShri Abhyankar */ 36054683f7a4SShri Abhyankar Mbs = M/bs; 36064683f7a4SShri Abhyankar extra_rows = bs - M + bs*Mbs; 36074683f7a4SShri Abhyankar if (extra_rows == bs) extra_rows = 0; 36084683f7a4SShri Abhyankar else Mbs++; 36094683f7a4SShri Abhyankar if (extra_rows && !rank) { 36104683f7a4SShri Abhyankar ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr); 36114683f7a4SShri Abhyankar } 36124683f7a4SShri Abhyankar 36134683f7a4SShri Abhyankar /* determine ownership of all rows */ 36144683f7a4SShri Abhyankar if (newmat->rmap->n < 0) { /* PETSC_DECIDE */ 36154683f7a4SShri Abhyankar mbs = Mbs/size + ((Mbs % size) > rank); 36164683f7a4SShri Abhyankar m = mbs*bs; 36174683f7a4SShri Abhyankar } else { /* User set */ 36184683f7a4SShri Abhyankar m = newmat->rmap->n; 36194683f7a4SShri Abhyankar mbs = m/bs; 36204683f7a4SShri Abhyankar } 36214683f7a4SShri Abhyankar ierr = PetscMalloc2(size+1,PetscInt,&rowners,size+1,PetscInt,&browners);CHKERRQ(ierr); 36224683f7a4SShri Abhyankar ierr = MPI_Allgather(&mbs,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 36234683f7a4SShri Abhyankar 36244683f7a4SShri Abhyankar /* process 0 needs enough room for process with most rows */ 36254683f7a4SShri Abhyankar if (!rank) { 36264683f7a4SShri Abhyankar mmax = rowners[1]; 36274683f7a4SShri Abhyankar for (i=2; i<size; i++) { 36284683f7a4SShri Abhyankar mmax = PetscMax(mmax,rowners[i]); 36294683f7a4SShri Abhyankar } 36304683f7a4SShri Abhyankar mmax*=bs; 36314683f7a4SShri Abhyankar } else mmax = m; 36324683f7a4SShri Abhyankar 36334683f7a4SShri Abhyankar rowners[0] = 0; 36344683f7a4SShri Abhyankar for (i=2; i<=size; i++) rowners[i] += rowners[i-1]; 36354683f7a4SShri Abhyankar for (i=0; i<=size; i++) browners[i] = rowners[i]*bs; 36364683f7a4SShri Abhyankar rstart = rowners[rank]; 36374683f7a4SShri Abhyankar rend = rowners[rank+1]; 36384683f7a4SShri Abhyankar 36394683f7a4SShri Abhyankar /* distribute row lengths to all processors */ 36404683f7a4SShri Abhyankar ierr = PetscMalloc((mmax+1)*sizeof(PetscInt),&locrowlens);CHKERRQ(ierr); 36414683f7a4SShri Abhyankar if (!rank) { 36424683f7a4SShri Abhyankar mend = m; 36434683f7a4SShri Abhyankar if (size == 1) mend = mend - extra_rows; 36444683f7a4SShri Abhyankar ierr = PetscBinaryRead(fd,locrowlens,mend,PETSC_INT);CHKERRQ(ierr); 36454683f7a4SShri Abhyankar for (j=mend; j<m; j++) locrowlens[j] = 1; 36464683f7a4SShri Abhyankar ierr = PetscMalloc(m*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 36474683f7a4SShri Abhyankar ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr); 36484683f7a4SShri Abhyankar ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr); 36494683f7a4SShri Abhyankar for (j=0; j<m; j++) { 36504683f7a4SShri Abhyankar procsnz[0] += locrowlens[j]; 36514683f7a4SShri Abhyankar } 36524683f7a4SShri Abhyankar for (i=1; i<size; i++) { 36534683f7a4SShri Abhyankar mend = browners[i+1] - browners[i]; 36544683f7a4SShri Abhyankar if (i == size-1) mend = mend - extra_rows; 36554683f7a4SShri Abhyankar ierr = PetscBinaryRead(fd,rowlengths,mend,PETSC_INT);CHKERRQ(ierr); 36564683f7a4SShri Abhyankar for (j=mend; j<browners[i+1] - browners[i]; j++) rowlengths[j] = 1; 36574683f7a4SShri Abhyankar /* calculate the number of nonzeros on each processor */ 36584683f7a4SShri Abhyankar for (j=0; j<browners[i+1]-browners[i]; j++) { 36594683f7a4SShri Abhyankar procsnz[i] += rowlengths[j]; 36604683f7a4SShri Abhyankar } 36614683f7a4SShri Abhyankar ierr = MPI_Send(rowlengths,browners[i+1]-browners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr); 36624683f7a4SShri Abhyankar } 36634683f7a4SShri Abhyankar ierr = PetscFree(rowlengths);CHKERRQ(ierr); 36644683f7a4SShri Abhyankar } else { 36654683f7a4SShri Abhyankar ierr = MPI_Recv(locrowlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 36664683f7a4SShri Abhyankar } 36674683f7a4SShri Abhyankar 36684683f7a4SShri Abhyankar if (!rank) { 36694683f7a4SShri Abhyankar /* determine max buffer needed and allocate it */ 36704683f7a4SShri Abhyankar maxnz = procsnz[0]; 36714683f7a4SShri Abhyankar for (i=1; i<size; i++) { 36724683f7a4SShri Abhyankar maxnz = PetscMax(maxnz,procsnz[i]); 36734683f7a4SShri Abhyankar } 36744683f7a4SShri Abhyankar ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr); 36754683f7a4SShri Abhyankar 36764683f7a4SShri Abhyankar /* read in my part of the matrix column indices */ 36774683f7a4SShri Abhyankar nz = procsnz[0]; 36784683f7a4SShri Abhyankar ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&ibuf);CHKERRQ(ierr); 36794683f7a4SShri Abhyankar mycols = ibuf; 36804683f7a4SShri Abhyankar if (size == 1) nz -= extra_rows; 36814683f7a4SShri Abhyankar ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr); 36824683f7a4SShri Abhyankar if (size == 1) for (i=0; i< extra_rows; i++) { mycols[nz+i] = M+i; } 36834683f7a4SShri Abhyankar 36844683f7a4SShri Abhyankar /* read in every ones (except the last) and ship off */ 36854683f7a4SShri Abhyankar for (i=1; i<size-1; i++) { 36864683f7a4SShri Abhyankar nz = procsnz[i]; 36874683f7a4SShri Abhyankar ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr); 36884683f7a4SShri Abhyankar ierr = MPI_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 36894683f7a4SShri Abhyankar } 36904683f7a4SShri Abhyankar /* read in the stuff for the last proc */ 36914683f7a4SShri Abhyankar if (size != 1) { 36924683f7a4SShri Abhyankar nz = procsnz[size-1] - extra_rows; /* the extra rows are not on the disk */ 36934683f7a4SShri Abhyankar ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr); 36944683f7a4SShri Abhyankar for (i=0; i<extra_rows; i++) cols[nz+i] = M+i; 36954683f7a4SShri Abhyankar ierr = MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);CHKERRQ(ierr); 36964683f7a4SShri Abhyankar } 36974683f7a4SShri Abhyankar ierr = PetscFree(cols);CHKERRQ(ierr); 36984683f7a4SShri Abhyankar } else { 36994683f7a4SShri Abhyankar /* determine buffer space needed for message */ 37004683f7a4SShri Abhyankar nz = 0; 37014683f7a4SShri Abhyankar for (i=0; i<m; i++) { 37024683f7a4SShri Abhyankar nz += locrowlens[i]; 37034683f7a4SShri Abhyankar } 37044683f7a4SShri Abhyankar ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&ibuf);CHKERRQ(ierr); 37054683f7a4SShri Abhyankar mycols = ibuf; 37064683f7a4SShri Abhyankar /* receive message of column indices*/ 37074683f7a4SShri Abhyankar ierr = MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 37084683f7a4SShri Abhyankar ierr = MPI_Get_count(&status,MPIU_INT,&maxnz);CHKERRQ(ierr); 37094683f7a4SShri Abhyankar if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file"); 37104683f7a4SShri Abhyankar } 37114683f7a4SShri Abhyankar 37124683f7a4SShri Abhyankar /* loop over local rows, determining number of off diagonal entries */ 37134683f7a4SShri Abhyankar ierr = PetscMalloc2(rend-rstart,PetscInt,&dlens,rend-rstart,PetscInt,&odlens);CHKERRQ(ierr); 37144683f7a4SShri Abhyankar ierr = PetscMalloc3(Mbs,PetscInt,&mask,Mbs,PetscInt,&masked1,Mbs,PetscInt,&masked2);CHKERRQ(ierr); 37154683f7a4SShri Abhyankar ierr = PetscMemzero(mask,Mbs*sizeof(PetscInt));CHKERRQ(ierr); 37164683f7a4SShri Abhyankar ierr = PetscMemzero(masked1,Mbs*sizeof(PetscInt));CHKERRQ(ierr); 37174683f7a4SShri Abhyankar ierr = PetscMemzero(masked2,Mbs*sizeof(PetscInt));CHKERRQ(ierr); 37184683f7a4SShri Abhyankar rowcount = 0; nzcount = 0; 37194683f7a4SShri Abhyankar for (i=0; i<mbs; i++) { 37204683f7a4SShri Abhyankar dcount = 0; 37214683f7a4SShri Abhyankar odcount = 0; 37224683f7a4SShri Abhyankar for (j=0; j<bs; j++) { 37234683f7a4SShri Abhyankar kmax = locrowlens[rowcount]; 37244683f7a4SShri Abhyankar for (k=0; k<kmax; k++) { 37254683f7a4SShri Abhyankar tmp = mycols[nzcount++]/bs; 37264683f7a4SShri Abhyankar if (!mask[tmp]) { 37274683f7a4SShri Abhyankar mask[tmp] = 1; 37284683f7a4SShri Abhyankar if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp; 37294683f7a4SShri Abhyankar else masked1[dcount++] = tmp; 37304683f7a4SShri Abhyankar } 37314683f7a4SShri Abhyankar } 37324683f7a4SShri Abhyankar rowcount++; 37334683f7a4SShri Abhyankar } 37344683f7a4SShri Abhyankar 37354683f7a4SShri Abhyankar dlens[i] = dcount; 37364683f7a4SShri Abhyankar odlens[i] = odcount; 37374683f7a4SShri Abhyankar 37384683f7a4SShri Abhyankar /* zero out the mask elements we set */ 37394683f7a4SShri Abhyankar for (j=0; j<dcount; j++) mask[masked1[j]] = 0; 37404683f7a4SShri Abhyankar for (j=0; j<odcount; j++) mask[masked2[j]] = 0; 37414683f7a4SShri Abhyankar } 37424683f7a4SShri Abhyankar 37434683f7a4SShri Abhyankar 37444683f7a4SShri Abhyankar if (!sizesset) { 37454683f7a4SShri Abhyankar ierr = MatSetSizes(newmat,m,m,M+extra_rows,N+extra_rows);CHKERRQ(ierr); 37464683f7a4SShri Abhyankar } 37474683f7a4SShri Abhyankar ierr = MatMPIBAIJSetPreallocation(newmat,bs,0,dlens,0,odlens);CHKERRQ(ierr); 37484683f7a4SShri Abhyankar 37494683f7a4SShri Abhyankar if (!rank) { 37504683f7a4SShri Abhyankar ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&buf);CHKERRQ(ierr); 37514683f7a4SShri Abhyankar /* read in my part of the matrix numerical values */ 37524683f7a4SShri Abhyankar nz = procsnz[0]; 37534683f7a4SShri Abhyankar vals = buf; 37544683f7a4SShri Abhyankar mycols = ibuf; 37554683f7a4SShri Abhyankar if (size == 1) nz -= extra_rows; 37564683f7a4SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 37574683f7a4SShri Abhyankar if (size == 1) for (i=0; i< extra_rows; i++) { vals[nz+i] = 1.0; } 37584683f7a4SShri Abhyankar 37594683f7a4SShri Abhyankar /* insert into matrix */ 37604683f7a4SShri Abhyankar jj = rstart*bs; 37614683f7a4SShri Abhyankar for (i=0; i<m; i++) { 37624683f7a4SShri Abhyankar ierr = MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);CHKERRQ(ierr); 37634683f7a4SShri Abhyankar mycols += locrowlens[i]; 37644683f7a4SShri Abhyankar vals += locrowlens[i]; 37654683f7a4SShri Abhyankar jj++; 37664683f7a4SShri Abhyankar } 37674683f7a4SShri Abhyankar /* read in other processors (except the last one) and ship out */ 37684683f7a4SShri Abhyankar for (i=1; i<size-1; i++) { 37694683f7a4SShri Abhyankar nz = procsnz[i]; 37704683f7a4SShri Abhyankar vals = buf; 37714683f7a4SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 37724683f7a4SShri Abhyankar ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newmat)->tag,comm);CHKERRQ(ierr); 37734683f7a4SShri Abhyankar } 37744683f7a4SShri Abhyankar /* the last proc */ 37754683f7a4SShri Abhyankar if (size != 1){ 37764683f7a4SShri Abhyankar nz = procsnz[i] - extra_rows; 37774683f7a4SShri Abhyankar vals = buf; 37784683f7a4SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 37794683f7a4SShri Abhyankar for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0; 37804683f7a4SShri Abhyankar ierr = MPI_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,((PetscObject)newmat)->tag,comm);CHKERRQ(ierr); 37814683f7a4SShri Abhyankar } 37824683f7a4SShri Abhyankar ierr = PetscFree(procsnz);CHKERRQ(ierr); 37834683f7a4SShri Abhyankar } else { 37844683f7a4SShri Abhyankar /* receive numeric values */ 37854683f7a4SShri Abhyankar ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&buf);CHKERRQ(ierr); 37864683f7a4SShri Abhyankar 37874683f7a4SShri Abhyankar /* receive message of values*/ 37884683f7a4SShri Abhyankar vals = buf; 37894683f7a4SShri Abhyankar mycols = ibuf; 37904683f7a4SShri Abhyankar ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newmat)->tag,comm,&status);CHKERRQ(ierr); 37914683f7a4SShri Abhyankar ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr); 37924683f7a4SShri Abhyankar if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file"); 37934683f7a4SShri Abhyankar 37944683f7a4SShri Abhyankar /* insert into matrix */ 37954683f7a4SShri Abhyankar jj = rstart*bs; 37964683f7a4SShri Abhyankar for (i=0; i<m; i++) { 37974683f7a4SShri Abhyankar ierr = MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);CHKERRQ(ierr); 37984683f7a4SShri Abhyankar mycols += locrowlens[i]; 37994683f7a4SShri Abhyankar vals += locrowlens[i]; 38004683f7a4SShri Abhyankar jj++; 38014683f7a4SShri Abhyankar } 38024683f7a4SShri Abhyankar } 38034683f7a4SShri Abhyankar ierr = PetscFree(locrowlens);CHKERRQ(ierr); 38044683f7a4SShri Abhyankar ierr = PetscFree(buf);CHKERRQ(ierr); 38054683f7a4SShri Abhyankar ierr = PetscFree(ibuf);CHKERRQ(ierr); 38064683f7a4SShri Abhyankar ierr = PetscFree2(rowners,browners);CHKERRQ(ierr); 38074683f7a4SShri Abhyankar ierr = PetscFree2(dlens,odlens);CHKERRQ(ierr); 38084683f7a4SShri Abhyankar ierr = PetscFree3(mask,masked1,masked2);CHKERRQ(ierr); 38094683f7a4SShri Abhyankar ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 38104683f7a4SShri Abhyankar ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 38114683f7a4SShri Abhyankar 38124683f7a4SShri Abhyankar PetscFunctionReturn(0); 38134683f7a4SShri Abhyankar } 38144683f7a4SShri Abhyankar 38154683f7a4SShri Abhyankar #undef __FUNCT__ 38164a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJSetHashTableFactor" 3817133cdb44SSatish Balay /*@ 3818133cdb44SSatish Balay MatMPIBAIJSetHashTableFactor - Sets the factor required to compute the size of the HashTable. 3819133cdb44SSatish Balay 3820133cdb44SSatish Balay Input Parameters: 3821133cdb44SSatish Balay . mat - the matrix 3822133cdb44SSatish Balay . fact - factor 3823133cdb44SSatish Balay 3824c5eb9154SBarry Smith Not Collective, each process can use a different factor 3825fee21e36SBarry Smith 38268c890885SBarry Smith Level: advanced 38278c890885SBarry Smith 3828133cdb44SSatish Balay Notes: 38298c07d4e3SBarry Smith This can also be set by the command line option: -mat_use_hash_table <fact> 3830133cdb44SSatish Balay 3831133cdb44SSatish Balay .keywords: matrix, hashtable, factor, HT 3832133cdb44SSatish Balay 3833133cdb44SSatish Balay .seealso: MatSetOption() 3834133cdb44SSatish Balay @*/ 3835be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJSetHashTableFactor(Mat mat,PetscReal fact) 3836133cdb44SSatish Balay { 38374ac538c5SBarry Smith PetscErrorCode ierr; 38385bf65638SKris Buschelman 38395bf65638SKris Buschelman PetscFunctionBegin; 38404ac538c5SBarry Smith ierr = PetscTryMethod(mat,"MatSetHashTableFactor_C",(Mat,PetscReal),(mat,fact));CHKERRQ(ierr); 38415bf65638SKris Buschelman PetscFunctionReturn(0); 38425bf65638SKris Buschelman } 38435bf65638SKris Buschelman 3844be1d678aSKris Buschelman EXTERN_C_BEGIN 38455bf65638SKris Buschelman #undef __FUNCT__ 38465bf65638SKris Buschelman #define __FUNCT__ "MatSetHashTableFactor_MPIBAIJ" 3847be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSetHashTableFactor_MPIBAIJ(Mat mat,PetscReal fact) 38485bf65638SKris Buschelman { 384925fdafccSSatish Balay Mat_MPIBAIJ *baij; 3850133cdb44SSatish Balay 3851133cdb44SSatish Balay PetscFunctionBegin; 3852133cdb44SSatish Balay baij = (Mat_MPIBAIJ*)mat->data; 3853133cdb44SSatish Balay baij->ht_fact = fact; 3854133cdb44SSatish Balay PetscFunctionReturn(0); 3855133cdb44SSatish Balay } 3856be1d678aSKris Buschelman EXTERN_C_END 3857f2a5309cSSatish Balay 38584a2ae208SSatish Balay #undef __FUNCT__ 38594a2ae208SSatish Balay #define __FUNCT__ "MatMPIBAIJGetSeqBAIJ" 3860be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIBAIJGetSeqBAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[]) 3861f2a5309cSSatish Balay { 3862f2a5309cSSatish Balay Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data; 3863f2a5309cSSatish Balay PetscFunctionBegin; 3864f2a5309cSSatish Balay *Ad = a->A; 3865f2a5309cSSatish Balay *Ao = a->B; 3866195d93cdSBarry Smith *colmap = a->garray; 3867f2a5309cSSatish Balay PetscFunctionReturn(0); 3868f2a5309cSSatish Balay } 386985535b8eSBarry Smith 387085535b8eSBarry Smith /* 387185535b8eSBarry Smith Special version for direct calls from Fortran (to eliminate two function call overheads 387285535b8eSBarry Smith */ 387385535b8eSBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 387485535b8eSBarry Smith #define matmpibaijsetvaluesblocked_ MATMPIBAIJSETVALUESBLOCKED 387585535b8eSBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 387685535b8eSBarry Smith #define matmpibaijsetvaluesblocked_ matmpibaijsetvaluesblocked 387785535b8eSBarry Smith #endif 387885535b8eSBarry Smith 387985535b8eSBarry Smith #undef __FUNCT__ 388085535b8eSBarry Smith #define __FUNCT__ "matmpibiajsetvaluesblocked" 388185535b8eSBarry Smith /*@C 388285535b8eSBarry Smith MatMPIBAIJSetValuesBlocked - Direct Fortran call to replace call to MatSetValuesBlocked() 388385535b8eSBarry Smith 388485535b8eSBarry Smith Collective on Mat 388585535b8eSBarry Smith 388685535b8eSBarry Smith Input Parameters: 388785535b8eSBarry Smith + mat - the matrix 388885535b8eSBarry Smith . min - number of input rows 388985535b8eSBarry Smith . im - input rows 389085535b8eSBarry Smith . nin - number of input columns 389185535b8eSBarry Smith . in - input columns 389285535b8eSBarry Smith . v - numerical values input 389385535b8eSBarry Smith - addvin - INSERT_VALUES or ADD_VALUES 389485535b8eSBarry Smith 389585535b8eSBarry Smith Notes: This has a complete copy of MatSetValuesBlocked_MPIBAIJ() which is terrible code un-reuse. 389685535b8eSBarry Smith 389785535b8eSBarry Smith Level: advanced 389885535b8eSBarry Smith 389985535b8eSBarry Smith .seealso: MatSetValuesBlocked() 390085535b8eSBarry Smith @*/ 390185535b8eSBarry Smith PetscErrorCode matmpibaijsetvaluesblocked_(Mat *matin,PetscInt *min,const PetscInt im[],PetscInt *nin,const PetscInt in[],const MatScalar v[],InsertMode *addvin) 390285535b8eSBarry Smith { 390385535b8eSBarry Smith /* convert input arguments to C version */ 390485535b8eSBarry Smith Mat mat = *matin; 390585535b8eSBarry Smith PetscInt m = *min, n = *nin; 390685535b8eSBarry Smith InsertMode addv = *addvin; 390785535b8eSBarry Smith 390885535b8eSBarry Smith Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data; 390985535b8eSBarry Smith const MatScalar *value; 391085535b8eSBarry Smith MatScalar *barray=baij->barray; 3911ace3abfcSBarry Smith PetscBool roworiented = baij->roworiented; 391285535b8eSBarry Smith PetscErrorCode ierr; 391385535b8eSBarry Smith PetscInt i,j,ii,jj,row,col,rstart=baij->rstartbs; 391485535b8eSBarry Smith PetscInt rend=baij->rendbs,cstart=baij->cstartbs,stepval; 3915d0f46423SBarry Smith PetscInt cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2; 391685535b8eSBarry Smith 391785535b8eSBarry Smith PetscFunctionBegin; 391885535b8eSBarry Smith /* tasks normally handled by MatSetValuesBlocked() */ 391985535b8eSBarry Smith if (mat->insertmode == NOT_SET_VALUES) { 392085535b8eSBarry Smith mat->insertmode = addv; 392185535b8eSBarry Smith } 392285535b8eSBarry Smith #if defined(PETSC_USE_DEBUG) 3923e7e72b3dSBarry Smith else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 3924e32f2f54SBarry Smith if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 392585535b8eSBarry Smith #endif 392685535b8eSBarry Smith if (mat->assembled) { 392785535b8eSBarry Smith mat->was_assembled = PETSC_TRUE; 392885535b8eSBarry Smith mat->assembled = PETSC_FALSE; 392985535b8eSBarry Smith } 393085535b8eSBarry Smith ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 393185535b8eSBarry Smith 393285535b8eSBarry Smith 393385535b8eSBarry Smith if(!barray) { 393485535b8eSBarry Smith ierr = PetscMalloc(bs2*sizeof(MatScalar),&barray);CHKERRQ(ierr); 393585535b8eSBarry Smith baij->barray = barray; 393685535b8eSBarry Smith } 393785535b8eSBarry Smith 393885535b8eSBarry Smith if (roworiented) { 393985535b8eSBarry Smith stepval = (n-1)*bs; 394085535b8eSBarry Smith } else { 394185535b8eSBarry Smith stepval = (m-1)*bs; 394285535b8eSBarry Smith } 394385535b8eSBarry Smith for (i=0; i<m; i++) { 394485535b8eSBarry Smith if (im[i] < 0) continue; 394585535b8eSBarry Smith #if defined(PETSC_USE_DEBUG) 3946e32f2f54SBarry 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); 394785535b8eSBarry Smith #endif 394885535b8eSBarry Smith if (im[i] >= rstart && im[i] < rend) { 394985535b8eSBarry Smith row = im[i] - rstart; 395085535b8eSBarry Smith for (j=0; j<n; j++) { 395185535b8eSBarry Smith /* If NumCol = 1 then a copy is not required */ 395285535b8eSBarry Smith if ((roworiented) && (n == 1)) { 395385535b8eSBarry Smith barray = (MatScalar*)v + i*bs2; 395485535b8eSBarry Smith } else if((!roworiented) && (m == 1)) { 395585535b8eSBarry Smith barray = (MatScalar*)v + j*bs2; 395685535b8eSBarry Smith } else { /* Here a copy is required */ 395785535b8eSBarry Smith if (roworiented) { 395885535b8eSBarry Smith value = v + i*(stepval+bs)*bs + j*bs; 395985535b8eSBarry Smith } else { 396085535b8eSBarry Smith value = v + j*(stepval+bs)*bs + i*bs; 396185535b8eSBarry Smith } 396285535b8eSBarry Smith for (ii=0; ii<bs; ii++,value+=stepval) { 396385535b8eSBarry Smith for (jj=0; jj<bs; jj++) { 396485535b8eSBarry Smith *barray++ = *value++; 396585535b8eSBarry Smith } 396685535b8eSBarry Smith } 396785535b8eSBarry Smith barray -=bs2; 396885535b8eSBarry Smith } 396985535b8eSBarry Smith 397085535b8eSBarry Smith if (in[j] >= cstart && in[j] < cend){ 397185535b8eSBarry Smith col = in[j] - cstart; 397297e5c40aSBarry Smith ierr = MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);CHKERRQ(ierr); 397385535b8eSBarry Smith } 397485535b8eSBarry Smith else if (in[j] < 0) continue; 397585535b8eSBarry Smith #if defined(PETSC_USE_DEBUG) 3976cb9801acSJed 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); 397785535b8eSBarry Smith #endif 397885535b8eSBarry Smith else { 397985535b8eSBarry Smith if (mat->was_assembled) { 398085535b8eSBarry Smith if (!baij->colmap) { 398185535b8eSBarry Smith ierr = CreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr); 398285535b8eSBarry Smith } 398385535b8eSBarry Smith 398485535b8eSBarry Smith #if defined(PETSC_USE_DEBUG) 398585535b8eSBarry Smith #if defined (PETSC_USE_CTABLE) 398685535b8eSBarry Smith { PetscInt data; 398785535b8eSBarry Smith ierr = PetscTableFind(baij->colmap,in[j]+1,&data);CHKERRQ(ierr); 3988e32f2f54SBarry Smith if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap"); 398985535b8eSBarry Smith } 399085535b8eSBarry Smith #else 3991e32f2f54SBarry Smith if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap"); 399285535b8eSBarry Smith #endif 399385535b8eSBarry Smith #endif 399485535b8eSBarry Smith #if defined (PETSC_USE_CTABLE) 399585535b8eSBarry Smith ierr = PetscTableFind(baij->colmap,in[j]+1,&col);CHKERRQ(ierr); 399685535b8eSBarry Smith col = (col - 1)/bs; 399785535b8eSBarry Smith #else 399885535b8eSBarry Smith col = (baij->colmap[in[j]] - 1)/bs; 399985535b8eSBarry Smith #endif 400085535b8eSBarry Smith if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) { 400185535b8eSBarry Smith ierr = DisAssemble_MPIBAIJ(mat);CHKERRQ(ierr); 400285535b8eSBarry Smith col = in[j]; 400385535b8eSBarry Smith } 400485535b8eSBarry Smith } 400585535b8eSBarry Smith else col = in[j]; 400697e5c40aSBarry Smith ierr = MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);CHKERRQ(ierr); 400785535b8eSBarry Smith } 400885535b8eSBarry Smith } 400985535b8eSBarry Smith } else { 401085535b8eSBarry Smith if (!baij->donotstash) { 401185535b8eSBarry Smith if (roworiented) { 401285535b8eSBarry Smith ierr = MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr); 401385535b8eSBarry Smith } else { 401485535b8eSBarry Smith ierr = MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);CHKERRQ(ierr); 401585535b8eSBarry Smith } 401685535b8eSBarry Smith } 401785535b8eSBarry Smith } 401885535b8eSBarry Smith } 401985535b8eSBarry Smith 402085535b8eSBarry Smith /* task normally handled by MatSetValuesBlocked() */ 402185535b8eSBarry Smith ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 402285535b8eSBarry Smith PetscFunctionReturn(0); 402385535b8eSBarry Smith } 4024dfb205c3SBarry Smith 4025dfb205c3SBarry Smith #undef __FUNCT__ 4026dfb205c3SBarry Smith #define __FUNCT__ "MatCreateMPIBAIJWithArrays" 4027dfb205c3SBarry Smith /*@ 4028dfb205c3SBarry Smith MatCreateMPIBAIJWithArrays - creates a MPI BAIJ matrix using arrays that contain in standard 4029dfb205c3SBarry Smith CSR format the local rows. 4030dfb205c3SBarry Smith 4031dfb205c3SBarry Smith Collective on MPI_Comm 4032dfb205c3SBarry Smith 4033dfb205c3SBarry Smith Input Parameters: 4034dfb205c3SBarry Smith + comm - MPI communicator 4035dfb205c3SBarry Smith . bs - the block size, only a block size of 1 is supported 4036dfb205c3SBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 4037dfb205c3SBarry Smith . n - This value should be the same as the local size used in creating the 4038dfb205c3SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 4039dfb205c3SBarry Smith calculated if N is given) For square matrices n is almost always m. 4040dfb205c3SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 4041dfb205c3SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 4042dfb205c3SBarry Smith . i - row indices 4043dfb205c3SBarry Smith . j - column indices 4044dfb205c3SBarry Smith - a - matrix values 4045dfb205c3SBarry Smith 4046dfb205c3SBarry Smith Output Parameter: 4047dfb205c3SBarry Smith . mat - the matrix 4048dfb205c3SBarry Smith 4049dfb205c3SBarry Smith Level: intermediate 4050dfb205c3SBarry Smith 4051dfb205c3SBarry Smith Notes: 4052dfb205c3SBarry Smith The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 4053dfb205c3SBarry Smith thus you CANNOT change the matrix entries by changing the values of a[] after you have 4054dfb205c3SBarry Smith called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 4055dfb205c3SBarry Smith 4056dfb205c3SBarry Smith The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 4057dfb205c3SBarry Smith 4058dfb205c3SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 4059dfb205c3SBarry Smith 4060dfb205c3SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 4061dfb205c3SBarry Smith MPIAIJ, MatCreateMPIAIJ(), MatCreateMPIAIJWithSplitArrays() 4062dfb205c3SBarry Smith @*/ 4063dfb205c3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT 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) 4064dfb205c3SBarry Smith { 4065dfb205c3SBarry Smith PetscErrorCode ierr; 4066dfb205c3SBarry Smith 4067dfb205c3SBarry Smith 4068dfb205c3SBarry Smith PetscFunctionBegin; 4069dfb205c3SBarry Smith if (i[0]) { 4070dfb205c3SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 4071dfb205c3SBarry Smith } 4072dfb205c3SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 4073dfb205c3SBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 4074dfb205c3SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 4075dfb205c3SBarry Smith ierr = MatSetType(*mat,MATMPISBAIJ);CHKERRQ(ierr); 4076dfb205c3SBarry Smith ierr = MatMPIBAIJSetPreallocationCSR(*mat,bs,i,j,a);CHKERRQ(ierr); 4077dfb205c3SBarry Smith PetscFunctionReturn(0); 4078dfb205c3SBarry Smith } 4079