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