DPC++ Runtime
Runtime libraries for oneAPI DPC++
device_selector.hpp
Go to the documentation of this file.
1 //==------ device_selector.hpp - SYCL device selector ---------*- C++ --*---==//
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/detail/defines_elementary.hpp> // for __SYCL2020_DEPRECATED
12 #include <sycl/detail/export.hpp> // for __SYCL_EXPORT
13 
14 #include <functional> // for function
15 #include <type_traits> // for enable_if_t
16 #include <vector> // for vector
17 
18 // 4.6.1 Device selection class
19 
20 namespace sycl {
21 inline namespace _V1 {
22 
23 // Forward declarations
24 class device;
25 class context;
26 enum class aspect;
27 
28 namespace ext::oneapi {
29 class filter_selector;
30 } // namespace ext::oneapi
31 
38 class __SYCL_EXPORT __SYCL2020_DEPRECATED(
39  "Use SYCL 2020 callable device selectors instead.") device_selector {
40 
41 public:
42  virtual ~device_selector() = default;
43 
44  virtual device select_device() const;
45 
46  virtual int operator()(const device &device) const = 0;
47 };
48 
54 class __SYCL_EXPORT __SYCL2020_DEPRECATED(
55  "Use the callable sycl::default_selector_v instead.") default_selector
56  : public device_selector {
57 public:
58  int operator()(const device &dev) const override;
59 };
60 
66 class __SYCL_EXPORT __SYCL2020_DEPRECATED(
67  "Use the callable sycl::gpu_selector_v instead.") gpu_selector
68  : public device_selector {
69 public:
70  int operator()(const device &dev) const override;
71 };
72 
78 class __SYCL_EXPORT __SYCL2020_DEPRECATED(
79  "Use the callable sycl::cpu_selector_v instead.") cpu_selector
80  : public device_selector {
81 public:
82  int operator()(const device &dev) const override;
83 };
84 
90 class __SYCL_EXPORT
93 public:
94  int operator()(const device &dev) const override;
95 };
96 
102 class __SYCL_EXPORT
103 __SYCL2020_DEPRECATED("Host device is no longer supported.") host_selector
104  : public device_selector {
105 public:
106  int operator()(const device &dev) const override;
107 };
108 
109 // -------------- SYCL 2020
110 
111 // SYCL 2020 standalone selectors
112 __SYCL_EXPORT int default_selector_v(const device &dev);
113 __SYCL_EXPORT int gpu_selector_v(const device &dev);
114 __SYCL_EXPORT int cpu_selector_v(const device &dev);
115 __SYCL_EXPORT int accelerator_selector_v(const device &dev);
116 
117 namespace detail {
118 // SYCL 2020 section 4.6.1.1 defines a negative score to reject a device from
119 // selection
120 static constexpr int REJECT_DEVICE_SCORE = -1;
121 
122 using DSelectorInvocableType = std::function<int(const sycl::device &)>;
123 
124 template <typename LastT>
125 void fill_aspect_vector(std::vector<aspect> &V, LastT L) {
126  V.emplace_back(L);
127 }
128 
129 template <typename FirstT, typename... OtherTs>
130 void fill_aspect_vector(std::vector<aspect> &V, FirstT F, OtherTs... O) {
131  V.emplace_back(F);
132  fill_aspect_vector(V, O...);
133 }
134 
135 // Enable if DeviceSelector callable has matching signature, but
136 // exclude if descended from filter_selector which is not purely callable or
137 // if descended from it is descended from SYCL 1.2.1 device_selector.
138 // See [FilterSelector not Callable] in device_selector.cpp
139 template <typename DeviceSelector>
141  std::is_invocable_r_v<int, DeviceSelector &, const device &> &&
142  !std::is_base_of_v<ext::oneapi::filter_selector, DeviceSelector> &&
143  !std::is_base_of_v<device_selector, DeviceSelector>>;
144 
145 __SYCL_EXPORT device
146 select_device(const DSelectorInvocableType &DeviceSelectorInvocable);
147 
148 __SYCL_EXPORT device
149 select_device(const DSelectorInvocableType &DeviceSelectorInvocable,
150  const context &SyclContext);
151 
152 } // namespace detail
153 
154 __SYCL_EXPORT detail::DSelectorInvocableType
155 aspect_selector(const std::vector<aspect> &RequireList,
156  const std::vector<aspect> &DenyList = {});
157 
158 template <typename... AspectListT>
160  std::vector<aspect> RequireList;
161  RequireList.reserve(sizeof...(AspectList));
162 
163  detail::fill_aspect_vector(RequireList, AspectList...);
164 
165  return aspect_selector(RequireList, {});
166 }
167 
168 template <aspect... AspectList>
170  return aspect_selector({AspectList...}, {});
171 }
172 
173 } // namespace _V1
174 } // namespace sycl
The default selector chooses the first available SYCL device.
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:51
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:59
class __SYCL2020_DEPRECATED("Host device is no longer supported.") host_selector int default_selector_v(const device &dev)
Selects SYCL host device.
void fill_aspect_vector(std::vector< aspect > &V, LastT L)
std::function< int(const sycl::device &)> DSelectorInvocableType
static constexpr int REJECT_DEVICE_SCORE
device select_device(const DSelectorInvocableType &DeviceSelectorInvocable)
device select_device(const DSelectorInvocableType &DeviceSelectorInvocable, const context &SyclContext)
std::enable_if_t< std::is_invocable_r_v< int, DeviceSelector &, const device & > &&!std::is_base_of_v< ext::oneapi::filter_selector, DeviceSelector > &&!std::is_base_of_v< device_selector, DeviceSelector > > EnableIfSYCL2020DeviceSelectorInvocable
std::uint8_t instead
Definition: aliases.hpp:93
int accelerator_selector_v(const device &dev)
signed char __SYCL2020_DEPRECATED
Definition: aliases.hpp:94
int cpu_selector_v(const device &dev)
detail::DSelectorInvocableType aspect_selector(const std::vector< aspect > &RequireList, const std::vector< aspect > &DenyList={})
int gpu_selector_v(const device &dev)
Definition: access.hpp:18