23 inline namespace _V1 {
24 namespace ext::oneapi::detail {
26 std::vector<std::string>
tokenize(
const std::string &Filter,
27 const std::string &Delim) {
28 std::vector<std::string> Tokens;
30 std::string Input = Filter;
33 while ((Pos = Input.find(Delim)) != std::string::npos) {
34 Tok = Input.substr(0, Pos);
35 Input.erase(0, Pos + Delim.length());
38 Tokens.push_back(std::move(Tok));
44 Tokens.push_back(std::move(Input));
51 constexpr
auto Error =
"Invalid filter string! Valid strings conform to "
52 "BE:DeviceType:DeviceNum, where any are optional";
54 std::vector<std::string> Tokens =
tokenize(Input,
":");
55 std::regex IntegerExpr(
"[[:digit:]]+");
59 if (Tokens.size() > 3)
60 throw sycl::runtime_error(Error, PI_ERROR_INVALID_VALUE);
62 for (
const std::string &Token : Tokens) {
63 if (Token ==
"cpu" && !Result.DeviceType) {
64 Result.DeviceType = sycl::info::device_type::cpu;
65 }
else if (Token ==
"gpu" && !Result.DeviceType) {
66 Result.DeviceType = sycl::info::device_type::gpu;
67 }
else if (Token ==
"accelerator" && !Result.DeviceType) {
68 Result.DeviceType = sycl::info::device_type::accelerator;
69 }
else if (Token ==
"opencl" && !Result.Backend) {
71 }
else if (Token ==
"level_zero" && !Result.Backend) {
73 }
else if (Token ==
"cuda" && !Result.Backend) {
75 }
else if (Token ==
"hip" && !Result.Backend) {
77 }
else if (Token ==
"esimd_emulator" && !Result.Backend) {
78 Result.Backend = backend::ext_intel_esimd_emulator;
79 }
else if (std::regex_match(Token, IntegerExpr) && !Result.DeviceNum) {
81 Result.DeviceNum = std::stoi(Token);
82 }
catch (std::logic_error &) {
83 throw sycl::runtime_error(Error, PI_ERROR_INVALID_VALUE);
86 throw sycl::runtime_error(Error, PI_ERROR_INVALID_VALUE);
94 : mFilters(), mNumDevicesSeen(0), mMatchFound(false) {
98 for (
const std::string &Filter : Filters) {
100 mFilters.push_back(std::move(F));
106 "filter_selector_impl should not be used with host.");
108 int Score = REJECT_DEVICE_SCORE;
110 for (
auto &Filter : mFilters) {
111 bool BackendOK =
true;
112 bool DeviceTypeOK =
true;
113 bool DeviceNumOK =
true;
115 if (Filter.Backend) {
121 BackendOK = (BE == Filter.Backend.value());
123 if (Filter.DeviceType) {
130 DeviceTypeOK = (DT == Filter.DeviceType);
132 if (Filter.DeviceNum) {
134 if (BackendOK && DeviceTypeOK) {
136 DeviceNumOK = (Filter.MatchesSeen == Filter.DeviceNum.value());
138 Filter.MatchesSeen++;
141 if (BackendOK && DeviceTypeOK && DeviceNumOK) {
149 if ((mNumDevicesSeen == mNumTotalDevices) && !mMatchFound) {
150 throw sycl::runtime_error(
151 "Could not find a device that matches the specified filter(s)!",
152 PI_ERROR_DEVICE_NOT_FOUND);
161 for (
auto &Filter : mFilters) {
162 Filter.MatchesSeen = 0;
171 using namespace ext::oneapi;