1 2 #ifdef PETSC_RCS_HEADER 3 static char vcid[] = "$Id: fdmpiaij.c,v 1.12 1997/08/22 15:13:39 bsmith Exp bsmith $"; 4 #endif 5 6 #include "src/mat/impls/aij/mpi/mpiaij.h" 7 #include "src/vec/vecimpl.h" 8 #include "petsc.h" 9 10 extern int CreateColmap_MPIAIJ_Private(Mat); 11 extern int MatGetColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*); 12 extern int MatRestoreColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*); 13 14 #undef __FUNC__ 15 #define __FUNC__ "MatFDColoringCreate_MPIAIJ" 16 int MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c) 17 { 18 Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 19 int i,*is,n,nrows,j,k,m,*rows = 0,ierr,*A_ci,*A_cj,ncols,col,flg; 20 int nis = iscoloring->n,*ncolsonproc,size,nctot,*cols,*disp,*B_ci,*B_cj; 21 int *rowhit, M = mat->m,cstart = aij->cstart, cend = aij->cend,colb; 22 int *columnsforrow; 23 IS *isa = iscoloring->is; 24 PetscTruth done; 25 26 PetscFunctionBegin; 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 MPI_Comm_size(mat->comm,&size); 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(0,"-matfdcoloring_slow",&flg); 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 PetscMemcpy(c->columns[i],is,n*sizeof(int)); 72 } else { 73 c->columns[i] = 0; 74 } 75 76 /* Determine the total (parallel) number of columns of this color */ 77 MPI_Allgather(&n,1,MPI_INT,ncolsonproc,1,MPI_INT,mat->comm); 78 nctot = 0; for ( j=0; j<size; j++ ) {nctot += ncolsonproc[j];} 79 if (!nctot) SETERRQ(1,0,"Invalid coloring"); 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 MPI_Allgatherv(is,n,MPI_INT,cols,ncolsonproc,disp,MPI_INT,mat->comm); 89 90 /* 91 for ( j=0; j<nctot; j++ ) { 92 printf("color %d %d col %d\n",i,j,cols[j]); 93 } 94 */ 95 96 /* 97 Mark all rows affect by these columns 98 */ 99 if (flg) {/*-----------------------------------------------------------------------------*/ 100 /* crude, slow version */ 101 PetscMemzero(rowhit,M*sizeof(int)); 102 /* loop over columns*/ 103 for ( j=0; j<nctot; j++ ) { 104 col = cols[j]; 105 if (col >= cstart && col < cend) { 106 /* column is in diagonal block of matrix */ 107 rows = A_cj + A_ci[col-cstart]; 108 m = A_ci[col-cstart+1] - A_ci[col-cstart]; 109 } else { 110 colb = aij->colmap[col] - 1; 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 /* 125 printf("for col %d found rows \n",i); 126 for ( j=0; j<M; j++ ) printf("rhow hit %d %d\n",j,rowhit[j]); 127 */ 128 129 /* count the number of hits */ 130 nrows = 0; 131 for ( j=0; j<M; j++ ) { 132 if (rowhit[j]) nrows++; 133 } 134 c->nrows[i] = nrows; 135 c->rows[i] = (int *) PetscMalloc((nrows+1)*sizeof(int)); CHKPTRQ(c->rows[i]); 136 c->columnsforrow[i] = (int *) PetscMalloc((nrows+1)*sizeof(int)); CHKPTRQ(c->columnsforrow[i]); 137 PLogObjectMemory(c,2*(nrows+1)*sizeof(int)); 138 nrows = 0; 139 for ( j=0; j<M; j++ ) { 140 if (rowhit[j]) { 141 c->rows[i][nrows] = j; 142 c->columnsforrow[i][nrows] = rowhit[j] - 1; 143 nrows++; 144 } 145 } 146 } else {/*-------------------------------------------------------------------------------*/ 147 /* efficient version, using rowhit as a linked list */ 148 int currentcol,fm,mfm; 149 rowhit[M] = M; 150 nrows = 0; 151 /* loop over columns*/ 152 for ( j=0; j<nctot; j++ ) { 153 col = cols[j]; 154 if (col >= cstart && col < cend) { 155 /* column is in diagonal block of matrix */ 156 rows = A_cj + A_ci[col-cstart]; 157 m = A_ci[col-cstart+1] - A_ci[col-cstart]; 158 } else { 159 colb = aij->colmap[col] - 1; 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(1,0,"Invalid coloring"); 187 } 188 } 189 } 190 c->nrows[i] = nrows; 191 c->rows[i] = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->rows[i]); 192 c->columnsforrow[i] = (int *)PetscMalloc((nrows+1)*sizeof(int));CHKPTRQ(c->columnsforrow[i]); 193 PLogObjectMemory(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 PetscFree(cols); 204 } 205 PetscFree(rowhit); 206 PetscFree(columnsforrow); 207 PetscFree(ncolsonproc); 208 ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done); CHKERRQ(ierr); 209 ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done); CHKERRQ(ierr); 210 211 c->scale = (Scalar *) PetscMalloc( 2*mat->N*sizeof(Scalar) ); CHKPTRQ(c->scale); 212 PLogObjectMemory(c,2*mat->N*sizeof(Scalar)); 213 c->wscale = c->scale + mat->N; 214 PetscFunctionReturn(0); 215 } 216 217