1*be1d678aSKris Buschelman #define PETSCMAT_DLL 2a64fbb32SBarry Smith 36eaac0f3SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h" 4a64fbb32SBarry Smith 5dfbe8321SBarry Smith EXTERN PetscErrorCode CreateColmap_MPIAIJ_Private(Mat); 6b1d57f15SBarry Smith EXTERN PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat,PetscInt,PetscTruth,PetscInt*,PetscInt*[],PetscInt*[],PetscTruth*); 7b1d57f15SBarry Smith EXTERN PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat,PetscInt,PetscTruth,PetscInt*,PetscInt*[],PetscInt*[],PetscTruth*); 8a64fbb32SBarry Smith 94a2ae208SSatish Balay #undef __FUNCT__ 104a2ae208SSatish Balay #define __FUNCT__ "MatFDColoringCreate_MPIAIJ" 11dfbe8321SBarry Smith PetscErrorCode MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c) 12a64fbb32SBarry Smith { 136eaac0f3SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 146849ba73SBarry Smith PetscErrorCode ierr; 15b1d57f15SBarry Smith PetscMPIInt size,*ncolsonproc,*disp,nn; 16b1d57f15SBarry Smith PetscInt i,*is,n,nrows,j,k,m,*rows = 0,*A_ci,*A_cj,ncols,col; 17b1d57f15SBarry Smith PetscInt nis = iscoloring->n,nctot,*cols,*B_ci,*B_cj; 18b1d57f15SBarry Smith PetscInt *rowhit,M = mat->m,cstart = aij->cstart,cend = aij->cend,colb; 19b1d57f15SBarry Smith PetscInt *columnsforrow,l; 20b9617806SBarry Smith IS *isa; 21f1af5d2fSBarry Smith PetscTruth done,flg; 22a64fbb32SBarry Smith 233a40ed3dSBarry Smith PetscFunctionBegin; 24522c5e43SBarry Smith if (!mat->assembled) { 2529bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();"); 26522c5e43SBarry Smith } 27522c5e43SBarry Smith 28b9617806SBarry Smith ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr); 29005c665bSBarry Smith c->M = mat->M; /* set the global rows and columns and local rows */ 30005c665bSBarry Smith c->N = mat->N; 31005c665bSBarry Smith c->m = mat->m; 32005c665bSBarry Smith c->rstart = aij->rstart; 33005c665bSBarry Smith 34a64fbb32SBarry Smith c->ncolors = nis; 35b1d57f15SBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt),&c->ncolumns);CHKERRQ(ierr); 36b1d57f15SBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt*),&c->columns);CHKERRQ(ierr); 37b1d57f15SBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt),&c->nrows);CHKERRQ(ierr); 38b1d57f15SBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt*),&c->rows);CHKERRQ(ierr); 39b1d57f15SBarry Smith ierr = PetscMalloc(nis*sizeof(PetscInt*),&c->columnsforrow);CHKERRQ(ierr); 4052e6d16bSBarry Smith ierr = PetscLogObjectMemory(c,5*nis*sizeof(PetscInt));CHKERRQ(ierr); 416eaac0f3SBarry Smith 426eaac0f3SBarry Smith /* Allow access to data structures of local part of matrix */ 436eaac0f3SBarry Smith if (!aij->colmap) { 446eaac0f3SBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 456eaac0f3SBarry Smith } 4643a90d84SBarry Smith /* 4743a90d84SBarry Smith Calls the _SeqAIJ() version of these routines to make sure it does not 4843a90d84SBarry Smith get the reduced (by inodes) version of I and J 4943a90d84SBarry Smith */ 5043a90d84SBarry Smith ierr = MatGetColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr); 5143a90d84SBarry Smith ierr = MatGetColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr); 526eaac0f3SBarry Smith 531dab6e02SBarry Smith ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr); 54b1d57f15SBarry Smith ierr = PetscMalloc(2*size*sizeof(PetscInt*),&ncolsonproc);CHKERRQ(ierr); 556eaac0f3SBarry Smith disp = ncolsonproc + size; 566eaac0f3SBarry Smith 57b1d57f15SBarry Smith ierr = PetscMalloc((M+1)*sizeof(PetscInt),&rowhit);CHKERRQ(ierr); 58b1d57f15SBarry Smith ierr = PetscMalloc((M+1)*sizeof(PetscInt),&columnsforrow);CHKERRQ(ierr); 596eaac0f3SBarry Smith 60a64fbb32SBarry Smith /* 61a64fbb32SBarry Smith Temporary option to allow for debugging/testing 62a64fbb32SBarry Smith */ 63b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-matfdcoloring_slow",&flg);CHKERRQ(ierr); 646eaac0f3SBarry Smith 65a64fbb32SBarry Smith for (i=0; i<nis; i++) { 66b9b97703SBarry Smith ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr); 67a64fbb32SBarry Smith ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr); 68a64fbb32SBarry Smith c->ncolumns[i] = n; 696eaac0f3SBarry Smith c->ncolumns[i] = n; 70a64fbb32SBarry Smith if (n) { 71b1d57f15SBarry Smith ierr = PetscMalloc(n*sizeof(PetscInt),&c->columns[i]);CHKERRQ(ierr); 7252e6d16bSBarry Smith ierr = PetscLogObjectMemory(c,n*sizeof(PetscInt));CHKERRQ(ierr); 73b1d57f15SBarry Smith ierr = PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));CHKERRQ(ierr); 74a64fbb32SBarry Smith } else { 75a64fbb32SBarry Smith c->columns[i] = 0; 76a64fbb32SBarry Smith } 77a64fbb32SBarry Smith 786eaac0f3SBarry Smith /* Determine the total (parallel) number of columns of this color */ 79b1d57f15SBarry Smith nn = (PetscMPIInt)n; 80b1d57f15SBarry Smith ierr = MPI_Allgather(&nn,1,MPI_INT,ncolsonproc,1,MPI_INT,mat->comm);CHKERRQ(ierr); 816eaac0f3SBarry Smith nctot = 0; for (j=0; j<size; j++) {nctot += ncolsonproc[j];} 823a7fca6bSBarry Smith if (!nctot) { 8363ba0a88SBarry Smith ierr = PetscLogInfo(((PetscObject)mat,"MatFDColoringCreate_MPIAIJ: Coloring of matrix has some unneeded colors with no corresponding rows\n"));CHKERRQ(ierr); 843a7fca6bSBarry Smith } 856eaac0f3SBarry Smith 866eaac0f3SBarry Smith disp[0] = 0; 876eaac0f3SBarry Smith for (j=1; j<size; j++) { 886eaac0f3SBarry Smith disp[j] = disp[j-1] + ncolsonproc[j-1]; 896eaac0f3SBarry Smith } 906eaac0f3SBarry Smith 916eaac0f3SBarry Smith /* Get complete list of columns for color on each processor */ 92b1d57f15SBarry Smith ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr); 93b1d57f15SBarry Smith ierr = MPI_Allgatherv(is,n,MPIU_INT,cols,ncolsonproc,disp,MPIU_INT,mat->comm);CHKERRQ(ierr); 946eaac0f3SBarry Smith 956eaac0f3SBarry Smith /* 966eaac0f3SBarry Smith Mark all rows affect by these columns 976eaac0f3SBarry Smith */ 98f158e583SBarry Smith if (!flg) {/*-----------------------------------------------------------------------------*/ 99f158e583SBarry Smith /* crude, fast version */ 100b1d57f15SBarry Smith ierr = PetscMemzero(rowhit,M*sizeof(PetscInt));CHKERRQ(ierr); 101a64fbb32SBarry Smith /* loop over columns*/ 1026eaac0f3SBarry Smith for (j=0; j<nctot; j++) { 1036eaac0f3SBarry Smith col = cols[j]; 1046eaac0f3SBarry Smith if (col >= cstart && col < cend) { 1056eaac0f3SBarry Smith /* column is in diagonal block of matrix */ 1066eaac0f3SBarry Smith rows = A_cj + A_ci[col-cstart]; 1076eaac0f3SBarry Smith m = A_ci[col-cstart+1] - A_ci[col-cstart]; 1086eaac0f3SBarry Smith } else { 109aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 1100f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr) 111fa46199cSSatish Balay colb --; 112b3d2dc96SSatish Balay #else 1136eaac0f3SBarry Smith colb = aij->colmap[col] - 1; 114b3d2dc96SSatish Balay #endif 1156eaac0f3SBarry Smith if (colb == -1) { 1166eaac0f3SBarry Smith m = 0; 1176eaac0f3SBarry Smith } else { 1186eaac0f3SBarry Smith rows = B_cj + B_ci[colb]; 1196eaac0f3SBarry Smith m = B_ci[colb+1] - B_ci[colb]; 1206eaac0f3SBarry Smith } 1216eaac0f3SBarry Smith } 122a64fbb32SBarry Smith /* loop over columns marking them in rowhit */ 123a64fbb32SBarry Smith for (k=0; k<m; k++) { 124a64fbb32SBarry Smith rowhit[*rows++] = col + 1; 125a64fbb32SBarry Smith } 126a64fbb32SBarry Smith } 1276eaac0f3SBarry Smith 128a64fbb32SBarry Smith /* count the number of hits */ 129a64fbb32SBarry Smith nrows = 0; 1306eaac0f3SBarry Smith for (j=0; j<M; j++) { 131a64fbb32SBarry Smith if (rowhit[j]) nrows++; 132a64fbb32SBarry Smith } 133a64fbb32SBarry Smith c->nrows[i] = nrows; 134b1d57f15SBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr); 135b1d57f15SBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr); 13652e6d16bSBarry Smith ierr = PetscLogObjectMemory(c,2*(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr); 137a64fbb32SBarry Smith nrows = 0; 1386eaac0f3SBarry Smith for (j=0; j<M; j++) { 139a64fbb32SBarry Smith if (rowhit[j]) { 140a64fbb32SBarry Smith c->rows[i][nrows] = j; 141a64fbb32SBarry Smith c->columnsforrow[i][nrows] = rowhit[j] - 1; 142a64fbb32SBarry Smith nrows++; 143a64fbb32SBarry Smith } 144a64fbb32SBarry Smith } 145a64fbb32SBarry Smith } else {/*-------------------------------------------------------------------------------*/ 146f158e583SBarry Smith /* slow version, using rowhit as a linked list */ 147b1d57f15SBarry Smith PetscInt currentcol,fm,mfm; 1486eaac0f3SBarry Smith rowhit[M] = M; 149a64fbb32SBarry Smith nrows = 0; 150a64fbb32SBarry Smith /* loop over columns*/ 1516eaac0f3SBarry Smith for (j=0; j<nctot; j++) { 1526eaac0f3SBarry Smith col = cols[j]; 1536eaac0f3SBarry Smith if (col >= cstart && col < cend) { 1546eaac0f3SBarry Smith /* column is in diagonal block of matrix */ 1556eaac0f3SBarry Smith rows = A_cj + A_ci[col-cstart]; 1566eaac0f3SBarry Smith m = A_ci[col-cstart+1] - A_ci[col-cstart]; 1576eaac0f3SBarry Smith } else { 158aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 1590f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr); 160fa46199cSSatish Balay colb --; 161b3d2dc96SSatish Balay #else 1626eaac0f3SBarry Smith colb = aij->colmap[col] - 1; 163b3d2dc96SSatish Balay #endif 1646eaac0f3SBarry Smith if (colb == -1) { 1656eaac0f3SBarry Smith m = 0; 1666eaac0f3SBarry Smith } else { 1676eaac0f3SBarry Smith rows = B_cj + B_ci[colb]; 1686eaac0f3SBarry Smith m = B_ci[colb+1] - B_ci[colb]; 1696eaac0f3SBarry Smith } 1706eaac0f3SBarry Smith } 171a64fbb32SBarry Smith /* loop over columns marking them in rowhit */ 1726eaac0f3SBarry Smith fm = M; /* fm points to first entry in linked list */ 173a64fbb32SBarry Smith for (k=0; k<m; k++) { 174a64fbb32SBarry Smith currentcol = *rows++; 175a64fbb32SBarry Smith /* is it already in the list? */ 176a64fbb32SBarry Smith do { 177a64fbb32SBarry Smith mfm = fm; 178a64fbb32SBarry Smith fm = rowhit[fm]; 179a64fbb32SBarry Smith } while (fm < currentcol); 180a64fbb32SBarry Smith /* not in list so add it */ 181a64fbb32SBarry Smith if (fm != currentcol) { 182a64fbb32SBarry Smith nrows++; 183a64fbb32SBarry Smith columnsforrow[currentcol] = col; 184a64fbb32SBarry Smith /* next three lines insert new entry into linked list */ 185a64fbb32SBarry Smith rowhit[mfm] = currentcol; 186a64fbb32SBarry Smith rowhit[currentcol] = fm; 187a64fbb32SBarry Smith fm = currentcol; 188a64fbb32SBarry Smith /* fm points to present position in list since we know the columns are sorted */ 189a64fbb32SBarry Smith } else { 19029bbc08cSBarry Smith SETERRQ(PETSC_ERR_PLIB,"Invalid coloring of matrix detected"); 191a64fbb32SBarry Smith } 192a64fbb32SBarry Smith } 193a64fbb32SBarry Smith } 194a64fbb32SBarry Smith c->nrows[i] = nrows; 195b1d57f15SBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr); 196b1d57f15SBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr); 19752e6d16bSBarry Smith ierr = PetscLogObjectMemory(c,(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr); 198a64fbb32SBarry Smith /* now store the linked list of rows into c->rows[i] */ 199a64fbb32SBarry Smith nrows = 0; 2006eaac0f3SBarry Smith fm = rowhit[M]; 201a64fbb32SBarry Smith do { 202a64fbb32SBarry Smith c->rows[i][nrows] = fm; 203a64fbb32SBarry Smith c->columnsforrow[i][nrows++] = columnsforrow[fm]; 204a64fbb32SBarry Smith fm = rowhit[fm]; 2056eaac0f3SBarry Smith } while (fm < M); 2066eaac0f3SBarry Smith } /* ---------------------------------------------------------------------------------------*/ 207606d414cSSatish Balay ierr = PetscFree(cols);CHKERRQ(ierr); 2086eaac0f3SBarry Smith } 20930b34957SBarry Smith 21030b34957SBarry Smith /* Optimize by adding the vscale, and scaleforrow[][] fields */ 21130b34957SBarry Smith /* 21230b34957SBarry Smith vscale will contain the "diagonal" on processor scalings followed by the off processor 21330b34957SBarry Smith */ 21430b34957SBarry Smith ierr = VecCreateGhost(mat->comm,aij->A->m,PETSC_DETERMINE,aij->B->n,aij->garray,&c->vscale);CHKERRQ(ierr) 215b1d57f15SBarry Smith ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr); 21630b34957SBarry Smith for (k=0; k<c->ncolors; k++) { 217b1d57f15SBarry Smith ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr); 21830b34957SBarry Smith for (l=0; l<c->nrows[k]; l++) { 21930b34957SBarry Smith col = c->columnsforrow[k][l]; 22030b34957SBarry Smith if (col >= cstart && col < cend) { 22130b34957SBarry Smith /* column is in diagonal block of matrix */ 22230b34957SBarry Smith colb = col - cstart; 22330b34957SBarry Smith } else { 22430b34957SBarry Smith /* column is in "off-processor" part */ 22530b34957SBarry Smith #if defined (PETSC_USE_CTABLE) 22630b34957SBarry Smith ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr); 22730b34957SBarry Smith colb --; 22830b34957SBarry Smith #else 22930b34957SBarry Smith colb = aij->colmap[col] - 1; 23030b34957SBarry Smith #endif 23130b34957SBarry Smith colb += cend - cstart; 23230b34957SBarry Smith } 23330b34957SBarry Smith c->vscaleforrow[k][l] = colb; 23430b34957SBarry Smith } 23530b34957SBarry Smith } 236b9617806SBarry Smith ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr); 23730b34957SBarry Smith 238606d414cSSatish Balay ierr = PetscFree(rowhit);CHKERRQ(ierr); 239606d414cSSatish Balay ierr = PetscFree(columnsforrow);CHKERRQ(ierr); 240606d414cSSatish Balay ierr = PetscFree(ncolsonproc);CHKERRQ(ierr); 24143a90d84SBarry Smith ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr); 24243a90d84SBarry Smith ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr); 2433a40ed3dSBarry Smith PetscFunctionReturn(0); 244a64fbb32SBarry Smith } 245a64fbb32SBarry Smith 246b9617806SBarry Smith 247b9617806SBarry Smith 248b9617806SBarry Smith 249b9617806SBarry Smith 250b9617806SBarry Smith 251