Skip to content

Commit 0125496

Browse files
authored
[SYCL] Report an error when SYCL_DEVICE_TYPE is set incorrectly (#1134)
Signed-off-by: Gail Lyons <gail.lyons@intel.com>
1 parent f87291d commit 0125496

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

sycl/source/detail/force_device.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <CL/sycl/detail/force_device.hpp>
1010
#include <CL/sycl/info/info_desc.hpp>
1111
#include <CL/sycl/stl.hpp>
12+
13+
#include <algorithm>
1214
#include <cstdlib>
1315

1416
__SYCL_INLINE_NAMESPACE(cl) {
@@ -21,18 +23,23 @@ bool match_types(const info::device_type &l, const info::device_type &r) {
2123

2224
info::device_type get_forced_type() {
2325
if (const char *val = std::getenv("SYCL_DEVICE_TYPE")) {
24-
if (string_class(val) == "CPU") {
26+
std::string type(val);
27+
std::transform(type.begin(), type.end(), type.begin(), ::tolower);
28+
29+
if (type == "cpu") {
2530
return info::device_type::cpu;
2631
}
27-
if (string_class(val) == "GPU") {
32+
if (type == "gpu") {
2833
return info::device_type::gpu;
2934
}
30-
if (string_class(val) == "ACC") {
35+
if (type == "acc") {
3136
return info::device_type::accelerator;
3237
}
33-
if (string_class(val) == "HOST") {
38+
if (type == "host") {
3439
return info::device_type::host;
3540
}
41+
throw cl::sycl::runtime_error("SYCL_DEVICE_TYPE is not recognized. Must "
42+
"be GPU, CPU, ACC or HOST.");
3643
}
3744
return info::device_type::all;
3845
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// RUN: %clangxx -fsycl %s -o %t.out
2+
// RUN: env SYCL_DEVICE_TYPE=cpu %t.out
3+
// RUN: env SYCL_DEVICE_TYPE=gpu %t.out
4+
// RUN: env SYCL_DEVICE_TYPE=acc %t.out
5+
// RUN: env SYCL_DEVICE_TYPE=host %t.out
6+
// RUN: env SYCL_DEVICE_TYPE=CPU %t.out
7+
// RUN: env SYCL_DEVICE_TYPE=GPU %t.out
8+
// RUN: env SYCL_DEVICE_TYPE=ACC %t.out
9+
// RUN: env SYCL_DEVICE_TYPE=HOST %t.out
10+
// RUN: env SYCL_DEVICE_TYPE=Cpu %t.out
11+
// RUN: env SYCL_DEVICE_TYPE=Gpu %t.out
12+
// RUN: env SYCL_DEVICE_TYPE=Acc %t.out
13+
// RUN: env SYCL_DEVICE_TYPE=Host %t.out
14+
// RUN: env SYCL_DEVICE_TYPE=XPU %t.out
15+
16+
//==------------------- device-check.cpp --------------------------==//
17+
// This is a diagnostic test which ensures that
18+
// device types are case-insensitive.
19+
// It also checks for SYCL_DEVICE being set incorrectly.
20+
//==---------------------------------------------------------------==//
21+
22+
#include <CL/sycl.hpp>
23+
#include <iostream>
24+
25+
using namespace cl::sycl;
26+
27+
28+
int main() {
29+
try {
30+
queue q = queue();
31+
auto device = q.get_device();
32+
auto deviceName = device.get_info<cl::sycl::info::device::name>();
33+
std::cout << " Device Name: " << deviceName << std::endl;
34+
}
35+
36+
catch (runtime_error &E) {
37+
if (std::string(E.what()).find(
38+
"SYCL_DEVICE_TYPE is not recognized. Must be GPU, CPU, ACC or HOST.") ==
39+
std::string::npos) {
40+
std::cout << "Test failed: received error is incorrect." << std::endl;
41+
return 1;
42+
} else {
43+
std::cout << "Test passed: caught the expected error." << std::endl;
44+
return 0;
45+
}
46+
}
47+
48+
std::cout << "Test passed: results are correct." << std::endl;
49+
return 0;
50+
}

0 commit comments

Comments
 (0)