DPC++ Runtime
Runtime libraries for oneAPI DPC++
device_impl.hpp
Go to the documentation of this file.
1 //==----------------- device_impl.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 <detail/device_info.hpp>
12 #include <detail/platform_impl.hpp>
13 #include <sycl/aspects.hpp>
14 #include <sycl/detail/cl.h>
15 #include <sycl/detail/pi.hpp>
16 #include <sycl/kernel_bundle.hpp>
17 #include <sycl/stl.hpp>
18 
19 #include <memory>
20 #include <mutex>
21 #include <utility>
22 
23 namespace sycl {
25 
26 // Forward declaration
27 class platform;
28 
29 namespace detail {
30 
31 // Forward declaration
32 class platform_impl;
33 using PlatformImplPtr = std::shared_ptr<platform_impl>;
34 
35 // TODO: Make code thread-safe
36 class device_impl {
37 public:
39  device_impl();
40 
42  explicit device_impl(pi_native_handle, const plugin &Plugin);
43 
46  explicit device_impl(RT::PiDevice Device, PlatformImplPtr Platform);
47 
50  explicit device_impl(RT::PiDevice Device, const plugin &Plugin);
51 
52  ~device_impl();
53 
58  cl_device_id get() const;
59 
66  if (MIsHostDevice)
67  throw invalid_object_error("This instance of device is a host instance",
68  PI_ERROR_INVALID_DEVICE);
69 
70  return MDevice;
71  }
72 
78  const RT::PiDevice &getHandleRef() const {
79  if (MIsHostDevice)
80  throw invalid_object_error("This instance of device is a host instance",
81  PI_ERROR_INVALID_DEVICE);
82 
83  return MDevice;
84  }
85 
89  bool is_host() const { return MIsHostDevice; }
90 
94  bool is_cpu() const { return (!is_host() && (MType == PI_DEVICE_TYPE_CPU)); }
95 
99  bool is_gpu() const { return (!is_host() && (MType == PI_DEVICE_TYPE_GPU)); }
100 
104  bool is_accelerator() const {
105  return (!is_host() && (MType == PI_DEVICE_TYPE_ACC));
106  }
107 
111  RT::PiDeviceType get_device_type() const { return MType; }
112 
123  platform get_platform() const;
124 
126  const plugin &getPlugin() const { return MPlatform->getPlugin(); }
127 
132  bool has_extension(const std::string &ExtensionName) const;
133 
134  std::vector<device>
135  create_sub_devices(const cl_device_partition_property *Properties,
136  size_t SubDevicesCount) const;
137 
148  std::vector<device> create_sub_devices(size_t ComputeUnits) const;
149 
159  std::vector<device>
160  create_sub_devices(const std::vector<size_t> &Counts) const;
161 
173  std::vector<device>
174  create_sub_devices(info::partition_affinity_domain AffinityDomain) const;
175 
184  std::vector<device> create_sub_devices() const;
185 
191  bool is_partition_supported(info::partition_property Prop) const;
192 
201  template <typename Param> typename Param::return_type get_info() const {
202  if (is_host()) {
203  return get_device_info_host<Param>();
204  }
205  return get_device_info<Param>(this->getHandleRef(), this->getPlugin());
206  }
207 
214  bool
215  is_affinity_supported(info::partition_affinity_domain AffinityDomain) const;
216 
220  pi_native_handle getNative() const;
221 
226  //
228  bool has(aspect Aspect) const;
229 
233  static std::shared_ptr<device_impl> getHostDeviceImpl();
234 
235  bool isAssertFailSupported() const;
236 
237  bool isRootDevice() const { return MRootDevice == nullptr; }
238 
239  std::string getDeviceName() const;
240 
243  uint64_t getCurrentDeviceTime();
244 
245 private:
246  explicit device_impl(pi_native_handle InteropDevice, RT::PiDevice Device,
247  PlatformImplPtr Platform, const plugin &Plugin);
248  RT::PiDevice MDevice = 0;
249  RT::PiDeviceType MType;
250  RT::PiDevice MRootDevice = nullptr;
251  bool MIsHostDevice;
252  PlatformImplPtr MPlatform;
253  bool MIsAssertFailSupported = false;
254  mutable std::string MDeviceName;
255  mutable std::once_flag MDeviceNameFlag;
256  std::pair<uint64_t, uint64_t> MDeviceHostBaseTime;
257 }; // class device_impl
258 
259 } // namespace detail
260 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
261 } // namespace sycl
sycl::_V1::info::partition_affinity_domain
partition_affinity_domain
Definition: info_desc.hpp:63
aspects.hpp
device_info.hpp
sycl::_V1::errc::platform
@ platform
sycl::_V1::opencl::has_extension
bool has_extension(const sycl::platform &SyclPlatform, const std::string &Extension)
Definition: opencl.cpp:54
sycl::_V1::info::partition_property
partition_property
Definition: info_desc.hpp:55
sycl::_V1::detail::PlatformImplPtr
std::shared_ptr< detail::platform_impl > PlatformImplPtr
Definition: context_impl.hpp:32
sycl::_V1::detail::device_impl::is_accelerator
bool is_accelerator() const
Check if device is an accelerator device.
Definition: device_impl.hpp:104
stl.hpp
__SYCL_INLINE_VER_NAMESPACE
#define __SYCL_INLINE_VER_NAMESPACE(X)
Definition: defines_elementary.hpp:11
sycl::_V1::detail::device_impl::getHandleRef
const RT::PiDevice & getHandleRef() const
Get constant reference to PI device.
Definition: device_impl.hpp:78
sycl::_V1::detail::pi::PiDevice
::pi_device PiDevice
Definition: pi.hpp:124
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
pi.hpp
sycl::_V1::detail::pi::getPlugin
const plugin & getPlugin()
Definition: pi.cpp:506
sycl::_V1::detail::pi::PiDeviceType
::pi_device_type PiDeviceType
Definition: pi.hpp:125
std::get
constexpr tuple_element< I, tuple< Types... > >::type & get(sycl::detail::tuple< Types... > &Arg) noexcept
Definition: tuple.hpp:199
sycl::_V1::detail::plugin
The plugin class provides a unified interface to the underlying low-level runtimes for the device-agn...
Definition: plugin.hpp:90
sycl::_V1::detail::device_impl::getHandleRef
RT::PiDevice & getHandleRef()
Get reference to PI device.
Definition: device_impl.hpp:65
PI_DEVICE_TYPE_GPU
@ PI_DEVICE_TYPE_GPU
A PI device that is a GPU.
Definition: pi.h:192
platform_impl.hpp
kernel_bundle.hpp
cl.h
PI_DEVICE_TYPE_ACC
@ PI_DEVICE_TYPE_ACC
A PI device that is a dedicated accelerator.
Definition: pi.h:193
sycl::_V1::detail::device_impl
Definition: device_impl.hpp:36
sycl::_V1::detail::device_impl::get_device_type
RT::PiDeviceType get_device_type() const
Return device type.
Definition: device_impl.hpp:111
pi_native_handle
uintptr_t pi_native_handle
Definition: pi.h:133
sycl::_V1::detail::device_impl::is_host
bool is_host() const
Check if SYCL device is a host device.
Definition: device_impl.hpp:89
PI_DEVICE_TYPE_CPU
@ PI_DEVICE_TYPE_CPU
A PI device that is the host processor.
Definition: pi.h:191
sycl::_V1::detail::device_impl::getPlugin
const plugin & getPlugin() const
Definition: device_impl.hpp:126
sycl::_V1::platform
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:45
sycl::_V1::detail::device_impl::isRootDevice
bool isRootDevice() const
Definition: device_impl.hpp:237
sycl::_V1::detail::device_impl::is_cpu
bool is_cpu() const
Check if device is a CPU device.
Definition: device_impl.hpp:94
sycl::_V1::detail::device_impl::get_info
Param::return_type get_info() const
Queries this SYCL device for information requested by the template parameter param.
Definition: device_impl.hpp:201
sycl::_V1::detail::device_impl::is_gpu
bool is_gpu() const
Check if device is a GPU device.
Definition: device_impl.hpp:99