dcontext.cxx (fb664f9e710748ae2f81f388b5f0c9179833c49b) dcontext.cxx (6a4a1270853b5b1995c94de98f44d28bec57470a)
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>

--- 74 unchanged lines hidden (view full) ---

83.seealso: `PetscDeviceContextDuplicate()`, `PetscDeviceContextSetDevice()`,
84`PetscDeviceContextSetStreamType()`, `PetscDeviceContextSetUp()`,
85`PetscDeviceContextSetFromOptions()`, `PetscDeviceContextView()`, `PetscDeviceContextDestroy()`
86@*/
87PetscErrorCode PetscDeviceContextCreate(PetscDeviceContext *dctx) {
88 PetscFunctionBegin;
89 PetscValidPointer(dctx, 1);
90 PetscCall(PetscDeviceInitializePackage());
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>

--- 74 unchanged lines hidden (view full) ---

83.seealso: `PetscDeviceContextDuplicate()`, `PetscDeviceContextSetDevice()`,
84`PetscDeviceContextSetStreamType()`, `PetscDeviceContextSetUp()`,
85`PetscDeviceContextSetFromOptions()`, `PetscDeviceContextView()`, `PetscDeviceContextDestroy()`
86@*/
87PetscErrorCode PetscDeviceContextCreate(PetscDeviceContext *dctx) {
88 PetscFunctionBegin;
89 PetscValidPointer(dctx, 1);
90 PetscCall(PetscDeviceInitializePackage());
91 PetscCall(PetscLogEventBegin(DCONTEXT_Create, 0, 0, 0, 0));
91 PetscCall(PetscLogEventBegin(DCONTEXT_Create, nullptr, nullptr, nullptr, nullptr));
92 PetscCall(contextPool.allocate(dctx));
92 PetscCall(contextPool.allocate(dctx));
93 PetscCall(PetscLogEventEnd(DCONTEXT_Create, 0, 0, 0, 0));
93 PetscCall(PetscLogEventEnd(DCONTEXT_Create, nullptr, nullptr, nullptr, nullptr));
94 PetscFunctionReturn(0);
95}
96
97/*@C
98 PetscDeviceContextDestroy - Frees a `PetscDeviceContext`
99
100 Not Collective
101

--- 24 unchanged lines hidden (view full) ---

126
127.seealso: `PetscDeviceContextCreate()`, `PetscDeviceContextSetDevice()`,
128`PetscDeviceContextSetUp()`, `PetscDeviceContextSynchronize()`
129@*/
130PetscErrorCode PetscDeviceContextDestroy(PetscDeviceContext *dctx) {
131 PetscFunctionBegin;
132 PetscValidPointer(dctx, 1);
133 if (!*dctx) PetscFunctionReturn(0);
94 PetscFunctionReturn(0);
95}
96
97/*@C
98 PetscDeviceContextDestroy - Frees a `PetscDeviceContext`
99
100 Not Collective
101

--- 24 unchanged lines hidden (view full) ---

126
127.seealso: `PetscDeviceContextCreate()`, `PetscDeviceContextSetDevice()`,
128`PetscDeviceContextSetUp()`, `PetscDeviceContextSynchronize()`
129@*/
130PetscErrorCode PetscDeviceContextDestroy(PetscDeviceContext *dctx) {
131 PetscFunctionBegin;
132 PetscValidPointer(dctx, 1);
133 if (!*dctx) PetscFunctionReturn(0);
134 PetscCall(PetscLogEventBegin(DCONTEXT_Destroy, 0, 0, 0, 0));
134 PetscCall(PetscLogEventBegin(DCONTEXT_Destroy, nullptr, nullptr, nullptr, nullptr));
135 if (--(PetscObjectCast(*dctx)->refct) <= 0) {
136 PetscCall(PetscDeviceContextCheckNotOrphaned_Internal(*dctx));
137 // std::move of the expression of the trivially-copyable type 'PetscDeviceContext' (aka
138 // '_n_PetscDeviceContext *') has no effect; remove std::move() [performance-move-const-arg]
139 // can't remove std::move, since reclaim only takes r-value reference
140 PetscCall(contextPool.deallocate(std::move(*dctx))); // NOLINT (performance-move-const-arg)
141 }
135 if (--(PetscObjectCast(*dctx)->refct) <= 0) {
136 PetscCall(PetscDeviceContextCheckNotOrphaned_Internal(*dctx));
137 // std::move of the expression of the trivially-copyable type 'PetscDeviceContext' (aka
138 // '_n_PetscDeviceContext *') has no effect; remove std::move() [performance-move-const-arg]
139 // can't remove std::move, since reclaim only takes r-value reference
140 PetscCall(contextPool.deallocate(std::move(*dctx))); // NOLINT (performance-move-const-arg)
141 }
142 PetscCall(PetscLogEventEnd(DCONTEXT_Destroy, 0, 0, 0, 0));
142 PetscCall(PetscLogEventEnd(DCONTEXT_Destroy, nullptr, nullptr, nullptr, nullptr));
143 *dctx = nullptr;
144 PetscFunctionReturn(0);
145}
146
147/*@C
148 PetscDeviceContextSetStreamType - Set the implementation type of the underlying stream for a
149 `PetscDeviceContext`
150

--- 17 unchanged lines hidden (view full) ---

168 PetscFunctionBegin;
169 // do not use getoptionalnullcontext here since we do not want the user to change the stream
170 // type
171 PetscValidDeviceContext(dctx, 1);
172 PetscValidStreamType(type, 2);
173 // only need to do complex swapping if the object has already been setup
174 if (dctx->setup && (dctx->streamType != type)) {
175 dctx->setup = PETSC_FALSE;
143 *dctx = nullptr;
144 PetscFunctionReturn(0);
145}
146
147/*@C
148 PetscDeviceContextSetStreamType - Set the implementation type of the underlying stream for a
149 `PetscDeviceContext`
150

--- 17 unchanged lines hidden (view full) ---

168 PetscFunctionBegin;
169 // do not use getoptionalnullcontext here since we do not want the user to change the stream
170 // type
171 PetscValidDeviceContext(dctx, 1);
172 PetscValidStreamType(type, 2);
173 // only need to do complex swapping if the object has already been setup
174 if (dctx->setup && (dctx->streamType != type)) {
175 dctx->setup = PETSC_FALSE;
176 PetscCall(PetscLogEventBegin(DCONTEXT_ChangeStream, dctx, 0, 0, 0));
176 PetscCall(PetscLogEventBegin(DCONTEXT_ChangeStream, dctx, nullptr, nullptr, nullptr));
177 PetscUseTypeMethod(dctx, changestreamtype, type);
177 PetscUseTypeMethod(dctx, changestreamtype, type);
178 PetscCall(PetscLogEventEnd(DCONTEXT_ChangeStream, dctx, 0, 0, 0));
178 PetscCall(PetscLogEventEnd(DCONTEXT_ChangeStream, dctx, nullptr, nullptr, nullptr));
179 }
180 dctx->streamType = type;
181 PetscFunctionReturn(0);
182}
183
184/*@C
185 PetscDeviceContextGetStreamType - Get the implementation type of the underlying stream for a
186 `PetscDeviceContext`

--- 37 unchanged lines hidden (view full) ---

224 Thus we need to keep track whether the user explicitly sets the device contexts device.
225*/
226static PetscErrorCode PetscDeviceContextSetDevice_Private(PetscDeviceContext dctx, PetscDevice device, PetscBool user_set) {
227 PetscFunctionBegin;
228 // do not use getoptionalnullcontext here since we do not want the user to change its device
229 PetscValidDeviceContext(dctx, 1);
230 PetscValidDevice(device, 2);
231 if (dctx->device && (dctx->device->id == device->id)) PetscFunctionReturn(0);
179 }
180 dctx->streamType = type;
181 PetscFunctionReturn(0);
182}
183
184/*@C
185 PetscDeviceContextGetStreamType - Get the implementation type of the underlying stream for a
186 `PetscDeviceContext`

--- 37 unchanged lines hidden (view full) ---

224 Thus we need to keep track whether the user explicitly sets the device contexts device.
225*/
226static PetscErrorCode PetscDeviceContextSetDevice_Private(PetscDeviceContext dctx, PetscDevice device, PetscBool user_set) {
227 PetscFunctionBegin;
228 // do not use getoptionalnullcontext here since we do not want the user to change its device
229 PetscValidDeviceContext(dctx, 1);
230 PetscValidDevice(device, 2);
231 if (dctx->device && (dctx->device->id == device->id)) PetscFunctionReturn(0);
232 PetscCall(PetscLogEventBegin(DCONTEXT_SetDevice, dctx, 0, 0, 0));
232 PetscCall(PetscLogEventBegin(DCONTEXT_SetDevice, dctx, nullptr, nullptr, nullptr));
233 if (const auto destroy = dctx->ops->destroy) PetscCall((*destroy)(dctx));
234 PetscCall(PetscDeviceDestroy(&dctx->device));
235 PetscCall(PetscMemzero(dctx->ops, sizeof(*dctx->ops)));
236 PetscCall((*device->ops->createcontext)(dctx));
233 if (const auto destroy = dctx->ops->destroy) PetscCall((*destroy)(dctx));
234 PetscCall(PetscDeviceDestroy(&dctx->device));
235 PetscCall(PetscMemzero(dctx->ops, sizeof(*dctx->ops)));
236 PetscCall((*device->ops->createcontext)(dctx));
237 PetscCall(PetscLogEventEnd(DCONTEXT_SetDevice, dctx, 0, 0, 0));
237 PetscCall(PetscLogEventEnd(DCONTEXT_SetDevice, dctx, nullptr, nullptr, nullptr));
238 PetscCall(PetscDeviceReference_Internal(device));
239 dctx->device = device;
240 dctx->setup = PETSC_FALSE;
241 dctx->usersetdevice = user_set;
242 PetscFunctionReturn(0);
243}
244
245PetscErrorCode PetscDeviceContextSetDefaultDeviceForType_Internal(PetscDeviceContext dctx, PetscDeviceType type) {

--- 115 unchanged lines hidden (view full) ---

361 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx));
362 if (dctx->setup) PetscFunctionReturn(0);
363 if (!dctx->device) {
364 const auto default_dtype = PETSC_DEVICE_DEFAULT();
365
366 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]));
367 PetscCall(PetscDeviceContextSetDefaultDeviceForType_Internal(dctx, default_dtype));
368 }
238 PetscCall(PetscDeviceReference_Internal(device));
239 dctx->device = device;
240 dctx->setup = PETSC_FALSE;
241 dctx->usersetdevice = user_set;
242 PetscFunctionReturn(0);
243}
244
245PetscErrorCode PetscDeviceContextSetDefaultDeviceForType_Internal(PetscDeviceContext dctx, PetscDeviceType type) {

--- 115 unchanged lines hidden (view full) ---

361 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx));
362 if (dctx->setup) PetscFunctionReturn(0);
363 if (!dctx->device) {
364 const auto default_dtype = PETSC_DEVICE_DEFAULT();
365
366 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]));
367 PetscCall(PetscDeviceContextSetDefaultDeviceForType_Internal(dctx, default_dtype));
368 }
369 PetscCall(PetscLogEventBegin(DCONTEXT_SetUp, dctx, 0, 0, 0));
369 PetscCall(PetscLogEventBegin(DCONTEXT_SetUp, dctx, nullptr, nullptr, nullptr));
370 PetscUseTypeMethod(dctx, setup);
370 PetscUseTypeMethod(dctx, setup);
371 PetscCall(PetscLogEventEnd(DCONTEXT_SetUp, dctx, 0, 0, 0));
371 PetscCall(PetscLogEventEnd(DCONTEXT_SetUp, dctx, nullptr, nullptr, nullptr));
372 dctx->setup = PETSC_TRUE;
373 PetscFunctionReturn(0);
374}
375
376static PetscErrorCode PetscDeviceContextDuplicate_Private(PetscDeviceContext dctx, PetscStreamType stype, PetscDeviceContext *dctxdup) {
377 PetscFunctionBegin;
372 dctx->setup = PETSC_TRUE;
373 PetscFunctionReturn(0);
374}
375
376static PetscErrorCode PetscDeviceContextDuplicate_Private(PetscDeviceContext dctx, PetscStreamType stype, PetscDeviceContext *dctxdup) {
377 PetscFunctionBegin;
378 PetscCall(PetscLogEventBegin(DCONTEXT_Duplicate, dctx, 0, 0, 0));
378 PetscCall(PetscLogEventBegin(DCONTEXT_Duplicate, dctx, nullptr, nullptr, nullptr));
379 PetscCall(PetscDeviceContextCreate(dctxdup));
380 PetscCall(PetscDeviceContextSetStreamType(*dctxdup, stype));
381 if (const auto device = dctx->device) PetscCall(PetscDeviceContextSetDevice_Private(*dctxdup, device, dctx->usersetdevice));
382 PetscCall(PetscDeviceContextSetUp(*dctxdup));
379 PetscCall(PetscDeviceContextCreate(dctxdup));
380 PetscCall(PetscDeviceContextSetStreamType(*dctxdup, stype));
381 if (const auto device = dctx->device) PetscCall(PetscDeviceContextSetDevice_Private(*dctxdup, device, dctx->usersetdevice));
382 PetscCall(PetscDeviceContextSetUp(*dctxdup));
383 PetscCall(PetscLogEventEnd(DCONTEXT_Duplicate, dctx, 0, 0, 0));
383 PetscCall(PetscLogEventEnd(DCONTEXT_Duplicate, dctx, nullptr, nullptr, nullptr));
384 PetscFunctionReturn(0);
385}
386
387/*@C
388 PetscDeviceContextDuplicate - Duplicates a `PetscDeviceContext` object
389
390 Not Collective
391

--- 53 unchanged lines hidden (view full) ---

445 Level: intermediate
446
447.seealso: `PetscDeviceContextCreate()`, `PetscDeviceContextWaitForContext()`, `PetscDeviceContextFork()`
448@*/
449PetscErrorCode PetscDeviceContextQueryIdle(PetscDeviceContext dctx, PetscBool *idle) {
450 PetscFunctionBegin;
451 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx));
452 PetscValidBoolPointer(idle, 2);
384 PetscFunctionReturn(0);
385}
386
387/*@C
388 PetscDeviceContextDuplicate - Duplicates a `PetscDeviceContext` object
389
390 Not Collective
391

--- 53 unchanged lines hidden (view full) ---

445 Level: intermediate
446
447.seealso: `PetscDeviceContextCreate()`, `PetscDeviceContextWaitForContext()`, `PetscDeviceContextFork()`
448@*/
449PetscErrorCode PetscDeviceContextQueryIdle(PetscDeviceContext dctx, PetscBool *idle) {
450 PetscFunctionBegin;
451 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx));
452 PetscValidBoolPointer(idle, 2);
453 PetscCall(PetscLogEventBegin(DCONTEXT_QueryIdle, dctx, 0, 0, 0));
453 PetscCall(PetscLogEventBegin(DCONTEXT_QueryIdle, dctx, nullptr, nullptr, nullptr));
454 PetscUseTypeMethod(dctx, query, idle);
454 PetscUseTypeMethod(dctx, query, idle);
455 PetscCall(PetscLogEventEnd(DCONTEXT_QueryIdle, dctx, 0, 0, 0));
455 PetscCall(PetscLogEventEnd(DCONTEXT_QueryIdle, dctx, nullptr, nullptr, nullptr));
456 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"));
457 PetscFunctionReturn(0);
458}
459
460/*@C
461 PetscDeviceContextWaitForContext - Make one context wait for another context to finish
462
463 Not Collective

--- 29 unchanged lines hidden (view full) ---

493 PetscObject aobj;
494
495 PetscFunctionBegin;
496 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctxa));
497 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctxb));
498 PetscCheckCompatibleDeviceContexts(dctxa, 1, dctxb, 2);
499 if (dctxa == dctxb) PetscFunctionReturn(0);
500 aobj = PetscObjectCast(dctxa);
456 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"));
457 PetscFunctionReturn(0);
458}
459
460/*@C
461 PetscDeviceContextWaitForContext - Make one context wait for another context to finish
462
463 Not Collective

--- 29 unchanged lines hidden (view full) ---

493 PetscObject aobj;
494
495 PetscFunctionBegin;
496 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctxa));
497 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctxb));
498 PetscCheckCompatibleDeviceContexts(dctxa, 1, dctxb, 2);
499 if (dctxa == dctxb) PetscFunctionReturn(0);
500 aobj = PetscObjectCast(dctxa);
501 PetscCall(PetscLogEventBegin(DCONTEXT_WaitForCtx, dctxa, dctxb, 0, 0));
501 PetscCall(PetscLogEventBegin(DCONTEXT_WaitForCtx, dctxa, dctxb, nullptr, nullptr));
502 PetscUseTypeMethod(dctxa, waitforcontext, dctxb);
503 PetscCallCXX(CxxDataCast(dctxa)->upstream[dctxb] = CxxData::parent_type(dctxb));
502 PetscUseTypeMethod(dctxa, waitforcontext, dctxb);
503 PetscCallCXX(CxxDataCast(dctxa)->upstream[dctxb] = CxxData::parent_type(dctxb));
504 PetscCall(PetscLogEventEnd(DCONTEXT_WaitForCtx, dctxa, dctxb, 0, 0));
504 PetscCall(PetscLogEventEnd(DCONTEXT_WaitForCtx, dctxa, dctxb, nullptr, nullptr));
505 PetscCall(PetscInfo(dctxa, "dctx %" PetscInt64_FMT " waiting on dctx %" PetscInt64_FMT "\n", aobj->id, PetscObjectCast(dctxb)->id));
506 PetscCall(PetscObjectStateIncrease(aobj));
507 PetscFunctionReturn(0);
508}
509
510/*@C
511 PetscDeviceContextForkWithStreamType - Create a set of dependent child contexts from a parent
512 context with a prescribed `PetscStreamType`

--- 48 unchanged lines hidden (view full) ---

561
562 PetscFunctionBegin;
563 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx));
564 PetscAssert(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of contexts requested %" PetscInt_FMT " < 0", n);
565 PetscValidPointer(dsub, 4);
566 *dsub = nullptr;
567 /* reserve 4 chars per id, 2 for number and 2 for ', ' separator */
568 if (PetscDefined(USE_DEBUG_AND_INFO)) PetscCallCXX(idList.reserve(4 * n));
505 PetscCall(PetscInfo(dctxa, "dctx %" PetscInt64_FMT " waiting on dctx %" PetscInt64_FMT "\n", aobj->id, PetscObjectCast(dctxb)->id));
506 PetscCall(PetscObjectStateIncrease(aobj));
507 PetscFunctionReturn(0);
508}
509
510/*@C
511 PetscDeviceContextForkWithStreamType - Create a set of dependent child contexts from a parent
512 context with a prescribed `PetscStreamType`

--- 48 unchanged lines hidden (view full) ---

561
562 PetscFunctionBegin;
563 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx));
564 PetscAssert(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of contexts requested %" PetscInt_FMT " < 0", n);
565 PetscValidPointer(dsub, 4);
566 *dsub = nullptr;
567 /* reserve 4 chars per id, 2 for number and 2 for ', ' separator */
568 if (PetscDefined(USE_DEBUG_AND_INFO)) PetscCallCXX(idList.reserve(4 * n));
569 PetscCall(PetscLogEventBegin(DCONTEXT_Fork, dctx, 0, 0, 0));
569 PetscCall(PetscLogEventBegin(DCONTEXT_Fork, dctx, nullptr, nullptr, nullptr));
570 /* update child totals */
571 dctx->numChildren += n;
572 /* now to find out if we have room */
573 if (dctx->numChildren > dctx->maxNumChildren) {
574 const auto numChildren = dctx->numChildren;
575 auto &maxNumChildren = dctx->maxNumChildren;
576 auto numAllocated = numChildren;
577

--- 28 unchanged lines hidden (view full) ---

606 PetscCall(PetscObjectGetId(PetscObjectCast(childctx), &childID));
607 if (PetscDefined(USE_DEBUG_AND_INFO)) {
608 PetscCallCXX(idList += std::to_string(childID));
609 if (ninput != 1) PetscCallCXX(idList += ", ");
610 }
611 --ninput;
612 }
613 }
570 /* update child totals */
571 dctx->numChildren += n;
572 /* now to find out if we have room */
573 if (dctx->numChildren > dctx->maxNumChildren) {
574 const auto numChildren = dctx->numChildren;
575 auto &maxNumChildren = dctx->maxNumChildren;
576 auto numAllocated = numChildren;
577

--- 28 unchanged lines hidden (view full) ---

606 PetscCall(PetscObjectGetId(PetscObjectCast(childctx), &childID));
607 if (PetscDefined(USE_DEBUG_AND_INFO)) {
608 PetscCallCXX(idList += std::to_string(childID));
609 if (ninput != 1) PetscCallCXX(idList += ", ");
610 }
611 --ninput;
612 }
613 }
614 PetscCall(PetscLogEventEnd(DCONTEXT_Fork, dctx, 0, 0, 0));
614 PetscCall(PetscLogEventEnd(DCONTEXT_Fork, dctx, nullptr, nullptr, nullptr));
615 PetscCall(PetscDebugInfo(dctx, "Forked %" PetscInt_FMT " children from parent %" PetscInt64_FMT " with IDs: %s\n", n, PetscObjectCast(dctx)->id, idList.c_str()));
616 PetscFunctionReturn(0);
617}
618
619/*@C
620 PetscDeviceContextFork - Create a set of dependent child contexts from a parent context
621
622 Not Collective, Asynchronous

--- 105 unchanged lines hidden (view full) ---

728 PetscFunctionBegin;
729 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx));
730 /* validity of dctx is checked in the wait-for loop */
731 PetscValidPointer(dsub, 4);
732 PetscAssert(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of contexts merged %" PetscInt_FMT " < 0", n);
733 /* reserve 4 chars per id, 2 for number and 2 for ', ' separator */
734 if (PetscDefined(USE_DEBUG_AND_INFO)) PetscCallCXX(idList.reserve(4 * n));
735 /* first dctx waits on all the incoming edges */
615 PetscCall(PetscDebugInfo(dctx, "Forked %" PetscInt_FMT " children from parent %" PetscInt64_FMT " with IDs: %s\n", n, PetscObjectCast(dctx)->id, idList.c_str()));
616 PetscFunctionReturn(0);
617}
618
619/*@C
620 PetscDeviceContextFork - Create a set of dependent child contexts from a parent context
621
622 Not Collective, Asynchronous

--- 105 unchanged lines hidden (view full) ---

728 PetscFunctionBegin;
729 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx));
730 /* validity of dctx is checked in the wait-for loop */
731 PetscValidPointer(dsub, 4);
732 PetscAssert(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of contexts merged %" PetscInt_FMT " < 0", n);
733 /* reserve 4 chars per id, 2 for number and 2 for ', ' separator */
734 if (PetscDefined(USE_DEBUG_AND_INFO)) PetscCallCXX(idList.reserve(4 * n));
735 /* first dctx waits on all the incoming edges */
736 PetscCall(PetscLogEventBegin(DCONTEXT_Join, dctx, 0, 0, 0));
736 PetscCall(PetscLogEventBegin(DCONTEXT_Join, dctx, nullptr, nullptr, nullptr));
737 for (PetscInt i = 0; i < n; ++i) {
738 PetscCheckCompatibleDeviceContexts(dctx, 1, (*dsub)[i], 4);
739 PetscCall(PetscDeviceContextWaitForContext(dctx, (*dsub)[i]));
740 if (PetscDefined(USE_DEBUG_AND_INFO)) {
741 PetscCallCXX(idList += std::to_string(PetscObjectCast((*dsub)[i])->id));
742 if (i + 1 < n) PetscCallCXX(idList += ", ");
743 }
744 }

--- 23 unchanged lines hidden (view full) ---

768 PetscCall(PetscFree(*dsub));
769 PetscCall(PetscLogObjectMemory(PetscObjectCast(dctx), -n * sizeof(*dsub)));
770 } break;
771 case PETSC_DEVICE_CONTEXT_JOIN_SYNC:
772 for (PetscInt i = 0; i < n; ++i) PetscCall(PetscDeviceContextWaitForContext((*dsub)[i], dctx));
773 case PETSC_DEVICE_CONTEXT_JOIN_NO_SYNC: break;
774 default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown PetscDeviceContextJoinMode given");
775 }
737 for (PetscInt i = 0; i < n; ++i) {
738 PetscCheckCompatibleDeviceContexts(dctx, 1, (*dsub)[i], 4);
739 PetscCall(PetscDeviceContextWaitForContext(dctx, (*dsub)[i]));
740 if (PetscDefined(USE_DEBUG_AND_INFO)) {
741 PetscCallCXX(idList += std::to_string(PetscObjectCast((*dsub)[i])->id));
742 if (i + 1 < n) PetscCallCXX(idList += ", ");
743 }
744 }

