1 #include "petscdevice_interface_internal.hpp" /*I <petscdevice.h> I*/ 2 #include <petsc/private/viewerimpl.h> // _p_PetscViewer for PetscObjectCast() 3 4 #include <petsc/private/cpp/object_pool.hpp> 5 #include <petsc/private/cpp/utility.hpp> 6 #include <petsc/private/cpp/array.hpp> 7 8 #include <vector> 9 #include <string> // std::to_string among other things 10 11 /* Define the allocator */ 12 class PetscDeviceContextAllocator : public Petsc::AllocatorBase<PetscDeviceContext> { 13 public: 14 PETSC_CXX_COMPAT_DECL(PetscErrorCode create(PetscDeviceContext *dctx)) 15 { 16 PetscFunctionBegin; 17 PetscCall(PetscHeaderCreate(*dctx, PETSC_DEVICE_CONTEXT_CLASSID, "PetscDeviceContext", "PetscDeviceContext", "Sys", PETSC_COMM_SELF, PetscDeviceContextDestroy, PetscDeviceContextView)); 18 PetscCallCXX(PetscObjectCast(*dctx)->cpp = new CxxData()); 19 PetscCall(reset(*dctx, false)); 20 PetscFunctionReturn(0); 21 } 22 23 PETSC_CXX_COMPAT_DECL(PetscErrorCode destroy(PetscDeviceContext dctx)) 24 { 25 PetscFunctionBegin; 26 PetscAssert(!dctx->numChildren, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Device context still has %" PetscInt_FMT " un-joined children, must call PetscDeviceContextJoin() with all children before destroying", dctx->numChildren); 27 PetscTryTypeMethod(dctx, destroy); 28 PetscCall(PetscDeviceDestroy(&dctx->device)); 29 PetscCall(PetscFree(dctx->childIDs)); 30 delete CxxDataCast(dctx); 31 PetscCall(PetscHeaderDestroy(&dctx)); 32 PetscFunctionReturn(0); 33 } 34 35 PETSC_CXX_COMPAT_DECL(PetscErrorCode reset(PetscDeviceContext dctx, bool zero = true)) 36 { 37 PetscFunctionBegin; 38 if (zero) { 39 // reset the device if the user set it 40 if (auto &userset = dctx->usersetdevice) { 41 userset = PETSC_FALSE; 42 PetscTryTypeMethod(dctx, destroy); 43 PetscCall(PetscDeviceDestroy(&dctx->device)); 44 PetscCall(PetscArrayzero(dctx->ops, 1)); 45 dctx->data = nullptr; 46 } 47 PetscCall(PetscHeaderReset_Internal(PetscObjectCast(dctx))); 48 dctx->numChildren = 0; 49 dctx->setup = PETSC_FALSE; 50 // don't deallocate the child array, rather just zero it out 51 PetscCall(PetscArrayzero(dctx->childIDs, dctx->maxNumChildren)); 52 PetscCall(CxxDataCast(dctx)->clear()); 53 } 54 dctx->streamType = PETSC_STREAM_DEFAULT_BLOCKING; 55 PetscFunctionReturn(0); 56 } 57 }; 58 59 static Petsc::ObjectPool<PetscDeviceContext, PetscDeviceContextAllocator> contextPool; 60 61 /*@C 62 PetscDeviceContextCreate - Creates a `PetscDeviceContext` 63 64 Not Collective 65 66 Output Parameter: 67 . dctx - The `PetscDeviceContext` 68 69 Note: 70 Unlike almost every other PETSc class it is advised that most users use 71 `PetscDeviceContextDuplicate()` rather than this routine to create new contexts. Contexts of 72 different types are incompatible with one another; using `PetscDeviceContextDuplicate()` 73 ensures compatible types. 74 75 DAG representation: 76 .vb 77 time -> 78 79 |= CALL =| - dctx -> 80 .ve 81 82 Level: beginner 83 84 .N ASYNC_API 85 86 .seealso: `PetscDeviceContextDuplicate()`, `PetscDeviceContextSetDevice()`, 87 `PetscDeviceContextSetStreamType()`, `PetscDeviceContextSetUp()`, 88 `PetscDeviceContextSetFromOptions()`, `PetscDeviceContextView()`, `PetscDeviceContextDestroy()` 89 @*/ 90 PetscErrorCode PetscDeviceContextCreate(PetscDeviceContext *dctx) 91 { 92 PetscFunctionBegin; 93 PetscValidPointer(dctx, 1); 94 PetscCall(PetscDeviceInitializePackage()); 95 PetscCall(PetscLogEventBegin(DCONTEXT_Create, nullptr, nullptr, nullptr, nullptr)); 96 PetscCall(contextPool.allocate(dctx)); 97 PetscCall(PetscLogEventEnd(DCONTEXT_Create, nullptr, nullptr, nullptr, nullptr)); 98 PetscFunctionReturn(0); 99 } 100 101 /*@C 102 PetscDeviceContextDestroy - Frees a `PetscDeviceContext` 103 104 Not Collective 105 106 Input Parameters: 107 . dctx - The `PetscDeviceContext` 108 109 Notes: 110 No implicit synchronization occurs due to this routine, all resources are released completely 111 asynchronously w.r.t. the host. If one needs to guarantee access to the data produced on 112 `dctx`'s stream the user is responsible for calling `PetscDeviceContextSynchronize()` before 113 calling this routine. 114 115 DAG represetation: 116 .vb 117 time -> 118 119 -> dctx - |= CALL =| 120 .ve 121 122 Developer Notes: 123 `dctx` is never actually "destroyed" in the classical sense. It is returned to an ever 124 growing pool of `PetscDeviceContext`s. There are currently no limits on the size of the pool, 125 this should perhaps be implemented. 126 127 Level: beginner 128 129 .N ASYNC_API 130 131 .seealso: `PetscDeviceContextCreate()`, `PetscDeviceContextSetDevice()`, 132 `PetscDeviceContextSetUp()`, `PetscDeviceContextSynchronize()` 133 @*/ 134 PetscErrorCode PetscDeviceContextDestroy(PetscDeviceContext *dctx) 135 { 136 PetscFunctionBegin; 137 PetscValidPointer(dctx, 1); 138 if (!*dctx) PetscFunctionReturn(0); 139 PetscCall(PetscLogEventBegin(DCONTEXT_Destroy, nullptr, nullptr, nullptr, nullptr)); 140 if (--(PetscObjectCast(*dctx)->refct) <= 0) { 141 PetscCall(PetscDeviceContextCheckNotOrphaned_Internal(*dctx)); 142 // std::move of the expression of the trivially-copyable type 'PetscDeviceContext' (aka 143 // '_n_PetscDeviceContext *') has no effect; remove std::move() [performance-move-const-arg] 144 // can't remove std::move, since reclaim only takes r-value reference 145 PetscCall(contextPool.deallocate(std::move(*dctx))); // NOLINT (performance-move-const-arg) 146 } 147 PetscCall(PetscLogEventEnd(DCONTEXT_Destroy, nullptr, nullptr, nullptr, nullptr)); 148 *dctx = nullptr; 149 PetscFunctionReturn(0); 150 } 151 152 /*@C 153 PetscDeviceContextSetStreamType - Set the implementation type of the underlying stream for a 154 `PetscDeviceContext` 155 156 Not Collective 157 158 Input Parameters: 159 + dctx - The `PetscDeviceContext` 160 - type - The `PetscStreamType` 161 162 Notes: 163 See `PetscStreamType` in `include/petscdevicetypes.h` for more information on the available 164 types and their interactions. If the `PetscDeviceContext` was previously set up and stream 165 type was changed, you must call `PetscDeviceContextSetUp()` again after this routine. 166 167 Level: beginner 168 169 .seealso: `PetscStreamType`, `PetscDeviceContextGetStreamType()`, `PetscDeviceContextCreate()`, 170 `PetscDeviceContextSetUp()`, `PetscDeviceContextSetFromOptions()` 171 @*/ 172 PetscErrorCode PetscDeviceContextSetStreamType(PetscDeviceContext dctx, PetscStreamType type) 173 { 174 PetscFunctionBegin; 175 // do not use getoptionalnullcontext here since we do not want the user to change the stream 176 // type 177 PetscValidDeviceContext(dctx, 1); 178 PetscValidStreamType(type, 2); 179 // only need to do complex swapping if the object has already been setup 180 if (dctx->setup && (dctx->streamType != type)) { 181 dctx->setup = PETSC_FALSE; 182 PetscCall(PetscLogEventBegin(DCONTEXT_ChangeStream, dctx, nullptr, nullptr, nullptr)); 183 PetscUseTypeMethod(dctx, changestreamtype, type); 184 PetscCall(PetscLogEventEnd(DCONTEXT_ChangeStream, dctx, nullptr, nullptr, nullptr)); 185 } 186 dctx->streamType = type; 187 PetscFunctionReturn(0); 188 } 189 190 /*@C 191 PetscDeviceContextGetStreamType - Get the implementation type of the underlying stream for a 192 `PetscDeviceContext` 193 194 Not Collective 195 196 Input Parameter: 197 . dctx - The `PetscDeviceContext` 198 199 Output Parameter: 200 . type - The `PetscStreamType` 201 202 Notes: 203 See `PetscStreamType` in `include/petscdevicetypes.h` for more information on the available 204 types and their interactions 205 206 Level: beginner 207 208 .seealso: `PetscDeviceContextSetStreamType()`, `PetscDeviceContextCreate()`, 209 `PetscDeviceContextSetFromOptions()` 210 @*/ 211 PetscErrorCode PetscDeviceContextGetStreamType(PetscDeviceContext dctx, PetscStreamType *type) 212 { 213 PetscFunctionBegin; 214 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx)); 215 PetscValidIntPointer(type, 2); 216 *type = dctx->streamType; 217 PetscFunctionReturn(0); 218 } 219 220 /* 221 Actual function to set the device. 222 223 1. Repeatedly destroying and recreating internal data structures (like streams and events) 224 for recycled PetscDeviceContexts is not free. If done often, it does add up. 225 2. The vast majority of PetscDeviceContexts are created by PETSc either as children or 226 default contexts. The default contexts *never* change type, and the chilren are extremely 227 unlikely to (chances are if you fork once, you will fork again very soon). 228 3. The only time this calculus changes is if the user themselves sets the device type. In 229 this case we do not know what the user has changed, so must always wipe the slate clean. 230 231 Thus we need to keep track whether the user explicitly sets the device contexts device. 232 */ 233 static PetscErrorCode PetscDeviceContextSetDevice_Private(PetscDeviceContext dctx, PetscDevice device, PetscBool user_set) 234 { 235 PetscFunctionBegin; 236 // do not use getoptionalnullcontext here since we do not want the user to change its device 237 PetscValidDeviceContext(dctx, 1); 238 PetscValidDevice(device, 2); 239 if (dctx->device && (dctx->device->id == device->id)) PetscFunctionReturn(0); 240 PetscCall(PetscLogEventBegin(DCONTEXT_SetDevice, dctx, nullptr, nullptr, nullptr)); 241 if (const auto destroy = dctx->ops->destroy) PetscCall((*destroy)(dctx)); 242 PetscCall(PetscDeviceDestroy(&dctx->device)); 243 PetscCall(PetscMemzero(dctx->ops, sizeof(*dctx->ops))); 244 PetscCall((*device->ops->createcontext)(dctx)); 245 PetscCall(PetscLogEventEnd(DCONTEXT_SetDevice, dctx, nullptr, nullptr, nullptr)); 246 PetscCall(PetscDeviceReference_Internal(device)); 247 dctx->device = device; 248 dctx->setup = PETSC_FALSE; 249 dctx->usersetdevice = user_set; 250 PetscFunctionReturn(0); 251 } 252 253 PetscErrorCode PetscDeviceContextSetDefaultDeviceForType_Internal(PetscDeviceContext dctx, PetscDeviceType type) 254 { 255 PetscDevice device; 256 257 PetscFunctionBegin; 258 PetscCall(PetscDeviceGetDefaultForType_Internal(type, &device)); 259 PetscCall(PetscDeviceContextSetDevice_Private(dctx, device, PETSC_FALSE)); 260 PetscFunctionReturn(0); 261 } 262 263 /*@C 264 PetscDeviceContextSetDevice - Set the underlying `PetscDevice` for a `PetscDeviceContext` 265 266 Not Collective 267 268 Input Parameters: 269 + dctx - The `PetscDeviceContext` 270 - device - The `PetscDevice` 271 272 Notes: 273 This routine is effectively `PetscDeviceContext`'s "set-type" (so every `PetscDeviceContext` must 274 also have an attached `PetscDevice`). Unlike the usual set-type semantics, it is not stricly 275 necessary to set a contexts device to enable usage, any created `PetscDeviceContext`s will 276 always come equipped with the "default" device. 277 278 This routine is a no-op if `device` is already attached to `dctx`. 279 280 This routine may (but is very unlikely to) initialize the backend device and may incur 281 synchronization. 282 283 Level: intermediate 284 285 .seealso: `PetscDeviceCreate()`, `PetscDeviceConfigure()`, `PetscDeviceContextGetDevice()`, 286 `PetscDeviceContextGetDeviceType()` 287 @*/ 288 PetscErrorCode PetscDeviceContextSetDevice(PetscDeviceContext dctx, PetscDevice device) 289 { 290 PetscFunctionBegin; 291 PetscCall(PetscDeviceContextSetDevice_Private(dctx, device, PETSC_TRUE)); 292 PetscFunctionReturn(0); 293 } 294 295 /*@C 296 PetscDeviceContextGetDevice - Get the underlying `PetscDevice` for a `PetscDeviceContext` 297 298 Not Collective 299 300 Input Parameter: 301 . dctx - the `PetscDeviceContext` 302 303 Output Parameter: 304 . device - The `PetscDevice` 305 306 Notes: 307 This is a borrowed reference, the user should not destroy `device`. 308 309 Level: intermediate 310 311 .seealso: `PetscDeviceContextSetDevice()`, `PetscDevice`, `PetscDeviceContextGetDeviceType()` 312 @*/ 313 PetscErrorCode PetscDeviceContextGetDevice(PetscDeviceContext dctx, PetscDevice *device) 314 { 315 PetscFunctionBegin; 316 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx)); 317 PetscValidPointer(device, 2); 318 PetscAssert(dctx->device, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "PetscDeviceContext %" PetscInt64_FMT " has no attached PetscDevice to get", PetscObjectCast(dctx)->id); 319 *device = dctx->device; 320 PetscFunctionReturn(0); 321 } 322 323 /*@C 324 PetscDeviceContextGetDeviceType - Get the `PetscDeviceType` for a `PetscDeviceContext` 325 326 Not Collective 327 328 Input Parameter: 329 . dctx - The `PetscDeviceContext` 330 331 Output Parameter: 332 . type - The `PetscDeviceType` 333 334 Notes: 335 This routine is a convenience shorthand for `PetscDeviceContextGetDevice()` -> 336 `PetscDeviceGetType()`. 337 338 Level: beginner 339 340 .seealso: `PetscDeviceType`, `PetscDeviceContextGetDevice()`, `PetscDeviceGetType()`, `PetscDevice` 341 @*/ 342 PetscErrorCode PetscDeviceContextGetDeviceType(PetscDeviceContext dctx, PetscDeviceType *type) 343 { 344 PetscDevice device = nullptr; 345 346 PetscFunctionBegin; 347 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx)); 348 PetscValidPointer(type, 2); 349 PetscCall(PetscDeviceContextGetDevice(dctx, &device)); 350 PetscCall(PetscDeviceGetType(device, type)); 351 PetscFunctionReturn(0); 352 } 353 354 /*@C 355 PetscDeviceContextSetUp - Prepares a `PetscDeviceContext` for use 356 357 Not Collective 358 359 Input Parameter: 360 . dctx - The `PetscDeviceContext` 361 362 Developer Notes: 363 This routine is usually the stage where a `PetscDeviceContext` acquires device-side data 364 structures such as streams, events, and (possibly) handles. 365 366 Level: beginner 367 368 .seealso: `PetscDeviceContextCreate()`, `PetscDeviceContextSetDevice()`, 369 `PetscDeviceContextDestroy()`, `PetscDeviceContextSetFromOptions()` 370 @*/ 371 PetscErrorCode PetscDeviceContextSetUp(PetscDeviceContext dctx) 372 { 373 PetscFunctionBegin; 374 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx)); 375 if (dctx->setup) PetscFunctionReturn(0); 376 if (!dctx->device) { 377 const auto default_dtype = PETSC_DEVICE_DEFAULT(); 378 379 PetscCall(PetscInfo(dctx, "PetscDeviceContext %" PetscInt64_FMT " did not have an explicitly attached PetscDevice, using default with type %s\n", PetscObjectCast(dctx)->id, PetscDeviceTypes[default_dtype])); 380 PetscCall(PetscDeviceContextSetDefaultDeviceForType_Internal(dctx, default_dtype)); 381 } 382 PetscCall(PetscLogEventBegin(DCONTEXT_SetUp, dctx, nullptr, nullptr, nullptr)); 383 PetscUseTypeMethod(dctx, setup); 384 PetscCall(PetscLogEventEnd(DCONTEXT_SetUp, dctx, nullptr, nullptr, nullptr)); 385 dctx->setup = PETSC_TRUE; 386 PetscFunctionReturn(0); 387 } 388 389 static PetscErrorCode PetscDeviceContextDuplicate_Private(PetscDeviceContext dctx, PetscStreamType stype, PetscDeviceContext *dctxdup) 390 { 391 PetscFunctionBegin; 392 PetscCall(PetscLogEventBegin(DCONTEXT_Duplicate, dctx, nullptr, nullptr, nullptr)); 393 PetscCall(PetscDeviceContextCreate(dctxdup)); 394 PetscCall(PetscDeviceContextSetStreamType(*dctxdup, stype)); 395 if (const auto device = dctx->device) PetscCall(PetscDeviceContextSetDevice_Private(*dctxdup, device, dctx->usersetdevice)); 396 PetscCall(PetscDeviceContextSetUp(*dctxdup)); 397 PetscCall(PetscLogEventEnd(DCONTEXT_Duplicate, dctx, nullptr, nullptr, nullptr)); 398 PetscFunctionReturn(0); 399 } 400 401 /*@C 402 PetscDeviceContextDuplicate - Duplicates a `PetscDeviceContext` object 403 404 Not Collective 405 406 Input Parameter: 407 . dctx - The `PetscDeviceContext` to duplicate 408 409 Output Parameter: 410 . dctxdup - The duplicated `PetscDeviceContext` 411 412 Notes: 413 This is a shorthand method for creating a `PetscDeviceContext` with the exact same settings as 414 another. Note however that `dctxdup` does not share any of the underlying data with `dctx`, 415 (including its current stream-state) they are completely separate objects. 416 417 There is no implied ordering between `dctx` or `dctxdup`. 418 419 DAG representation: 420 .vb 421 time -> 422 423 -> dctx - |= CALL =| - dctx ----> 424 - dctxdup -> 425 .ve 426 427 Level: beginner 428 429 .seealso: `PetscDeviceContextCreate()`, `PetscDeviceContextSetDevice()`, 430 `PetscDeviceContextSetStreamType()` 431 @*/ 432 PetscErrorCode PetscDeviceContextDuplicate(PetscDeviceContext dctx, PetscDeviceContext *dctxdup) 433 { 434 auto stype = PETSC_STREAM_DEFAULT_BLOCKING; 435 436 PetscFunctionBegin; 437 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx)); 438 PetscValidPointer(dctxdup, 2); 439 PetscCall(PetscDeviceContextGetStreamType(dctx, &stype)); 440 PetscCall(PetscDeviceContextDuplicate_Private(dctx, stype, dctxdup)); 441 PetscFunctionReturn(0); 442 } 443 444 /*@C 445 PetscDeviceContextQueryIdle - Returns whether or not a `PetscDeviceContext` is idle 446 447 Not Collective 448 449 Input Parameter: 450 . dctx - The `PetscDeviceContext` 451 452 Output Parameter: 453 . idle - `PETSC_TRUE` if `dctx` has NO work, `PETSC_FALSE` if it has work 454 455 Note: 456 This routine only refers a singular context and does NOT take any of its children into 457 account. That is, if `dctx` is idle but has dependents who do have work this routine still 458 returns `PETSC_TRUE`. 459 460 Level: intermediate 461 462 .seealso: `PetscDeviceContextCreate()`, `PetscDeviceContextWaitForContext()`, `PetscDeviceContextFork()` 463 @*/ 464 PetscErrorCode PetscDeviceContextQueryIdle(PetscDeviceContext dctx, PetscBool *idle) 465 { 466 PetscFunctionBegin; 467 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx)); 468 PetscValidBoolPointer(idle, 2); 469 PetscCall(PetscLogEventBegin(DCONTEXT_QueryIdle, dctx, nullptr, nullptr, nullptr)); 470 PetscUseTypeMethod(dctx, query, idle); 471 PetscCall(PetscLogEventEnd(DCONTEXT_QueryIdle, dctx, nullptr, nullptr, nullptr)); 472 PetscCall(PetscInfo(dctx, "PetscDeviceContext ('%s', id %" PetscInt64_FMT ") %s idle\n", PetscObjectCast(dctx)->name ? PetscObjectCast(dctx)->name : "unnamed", PetscObjectCast(dctx)->id, *idle ? "was" : "was not")); 473 PetscFunctionReturn(0); 474 } 475 476 /*@C 477 PetscDeviceContextWaitForContext - Make one context wait for another context to finish 478 479 Not Collective 480 481 Input Parameters: 482 + dctxa - The `PetscDeviceContext` object that is waiting 483 - dctxb - The `PetscDeviceContext` object that is being waited on 484 485 Notes: 486 Serializes two `PetscDeviceContext`s. Serialization is performed asynchronously; the host 487 does not wait for the serialization to actually occur. 488 489 This routine uses only the state of `dctxb` at the moment this routine was called, so any 490 future work queued will not affect `dctxa`. It is safe to pass the same context to both 491 arguments (in which case this routine does nothing). 492 493 DAG representation: 494 .vb 495 time -> 496 497 -> dctxa ---/- |= CALL =| - dctxa -> 498 / 499 -> dctxb -/------------------------> 500 .ve 501 502 Level: beginner 503 504 .N ASYNC_API 505 506 .seealso: `PetscDeviceContextCreate()`, `PetscDeviceContextQueryIdle()`, `PetscDeviceContextJoin()` 507 @*/ 508 PetscErrorCode PetscDeviceContextWaitForContext(PetscDeviceContext dctxa, PetscDeviceContext dctxb) 509 { 510 PetscObject aobj; 511 512 PetscFunctionBegin; 513 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctxa)); 514 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctxb)); 515 PetscCheckCompatibleDeviceContexts(dctxa, 1, dctxb, 2); 516 if (dctxa == dctxb) PetscFunctionReturn(0); 517 aobj = PetscObjectCast(dctxa); 518 PetscCall(PetscLogEventBegin(DCONTEXT_WaitForCtx, dctxa, dctxb, nullptr, nullptr)); 519 PetscUseTypeMethod(dctxa, waitforcontext, dctxb); 520 PetscCallCXX(CxxDataCast(dctxa)->upstream[dctxb] = CxxData::parent_type(dctxb)); 521 PetscCall(PetscLogEventEnd(DCONTEXT_WaitForCtx, dctxa, dctxb, nullptr, nullptr)); 522 PetscCall(PetscInfo(dctxa, "dctx %" PetscInt64_FMT " waiting on dctx %" PetscInt64_FMT "\n", aobj->id, PetscObjectCast(dctxb)->id)); 523 PetscCall(PetscObjectStateIncrease(aobj)); 524 PetscFunctionReturn(0); 525 } 526 527 /*@C 528 PetscDeviceContextForkWithStreamType - Create a set of dependent child contexts from a parent 529 context with a prescribed `PetscStreamType` 530 531 Not Collective, Asynchronous 532 533 Input Parameters: 534 + dctx - The parent `PetscDeviceContext` 535 . stype - The prescribed `PetscStreamType` 536 - n - The number of children to create 537 538 Output Parameter: 539 . dsub - The created child context(s) 540 541 Notes: 542 This routine creates `n` edges of a DAG from a source node which are causally dependent on the 543 source node. This causal dependency is established as-if by calling 544 `PetscDeviceContextWaitForContext()` on every child. 545 546 `dsub` is allocated by this routine and has its lifetime bounded by `dctx`. That is, `dctx` 547 expects to free `dsub` (via `PetscDeviceContextJoin()`) before it itself is destroyed. 548 549 This routine only accounts for work queued on `dctx` up until calling this routine, any 550 subsequent work enqueued on `dctx` has no effect on `dsub`. 551 552 The `PetscStreamType` of `dctx` does not have to equal `stype`. In fact, it is often the case 553 that they are different. This is useful in cases where a routine can locally exploit stream 554 parallelism without needing to worry about what stream type the incoming `PetscDeviceContext` 555 carries. 556 557 DAG representation: 558 .vb 559 time -> 560 561 -> dctx - |= CALL =| -\----> dctx ------> 562 \---> dsub[0] ---> 563 \--> ... -------> 564 \-> dsub[n-1] -> 565 .ve 566 567 Level: intermediate 568 569 .N ASYNC_API 570 571 .seealso: `PetscDeviceContextJoin()`, `PetscDeviceContextSynchronize()`, 572 `PetscDeviceContextQueryIdle()`, `PetscDeviceContextWaitForContext()` 573 @*/ 574 PetscErrorCode PetscDeviceContextForkWithStreamType(PetscDeviceContext dctx, PetscStreamType stype, PetscInt n, PetscDeviceContext **dsub) 575 { 576 // debugging only 577 std::string idList; 578 auto ninput = n; 579 580 PetscFunctionBegin; 581 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx)); 582 PetscAssert(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of contexts requested %" PetscInt_FMT " < 0", n); 583 PetscValidPointer(dsub, 4); 584 *dsub = nullptr; 585 /* reserve 4 chars per id, 2 for number and 2 for ', ' separator */ 586 if (PetscDefined(USE_DEBUG_AND_INFO)) PetscCallCXX(idList.reserve(4 * n)); 587 PetscCall(PetscLogEventBegin(DCONTEXT_Fork, dctx, nullptr, nullptr, nullptr)); 588 /* update child totals */ 589 dctx->numChildren += n; 590 /* now to find out if we have room */ 591 if (dctx->numChildren > dctx->maxNumChildren) { 592 const auto numChildren = dctx->numChildren; 593 auto &maxNumChildren = dctx->maxNumChildren; 594 auto numAllocated = numChildren; 595 596 /* no room, either from having too many kids or not having any */ 597 if (auto &childIDs = dctx->childIDs) { 598 // the difference is backwards because we have not updated maxNumChildren yet 599 numAllocated -= maxNumChildren; 600 /* have existing children, must reallocate them */ 601 PetscCall(PetscRealloc(numChildren * sizeof(*childIDs), &childIDs)); 602 /* clear the extra memory since realloc doesn't do it for us */ 603 PetscCall(PetscArrayzero(std::next(childIDs, maxNumChildren), numAllocated)); 604 } else { 605 /* have no children */ 606 PetscCall(PetscCalloc1(numChildren, &childIDs)); 607 } 608 /* update total number of children */ 609 maxNumChildren = numChildren; 610 } 611 PetscCall(PetscMalloc1(n, dsub)); 612 for (PetscInt i = 0; ninput && (i < dctx->numChildren); ++i) { 613 auto &childID = dctx->childIDs[i]; 614 /* empty child slot */ 615 if (!childID) { 616 auto &childctx = (*dsub)[i]; 617 618 /* create the child context in the image of its parent */ 619 PetscCall(PetscDeviceContextDuplicate_Private(dctx, stype, &childctx)); 620 PetscCall(PetscDeviceContextWaitForContext(childctx, dctx)); 621 /* register the child with its parent */ 622 PetscCall(PetscObjectGetId(PetscObjectCast(childctx), &childID)); 623 if (PetscDefined(USE_DEBUG_AND_INFO)) { 624 PetscCallCXX(idList += std::to_string(childID)); 625 if (ninput != 1) PetscCallCXX(idList += ", "); 626 } 627 --ninput; 628 } 629 } 630 PetscCall(PetscLogEventEnd(DCONTEXT_Fork, dctx, nullptr, nullptr, nullptr)); 631 PetscCall(PetscDebugInfo(dctx, "Forked %" PetscInt_FMT " children from parent %" PetscInt64_FMT " with IDs: %s\n", n, PetscObjectCast(dctx)->id, idList.c_str())); 632 PetscFunctionReturn(0); 633 } 634 635 /*@C 636 PetscDeviceContextFork - Create a set of dependent child contexts from a parent context 637 638 Not Collective, Asynchronous 639 640 Input Parameters: 641 + dctx - The parent `PetscDeviceContext` 642 - n - The number of children to create 643 644 Output Parameter: 645 . dsub - The created child context(s) 646 647 Notes: 648 Behaves identically to `PetscDeviceContextForkWithStreamType()` except that the prescribed 649 `PetscStreamType` is taken from `dctx`. In effect this routine is shorthand for\: 650 651 .vb 652 PetscStreamType stype; 653 654 PetscDeviceContextGetStreamType(dctx, &stype); 655 PetscDeviceContextForkWithStreamType(dctx, stype, ...); 656 .ve 657 658 Level: beginner 659 660 .N ASYNC_API 661 662 .seealso: `PetscDeviceContextForkWithStreamType()`, `PetscDeviceContextJoin()`, 663 `PetscDeviceContextSynchronize()`, `PetscDeviceContextQueryIdle()` 664 @*/ 665 PetscErrorCode PetscDeviceContextFork(PetscDeviceContext dctx, PetscInt n, PetscDeviceContext **dsub) 666 { 667 auto stype = PETSC_STREAM_DEFAULT_BLOCKING; 668 669 PetscFunctionBegin; 670 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx)); 671 PetscCall(PetscDeviceContextGetStreamType(dctx, &stype)); 672 PetscCall(PetscDeviceContextForkWithStreamType(dctx, stype, n, dsub)); 673 PetscFunctionReturn(0); 674 } 675 676 /*@C 677 PetscDeviceContextJoin - Converge a set of child contexts 678 679 Not Collective, Asynchronous 680 681 Input Parameters: 682 + dctx - A `PetscDeviceContext` to converge on 683 . n - The number of sub contexts to converge 684 . joinMode - The type of join to perform 685 - dsub - The sub contexts to converge 686 687 Notes: 688 If `PetscDeviceContextFork()` creates `n` edges from a source node which all depend on the source 689 node, then this routine is the exact mirror. That is, it creates a node (represented in `dctx`) 690 which recieves `n` edges (and optionally destroys them) which is dependent on the completion 691 of all incoming edges. 692 693 If `joinMode` is `PETSC_DEVICE_CONTEXT_JOIN_DESTROY`. All contexts in `dsub` will be 694 destroyed by this routine. Thus all sub contexts must have been created with the `dctx` 695 passed to this routine. 696 697 If `joinMode` is `PETSC_DEVICE_CONTEXT_JOIN_SYNC`. All sub contexts will additionally wait on 698 `dctx` after converging. This has the effect of "synchronizing" the outgoing edges. Note the 699 sync suffix does NOT refer to the host, i.e. this routine does NOT call 700 `PetscDeviceSynchronize()`. 701 702 If `joinMode` is `PETSC_DEVICE_CONTEXT_JOIN_NO_SYNC`. `dctx` waits for all sub contexts but 703 the sub contexts do not wait for one another or `dctx` afterwards. 704 705 DAG representations: 706 If `joinMode` is `PETSC_DEVICE_CONTEXT_JOIN_DESTROY` 707 .vb 708 time -> 709 710 -> dctx ---------/- |= CALL =| - dctx -> 711 -> dsub[0] -----/ 712 -> ... -------/ 713 -> dsub[n-1] -/ 714 .ve 715 If `joinMode` is `PETSC_DEVICE_CONTEXT_JOIN_SYNC` 716 .vb 717 time -> 718 719 -> dctx ---------/- |= CALL =| -\----> dctx ------> 720 -> dsub[0] -----/ \---> dsub[0] ---> 721 -> ... -------/ \--> ... -------> 722 -> dsub[n-1] -/ \-> dsub[n-1] -> 723 .ve 724 If `joinMode` is `PETSC_DEVICE_CONTEXT_JOIN_NO_SYNC` 725 .vb 726 time -> 727 728 -> dctx ----------/- |= CALL =| - dctx -> 729 -> dsub[0] ------/-----------------------> 730 -> ... --------/------------------------> 731 -> dsub[n-1] --/-------------------------> 732 .ve 733 734 Level: beginner 735 736 .N ASYNC_API 737 738 .seealso: `PetscDeviceContextFork()`, `PetscDeviceContextForkWithStreamType()`, 739 `PetscDeviceContextSynchronize()`, `PetscDeviceContextJoinMode` 740 @*/ 741 PetscErrorCode PetscDeviceContextJoin(PetscDeviceContext dctx, PetscInt n, PetscDeviceContextJoinMode joinMode, PetscDeviceContext **dsub) 742 { 743 // debugging only 744 std::string idList; 745 746 PetscFunctionBegin; 747 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx)); 748 /* validity of dctx is checked in the wait-for loop */ 749 PetscValidPointer(dsub, 4); 750 PetscAssert(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of contexts merged %" PetscInt_FMT " < 0", n); 751 /* reserve 4 chars per id, 2 for number and 2 for ', ' separator */ 752 if (PetscDefined(USE_DEBUG_AND_INFO)) PetscCallCXX(idList.reserve(4 * n)); 753 /* first dctx waits on all the incoming edges */ 754 PetscCall(PetscLogEventBegin(DCONTEXT_Join, dctx, nullptr, nullptr, nullptr)); 755 for (PetscInt i = 0; i < n; ++i) { 756 PetscCheckCompatibleDeviceContexts(dctx, 1, (*dsub)[i], 4); 757 PetscCall(PetscDeviceContextWaitForContext(dctx, (*dsub)[i])); 758 if (PetscDefined(USE_DEBUG_AND_INFO)) { 759 PetscCallCXX(idList += std::to_string(PetscObjectCast((*dsub)[i])->id)); 760 if (i + 1 < n) PetscCallCXX(idList += ", "); 761 } 762 } 763 764 /* now we handle the aftermath */ 765 switch (joinMode) { 766 case PETSC_DEVICE_CONTEXT_JOIN_DESTROY: { 767 const auto children = dctx->childIDs; 768 const auto maxchild = dctx->maxNumChildren; 769 auto &nchild = dctx->numChildren; 770 PetscInt j = 0; 771 772 PetscCheck(n <= nchild, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Trying to destroy %" PetscInt_FMT " children of a parent context that only has %" PetscInt_FMT " children, likely trying to restore to wrong parent", n, nchild); 773 /* update child count while it's still fresh in memory */ 774 nchild -= n; 775 for (PetscInt i = 0; i < maxchild; ++i) { 776 if (children[i] && (children[i] == PetscObjectCast((*dsub)[j])->id)) { 777 /* child is one of ours, can destroy it */ 778 PetscCall(PetscDeviceContextDestroy((*dsub) + j)); 779 /* reset the child slot */ 780 children[i] = 0; 781 if (++j == n) break; 782 } 783 } 784 /* gone through the loop but did not find every child */ 785 PetscCheck(j == n, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "%" PetscInt_FMT " contexts still remain after destroy, this may be because you are trying to restore to the wrong parent context, or the device contexts are not in the same order as they were checked out out in", n - j); 786 PetscCall(PetscFree(*dsub)); 787 } break; 788 case PETSC_DEVICE_CONTEXT_JOIN_SYNC: 789 for (PetscInt i = 0; i < n; ++i) PetscCall(PetscDeviceContextWaitForContext((*dsub)[i], dctx)); 790 case PETSC_DEVICE_CONTEXT_JOIN_NO_SYNC: 791 break; 792 default: 793 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown PetscDeviceContextJoinMode given"); 794 } 795 PetscCall(PetscLogEventEnd(DCONTEXT_Join, dctx, nullptr, nullptr, nullptr)); 796 797 PetscCall(PetscDebugInfo(dctx, "Joined %" PetscInt_FMT " ctxs to ctx %" PetscInt64_FMT ", mode %s with IDs: %s\n", n, PetscObjectCast(dctx)->id, PetscDeviceContextJoinModes[joinMode], idList.c_str())); 798 PetscFunctionReturn(0); 799 } 800 801 /*@C 802 PetscDeviceContextSynchronize - Block the host until all work queued on a 803 `PetscDeviceContext` has finished 804 805 Not Collective 806 807 Input Parameters: 808 . dctx - The `PetscDeviceContext` to synchronize 809 810 Notes: 811 The host will not return from this routine until `dctx` is idle. Any and all memory 812 operations queued on or otherwise associated with (either explicitly or implicitly via 813 dependencies) are guaranteed to have finished and be globally visible on return. 814 815 In effect, this routine serves as memory and execution barrier. 816 817 DAG representation: 818 .vb 819 time -> 820 821 -> dctx - |= CALL =| - dctx -> 822 .ve 823 824 Level: beginner 825 826 .seealso: `PetscDeviceContextFork()`, `PetscDeviceContextJoin()`, `PetscDeviceContextQueryIdle()` 827 @*/ 828 PetscErrorCode PetscDeviceContextSynchronize(PetscDeviceContext dctx) 829 { 830 PetscFunctionBegin; 831 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx)); 832 PetscCall(PetscLogEventBegin(DCONTEXT_Sync, dctx, nullptr, nullptr, nullptr)); 833 /* if it isn't setup there is nothing to sync on */ 834 if (dctx->setup) { 835 PetscCall((*dctx->ops->synchronize)(dctx)); 836 PetscCall(PetscDeviceContextSyncClearMap_Internal(dctx)); 837 } 838 PetscCall(PetscLogEventEnd(DCONTEXT_Sync, dctx, nullptr, nullptr, nullptr)); 839 PetscFunctionReturn(0); 840 } 841 842 /* every device type has a vector of null PetscDeviceContexts -- one for each device */ 843 static auto nullContexts = std::array<std::vector<PetscDeviceContext>, PETSC_DEVICE_MAX>{}; 844 static auto nullContextsFinalizer = false; 845 846 static PetscErrorCode PetscDeviceContextGetNullContextForDevice_Private(PetscBool user_set_device, PetscDevice device, PetscDeviceContext *dctx) 847 { 848 PetscInt devid; 849 PetscDeviceType dtype; 850 851 PetscFunctionBegin; 852 PetscValidDevice(device, 2); 853 PetscValidPointer(dctx, 3); 854 if (PetscUnlikely(!nullContextsFinalizer)) { 855 const auto finalizer = [] { 856 PetscFunctionBegin; 857 for (auto &&dvec : nullContexts) { 858 for (auto &&dctx : dvec) PetscCall(PetscDeviceContextDestroy(&dctx)); 859 PetscCallCXX(dvec.clear()); 860 } 861 nullContextsFinalizer = false; 862 PetscFunctionReturn(0); 863 }; 864 865 nullContextsFinalizer = true; 866 PetscCall(PetscRegisterFinalize(std::move(finalizer))); 867 } 868 PetscCall(PetscDeviceGetDeviceId(device, &devid)); 869 PetscCall(PetscDeviceGetType(device, &dtype)); 870 { 871 auto &ctxlist = nullContexts[dtype]; 872 873 PetscCheck(devid >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Device ID (%" PetscInt_FMT ") must be positive", devid); 874 // need to resize the container if not big enough because incrementing the iterator in 875 // std::next() (if we haven't initialized that ctx yet) may cause it to fall outside the 876 // current size of the container. 877 if (static_cast<std::size_t>(devid) >= ctxlist.size()) PetscCallCXX(ctxlist.resize(devid + 1)); 878 if (PetscUnlikely(!ctxlist[devid])) { 879 // we have not seen this device before 880 PetscCall(PetscDeviceContextCreate(dctx)); 881 PetscCall(PetscInfo(*dctx, "Initializing null PetscDeviceContext (of type %s) for device %" PetscInt_FMT "\n", PetscDeviceTypes[dtype], devid)); 882 { 883 const auto pobj = PetscObjectCast(*dctx); 884 const auto name = "null context " + std::to_string(devid); 885 const auto prefix = "null_context_" + std::to_string(devid) + '_'; 886 887 PetscCall(PetscObjectSetName(pobj, name.c_str())); 888 PetscCall(PetscObjectSetOptionsPrefix(pobj, prefix.c_str())); 889 } 890 PetscCall(PetscDeviceContextSetStreamType(*dctx, PETSC_STREAM_GLOBAL_BLOCKING)); 891 PetscCall(PetscDeviceContextSetDevice_Private(*dctx, device, user_set_device)); 892 PetscCall(PetscDeviceContextSetUp(*dctx)); 893 // would use ctxlist.cbegin() but GCC 4.8 can't handle const iterator insert! 894 PetscCallCXX(ctxlist.insert(std::next(ctxlist.begin(), devid), *dctx)); 895 } else *dctx = ctxlist[devid]; 896 } 897 PetscFunctionReturn(0); 898 } 899 900 /* 901 Gets the "NULL" context for the current PetscDeviceType and PetscDevice. NULL contexts are 902 guaranteed to always be globally blocking. 903 */ 904 PetscErrorCode PetscDeviceContextGetNullContext_Internal(PetscDeviceContext *dctx) 905 { 906 PetscDeviceContext gctx; 907 PetscDevice gdev = nullptr; 908 909 PetscFunctionBegin; 910 PetscValidPointer(dctx, 1); 911 PetscCall(PetscDeviceContextGetCurrentContext(&gctx)); 912 PetscCall(PetscDeviceContextGetDevice(gctx, &gdev)); 913 PetscCall(PetscDeviceContextGetNullContextForDevice_Private(gctx->usersetdevice, gdev, dctx)); 914 PetscFunctionReturn(0); 915 } 916 917 /*@C 918 PetscDeviceContextSetFromOptions - Configure a `PetscDeviceContext` from the options database 919 920 Collective on `comm` or `dctx` 921 922 Input Parameters: 923 + comm - MPI communicator on which to query the options database (optional) 924 - dctx - The `PetscDeviceContext` to configure 925 926 Output Parameter: 927 . dctx - The `PetscDeviceContext` 928 929 Options Database: 930 + -device_context_stream_type - type of stream to create inside the `PetscDeviceContext` - 931 `PetscDeviceContextSetStreamType()` 932 - -device_context_device_type - the type of `PetscDevice` to attach by default - `PetscDeviceType` 933 934 Notes: 935 The user may pass `MPI_COMM_NULL` for `comm` in which case the communicator of `dctx` is 936 used (which is always `PETSC_COMM_SELF`). 937 938 Level: beginner 939 940 .seealso: `PetscDeviceContextSetStreamType()`, `PetscDeviceContextSetDevice()`, 941 `PetscDeviceContextView()` 942 @*/ 943 PetscErrorCode PetscDeviceContextSetFromOptions(MPI_Comm comm, PetscDeviceContext dctx) 944 { 945 const auto pobj = PetscObjectCast(dctx); 946 auto dtype = std::make_pair(PETSC_DEVICE_DEFAULT(), PETSC_FALSE); 947 auto stype = std::make_pair(PETSC_DEVICE_CONTEXT_DEFAULT_STREAM_TYPE, PETSC_FALSE); 948 auto old_comm = PETSC_COMM_SELF; 949 950 PetscFunctionBegin; 951 // do not user getoptionalnullcontext here, the user is not allowed to set it from options! 952 PetscValidDeviceContext(dctx, 2); 953 /* set the device type first */ 954 if (const auto device = dctx->device) PetscCall(PetscDeviceGetType(device, &dtype.first)); 955 PetscCall(PetscDeviceContextGetStreamType(dctx, &stype.first)); 956 957 if (comm == MPI_COMM_NULL) { 958 PetscCall(PetscObjectGetComm(pobj, &comm)); 959 } else { 960 // briefly set the communicator for dctx (it is always PETSC_COMM_SELF) so 961 // PetscObjectOptionsBegin() behaves as if dctx had comm 962 old_comm = Petsc::util::exchange(pobj->comm, comm); 963 } 964 965 PetscObjectOptionsBegin(pobj); 966 PetscCall(PetscDeviceContextQueryOptions_Internal(PetscOptionsObject, dtype, stype)); 967 PetscOptionsEnd(); 968 // reset the comm (should be PETSC_COMM_SELF) 969 if (comm != MPI_COMM_NULL) pobj->comm = old_comm; 970 if (dtype.second) PetscCall(PetscDeviceContextSetDefaultDeviceForType_Internal(dctx, dtype.first)); 971 if (stype.second) PetscCall(PetscDeviceContextSetStreamType(dctx, stype.first)); 972 PetscCall(PetscDeviceContextSetUp(dctx)); 973 PetscFunctionReturn(0); 974 } 975 976 /*@C 977 PetscDeviceContextView - View a `PetscDeviceContext` 978 979 Collective on `viewer` 980 981 Input Parameters: 982 + dctx - The `PetscDeviceContext` 983 - viewer - The `PetscViewer` to view `dctx` with (may be `NULL`) 984 985 Notes: 986 If `viewer` is `NULL`, `PETSC_VIEWER_STDOUT_WORLD` is used instead, in which case this 987 routine is collective on `PETSC_COMM_WORLD`. 988 989 Level: beginner 990 991 .seealso: `PetscDeviceContextViewFromOptions()`, `PetscDeviceView()`, `PETSC_VIEWER_STDOUT_WORLD`, `PetscDeviceContextCreate()` 992 @*/ 993 PetscErrorCode PetscDeviceContextView(PetscDeviceContext dctx, PetscViewer viewer) 994 { 995 PetscBool iascii; 996 997 PetscFunctionBegin; 998 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx)); 999 if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_WORLD, &viewer)); 1000 PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1001 PetscCall(PetscObjectTypeCompare(PetscObjectCast(viewer), PETSCVIEWERASCII, &iascii)); 1002 if (iascii) { 1003 auto stype = PETSC_STREAM_DEFAULT_BLOCKING; 1004 PetscViewer sub; 1005 1006 PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sub)); 1007 PetscCall(PetscObjectPrintClassNamePrefixType(PetscObjectCast(dctx), sub)); 1008 PetscCall(PetscViewerASCIIPushTab(sub)); 1009 PetscCall(PetscDeviceContextGetStreamType(dctx, &stype)); 1010 PetscCall(PetscViewerASCIIPrintf(sub, "stream type: %s\n", PetscStreamTypes[stype])); 1011 PetscCall(PetscViewerASCIIPrintf(sub, "children: %" PetscInt_FMT "\n", dctx->numChildren)); 1012 if (const auto nchild = dctx->numChildren) { 1013 PetscCall(PetscViewerASCIIPushTab(sub)); 1014 for (PetscInt i = 0; i < nchild; ++i) { 1015 if (i == nchild - 1) { 1016 PetscCall(PetscViewerASCIIPrintf(sub, "%" PetscInt64_FMT, dctx->childIDs[i])); 1017 } else { 1018 PetscCall(PetscViewerASCIIPrintf(sub, "%" PetscInt64_FMT ", ", dctx->childIDs[i])); 1019 } 1020 } 1021 } 1022 PetscCall(PetscViewerASCIIPopTab(sub)); 1023 PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sub)); 1024 PetscCall(PetscViewerFlush(viewer)); 1025 PetscCall(PetscViewerASCIIPushTab(viewer)); 1026 } 1027 if (const auto device = dctx->device) PetscCall(PetscDeviceView(device, viewer)); 1028 if (iascii) PetscCall(PetscViewerASCIIPopTab(viewer)); 1029 PetscFunctionReturn(0); 1030 } 1031 1032 /*@C 1033 PetscDeviceContextViewFromOptions - View a `PetscDeviceContext` from options 1034 1035 Input Parameters: 1036 + dctx - The `PetscDeviceContext` to view 1037 . obj - Optional `PetscObject` to associate (may be `NULL`) 1038 - name - The command line option 1039 1040 Level: beginner 1041 1042 .seealso: `PetscDeviceContextView()`, `PetscObjectViewFromOptions()`, `PetscDeviceContextCreate()` 1043 @*/ 1044 PetscErrorCode PetscDeviceContextViewFromOptions(PetscDeviceContext dctx, PetscObject obj, const char name[]) 1045 { 1046 PetscFunctionBegin; 1047 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx)); 1048 if (obj) PetscValidHeader(obj, 2); 1049 PetscValidCharPointer(name, 3); 1050 PetscCall(PetscObjectViewFromOptions(PetscObjectCast(dctx), obj, name)); 1051 PetscFunctionReturn(0); 1052 } 1053