1 2 /* 3 Defines a block Jacobi preconditioner. 4 */ 5 6 #include <../src/ksp/pc/impls/bjacobi/bjacobi.h> /*I "petscpc.h" I*/ 7 8 static PetscErrorCode PCSetUp_BJacobi_Singleblock(PC, Mat, Mat); 9 static PetscErrorCode PCSetUp_BJacobi_Multiblock(PC, Mat, Mat); 10 static PetscErrorCode PCSetUp_BJacobi_Multiproc(PC); 11 12 static PetscErrorCode PCSetUp_BJacobi(PC pc) 13 { 14 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 15 Mat mat = pc->mat, pmat = pc->pmat; 16 PetscBool hasop; 17 PetscInt N, M, start, i, sum, end; 18 PetscInt bs, i_start = -1, i_end = -1; 19 PetscMPIInt rank, size; 20 21 PetscFunctionBegin; 22 PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank)); 23 PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size)); 24 PetscCall(MatGetLocalSize(pc->pmat, &M, &N)); 25 PetscCall(MatGetBlockSize(pc->pmat, &bs)); 26 27 if (jac->n > 0 && jac->n < size) { 28 PetscCall(PCSetUp_BJacobi_Multiproc(pc)); 29 PetscFunctionReturn(0); 30 } 31 32 /* Determines the number of blocks assigned to each processor */ 33 /* local block count given */ 34 if (jac->n_local > 0 && jac->n < 0) { 35 PetscCall(MPIU_Allreduce(&jac->n_local, &jac->n, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)pc))); 36 if (jac->l_lens) { /* check that user set these correctly */ 37 sum = 0; 38 for (i = 0; i < jac->n_local; i++) { 39 PetscCheck(jac->l_lens[i] / bs * bs == jac->l_lens[i], PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Mat blocksize doesn't match block Jacobi layout"); 40 sum += jac->l_lens[i]; 41 } 42 PetscCheck(sum == M, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Local lens set incorrectly"); 43 } else { 44 PetscCall(PetscMalloc1(jac->n_local, &jac->l_lens)); 45 for (i = 0; i < jac->n_local; i++) jac->l_lens[i] = bs * ((M / bs) / jac->n_local + (((M / bs) % jac->n_local) > i)); 46 } 47 } else if (jac->n > 0 && jac->n_local < 0) { /* global block count given */ 48 /* global blocks given: determine which ones are local */ 49 if (jac->g_lens) { 50 /* check if the g_lens is has valid entries */ 51 for (i = 0; i < jac->n; i++) { 52 PetscCheck(jac->g_lens[i], PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Zero block not allowed"); 53 PetscCheck(jac->g_lens[i] / bs * bs == jac->g_lens[i], PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Mat blocksize doesn't match block Jacobi layout"); 54 } 55 if (size == 1) { 56 jac->n_local = jac->n; 57 PetscCall(PetscMalloc1(jac->n_local, &jac->l_lens)); 58 PetscCall(PetscArraycpy(jac->l_lens, jac->g_lens, jac->n_local)); 59 /* check that user set these correctly */ 60 sum = 0; 61 for (i = 0; i < jac->n_local; i++) sum += jac->l_lens[i]; 62 PetscCheck(sum == M, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Global lens set incorrectly"); 63 } else { 64 PetscCall(MatGetOwnershipRange(pc->pmat, &start, &end)); 65 /* loop over blocks determing first one owned by me */ 66 sum = 0; 67 for (i = 0; i < jac->n + 1; i++) { 68 if (sum == start) { 69 i_start = i; 70 goto start_1; 71 } 72 if (i < jac->n) sum += jac->g_lens[i]; 73 } 74 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Block sizes used in PCBJacobiSetTotalBlocks()\nare not compatible with parallel matrix layout"); 75 start_1: 76 for (i = i_start; i < jac->n + 1; i++) { 77 if (sum == end) { 78 i_end = i; 79 goto end_1; 80 } 81 if (i < jac->n) sum += jac->g_lens[i]; 82 } 83 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Block sizes used in PCBJacobiSetTotalBlocks()\nare not compatible with parallel matrix layout"); 84 end_1: 85 jac->n_local = i_end - i_start; 86 PetscCall(PetscMalloc1(jac->n_local, &jac->l_lens)); 87 PetscCall(PetscArraycpy(jac->l_lens, jac->g_lens + i_start, jac->n_local)); 88 } 89 } else { /* no global blocks given, determine then using default layout */ 90 jac->n_local = jac->n / size + ((jac->n % size) > rank); 91 PetscCall(PetscMalloc1(jac->n_local, &jac->l_lens)); 92 for (i = 0; i < jac->n_local; i++) { 93 jac->l_lens[i] = ((M / bs) / jac->n_local + (((M / bs) % jac->n_local) > i)) * bs; 94 PetscCheck(jac->l_lens[i], PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Too many blocks given"); 95 } 96 } 97 } else if (jac->n < 0 && jac->n_local < 0) { /* no blocks given */ 98 jac->n = size; 99 jac->n_local = 1; 100 PetscCall(PetscMalloc1(1, &jac->l_lens)); 101 jac->l_lens[0] = M; 102 } else { /* jac->n > 0 && jac->n_local > 0 */ 103 if (!jac->l_lens) { 104 PetscCall(PetscMalloc1(jac->n_local, &jac->l_lens)); 105 for (i = 0; i < jac->n_local; i++) jac->l_lens[i] = bs * ((M / bs) / jac->n_local + (((M / bs) % jac->n_local) > i)); 106 } 107 } 108 PetscCheck(jac->n_local >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of blocks is less than number of processors"); 109 110 /* Determines mat and pmat */ 111 PetscCall(MatHasOperation(pc->mat, MATOP_GET_DIAGONAL_BLOCK, &hasop)); 112 if (!hasop && size == 1) { 113 mat = pc->mat; 114 pmat = pc->pmat; 115 } else { 116 if (pc->useAmat) { 117 /* use block from Amat matrix, not Pmat for local MatMult() */ 118 PetscCall(MatGetDiagonalBlock(pc->mat, &mat)); 119 } 120 if (pc->pmat != pc->mat || !pc->useAmat) { 121 PetscCall(MatGetDiagonalBlock(pc->pmat, &pmat)); 122 } else pmat = mat; 123 } 124 125 /* 126 Setup code depends on the number of blocks 127 */ 128 if (jac->n_local == 1) { 129 PetscCall(PCSetUp_BJacobi_Singleblock(pc, mat, pmat)); 130 } else { 131 PetscCall(PCSetUp_BJacobi_Multiblock(pc, mat, pmat)); 132 } 133 PetscFunctionReturn(0); 134 } 135 136 /* Default destroy, if it has never been setup */ 137 static PetscErrorCode PCDestroy_BJacobi(PC pc) 138 { 139 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 140 141 PetscFunctionBegin; 142 PetscCall(PetscFree(jac->g_lens)); 143 PetscCall(PetscFree(jac->l_lens)); 144 PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiGetSubKSP_C", NULL)); 145 PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiSetTotalBlocks_C", NULL)); 146 PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiGetTotalBlocks_C", NULL)); 147 PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiSetLocalBlocks_C", NULL)); 148 PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiGetLocalBlocks_C", NULL)); 149 PetscCall(PetscFree(pc->data)); 150 PetscFunctionReturn(0); 151 } 152 153 static PetscErrorCode PCSetFromOptions_BJacobi(PC pc, PetscOptionItems *PetscOptionsObject) 154 { 155 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 156 PetscInt blocks, i; 157 PetscBool flg; 158 159 PetscFunctionBegin; 160 PetscOptionsHeadBegin(PetscOptionsObject, "Block Jacobi options"); 161 PetscCall(PetscOptionsInt("-pc_bjacobi_blocks", "Total number of blocks", "PCBJacobiSetTotalBlocks", jac->n, &blocks, &flg)); 162 if (flg) PetscCall(PCBJacobiSetTotalBlocks(pc, blocks, NULL)); 163 PetscCall(PetscOptionsInt("-pc_bjacobi_local_blocks", "Local number of blocks", "PCBJacobiSetLocalBlocks", jac->n_local, &blocks, &flg)); 164 if (flg) PetscCall(PCBJacobiSetLocalBlocks(pc, blocks, NULL)); 165 if (jac->ksp) { 166 /* The sub-KSP has already been set up (e.g., PCSetUp_BJacobi_Singleblock), but KSPSetFromOptions was not called 167 * unless we had already been called. */ 168 for (i = 0; i < jac->n_local; i++) PetscCall(KSPSetFromOptions(jac->ksp[i])); 169 } 170 PetscOptionsHeadEnd(); 171 PetscFunctionReturn(0); 172 } 173 174 #include <petscdraw.h> 175 static PetscErrorCode PCView_BJacobi(PC pc, PetscViewer viewer) 176 { 177 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 178 PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc *)jac->data; 179 PetscMPIInt rank; 180 PetscInt i; 181 PetscBool iascii, isstring, isdraw; 182 PetscViewer sviewer; 183 PetscViewerFormat format; 184 const char *prefix; 185 186 PetscFunctionBegin; 187 PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 188 PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring)); 189 PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw)); 190 if (iascii) { 191 if (pc->useAmat) PetscCall(PetscViewerASCIIPrintf(viewer, " using Amat local matrix, number of blocks = %" PetscInt_FMT "\n", jac->n)); 192 PetscCall(PetscViewerASCIIPrintf(viewer, " number of blocks = %" PetscInt_FMT "\n", jac->n)); 193 PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank)); 194 PetscCall(PetscViewerGetFormat(viewer, &format)); 195 if (format != PETSC_VIEWER_ASCII_INFO_DETAIL) { 196 PetscCall(PetscViewerASCIIPrintf(viewer, " Local solver information for first block is in the following KSP and PC objects on rank 0:\n")); 197 PetscCall(PCGetOptionsPrefix(pc, &prefix)); 198 PetscCall(PetscViewerASCIIPrintf(viewer, " Use -%sksp_view ::ascii_info_detail to display information for all blocks\n", prefix ? prefix : "")); 199 if (jac->ksp && !jac->psubcomm) { 200 PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 201 if (rank == 0) { 202 PetscCall(PetscViewerASCIIPushTab(viewer)); 203 PetscCall(KSPView(jac->ksp[0], sviewer)); 204 PetscCall(PetscViewerASCIIPopTab(viewer)); 205 } 206 PetscCall(PetscViewerFlush(sviewer)); 207 PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 208 PetscCall(PetscViewerFlush(viewer)); 209 /* extra call needed because of the two calls to PetscViewerASCIIPushSynchronized() in PetscViewerGetSubViewer() */ 210 PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 211 } else if (mpjac && jac->ksp && mpjac->psubcomm) { 212 PetscCall(PetscViewerGetSubViewer(viewer, mpjac->psubcomm->child, &sviewer)); 213 if (!mpjac->psubcomm->color) { 214 PetscCall(PetscViewerASCIIPushTab(viewer)); 215 PetscCall(KSPView(*(jac->ksp), sviewer)); 216 PetscCall(PetscViewerASCIIPopTab(viewer)); 217 } 218 PetscCall(PetscViewerFlush(sviewer)); 219 PetscCall(PetscViewerRestoreSubViewer(viewer, mpjac->psubcomm->child, &sviewer)); 220 PetscCall(PetscViewerFlush(viewer)); 221 /* extra call needed because of the two calls to PetscViewerASCIIPushSynchronized() in PetscViewerGetSubViewer() */ 222 PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 223 } else { 224 PetscCall(PetscViewerFlush(viewer)); 225 } 226 } else { 227 PetscInt n_global; 228 PetscCall(MPIU_Allreduce(&jac->n_local, &n_global, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)pc))); 229 PetscCall(PetscViewerASCIIPushSynchronized(viewer)); 230 PetscCall(PetscViewerASCIIPrintf(viewer, " Local solver information for each block is in the following KSP and PC objects:\n")); 231 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] number of local blocks = %" PetscInt_FMT ", first local block number = %" PetscInt_FMT "\n", rank, jac->n_local, jac->first_local)); 232 PetscCall(PetscViewerASCIIPushTab(viewer)); 233 PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 234 for (i = 0; i < jac->n_local; i++) { 235 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] local block number %" PetscInt_FMT "\n", rank, i)); 236 PetscCall(KSPView(jac->ksp[i], sviewer)); 237 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "- - - - - - - - - - - - - - - - - -\n")); 238 } 239 PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 240 PetscCall(PetscViewerASCIIPopTab(viewer)); 241 PetscCall(PetscViewerFlush(viewer)); 242 PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 243 } 244 } else if (isstring) { 245 PetscCall(PetscViewerStringSPrintf(viewer, " blks=%" PetscInt_FMT, jac->n)); 246 PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 247 if (jac->ksp) PetscCall(KSPView(jac->ksp[0], sviewer)); 248 PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer)); 249 } else if (isdraw) { 250 PetscDraw draw; 251 char str[25]; 252 PetscReal x, y, bottom, h; 253 254 PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw)); 255 PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y)); 256 PetscCall(PetscSNPrintf(str, 25, "Number blocks %" PetscInt_FMT, jac->n)); 257 PetscCall(PetscDrawStringBoxed(draw, x, y, PETSC_DRAW_RED, PETSC_DRAW_BLACK, str, NULL, &h)); 258 bottom = y - h; 259 PetscCall(PetscDrawPushCurrentPoint(draw, x, bottom)); 260 /* warning the communicator on viewer is different then on ksp in parallel */ 261 if (jac->ksp) PetscCall(KSPView(jac->ksp[0], viewer)); 262 PetscCall(PetscDrawPopCurrentPoint(draw)); 263 } 264 PetscFunctionReturn(0); 265 } 266 267 static PetscErrorCode PCBJacobiGetSubKSP_BJacobi(PC pc, PetscInt *n_local, PetscInt *first_local, KSP **ksp) 268 { 269 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 270 271 PetscFunctionBegin; 272 PetscCheck(pc->setupcalled, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must call KSPSetUp() or PCSetUp() first"); 273 274 if (n_local) *n_local = jac->n_local; 275 if (first_local) *first_local = jac->first_local; 276 if (ksp) *ksp = jac->ksp; 277 PetscFunctionReturn(0); 278 } 279 280 static PetscErrorCode PCBJacobiSetTotalBlocks_BJacobi(PC pc, PetscInt blocks, PetscInt *lens) 281 { 282 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 283 284 PetscFunctionBegin; 285 PetscCheck(pc->setupcalled <= 0 || jac->n == blocks, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Cannot alter number of blocks after PCSetUp()/KSPSetUp() has been called"); 286 jac->n = blocks; 287 if (!lens) jac->g_lens = NULL; 288 else { 289 PetscCall(PetscMalloc1(blocks, &jac->g_lens)); 290 PetscCall(PetscArraycpy(jac->g_lens, lens, blocks)); 291 } 292 PetscFunctionReturn(0); 293 } 294 295 static PetscErrorCode PCBJacobiGetTotalBlocks_BJacobi(PC pc, PetscInt *blocks, const PetscInt *lens[]) 296 { 297 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 298 299 PetscFunctionBegin; 300 *blocks = jac->n; 301 if (lens) *lens = jac->g_lens; 302 PetscFunctionReturn(0); 303 } 304 305 static PetscErrorCode PCBJacobiSetLocalBlocks_BJacobi(PC pc, PetscInt blocks, const PetscInt lens[]) 306 { 307 PC_BJacobi *jac; 308 309 PetscFunctionBegin; 310 jac = (PC_BJacobi *)pc->data; 311 312 jac->n_local = blocks; 313 if (!lens) jac->l_lens = NULL; 314 else { 315 PetscCall(PetscMalloc1(blocks, &jac->l_lens)); 316 PetscCall(PetscArraycpy(jac->l_lens, lens, blocks)); 317 } 318 PetscFunctionReturn(0); 319 } 320 321 static PetscErrorCode PCBJacobiGetLocalBlocks_BJacobi(PC pc, PetscInt *blocks, const PetscInt *lens[]) 322 { 323 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 324 325 PetscFunctionBegin; 326 *blocks = jac->n_local; 327 if (lens) *lens = jac->l_lens; 328 PetscFunctionReturn(0); 329 } 330 331 /*@C 332 PCBJacobiGetSubKSP - Gets the local `KSP` contexts for all blocks on 333 this processor. 334 335 Not Collective 336 337 Input Parameter: 338 . pc - the preconditioner context 339 340 Output Parameters: 341 + n_local - the number of blocks on this processor, or NULL 342 . first_local - the global number of the first block on this processor, or NULL 343 - ksp - the array of KSP contexts 344 345 Notes: 346 After `PCBJacobiGetSubKSP()` the array of `KSP` contexts is not to be freed. 347 348 Currently for some matrix implementations only 1 block per processor 349 is supported. 350 351 You must call `KSPSetUp()` or `PCSetUp()` before calling `PCBJacobiGetSubKSP()`. 352 353 Fortran Usage: 354 You must pass in a `KSP` array that is large enough to contain all the local `KSP`s. 355 356 You can call `PCBJacobiGetSubKSP`(pc,nlocal,firstlocal,`PETSC_NULL_KSP`,ierr) to determine how large the 357 `KSP` array must be. 358 359 Level: advanced 360 361 .seealso: `PCBJACOBI`, `PCASM`, `PCASMGetSubKSP()` 362 @*/ 363 PetscErrorCode PCBJacobiGetSubKSP(PC pc, PetscInt *n_local, PetscInt *first_local, KSP *ksp[]) 364 { 365 PetscFunctionBegin; 366 PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 367 PetscUseMethod(pc, "PCBJacobiGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (pc, n_local, first_local, ksp)); 368 PetscFunctionReturn(0); 369 } 370 371 /*@ 372 PCBJacobiSetTotalBlocks - Sets the global number of blocks for the block 373 Jacobi preconditioner. 374 375 Collective on pc 376 377 Input Parameters: 378 + pc - the preconditioner context 379 . blocks - the number of blocks 380 - lens - [optional] integer array containing the size of each block 381 382 Options Database Key: 383 . -pc_bjacobi_blocks <blocks> - Sets the number of global blocks 384 385 Note: 386 Currently only a limited number of blocking configurations are supported. 387 All processors sharing the `PC` must call this routine with the same data. 388 389 Level: intermediate 390 391 .seealso: `PCBJACOBI`, `PCSetUseAmat()`, `PCBJacobiSetLocalBlocks()` 392 @*/ 393 PetscErrorCode PCBJacobiSetTotalBlocks(PC pc, PetscInt blocks, const PetscInt lens[]) 394 { 395 PetscFunctionBegin; 396 PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 397 PetscCheck(blocks > 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Must have positive blocks"); 398 PetscTryMethod(pc, "PCBJacobiSetTotalBlocks_C", (PC, PetscInt, const PetscInt[]), (pc, blocks, lens)); 399 PetscFunctionReturn(0); 400 } 401 402 /*@C 403 PCBJacobiGetTotalBlocks - Gets the global number of blocks for the block 404 Jacobi, `PCBJACOBI`, preconditioner. 405 406 Not Collective 407 408 Input Parameter: 409 . pc - the preconditioner context 410 411 Output parameters: 412 + blocks - the number of blocks 413 - lens - integer array containing the size of each block 414 415 Level: intermediate 416 417 .seealso: `PCBJACOBI`, `PCSetUseAmat()`, `PCBJacobiGetLocalBlocks()` 418 @*/ 419 PetscErrorCode PCBJacobiGetTotalBlocks(PC pc, PetscInt *blocks, const PetscInt *lens[]) 420 { 421 PetscFunctionBegin; 422 PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 423 PetscValidIntPointer(blocks, 2); 424 PetscUseMethod(pc, "PCBJacobiGetTotalBlocks_C", (PC, PetscInt *, const PetscInt *[]), (pc, blocks, lens)); 425 PetscFunctionReturn(0); 426 } 427 428 /*@ 429 PCBJacobiSetLocalBlocks - Sets the local number of blocks for the block 430 Jacobi, `PCBJACOBI`, preconditioner. 431 432 Not Collective 433 434 Input Parameters: 435 + pc - the preconditioner context 436 . blocks - the number of blocks 437 - lens - [optional] integer array containing size of each block 438 439 Options Database Key: 440 . -pc_bjacobi_local_blocks <blocks> - Sets the number of local blocks 441 442 Note: 443 Currently only a limited number of blocking configurations are supported. 444 445 Level: intermediate 446 447 .seealso: `PCBJACOBI`, `PCSetUseAmat()`, `PCBJacobiSetTotalBlocks()` 448 @*/ 449 PetscErrorCode PCBJacobiSetLocalBlocks(PC pc, PetscInt blocks, const PetscInt lens[]) 450 { 451 PetscFunctionBegin; 452 PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 453 PetscCheck(blocks >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must have nonegative blocks"); 454 PetscTryMethod(pc, "PCBJacobiSetLocalBlocks_C", (PC, PetscInt, const PetscInt[]), (pc, blocks, lens)); 455 PetscFunctionReturn(0); 456 } 457 458 /*@C 459 PCBJacobiGetLocalBlocks - Gets the local number of blocks for the block 460 Jacobi, `PCBJACOBI`, preconditioner. 461 462 Not Collective 463 464 Input Parameters: 465 + pc - the preconditioner context 466 . blocks - the number of blocks 467 - lens - [optional] integer array containing size of each block 468 469 Note: 470 Currently only a limited number of blocking configurations are supported. 471 472 Level: intermediate 473 474 .seealso: `PCBJACOBI`, `PCSetUseAmat()`, `PCBJacobiGetTotalBlocks()` 475 @*/ 476 PetscErrorCode PCBJacobiGetLocalBlocks(PC pc, PetscInt *blocks, const PetscInt *lens[]) 477 { 478 PetscFunctionBegin; 479 PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 480 PetscValidIntPointer(blocks, 2); 481 PetscUseMethod(pc, "PCBJacobiGetLocalBlocks_C", (PC, PetscInt *, const PetscInt *[]), (pc, blocks, lens)); 482 PetscFunctionReturn(0); 483 } 484 485 /*MC 486 PCBJACOBI - Use block Jacobi preconditioning, each block is (approximately) solved with 487 its own `KSP` object. 488 489 Options Database Keys: 490 + -pc_use_amat - use Amat to apply block of operator in inner Krylov method 491 - -pc_bjacobi_blocks <n> - use n total blocks 492 493 Notes: 494 See `PCJACOBI` for diagonal Jacobi, `PCVPBJACOBI` for variable point block, and `PCPBJACOBI` for fixed size point block 495 496 Each processor can have one or more blocks, or a single block can be shared by several processes. Defaults to one block per processor. 497 498 To set options on the solvers for each block append -sub_ to all the `KSP` and `PC` 499 options database keys. For example, -sub_pc_type ilu -sub_pc_factor_levels 1 -sub_ksp_type preonly 500 501 To set the options on the solvers separate for each block call `PCBJacobiGetSubKSP()` 502 and set the options directly on the resulting `KSP` object (you can access its `PC` 503 `KSPGetPC())` 504 505 For GPU-based vectors (`VECCUDA`, `VECViennaCL`) it is recommended to use exactly one block per MPI process for best 506 performance. Different block partitioning may lead to additional data transfers 507 between host and GPU that lead to degraded performance. 508 509 When multiple processes share a single block, each block encompasses exactly all the unknowns owned its set of processes. 510 511 Level: beginner 512 513 .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCType`, 514 `PCASM`, `PCSetUseAmat()`, `PCGetUseAmat()`, `PCBJacobiGetSubKSP()`, `PCBJacobiSetTotalBlocks()`, 515 `PCBJacobiSetLocalBlocks()`, `PCSetModifySubMatrices()`, `PCJACOBI`, `PCVPBJACOBI`, `PCPBJACOBI` 516 M*/ 517 518 PETSC_EXTERN PetscErrorCode PCCreate_BJacobi(PC pc) 519 { 520 PetscMPIInt rank; 521 PC_BJacobi *jac; 522 523 PetscFunctionBegin; 524 PetscCall(PetscNew(&jac)); 525 PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank)); 526 527 pc->ops->apply = NULL; 528 pc->ops->matapply = NULL; 529 pc->ops->applytranspose = NULL; 530 pc->ops->setup = PCSetUp_BJacobi; 531 pc->ops->destroy = PCDestroy_BJacobi; 532 pc->ops->setfromoptions = PCSetFromOptions_BJacobi; 533 pc->ops->view = PCView_BJacobi; 534 pc->ops->applyrichardson = NULL; 535 536 pc->data = (void *)jac; 537 jac->n = -1; 538 jac->n_local = -1; 539 jac->first_local = rank; 540 jac->ksp = NULL; 541 jac->g_lens = NULL; 542 jac->l_lens = NULL; 543 jac->psubcomm = NULL; 544 545 PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiGetSubKSP_C", PCBJacobiGetSubKSP_BJacobi)); 546 PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiSetTotalBlocks_C", PCBJacobiSetTotalBlocks_BJacobi)); 547 PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiGetTotalBlocks_C", PCBJacobiGetTotalBlocks_BJacobi)); 548 PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiSetLocalBlocks_C", PCBJacobiSetLocalBlocks_BJacobi)); 549 PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCBJacobiGetLocalBlocks_C", PCBJacobiGetLocalBlocks_BJacobi)); 550 PetscFunctionReturn(0); 551 } 552 553 /* 554 These are for a single block per processor; works for AIJ, BAIJ; Seq and MPI 555 */ 556 static PetscErrorCode PCReset_BJacobi_Singleblock(PC pc) 557 { 558 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 559 PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock *)jac->data; 560 561 PetscFunctionBegin; 562 PetscCall(KSPReset(jac->ksp[0])); 563 PetscCall(VecDestroy(&bjac->x)); 564 PetscCall(VecDestroy(&bjac->y)); 565 PetscFunctionReturn(0); 566 } 567 568 static PetscErrorCode PCDestroy_BJacobi_Singleblock(PC pc) 569 { 570 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 571 PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock *)jac->data; 572 573 PetscFunctionBegin; 574 PetscCall(PCReset_BJacobi_Singleblock(pc)); 575 PetscCall(KSPDestroy(&jac->ksp[0])); 576 PetscCall(PetscFree(jac->ksp)); 577 PetscCall(PetscFree(bjac)); 578 PetscCall(PCDestroy_BJacobi(pc)); 579 PetscFunctionReturn(0); 580 } 581 582 static PetscErrorCode PCSetUpOnBlocks_BJacobi_Singleblock(PC pc) 583 { 584 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 585 KSP subksp = jac->ksp[0]; 586 KSPConvergedReason reason; 587 588 PetscFunctionBegin; 589 PetscCall(KSPSetUp(subksp)); 590 PetscCall(KSPGetConvergedReason(subksp, &reason)); 591 if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR; 592 PetscFunctionReturn(0); 593 } 594 595 static PetscErrorCode PCApply_BJacobi_Singleblock(PC pc, Vec x, Vec y) 596 { 597 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 598 PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock *)jac->data; 599 600 PetscFunctionBegin; 601 PetscCall(VecGetLocalVectorRead(x, bjac->x)); 602 PetscCall(VecGetLocalVector(y, bjac->y)); 603 /* Since the inner KSP matrix may point directly to the diagonal block of an MPI matrix the inner 604 matrix may change even if the outer KSP/PC has not updated the preconditioner, this will trigger a rebuild 605 of the inner preconditioner automatically unless we pass down the outer preconditioners reuse flag.*/ 606 PetscCall(KSPSetReusePreconditioner(jac->ksp[0], pc->reusepreconditioner)); 607 PetscCall(KSPSolve(jac->ksp[0], bjac->x, bjac->y)); 608 PetscCall(KSPCheckSolve(jac->ksp[0], pc, bjac->y)); 609 PetscCall(VecRestoreLocalVectorRead(x, bjac->x)); 610 PetscCall(VecRestoreLocalVector(y, bjac->y)); 611 PetscFunctionReturn(0); 612 } 613 614 static PetscErrorCode PCMatApply_BJacobi_Singleblock(PC pc, Mat X, Mat Y) 615 { 616 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 617 Mat sX, sY; 618 619 PetscFunctionBegin; 620 /* Since the inner KSP matrix may point directly to the diagonal block of an MPI matrix the inner 621 matrix may change even if the outer KSP/PC has not updated the preconditioner, this will trigger a rebuild 622 of the inner preconditioner automatically unless we pass down the outer preconditioners reuse flag.*/ 623 PetscCall(KSPSetReusePreconditioner(jac->ksp[0], pc->reusepreconditioner)); 624 PetscCall(MatDenseGetLocalMatrix(X, &sX)); 625 PetscCall(MatDenseGetLocalMatrix(Y, &sY)); 626 PetscCall(KSPMatSolve(jac->ksp[0], sX, sY)); 627 PetscFunctionReturn(0); 628 } 629 630 static PetscErrorCode PCApplySymmetricLeft_BJacobi_Singleblock(PC pc, Vec x, Vec y) 631 { 632 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 633 PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock *)jac->data; 634 PetscScalar *y_array; 635 const PetscScalar *x_array; 636 PC subpc; 637 638 PetscFunctionBegin; 639 /* 640 The VecPlaceArray() is to avoid having to copy the 641 y vector into the bjac->x vector. The reason for 642 the bjac->x vector is that we need a sequential vector 643 for the sequential solve. 644 */ 645 PetscCall(VecGetArrayRead(x, &x_array)); 646 PetscCall(VecGetArray(y, &y_array)); 647 PetscCall(VecPlaceArray(bjac->x, x_array)); 648 PetscCall(VecPlaceArray(bjac->y, y_array)); 649 /* apply the symmetric left portion of the inner PC operator */ 650 /* note this by-passes the inner KSP and its options completely */ 651 PetscCall(KSPGetPC(jac->ksp[0], &subpc)); 652 PetscCall(PCApplySymmetricLeft(subpc, bjac->x, bjac->y)); 653 PetscCall(VecResetArray(bjac->x)); 654 PetscCall(VecResetArray(bjac->y)); 655 PetscCall(VecRestoreArrayRead(x, &x_array)); 656 PetscCall(VecRestoreArray(y, &y_array)); 657 PetscFunctionReturn(0); 658 } 659 660 static PetscErrorCode PCApplySymmetricRight_BJacobi_Singleblock(PC pc, Vec x, Vec y) 661 { 662 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 663 PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock *)jac->data; 664 PetscScalar *y_array; 665 const PetscScalar *x_array; 666 PC subpc; 667 668 PetscFunctionBegin; 669 /* 670 The VecPlaceArray() is to avoid having to copy the 671 y vector into the bjac->x vector. The reason for 672 the bjac->x vector is that we need a sequential vector 673 for the sequential solve. 674 */ 675 PetscCall(VecGetArrayRead(x, &x_array)); 676 PetscCall(VecGetArray(y, &y_array)); 677 PetscCall(VecPlaceArray(bjac->x, x_array)); 678 PetscCall(VecPlaceArray(bjac->y, y_array)); 679 680 /* apply the symmetric right portion of the inner PC operator */ 681 /* note this by-passes the inner KSP and its options completely */ 682 683 PetscCall(KSPGetPC(jac->ksp[0], &subpc)); 684 PetscCall(PCApplySymmetricRight(subpc, bjac->x, bjac->y)); 685 686 PetscCall(VecResetArray(bjac->x)); 687 PetscCall(VecResetArray(bjac->y)); 688 PetscCall(VecRestoreArrayRead(x, &x_array)); 689 PetscCall(VecRestoreArray(y, &y_array)); 690 PetscFunctionReturn(0); 691 } 692 693 static PetscErrorCode PCApplyTranspose_BJacobi_Singleblock(PC pc, Vec x, Vec y) 694 { 695 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 696 PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock *)jac->data; 697 PetscScalar *y_array; 698 const PetscScalar *x_array; 699 700 PetscFunctionBegin; 701 /* 702 The VecPlaceArray() is to avoid having to copy the 703 y vector into the bjac->x vector. The reason for 704 the bjac->x vector is that we need a sequential vector 705 for the sequential solve. 706 */ 707 PetscCall(VecGetArrayRead(x, &x_array)); 708 PetscCall(VecGetArray(y, &y_array)); 709 PetscCall(VecPlaceArray(bjac->x, x_array)); 710 PetscCall(VecPlaceArray(bjac->y, y_array)); 711 PetscCall(KSPSolveTranspose(jac->ksp[0], bjac->x, bjac->y)); 712 PetscCall(KSPCheckSolve(jac->ksp[0], pc, bjac->y)); 713 PetscCall(VecResetArray(bjac->x)); 714 PetscCall(VecResetArray(bjac->y)); 715 PetscCall(VecRestoreArrayRead(x, &x_array)); 716 PetscCall(VecRestoreArray(y, &y_array)); 717 PetscFunctionReturn(0); 718 } 719 720 static PetscErrorCode PCSetUp_BJacobi_Singleblock(PC pc, Mat mat, Mat pmat) 721 { 722 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 723 PetscInt m; 724 KSP ksp; 725 PC_BJacobi_Singleblock *bjac; 726 PetscBool wasSetup = PETSC_TRUE; 727 VecType vectype; 728 const char *prefix; 729 730 PetscFunctionBegin; 731 if (!pc->setupcalled) { 732 if (!jac->ksp) { 733 wasSetup = PETSC_FALSE; 734 735 PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp)); 736 PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure)); 737 PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1)); 738 PetscCall(KSPSetType(ksp, KSPPREONLY)); 739 PetscCall(PCGetOptionsPrefix(pc, &prefix)); 740 PetscCall(KSPSetOptionsPrefix(ksp, prefix)); 741 PetscCall(KSPAppendOptionsPrefix(ksp, "sub_")); 742 743 pc->ops->reset = PCReset_BJacobi_Singleblock; 744 pc->ops->destroy = PCDestroy_BJacobi_Singleblock; 745 pc->ops->apply = PCApply_BJacobi_Singleblock; 746 pc->ops->matapply = PCMatApply_BJacobi_Singleblock; 747 pc->ops->applysymmetricleft = PCApplySymmetricLeft_BJacobi_Singleblock; 748 pc->ops->applysymmetricright = PCApplySymmetricRight_BJacobi_Singleblock; 749 pc->ops->applytranspose = PCApplyTranspose_BJacobi_Singleblock; 750 pc->ops->setuponblocks = PCSetUpOnBlocks_BJacobi_Singleblock; 751 752 PetscCall(PetscMalloc1(1, &jac->ksp)); 753 jac->ksp[0] = ksp; 754 755 PetscCall(PetscNew(&bjac)); 756 jac->data = (void *)bjac; 757 } else { 758 ksp = jac->ksp[0]; 759 bjac = (PC_BJacobi_Singleblock *)jac->data; 760 } 761 762 /* 763 The reason we need to generate these vectors is to serve 764 as the right-hand side and solution vector for the solve on the 765 block. We do not need to allocate space for the vectors since 766 that is provided via VecPlaceArray() just before the call to 767 KSPSolve() on the block. 768 */ 769 PetscCall(MatGetSize(pmat, &m, &m)); 770 PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, m, NULL, &bjac->x)); 771 PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, m, NULL, &bjac->y)); 772 PetscCall(MatGetVecType(pmat, &vectype)); 773 PetscCall(VecSetType(bjac->x, vectype)); 774 PetscCall(VecSetType(bjac->y, vectype)); 775 } else { 776 ksp = jac->ksp[0]; 777 bjac = (PC_BJacobi_Singleblock *)jac->data; 778 } 779 PetscCall(KSPGetOptionsPrefix(ksp, &prefix)); 780 if (pc->useAmat) { 781 PetscCall(KSPSetOperators(ksp, mat, pmat)); 782 PetscCall(MatSetOptionsPrefix(mat, prefix)); 783 } else { 784 PetscCall(KSPSetOperators(ksp, pmat, pmat)); 785 } 786 PetscCall(MatSetOptionsPrefix(pmat, prefix)); 787 if (!wasSetup && pc->setfromoptionscalled) { 788 /* If PCSetFromOptions_BJacobi is called later, KSPSetFromOptions will be called at that time. */ 789 PetscCall(KSPSetFromOptions(ksp)); 790 } 791 PetscFunctionReturn(0); 792 } 793 794 static PetscErrorCode PCReset_BJacobi_Multiblock(PC pc) 795 { 796 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 797 PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock *)jac->data; 798 PetscInt i; 799 800 PetscFunctionBegin; 801 if (bjac && bjac->pmat) { 802 PetscCall(MatDestroyMatrices(jac->n_local, &bjac->pmat)); 803 if (pc->useAmat) PetscCall(MatDestroyMatrices(jac->n_local, &bjac->mat)); 804 } 805 806 for (i = 0; i < jac->n_local; i++) { 807 PetscCall(KSPReset(jac->ksp[i])); 808 if (bjac && bjac->x) { 809 PetscCall(VecDestroy(&bjac->x[i])); 810 PetscCall(VecDestroy(&bjac->y[i])); 811 PetscCall(ISDestroy(&bjac->is[i])); 812 } 813 } 814 PetscCall(PetscFree(jac->l_lens)); 815 PetscCall(PetscFree(jac->g_lens)); 816 PetscFunctionReturn(0); 817 } 818 819 static PetscErrorCode PCDestroy_BJacobi_Multiblock(PC pc) 820 { 821 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 822 PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock *)jac->data; 823 PetscInt i; 824 825 PetscFunctionBegin; 826 PetscCall(PCReset_BJacobi_Multiblock(pc)); 827 if (bjac) { 828 PetscCall(PetscFree2(bjac->x, bjac->y)); 829 PetscCall(PetscFree(bjac->starts)); 830 PetscCall(PetscFree(bjac->is)); 831 } 832 PetscCall(PetscFree(jac->data)); 833 for (i = 0; i < jac->n_local; i++) PetscCall(KSPDestroy(&jac->ksp[i])); 834 PetscCall(PetscFree(jac->ksp)); 835 PetscCall(PCDestroy_BJacobi(pc)); 836 PetscFunctionReturn(0); 837 } 838 839 static PetscErrorCode PCSetUpOnBlocks_BJacobi_Multiblock(PC pc) 840 { 841 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 842 PetscInt i, n_local = jac->n_local; 843 KSPConvergedReason reason; 844 845 PetscFunctionBegin; 846 for (i = 0; i < n_local; i++) { 847 PetscCall(KSPSetUp(jac->ksp[i])); 848 PetscCall(KSPGetConvergedReason(jac->ksp[i], &reason)); 849 if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR; 850 } 851 PetscFunctionReturn(0); 852 } 853 854 /* 855 Preconditioner for block Jacobi 856 */ 857 static PetscErrorCode PCApply_BJacobi_Multiblock(PC pc, Vec x, Vec y) 858 { 859 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 860 PetscInt i, n_local = jac->n_local; 861 PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock *)jac->data; 862 PetscScalar *yin; 863 const PetscScalar *xin; 864 865 PetscFunctionBegin; 866 PetscCall(VecGetArrayRead(x, &xin)); 867 PetscCall(VecGetArray(y, &yin)); 868 for (i = 0; i < n_local; i++) { 869 /* 870 To avoid copying the subvector from x into a workspace we instead 871 make the workspace vector array point to the subpart of the array of 872 the global vector. 873 */ 874 PetscCall(VecPlaceArray(bjac->x[i], xin + bjac->starts[i])); 875 PetscCall(VecPlaceArray(bjac->y[i], yin + bjac->starts[i])); 876 877 PetscCall(PetscLogEventBegin(PC_ApplyOnBlocks, jac->ksp[i], bjac->x[i], bjac->y[i], 0)); 878 PetscCall(KSPSolve(jac->ksp[i], bjac->x[i], bjac->y[i])); 879 PetscCall(KSPCheckSolve(jac->ksp[i], pc, bjac->y[i])); 880 PetscCall(PetscLogEventEnd(PC_ApplyOnBlocks, jac->ksp[i], bjac->x[i], bjac->y[i], 0)); 881 882 PetscCall(VecResetArray(bjac->x[i])); 883 PetscCall(VecResetArray(bjac->y[i])); 884 } 885 PetscCall(VecRestoreArrayRead(x, &xin)); 886 PetscCall(VecRestoreArray(y, &yin)); 887 PetscFunctionReturn(0); 888 } 889 890 /* 891 Preconditioner for block Jacobi 892 */ 893 static PetscErrorCode PCApplyTranspose_BJacobi_Multiblock(PC pc, Vec x, Vec y) 894 { 895 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 896 PetscInt i, n_local = jac->n_local; 897 PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock *)jac->data; 898 PetscScalar *yin; 899 const PetscScalar *xin; 900 901 PetscFunctionBegin; 902 PetscCall(VecGetArrayRead(x, &xin)); 903 PetscCall(VecGetArray(y, &yin)); 904 for (i = 0; i < n_local; i++) { 905 /* 906 To avoid copying the subvector from x into a workspace we instead 907 make the workspace vector array point to the subpart of the array of 908 the global vector. 909 */ 910 PetscCall(VecPlaceArray(bjac->x[i], xin + bjac->starts[i])); 911 PetscCall(VecPlaceArray(bjac->y[i], yin + bjac->starts[i])); 912 913 PetscCall(PetscLogEventBegin(PC_ApplyTransposeOnBlocks, jac->ksp[i], bjac->x[i], bjac->y[i], 0)); 914 PetscCall(KSPSolveTranspose(jac->ksp[i], bjac->x[i], bjac->y[i])); 915 PetscCall(KSPCheckSolve(jac->ksp[i], pc, bjac->y[i])); 916 PetscCall(PetscLogEventEnd(PC_ApplyTransposeOnBlocks, jac->ksp[i], bjac->x[i], bjac->y[i], 0)); 917 918 PetscCall(VecResetArray(bjac->x[i])); 919 PetscCall(VecResetArray(bjac->y[i])); 920 } 921 PetscCall(VecRestoreArrayRead(x, &xin)); 922 PetscCall(VecRestoreArray(y, &yin)); 923 PetscFunctionReturn(0); 924 } 925 926 static PetscErrorCode PCSetUp_BJacobi_Multiblock(PC pc, Mat mat, Mat pmat) 927 { 928 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 929 PetscInt m, n_local, N, M, start, i; 930 const char *prefix; 931 KSP ksp; 932 Vec x, y; 933 PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock *)jac->data; 934 PC subpc; 935 IS is; 936 MatReuse scall; 937 VecType vectype; 938 939 PetscFunctionBegin; 940 PetscCall(MatGetLocalSize(pc->pmat, &M, &N)); 941 942 n_local = jac->n_local; 943 944 if (pc->useAmat) { 945 PetscBool same; 946 PetscCall(PetscObjectTypeCompare((PetscObject)mat, ((PetscObject)pmat)->type_name, &same)); 947 PetscCheck(same, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "Matrices not of same type"); 948 } 949 950 if (!pc->setupcalled) { 951 scall = MAT_INITIAL_MATRIX; 952 953 if (!jac->ksp) { 954 pc->ops->reset = PCReset_BJacobi_Multiblock; 955 pc->ops->destroy = PCDestroy_BJacobi_Multiblock; 956 pc->ops->apply = PCApply_BJacobi_Multiblock; 957 pc->ops->matapply = NULL; 958 pc->ops->applytranspose = PCApplyTranspose_BJacobi_Multiblock; 959 pc->ops->setuponblocks = PCSetUpOnBlocks_BJacobi_Multiblock; 960 961 PetscCall(PetscNew(&bjac)); 962 PetscCall(PetscMalloc1(n_local, &jac->ksp)); 963 PetscCall(PetscMalloc2(n_local, &bjac->x, n_local, &bjac->y)); 964 PetscCall(PetscMalloc1(n_local, &bjac->starts)); 965 966 jac->data = (void *)bjac; 967 PetscCall(PetscMalloc1(n_local, &bjac->is)); 968 969 for (i = 0; i < n_local; i++) { 970 PetscCall(KSPCreate(PETSC_COMM_SELF, &ksp)); 971 PetscCall(KSPSetErrorIfNotConverged(ksp, pc->erroriffailure)); 972 PetscCall(PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject)pc, 1)); 973 PetscCall(KSPSetType(ksp, KSPPREONLY)); 974 PetscCall(KSPGetPC(ksp, &subpc)); 975 PetscCall(PCGetOptionsPrefix(pc, &prefix)); 976 PetscCall(KSPSetOptionsPrefix(ksp, prefix)); 977 PetscCall(KSPAppendOptionsPrefix(ksp, "sub_")); 978 979 jac->ksp[i] = ksp; 980 } 981 } else { 982 bjac = (PC_BJacobi_Multiblock *)jac->data; 983 } 984 985 start = 0; 986 PetscCall(MatGetVecType(pmat, &vectype)); 987 for (i = 0; i < n_local; i++) { 988 m = jac->l_lens[i]; 989 /* 990 The reason we need to generate these vectors is to serve 991 as the right-hand side and solution vector for the solve on the 992 block. We do not need to allocate space for the vectors since 993 that is provided via VecPlaceArray() just before the call to 994 KSPSolve() on the block. 995 996 */ 997 PetscCall(VecCreateSeq(PETSC_COMM_SELF, m, &x)); 998 PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, m, NULL, &y)); 999 PetscCall(VecSetType(x, vectype)); 1000 PetscCall(VecSetType(y, vectype)); 1001 1002 bjac->x[i] = x; 1003 bjac->y[i] = y; 1004 bjac->starts[i] = start; 1005 1006 PetscCall(ISCreateStride(PETSC_COMM_SELF, m, start, 1, &is)); 1007 bjac->is[i] = is; 1008 1009 start += m; 1010 } 1011 } else { 1012 bjac = (PC_BJacobi_Multiblock *)jac->data; 1013 /* 1014 Destroy the blocks from the previous iteration 1015 */ 1016 if (pc->flag == DIFFERENT_NONZERO_PATTERN) { 1017 PetscCall(MatDestroyMatrices(n_local, &bjac->pmat)); 1018 if (pc->useAmat) PetscCall(MatDestroyMatrices(n_local, &bjac->mat)); 1019 scall = MAT_INITIAL_MATRIX; 1020 } else scall = MAT_REUSE_MATRIX; 1021 } 1022 1023 PetscCall(MatCreateSubMatrices(pmat, n_local, bjac->is, bjac->is, scall, &bjac->pmat)); 1024 if (pc->useAmat) PetscCall(MatCreateSubMatrices(mat, n_local, bjac->is, bjac->is, scall, &bjac->mat)); 1025 /* Return control to the user so that the submatrices can be modified (e.g., to apply 1026 different boundary conditions for the submatrices than for the global problem) */ 1027 PetscCall(PCModifySubMatrices(pc, n_local, bjac->is, bjac->is, bjac->pmat, pc->modifysubmatricesP)); 1028 1029 for (i = 0; i < n_local; i++) { 1030 PetscCall(KSPGetOptionsPrefix(jac->ksp[i], &prefix)); 1031 if (pc->useAmat) { 1032 PetscCall(KSPSetOperators(jac->ksp[i], bjac->mat[i], bjac->pmat[i])); 1033 PetscCall(MatSetOptionsPrefix(bjac->mat[i], prefix)); 1034 } else { 1035 PetscCall(KSPSetOperators(jac->ksp[i], bjac->pmat[i], bjac->pmat[i])); 1036 } 1037 PetscCall(MatSetOptionsPrefix(bjac->pmat[i], prefix)); 1038 if (pc->setfromoptionscalled) PetscCall(KSPSetFromOptions(jac->ksp[i])); 1039 } 1040 PetscFunctionReturn(0); 1041 } 1042 1043 /* 1044 These are for a single block with multiple processes 1045 */ 1046 static PetscErrorCode PCSetUpOnBlocks_BJacobi_Multiproc(PC pc) 1047 { 1048 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 1049 KSP subksp = jac->ksp[0]; 1050 KSPConvergedReason reason; 1051 1052 PetscFunctionBegin; 1053 PetscCall(KSPSetUp(subksp)); 1054 PetscCall(KSPGetConvergedReason(subksp, &reason)); 1055 if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR; 1056 PetscFunctionReturn(0); 1057 } 1058 1059 static PetscErrorCode PCReset_BJacobi_Multiproc(PC pc) 1060 { 1061 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 1062 PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc *)jac->data; 1063 1064 PetscFunctionBegin; 1065 PetscCall(VecDestroy(&mpjac->ysub)); 1066 PetscCall(VecDestroy(&mpjac->xsub)); 1067 PetscCall(MatDestroy(&mpjac->submats)); 1068 if (jac->ksp) PetscCall(KSPReset(jac->ksp[0])); 1069 PetscFunctionReturn(0); 1070 } 1071 1072 static PetscErrorCode PCDestroy_BJacobi_Multiproc(PC pc) 1073 { 1074 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 1075 PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc *)jac->data; 1076 1077 PetscFunctionBegin; 1078 PetscCall(PCReset_BJacobi_Multiproc(pc)); 1079 PetscCall(KSPDestroy(&jac->ksp[0])); 1080 PetscCall(PetscFree(jac->ksp)); 1081 PetscCall(PetscSubcommDestroy(&mpjac->psubcomm)); 1082 1083 PetscCall(PetscFree(mpjac)); 1084 PetscCall(PCDestroy_BJacobi(pc)); 1085 PetscFunctionReturn(0); 1086 } 1087 1088 static PetscErrorCode PCApply_BJacobi_Multiproc(PC pc, Vec x, Vec y) 1089 { 1090 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 1091 PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc *)jac->data; 1092 PetscScalar *yarray; 1093 const PetscScalar *xarray; 1094 KSPConvergedReason reason; 1095 1096 PetscFunctionBegin; 1097 /* place x's and y's local arrays into xsub and ysub */ 1098 PetscCall(VecGetArrayRead(x, &xarray)); 1099 PetscCall(VecGetArray(y, &yarray)); 1100 PetscCall(VecPlaceArray(mpjac->xsub, xarray)); 1101 PetscCall(VecPlaceArray(mpjac->ysub, yarray)); 1102 1103 /* apply preconditioner on each matrix block */ 1104 PetscCall(PetscLogEventBegin(PC_ApplyOnBlocks, jac->ksp[0], mpjac->xsub, mpjac->ysub, 0)); 1105 PetscCall(KSPSolve(jac->ksp[0], mpjac->xsub, mpjac->ysub)); 1106 PetscCall(KSPCheckSolve(jac->ksp[0], pc, mpjac->ysub)); 1107 PetscCall(PetscLogEventEnd(PC_ApplyOnBlocks, jac->ksp[0], mpjac->xsub, mpjac->ysub, 0)); 1108 PetscCall(KSPGetConvergedReason(jac->ksp[0], &reason)); 1109 if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR; 1110 1111 PetscCall(VecResetArray(mpjac->xsub)); 1112 PetscCall(VecResetArray(mpjac->ysub)); 1113 PetscCall(VecRestoreArrayRead(x, &xarray)); 1114 PetscCall(VecRestoreArray(y, &yarray)); 1115 PetscFunctionReturn(0); 1116 } 1117 1118 static PetscErrorCode PCMatApply_BJacobi_Multiproc(PC pc, Mat X, Mat Y) 1119 { 1120 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 1121 KSPConvergedReason reason; 1122 Mat sX, sY; 1123 const PetscScalar *x; 1124 PetscScalar *y; 1125 PetscInt m, N, lda, ldb; 1126 1127 PetscFunctionBegin; 1128 /* apply preconditioner on each matrix block */ 1129 PetscCall(MatGetLocalSize(X, &m, NULL)); 1130 PetscCall(MatGetSize(X, NULL, &N)); 1131 PetscCall(MatDenseGetLDA(X, &lda)); 1132 PetscCall(MatDenseGetLDA(Y, &ldb)); 1133 PetscCall(MatDenseGetArrayRead(X, &x)); 1134 PetscCall(MatDenseGetArrayWrite(Y, &y)); 1135 PetscCall(MatCreateDense(PetscObjectComm((PetscObject)jac->ksp[0]), m, PETSC_DECIDE, PETSC_DECIDE, N, (PetscScalar *)x, &sX)); 1136 PetscCall(MatCreateDense(PetscObjectComm((PetscObject)jac->ksp[0]), m, PETSC_DECIDE, PETSC_DECIDE, N, y, &sY)); 1137 PetscCall(MatDenseSetLDA(sX, lda)); 1138 PetscCall(MatDenseSetLDA(sY, ldb)); 1139 PetscCall(PetscLogEventBegin(PC_ApplyOnBlocks, jac->ksp[0], X, Y, 0)); 1140 PetscCall(KSPMatSolve(jac->ksp[0], sX, sY)); 1141 PetscCall(KSPCheckSolve(jac->ksp[0], pc, NULL)); 1142 PetscCall(PetscLogEventEnd(PC_ApplyOnBlocks, jac->ksp[0], X, Y, 0)); 1143 PetscCall(MatDestroy(&sY)); 1144 PetscCall(MatDestroy(&sX)); 1145 PetscCall(MatDenseRestoreArrayWrite(Y, &y)); 1146 PetscCall(MatDenseRestoreArrayRead(X, &x)); 1147 PetscCall(KSPGetConvergedReason(jac->ksp[0], &reason)); 1148 if (reason == KSP_DIVERGED_PC_FAILED) pc->failedreason = PC_SUBPC_ERROR; 1149 PetscFunctionReturn(0); 1150 } 1151 1152 static PetscErrorCode PCSetUp_BJacobi_Multiproc(PC pc) 1153 { 1154 PC_BJacobi *jac = (PC_BJacobi *)pc->data; 1155 PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc *)jac->data; 1156 PetscInt m, n; 1157 MPI_Comm comm, subcomm = 0; 1158 const char *prefix; 1159 PetscBool wasSetup = PETSC_TRUE; 1160 VecType vectype; 1161 1162 PetscFunctionBegin; 1163 PetscCall(PetscObjectGetComm((PetscObject)pc, &comm)); 1164 PetscCheck(jac->n_local <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Only a single block in a subcommunicator is supported"); 1165 jac->n_local = 1; /* currently only a single block is supported for a subcommunicator */ 1166 if (!pc->setupcalled) { 1167 wasSetup = PETSC_FALSE; 1168 PetscCall(PetscNew(&mpjac)); 1169 jac->data = (void *)mpjac; 1170 1171 /* initialize datastructure mpjac */ 1172 if (!jac->psubcomm) { 1173 /* Create default contiguous subcommunicatiors if user does not provide them */ 1174 PetscCall(PetscSubcommCreate(comm, &jac->psubcomm)); 1175 PetscCall(PetscSubcommSetNumber(jac->psubcomm, jac->n)); 1176 PetscCall(PetscSubcommSetType(jac->psubcomm, PETSC_SUBCOMM_CONTIGUOUS)); 1177 } 1178 mpjac->psubcomm = jac->psubcomm; 1179 subcomm = PetscSubcommChild(mpjac->psubcomm); 1180 1181 /* Get matrix blocks of pmat */ 1182 PetscCall(MatGetMultiProcBlock(pc->pmat, subcomm, MAT_INITIAL_MATRIX, &mpjac->submats)); 1183 1184 /* create a new PC that processors in each subcomm have copy of */ 1185 PetscCall(PetscMalloc1(1, &jac->ksp)); 1186 PetscCall(KSPCreate(subcomm, &jac->ksp[0])); 1187 PetscCall(KSPSetErrorIfNotConverged(jac->ksp[0], pc->erroriffailure)); 1188 PetscCall(PetscObjectIncrementTabLevel((PetscObject)jac->ksp[0], (PetscObject)pc, 1)); 1189 PetscCall(KSPSetOperators(jac->ksp[0], mpjac->submats, mpjac->submats)); 1190 PetscCall(KSPGetPC(jac->ksp[0], &mpjac->pc)); 1191 1192 PetscCall(PCGetOptionsPrefix(pc, &prefix)); 1193 PetscCall(KSPSetOptionsPrefix(jac->ksp[0], prefix)); 1194 PetscCall(KSPAppendOptionsPrefix(jac->ksp[0], "sub_")); 1195 PetscCall(KSPGetOptionsPrefix(jac->ksp[0], &prefix)); 1196 PetscCall(MatSetOptionsPrefix(mpjac->submats, prefix)); 1197 1198 /* create dummy vectors xsub and ysub */ 1199 PetscCall(MatGetLocalSize(mpjac->submats, &m, &n)); 1200 PetscCall(VecCreateMPIWithArray(subcomm, 1, n, PETSC_DECIDE, NULL, &mpjac->xsub)); 1201 PetscCall(VecCreateMPIWithArray(subcomm, 1, m, PETSC_DECIDE, NULL, &mpjac->ysub)); 1202 PetscCall(MatGetVecType(mpjac->submats, &vectype)); 1203 PetscCall(VecSetType(mpjac->xsub, vectype)); 1204 PetscCall(VecSetType(mpjac->ysub, vectype)); 1205 1206 pc->ops->setuponblocks = PCSetUpOnBlocks_BJacobi_Multiproc; 1207 pc->ops->reset = PCReset_BJacobi_Multiproc; 1208 pc->ops->destroy = PCDestroy_BJacobi_Multiproc; 1209 pc->ops->apply = PCApply_BJacobi_Multiproc; 1210 pc->ops->matapply = PCMatApply_BJacobi_Multiproc; 1211 } else { /* pc->setupcalled */ 1212 subcomm = PetscSubcommChild(mpjac->psubcomm); 1213 if (pc->flag == DIFFERENT_NONZERO_PATTERN) { 1214 /* destroy old matrix blocks, then get new matrix blocks */ 1215 if (mpjac->submats) PetscCall(MatDestroy(&mpjac->submats)); 1216 PetscCall(MatGetMultiProcBlock(pc->pmat, subcomm, MAT_INITIAL_MATRIX, &mpjac->submats)); 1217 } else { 1218 PetscCall(MatGetMultiProcBlock(pc->pmat, subcomm, MAT_REUSE_MATRIX, &mpjac->submats)); 1219 } 1220 PetscCall(KSPSetOperators(jac->ksp[0], mpjac->submats, mpjac->submats)); 1221 } 1222 1223 if (!wasSetup && pc->setfromoptionscalled) PetscCall(KSPSetFromOptions(jac->ksp[0])); 1224 PetscFunctionReturn(0); 1225 } 1226