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 {
25 namespace oneapi {
26 class filter_selector;
27 }
28 } // namespace ext
29 
36 class __SYCL_EXPORT __SYCL2020_DEPRECATED(
37  "Use SYCL 2020 callable device selectors instead.") device_selector {
38 
39 public:
40  virtual ~device_selector() = default;
41 
42  virtual device select_device() const;
43 
44  virtual int operator()(const device &device) const = 0;
45 };
46 
52 class __SYCL_EXPORT __SYCL2020_DEPRECATED(
53  "Use the callable sycl::default_selector_v instead.") default_selector
54  : public device_selector {
55 public:
56  int operator()(const device &dev) const override;
57 };
58 
64 class __SYCL_EXPORT __SYCL2020_DEPRECATED(
65  "Use the callable sycl::gpu_selector_v instead.") gpu_selector
66  : public device_selector {
67 public:
68  int operator()(const device &dev) const override;
69 };
70 
76 class __SYCL_EXPORT __SYCL2020_DEPRECATED(
77  "Use the callable sycl::cpu_selector_v instead.") cpu_selector
78  : public device_selector {
79 public:
80  int operator()(const device &dev) const override;
81 };
82 
88 class __SYCL_EXPORT
91 public:
92  int operator()(const device &dev) const override;
93 };
94 
100 class __SYCL_EXPORT
101 __SYCL2020_DEPRECATED("Host device is no longer supported.") host_selector
102  : public device_selector {
103 public:
104  int operator()(const device &dev) const override;
105 };
106 
107 // -------------- SYCL 2020
108 
109 // SYCL 2020 standalone selectors
110 __SYCL_EXPORT int default_selector_v(const device &dev);
111 __SYCL_EXPORT int gpu_selector_v(const device &dev);
112 __SYCL_EXPORT int cpu_selector_v(const device &dev);
113 __SYCL_EXPORT int accelerator_selector_v(const device &dev);
114 
115 namespace detail {
116 // SYCL 2020 section 4.6.1.1 defines a negative score to reject a device from
117 // selection
118 static constexpr int REJECT_DEVICE_SCORE = -1;
119 
120 using DSelectorInvocableType = std::function<int(const sycl::device &)>;
121 
122 template <typename LastT>
123 void fill_aspect_vector(std::vector<aspect> &V, LastT L) {
124  V.emplace_back(L);
125 }
126 
127 template <typename FirstT, typename... OtherTs>
128 void fill_aspect_vector(std::vector<aspect> &V, FirstT F, OtherTs... O) {
129  V.emplace_back(F);
130  fill_aspect_vector(V, O...);
131 }
132 
133 #if __cplusplus >= 201703L
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>
140 using EnableIfSYCL2020DeviceSelectorInvocable = std::enable_if_t<
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 #endif
145 
146 __SYCL_EXPORT device
147 select_device(const DSelectorInvocableType &DeviceSelectorInvocable);
148 
149 __SYCL_EXPORT device
150 select_device(const DSelectorInvocableType &DeviceSelectorInvocable,
151  const context &SyclContext);
152 
153 } // namespace detail
154 
155 __SYCL_EXPORT detail::DSelectorInvocableType
156 aspect_selector(const std::vector<aspect> &RequireList,
157  const std::vector<aspect> &DenyList = {});
158 
159 template <typename... AspectListT>
161  std::vector<aspect> RequireList;
162  RequireList.reserve(sizeof...(AspectList));
163 
164  detail::fill_aspect_vector(RequireList, AspectList...);
165 
166  return aspect_selector(RequireList, {});
167 }
168 
169 template <aspect... AspectList>
171  return aspect_selector({AspectList...}, {});
172 }
173 
174 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
175 } // 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:41
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:49
#define __SYCL_INLINE_VER_NAMESPACE(X)
#define __SYCL2020_DEPRECATED(message)
class __SYCL2020_DEPRECATED("Host device is no longer supported.") host_selector int default_selector_v(const device &dev)
Selects SYCL host device.
std::function< int(const sycl::device &)> DSelectorInvocableType
static constexpr int REJECT_DEVICE_SCORE
void fill_aspect_vector(std::vector< aspect > &V, FirstT F, OtherTs... O)
device select_device(const DSelectorInvocableType &DeviceSelectorInvocable, const context &SyclContext)
int accelerator_selector_v(const device &dev)
detail::DSelectorInvocableType aspect_selector()
int cpu_selector_v(const device &dev)
std::uint8_t instead
Definition: aliases.hpp:69
int gpu_selector_v(const device &dev)
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
@ Host