diff --git a/sycl/source/detail/force_device.cpp b/sycl/source/detail/force_device.cpp index e23c53704405d..f261f24480052 100644 --- a/sycl/source/detail/force_device.cpp +++ b/sycl/source/detail/force_device.cpp @@ -9,6 +9,8 @@ #include #include #include + +#include #include __SYCL_INLINE_NAMESPACE(cl) { @@ -21,18 +23,23 @@ bool match_types(const info::device_type &l, const info::device_type &r) { info::device_type get_forced_type() { if (const char *val = std::getenv("SYCL_DEVICE_TYPE")) { - if (string_class(val) == "CPU") { + std::string type(val); + std::transform(type.begin(), type.end(), type.begin(), ::tolower); + + if (type == "cpu") { return info::device_type::cpu; } - if (string_class(val) == "GPU") { + if (type == "gpu") { return info::device_type::gpu; } - if (string_class(val) == "ACC") { + if (type == "acc") { return info::device_type::accelerator; } - if (string_class(val) == "HOST") { + if (type == "host") { return info::device_type::host; } + throw cl::sycl::runtime_error("SYCL_DEVICE_TYPE is not recognized. Must " + "be GPU, CPU, ACC or HOST."); } return info::device_type::all; } diff --git a/sycl/test/basic_tests/diagnostics/device-check.cpp b/sycl/test/basic_tests/diagnostics/device-check.cpp new file mode 100644 index 0000000000000..1eee76668a5cb --- /dev/null +++ b/sycl/test/basic_tests/diagnostics/device-check.cpp @@ -0,0 +1,50 @@ +// RUN: %clangxx -fsycl %s -o %t.out +// RUN: env SYCL_DEVICE_TYPE=cpu %t.out +// RUN: env SYCL_DEVICE_TYPE=gpu %t.out +// RUN: env SYCL_DEVICE_TYPE=acc %t.out +// RUN: env SYCL_DEVICE_TYPE=host %t.out +// RUN: env SYCL_DEVICE_TYPE=CPU %t.out +// RUN: env SYCL_DEVICE_TYPE=GPU %t.out +// RUN: env SYCL_DEVICE_TYPE=ACC %t.out +// RUN: env SYCL_DEVICE_TYPE=HOST %t.out +// RUN: env SYCL_DEVICE_TYPE=Cpu %t.out +// RUN: env SYCL_DEVICE_TYPE=Gpu %t.out +// RUN: env SYCL_DEVICE_TYPE=Acc %t.out +// RUN: env SYCL_DEVICE_TYPE=Host %t.out +// RUN: env SYCL_DEVICE_TYPE=XPU %t.out + +//==------------------- device-check.cpp --------------------------==// +// This is a diagnostic test which ensures that +// device types are case-insensitive. +// It also checks for SYCL_DEVICE being set incorrectly. +//==---------------------------------------------------------------==// + +#include +#include + +using namespace cl::sycl; + + +int main() { + try { + queue q = queue(); + auto device = q.get_device(); + auto deviceName = device.get_info(); + std::cout << " Device Name: " << deviceName << std::endl; + } + + catch (runtime_error &E) { + if (std::string(E.what()).find( + "SYCL_DEVICE_TYPE is not recognized. Must be GPU, CPU, ACC or HOST.") == + std::string::npos) { + std::cout << "Test failed: received error is incorrect." << std::endl; + return 1; + } else { + std::cout << "Test passed: caught the expected error." << std::endl; + return 0; + } + } + + std::cout << "Test passed: results are correct." << std::endl; + return 0; +}