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 
22 namespace sycl {
24 
25 // Forward declaration
26 class platform;
27 
28 namespace detail {
29 
30 // Forward declaration
31 class platform_impl;
32 using PlatformImplPtr = std::shared_ptr<platform_impl>;
33 
34 // TODO: Make code thread-safe
35 class device_impl {
36 public:
38  device_impl();
39 
41  explicit device_impl(pi_native_handle, const plugin &Plugin);
42 
45  explicit device_impl(RT::PiDevice Device, PlatformImplPtr Platform);
46 
49  explicit device_impl(RT::PiDevice Device, const plugin &Plugin);
50 
51  ~device_impl();
52 
57  cl_device_id get() const;
58 
65  if (MIsHostDevice)
66  throw invalid_object_error("This instance of device is a host instance",
67  PI_ERROR_INVALID_DEVICE);
68 
69  return MDevice;
70  }
71 
77  const RT::PiDevice &getHandleRef() const {
78  if (MIsHostDevice)
79  throw invalid_object_error("This instance of device is a host instance",
80  PI_ERROR_INVALID_DEVICE);
81 
82  return MDevice;
83  }
84 
88  bool is_host() const { return MIsHostDevice; }
89 
93  bool is_cpu() const { return (!is_host() && (MType == PI_DEVICE_TYPE_CPU)); }
94 
98  bool is_gpu() const { return (!is_host() && (MType == PI_DEVICE_TYPE_GPU)); }
99 
103  bool is_accelerator() const {
104  return (!is_host() && (MType == PI_DEVICE_TYPE_ACC));
105  }
106 
110  RT::PiDeviceType get_device_type() const { return MType; }
111 
122  platform get_platform() const;
123 
125  const plugin &getPlugin() const { return MPlatform->getPlugin(); }
126 
131  bool has_extension(const std::string &ExtensionName) const;
132 
133  std::vector<device>
134  create_sub_devices(const cl_device_partition_property *Properties,
135  size_t SubDevicesCount) const;
136 
147  std::vector<device> create_sub_devices(size_t ComputeUnits) const;
148 
158  std::vector<device>
159  create_sub_devices(const std::vector<size_t> &Counts) const;
160 
172  std::vector<device>
173  create_sub_devices(info::partition_affinity_domain AffinityDomain) const;
174 
180  bool is_partition_supported(info::partition_property Prop) const;
181 
190  template <typename Param> typename Param::return_type get_info() const {
191  if (is_host()) {
192  return get_device_info_host<Param>();
193  }
194  return get_device_info<Param>(this->getHandleRef(), this->getPlugin());
195  }
196 
203  bool
204  is_affinity_supported(info::partition_affinity_domain AffinityDomain) const;
205 
209  pi_native_handle getNative() const;
210 
215  //
217  bool has(aspect Aspect) const;
218 
222  static std::shared_ptr<device_impl> getHostDeviceImpl();
223 
224  bool isAssertFailSupported() const;
225 
226  bool isRootDevice() const { return MRootDevice == nullptr; }
227 
228  std::string getDeviceName() const;
229 
230 private:
231  explicit device_impl(pi_native_handle InteropDevice, RT::PiDevice Device,
232  PlatformImplPtr Platform, const plugin &Plugin);
233  RT::PiDevice MDevice = 0;
234  RT::PiDeviceType MType;
235  RT::PiDevice MRootDevice = nullptr;
236  bool MIsHostDevice;
237  PlatformImplPtr MPlatform;
238  bool MIsAssertFailSupported = false;
239  mutable std::string MDeviceName;
240  mutable std::once_flag MDeviceNameFlag;
241 }; // class device_impl
242 
243 } // namespace detail
244 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
245 } // namespace sycl
bool is_host() const
Check if SYCL device is a host device.
Definition: device_impl.hpp:88
Param::return_type get_info() const
Queries this SYCL device for information requested by the template parameter param.
const RT::PiDevice & getHandleRef() const
Get constant reference to PI device.
Definition: device_impl.hpp:77
bool is_cpu() const
Check if device is a CPU device.
Definition: device_impl.hpp:93
bool is_gpu() const
Check if device is a GPU device.
Definition: device_impl.hpp:98
RT::PiDeviceType get_device_type() const
Return device type.
const plugin & getPlugin() const
RT::PiDevice & getHandleRef()
Get reference to PI device.
Definition: device_impl.hpp:64
bool is_accelerator() const
Check if device is an accelerator device.
The plugin class provides a unified interface to the underlying low-level runtimes for the device-agn...
Definition: plugin.hpp:90
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:47
#define __SYCL_INLINE_VER_NAMESPACE(X)
constexpr tuple_element< I, tuple< Types... > >::type & get(sycl::detail::tuple< Types... > &Arg) noexcept
Definition: tuple.hpp:199
::pi_device PiDevice
Definition: pi.hpp:110
::pi_device_type PiDeviceType
Definition: pi.hpp:111
const plugin & getPlugin()
Definition: pi.cpp:509
std::shared_ptr< detail::platform_impl > PlatformImplPtr
bool has_extension(const sycl::platform &SyclPlatform, const std::string &Extension)
Definition: opencl.cpp:54
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
uintptr_t pi_native_handle
Definition: pi.h:111
@ PI_DEVICE_TYPE_ACC
A PI device that is a dedicated accelerator.
Definition: pi.h:171
@ PI_DEVICE_TYPE_GPU
A PI device that is a GPU.
Definition: pi.h:170
@ PI_DEVICE_TYPE_CPU
A PI device that is the host processor.
Definition: pi.h:169
C++ wrapper of extern "C" PI interfaces.
@ Device