1 2 #ifdef PETSC_RCS_HEADER 3 static char vcid[] = "$Id: fdmpiaij.c,v 1.10 1997/07/09 20:54:04 balay 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" /* ADIC Ignore */ 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 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 c->ncolumns = (int *) PetscMalloc( nis*sizeof(int) ); CHKPTRQ(c->ncolumns); 33 c->columns = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->columns); 34 c->nrows = (int *) PetscMalloc( nis*sizeof(int) ); CHKPTRQ(c->nrows); 35 c->rows = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->rows); 36 c->columnsforrow = (int **) PetscMalloc( nis*sizeof(int *)); CHKPTRQ(c->columnsforrow); 37 PLogObjectMemory(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 MPI_Comm_size(mat->comm,&size); 51 ncolsonproc = (int *) PetscMalloc( 2*size*sizeof(int *) ); CHKPTRQ(ncolsonproc); 52 disp = ncolsonproc + size; 53 54 rowhit = (int *) PetscMalloc( (M+1)*sizeof(int) ); CHKPTRQ(rowhit); 55 columnsforrow = (int *) PetscMalloc( (M+1)*sizeof(int) );CHKPTRQ(columnsforrow); 56 57 /* 58 Temporary option to allow for debugging/testing 59 */ 60 ierr = OptionsHasName(0,"-matfdcoloring_slow",&flg); 61 62 for ( i=0; i<nis; i++ ) { 63 ierr = ISGetSize(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 c->columns[i] = (int *) PetscMalloc( n*sizeof(int) ); CHKPTRQ(c->columns[i]); 69 PLogObjectMemory(c,n*sizeof(int)); 70 PetscMemcpy(c->columns[i],is,n*sizeof(int)); 71 } else { 72 c->columns[i] = 0; 73 } 74 75 /* Determine the total (parallel) number of columns of this color */ 76 MPI_Allgather(&n,1,MPI_INT,ncolsonproc,1,MPI_INT,mat->comm); 77 nctot = 0; for ( j=0; j<size; j++ ) {nctot += ncolsonproc[j];} 78 if (!nctot) SETERRQ(1,0,"Invalid coloring"); 79 80 disp[0] = 0; 81 for ( j=1; j<size; j++ ) { 82 disp[j] = disp[j-1] + ncolsonproc[j-1]; 83 } 84 85 /* Get complete list of columns for color on each processor */ 86 cols = (int *) PetscMalloc( nctot*sizeof(int) ); CHKPTRQ(cols); 87 MPI_Allgatherv(is,n,MPI_INT,cols,ncolsonproc,disp,MPI_INT,mat->comm); 88 89 /* 90 for ( j=0; j<nctot; j++ ) { 91 printf("color %d %d col %d\n",i,j,cols[j]); 92 } 93 */ 94 95 /* 96 Mark all rows affect by these columns 97 */ 98 if (flg) {/*-----------------------------------------------------------------------------*/ 99 /* crude, slow version */ 100 PetscMemzero(rowhit,M*sizeof(int)); 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 colb = aij->colmap[col] - 1; 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 /* 124 printf("for col %d found rows \n",i); 125 for ( j=0; j<M; j++ ) printf("rhow hit %d %d\n",j,rowhit[j]); 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 c->rows[i] = (int *) PetscMalloc((nrows+1)*sizeof(int)); CHKPTRQ(c->rows[i]); 135 c->columnsforrow[i] = (int *) PetscMalloc((nrows+1)*sizeof(int)); CHKPTRQ(c->columnsforrow[i]); 136 PLogObjectMemory(c,2*(nrows+1)*sizeof(int)); 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 /* efficient version, using rowhit as a linked list */ 147 int 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 colb = aij->colmap[col] - 1; 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(1,0,"Invalid coloring"); 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 PetscFree(cols); 203 } 204 PetscFree(rowhit); 205 PetscFree(columnsforrow); 206 PetscFree(ncolsonproc); 207 ierr = MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done); CHKERRQ(ierr); 208 ierr = MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done); CHKERRQ(ierr); 209 210 c->scale = (Scalar *) PetscMalloc( 2*mat->N*sizeof(Scalar) ); CHKPTRQ(c->scale); 211 PLogObjectMemory(c,2*mat->N*sizeof(Scalar)); 212 c->wscale = c->scale + mat->N; 213 return 0; 214 } 215 216