Skip to content

[SYCL] Fix broken uniqueness in make_device #6204

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

Closed
wants to merge 5 commits into from
Closed
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
7 changes: 6 additions & 1 deletion sycl/include/CL/sycl/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ class device_selector;
class device;
namespace detail {
class platform_impl;
}
device make_device(pi_native_handle NativeHandle, backend Backend);
} // namespace detail

/// Encapsulates a SYCL platform on which kernels may be executed.
///
/// \ingroup sycl_api
class __SYCL_EXPORT platform {
public:

friend device detail::make_device(pi_native_handle NativeHandle,
backend Backend);

/// Constructs a SYCL platform as a host platform.
platform();

Expand Down
11 changes: 11 additions & 0 deletions sycl/include/sycl/ext/oneapi/experimental/backend/cuda.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ interop_handle::get_native_context<backend::ext_oneapi_cuda>() const {
template <>
inline device make_device<backend::ext_oneapi_cuda>(
const backend_input_t<backend::ext_oneapi_cuda, device> &BackendObject) {
auto plts = platform::get_platforms();
for (const auto &plt : plts) {
if (plt.get_backend() == backend::ext_oneapi_cuda) {
auto devs = plt.get_devices(info::device_type::all);
for (auto &dev : devs) {
if (BackendObject == get_native<backend::ext_oneapi_cuda>(dev)) {
return dev;
}
}
}
}
pi_native_handle NativeHandle = static_cast<pi_native_handle>(BackendObject);
return ext::oneapi::cuda::make_device(NativeHandle);
}
Expand Down
19 changes: 14 additions & 5 deletions sycl/source/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,21 @@ platform make_platform(pi_native_handle NativeHandle, backend Backend) {

__SYCL_EXPORT device make_device(pi_native_handle NativeHandle,
backend Backend) {
const auto &Plugin = getPlugin(Backend);

pi::PiDevice PiDevice = nullptr;
Plugin.call<PiApiKind::piextDeviceCreateWithNativeHandle>(NativeHandle,
nullptr, &PiDevice);
// Construct the SYCL device from PI device.
auto plts = platform::get_platforms();
detail::pi::PiDevice PiDevice = nullptr;
const auto &Plugin = detail::getPlugin(Backend);
for (const auto &plt : plts) {
if (plt.get_backend() == Backend) {
Plugin.call<detail::PiApiKind::piextDeviceCreateWithNativeHandle>(
NativeHandle, nullptr, &PiDevice);
auto devImpl = plt.impl->getDeviceImpl(PiDevice, plt.impl);
if (devImpl != nullptr) {
return detail::createSyclObjFromImpl<device>(devImpl);
}
}
}

return detail::createSyclObjFromImpl<device>(
std::make_shared<device_impl>(PiDevice, Plugin));
}
Expand Down
14 changes: 14 additions & 0 deletions sycl/source/detail/platform_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,20 @@ static void filterDeviceFilter(std::vector<RT::PiDevice> &PiDevices,
Plugin.setLastDeviceId(Platform, DeviceNum);
}

std::shared_ptr<device_impl> platform_impl::getDeviceImpl(
RT::PiDevice PiDevice, const std::shared_ptr<platform_impl> &PlatformImpl) {
const std::lock_guard<std::mutex> Guard(MDeviceMapMutex);

// If we've already seen this device, return the impl
for (const std::weak_ptr<device_impl> &DeviceWP : MDeviceCache) {
if (std::shared_ptr<device_impl> Device = DeviceWP.lock()) {
if (Device->getHandleRef() == PiDevice)
return Device;
}
}
return nullptr;
}

std::shared_ptr<device_impl> platform_impl::getOrMakeDeviceImpl(
RT::PiDevice PiDevice, const std::shared_ptr<platform_impl> &PlatformImpl) {
const std::lock_guard<std::mutex> Guard(MDeviceMapMutex);
Expand Down
4 changes: 4 additions & 0 deletions sycl/source/detail/platform_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ class platform_impl {
getOrMakeDeviceImpl(RT::PiDevice PiDevice,
const std::shared_ptr<platform_impl> &PlatformImpl);

std::shared_ptr<device_impl>
getDeviceImpl(RT::PiDevice PiDevice,
const std::shared_ptr<platform_impl> &PlatformImpl);

/// Static functions that help maintain platform uniquess and
/// equality of comparison

Expand Down