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 
97 // -------------- SYCL 2020
98 
99 // SYCL 2020 standalone selectors
100 __SYCL_EXPORT int default_selector_v(const device &dev);
101 __SYCL_EXPORT int gpu_selector_v(const device &dev);
102 __SYCL_EXPORT int cpu_selector_v(const device &dev);
103 __SYCL_EXPORT int accelerator_selector_v(const device &dev);
104 
105 namespace detail {
106 // SYCL 2020 section 4.6.1.1 defines a negative score to reject a device from
107 // selection
108 static constexpr int REJECT_DEVICE_SCORE = -1;
109 
110 using DSelectorInvocableType = std::function<int(const sycl::device &)>;
111 
112 template <typename LastT>
113 void fill_aspect_vector(std::vector<aspect> &V, LastT L) {
114  V.emplace_back(L);
115 }
116 
117 template <typename FirstT, typename... OtherTs>
118 void fill_aspect_vector(std::vector<aspect> &V, FirstT F, OtherTs... O) {
119  V.emplace_back(F);
120  fill_aspect_vector(V, O...);
121 }
122 
123 // Enable if DeviceSelector callable has matching signature, but
124 // exclude if descended from filter_selector which is not purely callable or
125 // if descended from it is descended from SYCL 1.2.1 device_selector.
126 // See [FilterSelector not Callable] in device_selector.cpp
127 template <typename DeviceSelector>
129  std::is_invocable_r_v<int, DeviceSelector &, const device &> &&
130  !std::is_base_of_v<ext::oneapi::filter_selector, DeviceSelector> &&
131  !std::is_base_of_v<device_selector, DeviceSelector>>;
132 
133 __SYCL_EXPORT device
134 select_device(const DSelectorInvocableType &DeviceSelectorInvocable);
135 
136 __SYCL_EXPORT device
137 select_device(const DSelectorInvocableType &DeviceSelectorInvocable,
138  const context &SyclContext);
139 
140 } // namespace detail
141 
142 __SYCL_EXPORT detail::DSelectorInvocableType
143 aspect_selector(const std::vector<aspect> &RequireList,
144  const std::vector<aspect> &DenyList = {});
145 
146 template <typename... AspectListT>
148  std::vector<aspect> RequireList;
149  RequireList.reserve(sizeof...(AspectList));
150 
151  detail::fill_aspect_vector(RequireList, AspectList...);
152 
153  return aspect_selector(RequireList, {});
154 }
155 
156 template <aspect... AspectList>
158  return aspect_selector({AspectList...}, {});
159 }
160 
161 } // namespace _V1
162 } // 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:50
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:64
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
int accelerator_selector_v(const device &dev)
signed char __SYCL2020_DEPRECATED
Definition: aliases.hpp:94
int cpu_selector_v(const device &dev)
int default_selector_v(const device &dev)
std::uint8_t instead
Definition: aliases.hpp:93
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