-
Notifications
You must be signed in to change notification settings - Fork 797
[SYCL] Implement new env var SYCL_DEVICE_FILTER #2239
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
Changes from 24 commits
b4a5ffa
0456825
72634d5
6ec2671
5471b63
6b25217
0e9c8d4
6304163
35937b5
8e38292
779d304
f8034c3
da4eab2
b995852
fa1fd6e
230bbd4
1e4bac0
ded32d0
18bb025
0eb0697
1b12fb2
c1475c7
1c0226b
a1f075e
e0d037f
7721ca5
432eb20
ff720c4
b70a425
7a375f4
52c1c88
c46a497
facf402
a996dc0
be44799
800afe4
dd06217
6864017
8494203
156045a
8de7500
39c0725
f7f3718
4399a96
9b83eee
092673f
84a80ef
0a0cf63
dd12cba
f3c6387
4708688
ba2c293
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| //==---------- device_filter.hpp - SYCL device filter descriptor -----------==// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <CL/sycl/backend_types.hpp> | ||
| #include <CL/sycl/detail/defines.hpp> | ||
| #include <CL/sycl/info/info_desc.hpp> | ||
|
|
||
| #include <iostream> | ||
| #include <string> | ||
|
|
||
| __SYCL_INLINE_NAMESPACE(cl) { | ||
| namespace sycl { | ||
| namespace detail { | ||
|
|
||
| struct device_filter { | ||
| backend Backend = backend::host; | ||
bso-intel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| info::device_type DeviceType = info::device_type::all; | ||
| int DeviceNum = 0; | ||
| bool HasBackend = false; | ||
| bool HasDeviceType = false; | ||
| bool HasDeviceNum = false; | ||
vladimirlaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| int MatchesSeen = 0; | ||
|
|
||
| device_filter(){}; | ||
| device_filter(std::string &FilterString); | ||
bso-intel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| friend std::ostream &operator<<(std::ostream &Out, | ||
| const device_filter &Filter); | ||
| }; | ||
|
|
||
| class device_filter_list { | ||
| std::vector<device_filter> FilterList; | ||
|
|
||
| public: | ||
| device_filter_list() {} | ||
| device_filter_list(std::string &FilterString); | ||
bso-intel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| device_filter_list(device_filter &Filter); | ||
| std::vector<device_filter> &get() { return FilterList; } | ||
| friend std::ostream &operator<<(std::ostream &Out, | ||
| const device_filter_list &List); | ||
| }; | ||
|
|
||
| inline std::ostream &operator<<(std::ostream &Out, | ||
| const device_filter &Filter) { | ||
| switch (Filter.Backend) { | ||
| case backend::host: | ||
| Out << std::string("host"); | ||
v-klochkov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| break; | ||
| case backend::opencl: | ||
| Out << std::string("opencl"); | ||
| break; | ||
| case backend::level_zero: | ||
| Out << std::string("level-zero"); | ||
| break; | ||
| case backend::cuda: | ||
| Out << std::string("cuda"); | ||
| break; | ||
| case backend::all: | ||
| Out << std::string("*"); | ||
| } | ||
| Out << std::string(":"); | ||
| if (Filter.DeviceType == info::device_type::host) { | ||
| Out << std::string("host"); | ||
| } else if (Filter.DeviceType == info::device_type::cpu) { | ||
| Out << std::string("cpu"); | ||
| } else if (Filter.DeviceType == info::device_type::gpu) { | ||
| Out << std::string("gpu"); | ||
| } else if (Filter.DeviceType == info::device_type::accelerator) { | ||
| Out << std::string("acceclerator"); | ||
v-klochkov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else if (Filter.DeviceType == info::device_type::all) { | ||
| Out << std::string("*"); | ||
| } | ||
| if (Filter.HasDeviceNum) { | ||
| Out << std::string(":") << Filter.DeviceNum; | ||
| } | ||
| return Out; | ||
| } | ||
|
|
||
| inline std::ostream &operator<<(std::ostream &Out, | ||
| const device_filter_list &List) { | ||
| for (const device_filter &Filter : List.FilterList) { | ||
| Out << Filter; | ||
| Out << ","; | ||
| } | ||
| return Out; | ||
| } | ||
|
|
||
| } // namespace detail | ||
| } // namespace sycl | ||
| } // __SYCL_INLINE_NAMESPACE(cl) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| //==------------------- device_filter.cpp ----------------------------------==// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include <CL/sycl/detail/device_filter.hpp> | ||
| #include <CL/sycl/info/info_desc.hpp> | ||
| #include <detail/config.hpp> | ||
| #include <detail/device_impl.hpp> | ||
|
|
||
| #include <cstring> | ||
|
|
||
| __SYCL_INLINE_NAMESPACE(cl) { | ||
| namespace sycl { | ||
| namespace detail { | ||
|
|
||
| device_filter::device_filter(std::string &FilterString) { | ||
| const std::array<std::pair<std::string, info::device_type>, 5> | ||
| SyclDeviceTypeMap = {{{"host", info::device_type::host}, | ||
| {"cpu", info::device_type::cpu}, | ||
| {"gpu", info::device_type::gpu}, | ||
| {"acc", info::device_type::accelerator}, | ||
| {"*", info::device_type::all}}}; | ||
| const std::array<std::pair<std::string, backend>, 4> SyclBeMap = { | ||
| {{"opencl", backend::opencl}, | ||
| {"level_zero", backend::level_zero}, | ||
| {"cuda", backend::cuda}, | ||
| {"*", backend::all}}}; | ||
|
|
||
| // handle the optional 1st entry, backend | ||
| size_t Cursor = 0; | ||
| size_t ColonPos = FilterString.find(":", Cursor); | ||
| auto It = std::find_if( | ||
v-klochkov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| std::begin(SyclBeMap), std::end(SyclBeMap), | ||
| [=, &Cursor](const std::pair<std::string, backend> &Element) { | ||
| size_t Found = FilterString.find(Element.first, Cursor); | ||
| if (Found != std::string::npos) { | ||
| Cursor = Found; | ||
| return true; | ||
| } | ||
| return false; | ||
| }); | ||
| if (It == SyclBeMap.end()) { | ||
| Backend = backend::all; | ||
| } else { | ||
| Backend = It->second; | ||
| if (ColonPos != std::string::npos) { | ||
| Cursor = ColonPos + 1; | ||
| } else { | ||
| Cursor = Cursor + It->first.size(); | ||
| } | ||
| } | ||
|
|
||
| // handle the optional 2nd entry, device type | ||
| auto Iter = std::find_if( | ||
| std::begin(SyclDeviceTypeMap), std::end(SyclDeviceTypeMap), | ||
| [=, &Cursor](const std::pair<std::string, info::device_type> &Element) { | ||
| size_t Found = FilterString.find(Element.first, Cursor); | ||
| if (Found != std::string::npos) { | ||
| Cursor = Found; | ||
| return true; | ||
| } | ||
| return false; | ||
| }); | ||
| if (Iter == SyclDeviceTypeMap.end()) { | ||
| DeviceType = info::device_type::all; | ||
| } else { | ||
| DeviceType = Iter->second; | ||
| ColonPos = FilterString.find(":", Cursor); | ||
| if (ColonPos != std::string::npos) { | ||
| Cursor = ColonPos + 1; | ||
| } else { | ||
| Cursor = Cursor + Iter->first.size(); | ||
| } | ||
| } | ||
|
|
||
| // handle the optional 3rd entry, device number | ||
| if (Cursor < FilterString.size()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please comment the code from time to time.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Done. |
||
| try { | ||
| DeviceNum = stoi(FilterString.substr(ColonPos + 1)); | ||
| HasDeviceNum = true; | ||
| } catch (...) { | ||
| char message[100]; | ||
v-klochkov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
v-klochkov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| strcpy(message, "Invalid device filter: "); | ||
| std::strcat(message, FilterString.c_str()); | ||
| std::strcat(message, | ||
| "\nPossible backend values are {opencl,level_zero,cuda,*}."); | ||
| std::strcat(message, "\nPossible device types are {host,cpu,gpu,acc,*}."); | ||
| std::strcat(message, | ||
| "\nDevice number should be an non-negative integer.\n"); | ||
| throw cl::sycl::invalid_parameter_error(message, PI_INVALID_VALUE); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| device_filter_list::device_filter_list(std::string &FilterString) { | ||
| std::transform(FilterString.begin(), FilterString.end(), FilterString.begin(), | ||
| ::tolower); | ||
| size_t Pos = 0; | ||
| while (Pos < FilterString.size()) { | ||
| size_t CommaPos = FilterString.find(",", Pos); | ||
| if (CommaPos == std::string::npos) { | ||
v-klochkov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| CommaPos = FilterString.size(); | ||
| } | ||
| std::string SubString = FilterString.substr(Pos, CommaPos - Pos); | ||
| FilterList.push_back(device_filter(SubString)); | ||
| Pos = CommaPos + 1; | ||
| } | ||
| } | ||
|
|
||
| } // namespace detail | ||
| } // namespace sycl | ||
| } // __SYCL_INLINE_NAMESPACE(cl) | ||
Uh oh!
There was an error while loading. Please reload this page.