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/string.hpp>
19 #include <sycl/detail/util.hpp>
20 #include <sycl/device_selector.hpp>
22 #include <sycl/info/info_desc.hpp>
24 #include <sycl/platform.hpp>
25 #include <ur_api.h>
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 
125  bool is_cpu() const;
126 
130  bool is_gpu() const;
131 
135  bool is_accelerator() const;
136 
146  platform get_platform() const;
147 
159  template <info::partition_property prop>
160  std::vector<device> create_sub_devices(size_t ComputeUnits) const;
161 
172  template <info::partition_property prop>
173  std::vector<device>
174  create_sub_devices(const std::vector<size_t> &Counts) const;
175 
189  template <info::partition_property prop>
190  std::vector<device>
191  create_sub_devices(info::partition_affinity_domain AffinityDomain) const;
192 
203  template <info::partition_property prop>
204  std::vector<device> create_sub_devices() const;
205 
214  template <typename Param>
216  return detail::convert_from_abi_neutral(get_info_impl<Param>());
217  }
218 
222  template <typename Param>
224  get_backend_info() const;
225 
230  __SYCL2020_DEPRECATED("use device::has() function with aspects APIs instead")
231  bool has_extension(const std::string &extension_name) const {
232  return has_extension(detail::string_view{extension_name});
233  }
234 
240  static std::vector<device>
241  get_devices(info::device_type deviceType = info::device_type::all);
242 
246  backend get_backend() const noexcept;
247 
248 // Clang may warn about the use of diagnose_if in __SYCL_WARN_IMAGE_ASPECT, so
249 // we disable that warning as we make appropriate checks to ensure its
250 // existence.
251 // TODO: Remove this diagnostics when __SYCL_WARN_IMAGE_ASPECT is removed.
252 #if defined(__clang__)
253 #pragma clang diagnostic push
254 #pragma clang diagnostic ignored "-Wgcc-compat"
255 #endif // defined(__clang__)
256 
263  bool has(aspect Aspect) const __SYCL_WARN_IMAGE_ASPECT(Aspect);
264 
273  bool ext_oneapi_architecture_is(ext::oneapi::experimental::architecture arch);
274 
283  bool
284  ext_oneapi_architecture_is(ext::oneapi::experimental::arch_category category);
285 
287 
296  bool
297  ext_oneapi_can_compile(ext::oneapi::experimental::source_language Language);
298 
305  bool ext_oneapi_supports_cl_c_feature(const std::string &Feature) {
306  return ext_oneapi_supports_cl_c_feature(detail::string_view{Feature});
307  }
308 
316  bool ext_oneapi_supports_cl_c_version(
317  const ext::oneapi::experimental::cl_version &Version) const;
318 
326  const std::string &name,
327  ext::oneapi::experimental::cl_version *version = nullptr) const {
328  return ext_oneapi_supports_cl_extension(detail::string_view{name}, version);
329  }
330 
339  std::string ext_oneapi_cl_profile() const;
340 
341 // TODO: Remove this diagnostics when __SYCL_WARN_IMAGE_ASPECT is removed.
342 #if defined(__clang__)
343 #pragma clang diagnostic pop
344 #endif // defined(__clang__)
345 
346 private:
347  std::shared_ptr<detail::device_impl> impl;
348  device(std::shared_ptr<detail::device_impl> impl) : impl(impl) {}
349 
350  ur_native_handle_t getNative() const;
351 
352  template <class Obj>
353  friend const decltype(Obj::impl) &
354  detail::getSyclObjImpl(const Obj &SyclObject);
355 
356  template <class T>
357  friend T detail::createSyclObjFromImpl(decltype(T::impl) ImplObj);
358 
359  template <backend BackendName, class SyclObjectT>
360  friend auto get_native(const SyclObjectT &Obj)
362 
363  template <typename Param>
364  typename detail::ABINeutralT_t<
366  get_info_impl() const;
367 
368  bool has_extension(detail::string_view extension_name) const;
369  bool ext_oneapi_supports_cl_c_feature(detail::string_view Feature);
370  bool ext_oneapi_supports_cl_extension(
371  detail::string_view name,
372  ext::oneapi::experimental::cl_version *version = nullptr) const;
373 };
374 
375 } // namespace _V1
376 } // namespace sycl
377 
378 namespace std {
379 template <> struct hash<sycl::device> {
380  size_t operator()(const sycl::device &Device) const {
381  return hash<std::shared_ptr<sycl::detail::device_impl>>()(
383  }
384 };
385 } // namespace std
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:64
bool ext_oneapi_supports_cl_extension(const std::string &name, ext::oneapi::experimental::cl_version *version=nullptr) const
If the device supports kernel bundles using the OpenCL extension identified by name and if version is...
Definition: device.hpp:325
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
detail::is_device_info_desc< Param >::return_type get_info() const
Queries this SYCL device for information requested by the template parameter param.
Definition: device.hpp:215
bool ext_oneapi_supports_cl_c_feature(const std::string &Feature)
Indicates if the device supports a given feature when compiling the OpenCL C language.
Definition: device.hpp:305
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)
decltype(Obj::impl) const & getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:31
auto getDeviceComparisonLambda()
auto convert_from_abi_neutral(ParamT &&Info)
Definition: platform.hpp:74
T createSyclObjFromImpl(decltype(T::impl) ImplObj)
Definition: impl_utils.hpp:40
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:25
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:138
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:941
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324
size_t operator()(const sycl::device &Device) const
Definition: device.hpp:380