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.hpp>
12 #include <sycl/detail/export.hpp>
13 
14 #include <vector>
15 
16 // 4.6.1 Device selection class
17 
18 namespace sycl {
20 
21 // Forward declarations
22 class device;
23 
24 namespace ext::oneapi {
25 class filter_selector;
26 } // namespace ext::oneapi
27 
34 class __SYCL_EXPORT __SYCL2020_DEPRECATED(
35  "Use SYCL 2020 callable device selectors instead.") device_selector {
36 
37 public:
38  virtual ~device_selector() = default;
39 
40  virtual device select_device() const;
41 
42  virtual int operator()(const device &device) const = 0;
43 };
44 
50 class __SYCL_EXPORT __SYCL2020_DEPRECATED(
51  "Use the callable sycl::default_selector_v instead.") default_selector
52  : public device_selector {
53 public:
54  int operator()(const device &dev) const override;
55 };
56 
62 class __SYCL_EXPORT __SYCL2020_DEPRECATED(
63  "Use the callable sycl::gpu_selector_v instead.") gpu_selector
64  : public device_selector {
65 public:
66  int operator()(const device &dev) const override;
67 };
68 
74 class __SYCL_EXPORT __SYCL2020_DEPRECATED(
75  "Use the callable sycl::cpu_selector_v instead.") cpu_selector
76  : public device_selector {
77 public:
78  int operator()(const device &dev) const override;
79 };
80 
86 class __SYCL_EXPORT
89 public:
90  int operator()(const device &dev) const override;
91 };
92 
98 class __SYCL_EXPORT
99 __SYCL2020_DEPRECATED("Host device is no longer supported.") host_selector
100  : public device_selector {
101 public:
102  int operator()(const device &dev) const override;
103 };
104 
105 // -------------- SYCL 2020
106 
107 // SYCL 2020 standalone selectors
108 __SYCL_EXPORT int default_selector_v(const device &dev);
109 __SYCL_EXPORT int gpu_selector_v(const device &dev);
110 __SYCL_EXPORT int cpu_selector_v(const device &dev);
111 __SYCL_EXPORT int accelerator_selector_v(const device &dev);
112 
113 namespace detail {
114 // SYCL 2020 section 4.6.1.1 defines a negative score to reject a device from
115 // selection
116 static constexpr int REJECT_DEVICE_SCORE = -1;
117 
118 using DSelectorInvocableType = std::function<int(const sycl::device &)>;
119 
120 template <typename LastT>
121 void fill_aspect_vector(std::vector<aspect> &V, LastT L) {
122  V.emplace_back(L);
123 }
124 
125 template <typename FirstT, typename... OtherTs>
126 void fill_aspect_vector(std::vector<aspect> &V, FirstT F, OtherTs... O) {
127  V.emplace_back(F);
128  fill_aspect_vector(V, O...);
129 }
130 
131 // Enable if DeviceSelector callable has matching signature, but
132 // exclude if descended from filter_selector which is not purely callable or
133 // if descended from it is descended from SYCL 1.2.1 device_selector.
134 // See [FilterSelector not Callable] in device_selector.cpp
135 template <typename DeviceSelector>
136 using EnableIfSYCL2020DeviceSelectorInvocable = std::enable_if_t<
137  std::is_invocable_r_v<int, DeviceSelector &, const device &> &&
138  !std::is_base_of_v<ext::oneapi::filter_selector, DeviceSelector> &&
139  !std::is_base_of_v<device_selector, DeviceSelector>>;
140 
141 __SYCL_EXPORT device
142 select_device(const DSelectorInvocableType &DeviceSelectorInvocable);
143 
144 __SYCL_EXPORT device
145 select_device(const DSelectorInvocableType &DeviceSelectorInvocable,
146  const context &SyclContext);
147 
148 } // namespace detail
149 
150 __SYCL_EXPORT detail::DSelectorInvocableType
151 aspect_selector(const std::vector<aspect> &RequireList,
152  const std::vector<aspect> &DenyList = {});
153 
154 template <typename... AspectListT>
156  std::vector<aspect> RequireList;
157  RequireList.reserve(sizeof...(AspectList));
158 
159  detail::fill_aspect_vector(RequireList, AspectList...);
160 
161  return aspect_selector(RequireList, {});
162 }
163 
164 template <aspect... AspectList>
166  return aspect_selector({AspectList...}, {});
167 }
168 
169 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
170 } // namespace sycl
sycl::_V1::__SYCL2020_DEPRECATED
signed char __SYCL2020_DEPRECATED
Definition: aliases.hpp:96
sycl::_V1::instead
std::uint8_t instead
Definition: aliases.hpp:95
__SYCL_INLINE_VER_NAMESPACE
#define __SYCL_INLINE_VER_NAMESPACE(X)
Definition: defines_elementary.hpp:11
sycl::_V1::gpu_selector_v
int gpu_selector_v(const device &dev)
Definition: device_selector.cpp:210
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
sycl::_V1::detail::select_device
device select_device(const DSelectorInvocableType &DeviceSelectorInvocable, const context &SyclContext)
Definition: device_selector.cpp:145
sycl::_V1::accelerator_selector
The default selector chooses the first available SYCL device.
Definition: device_selector.hpp:86
sycl::_V1::default_selector_v
class __SYCL2020_DEPRECATED("Host device is no longer supported.") host_selector int default_selector_v(const device &dev)
Selects SYCL host device.
Definition: device_selector.cpp:179
export.hpp
sycl::_V1::accelerator_selector_v
int accelerator_selector_v(const device &dev)
Definition: device_selector.cpp:236
device_selector
defines.hpp
sycl::_V1::device
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:49
sycl::_V1::access::target::device
@ device
sycl::_V1::detail::DSelectorInvocableType
std::function< int(const sycl::device &)> DSelectorInvocableType
Definition: device_selector.hpp:118
sycl::_V1::detail::fill_aspect_vector
void fill_aspect_vector(std::vector< aspect > &V, FirstT F, OtherTs... O)
Definition: device_selector.hpp:126
usm_settings::Host
@ Host
Definition: usm_allocator_config.hpp:18
sycl::_V1::cpu_selector_v
int cpu_selector_v(const device &dev)
Definition: device_selector.cpp:225
sycl::_V1::detail::REJECT_DEVICE_SCORE
static constexpr int REJECT_DEVICE_SCORE
Definition: device_selector.hpp:116
sycl::_V1::aspect_selector
detail::DSelectorInvocableType aspect_selector()
Definition: device_selector.hpp:165
__SYCL2020_DEPRECATED
#define __SYCL2020_DEPRECATED(message)
Definition: defines_elementary.hpp:57
sycl::_V1::detail::EnableIfSYCL2020DeviceSelectorInvocable
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
Definition: device_selector.hpp:139
sycl::_V1::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:41