Skip to content

[SYCL] add device_type to SYCL_PI_TRACE and device_selector exception #6896

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 2 commits into from
Sep 29, 2022
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
33 changes: 33 additions & 0 deletions sycl/source/device_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ device select_device(DSelectorInvocableType DeviceSelectorInvocable,
return *res;
}

auto Selector = DeviceSelectorInvocable.target<int (*)(const sycl::device &)>();
if ((Selector && *Selector == gpu_selector_v)
|| DeviceSelectorInvocable.target<sycl::gpu_selector>()) {
throw sycl::runtime_error(
"No device of requested type 'info::device_type::gpu' available.",
PI_ERROR_DEVICE_NOT_FOUND);
}
if ((Selector && *Selector == cpu_selector_v)
|| DeviceSelectorInvocable.target<sycl::cpu_selector>()) {
throw sycl::runtime_error(
"No device of requested type 'info::device_type::cpu' available.",
PI_ERROR_DEVICE_NOT_FOUND);
}
if ((Selector && *Selector == accelerator_selector_v)
|| DeviceSelectorInvocable.target<sycl::accelerator_selector>()) {
throw sycl::runtime_error("No device of requested type "
"'info::device_type::accelerator' available.",
PI_ERROR_DEVICE_NOT_FOUND);
}
throw sycl::runtime_error("No device of requested type available.",
PI_ERROR_DEVICE_NOT_FOUND);
}
Expand Down Expand Up @@ -137,10 +156,20 @@ select_device(const DSelectorInvocableType &DeviceSelectorInvocable,
/// 2. CPU
/// 3. Host
/// 4. Accelerator

static void traceDeviceSelector(const std::string &DeviceType) {
bool ShouldTrace = false;
ShouldTrace = detail::pi::trace(detail::pi::TraceLevel::PI_TRACE_BASIC);
if (ShouldTrace) {
std::cout << "SYCL_PI_TRACE[all]: Requested device_type: " << DeviceType << std::endl;
Copy link
Contributor

Choose a reason for hiding this comment

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

If you query for PI_TRACE_BASIC in L159, shouldn't this message start with "SYCL_PI_TRACE[basic]"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case it's not aligned with other device selector information that started with SYCL_PI_TRACE[all] but it is also printed as basic information in case of successful device initialization. So I did it in the same style for better readability.

SYCL_PI_TRACE[all]: Selected device: -> final score = 1000
SYCL_PI_TRACE[all]:   platform: Intel(R) OpenCL
SYCL_PI_TRACE[all]:   device: Intel(R) Xeon(R) Gold 6354 CPU @ 3.00GHz

}
}

__SYCL_EXPORT int default_selector_v(const device &dev) {
// The default selector doesn't reject any devices.
int Score = 0;

traceDeviceSelector("info::device_type::automatic");
if (dev.get_info<info::device::device_type>() == detail::get_forced_type())
Score += 2000;

Expand All @@ -165,6 +194,7 @@ __SYCL_EXPORT int default_selector_v(const device &dev) {
__SYCL_EXPORT int gpu_selector_v(const device &dev) {
int Score = detail::REJECT_DEVICE_SCORE;

traceDeviceSelector("info::device_type::gpu");
if (dev.is_gpu()) {
Score = 1000;
Score += detail::getDevicePreference(dev);
Expand All @@ -175,6 +205,7 @@ __SYCL_EXPORT int gpu_selector_v(const device &dev) {
__SYCL_EXPORT int cpu_selector_v(const device &dev) {
int Score = detail::REJECT_DEVICE_SCORE;

traceDeviceSelector("info::device_type::cpu");
if (dev.is_cpu()) {
Score = 1000;
Score += detail::getDevicePreference(dev);
Expand All @@ -185,6 +216,7 @@ __SYCL_EXPORT int cpu_selector_v(const device &dev) {
__SYCL_EXPORT int accelerator_selector_v(const device &dev) {
int Score = detail::REJECT_DEVICE_SCORE;

traceDeviceSelector("info::device_type::accelerator");
if (dev.is_accelerator()) {
Score = 1000;
Score += detail::getDevicePreference(dev);
Expand All @@ -196,6 +228,7 @@ int host_selector::operator()(const device &dev) const {
// Host device has been removed and host_selector has been deprecated, so this
// should never be able to select a device.
std::ignore = dev;
traceDeviceSelector("info::device_type::host");
return detail::REJECT_DEVICE_SCORE;
}

Expand Down
45 changes: 45 additions & 0 deletions sycl/test/basic_tests/device-selectors-exception.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
// RUN: env SYCL_DEVICE_FILTER="" %t.out

#include <sycl/sycl.hpp>
using namespace sycl;

int refuse_any_device_f(const device &d) { return -1; }

int main() {

// Check exception message for custom device selector
try {
queue custom_queue(refuse_any_device_f);
} catch (exception &E) {
assert(std::string(E.what()).find("info::device_type::") ==
std::string::npos &&
"Incorrect device type in exception message for custom selector.");
}

// Check exception message for pre-defined devices
try {
queue gpu_queue(gpu_selector_v);
} catch (exception &E) {
assert(std::string(E.what()).find("info::device_type::gpu") !=
std::string::npos &&
"Incorrect device type in exception message for GPU device.");
}
try {
queue cpu_queue(cpu_selector_v);
} catch (exception &E) {
assert(std::string(E.what()).find("info::device_type::cpu") !=
std::string::npos &&
"Incorrect device type in exception message for CPU device.");
}
try {
queue acc_queue(accelerator_selector_v);
} catch (exception &E) {
assert(
std::string(E.what()).find("info::device_type::accelerator") !=
std::string::npos &&
"Incorrect device type in exception message for Accelerator device.");
}

return 0;
}