Skip to content

Commit d3b9cda

Browse files
committed
tokenize filter
Signed-off-by: Byoungro So <[email protected]>
1 parent e8a26da commit d3b9cda

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

sycl/include/CL/sycl/detail/device_filter.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ inline std::ostream &operator<<(std::ostream &Out,
8282
return Out;
8383
}
8484

85+
std::vector<std::string> tokenize(const std::string &Filter,
86+
const std::string &Delim);
87+
8588
} // namespace detail
8689
} // namespace sycl
8790
} // __SYCL_INLINE_NAMESPACE(cl)

sycl/source/detail/device_filter.cpp

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,44 @@ __SYCL_INLINE_NAMESPACE(cl) {
1717
namespace sycl {
1818
namespace detail {
1919

20+
std::vector<std::string> tokenize(const std::string &Filter,
21+
const std::string &Delim) {
22+
std::vector<std::string> Tokens;
23+
size_t Pos = 0;
24+
std::string Input = Filter;
25+
std::string Tok;
26+
27+
while ((Pos = Input.find(Delim)) != std::string::npos) {
28+
Tok = Input.substr(0, Pos);
29+
Input.erase(0, Pos + Delim.length());
30+
31+
if (!Tok.empty()) {
32+
Tokens.push_back(std::move(Tok));
33+
}
34+
}
35+
36+
// Add remainder
37+
if (!Input.empty())
38+
Tokens.push_back(std::move(Input));
39+
40+
return Tokens;
41+
}
42+
2043
device_filter::device_filter(const std::string &FilterString) {
21-
size_t Cursor = 0;
22-
size_t ColonPos = 0;
44+
std::string SubString;
45+
2346
auto findElement = [&](auto Element) {
24-
size_t Found = FilterString.find(Element.first, Cursor);
47+
size_t Found = SubString.find(Element.first);
2548
if (Found == std::string::npos)
2649
return false;
27-
Cursor = Found;
2850
return true;
2951
};
3052

3153
// Handle the optional 1st field of the filter, backend
3254
// Check if the first entry matches with a known backend type
55+
std::vector<std::string> Tokens = tokenize(FilterString, ":");
56+
size_t i = 0;
57+
SubString = Tokens[i];
3358
auto It =
3459
std::find_if(std::begin(SyclBeMap), std::end(SyclBeMap), findElement);
3560
// If no match is found, set the backend type backend::all
@@ -38,15 +63,14 @@ device_filter::device_filter(const std::string &FilterString) {
3863
Backend = backend::all;
3964
else {
4065
Backend = It->second;
41-
ColonPos = FilterString.find(":", Cursor);
42-
if (ColonPos != std::string::npos)
43-
Cursor = ColonPos + 1;
44-
else
45-
Cursor = Cursor + It->first.size();
66+
i++;
67+
if (i < Tokens.size())
68+
SubString = Tokens[i];
4669
}
70+
4771
// Handle the optional 2nd field of the filter - device type.
4872
// Check if the 2nd entry matches with any known device type.
49-
if (Cursor >= FilterString.size()) {
73+
if (i >= Tokens.size()) {
5074
DeviceType = info::device_type::all;
5175
} else {
5276
auto Iter = std::find_if(std::begin(SyclDeviceTypeMap),
@@ -57,20 +81,18 @@ device_filter::device_filter(const std::string &FilterString) {
5781
DeviceType = info::device_type::all;
5882
else {
5983
DeviceType = Iter->second;
60-
ColonPos = FilterString.find(":", Cursor);
61-
if (ColonPos != std::string::npos)
62-
Cursor = ColonPos + 1;
63-
else
64-
Cursor = Cursor + Iter->first.size();
84+
i++;
85+
if (i < Tokens.size())
86+
SubString = Tokens[i];
6587
}
6688
}
6789

6890
// Handle the optional 3rd field of the filter, device number
6991
// Try to convert the remaining string to an integer.
7092
// If succeessful, the converted integer is the desired device num.
71-
if (Cursor < FilterString.size()) {
93+
if (i < Tokens.size()) {
7294
try {
73-
DeviceNum = stoi(FilterString.substr(Cursor));
95+
DeviceNum = stoi(SubString);
7496
HasDeviceNum = true;
7597
} catch (...) {
7698
std::string Message =

sycl/source/detail/filter_selector_impl.cpp

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,12 @@ namespace sycl {
2424
namespace ONEAPI {
2525
namespace detail {
2626

27-
std::vector<std::string> tokenize(const std::string &Filter,
28-
const std::string &Delim) {
29-
std::vector<std::string> Tokens;
30-
size_t Pos = 0;
31-
std::string Input = Filter;
32-
std::string Tok;
33-
34-
while ((Pos = Input.find(Delim)) != std::string::npos) {
35-
Tok = Input.substr(0, Pos);
36-
Input.erase(0, Pos + Delim.length());
37-
38-
if (!Tok.empty()) {
39-
Tokens.push_back(std::move(Tok));
40-
}
41-
}
42-
43-
// Add remainder
44-
if (!Input.empty())
45-
Tokens.push_back(std::move(Input));
46-
47-
return Tokens;
48-
}
49-
5027
filter create_filter(const std::string &Input) {
5128
filter Result;
5229
constexpr auto Error = "Invalid filter string! Valid strings conform to "
5330
"BE:DeviceType:DeviceNum, where any are optional";
5431

55-
std::vector<std::string> Tokens = tokenize(Input, ":");
32+
std::vector<std::string> Tokens = sycl::detail::tokenize(Input, ":");
5633
std::regex IntegerExpr("[[:digit:]]+");
5734

5835
// There should only be up to 3 tokens.
@@ -106,7 +83,7 @@ filter create_filter(const std::string &Input) {
10683

10784
filter_selector_impl::filter_selector_impl(const std::string &Input)
10885
: mFilters(), mRanker(), mNumDevicesSeen(0), mMatchFound(false) {
109-
std::vector<std::string> Filters = detail::tokenize(Input, ",");
86+
std::vector<std::string> Filters = sycl::detail::tokenize(Input, ",");
11087
mNumTotalDevices = device::get_devices().size();
11188

11289
for (const std::string &Filter : Filters) {

0 commit comments

Comments
 (0)