From 72a077ac1e6326e4e27eb5768b7ef5865d983634 Mon Sep 17 00:00:00 2001 From: Sergey Semenov Date: Tue, 3 Mar 2020 11:52:49 +0300 Subject: [PATCH] [SYCL] Fix device::get_devices() with a non-host device type This patches fixes a bug where a host device was always included in the device list returned by device::get_devices(). Signed-off-by: Sergey Semenov --- sycl/source/device.cpp | 6 +++-- sycl/test/basic_tests/get_nonhost_devices.cpp | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 sycl/test/basic_tests/get_nonhost_devices.cpp diff --git a/sycl/source/device.cpp b/sycl/source/device.cpp index 6da02b72eeeda..1f9fa6ab9d071 100644 --- a/sycl/source/device.cpp +++ b/sycl/source/device.cpp @@ -37,13 +37,15 @@ device::device(const device_selector &deviceSelector) { vector_class device::get_devices(info::device_type deviceType) { vector_class devices; + // Host device availability should not depend on the forced type + const bool includeHost = + detail::match_types(deviceType, info::device_type::host); info::device_type forced_type = detail::get_forced_type(); // Exclude devices which do not match requested device type if (detail::match_types(deviceType, forced_type)) { detail::force_type(deviceType, forced_type); for (const auto &plt : platform::get_platforms()) { - // Host device must always be available. - if (plt.is_host()) { + if (includeHost && plt.is_host()) { vector_class host_device( plt.get_devices(info::device_type::host)); if (!host_device.empty()) diff --git a/sycl/test/basic_tests/get_nonhost_devices.cpp b/sycl/test/basic_tests/get_nonhost_devices.cpp new file mode 100644 index 0000000000000..aefbfafce82cf --- /dev/null +++ b/sycl/test/basic_tests/get_nonhost_devices.cpp @@ -0,0 +1,25 @@ +// RUN: %clangxx -fsycl %s -o %t.out +// RUN: %t.out + +// Check that the host device is not included in devices returned by +// get_devices() if a non-host device type is specified. + +#include + +#include + +using namespace cl::sycl; + +void check(info::device_type DT) { + vector_class Devices = device::get_devices(DT); + for (const auto &Device : Devices) + assert(!Device.is_host()); +} + +int main() { + check(info::device_type::cpu); + check(info::device_type::gpu); + check(info::device_type::accelerator); + check(info::device_type::custom); + check(info::device_type::automatic); +}