From dd6f090f74e67fb2693d456cbfeb01d30475d51e Mon Sep 17 00:00:00 2001 From: JackAKirk Date: Thu, 26 May 2022 18:27:21 +0100 Subject: [PATCH 1/4] If sycl device already exists make_device returns it. Signed-off-by: JackAKirk --- sycl/include/CL/sycl/backend.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sycl/include/CL/sycl/backend.hpp b/sycl/include/CL/sycl/backend.hpp index b2f7b87bf1ecb..3a2c1de3864b5 100644 --- a/sycl/include/CL/sycl/backend.hpp +++ b/sycl/include/CL/sycl/backend.hpp @@ -256,6 +256,13 @@ typename std::enable_if< detail::InteropFeatureSupportMap::MakeDevice == true, device>::type make_device(const typename backend_traits::template input_type &BackendObject) { + auto devs = device::get_devices(info::device_type::all); + for (auto &dev : devs) { + if (dev.get_backend() == Backend && + BackendObject == get_native(dev)) { + return dev; + } + } return detail::make_device(detail::pi::cast(BackendObject), Backend); } From 8c7d856e2d5466aee1ed2954aecc799e459ff33d Mon Sep 17 00:00:00 2001 From: JackAKirk Date: Thu, 2 Jun 2022 16:47:33 +0100 Subject: [PATCH 2/4] example only iter backend devices. Signed-off-by: JackAKirk --- sycl/include/CL/sycl/backend.hpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/sycl/include/CL/sycl/backend.hpp b/sycl/include/CL/sycl/backend.hpp index 3a2c1de3864b5..e21a310ee98e9 100644 --- a/sycl/include/CL/sycl/backend.hpp +++ b/sycl/include/CL/sycl/backend.hpp @@ -256,11 +256,16 @@ typename std::enable_if< detail::InteropFeatureSupportMap::MakeDevice == true, device>::type make_device(const typename backend_traits::template input_type &BackendObject) { - auto devs = device::get_devices(info::device_type::all); - for (auto &dev : devs) { - if (dev.get_backend() == Backend && - BackendObject == get_native(dev)) { - return dev; +auto plts = platform::get_platforms(); +for (const auto &plt : plts) { + if (plt.get_backend() == Backend) + + { + auto devs = plt.get_devices(info::device_type::all); + for (auto &dev : devs) { + if (BackendObject == get_native(dev)) { + return dev; + } } } return detail::make_device(detail::pi::cast(BackendObject), From 8e846e0f6bafadfaa6f9f55b595db5409363d349 Mon Sep 17 00:00:00 2001 From: JackAKirk Date: Thu, 2 Jun 2022 17:02:30 +0100 Subject: [PATCH 3/4] fix typo Signed-off-by: JackAKirk --- sycl/include/CL/sycl/backend.hpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/sycl/include/CL/sycl/backend.hpp b/sycl/include/CL/sycl/backend.hpp index e21a310ee98e9..5df4f9903d8f7 100644 --- a/sycl/include/CL/sycl/backend.hpp +++ b/sycl/include/CL/sycl/backend.hpp @@ -256,15 +256,14 @@ typename std::enable_if< detail::InteropFeatureSupportMap::MakeDevice == true, device>::type make_device(const typename backend_traits::template input_type &BackendObject) { -auto plts = platform::get_platforms(); -for (const auto &plt : plts) { - if (plt.get_backend() == Backend) - - { - auto devs = plt.get_devices(info::device_type::all); - for (auto &dev : devs) { - if (BackendObject == get_native(dev)) { - return dev; + auto plts = platform::get_platforms(); + for (const auto &plt : plts) { + if (plt.get_backend() == Backend) { + auto devs = plt.get_devices(info::device_type::all); + for (auto &dev : devs) { + if (BackendObject == get_native(dev)) { + return dev; + } } } } From 6df9b9ac8118065b050c44b03717591456ccd2bb Mon Sep 17 00:00:00 2001 From: JackAKirk Date: Thu, 14 Jul 2022 15:05:53 +0100 Subject: [PATCH 4/4] Example implementation using getDeviceImpl. Signed-off-by: JackAKirk --- sycl/include/CL/sycl/backend.hpp | 11 ----------- sycl/include/CL/sycl/platform.hpp | 7 ++++++- .../ext/oneapi/experimental/backend/cuda.hpp | 11 +++++++++++ sycl/source/backend.cpp | 19 ++++++++++++++----- sycl/source/detail/platform_impl.cpp | 14 ++++++++++++++ sycl/source/detail/platform_impl.hpp | 4 ++++ 6 files changed, 49 insertions(+), 17 deletions(-) diff --git a/sycl/include/CL/sycl/backend.hpp b/sycl/include/CL/sycl/backend.hpp index 4335629fc8475..40a3b76bbe235 100644 --- a/sycl/include/CL/sycl/backend.hpp +++ b/sycl/include/CL/sycl/backend.hpp @@ -260,17 +260,6 @@ typename std::enable_if< detail::InteropFeatureSupportMap::MakeDevice == true, device>::type make_device(const typename backend_traits::template input_type &BackendObject) { - auto plts = platform::get_platforms(); - for (const auto &plt : plts) { - if (plt.get_backend() == Backend) { - auto devs = plt.get_devices(info::device_type::all); - for (auto &dev : devs) { - if (BackendObject == get_native(dev)) { - return dev; - } - } - } - } return detail::make_device(detail::pi::cast(BackendObject), Backend); } diff --git a/sycl/include/CL/sycl/platform.hpp b/sycl/include/CL/sycl/platform.hpp index e6249a4883bd5..f8158d3e773cb 100644 --- a/sycl/include/CL/sycl/platform.hpp +++ b/sycl/include/CL/sycl/platform.hpp @@ -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(); diff --git a/sycl/include/sycl/ext/oneapi/experimental/backend/cuda.hpp b/sycl/include/sycl/ext/oneapi/experimental/backend/cuda.hpp index 13429456e5b95..45b9070bcb4f8 100644 --- a/sycl/include/sycl/ext/oneapi/experimental/backend/cuda.hpp +++ b/sycl/include/sycl/ext/oneapi/experimental/backend/cuda.hpp @@ -75,6 +75,17 @@ interop_handle::get_native_context() const { template <> inline device make_device( const backend_input_t &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(dev)) { + return dev; + } + } + } + } pi_native_handle NativeHandle = static_cast(BackendObject); return ext::oneapi::cuda::make_device(NativeHandle); } diff --git a/sycl/source/backend.cpp b/sycl/source/backend.cpp index f492716c47fa0..13c1f4e1db3bf 100644 --- a/sycl/source/backend.cpp +++ b/sycl/source/backend.cpp @@ -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(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( + NativeHandle, nullptr, &PiDevice); + auto devImpl = plt.impl->getDeviceImpl(PiDevice, plt.impl); + if (devImpl != nullptr) { + return detail::createSyclObjFromImpl(devImpl); + } + } + } + return detail::createSyclObjFromImpl( std::make_shared(PiDevice, Plugin)); } diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 1cd0e76244b73..029f688f93d45 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -212,6 +212,20 @@ static void filterDeviceFilter(std::vector &PiDevices, Plugin.setLastDeviceId(Platform, DeviceNum); } +std::shared_ptr platform_impl::getDeviceImpl( + RT::PiDevice PiDevice, const std::shared_ptr &PlatformImpl) { + const std::lock_guard Guard(MDeviceMapMutex); + + // If we've already seen this device, return the impl + for (const std::weak_ptr &DeviceWP : MDeviceCache) { + if (std::shared_ptr Device = DeviceWP.lock()) { + if (Device->getHandleRef() == PiDevice) + return Device; + } + } + return nullptr; + } + std::shared_ptr platform_impl::getOrMakeDeviceImpl( RT::PiDevice PiDevice, const std::shared_ptr &PlatformImpl) { const std::lock_guard Guard(MDeviceMapMutex); diff --git a/sycl/source/detail/platform_impl.hpp b/sycl/source/detail/platform_impl.hpp index a0e673d440f7d..630d20609cf20 100644 --- a/sycl/source/detail/platform_impl.hpp +++ b/sycl/source/detail/platform_impl.hpp @@ -152,6 +152,10 @@ class platform_impl { getOrMakeDeviceImpl(RT::PiDevice PiDevice, const std::shared_ptr &PlatformImpl); + std::shared_ptr + getDeviceImpl(RT::PiDevice PiDevice, + const std::shared_ptr &PlatformImpl); + /// Static functions that help maintain platform uniquess and /// equality of comparison