Skip to content

Commit 2488236

Browse files
committed
[SYCL] Add 2 queue constructors accepting sycl context and device arguments
Signed-off-by: Klochkov <[email protected]> Signed-off-by: Vyacheslav N Klochkov <[email protected]>
1 parent ffcad03 commit 2488236

File tree

6 files changed

+70
-5
lines changed

6 files changed

+70
-5
lines changed

sycl/include/CL/sycl/detail/context_impl.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ class context_impl {
138138

139139
KernelProgramCache &getKernelProgramCache() const;
140140

141+
/// Returns true if and only if context contains the given device.
142+
bool hasDevice(shared_ptr_class<detail::device_impl> Device) const;
143+
141144
private:
142145
async_handler MAsyncHandler;
143146
vector_class<device> MDevices;

sycl/include/CL/sycl/detail/queue_impl.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace sycl {
2727
namespace detail {
2828

2929
using ContextImplPtr = std::shared_ptr<detail::context_impl>;
30-
using DeviceImplPtr = std::shared_ptr<detail::device_impl>;
30+
using DeviceImplPtr = shared_ptr_class<detail::device_impl>;
3131

3232
/// Sets max number of queues supported by FPGA RT.
3333
const size_t MaxNumQueues = 256;
@@ -72,6 +72,10 @@ class queue_impl {
7272
if (!MHostQueue) {
7373
MCommandQueue = createQueue(Order);
7474
}
75+
if (!Context->hasDevice(Device))
76+
throw cl::sycl::invalid_parameter_error(
77+
"Queue cannot be constructed with the given context and device "
78+
"as the context does not contain the given device.");
7579
}
7680

7781
/// Constructs a SYCL queue from plugin interoperability handle.
@@ -92,8 +96,7 @@ class queue_impl {
9296
// TODO catch an exception and put it to list of asynchronous exceptions
9397
Plugin.call<PiApiKind::piQueueGetInfo>(MCommandQueue, PI_QUEUE_INFO_DEVICE,
9498
sizeof(Device), &Device, nullptr);
95-
MDevice =
96-
std::make_shared<device_impl>(Device, Context->getPlatformImpl());
99+
MDevice = DeviceImplPtr(new device_impl(Device, Context->getPlatformImpl()));
97100

98101
// TODO catch an exception and put it to list of asynchronous exceptions
99102
Plugin.call<PiApiKind::piQueueRetain>(MCommandQueue);

sycl/include/CL/sycl/queue.hpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class queue {
8383
/// Constructs a SYCL queue instance that is associated with the context
8484
/// provided, using the device returned by the device selector.
8585
///
86-
/// @param SyclDevice is an instance of SYCL device.
86+
/// @param SyclContext is an instance of SYCL context.
8787
/// @param DeviceSelector is an instance of SYCL device selector.
8888
/// @param PropList is a list of properties for queue construction.
8989
queue(const context &SyclContext, const device_selector &DeviceSelector,
@@ -93,13 +93,35 @@ class queue {
9393
/// with the context provided, using the device returned by the device
9494
/// selector.
9595
///
96-
/// @param SyclDevice is an instance of SYCL device.
96+
/// @param SyclContext is an instance of SYCL context.
9797
/// @param DeviceSelector is an instance of SYCL device selector.
9898
/// @param AsyncHandler is a SYCL asynchronous exception handler.
9999
/// @param PropList is a list of properties for queue construction.
100100
queue(const context &SyclContext, const device_selector &DeviceSelector,
101101
const async_handler &AsyncHandler, const property_list &PropList = {});
102102

103+
/// Constructs a SYCL queue associated with the given context, device
104+
/// and optional properties list.
105+
///
106+
/// @param SyclContext is an instance of SYCL context.
107+
/// @param SyclDevice is an instance of SYCL device.
108+
/// @param PropList is a list of properties for queue construction.
109+
queue(const context &SyclContext, const device &SyclDevice,
110+
const property_list &PropList = {})
111+
: queue(SyclContext, SyclDevice,
112+
detail::getSyclObjImpl(SyclContext)->get_async_handler(),
113+
PropList) {};
114+
115+
/// Constructs a SYCL queue associated with the given context, device,
116+
/// asynchronous exception handler and optional properties list.
117+
///
118+
/// @param SyclContext is an instance of SYCL context.
119+
/// @param SyclDevice is an instance of SYCL device.
120+
/// @param AsyncHandler is a SYCL asynchronous exception handler.
121+
/// @param PropList is a list of properties for queue construction.
122+
queue(const context &SyclContext, const device &SyclDevice,
123+
const async_handler &AsyncHandler, const property_list &PropList = {});
124+
103125
/// Constructs a SYCL queue with an optional async_handler from an OpenCL
104126
/// cl_command_queue.
105127
///

sycl/source/detail/context_impl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ KernelProgramCache &context_impl::getKernelProgramCache() const {
125125
return MKernelProgramCache;
126126
}
127127

128+
bool
129+
context_impl::hasDevice(shared_ptr_class<detail::device_impl> Device) const {
130+
for (auto D : MDevices)
131+
if (getSyclObjImpl(D) == Device)
132+
return true;
133+
return false;
134+
}
135+
128136
} // namespace detail
129137
} // namespace sycl
130138
} // namespace cl

sycl/source/queue.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ queue::queue(const context &syclContext, const device_selector &deviceSelector,
4545
asyncHandler, detail::getQueueOrder(propList), propList);
4646
}
4747

48+
queue::queue(const context &syclContext,
49+
const device &syclDevice,
50+
const async_handler &asyncHandler,
51+
const property_list &propList) {
52+
impl = std::make_shared<detail::queue_impl>(
53+
detail::getSyclObjImpl(syclDevice), detail::getSyclObjImpl(syclContext),
54+
asyncHandler, cl::sycl::detail::QueueOrder::OOO, propList);
55+
}
56+
4857
queue::queue(const device &syclDevice, const async_handler &asyncHandler,
4958
const property_list &propList) {
5059
impl = std::make_shared<detail::queue_impl>(

sycl/test/basic_tests/queue.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clangxx -fsycl %s -o %t.out
22
// RUN: env SYCL_DEVICE_TYPE=HOST %t.out
3+
// RUN: %t.out
34
//==--------------- queue.cpp - SYCL queue test ----------------------------==//
45
//
56
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
@@ -100,4 +101,23 @@ int main() {
100101
queue Queue(Context, Selector);
101102
assert(Context == Queue.get_context());
102103
}
104+
105+
{
106+
context Context(deviceA);
107+
queue Queue(Context, deviceA);
108+
assert(Context == Queue.get_context());
109+
}
110+
111+
if (devices.size() > 1) {
112+
bool GotException = false;
113+
try {
114+
context Context(deviceA);
115+
queue Queue(Context, deviceB);
116+
assert(Context == Queue.get_context());
117+
} catch (std::exception &e) {
118+
std::cout << "Exception check passed: " << e.what() << std::endl;
119+
GotException = true;
120+
}
121+
assert(GotException);
122+
}
103123
}

0 commit comments

Comments
 (0)