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