1*b0a32e0cSBarry Smith /*$Id: fdmpiaij.c,v 1.36 2000/09/28 21:11:06 bsmith Exp bsmith $*/ 2a64fbb32SBarry Smith 36eaac0f3SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h" 4a64fbb32SBarry Smith #include "src/vec/vecimpl.h" 5a64fbb32SBarry Smith 6ca44d042SBarry Smith EXTERN int CreateColmap_MPIAIJ_Private(Mat); 7ca44d042SBarry Smith EXTERN int MatGetColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*); 8ca44d042SBarry Smith EXTERN int MatRestoreColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*); 9a64fbb32SBarry Smith 105615d1e5SSatish Balay #undef __FUNC__ 11*b0a32e0cSBarry Smith #define __FUNC__ "MatFDColoringCreate_MPIAIJ" 126eaac0f3SBarry Smith int MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c) 13a64fbb32SBarry Smith { 146eaac0f3SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 15f1af5d2fSBarry Smith int i,*is,n,nrows,j,k,m,*rows = 0,ierr,*A_ci,*A_cj,ncols,col; 166eaac0f3SBarry Smith int nis = iscoloring->n,*ncolsonproc,size,nctot,*cols,*disp,*B_ci,*B_cj; 176eaac0f3SBarry Smith int *rowhit,M = mat->m,cstart = aij->cstart,cend = aij->cend,colb; 1830b34957SBarry Smith int *columnsforrow,l; 19a64fbb32SBarry Smith IS *isa = iscoloring->is; 20f1af5d2fSBarry Smith PetscTruth done,flg; 21a64fbb32SBarry Smith 223a40ed3dSBarry Smith PetscFunctionBegin; 23522c5e43SBarry Smith if (!mat->assembled) { 2429bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();"); 25522c5e43SBarry Smith } 26522c5e43SBarry Smith 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; 33*b0a32e0cSBarry Smith ierr = PetscMalloc(nis*sizeof(int),&c->ncolumns);CHKERRQ(ierr); 34*b0a32e0cSBarry Smith ierr = PetscMalloc(nis*sizeof(int*),&c->columns);CHKERRQ(ierr); 35*b0a32e0cSBarry Smith ierr = PetscMalloc(nis*sizeof(int),&c->nrows);CHKERRQ(ierr); 36*b0a32e0cSBarry Smith ierr = PetscMalloc(nis*sizeof(int*),&c->rows);CHKERRQ(ierr); 37*b0a32e0cSBarry Smith ierr = PetscMalloc(nis*sizeof(int*),&c->columnsforrow);CHKERRQ(ierr); 38*b0a32e0cSBarry 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); 52*b0a32e0cSBarry Smith ierr = PetscMalloc(2*size*sizeof(int*),&ncolsonproc);CHKERRQ(ierr); 536eaac0f3SBarry Smith disp = ncolsonproc + size; 546eaac0f3SBarry Smith 55*b0a32e0cSBarry Smith ierr = PetscMalloc((M+1)*sizeof(int),&rowhit);CHKERRQ(ierr); 56*b0a32e0cSBarry 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 */ 61*b0a32e0cSBarry 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) { 69*b0a32e0cSBarry Smith ierr = PetscMalloc(n*sizeof(int),&c->columns[i]);CHKERRQ(ierr); 70*b0a32e0cSBarry 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];} 7929bbc08cSBarry Smith if (!nctot) SETERRQ(PETSC_ERR_PLIB,"Invalid coloring of matrix detected"); 806eaac0f3SBarry Smith 816eaac0f3SBarry Smith disp[0] = 0; 826eaac0f3SBarry Smith for (j=1; j<size; j++) { 836eaac0f3SBarry Smith disp[j] = disp[j-1] + ncolsonproc[j-1]; 846eaac0f3SBarry Smith } 856eaac0f3SBarry Smith 866eaac0f3SBarry Smith /* Get complete list of columns for color on each processor */ 87*b0a32e0cSBarry Smith ierr = PetscMalloc(nctot*sizeof(int),&cols);CHKERRQ(ierr); 88ca161407SBarry Smith ierr = MPI_Allgatherv(is,n,MPI_INT,cols,ncolsonproc,disp,MPI_INT,mat->comm);CHKERRQ(ierr); 896eaac0f3SBarry Smith 906eaac0f3SBarry Smith /* 916eaac0f3SBarry Smith Mark all rows affect by these columns 926eaac0f3SBarry Smith */ 936eaac0f3SBarry Smith if (flg) {/*-----------------------------------------------------------------------------*/ 946eaac0f3SBarry Smith /* crude, slow version */ 95549d3d68SSatish Balay ierr = PetscMemzero(rowhit,M*sizeof(int));CHKERRQ(ierr); 96a64fbb32SBarry Smith /* loop over columns*/ 976eaac0f3SBarry Smith for (j=0; j<nctot; j++) { 986eaac0f3SBarry Smith col = cols[j]; 996eaac0f3SBarry Smith if (col >= cstart && col < cend) { 1006eaac0f3SBarry Smith /* column is in diagonal block of matrix */ 1016eaac0f3SBarry Smith rows = A_cj + A_ci[col-cstart]; 1026eaac0f3SBarry Smith m = A_ci[col-cstart+1] - A_ci[col-cstart]; 1036eaac0f3SBarry Smith } else { 104aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 1050f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr) 106fa46199cSSatish Balay colb --; 107b3d2dc96SSatish Balay #else 1086eaac0f3SBarry Smith colb = aij->colmap[col] - 1; 109b3d2dc96SSatish Balay #endif 1106eaac0f3SBarry Smith if (colb == -1) { 1116eaac0f3SBarry Smith m = 0; 1126eaac0f3SBarry Smith } else { 1136eaac0f3SBarry Smith rows = B_cj + B_ci[colb]; 1146eaac0f3SBarry Smith m = B_ci[colb+1] - B_ci[colb]; 1156eaac0f3SBarry Smith } 1166eaac0f3SBarry Smith } 117a64fbb32SBarry Smith /* loop over columns marking them in rowhit */ 118a64fbb32SBarry Smith for (k=0; k<m; k++) { 119a64fbb32SBarry Smith rowhit[*rows++] = col + 1; 120a64fbb32SBarry Smith } 121a64fbb32SBarry Smith } 1226eaac0f3SBarry Smith 123a64fbb32SBarry Smith /* count the number of hits */ 124a64fbb32SBarry Smith nrows = 0; 1256eaac0f3SBarry Smith for (j=0; j<M; j++) { 126a64fbb32SBarry Smith if (rowhit[j]) nrows++; 127a64fbb32SBarry Smith } 128a64fbb32SBarry Smith c->nrows[i] = nrows; 129*b0a32e0cSBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);CHKERRQ(ierr); 130*b0a32e0cSBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);CHKERRQ(ierr); 131*b0a32e0cSBarry Smith PetscLogObjectMemory(c,2*(nrows+1)*sizeof(int)); 132a64fbb32SBarry Smith nrows = 0; 1336eaac0f3SBarry Smith for (j=0; j<M; j++) { 134a64fbb32SBarry Smith if (rowhit[j]) { 135a64fbb32SBarry Smith c->rows[i][nrows] = j; 136a64fbb32SBarry Smith c->columnsforrow[i][nrows] = rowhit[j] - 1; 137a64fbb32SBarry Smith nrows++; 138a64fbb32SBarry Smith } 139a64fbb32SBarry Smith } 140a64fbb32SBarry Smith } else {/*-------------------------------------------------------------------------------*/ 141a64fbb32SBarry Smith /* efficient version, using rowhit as a linked list */ 1426eaac0f3SBarry Smith int currentcol,fm,mfm; 1436eaac0f3SBarry Smith rowhit[M] = M; 144a64fbb32SBarry Smith nrows = 0; 145a64fbb32SBarry Smith /* loop over columns*/ 1466eaac0f3SBarry Smith for (j=0; j<nctot; j++) { 1476eaac0f3SBarry Smith col = cols[j]; 1486eaac0f3SBarry Smith if (col >= cstart && col < cend) { 1496eaac0f3SBarry Smith /* column is in diagonal block of matrix */ 1506eaac0f3SBarry Smith rows = A_cj + A_ci[col-cstart]; 1516eaac0f3SBarry Smith m = A_ci[col-cstart+1] - A_ci[col-cstart]; 1526eaac0f3SBarry Smith } else { 153aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 1540f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr); 155fa46199cSSatish Balay colb --; 156b3d2dc96SSatish Balay #else 1576eaac0f3SBarry Smith colb = aij->colmap[col] - 1; 158b3d2dc96SSatish Balay #endif 1596eaac0f3SBarry Smith if (colb == -1) { 1606eaac0f3SBarry Smith m = 0; 1616eaac0f3SBarry Smith } else { 1626eaac0f3SBarry Smith rows = B_cj + B_ci[colb]; 1636eaac0f3SBarry Smith m = B_ci[colb+1] - B_ci[colb]; 1646eaac0f3SBarry Smith } 1656eaac0f3SBarry Smith } 166a64fbb32SBarry Smith /* loop over columns marking them in rowhit */ 1676eaac0f3SBarry Smith fm = M; /* fm points to first entry in linked list */ 168a64fbb32SBarry Smith for (k=0; k<m; k++) { 169a64fbb32SBarry Smith currentcol = *rows++; 170a64fbb32SBarry Smith /* is it already in the list? */ 171a64fbb32SBarry Smith do { 172a64fbb32SBarry Smith mfm = fm; 173a64fbb32SBarry Smith fm = rowhit[fm]; 174a64fbb32SBarry Smith } while (fm < currentcol); 175a64fbb32SBarry Smith /* not in list so add it */ 176a64fbb32SBarry Smith if (fm != currentcol) { 177a64fbb32SBarry Smith nrows++; 178a64fbb32SBarry Smith columnsforrow[currentcol] = col; 179a64fbb32SBarry Smith /* next three lines insert new entry into linked list */ 180a64fbb32SBarry Smith rowhit[mfm] = currentcol; 181a64fbb32SBarry Smith rowhit[currentcol] = fm; 182a64fbb32SBarry Smith fm = currentcol; 183a64fbb32SBarry Smith /* fm points to present position in list since we know the columns are sorted */ 184a64fbb32SBarry Smith } else { 18529bbc08cSBarry Smith SETERRQ(PETSC_ERR_PLIB,"Invalid coloring of matrix detected"); 186a64fbb32SBarry Smith } 187a64fbb32SBarry Smith } 188a64fbb32SBarry Smith } 189a64fbb32SBarry Smith c->nrows[i] = nrows; 190*b0a32e0cSBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);CHKERRQ(ierr); 191*b0a32e0cSBarry Smith ierr = PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);CHKERRQ(ierr); 192*b0a32e0cSBarry Smith PetscLogObjectMemory(c,(nrows+1)*sizeof(int)); 193a64fbb32SBarry Smith /* now store the linked list of rows into c->rows[i] */ 194a64fbb32SBarry Smith nrows = 0; 1956eaac0f3SBarry Smith fm = rowhit[M]; 196a64fbb32SBarry Smith do { 197a64fbb32SBarry Smith c->rows[i][nrows] = fm; 198a64fbb32SBarry Smith c->columnsforrow[i][nrows++] = columnsforrow[fm]; 199a64fbb32SBarry Smith fm = rowhit[fm]; 2006eaac0f3SBarry Smith } while (fm < M); 2016eaac0f3SBarry Smith } /* ---------------------------------------------------------------------------------------*/ 202606d414cSSatish Balay ierr = PetscFree(cols);CHKERRQ(ierr); 2036eaac0f3SBarry Smith } 20430b34957SBarry Smith 20530b34957SBarry Smith /* Optimize by adding the vscale, and scaleforrow[][] fields */ 20630b34957SBarry Smith /* 20730b34957SBarry Smith vscale will contain the "diagonal" on processor scalings followed by the off processor 20830b34957SBarry Smith */ 20930b34957SBarry Smith ierr = VecCreateGhost(mat->comm,aij->A->m,PETSC_DETERMINE,aij->B->n,aij->garray,&c->vscale);CHKERRQ(ierr) 210*b0a32e0cSBarry Smith ierr = PetscMalloc(c->ncolors*sizeof(int*),&c->vscaleforrow);CHKERRQ(ierr); 21130b34957SBarry Smith for (k=0; k<c->ncolors; k++) { 212*b0a32e0cSBarry Smith ierr = PetscMalloc((c->nrows[k]+1)*sizeof(int),&c->vscaleforrow[k]);CHKERRQ(ierr); 21330b34957SBarry Smith for (l=0; l<c->nrows[k]; l++) { 21430b34957SBarry Smith col = c->columnsforrow[k][l]; 21530b34957SBarry Smith if (col >= cstart && col < cend) { 21630b34957SBarry Smith /* column is in diagonal block of matrix */ 21730b34957SBarry Smith colb = col - cstart; 21830b34957SBarry Smith } else { 21930b34957SBarry Smith /* column is in "off-processor" part */ 22030b34957SBarry Smith #if defined (PETSC_USE_CTABLE) 22130b34957SBarry Smith ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr); 22230b34957SBarry Smith colb --; 22330b34957SBarry Smith #else 22430b34957SBarry Smith colb = aij->colmap[col] - 1; 22530b34957SBarry Smith #endif 22630b34957SBarry Smith colb += cend - cstart; 22730b34957SBarry Smith } 22830b34957SBarry Smith c->vscaleforrow[k][l] = colb; 22930b34957SBarry Smith } 23030b34957SBarry Smith } 23130b34957SBarry Smith 232606d414cSSatish Balay ierr = PetscFree(rowhit);CHKERRQ(ierr); 233606d414cSSatish Balay ierr = PetscFree(columnsforrow);CHKERRQ(ierr); 234606d414cSSatish Balay ierr = PetscFree(ncolsonproc);CHKERRQ(ierr); 23543a90d84SBarry Smith ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr); 23643a90d84SBarry Smith ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr); 2373a40ed3dSBarry Smith PetscFunctionReturn(0); 238a64fbb32SBarry Smith } 239a64fbb32SBarry Smith 240