Skip to content

Commit c66a526

Browse files
committed
[SYCL] add device_type to SYCL_PI_TRACE and device_selector exception
1 parent 5998d7c commit c66a526

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-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

0 commit comments

Comments
 (0)