Skip to content

Commit

Permalink
[SYCL] Fix ONEAPI_DEVICE_SELECTOR handling of discard filters. (intel…
Browse files Browse the repository at this point in the history
…#13927)

Currently, ONEAPI_DEVICE_SELECTOR incorrectly parses '!', used for
discard filters.
  • Loading branch information
uditagarwal97 committed May 31, 2024
1 parent 46cfb06 commit 1d24713
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
27 changes: 20 additions & 7 deletions sycl/source/detail/device_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ Parse_ONEAPI_DEVICE_SELECTOR(const std::string &envString) {
std::vector<std::string_view> Pair =
tokenize(Entry, ":", true /* ProhibitEmptyTokens */);

// Error handling. ONEAPI_DEVICE_SELECTOR terms should be in the
// format: <backend>:<devices>.
if (Pair.empty()) {
std::stringstream ss;
ss << "Incomplete selector! Backend and device must be specified.";
Expand All @@ -210,8 +212,24 @@ Parse_ONEAPI_DEVICE_SELECTOR(const std::string &envString) {
ss << "Incomplete selector! Try '" << Pair[0]
<< ":*' if all devices under the backend was original intention.";
throw sycl::exception(sycl::make_error_code(errc::invalid), ss.str());
} else if (Pair.size() == 2) {
backend be = Parse_ODS_Backend(Pair[0], Entry); // Pair[0] is backend.
} else if (Pair.size() > 2) {
std::stringstream ss;
ss << "Error parsing selector string \"" << Entry
<< "\" Too many colons (:)";
throw sycl::exception(sycl::make_error_code(errc::invalid), ss.str());
}

// Parse ONEAPI_DEVICE_SELECTOR terms for Pair.size() == 2.
else {

// Remove `!` from input backend string if it is present.
std::string_view input_be = Pair[0];
if (Pair[0][0] == '!')
input_be = Pair[0].substr(1);

backend be = Parse_ODS_Backend(input_be, Entry);

// For each backend, we can have multiple targets, seperated by ','.
std::vector<std::string_view> Targets = tokenize(Pair[1], ",");
for (auto TargetStr : Targets) {
ods_target DeviceTarget(be);
Expand All @@ -233,11 +251,6 @@ Parse_ONEAPI_DEVICE_SELECTOR(const std::string &envString) {
Parse_ODS_Device(DeviceTarget, TargetStr);
Result.push_back(DeviceTarget);
}
} else if (Pair.size() > 2) {
std::stringstream ss;
ss << "Error parsing selector string \"" << Entry
<< "\" Too many colons (:)";
throw sycl::exception(sycl::make_error_code(errc::invalid), ss.str());
}
}

Expand Down
26 changes: 26 additions & 0 deletions sycl/test-e2e/OneapiDeviceSelector/discard_filters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %{build} -o %t.out

// Test discard filters in ONEAPI_DEVICE_SELECTOR.
// RUN: env ONEAPI_DEVICE_SELECTOR="!*:gpu" %{run-unfiltered-devices} %t.out | FileCheck %s --allow-empty --implicit-check-not="[{{.*}}{{gpu|GPU|Gpu}}{{.*}}]:[{{.*}}]"
// RUN: env ONEAPI_DEVICE_SELECTOR="!*:cpu" %{run-unfiltered-devices} %t.out | FileCheck %s --allow-empty --implicit-check-not="[{{.*}}{{cpu|CPU|cpu}}{{.*}}]:[{{.*}}]"
// RUN: env ONEAPI_DEVICE_SELECTOR="!*:cpu,gpu" %{run-unfiltered-devices} %t.out | FileCheck %s --allow-empty --implicit-check-not="[{{.*}}{{cpu|CPU|cpu|gpu|GPU|Gpu}}{{.*}}]:[{{.*}}]"

// RUN: env ONEAPI_DEVICE_SELECTOR="!opencl:*" %{run-unfiltered-devices} %t.out | FileCheck %s --allow-empty --implicit-check-not="[{{.*}}]:[{{.*}}{{OpenCL|opencl|Opencl}}{{.*}}]"
// RUN: env ONEAPI_DEVICE_SELECTOR="!level_zero:*" %{run-unfiltered-devices} %t.out | FileCheck %s --allow-empty --implicit-check-not="[{{.*}}]:[{{.*}}{{Level-Zero}}{{.*}}]"

// RUN: env ONEAPI_DEVICE_SELECTOR="!level_zero:cpu" %{run-unfiltered-devices} %t.out | FileCheck %s --allow-empty --implicit-check-not="[{{.*}}{{cpu|CPU|Cpu}}{{.*}}]:[{{.*}}{{Level-Zero}}{{.*}}]"

#include <iostream>
#include <sycl/detail/core.hpp>
using namespace sycl;

int main() {
for (auto &d : device::get_devices()) {
// Get device name and backend name.
std::string device_name = d.get_info<info::device::name>();
std::string be_name = d.get_platform().get_info<info::platform::name>();

std::cout << "[" << device_name << "]:[" << be_name << "]" << std::endl;
}
return 0;
}

0 comments on commit 1d24713

Please sign in to comment.