Skip to content

Commit 4b2a158

Browse files
[SYCL] match existing device_impl from context, rather than creating a new one. (#6423)
The queue_impl(PI) constructor is creating a new device_impl, but that is not strictly correct. Presently this SYCL implementation assumes there is a 1:1 relationship between devicesPI and devic_impl. By creating a new one, there will now be two device_impls (one already in the context) for the same devicePI. In this PR we are simply finding the matching device_impl in the Context and using that rather than creating a new one. Signed-off-by: Chris Perkins <chris.perkins@intel.com>
1 parent ee93fbc commit 4b2a158

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

sycl/source/detail/context_impl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ bool context_impl::hasDevice(
199199
return false;
200200
}
201201

202+
DeviceImplPtr
203+
context_impl::findMatchingDeviceImpl(RT::PiDevice &DevicePI) const {
204+
for (device D : MDevices)
205+
if (getSyclObjImpl(D)->getHandleRef() == DevicePI)
206+
return getSyclObjImpl(D);
207+
208+
return nullptr;
209+
}
210+
202211
pi_native_handle context_impl::getNative() const {
203212
auto Plugin = getPlugin();
204213
if (Plugin.getBackend() == backend::opencl)

sycl/source/detail/context_impl.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ class context_impl {
165165
/// Returns true if and only if context contains the given device.
166166
bool hasDevice(std::shared_ptr<detail::device_impl> Device) const;
167167

168+
/// Given a PiDevice, returns the matching shared_ptr<device_impl>
169+
/// within this context. May return nullptr if no match discovered.
170+
DeviceImplPtr findMatchingDeviceImpl(RT::PiDevice &DevicePI) const;
171+
168172
/// Gets the native handle of the SYCL context.
169173
///
170174
/// \return a native handle.

sycl/source/detail/queue_impl.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,16 @@ class queue_impl {
158158

159159
MQueues.push_back(pi::cast<RT::PiQueue>(PiQueue));
160160

161-
RT::PiDevice Device{};
161+
RT::PiDevice DevicePI{};
162162
const detail::plugin &Plugin = getPlugin();
163163
// TODO catch an exception and put it to list of asynchronous exceptions
164-
Plugin.call<PiApiKind::piQueueGetInfo>(MQueues[0], PI_QUEUE_INFO_DEVICE,
165-
sizeof(Device), &Device, nullptr);
166-
MDevice =
167-
DeviceImplPtr(new device_impl(Device, Context->getPlatformImpl()));
164+
Plugin.call<PiApiKind::piQueueGetInfo>(
165+
MQueues[0], PI_QUEUE_INFO_DEVICE, sizeof(DevicePI), &DevicePI, nullptr);
166+
MDevice = MContext->findMatchingDeviceImpl(DevicePI);
167+
if (MDevice == nullptr)
168+
throw sycl::exception(
169+
make_error_code(errc::invalid),
170+
"Device provided by native Queue not found in Context.");
168171
}
169172

170173
~queue_impl() {

0 commit comments

Comments
 (0)