DPC++ Runtime
Runtime libraries for oneAPI DPC++
device_filter.hpp
Go to the documentation of this file.
1 //==---------- device_filter.hpp - SYCL device filter descriptor -----------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #include <sycl/backend_types.hpp>
12 #include <sycl/detail/defines.hpp>
13 #include <sycl/info/info_desc.hpp>
14 
15 #include <optional>
16 #include <ostream>
17 #include <string>
18 
19 namespace sycl {
20 inline namespace _V1 {
21 namespace detail {
22 
23 // ---------------------------------------
24 // ONEAPI_DEVICE_SELECTOR support
25 
26 template <typename T>
27 std::ostream &operator<<(std::ostream &os, std::optional<T> const &opt) {
28  return opt ? os << opt.value() : os << "not set ";
29 }
30 
31 // the ONEAPI_DEVICE_SELECTOR string gets broken down into these targets
32 // will will match devices. If the target is negative, such as !opencl:*
33 // then matching devices will not be made available to the user.
34 struct ods_target {
35 public:
36  std::optional<backend> Backend;
37  std::optional<info::device_type> DeviceType;
38 
39  bool HasDeviceWildCard = false;
40  std::optional<int> DeviceNum;
41 
42  bool HasSubDeviceWildCard = false;
43  std::optional<unsigned> SubDeviceNum;
44 
45  bool HasSubSubDeviceWildCard = false; // two levels of sub-devices.
46  std::optional<unsigned> SubSubDeviceNum;
47 
48  bool IsNegativeTarget = false; // used to represent negative filters.
49 
50  ods_target(backend be) { Backend = be; };
52  friend std::ostream &operator<<(std::ostream &Out, const ods_target &Target);
53 };
54 
56  std::vector<ods_target> TargetList;
57 
58 public:
60  ods_target_list(const std::string &FilterString);
61  std::vector<ods_target> &get() { return TargetList; }
62  bool containsHost();
63  bool backendCompatible(backend Backend);
64 };
65 
66 std::ostream &operator<<(std::ostream &Out, const ods_target &Target);
67 std::vector<ods_target> Parse_ONEAPI_DEVICE_SELECTOR(const std::string &envStr);
68 
69 // ---------------------------------------
70 // SYCL_DEVICE_FILTER support
71 
72 struct device_filter {
73  std::optional<backend> Backend;
74  std::optional<info::device_type> DeviceType;
75  std::optional<int> DeviceNum;
76  int MatchesSeen = 0;
77 
79  device_filter(const std::string &FilterString);
80  friend std::ostream &operator<<(std::ostream &Out,
81  const device_filter &Filter);
82 };
83 
85  std::vector<device_filter> FilterList;
86 
87 public:
89  device_filter_list(const std::string &FilterString);
91  void addFilter(device_filter &Filter);
92  std::vector<device_filter> &get() { return FilterList; }
93  bool backendCompatible(backend Backend);
94  bool deviceTypeCompatible(info::device_type DeviceType);
95  bool deviceNumberCompatible(int DeviceNum);
96  friend std::ostream &operator<<(std::ostream &Out,
97  const device_filter_list &List);
98 };
99 
100 inline std::ostream &operator<<(std::ostream &Out,
101  const device_filter &Filter) {
102  Out << Filter.Backend << ":";
103  if (Filter.DeviceType == info::device_type::host) {
104  Out << "host";
105  } else if (Filter.DeviceType == info::device_type::cpu) {
106  Out << "cpu";
107  } else if (Filter.DeviceType == info::device_type::gpu) {
108  Out << "gpu";
109  } else if (Filter.DeviceType == info::device_type::accelerator) {
110  Out << "accelerator";
111  } else if (Filter.DeviceType == info::device_type::all) {
112  Out << "*";
113  } else {
114  Out << "unknown";
115  }
116  if (Filter.DeviceNum) {
117  Out << ":" << Filter.DeviceNum.value();
118  }
119  return Out;
120 }
121 
122 inline std::ostream &operator<<(std::ostream &Out,
123  const device_filter_list &List) {
124  for (const device_filter &Filter : List.FilterList) {
125  Out << Filter;
126  Out << ",";
127  }
128  return Out;
129 }
130 
131 } // namespace detail
132 } // namespace _V1
133 } // namespace sycl
sycl::_V1::detail::ods_target::SubDeviceNum
std::optional< unsigned > SubDeviceNum
Definition: device_filter.hpp:43
sycl::_V1::backend
backend
Definition: backend_types.hpp:18
sycl::_V1::detail::device_filter_list::deviceNumberCompatible
bool deviceNumberCompatible(int DeviceNum)
Definition: device_filter.cpp:402
sycl::_V1::detail::ods_target::ods_target
ods_target()
Definition: device_filter.hpp:51
sycl::_V1::detail::ods_target::HasDeviceWildCard
bool HasDeviceWildCard
Definition: device_filter.hpp:39
sycl::_V1::detail::ods_target_list::ods_target_list
ods_target_list()
Definition: device_filter.hpp:59
sycl::_V1::detail::device_filter::Backend
std::optional< backend > Backend
Definition: device_filter.hpp:73
sycl::_V1::detail::Parse_ONEAPI_DEVICE_SELECTOR
std::vector< ods_target > Parse_ONEAPI_DEVICE_SELECTOR(const std::string &envStr)
Definition: device_filter.cpp:169
sycl::_V1::detail::device_filter::DeviceType
std::optional< info::device_type > DeviceType
Definition: device_filter.hpp:74
sycl::_V1::detail::ods_target::SubSubDeviceNum
std::optional< unsigned > SubSubDeviceNum
Definition: device_filter.hpp:46
sycl::_V1::detail::device_filter::operator<<
friend std::ostream & operator<<(std::ostream &Out, const device_filter &Filter)
Definition: device_filter.hpp:100
sycl::_V1::detail::device_filter_list::deviceTypeCompatible
bool deviceTypeCompatible(info::device_type DeviceType)
Definition: device_filter.cpp:392
detail
---— Error handling, matching OpenCL plugin semantics.
Definition: common.hpp:44
sycl::_V1::detail::ods_target
Definition: device_filter.hpp:34
sycl::_V1::detail::ods_target_list::backendCompatible
bool backendCompatible(backend Backend)
Definition: device_filter.cpp:273
sycl
Definition: access.hpp:18
sycl::_V1::detail::ods_target::ods_target
ods_target(backend be)
Definition: device_filter.hpp:50
sycl::_V1::detail::device_filter_list::get
std::vector< device_filter > & get()
Definition: device_filter.hpp:92
sycl::_V1::info::device_type::gpu
@ gpu
sycl::_V1::detail::ods_target::IsNegativeTarget
bool IsNegativeTarget
Definition: device_filter.hpp:48
sycl::_V1::detail::device_filter_list::backendCompatible
bool backendCompatible(backend Backend)
Definition: device_filter.cpp:384
sycl::_V1::detail::ods_target_list::containsHost
bool containsHost()
sycl::_V1::detail::ods_target::HasSubSubDeviceWildCard
bool HasSubSubDeviceWildCard
Definition: device_filter.hpp:45
sycl::_V1::detail::operator<<
std::ostream & operator<<(std::ostream &os, std::optional< T > const &opt)
Definition: device_filter.hpp:27
defines.hpp
sycl::_V1::detail::ods_target::HasSubDeviceWildCard
bool HasSubDeviceWildCard
Definition: device_filter.hpp:42
sycl::_V1::detail::device_filter::MatchesSeen
int MatchesSeen
Definition: device_filter.hpp:76
sycl::_V1::detail::ods_target::Backend
std::optional< backend > Backend
Definition: device_filter.hpp:36
sycl::_V1::info::device_type
device_type
Definition: info_desc.hpp:54
sycl::_V1::info::device_type::host
@ host
sycl::_V1::detail::ods_target::DeviceType
std::optional< info::device_type > DeviceType
Definition: device_filter.hpp:37
sycl::_V1::info::device_type::all
@ all
sycl::_V1::info::device_type::cpu
@ cpu
sycl::_V1::detail::device_filter_list::operator<<
friend std::ostream & operator<<(std::ostream &Out, const device_filter_list &List)
Definition: device_filter.hpp:122
sycl::_V1::detail::ods_target::operator<<
friend std::ostream & operator<<(std::ostream &Out, const ods_target &Target)
Definition: device_filter.cpp:241
sycl::_V1::detail::device_filter
Definition: device_filter.hpp:72
sycl::_V1::detail::device_filter_list::device_filter_list
device_filter_list()
Definition: device_filter.hpp:88
sycl::_V1::detail::device_filter::DeviceNum
std::optional< int > DeviceNum
Definition: device_filter.hpp:75
backend_types.hpp
sycl::_V1::detail::device_filter_list
Definition: device_filter.hpp:84
sycl::_V1::detail::ods_target_list
Definition: device_filter.hpp:55
sycl::_V1::info::device_type::accelerator
@ accelerator
sycl::_V1::detail::ods_target_list::get
std::vector< ods_target > & get()
Definition: device_filter.hpp:61
info_desc.hpp
sycl::_V1::detail::ods_target::DeviceNum
std::optional< int > DeviceNum
Definition: device_filter.hpp:40
sycl::_V1::detail::device_filter::device_filter
device_filter()
Definition: device_filter.hpp:78
sycl::_V1::detail::device_filter_list::addFilter
void addFilter(device_filter &Filter)
Definition: device_filter.cpp:377