Skip to content

Commit 6788713

Browse files
[SYCL][CUDA] Fix active context when creating base event (#1447)
Due to the top of the context stack being recovered before creating the base event, this event may be created using the wrong context. Signed-off-by: Steffen Larsen <[email protected]>
1 parent 20e8cde commit 6788713

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

sycl/plugins/cuda/pi_cuda.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,8 @@ pi_result cuda_piContextCreate(const pi_context_properties *properties,
14131413

14141414
std::unique_ptr<_pi_context> piContextPtr{nullptr};
14151415
try {
1416+
CUcontext current = nullptr;
1417+
14161418
if (property_cuda_primary) {
14171419
// Use the CUDA primary context and assume that we want to use it
14181420
// immediately as we want to forge context switches.
@@ -1424,23 +1426,26 @@ pi_result cuda_piContextCreate(const pi_context_properties *properties,
14241426
errcode_ret = PI_CHECK_ERROR(cuCtxPushCurrent(Ctxt));
14251427
} else {
14261428
// Create a scoped context.
1427-
CUcontext newContext, current;
1429+
CUcontext newContext;
14281430
PI_CHECK_ERROR(cuCtxGetCurrent(&current));
14291431
errcode_ret = PI_CHECK_ERROR(
14301432
cuCtxCreate(&newContext, CU_CTX_MAP_HOST, devices[0]->get()));
14311433
piContextPtr = std::unique_ptr<_pi_context>(new _pi_context{
14321434
_pi_context::kind::user_defined, newContext, *devices});
1433-
// For scoped contexts keep the last active CUDA one on top of the stack
1434-
// as `cuCtxCreate` replaces it implicitly otherwise.
1435-
if (current != nullptr) {
1436-
PI_CHECK_ERROR(cuCtxSetCurrent(current));
1437-
}
14381435
}
14391436

14401437
// Use default stream to record base event counter
14411438
PI_CHECK_ERROR(cuEventCreate(&piContextPtr->evBase_, CU_EVENT_DEFAULT));
14421439
PI_CHECK_ERROR(cuEventRecord(piContextPtr->evBase_, 0));
14431440

1441+
// For non-primary scoped contexts keep the last active on top of the stack
1442+
// as `cuCtxCreate` replaces it implicitly otherwise.
1443+
// Primary contexts are kept on top of the stack, so the previous context
1444+
// is not queried and therefore not recovered.
1445+
if (current != nullptr) {
1446+
PI_CHECK_ERROR(cuCtxSetCurrent(current));
1447+
}
1448+
14441449
*retcontext = piContextPtr.release();
14451450
} catch (pi_result err) {
14461451
errcode_ret = err;

0 commit comments

Comments
 (0)