1a64fbb32SBarry Smith 26eaac0f3SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h" 3a64fbb32SBarry Smith 4dfbe8321SBarry Smith EXTERN PetscErrorCode CreateColmap_MPIAIJ_Private(Mat); 5dfbe8321SBarry Smith EXTERN PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int*[],int*[],PetscTruth*); 6dfbe8321SBarry Smith EXTERN PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int*[],int*[],PetscTruth*); 7a64fbb32SBarry Smith 84a2ae208SSatish Balay #undef __FUNCT__ 94a2ae208SSatish Balay #define __FUNCT__ "MatFDColoringCreate_MPIAIJ" 10dfbe8321SBarry Smith PetscErrorCode MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c) 11a64fbb32SBarry Smith { 126eaac0f3SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 13*6849ba73SBarry Smith PetscErrorCode ierr; 14*6849ba73SBarry Smith int i,*is,n,nrows,j,k,m,*rows = 0,*A_ci,*A_cj,ncols,col; 156eaac0f3SBarry Smith int nis = iscoloring->n,*ncolsonproc,size,nctot,*cols,*disp,*B_ci,*B_cj; 166eaac0f3SBarry Smith int *rowhit,M = mat->m,cstart = aij->cstart,cend = aij->cend,colb; 1730b34957SBarry Smith int *columnsforrow,l; 18b9617806SBarry Smith IS *isa; 19f1af5d2fSBarry Smith PetscTruth done,flg; 20a64fbb32SBarry Smith 213a40ed3dSBarry Smith PetscFunctionBegin; 22522c5e43SBarry Smith if (!mat->assembled) { 2329bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();"); 24522c5e43SBarry Smith } 25522c5e43SBarry Smith 26b9617806SBarry Smith ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr); 27005c665bSBarry Smith c->M = mat->M; /* set the global rows and columns and local rows */ 28005c665bSBarry Smith c->N = mat->N; 29005c665bSBarry Smith c->m = mat->m; 30005c665bSBarry Smith c->rstart = aij->rstart; 31005c665bSBarry Smith 32a64fbb32SBarry Smith c->ncolors = nis; 33b0a32e0cSBarry Smith ierr = PetscMalloc(nis*sizeof(int),&c->ncolumns);CHKERRQ(ierr); 34b0a32e0cSBarry Smith ierr = PetscMalloc(nis*sizeof(int*),&c->columns);CHKERRQ(ierr); 35b0a32e0cSBarry Smith ierr = PetscMalloc(nis*sizeof(int),&c->nrows);CHKERRQ(ierr); 36b0a32e0cSBarry Smith ierr = PetscMalloc(nis*sizeof(int*),&c->rows);CHKERRQ(ierr); 37b0a32e0cSBarry Smith ierr = PetscMalloc(nis*sizeof(int*),&c->columnsforrow);CHKERRQ(ierr); 38b0a32e0cSBarry Smith PetscLogObjectMemory(c,5*nis*sizeof(int)); 396eaac0f3SBarry Smith 406eaac0f3SBarry Smith /* Allow access to data structures of local part of matrix */ 416eaac0f3SBarry Smith if (!aij->colmap) { 426eaac0f3SBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 436eaac0f3SBarry Smith } 4443a90d84SBarry Smith /* 4543a90d84SBarry Smith Calls the _SeqAIJ() version of these routines to make sure it does not 4643a90d84SBarry Smith get the reduced (by inodes) version of I and J 4743a90d84SBarry Smith */ 4843a90d84SBarry Smith ierr = MatGetColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr); 4943a90d84SBarry Smith ierr = MatGetColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr); 506eaac0f3SBarry Smith 511dab6e02SBarry Smith ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr); 52b0a32e0cSBarry Smith ierr = PetscMalloc(2*size*sizeof(int*),&ncolsonproc);CHKERRQ(ierr); 536eaac0f3SBarry Smith disp = ncolsonproc + size; 546eaac0f3SBarry Smith 55b0a32e0cSBarry Smith ierr = PetscMalloc((M+1)*sizeof(int),&rowhit);CHKERRQ(ierr); 56b0a32e0cSBarry Smith ierr = PetscMalloc((M+1)*sizeof(int),&columnsforrow);CHKERRQ(ierr); 576eaac0f3SBarry Smith 58a64fbb32SBarry Smith /* 59a64fbb32SBarry Smith Temporary option to allow for debugging/testing 60a64fbb32SBarry Smith */ 61b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-matfdcoloring_slow",&flg);CHKERRQ(ierr); 626eaac0f3SBarry Smith 63a64fbb32SBarry Smith for (i=0; i<nis; i++) { 64b9b97703SBarry Smith ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr); 65a64fbb32SBarry Smith ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr); 66a64fbb32SBarry Smith c->ncolumns[i] = n; 676eaac0f3SBarry Smith c->ncolumns[i] = n; 68a64fbb32SBarry Smith if (n) { 69b0a32e0cSBarry Smith ierr = PetscMalloc(n*sizeof(int),&c->columns[i]);CHKERRQ(ierr); 70b0a32e0cSBarry Smith PetscLogObjectMemory(c,n*sizeof(int)); 71549d3d68SSatish Balay ierr = PetscMemcpy(c->columns[i],is,n*sizeof(int));CHKERRQ(ierr); 72a64fbb32SBarry Smith } else { 73a64fbb32SBarry Smith c->columns[i] = 0; 74a64fbb32SBarry Smith } 75a64fbb32SBarry Smith 766eaac0f3SBarry Smith /* Determine the total (parallel) number of columns of this color */ 77ca161407SBarry Smith ierr = MPI_Allgather(&n,1,MPI_INT,ncolsonproc,1,MPI_INT,mat->comm);CHKERRQ(ierr); 786eaac0f3SBarry Smith nctot = 0; for (j=0; j<size; j++) {nctot += ncolsonproc[j];} 793a7fca6bSBarry Smith if (!nctot) { 803a7fca6bSBarry Smith PetscLogInfo((PetscObject)mat,"MatFDColoringCreate_MPIAIJ: Coloring of matrix has some unneeded colors with no corresponding rows\n"); 813a7fca6bSBarry Smith } 826eaac0f3SBarry Smith 836eaac0f3SBarry Smith disp[0] = 0; 846eaac0f3SBarry Smith for (j=1; j<size; j++) { 856eaac0f3SBarry Smith disp[j] = disp[j-1] + ncolsonproc[j-1]; 866eaac0f3SBarry Smith } 876eaac0f3SBarry Smith 886eaac0f3SBarry Smith /* Get complete list of columns for color on each processor */ 893a7fca6bSBarry Smith ierr = PetscMalloc((nctot+1)*sizeof(int),&cols);CHKERRQ(ierr); 90ca161407SBarry Smith ierr = MPI_Allgatherv(is,n,MPI_INT,cols,ncolsonproc,disp,MPI_INT,mat->comm);CHKERRQ(ierr); 916eaac0f3SBarry Smith 926eaac0f3SBarry Smith /* 936eaac0f3SBarry Smith Mark all rows affect by these columns 946eaac0f3SBarry Smith */ 95f158e583SBarry Smith if (!flg) {/*-----------------------------------------------------------------------------*/ 96f158e583SBarry Smith /* crude, fast version */ 97549d3d68SSatish Balay ierr = PetscMemzero(rowhit,M*sizeof(int));CHKERRQ(ierr); 98a64fbb32SBarry Smith /* loop over columns*/ 996eaac0f3SBarry Smith for (j=0; j<nctot; j++) { 1006eaac0f3SBarry Smith col = cols[j]; 1016eaac0f3SBarry Smith if (col >= cstart && col < cend) { 1026eaac0f3SBarry Smith /* column is in diagonal block of matrix */ 1036eaac0f3SBarry Smith rows = A_cj + A_ci[col-cstart]; 1046eaac0f3SBarry Smith m = A_ci[col-cstart+1] - A_ci[col-cstart]; 1056eaac0f3SBarry Smith } else { 106aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 1070f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr) 108fa46199cSSatish Balay colb --; 109b3d2dc96SSatish Balay #else 1106eaac0f3SBarry Smith colb = aij->colmap[col] - 1; 111b3d2dc96SSatish Balay #endif 1126eaac0f3SBarry Smith if (colb == -1) { 1136eaac0f3SBarry Smith m = 0; 1146eaac0f3SBarry Smith } else { 1156eaac0f3SBarry Smith rows = B_cj + B_ci[colb]; 1166eaac0f3SBarry Smith m = B_ci[colb+1] - B_ci[colb]; 1176eaac0f3SBarry Smith } 1186eaac0f3SBarry Smith } 119a64fbb32SBarry Smith /* loop over columns marking them in rowhit */ 120a64fbb32SBarry Smith for (k=0; k<m; k++) { 121a64fbb32SBarry Smith rowhit[*rows++] = col + 1; 122a64fbb32SBarry Smith } 123a64fbb32SBarry Smith } 1246eaac0f3SBarry Smith 125a64fbb32SBarry Smith /* count the number of hits */ 126a64fbb32SBarry Smith nrows = 0; 1276eaac0f3SBarry Smith for (j=0; j<M; j++) { 128a64fbb32SBarry Smith if (rowhit[j]) nrows++; 129a64fbb32SBarry Smith } 130a64fbb32SBarry Smith c->nrows[i] = nrows; 131b0a32e0cSBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);CHKERRQ(ierr); 132b0a32e0cSBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);CHKERRQ(ierr); 133b0a32e0cSBarry Smith PetscLogObjectMemory(c,2*(nrows+1)*sizeof(int)); 134a64fbb32SBarry Smith nrows = 0; 1356eaac0f3SBarry Smith for (j=0; j<M; j++) { 136a64fbb32SBarry Smith if (rowhit[j]) { 137a64fbb32SBarry Smith c->rows[i][nrows] = j; 138a64fbb32SBarry Smith c->columnsforrow[i][nrows] = rowhit[j] - 1; 139a64fbb32SBarry Smith nrows++; 140a64fbb32SBarry Smith } 141a64fbb32SBarry Smith } 142a64fbb32SBarry Smith } else {/*-------------------------------------------------------------------------------*/ 143f158e583SBarry Smith /* slow version, using rowhit as a linked list */ 1446eaac0f3SBarry Smith int currentcol,fm,mfm; 1456eaac0f3SBarry Smith rowhit[M] = M; 146a64fbb32SBarry Smith nrows = 0; 147a64fbb32SBarry Smith /* loop over columns*/ 1486eaac0f3SBarry Smith for (j=0; j<nctot; j++) { 1496eaac0f3SBarry Smith col = cols[j]; 1506eaac0f3SBarry Smith if (col >= cstart && col < cend) { 1516eaac0f3SBarry Smith /* column is in diagonal block of matrix */ 1526eaac0f3SBarry Smith rows = A_cj + A_ci[col-cstart]; 1536eaac0f3SBarry Smith m = A_ci[col-cstart+1] - A_ci[col-cstart]; 1546eaac0f3SBarry Smith } else { 155aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 1560f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr); 157fa46199cSSatish Balay colb --; 158b3d2dc96SSatish Balay #else 1596eaac0f3SBarry Smith colb = aij->colmap[col] - 1; 160b3d2dc96SSatish Balay #endif 1616eaac0f3SBarry Smith if (colb == -1) { 1626eaac0f3SBarry Smith m = 0; 1636eaac0f3SBarry Smith } else { 1646eaac0f3SBarry Smith rows = B_cj + B_ci[colb]; 1656eaac0f3SBarry Smith m = B_ci[colb+1] - B_ci[colb]; 1666eaac0f3SBarry Smith } 1676eaac0f3SBarry Smith } 168a64fbb32SBarry Smith /* loop over columns marking them in rowhit */ 1696eaac0f3SBarry Smith fm = M; /* fm points to first entry in linked list */ 170a64fbb32SBarry Smith for (k=0; k<m; k++) { 171a64fbb32SBarry Smith currentcol = *rows++; 172a64fbb32SBarry Smith /* is it already in the list? */ 173a64fbb32SBarry Smith do { 174a64fbb32SBarry Smith mfm = fm; 175a64fbb32SBarry Smith fm = rowhit[fm]; 176a64fbb32SBarry Smith } while (fm < currentcol); 177a64fbb32SBarry Smith /* not in list so add it */ 178a64fbb32SBarry Smith if (fm != currentcol) { 179a64fbb32SBarry Smith nrows++; 180a64fbb32SBarry Smith columnsforrow[currentcol] = col; 181a64fbb32SBarry Smith /* next three lines insert new entry into linked list */ 182a64fbb32SBarry Smith rowhit[mfm] = currentcol; 183a64fbb32SBarry Smith rowhit[currentcol] = fm; 184a64fbb32SBarry Smith fm = currentcol; 185a64fbb32SBarry Smith /* fm points to present position in list since we know the columns are sorted */ 186a64fbb32SBarry Smith } else { 18729bbc08cSBarry Smith SETERRQ(PETSC_ERR_PLIB,"Invalid coloring of matrix detected"); 188a64fbb32SBarry Smith } 189a64fbb32SBarry Smith } 190a64fbb32SBarry Smith } 191a64fbb32SBarry Smith c->nrows[i] = nrows; 192b0a32e0cSBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);CHKERRQ(ierr); 193b0a32e0cSBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);CHKERRQ(ierr); 194b0a32e0cSBarry Smith PetscLogObjectMemory(c,(nrows+1)*sizeof(int)); 195a64fbb32SBarry Smith /* now store the linked list of rows into c->rows[i] */ 196a64fbb32SBarry Smith nrows = 0; 1976eaac0f3SBarry Smith fm = rowhit[M]; 198a64fbb32SBarry Smith do { 199a64fbb32SBarry Smith c->rows[i][nrows] = fm; 200a64fbb32SBarry Smith c->columnsforrow[i][nrows++] = columnsforrow[fm]; 201a64fbb32SBarry Smith fm = rowhit[fm]; 2026eaac0f3SBarry Smith } while (fm < M); 2036eaac0f3SBarry Smith } /* ---------------------------------------------------------------------------------------*/ 204606d414cSSatish Balay ierr = PetscFree(cols);CHKERRQ(ierr); 2056eaac0f3SBarry Smith } 20630b34957SBarry Smith 20730b34957SBarry Smith /* Optimize by adding the vscale, and scaleforrow[][] fields */ 20830b34957SBarry Smith /* 20930b34957SBarry Smith vscale will contain the "diagonal" on processor scalings followed by the off processor 21030b34957SBarry Smith */ 21130b34957SBarry Smith ierr = VecCreateGhost(mat->comm,aij->A->m,PETSC_DETERMINE,aij->B->n,aij->garray,&c->vscale);CHKERRQ(ierr) 212b0a32e0cSBarry Smith ierr = PetscMalloc(c->ncolors*sizeof(int*),&c->vscaleforrow);CHKERRQ(ierr); 21330b34957SBarry Smith for (k=0; k<c->ncolors; k++) { 214b0a32e0cSBarry Smith ierr = PetscMalloc((c->nrows[k]+1)*sizeof(int),&c->vscaleforrow[k]);CHKERRQ(ierr); 21530b34957SBarry Smith for (l=0; l<c->nrows[k]; l++) { 21630b34957SBarry Smith col = c->columnsforrow[k][l]; 21730b34957SBarry Smith if (col >= cstart && col < cend) { 21830b34957SBarry Smith /* column is in diagonal block of matrix */ 21930b34957SBarry Smith colb = col - cstart; 22030b34957SBarry Smith } else { 22130b34957SBarry Smith /* column is in "off-processor" part */ 22230b34957SBarry Smith #if defined (PETSC_USE_CTABLE) 22330b34957SBarry Smith ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr); 22430b34957SBarry Smith colb --; 22530b34957SBarry Smith #else 22630b34957SBarry Smith colb = aij->colmap[col] - 1; 22730b34957SBarry Smith #endif 22830b34957SBarry Smith colb += cend - cstart; 22930b34957SBarry Smith } 23030b34957SBarry Smith c->vscaleforrow[k][l] = colb; 23130b34957SBarry Smith } 23230b34957SBarry Smith } 233b9617806SBarry Smith ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr); 23430b34957SBarry Smith 235606d414cSSatish Balay ierr = PetscFree(rowhit);CHKERRQ(ierr); 236606d414cSSatish Balay ierr = PetscFree(columnsforrow);CHKERRQ(ierr); 237606d414cSSatish Balay ierr = PetscFree(ncolsonproc);CHKERRQ(ierr); 23843a90d84SBarry Smith ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr); 23943a90d84SBarry Smith ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr); 2403a40ed3dSBarry Smith PetscFunctionReturn(0); 241a64fbb32SBarry Smith } 242a64fbb32SBarry Smith 243b9617806SBarry Smith 244b9617806SBarry Smith 245b9617806SBarry Smith 246b9617806SBarry Smith 247b9617806SBarry Smith 248