DPC++ Runtime
Runtime libraries for oneAPI DPC++
device.hpp
Go to the documentation of this file.
1 //==------------------- device.hpp - SYCL device ---------------------------==//
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/aspects.hpp>
12 #include <sycl/backend_types.hpp>
14 #include <sycl/detail/export.hpp>
17 #include <sycl/detail/pi.h>
18 #include <sycl/detail/string.hpp>
20 #include <sycl/detail/util.hpp>
21 #include <sycl/device_selector.hpp>
23 #include <sycl/info/info_desc.hpp>
25 #include <sycl/platform.hpp>
26 
27 #include <cstddef>
28 #include <memory>
29 #include <string>
30 #include <type_traits>
31 #include <typeinfo>
32 #include <variant>
33 #include <vector>
34 
35 namespace sycl {
36 inline namespace _V1 {
37 // Forward declarations
38 class device_selector;
39 template <backend BackendName, class SyclObjectT>
40 auto get_native(const SyclObjectT &Obj)
41  -> backend_return_t<BackendName, SyclObjectT>;
42 namespace detail {
43 class device_impl;
45 } // namespace detail
46 
47 enum class aspect;
48 
49 namespace ext::oneapi {
50 // Forward declaration
51 class filter_selector;
52 
53 enum class peer_access {
54  access_supported = 0x0,
55  atomics_supported = 0x1,
56 };
57 
58 } // namespace ext::oneapi
59 
64 class __SYCL_EXPORT device : public detail::OwnerLessBase<device> {
65 public:
67  device();
68 
73 #ifdef __SYCL_INTERNAL_API
74  explicit device(cl_device_id DeviceId);
75 #endif
76 
81  __SYCL2020_DEPRECATED("SYCL 1.2.1 device selectors are deprecated. Please "
82  "use SYCL 2020 device selectors instead.")
83  explicit device(const device_selector &DeviceSelector);
84 
89  template <typename DeviceSelector,
90  typename =
91  detail::EnableIfSYCL2020DeviceSelectorInvocable<DeviceSelector>>
92  explicit device(const DeviceSelector &deviceSelector)
93  : device(detail::select_device(deviceSelector)) {}
94 
95  bool operator==(const device &rhs) const { return impl == rhs.impl; }
96 
97  bool operator!=(const device &rhs) const { return !(*this == rhs); }
98 
99  device(const device &rhs) = default;
100 
101  device(device &&rhs) = default;
102 
103  device &operator=(const device &rhs) = default;
104 
105  device &operator=(device &&rhs) = default;
106 
107  void ext_oneapi_enable_peer_access(const device &peer);
108  void ext_oneapi_disable_peer_access(const device &peer);
109  bool
110  ext_oneapi_can_access_peer(const device &peer,
113 
118 #ifdef __SYCL_INTERNAL_API
119  cl_device_id get() const;
120 #endif
121 
126  "is_host() is deprecated as the host device is no longer supported.")
127  bool is_host() const;
128 
132  bool is_cpu() const;
133 
137  bool is_gpu() const;
138 
142  bool is_accelerator() const;
143 
154  platform get_platform() const;
155 
167  template <info::partition_property prop>
168  std::vector<device> create_sub_devices(size_t ComputeUnits) const;
169 
180  template <info::partition_property prop>
181  std::vector<device>
182  create_sub_devices(const std::vector<size_t> &Counts) const;
183 
197  template <info::partition_property prop>
198  std::vector<device>
199  create_sub_devices(info::partition_affinity_domain AffinityDomain) const;
200 
211  template <info::partition_property prop>
212  std::vector<device> create_sub_devices() const;
213 
222  template <typename Param>
223  typename detail::is_device_info_desc<Param>::return_type get_info() const {
224  return detail::convert_from_abi_neutral(get_info_impl<Param>());
225  }
226 
230  template <typename Param>
232  get_backend_info() const;
233 
238  __SYCL2020_DEPRECATED("use device::has() function with aspects APIs instead")
239  bool has_extension(const std::string &extension_name) const;
240 
249  static std::vector<device>
250  get_devices(info::device_type deviceType = info::device_type::all);
251 
255  backend get_backend() const noexcept;
256 
257 // Clang may warn about the use of diagnose_if in __SYCL_WARN_IMAGE_ASPECT, so
258 // we disable that warning as we make appropriate checks to ensure its
259 // existence.
260 // TODO: Remove this diagnostics when __SYCL_WARN_IMAGE_ASPECT is removed.
261 #if defined(__clang__)
262 #pragma clang diagnostic push
263 #pragma clang diagnostic ignored "-Wgcc-compat"
264 #endif // defined(__clang__)
265 
272  bool has(aspect Aspect) const __SYCL_WARN_IMAGE_ASPECT(Aspect);
273 
282  bool ext_oneapi_architecture_is(ext::oneapi::experimental::architecture arch);
283 
292  bool
293  ext_oneapi_architecture_is(ext::oneapi::experimental::arch_category category);
294 
296 
305  bool
306  ext_oneapi_can_compile(ext::oneapi::experimental::source_language Language);
307 
314  bool ext_oneapi_supports_cl_c_feature(const std::string &Feature);
315 
323  bool ext_oneapi_supports_cl_c_version(
324  const ext::oneapi::experimental::cl_version &Version) const;
325 
332  bool ext_oneapi_supports_cl_extension(
333  const std::string &name,
334  ext::oneapi::experimental::cl_version *version = nullptr) const;
335 
344  std::string ext_oneapi_cl_profile() const;
345 
346 // TODO: Remove this diagnostics when __SYCL_WARN_IMAGE_ASPECT is removed.
347 #if defined(__clang__)
348 #pragma clang diagnostic pop
349 #endif // defined(__clang__)
350 
351 private:
352  std::shared_ptr<detail::device_impl> impl;
353  device(std::shared_ptr<detail::device_impl> impl) : impl(impl) {}
354 
355  pi_native_handle getNative() const;
356 
357  template <class Obj>
358  friend decltype(Obj::impl) detail::getSyclObjImpl(const Obj &SyclObject);
359 
360  template <class T>
361  friend typename std::add_pointer_t<typename decltype(T::impl)::element_type>
362  detail::getRawSyclObjImpl(const T &SyclObject);
363 
364  template <class T>
365  friend T detail::createSyclObjFromImpl(decltype(T::impl) ImplObj);
366 
367  template <backend BackendName, class SyclObjectT>
368  friend auto get_native(const SyclObjectT &Obj)
370 
371  template <typename Param>
372  typename detail::ABINeutralT_t<
374  get_info_impl() const;
375 };
376 
377 } // namespace _V1
378 } // namespace sycl
379 
380 namespace std {
381 template <> struct hash<sycl::device> {
382  size_t operator()(const sycl::device &Device) const {
383  return hash<std::shared_ptr<sycl::detail::device_impl>>()(
385  }
386 };
387 } // namespace std
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:64
device(device &&rhs)=default
__SYCL2020_DEPRECATED("SYCL 1.2.1 device selectors are deprecated. Please " "use SYCL 2020 device selectors instead.") explicit device(const device_selector &DeviceSelector)
Constructs a SYCL device instance from an OpenCL cl_device_id in accordance with the requirements des...
device & operator=(const device &rhs)=default
device & operator=(device &&rhs)=default
__SYCL2020_DEPRECATED("is_host() is deprecated as the host device is no longer supported.") bool is_host() const
Get instance of device.
device(const device &rhs)=default
bool operator!=(const device &rhs) const
Definition: device.hpp:97
bool operator==(const device &rhs) const
Definition: device.hpp:95
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:99
#define __SYCL_WARN_IMAGE_ASPECT(aspect)
std::add_pointer_t< typename decltype(T::impl)::element_type > getRawSyclObjImpl(const T &SyclObject)
Definition: impl_utils.hpp:40
auto getDeviceComparisonLambda()
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:30
auto convert_from_abi_neutral(ParamT &&Info)
Definition: platform.hpp:74
T createSyclObjFromImpl(decltype(T::impl) ImplObj)
Definition: impl_utils.hpp:48
typename ABINeutralT< T >::type ABINeutralT_t
Definition: util.hpp:85
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
bool has_extension(const sycl::platform &SyclPlatform, const std::string &Extension)
Definition: opencl.cpp:54
signed char __SYCL2020_DEPRECATED
Definition: aliases.hpp:94
pointer get() const
Definition: multi_ptr.hpp:544
auto get_native(const SyclObjectT &Obj) -> backend_return_t< BackendName, SyclObjectT >
Definition: backend.hpp:136
typename backend_traits< Backend >::template return_type< SyclType > backend_return_t
Definition: backend.hpp:87
Definition: access.hpp:18
static std::enable_if_t< std::is_invocable_r_v< int, DeviceSelector, const sycl::device & > > select_device(const DeviceSelector &selector=sycl::gpu_selector_v)
Definition: device.hpp:800
uintptr_t pi_native_handle
Definition: pi.h:217
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324
size_t operator()(const sycl::device &Device) const
Definition: device.hpp:382