Skip to content

Commit c04e5db

Browse files
[SYCL] Use platform_impl::getOrMakeDeviceImpl in make_device (#18163)
This effectively reverts part of the changes made in #13483 in favor of a simpler solution. The tests added there are unchanged and are verifying this PR. This made one `device_impl` ctor overload unused with another already dead prior to this. Removing them both here and making some other simplifications following from that removal.
1 parent 53603ec commit c04e5db

File tree

4 files changed

+11
-63
lines changed

4 files changed

+11
-63
lines changed

sycl/include/sycl/backend.hpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,16 +305,6 @@ std::enable_if_t<detail::InteropFeatureSupportMap<Backend>::MakeDevice == true,
305305
device>
306306
make_device(const typename backend_traits<Backend>::template input_type<device>
307307
&BackendObject) {
308-
for (auto p : platform::get_platforms()) {
309-
if (p.get_backend() != Backend)
310-
continue;
311-
312-
for (auto d : p.get_devices()) {
313-
if (get_native<Backend>(d) == BackendObject)
314-
return d;
315-
}
316-
}
317-
318308
return detail::make_device(
319309
detail::ur::cast<ur_native_handle_t>(BackendObject), Backend);
320310
}

sycl/source/backend.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ __SYCL_EXPORT device make_device(ur_native_handle_t NativeHandle,
8787
ur_device_handle_t UrDevice = nullptr;
8888
Adapter->call<UrApiKind::urDeviceCreateWithNativeHandle>(
8989
NativeHandle, Adapter->getUrAdapter(), nullptr, &UrDevice);
90+
9091
// Construct the SYCL device from UR device.
92+
auto Platform = platform_impl::getPlatformFromUrDevice(UrDevice, Adapter);
9193
return detail::createSyclObjFromImpl<device>(
92-
std::make_shared<device_impl>(UrDevice, Adapter));
94+
Platform->getOrMakeDeviceImpl(UrDevice, Platform));
9395
}
9496

9597
__SYCL_EXPORT context make_context(ur_native_handle_t NativeHandle,

sycl/source/detail/device_impl.cpp

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,59 +19,29 @@ namespace sycl {
1919
inline namespace _V1 {
2020
namespace detail {
2121

22-
device_impl::device_impl(ur_native_handle_t InteropDeviceHandle,
23-
const AdapterPtr &Adapter)
24-
: device_impl(InteropDeviceHandle, nullptr, nullptr, Adapter) {}
25-
2622
/// Constructs a SYCL device instance using the provided
2723
/// UR device instance.
2824
device_impl::device_impl(ur_device_handle_t Device, PlatformImplPtr Platform)
29-
: device_impl(0, Device, Platform, Platform->getAdapter()) {}
30-
31-
/// Constructs a SYCL device instance using the provided
32-
/// UR device instance.
33-
device_impl::device_impl(ur_device_handle_t Device, const AdapterPtr &Adapter)
34-
: device_impl(0, Device, nullptr, Adapter) {}
35-
36-
device_impl::device_impl(ur_native_handle_t InteropDeviceHandle,
37-
ur_device_handle_t Device, PlatformImplPtr Platform,
38-
const AdapterPtr &Adapter)
39-
: MDevice(Device), MDeviceHostBaseTime(std::make_pair(0, 0)) {
40-
bool InteroperabilityConstructor = false;
41-
if (Device == nullptr) {
42-
assert(InteropDeviceHandle);
43-
// Get UR device from the raw device handle.
44-
// NOTE: this is for OpenCL interop only (and should go away).
45-
// With SYCL-2020 BE generalization "make" functions are used instead.
46-
Adapter->call<UrApiKind::urDeviceCreateWithNativeHandle>(
47-
InteropDeviceHandle, Adapter->getUrAdapter(), nullptr, &MDevice);
48-
InteroperabilityConstructor = true;
49-
}
25+
: MDevice(Device), MPlatform(Platform),
26+
MDeviceHostBaseTime(std::make_pair(0, 0)) {
27+
const AdapterPtr &Adapter = Platform->getAdapter();
5028

5129
// TODO catch an exception and put it to list of asynchronous exceptions
5230
Adapter->call<UrApiKind::urDeviceGetInfo>(
5331
MDevice, UR_DEVICE_INFO_TYPE, sizeof(ur_device_type_t), &MType, nullptr);
5432

5533
// No need to set MRootDevice when MAlwaysRootDevice is true
56-
if ((Platform == nullptr) || !Platform->MAlwaysRootDevice) {
34+
if (!Platform->MAlwaysRootDevice) {
5735
// TODO catch an exception and put it to list of asynchronous exceptions
5836
Adapter->call<UrApiKind::urDeviceGetInfo>(
5937
MDevice, UR_DEVICE_INFO_PARENT_DEVICE, sizeof(ur_device_handle_t),
6038
&MRootDevice, nullptr);
6139
}
6240

63-
if (!InteroperabilityConstructor) {
64-
// TODO catch an exception and put it to list of asynchronous exceptions
65-
// Interoperability Constructor already calls DeviceRetain in
66-
// urDeviceCreateWithNativeHandle.
67-
Adapter->call<UrApiKind::urDeviceRetain>(MDevice);
68-
}
69-
70-
// set MPlatform
71-
if (!Platform) {
72-
Platform = platform_impl::getPlatformFromUrDevice(MDevice, Adapter);
73-
}
74-
MPlatform = Platform;
41+
// TODO catch an exception and put it to list of asynchronous exceptions
42+
// Interoperability Constructor already calls DeviceRetain in
43+
// urDeviceCreateWithNativeHandle.
44+
Adapter->call<UrApiKind::urDeviceRetain>(MDevice);
7545

7646
Adapter->call<UrApiKind::urDeviceGetInfo>(
7747
MDevice, UR_DEVICE_INFO_USE_NATIVE_ASSERT, sizeof(ur_bool_t),

sycl/source/detail/device_impl.hpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,10 @@ using PlatformImplPtr = std::shared_ptr<platform_impl>;
3535
// TODO: Make code thread-safe
3636
class device_impl {
3737
public:
38-
/// Constructs a SYCL device instance as a host device.
39-
device_impl();
40-
41-
/// Constructs a SYCL device instance using the provided raw device handle.
42-
explicit device_impl(ur_native_handle_t, const AdapterPtr &Adapter);
43-
4438
/// Constructs a SYCL device instance using the provided
4539
/// UR device instance.
4640
explicit device_impl(ur_device_handle_t Device, PlatformImplPtr Platform);
4741

48-
/// Constructs a SYCL device instance using the provided
49-
/// UR device instance.
50-
explicit device_impl(ur_device_handle_t Device, const AdapterPtr &Adapter);
51-
5242
~device_impl();
5343

5444
/// Get instance of OpenCL device
@@ -299,10 +289,6 @@ class device_impl {
299289
ext::oneapi::experimental::architecture getDeviceArch() const;
300290

301291
private:
302-
explicit device_impl(ur_native_handle_t InteropDevice,
303-
ur_device_handle_t Device, PlatformImplPtr Platform,
304-
const AdapterPtr &Adapter);
305-
306292
ur_device_handle_t MDevice = 0;
307293
ur_device_type_t MType;
308294
ur_device_handle_t MRootDevice = nullptr;

0 commit comments

Comments
 (0)