Skip to content

[SYCL][CUDA] Fixes active context when creating base event #1447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions sycl/plugins/cuda/pi_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,8 @@ pi_result cuda_piContextCreate(const pi_context_properties *properties,

std::unique_ptr<_pi_context> piContextPtr{nullptr};
try {
CUcontext current = nullptr;

if (property_cuda_primary) {
// Use the CUDA primary context and assume that we want to use it
// immediately as we want to forge context switches.
Expand All @@ -1424,23 +1426,26 @@ pi_result cuda_piContextCreate(const pi_context_properties *properties,
errcode_ret = PI_CHECK_ERROR(cuCtxPushCurrent(Ctxt));
} else {
// Create a scoped context.
CUcontext newContext, current;
CUcontext newContext;
PI_CHECK_ERROR(cuCtxGetCurrent(&current));
errcode_ret = PI_CHECK_ERROR(
cuCtxCreate(&newContext, CU_CTX_MAP_HOST, devices[0]->get()));
piContextPtr = std::unique_ptr<_pi_context>(new _pi_context{
_pi_context::kind::user_defined, newContext, *devices});
// For scoped contexts keep the last active CUDA one on top of the stack
// as `cuCtxCreate` replaces it implicitly otherwise.
if (current != nullptr) {
PI_CHECK_ERROR(cuCtxSetCurrent(current));
}
}

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

// For non-primary scoped contexts keep the last active on top of the stack
// as `cuCtxCreate` replaces it implicitly otherwise.
// Primary contexts are kept on top of the stack, so the previous context
// is not queried and therefore not recovered.
if (current != nullptr) {
PI_CHECK_ERROR(cuCtxSetCurrent(current));
}

*retcontext = piContextPtr.release();
} catch (pi_result err) {
errcode_ret = err;
Expand Down