1 2 #include <../src/mat/impls/aij/mpi/mpiaij.h> 3 #include <../src/mat/impls/baij/mpi/mpibaij.h> 4 #include <petsc/private/isimpl.h> 5 6 #undef __FUNCT__ 7 #define __FUNCT__ "MatFDColoringApply_BAIJ" 8 PetscErrorCode MatFDColoringApply_BAIJ(Mat J,MatFDColoring coloring,Vec x1,void *sctx) 9 { 10 PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void*))coloring->f; 11 PetscErrorCode ierr; 12 PetscInt k,cstart,cend,l,row,col,nz,spidx,i,j; 13 PetscScalar dx=0.0,*w3_array,*dy_i,*dy=coloring->dy; 14 PetscScalar *vscale_array; 15 const PetscScalar *xx; 16 PetscReal epsilon=coloring->error_rel,umin=coloring->umin,unorm; 17 Vec w1=coloring->w1,w2=coloring->w2,w3,vscale=coloring->vscale; 18 void *fctx=coloring->fctx; 19 PetscInt ctype=coloring->ctype,nxloc,nrows_k; 20 PetscScalar *valaddr; 21 MatEntry *Jentry=coloring->matentry; 22 MatEntry2 *Jentry2=coloring->matentry2; 23 const PetscInt ncolors=coloring->ncolors,*ncolumns=coloring->ncolumns,*nrows=coloring->nrows; 24 PetscInt bs=J->rmap->bs; 25 26 PetscFunctionBegin; 27 /* (1) Set w1 = F(x1) */ 28 if (!coloring->fset) { 29 ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 30 ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr); 31 ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 32 } else { 33 coloring->fset = PETSC_FALSE; 34 } 35 36 /* (2) Compute vscale = 1./dx - the local scale factors, including ghost points */ 37 ierr = VecGetLocalSize(x1,&nxloc);CHKERRQ(ierr); 38 if (coloring->htype[0] == 'w') { 39 /* vscale = dx is a constant scalar */ 40 ierr = VecNorm(x1,NORM_2,&unorm);CHKERRQ(ierr); 41 dx = 1.0/(PetscSqrtReal(1.0 + unorm)*epsilon); 42 } else { 43 ierr = VecGetArrayRead(x1,&xx);CHKERRQ(ierr); 44 ierr = VecGetArray(vscale,&vscale_array);CHKERRQ(ierr); 45 for (col=0; col<nxloc; col++) { 46 dx = xx[col]; 47 if (PetscAbsScalar(dx) < umin) { 48 if (PetscRealPart(dx) >= 0.0) dx = umin; 49 else if (PetscRealPart(dx) < 0.0 ) dx = -umin; 50 } 51 dx *= epsilon; 52 vscale_array[col] = 1.0/dx; 53 } 54 ierr = VecRestoreArrayRead(x1,&xx);CHKERRQ(ierr); 55 ierr = VecRestoreArray(vscale,&vscale_array);CHKERRQ(ierr); 56 } 57 if (ctype == IS_COLORING_GLOBAL && coloring->htype[0] == 'd') { 58 ierr = VecGhostUpdateBegin(vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 59 ierr = VecGhostUpdateEnd(vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 60 } 61 62 /* (3) Loop over each color */ 63 if (!coloring->w3) { 64 ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr); 65 ierr = PetscLogObjectParent((PetscObject)coloring,(PetscObject)coloring->w3);CHKERRQ(ierr); 66 } 67 w3 = coloring->w3; 68 69 ierr = VecGetOwnershipRange(x1,&cstart,&cend);CHKERRQ(ierr); /* used by ghosted vscale */ 70 if (vscale) { 71 ierr = VecGetArray(vscale,&vscale_array);CHKERRQ(ierr); 72 } 73 nz = 0; 74 for (k=0; k<ncolors; k++) { 75 coloring->currentcolor = k; 76 77 /* 78 (3-1) Loop over each column associated with color 79 adding the perturbation to the vector w3 = x1 + dx. 80 */ 81 ierr = VecCopy(x1,w3);CHKERRQ(ierr); 82 dy_i = dy; 83 for (i=0; i<bs; i++) { /* Loop over a block of columns */ 84 ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr); 85 if (ctype == IS_COLORING_GLOBAL) w3_array -= cstart; /* shift pointer so global index can be used */ 86 if (coloring->htype[0] == 'w') { 87 for (l=0; l<ncolumns[k]; l++) { 88 col = i + bs*coloring->columns[k][l]; /* local column (in global index!) of the matrix we are probing for */ 89 w3_array[col] += 1.0/dx; 90 if (i) w3_array[col-1] -= 1.0/dx; /* resume original w3[col-1] */ 91 } 92 } else { /* htype == 'ds' */ 93 vscale_array -= cstart; /* shift pointer so global index can be used */ 94 for (l=0; l<ncolumns[k]; l++) { 95 col = i + bs*coloring->columns[k][l]; /* local column (in global index!) of the matrix we are probing for */ 96 w3_array[col] += 1.0/vscale_array[col]; 97 if (i) w3_array[col-1] -= 1.0/vscale_array[col-1]; /* resume original w3[col-1] */ 98 } 99 vscale_array += cstart; 100 } 101 if (ctype == IS_COLORING_GLOBAL) w3_array += cstart; 102 ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr); 103 104 /* 105 (3-2) Evaluate function at w3 = x1 + dx (here dx is a vector of perturbations) 106 w2 = F(x1 + dx) - F(x1) 107 */ 108 ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 109 ierr = VecPlaceArray(w2,dy_i);CHKERRQ(ierr); /* place w2 to the array dy_i */ 110 ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr); 111 ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 112 ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr); 113 ierr = VecResetArray(w2);CHKERRQ(ierr); 114 dy_i += nxloc; /* points to dy+i*nxloc */ 115 } 116 117 /* 118 (3-3) Loop over rows of vector, putting results into Jacobian matrix 119 */ 120 nrows_k = nrows[k]; 121 if (coloring->htype[0] == 'w') { 122 for (l=0; l<nrows_k; l++) { 123 row = bs*Jentry2[nz].row; /* local row index */ 124 valaddr = Jentry2[nz++].valaddr; 125 spidx = 0; 126 dy_i = dy; 127 for (i=0; i<bs; i++) { /* column of the block */ 128 for (j=0; j<bs; j++) { /* row of the block */ 129 valaddr[spidx++] = dy_i[row+j]*dx; 130 } 131 dy_i += nxloc; /* points to dy+i*nxloc */ 132 } 133 } 134 } else { /* htype == 'ds' */ 135 for (l=0; l<nrows_k; l++) { 136 row = bs*Jentry[nz].row; /* local row index */ 137 col = bs*Jentry[nz].col; /* local column index */ 138 valaddr = Jentry[nz++].valaddr; 139 spidx = 0; 140 dy_i = dy; 141 for (i=0; i<bs; i++) { /* column of the block */ 142 for (j=0; j<bs; j++) { /* row of the block */ 143 valaddr[spidx++] = dy_i[row+j]*vscale_array[col+i]; 144 } 145 dy_i += nxloc; /* points to dy+i*nxloc */ 146 } 147 } 148 } 149 } 150 ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 151 ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 152 if (vscale) { 153 ierr = VecRestoreArray(vscale,&vscale_array);CHKERRQ(ierr); 154 } 155 156 coloring->currentcolor = -1; 157 PetscFunctionReturn(0); 158 } 159 160 #undef __FUNCT__ 161 #define __FUNCT__ "MatFDColoringApply_AIJ" 162 PetscErrorCode MatFDColoringApply_AIJ(Mat J,MatFDColoring coloring,Vec x1,void *sctx) 163 { 164 PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void*))coloring->f; 165 PetscErrorCode ierr; 166 PetscInt k,cstart,cend,l,row,col,nz; 167 PetscScalar dx=0.0,*y,*w3_array; 168 const PetscScalar *xx; 169 PetscScalar *vscale_array; 170 PetscReal epsilon=coloring->error_rel,umin=coloring->umin,unorm; 171 Vec w1=coloring->w1,w2=coloring->w2,w3,vscale=coloring->vscale; 172 void *fctx=coloring->fctx; 173 PetscInt ctype=coloring->ctype,nxloc,nrows_k; 174 MatEntry *Jentry=coloring->matentry; 175 MatEntry2 *Jentry2=coloring->matentry2; 176 const PetscInt ncolors=coloring->ncolors,*ncolumns=coloring->ncolumns,*nrows=coloring->nrows; 177 178 PetscFunctionBegin; 179 /* (1) Set w1 = F(x1) */ 180 if (!coloring->fset) { 181 ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 182 ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr); 183 ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 184 } else { 185 coloring->fset = PETSC_FALSE; 186 } 187 188 /* (2) Compute vscale = 1./dx - the local scale factors, including ghost points */ 189 if (coloring->htype[0] == 'w') { 190 /* vscale = 1./dx is a constant scalar */ 191 ierr = VecNorm(x1,NORM_2,&unorm);CHKERRQ(ierr); 192 dx = 1.0/(PetscSqrtReal(1.0 + unorm)*epsilon); 193 } else { 194 ierr = VecGetLocalSize(x1,&nxloc);CHKERRQ(ierr); 195 ierr = VecGetArrayRead(x1,&xx);CHKERRQ(ierr); 196 ierr = VecGetArray(vscale,&vscale_array);CHKERRQ(ierr); 197 for (col=0; col<nxloc; col++) { 198 dx = xx[col]; 199 if (PetscAbsScalar(dx) < umin) { 200 if (PetscRealPart(dx) >= 0.0) dx = umin; 201 else if (PetscRealPart(dx) < 0.0 ) dx = -umin; 202 } 203 dx *= epsilon; 204 vscale_array[col] = 1.0/dx; 205 } 206 ierr = VecRestoreArrayRead(x1,&xx);CHKERRQ(ierr); 207 ierr = VecRestoreArray(vscale,&vscale_array);CHKERRQ(ierr); 208 } 209 if (ctype == IS_COLORING_GLOBAL && coloring->htype[0] == 'd') { 210 ierr = VecGhostUpdateBegin(vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 211 ierr = VecGhostUpdateEnd(vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 212 } 213 214 /* (3) Loop over each color */ 215 if (!coloring->w3) { 216 ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr); 217 ierr = PetscLogObjectParent((PetscObject)coloring,(PetscObject)coloring->w3);CHKERRQ(ierr); 218 } 219 w3 = coloring->w3; 220 221 ierr = VecGetOwnershipRange(x1,&cstart,&cend);CHKERRQ(ierr); /* used by ghosted vscale */ 222 if (vscale) { 223 ierr = VecGetArray(vscale,&vscale_array);CHKERRQ(ierr); 224 } 225 nz = 0; 226 227 if (coloring->bcols > 1) { /* use blocked insertion of Jentry */ 228 PetscInt i,m=J->rmap->n,nbcols,bcols=coloring->bcols; 229 PetscScalar *dy=coloring->dy,*dy_k; 230 231 nbcols = 0; 232 for (k=0; k<ncolors; k+=bcols) { 233 coloring->currentcolor = k; 234 235 /* 236 (3-1) Loop over each column associated with color 237 adding the perturbation to the vector w3 = x1 + dx. 238 */ 239 240 dy_k = dy; 241 if (k + bcols > ncolors) bcols = ncolors - k; 242 for (i=0; i<bcols; i++) { 243 244 ierr = VecCopy(x1,w3);CHKERRQ(ierr); 245 ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr); 246 if (ctype == IS_COLORING_GLOBAL) w3_array -= cstart; /* shift pointer so global index can be used */ 247 if (coloring->htype[0] == 'w') { 248 for (l=0; l<ncolumns[k+i]; l++) { 249 col = coloring->columns[k+i][l]; /* local column (in global index!) of the matrix we are probing for */ 250 w3_array[col] += 1.0/dx; 251 } 252 } else { /* htype == 'ds' */ 253 vscale_array -= cstart; /* shift pointer so global index can be used */ 254 for (l=0; l<ncolumns[k+i]; l++) { 255 col = coloring->columns[k+i][l]; /* local column (in global index!) of the matrix we are probing for */ 256 w3_array[col] += 1.0/vscale_array[col]; 257 } 258 vscale_array += cstart; 259 } 260 if (ctype == IS_COLORING_GLOBAL) w3_array += cstart; 261 ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr); 262 263 /* 264 (3-2) Evaluate function at w3 = x1 + dx (here dx is a vector of perturbations) 265 w2 = F(x1 + dx) - F(x1) 266 */ 267 ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 268 ierr = VecPlaceArray(w2,dy_k);CHKERRQ(ierr); /* place w2 to the array dy_i */ 269 ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr); 270 ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 271 ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr); 272 ierr = VecResetArray(w2);CHKERRQ(ierr); 273 dy_k += m; /* points to dy+i*nxloc */ 274 } 275 276 /* 277 (3-3) Loop over block rows of vector, putting results into Jacobian matrix 278 */ 279 nrows_k = nrows[nbcols++]; 280 ierr = VecGetArray(w2,&y);CHKERRQ(ierr); 281 282 if (coloring->htype[0] == 'w') { 283 for (l=0; l<nrows_k; l++) { 284 row = Jentry2[nz].row; /* local row index */ 285 *(Jentry2[nz++].valaddr) = dy[row]*dx; 286 } 287 } else { /* htype == 'ds' */ 288 for (l=0; l<nrows_k; l++) { 289 row = Jentry[nz].row; /* local row index */ 290 *(Jentry[nz].valaddr) = dy[row]*vscale_array[Jentry[nz].col]; 291 nz++; 292 } 293 } 294 ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr); 295 } 296 } else { /* bcols == 1 */ 297 for (k=0; k<ncolors; k++) { 298 coloring->currentcolor = k; 299 300 /* 301 (3-1) Loop over each column associated with color 302 adding the perturbation to the vector w3 = x1 + dx. 303 */ 304 ierr = VecCopy(x1,w3);CHKERRQ(ierr); 305 ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr); 306 if (ctype == IS_COLORING_GLOBAL) w3_array -= cstart; /* shift pointer so global index can be used */ 307 if (coloring->htype[0] == 'w') { 308 for (l=0; l<ncolumns[k]; l++) { 309 col = coloring->columns[k][l]; /* local column (in global index!) of the matrix we are probing for */ 310 w3_array[col] += 1.0/dx; 311 } 312 } else { /* htype == 'ds' */ 313 vscale_array -= cstart; /* shift pointer so global index can be used */ 314 for (l=0; l<ncolumns[k]; l++) { 315 col = coloring->columns[k][l]; /* local column (in global index!) of the matrix we are probing for */ 316 w3_array[col] += 1.0/vscale_array[col]; 317 } 318 vscale_array += cstart; 319 } 320 if (ctype == IS_COLORING_GLOBAL) w3_array += cstart; 321 ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr); 322 323 /* 324 (3-2) Evaluate function at w3 = x1 + dx (here dx is a vector of perturbations) 325 w2 = F(x1 + dx) - F(x1) 326 */ 327 ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 328 ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr); 329 ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 330 ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr); 331 332 /* 333 (3-3) Loop over rows of vector, putting results into Jacobian matrix 334 */ 335 nrows_k = nrows[k]; 336 ierr = VecGetArray(w2,&y);CHKERRQ(ierr); 337 if (coloring->htype[0] == 'w') { 338 for (l=0; l<nrows_k; l++) { 339 row = Jentry2[nz].row; /* local row index */ 340 *(Jentry2[nz++].valaddr) = y[row]*dx; 341 } 342 } else { /* htype == 'ds' */ 343 for (l=0; l<nrows_k; l++) { 344 row = Jentry[nz].row; /* local row index */ 345 *(Jentry[nz].valaddr) = y[row]*vscale_array[Jentry[nz].col]; 346 nz++; 347 } 348 } 349 ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr); 350 } 351 } 352 353 ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 354 ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 355 if (vscale) { 356 ierr = VecRestoreArray(vscale,&vscale_array);CHKERRQ(ierr); 357 } 358 coloring->currentcolor = -1; 359 PetscFunctionReturn(0); 360 } 361 362 /* 363 MarkRowsForCol_private - Mark all rows affect by the input columns 364 Input Parameters: 365 nctot - number of columns 366 cols - column indices 367 mat, ...,colmap - data structures in MatFDColoringSetUp_MPIXAIJ() 368 nrows_i_out - num of rows marked 369 370 Output Parameters: 371 nrows_i_out - updated num of rows marked 372 */ 373 #undef __FUNCT__ 374 #define __FUNCT__ "MarkRowsForCol_private" 375 PETSC_STATIC_INLINE PetscErrorCode MarkRowsForCol_private(PetscInt nctot,PetscInt *cols, 376 Mat mat,ISColoring iscoloring,MatFDColoring c,const PetscInt *ltog, 377 const PetscInt *A_ci,const PetscInt *A_cj,PetscScalar *A_val, 378 const PetscInt *B_ci,const PetscInt *B_cj,PetscScalar *B_val,PetscInt *spidxA,PetscInt *spidxB, 379 PetscInt *rowhit,PetscScalar **valaddrhit,PetscTable colmap,PetscInt *nrows_i_out) 380 { 381 PetscErrorCode ierr; 382 PetscInt ctype=c->ctype; 383 PetscInt j,col,nrows,k,spidx,colb,nrows_i; 384 const PetscInt *row=NULL; 385 PetscInt cstart,cend,bs; 386 387 PetscFunctionBegin; 388 bs = 1; /* only bs=1 is supported for non MPIBAIJ matrix */ 389 cstart = mat->cmap->rstart/bs; 390 cend = mat->cmap->rend/bs; 391 392 nrows_i = *nrows_i_out; 393 for (j=0; j<nctot; j++) { /* loop over columns*/ 394 if (ctype == IS_COLORING_GHOSTED) { 395 col = ltog[cols[j]]; 396 } else { 397 col = cols[j]; 398 } 399 400 if (col >= cstart && col < cend) { /* column is in A, diagonal block of mat */ 401 row = A_cj + A_ci[col-cstart]; 402 nrows = A_ci[col-cstart+1] - A_ci[col-cstart]; 403 nrows_i += nrows; 404 405 /* loop over columns of A marking them in rowhit */ 406 for (k=0; k<nrows; k++) { 407 /* set valaddrhit for part A */ 408 spidx = bs*bs*spidxA[A_ci[col-cstart] + k]; 409 valaddrhit[*row] = &A_val[spidx]; 410 rowhit[*row++] = col - cstart + 1; /* local column index */ 411 } 412 } else { /* column is in B, off-diagonal block of mat */ 413 #if defined(PETSC_USE_CTABLE) 414 ierr = PetscTableFind(colmap,col+1,&colb);CHKERRQ(ierr); 415 colb--; 416 #else 417 colb = colmap[col] - 1; /* local column index */ 418 #endif 419 if (colb == -1) { 420 nrows = 0; 421 } else { 422 colb = colb/bs; 423 row = B_cj + B_ci[colb]; 424 nrows = B_ci[colb+1] - B_ci[colb]; 425 } 426 nrows_i += nrows; 427 428 /* loop over columns of B marking them in rowhit */ 429 for (k=0; k<nrows; k++) { 430 /* set valaddrhit for part B */ 431 spidx = bs*bs*spidxB[B_ci[colb] + k]; 432 valaddrhit[*row] = &B_val[spidx]; 433 rowhit[*row++] = colb + 1 + cend - cstart; /* local column index */ 434 } 435 } 436 } 437 *nrows_i_out = nrows_i; 438 PetscFunctionReturn(0); 439 } 440 441 #undef __FUNCT__ 442 #define __FUNCT__ "MatFDColoringSetUp_MPIXAIJ" 443 PetscErrorCode MatFDColoringSetUp_MPIXAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c) 444 { 445 PetscErrorCode ierr; 446 PetscMPIInt size,*ncolsonproc,*disp,nn,rank; 447 PetscInt i,n,nrows,nrows_i,j,k,m,ncols,col,*rowhit,cstart,cend,colb; 448 const PetscInt *is,*A_ci,*A_cj,*B_ci,*B_cj,*row=NULL,*ltog=NULL; 449 PetscInt nis=iscoloring->n,nctot,*cols; 450 IS *isa; 451 ISLocalToGlobalMapping map=mat->cmap->mapping; 452 PetscInt ctype=c->ctype,*spidxA,*spidxB,nz,bs,bs2,spidx; 453 Mat A,B; 454 PetscScalar *A_val,*B_val,**valaddrhit; 455 MatEntry *Jentry; 456 MatEntry2 *Jentry2; 457 PetscBool isBAIJ; 458 PetscInt bcols=c->bcols; 459 MPI_Comm comm; 460 PetscMPIInt tag,nrecvs,nsends,proc; 461 MPI_Request *rwaits = NULL,*swaits = NULL; 462 MPI_Status status; 463 #if defined(PETSC_USE_CTABLE) 464 PetscTable colmap=NULL; 465 #else 466 PetscInt *colmap=NULL; /* local col number of off-diag col */ 467 #endif 468 469 PetscFunctionBegin; 470 ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 471 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 472 473 if (ctype == IS_COLORING_GHOSTED) { 474 if (!map) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_INCOMP,"When using ghosted differencing matrix must have local to global mapping provided with MatSetLocalToGlobalMapping"); 475 ierr = ISLocalToGlobalMappingGetIndices(map,<og);CHKERRQ(ierr); 476 } 477 478 ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr); 479 ierr = PetscObjectTypeCompare((PetscObject)mat,MATMPIBAIJ,&isBAIJ);CHKERRQ(ierr); 480 if (isBAIJ) { 481 Mat_MPIBAIJ *baij=(Mat_MPIBAIJ*)mat->data; 482 Mat_SeqBAIJ *spA,*spB; 483 A = baij->A; spA = (Mat_SeqBAIJ*)A->data; A_val = spA->a; 484 B = baij->B; spB = (Mat_SeqBAIJ*)B->data; B_val = spB->a; 485 nz = spA->nz + spB->nz; /* total nonzero entries of mat */ 486 if (!baij->colmap) { 487 ierr = MatCreateColmap_MPIBAIJ_Private(mat);CHKERRQ(ierr); 488 } 489 colmap = baij->colmap; 490 ierr = MatGetColumnIJ_SeqBAIJ_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr); 491 ierr = MatGetColumnIJ_SeqBAIJ_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr); 492 493 if (ctype == IS_COLORING_GLOBAL && c->htype[0] == 'd') { /* create vscale for storing dx */ 494 PetscInt *garray; 495 ierr = PetscMalloc1(B->cmap->n,&garray);CHKERRQ(ierr); 496 for (i=0; i<baij->B->cmap->n/bs; i++) { 497 for (j=0; j<bs; j++) { 498 garray[i*bs+j] = bs*baij->garray[i]+j; 499 } 500 } 501 ierr = VecCreateGhost(PetscObjectComm((PetscObject)mat),mat->cmap->n,PETSC_DETERMINE,B->cmap->n,garray,&c->vscale);CHKERRQ(ierr); 502 ierr = PetscFree(garray);CHKERRQ(ierr); 503 } 504 } else { 505 Mat_MPIAIJ *aij=(Mat_MPIAIJ*)mat->data; 506 Mat_SeqAIJ *spA,*spB; 507 A = aij->A; spA = (Mat_SeqAIJ*)A->data; A_val = spA->a; 508 B = aij->B; spB = (Mat_SeqAIJ*)B->data; B_val = spB->a; 509 nz = spA->nz + spB->nz; /* total nonzero entries of mat */ 510 if (!aij->colmap) { 511 /* Allow access to data structures of local part of matrix 512 - creates aij->colmap which maps global column number to local number in part B */ 513 ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 514 } 515 colmap = aij->colmap; 516 ierr = MatGetColumnIJ_SeqAIJ_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr); 517 ierr = MatGetColumnIJ_SeqAIJ_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr); 518 519 bs = 1; /* only bs=1 is supported for non MPIBAIJ matrix */ 520 521 if (ctype == IS_COLORING_GLOBAL && c->htype[0] == 'd') { /* create vscale for storing dx */ 522 ierr = VecCreateGhost(PetscObjectComm((PetscObject)mat),mat->cmap->n,PETSC_DETERMINE,B->cmap->n,aij->garray,&c->vscale);CHKERRQ(ierr); 523 } 524 } 525 526 m = mat->rmap->n/bs; 527 cstart = mat->cmap->rstart/bs; 528 cend = mat->cmap->rend/bs; 529 530 ierr = PetscMalloc1(nis,&c->ncolumns);CHKERRQ(ierr); 531 ierr = PetscMalloc1(nis,&c->columns);CHKERRQ(ierr); 532 ierr = PetscMalloc1(nis,&c->nrows);CHKERRQ(ierr); 533 ierr = PetscLogObjectMemory((PetscObject)c,3*nis*sizeof(PetscInt));CHKERRQ(ierr); 534 535 if (c->htype[0] == 'd') { 536 ierr = PetscMalloc1(nz,&Jentry);CHKERRQ(ierr); 537 ierr = PetscLogObjectMemory((PetscObject)c,nz*sizeof(MatEntry));CHKERRQ(ierr); 538 c->matentry = Jentry; 539 } else if (c->htype[0] == 'w') { 540 ierr = PetscMalloc1(nz,&Jentry2);CHKERRQ(ierr); 541 ierr = PetscLogObjectMemory((PetscObject)c,nz*sizeof(MatEntry2));CHKERRQ(ierr); 542 c->matentry2 = Jentry2; 543 } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"htype is not supported"); 544 545 ierr = PetscMalloc2(m+1,&rowhit,m+1,&valaddrhit);CHKERRQ(ierr); 546 nz = 0; 547 ierr = ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);CHKERRQ(ierr); 548 for (i=0; i<nis; i++) { /* for each local color */ 549 ierr = ISGetLocalSize(isa[i],&n);CHKERRQ(ierr); 550 ierr = ISGetIndices(isa[i],&is);CHKERRQ(ierr); 551 552 c->ncolumns[i] = n; /* local number of columns of this color on this process */ 553 if (n) { 554 ierr = PetscMalloc1(n,&c->columns[i]);CHKERRQ(ierr); 555 ierr = PetscLogObjectMemory((PetscObject)c,n*sizeof(PetscInt));CHKERRQ(ierr); 556 ierr = PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));CHKERRQ(ierr); 557 } else { 558 c->columns[i] = 0; 559 } 560 561 if (ctype == IS_COLORING_GLOBAL) { 562 /* Determine nctot, the total (parallel) number of columns of this color */ 563 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); 564 ierr = PetscMalloc2(size,&ncolsonproc,size,&disp);CHKERRQ(ierr); 565 566 /* ncolsonproc[j]: local ncolumns on proc[j] of this color */ 567 ierr = PetscMPIIntCast(n,&nn);CHKERRQ(ierr); 568 ierr = MPI_Allgather(&nn,1,MPI_INT,ncolsonproc,1,MPI_INT,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 569 nctot = 0; 570 for (j=0; j<size; j++) nctot += ncolsonproc[j]; 571 if (!nctot) { 572 ierr = PetscInfo(mat,"Coloring of matrix has some unneeded colors with no corresponding rows\n");CHKERRQ(ierr); 573 } 574 575 disp[0] = 0; 576 for (j=1; j<size; j++) { 577 disp[j] = disp[j-1] + ncolsonproc[j-1]; 578 } 579 580 /* Get cols, the complete list of columns for this color on each process */ 581 ierr = PetscMalloc1(nctot+1,&cols);CHKERRQ(ierr); 582 583 /****************** non-scalable !!! *********************/ 584 /* ierr = MPI_Allgatherv((void*)is,n,MPIU_INT,cols,ncolsonproc,disp,MPIU_INT,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); */ 585 586 nrecvs = size-1; 587 ierr = PetscCommGetNewTag(comm,&tag);CHKERRQ(ierr); 588 ierr = PetscMalloc2(nrecvs,&rwaits,nrecvs,&swaits);CHKERRQ(ierr); 589 590 nrecvs = 0; 591 for (proc=0; proc<size; proc++) { 592 if (proc == rank || ncolsonproc[proc] == 0 ) continue; 593 ierr = MPI_Irecv(cols+disp[proc],ncolsonproc[proc],MPIU_INT,proc,tag,comm,rwaits+nrecvs);CHKERRQ(ierr); 594 nrecvs++; 595 } 596 597 nsends = 0; 598 for (proc=0; proc<size; proc++) { 599 if (proc == rank || n == 0) continue; 600 ierr = MPI_Isend((void*)is,n,MPIU_INT,proc,tag,comm,swaits+nsends);CHKERRQ(ierr); 601 nsends++; 602 } 603 604 /* initialize rowhit and nrows_i for MarkRowsForCol_private() */ 605 nrows_i = 0; 606 ierr = PetscMemzero(rowhit,m*sizeof(PetscInt));CHKERRQ(ierr); 607 608 /* MarkRowsForCol for is in this proc */ 609 ierr = MarkRowsForCol_private(ncolsonproc[rank],(PetscInt *)is,mat,iscoloring,c,ltog,A_ci,A_cj,A_val,B_ci,B_cj,B_val,spidxA,spidxB,rowhit,valaddrhit,colmap,&nrows_i);CHKERRQ(ierr); 610 611 j = nrecvs; 612 while (j--) { 613 ierr = MPI_Waitany(nrecvs,rwaits,&k,&status);CHKERRQ(ierr); 614 615 /* MarkRowsForCol for received cols */ 616 proc = status.MPI_SOURCE; 617 ierr = MarkRowsForCol_private(ncolsonproc[proc],cols+disp[proc],mat,iscoloring,c,ltog,A_ci,A_cj,A_val,B_ci,B_cj,B_val,spidxA,spidxB,rowhit,valaddrhit,colmap,&nrows_i);CHKERRQ(ierr); 618 } 619 if (nsends) {ierr = MPI_Waitall(nrecvs,swaits,MPI_STATUSES_IGNORE);CHKERRQ(ierr);} 620 ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr); 621 622 /******************/ 623 ierr = PetscFree2(ncolsonproc,disp);CHKERRQ(ierr); 624 } else if (ctype == IS_COLORING_GHOSTED) { 625 /* Determine local number of columns of this color on this process, including ghost points */ 626 nctot = n; 627 ierr = PetscMalloc1(nctot+1,&cols);CHKERRQ(ierr); 628 ierr = PetscMemcpy(cols,is,n*sizeof(PetscInt));CHKERRQ(ierr); 629 630 /* initialize rowhit and nrows_i for MarkRowsForCol_private() */ 631 nrows_i = 0; 632 ierr = PetscMemzero(rowhit,m*sizeof(PetscInt));CHKERRQ(ierr); 633 634 ierr = MarkRowsForCol_private(nctot,cols,mat,iscoloring,c,ltog,A_ci,A_cj,A_val,B_ci,B_cj,B_val,spidxA,spidxB,rowhit,valaddrhit,colmap,&nrows_i);CHKERRQ(ierr); 635 } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not provided for this MatFDColoring type"); 636 637 /* Mark all rows affect by these columns */ 638 /********** new ********/ 639 //ierr = MarkRowsForCol_private(nctot,cols,mat,iscoloring,c,ltog,A_ci,A_cj,A_val,B_ci,B_cj,B_val,spidxA,spidxB,rowhit,valaddrhit,colmap,&nrows_i);CHKERRQ(ierr); 640 #if 0 641 ierr = PetscMemzero(rowhit,m*sizeof(PetscInt));CHKERRQ(ierr); 642 bs2 = bs*bs; 643 nrows_i = 0; 644 645 //printf(" \n[%d] nctot %d\n",rank,nctot); 646 for (j=0; j<nctot; j++) { /* loop over columns*/ 647 648 if (ctype == IS_COLORING_GHOSTED) { 649 col = ltog[cols[j]]; 650 } else { 651 col = cols[j]; 652 } 653 if (col >= cstart && col < cend) { /* column is in A, diagonal block of mat */ 654 row = A_cj + A_ci[col-cstart]; 655 nrows = A_ci[col-cstart+1] - A_ci[col-cstart]; 656 nrows_i += nrows; 657 658 /* loop over columns of A marking them in rowhit */ 659 for (k=0; k<nrows; k++) { 660 /* set valaddrhit for part A */ 661 spidx = bs2*spidxA[A_ci[col-cstart] + k]; 662 valaddrhit[*row] = &A_val[spidx]; 663 rowhit[*row] = col - cstart + 1; /* local column index */ 664 //printf("[%d] valaddrhit[%d] %p, rowhit %d\n",rank,*row, valaddrhit[*row],rowhit[*row] ); 665 row++; 666 } 667 } else { /* column is in B, off-diagonal block of mat */ 668 #if defined(PETSC_USE_CTABLE) 669 ierr = PetscTableFind(colmap,col+1,&colb);CHKERRQ(ierr); 670 colb--; 671 #else 672 colb = colmap[col] - 1; /* local column index */ 673 #endif 674 if (colb == -1) { 675 nrows = 0; 676 } else { 677 colb = colb/bs; 678 row = B_cj + B_ci[colb]; 679 nrows = B_ci[colb+1] - B_ci[colb]; 680 } 681 nrows_i += nrows; 682 683 /* loop over columns of B marking them in rowhit */ 684 for (k=0; k<nrows; k++) { 685 /* set valaddrhit for part B */ 686 spidx = bs2*spidxB[B_ci[colb] + k]; 687 valaddrhit[*row] = &B_val[spidx]; 688 rowhit[*row] = colb + 1 + cend - cstart; /* local column index */ 689 //printf("[%d] valaddrhit[%d] %p, rowhit %d\n",rank,*row, valaddrhit[*row],rowhit[*row] ); 690 row++; 691 } 692 } 693 } //endif loop 694 /********************************** */ 695 #endif 696 ierr = PetscFree(cols);CHKERRQ(ierr); 697 c->nrows[i] = nrows_i; 698 699 if (c->htype[0] == 'd') { 700 for (j=0; j<m; j++) { 701 if (rowhit[j]) { 702 Jentry[nz].row = j; /* local row index */ 703 Jentry[nz].col = rowhit[j] - 1; /* local column index */ 704 Jentry[nz].valaddr = valaddrhit[j]; /* address of mat value for this entry */ 705 nz++; 706 } 707 } 708 } else { /* c->htype == 'wp' */ 709 for (j=0; j<m; j++) { 710 if (rowhit[j]) { 711 Jentry2[nz].row = j; /* local row index */ 712 Jentry2[nz].valaddr = valaddrhit[j]; /* address of mat value for this entry */ 713 nz++; 714 } 715 } 716 } 717 } 718 719 if (bcols > 1) { /* reorder Jentry for faster MatFDColoringApply() */ 720 ierr = MatFDColoringSetUpBlocked_AIJ_Private(mat,c,nz);CHKERRQ(ierr); 721 } 722 723 if (isBAIJ) { 724 ierr = MatRestoreColumnIJ_SeqBAIJ_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr); 725 ierr = MatRestoreColumnIJ_SeqBAIJ_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr); 726 ierr = PetscMalloc1(bs*mat->rmap->n,&c->dy);CHKERRQ(ierr); 727 } else { 728 ierr = MatRestoreColumnIJ_SeqAIJ_Color(A,0,PETSC_FALSE,PETSC_FALSE,&ncols,&A_ci,&A_cj,&spidxA,NULL);CHKERRQ(ierr); 729 ierr = MatRestoreColumnIJ_SeqAIJ_Color(B,0,PETSC_FALSE,PETSC_FALSE,&ncols,&B_ci,&B_cj,&spidxB,NULL);CHKERRQ(ierr); 730 } 731 732 ierr = ISColoringRestoreIS(iscoloring,&isa);CHKERRQ(ierr); 733 ierr = PetscFree2(rowhit,valaddrhit);CHKERRQ(ierr); 734 735 if (ctype == IS_COLORING_GHOSTED) { 736 ierr = ISLocalToGlobalMappingRestoreIndices(map,<og);CHKERRQ(ierr); 737 } 738 ierr = PetscInfo3(c,"ncolors %D, brows %D and bcols %D are used.\n",c->ncolors,c->brows,c->bcols);CHKERRQ(ierr); 739 PetscFunctionReturn(0); 740 } 741 742 #undef __FUNCT__ 743 #define __FUNCT__ "MatFDColoringCreate_MPIXAIJ" 744 PetscErrorCode MatFDColoringCreate_MPIXAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c) 745 { 746 PetscErrorCode ierr; 747 PetscInt bs,nis=iscoloring->n,m=mat->rmap->n; 748 PetscBool isBAIJ; 749 750 PetscFunctionBegin; 751 /* set default brows and bcols for speedup inserting the dense matrix into sparse Jacobian; 752 bcols is chosen s.t. dy-array takes 50% of memory space as mat */ 753 ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr); 754 ierr = PetscObjectTypeCompare((PetscObject)mat,MATMPIBAIJ,&isBAIJ);CHKERRQ(ierr); 755 if (isBAIJ || m == 0) { 756 c->brows = m; 757 c->bcols = 1; 758 } else { /* mpiaij matrix */ 759 /* bcols is chosen s.t. dy-array takes 50% of local memory space as mat */ 760 Mat_MPIAIJ *aij=(Mat_MPIAIJ*)mat->data; 761 Mat_SeqAIJ *spA,*spB; 762 Mat A,B; 763 PetscInt nz,brows,bcols; 764 PetscReal mem; 765 766 bs = 1; /* only bs=1 is supported for MPIAIJ matrix */ 767 768 A = aij->A; spA = (Mat_SeqAIJ*)A->data; 769 B = aij->B; spB = (Mat_SeqAIJ*)B->data; 770 nz = spA->nz + spB->nz; /* total local nonzero entries of mat */ 771 mem = nz*(sizeof(PetscScalar) + sizeof(PetscInt)) + 3*m*sizeof(PetscInt); 772 bcols = (PetscInt)(0.5*mem /(m*sizeof(PetscScalar))); 773 brows = 1000/bcols; 774 if (bcols > nis) bcols = nis; 775 if (brows == 0 || brows > m) brows = m; 776 c->brows = brows; 777 c->bcols = bcols; 778 } 779 780 c->M = mat->rmap->N/bs; /* set the global rows and columns and local rows */ 781 c->N = mat->cmap->N/bs; 782 c->m = mat->rmap->n/bs; 783 c->rstart = mat->rmap->rstart/bs; 784 c->ncolors = nis; 785 PetscFunctionReturn(0); 786 } 787