Skip to content

Commit 6b83ad7

Browse files
authored
[SYCL] Add device_type to SYCL_PI_TRACE and device_selector exception (#6896)
Device selector exception message and SYCL_PI_TRACE are extended with device type info for better debuggability in case of missed device in application that works with multiple devices. Current exception message: ``` No device of requested type available. -1 (PI_ERROR_DEVICE_NOT_FOUND) # in case of any device selector ``` Example of the current output from SYCL_PI_TRACE: ``` 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 ``` New exception message: ``` No device of requested type available. -1 (PI_ERROR_DEVICE_NOT_FOUND) # in case of custom/default selector No device of requested type 'info::device_type::gpu' available. -1 (PI_ERROR_DEVICE_NOT_FOUND) # in case of gpu selector No device of requested type 'info::device_type::cpu' available. -1 (PI_ERROR_DEVICE_NOT_FOUND) # in case of cpu selector No device of requested type 'info::device_type::accelerator' available. -1 (PI_ERROR_DEVICE_NOT_FOUND) # in case of accelerator selector ``` Example of SYCL_PI_TRACE output with extra line about requested device_type: ``` SYCL_PI_TRACE[all]: Requested device_type: info::device_type::cpu 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 ```
1 parent 83febf9 commit 6b83ad7

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

sycl/source/device_selector.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@ device select_device(DSelectorInvocableType DeviceSelectorInvocable,
106106
return *res;
107107
}
108108

109+
auto Selector = DeviceSelectorInvocable.target<int (*)(const sycl::device &)>();
110+
if ((Selector && *Selector == gpu_selector_v)
111+
|| DeviceSelectorInvocable.target<sycl::gpu_selector>()) {
112+
throw sycl::runtime_error(
113+
"No device of requested type 'info::device_type::gpu' available.",
114+
PI_ERROR_DEVICE_NOT_FOUND);
115+
}
116+
if ((Selector && *Selector == cpu_selector_v)
117+
|| DeviceSelectorInvocable.target<sycl::cpu_selector>()) {
118+
throw sycl::runtime_error(
119+
"No device of requested type 'info::device_type::cpu' available.",
120+
PI_ERROR_DEVICE_NOT_FOUND);
121+
}
122+
if ((Selector && *Selector == accelerator_selector_v)
123+
|| DeviceSelectorInvocable.target<sycl::accelerator_selector>()) {
124+
throw sycl::runtime_error("No device of requested type "
125+
"'info::device_type::accelerator' available.",
126+
PI_ERROR_DEVICE_NOT_FOUND);
127+
}
109128
throw sycl::runtime_error("No device of requested type available.",
110129
PI_ERROR_DEVICE_NOT_FOUND);
111130
}
@@ -137,10 +156,20 @@ select_device(const DSelectorInvocableType &DeviceSelectorInvocable,
137156
/// 2. CPU
138157
/// 3. Host
139158
/// 4. Accelerator
159+
160+
static void traceDeviceSelector(const std::string &DeviceType) {
161+
bool ShouldTrace = false;
162+
ShouldTrace = detail::pi::trace(detail::pi::TraceLevel::PI_TRACE_BASIC);
163+
if (ShouldTrace) {
164+
std::cout << "SYCL_PI_TRACE[all]: Requested device_type: " << DeviceType << std::endl;
165+
}
166+
}
167+
140168
__SYCL_EXPORT int default_selector_v(const device &dev) {
141169
// The default selector doesn't reject any devices.
142170
int Score = 0;
143171

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

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

197+
traceDeviceSelector("info::device_type::gpu");
168198
if (dev.is_gpu()) {
169199
Score = 1000;
170200
Score += detail::getDevicePreference(dev);
@@ -175,6 +205,7 @@ __SYCL_EXPORT int gpu_selector_v(const device &dev) {
175205
__SYCL_EXPORT int cpu_selector_v(const device &dev) {
176206
int Score = detail::REJECT_DEVICE_SCORE;
177207

208+
traceDeviceSelector("info::device_type::cpu");
178209
if (dev.is_cpu()) {
179210
Score = 1000;
180211
Score += detail::getDevicePreference(dev);
@@ -185,6 +216,7 @@ __SYCL_EXPORT int cpu_selector_v(const device &dev) {
185216
__SYCL_EXPORT int accelerator_selector_v(const device &dev) {
186217
int Score = detail::REJECT_DEVICE_SCORE;
187218

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
2+
// RUN: env SYCL_DEVICE_FILTER="" %t.out
3+
4+
#include <sycl/sycl.hpp>
5+
using namespace sycl;
6+
7+
int refuse_any_device_f(const device &d) { return -1; }
8+
9+
int main() {
10+
11+
// Check exception message for custom device selector
12+
try {
13+
queue custom_queue(refuse_any_device_f);
14+
} catch (exception &E) {
15+
assert(std::string(E.what()).find("info::device_type::") ==
16+
std::string::npos &&
17+
"Incorrect device type in exception message for custom selector.");
18+
}
19+
20+
// Check exception message for pre-defined devices
21+
try {
22+
queue gpu_queue(gpu_selector_v);
23+
} catch (exception &E) {
24+
assert(std::string(E.what()).find("info::device_type::gpu") !=
25+
std::string::npos &&
26+
"Incorrect device type in exception message for GPU device.");
27+
}
28+
try {
29+
queue cpu_queue(cpu_selector_v);
30+
} catch (exception &E) {
31+
assert(std::string(E.what()).find("info::device_type::cpu") !=
32+
std::string::npos &&
33+
"Incorrect device type in exception message for CPU device.");
34+
}
35+
try {
36+
queue acc_queue(accelerator_selector_v);
37+
} catch (exception &E) {
38+
assert(
39+
std::string(E.what()).find("info::device_type::accelerator") !=
40+
std::string::npos &&
41+
"Incorrect device type in exception message for Accelerator device.");
42+
}
43+
44+
return 0;
45+
}

0 commit comments

Comments
 (0)