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