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