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