--- 23 unchanged lines hidden (view full) ---

768 PetscCall(PetscFree(*dsub));
769 PetscCall(PetscLogObjectMemory(PetscObjectCast(dctx), -n * sizeof(*dsub)));
770 } break;
771 case PETSC_DEVICE_CONTEXT_JOIN_SYNC:
772 for (PetscInt i = 0; i < n; ++i) PetscCall(PetscDeviceContextWaitForContext((*dsub)[i], dctx));
773 case PETSC_DEVICE_CONTEXT_JOIN_NO_SYNC: break;
774 default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown PetscDeviceContextJoinMode given");
775 }
776 PetscCall(PetscLogEventEnd(DCONTEXT_Join, dctx, 0, 0, 0));
776 PetscCall(PetscLogEventEnd(DCONTEXT_Join, dctx, nullptr, nullptr, nullptr));
777
778 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()));
779 PetscFunctionReturn(0);
780}
781
782/*@C
783 PetscDeviceContextSynchronize - Block the host until all work queued on a
784 `PetscDeviceContext` has finished

--- 19 unchanged lines hidden (view full) ---

804
805 Level: beginner
806
807.seealso: `PetscDeviceContextFork()`, `PetscDeviceContextJoin()`, `PetscDeviceContextQueryIdle()`
808@*/
809PetscErrorCode PetscDeviceContextSynchronize(PetscDeviceContext dctx) {
810 PetscFunctionBegin;
811 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx));
777
778 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()));
779 PetscFunctionReturn(0);
780}
781
782/*@C
783 PetscDeviceContextSynchronize - Block the host until all work queued on a
784 `PetscDeviceContext` has finished

--- 19 unchanged lines hidden (view full) ---

804
805 Level: beginner
806
807.seealso: `PetscDeviceContextFork()`, `PetscDeviceContextJoin()`, `PetscDeviceContextQueryIdle()`
808@*/
809PetscErrorCode PetscDeviceContextSynchronize(PetscDeviceContext dctx) {
810 PetscFunctionBegin;
811 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx));
812 PetscCall(PetscLogEventBegin(DCONTEXT_Sync, dctx, 0, 0, 0));
812 PetscCall(PetscLogEventBegin(DCONTEXT_Sync, dctx, nullptr, nullptr, nullptr));
813 /* if it isn't setup there is nothing to sync on */
814 if (dctx->setup) {
815 PetscCall((*dctx->ops->synchronize)(dctx));
816 PetscCall(PetscDeviceContextSyncClearMap_Internal(dctx));
817 }
813 /* if it isn't setup there is nothing to sync on */
814 if (dctx->setup) {
815 PetscCall((*dctx->ops->synchronize)(dctx));
816 PetscCall(PetscDeviceContextSyncClearMap_Internal(dctx));
817 }
818 PetscCall(PetscLogEventEnd(DCONTEXT_Sync, dctx, 0, 0, 0));
818 PetscCall(PetscLogEventEnd(DCONTEXT_Sync, dctx, nullptr, nullptr, nullptr));
819 PetscFunctionReturn(0);
820}
821
822/* every device type has a vector of null PetscDeviceContexts -- one for each device */
823static auto nullContexts = std::array<std::vector<PetscDeviceContext>, PETSC_DEVICE_MAX>{};
824static auto nullContextsFinalizer = false;
825
826static PetscErrorCode PetscDeviceContextGetNullContextForDevice_Private(PetscBool user_set_device, PetscDevice device, PetscDeviceContext *dctx) {

--- 201 unchanged lines hidden ---
819 PetscFunctionReturn(0);
820}
821
822/* every device type has a vector of null PetscDeviceContexts -- one for each device */
823static auto nullContexts = std::array<std::vector<PetscDeviceContext>, PETSC_DEVICE_MAX>{};
824static auto nullContextsFinalizer = false;
825
826static PetscErrorCode PetscDeviceContextGetNullContextForDevice_Private(PetscBool user_set_device, PetscDevice device, PetscDeviceContext *dctx) {

--- 201 unchanged lines hidden ---