Skip to content

[SYCL] Fix context release #2123

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 4 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
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
30 changes: 16 additions & 14 deletions sycl/source/detail/context_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ namespace detail {
context_impl::context_impl(const device &Device, async_handler AsyncHandler,
bool UseCUDAPrimaryContext)
: MAsyncHandler(AsyncHandler), MDevices(1, Device), MContext(nullptr),
MPlatform(), MPluginInterop(false), MHostContext(true),
MPlatform(), MHostContext(Device.is_host()),
MUseCUDAPrimaryContext(UseCUDAPrimaryContext) {
MKernelProgramCache.setContextPtr(this);
}

context_impl::context_impl(const vector_class<cl::sycl::device> Devices,
async_handler AsyncHandler, bool UseCUDAPrimaryContext)
async_handler AsyncHandler,
bool UseCUDAPrimaryContext)
: MAsyncHandler(AsyncHandler), MDevices(Devices), MContext(nullptr),
MPlatform(), MPluginInterop(true), MHostContext(false),
MPlatform(), MHostContext(false),
MUseCUDAPrimaryContext(UseCUDAPrimaryContext) {
MPlatform = detail::getSyclObjImpl(MDevices[0].get_platform());
vector_class<RT::PiDevice> DeviceIds;
Expand All @@ -48,14 +49,15 @@ context_impl::context_impl(const vector_class<cl::sycl::device> Devices,
static_cast<pi_context_properties>(PI_CONTEXT_PROPERTIES_CUDA_PRIMARY),
static_cast<pi_context_properties>(UseCUDAPrimaryContext), 0};

getPlugin().call<PiApiKind::piContextCreate>(props, DeviceIds.size(),
DeviceIds.data(), nullptr, nullptr, &MContext);
getPlugin().call<PiApiKind::piContextCreate>(
props, DeviceIds.size(), DeviceIds.data(), nullptr, nullptr, &MContext);
#else
cl::sycl::detail::pi::die("CUDA support was not enabled at compilation time");
#endif
} else {
getPlugin().call<PiApiKind::piContextCreate>(nullptr, DeviceIds.size(),
DeviceIds.data(), nullptr, nullptr, &MContext);
getPlugin().call<PiApiKind::piContextCreate>(nullptr, DeviceIds.size(),
DeviceIds.data(), nullptr,
nullptr, &MContext);
}

MKernelProgramCache.setContextPtr(this);
Expand All @@ -64,7 +66,7 @@ context_impl::context_impl(const vector_class<cl::sycl::device> Devices,
context_impl::context_impl(RT::PiContext PiContext, async_handler AsyncHandler,
const plugin &Plugin)
: MAsyncHandler(AsyncHandler), MDevices(), MContext(PiContext), MPlatform(),
MPluginInterop(true), MHostContext(false) {
MHostContext(false) {

vector_class<RT::PiDevice> DeviceIds;
size_t DevicesNum = 0;
Expand Down Expand Up @@ -92,7 +94,7 @@ context_impl::context_impl(RT::PiContext PiContext, async_handler AsyncHandler,
}

cl_context context_impl::get() const {
if (MPluginInterop) {
if (!MHostContext) {
// TODO catch an exception and put it to list of asynchronous exceptions
getPlugin().call<PiApiKind::piContextRetain>(MContext);
return pi::cast<cl_context>(MContext);
Expand All @@ -102,17 +104,17 @@ cl_context context_impl::get() const {
PI_INVALID_CONTEXT);
}

bool context_impl::is_host() const { return MHostContext || !MPluginInterop; }
bool context_impl::is_host() const { return MHostContext; }

context_impl::~context_impl() {
if (MPluginInterop) {
// TODO catch an exception and put it to list of asynchronous exceptions
getPlugin().call<PiApiKind::piContextRelease>(MContext);
}
for (auto LibProg : MCachedLibPrograms) {
assert(LibProg.second && "Null program must not be kept in the cache");
getPlugin().call<PiApiKind::piProgramRelease>(LibProg.second);
}
if (!MHostContext) {
// TODO catch an exception and put it to list of asynchronous exceptions
getPlugin().call<PiApiKind::piContextRelease>(MContext);
}
}

const async_handler &context_impl::get_async_handler() const {
Expand Down
1 change: 0 additions & 1 deletion sycl/source/detail/context_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ class context_impl {
vector_class<device> MDevices;
RT::PiContext MContext;
PlatformImplPtr MPlatform;
bool MPluginInterop;
bool MHostContext;
bool MUseCUDAPrimaryContext;
std::map<DeviceLibExt, RT::PiProgram> MCachedLibPrograms;
Expand Down
13 changes: 13 additions & 0 deletions sycl/test/regression/pi_release.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// REQUIRES: cpu
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
// RUN: env SYCL_PI_TRACE=-1 %CPU_RUN_PLACEHOLDER %t.out 2>&1 | FileCheck %s

#include <CL/sycl.hpp>

int main() {
sycl::queue q;
return 0;
}

// CHECK: piQueueRelease
// CHECK: piContextRelease