1 #define PETSCMAT_DLL 2 3 #include "src/mat/impls/aij/mpi/mpiaij.h" 4 5 EXTERN PetscErrorCode CreateColmap_MPIAIJ_Private(Mat); 6 EXTERN PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat,PetscInt,PetscTruth,PetscInt*,PetscInt*[],PetscInt*[],PetscTruth*); 7 EXTERN PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat,PetscInt,PetscTruth,PetscInt*,PetscInt*[],PetscInt*[],PetscTruth*); 8 9 #undef __FUNCT__ 10 #define __FUNCT__ "MatFDColoringCreate_MPIAIJ" 11 PetscErrorCode MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c) 12 { 13 Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 14 PetscErrorCode ierr; 15 PetscMPIInt size,*ncolsonproc,*disp,nn; 16 PetscInt i,*is,n,nrows,j,k,m,*rows = 0,*A_ci,*A_cj,ncols,col; 17 PetscInt nis = iscoloring->n,nctot,*cols,*B_ci,*B_cj; 18 PetscInt *rowhit,M = mat->m,cstart = aij->cstart,cend = aij->cend,colb; 19 PetscInt *columnsforrow,l; 20 IS *isa; 21 PetscTruth done,flg; 22 23 PetscFunctionBegin; 24 if (!mat->assembled) { 25 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();"); 26 } 27 28 ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr); 29 c->M = mat->M; /* set the global rows and columns and local rows */ 30 c->N = mat->N; 31 c->m = mat->m; 32 c->rstart = aij->rstart; 33 34 c->ncolors = nis; 35 ierr = PetscMalloc(nis*sizeof(PetscInt),&c->ncolumns);CHKERRQ(ierr); 36 ierr = PetscMalloc(nis*sizeof(PetscInt*),&c->columns);CHKERRQ(ierr); 37 ierr = PetscMalloc(nis*sizeof(PetscInt),&c->nrows);CHKERRQ(ierr); 38 ierr = PetscMalloc(nis*sizeof(PetscInt*),&c->rows);CHKERRQ(ierr); 39 ierr = PetscMalloc(nis*sizeof(PetscInt*),&c->columnsforrow);CHKERRQ(ierr); 40 ierr = PetscLogObjectMemory(c,5*nis*sizeof(PetscInt));CHKERRQ(ierr); 41 42 /* Allow access to data structures of local part of matrix */ 43 if (!aij->colmap) { 44 ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 45 } 46 /* 47 Calls the _SeqAIJ() version of these routines to make sure it does not 48 get the reduced (by inodes) version of I and J 49 */ 50 ierr = MatGetColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr); 51 ierr = MatGetColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr); 52 53 ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr); 54 ierr = PetscMalloc(2*size*sizeof(PetscInt*),&ncolsonproc);CHKERRQ(ierr); 55 disp = ncolsonproc + size; 56 57 ierr = PetscMalloc((M+1)*sizeof(PetscInt),&rowhit);CHKERRQ(ierr); 58 ierr = PetscMalloc((M+1)*sizeof(PetscInt),&columnsforrow);CHKERRQ(ierr); 59 60 /* 61 Temporary option to allow for debugging/testing 62 */ 63 ierr = PetscOptionsHasName(PETSC_NULL,"-matfdcoloring_slow",&flg);CHKERRQ(ierr); 64 65 for (i=0; i<nis; i++) { 66 ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr); 67 ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr); 68 c->ncolumns[i] = n; 69 c->ncolumns[i] = n; 70 if (n) { 71 ierr = PetscMalloc(n*sizeof(PetscInt),&c->columns[i]);CHKERRQ(ierr); 72 ierr = PetscLogObjectMemory(c,n*sizeof(PetscInt));CHKERRQ(ierr); 73 ierr = PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));CHKERRQ(ierr); 74 } else { 75 c->columns[i] = 0; 76 } 77 78 /* Determine the total (parallel) number of columns of this color */ 79 nn = (PetscMPIInt)n; 80 ierr = MPI_Allgather(&nn,1,MPI_INT,ncolsonproc,1,MPI_INT,mat->comm);CHKERRQ(ierr); 81 nctot = 0; for (j=0; j<size; j++) {nctot += ncolsonproc[j];} 82 if (!nctot) { 83 ierr = PetscVerboseInfo(((PetscObject)mat,"MatFDColoringCreate_MPIAIJ: Coloring of matrix has some unneeded colors with no corresponding rows\n"));CHKERRQ(ierr); 84 } 85 86 disp[0] = 0; 87 for (j=1; j<size; j++) { 88 disp[j] = disp[j-1] + ncolsonproc[j-1]; 89 } 90 91 /* Get complete list of columns for color on each processor */ 92 ierr = PetscMalloc((nctot+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr); 93 ierr = MPI_Allgatherv(is,n,MPIU_INT,cols,ncolsonproc,disp,MPIU_INT,mat->comm);CHKERRQ(ierr); 94 95 /* 96 Mark all rows affect by these columns 97 */ 98 if (!flg) {/*-----------------------------------------------------------------------------*/ 99 /* crude, fast version */ 100 ierr = PetscMemzero(rowhit,M*sizeof(PetscInt));CHKERRQ(ierr); 101 /* loop over columns*/ 102 for (j=0; j<nctot; j++) { 103 col = cols[j]; 104 if (col >= cstart && col < cend) { 105 /* column is in diagonal block of matrix */ 106 rows = A_cj + A_ci[col-cstart]; 107 m = A_ci[col-cstart+1] - A_ci[col-cstart]; 108 } else { 109 #if defined (PETSC_USE_CTABLE) 110 ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr) 111 colb --; 112 #else 113 colb = aij->colmap[col] - 1; 114 #endif 115 if (colb == -1) { 116 m = 0; 117 } else { 118 rows = B_cj + B_ci[colb]; 119 m = B_ci[colb+1] - B_ci[colb]; 120 } 121 } 122 /* loop over columns marking them in rowhit */ 123 for (k=0; k<m; k++) { 124 rowhit[*rows++] = col + 1; 125 } 126 } 127 128 /* count the number of hits */ 129 nrows = 0; 130 for (j=0; j<M; j++) { 131 if (rowhit[j]) nrows++; 132 } 133 c->nrows[i] = nrows; 134 ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr); 135 ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr); 136 ierr = PetscLogObjectMemory(c,2*(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr); 137 nrows = 0; 138 for (j=0; j<M; j++) { 139 if (rowhit[j]) { 140 c->rows[i][nrows] = j; 141 c->columnsforrow[i][nrows] = rowhit[j] - 1; 142 nrows++; 143 } 144 } 145 } else {/*-------------------------------------------------------------------------------*/ 146 /* slow version, using rowhit as a linked list */ 147 PetscInt currentcol,fm,mfm; 148 rowhit[M] = M; 149 nrows = 0; 150 /* loop over columns*/ 151 for (j=0; j<nctot; j++) { 152 col = cols[j]; 153 if (col >= cstart && col < cend) { 154 /* column is in diagonal block of matrix */ 155 rows = A_cj + A_ci[col-cstart]; 156 m = A_ci[col-cstart+1] - A_ci[col-cstart]; 157 } else { 158 #if defined (PETSC_USE_CTABLE) 159 ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr); 160 colb --; 161 #else 162 colb = aij->colmap[col] - 1; 163 #endif 164 if (colb == -1) { 165 m = 0; 166 } else { 167 rows = B_cj + B_ci[colb]; 168 m = B_ci[colb+1] - B_ci[colb]; 169 } 170 } 171 /* loop over columns marking them in rowhit */ 172 fm = M; /* fm points to first entry in linked list */ 173 for (k=0; k<m; k++) { 174 currentcol = *rows++; 175 /* is it already in the list? */ 176 do { 177 mfm = fm; 178 fm = rowhit[fm]; 179 } while (fm < currentcol); 180 /* not in list so add it */ 181 if (fm != currentcol) { 182 nrows++; 183 columnsforrow[currentcol] = col; 184 /* next three lines insert new entry into linked list */ 185 rowhit[mfm] = currentcol; 186 rowhit[currentcol] = fm; 187 fm = currentcol; 188 /* fm points to present position in list since we know the columns are sorted */ 189 } else { 190 SETERRQ(PETSC_ERR_PLIB,"Invalid coloring of matrix detected"); 191 } 192 } 193 } 194 c->nrows[i] = nrows; 195 ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);CHKERRQ(ierr); 196 ierr = PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);CHKERRQ(ierr); 197 ierr = PetscLogObjectMemory(c,(nrows+1)*sizeof(PetscInt));CHKERRQ(ierr); 198 /* now store the linked list of rows into c->rows[i] */ 199 nrows = 0; 200 fm = rowhit[M]; 201 do { 202 c->rows[i][nrows] = fm; 203 c->columnsforrow[i][nrows++] = columnsforrow[fm]; 204 fm = rowhit[fm]; 205 } while (fm < M); 206 } /* ---------------------------------------------------------------------------------------*/ 207 ierr = PetscFree(cols);CHKERRQ(ierr); 208 } 209 210 /* Optimize by adding the vscale, and scaleforrow[][] fields */ 211 /* 212 vscale will contain the "diagonal" on processor scalings followed by the off processor 213 */ 214 ierr = VecCreateGhost(mat->comm,aij->A->m,PETSC_DETERMINE,aij->B->n,aij->garray,&c->vscale);CHKERRQ(ierr) 215 ierr = PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);CHKERRQ(ierr); 216 for (k=0; k<c->ncolors; k++) { 217 ierr = PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);CHKERRQ(ierr); 218 for (l=0; l<c->nrows[k]; l++) { 219 col = c->columnsforrow[k][l]; 220 if (col >= cstart && col < cend) { 221 /* column is in diagonal block of matrix */ 222 colb = col - cstart; 223 } else { 224 /* column is in "off-processor" part */ 225 #if defined (PETSC_USE_CTABLE) 226 ierr = PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr); 227 colb --; 228 #else 229 colb = aij->colmap[col] - 1; 230 #endif 231 colb += cend - cstart; 232 } 233 c->vscaleforrow[k][l] = colb; 234 } 235 } 236 ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr); 237 238 ierr = PetscFree(rowhit);CHKERRQ(ierr); 239 ierr = PetscFree(columnsforrow);CHKERRQ(ierr); 240 ierr = PetscFree(ncolsonproc);CHKERRQ(ierr); 241 ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);CHKERRQ(ierr); 242 ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);CHKERRQ(ierr); 243 PetscFunctionReturn(0); 244 } 245 246 247 248 249 250 251