Skip to content

[SYCL] Add 2 queue constructors accepting sycl device argument and context #1080

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

Merged
merged 1 commit into from
Feb 13, 2020
Merged
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
3 changes: 3 additions & 0 deletions sycl/include/CL/sycl/detail/context_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class context_impl {

KernelProgramCache &getKernelProgramCache() const;

/// Returns true if and only if context contains the given device.
bool hasDevice(shared_ptr_class<detail::device_impl> Device) const;

private:
async_handler MAsyncHandler;
vector_class<device> MDevices;
Expand Down
9 changes: 6 additions & 3 deletions sycl/include/CL/sycl/detail/queue_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace sycl {
namespace detail {

using ContextImplPtr = std::shared_ptr<detail::context_impl>;
using DeviceImplPtr = std::shared_ptr<detail::device_impl>;
using DeviceImplPtr = shared_ptr_class<detail::device_impl>;

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

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

// TODO catch an exception and put it to list of asynchronous exceptions
Plugin.call<PiApiKind::piQueueRetain>(MCommandQueue);
Expand Down
26 changes: 24 additions & 2 deletions sycl/include/CL/sycl/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class queue {
/// Constructs a SYCL queue instance that is associated with the context
/// provided, using the device returned by the device selector.
///
/// @param SyclDevice is an instance of SYCL device.
/// @param SyclContext is an instance of SYCL context.
/// @param DeviceSelector is an instance of SYCL device selector.
/// @param PropList is a list of properties for queue construction.
queue(const context &SyclContext, const device_selector &DeviceSelector,
Expand All @@ -93,13 +93,35 @@ class queue {
/// with the context provided, using the device returned by the device
/// selector.
///
/// @param SyclDevice is an instance of SYCL device.
/// @param SyclContext is an instance of SYCL context.
/// @param DeviceSelector is an instance of SYCL device selector.
/// @param AsyncHandler is a SYCL asynchronous exception handler.
/// @param PropList is a list of properties for queue construction.
queue(const context &SyclContext, const device_selector &DeviceSelector,
const async_handler &AsyncHandler, const property_list &PropList = {});

/// Constructs a SYCL queue associated with the given context, device
/// and optional properties list.
///
/// @param SyclContext is an instance of SYCL context.
/// @param SyclDevice is an instance of SYCL device.
/// @param PropList is a list of properties for queue construction.
queue(const context &SyclContext, const device &SyclDevice,
const property_list &PropList = {})
: queue(SyclContext, SyclDevice,
detail::getSyclObjImpl(SyclContext)->get_async_handler(),
PropList) {};

/// Constructs a SYCL queue associated with the given context, device,
/// asynchronous exception handler and optional properties list.
///
/// @param SyclContext is an instance of SYCL context.
/// @param SyclDevice is an instance of SYCL device.
/// @param AsyncHandler is a SYCL asynchronous exception handler.
/// @param PropList is a list of properties for queue construction.
queue(const context &SyclContext, const device &SyclDevice,
const async_handler &AsyncHandler, const property_list &PropList = {});

/// Constructs a SYCL queue with an optional async_handler from an OpenCL
/// cl_command_queue.
///
Expand Down
8 changes: 8 additions & 0 deletions sycl/source/detail/context_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ KernelProgramCache &context_impl::getKernelProgramCache() const {
return MKernelProgramCache;
}

bool
context_impl::hasDevice(shared_ptr_class<detail::device_impl> Device) const {
for (auto D : MDevices)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an STL algorithm. OK, not very compelling while we do not have yet the C++20 syntax here...

if (getSyclObjImpl(D) == Device)
return true;
return false;
}

} // namespace detail
} // namespace sycl
} // namespace cl
9 changes: 9 additions & 0 deletions sycl/source/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ queue::queue(const context &syclContext, const device_selector &deviceSelector,
asyncHandler, detail::getQueueOrder(propList), propList);
}

queue::queue(const context &syclContext,
const device &syclDevice,
const async_handler &asyncHandler,
const property_list &propList) {
impl = std::make_shared<detail::queue_impl>(
detail::getSyclObjImpl(syclDevice), detail::getSyclObjImpl(syclContext),
asyncHandler, cl::sycl::detail::QueueOrder::OOO, propList);
}

queue::queue(const device &syclDevice, const async_handler &asyncHandler,
const property_list &propList) {
impl = std::make_shared<detail::queue_impl>(
Expand Down
20 changes: 20 additions & 0 deletions sycl/test/basic_tests/queue.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: %clangxx -fsycl %s -o %t.out
// RUN: env SYCL_DEVICE_TYPE=HOST %t.out
// RUN: %t.out
//==--------------- queue.cpp - SYCL queue test ----------------------------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Expand Down Expand Up @@ -100,4 +101,23 @@ int main() {
queue Queue(Context, Selector);
assert(Context == Queue.get_context());
}

{
context Context(deviceA);
queue Queue(Context, deviceA);
assert(Context == Queue.get_context());
}

if (devices.size() > 1) {
bool GotException = false;
try {
context Context(deviceA);
queue Queue(Context, deviceB);
assert(Context == Queue.get_context());
} catch (std::exception &e) {
std::cout << "Exception check passed: " << e.what() << std::endl;
GotException = true;
}
assert(GotException);
}
}