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>
14 #include <sycl/info/info_desc.hpp>
15 
16 #include <optional>
17 #include <string>
18 
19 namespace sycl {
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 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
133 } // namespace sycl
std::vector< device_filter > & get()
std::vector< ods_target > & get()
#define __SYCL_INLINE_VER_NAMESPACE(X)
std::vector< ods_target > Parse_ONEAPI_DEVICE_SELECTOR(const std::string &envStr)
std::ostream & operator<<(std::ostream &Out, const device_filter_list &List)
detail::enable_if_t< detail::is_sigeninteger< T >::value, detail::anyall_ret_t > all(T x) __NOEXC
Definition: builtins.hpp:1243
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
std::optional< backend > Backend
std::optional< int > DeviceNum
std::optional< info::device_type > DeviceType
std::optional< backend > Backend
std::optional< int > DeviceNum
std::optional< unsigned > SubDeviceNum
std::optional< info::device_type > DeviceType
std::optional< unsigned > SubSubDeviceNum