Skip to content

[SYCL] Report an error when SYCL_DEVICE_TYPE is set incorrectly. #1134

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 1 commit into from
Feb 26, 2020
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
15 changes: 11 additions & 4 deletions sycl/source/detail/force_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <CL/sycl/detail/force_device.hpp>
#include <CL/sycl/info/info_desc.hpp>
#include <CL/sycl/stl.hpp>

#include <algorithm>
#include <cstdlib>

__SYCL_INLINE_NAMESPACE(cl) {
Expand All @@ -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;
}
Expand Down
50 changes: 50 additions & 0 deletions sycl/test/basic_tests/diagnostics/device-check.cpp
Original file line number Diff line number Diff line change
@@ -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 <CL/sycl.hpp>
#include <iostream>

using namespace cl::sycl;


int main() {
try {
queue q = queue();
auto device = q.get_device();
auto deviceName = device.get_info<cl::sycl::info::device::name>();
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;
}