Skip to content

[SYCL] Make host device and host platform always available, even when forcin… #992

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 5 commits into from
Jan 15, 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
4 changes: 2 additions & 2 deletions sycl/source/detail/platform_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ vector_class<platform> platform_impl::get_platforms() {
}
}

if (ForcedType == info::device_type::host || ForcedType == info::device_type::all)
Platforms.emplace_back(platform());
// The host platform should always be available.
Platforms.emplace_back(platform());

return Platforms;
}
Expand Down
16 changes: 12 additions & 4 deletions sycl/source/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ vector_class<device> device::get_devices(info::device_type deviceType) {
if (detail::match_types(deviceType, forced_type)) {
detail::force_type(deviceType, forced_type);
for (const auto &plt : platform::get_platforms()) {
vector_class<device> found_devices(plt.get_devices(deviceType));
if (!found_devices.empty())
devices.insert(devices.end(), found_devices.begin(),
found_devices.end());
// Host device must always be available.
if (plt.is_host()) {
vector_class<device> host_device(
plt.get_devices(info::device_type::host));
if (!host_device.empty())
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this if redundant? I think insert will correctly handle case when begin iterator == end iterator.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No - get_devices is supposed to only return devices of the passed type, hence the special casing for host devices.

devices.insert(devices.end(), host_device.begin(), host_device.end());
} else {
vector_class<device> found_devices(plt.get_devices(deviceType));
if (!found_devices.empty())
devices.insert(devices.end(), found_devices.begin(),
found_devices.end());
}
}
}
return devices;
Expand Down
23 changes: 23 additions & 0 deletions sycl/test/basic_tests/host_always_works.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %clangxx -fsycl %s -o %t1.out
// RUN: env SYCL_DEVICE_TYPE=HOST %t1.out
// RUN: %CPU_RUN_PLACEHOLDER %t1.out
// RUN: %GPU_RUN_PLACEHOLDER %t1.out
// RUN: %ACC_RUN_PLACEHOLDER %t1.out

//==------ host_always_works.cpp - Host Device Availability test -----------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <CL/sycl.hpp>

using namespace cl::sycl;

int main() {
device(host_selector{});

return 0;
}
32 changes: 32 additions & 0 deletions sycl/test/basic_tests/host_platform_avail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: %clangxx -fsycl %s -o %t1.out
// RUN: env SYCL_DEVICE_TYPE=HOST %t1.out
// RUN: %CPU_RUN_PLACEHOLDER %t1.out
// RUN: %GPU_RUN_PLACEHOLDER %t1.out
// RUN: %ACC_RUN_PLACEHOLDER %t1.out

//==------ host_platform_avail.cpp - Host Platform Availability test -------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <CL/sycl.hpp>

using namespace cl::sycl;

int main() {
auto plats = platform::get_platforms();
bool found_host = false;

// Look for a host platform
for (const auto &plat : plats) {
if (plat.is_host()) {
found_host = true;
}
}

// Fail if we didn't find a host platform
return (!found_host);
}