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/platform_impl.hpp>
12 #include <sycl/aspects.hpp>
13 #include <sycl/detail/cl.h>
14 #include <sycl/detail/pi.hpp>
15 #include <sycl/kernel_bundle.hpp>
16 #include <sycl/stl.hpp>
17 
18 #include <memory>
19 #include <mutex>
20 #include <utility>
21 
22 namespace sycl {
23 inline namespace _V1 {
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 PluginPtr &Plugin);
42 
46  PlatformImplPtr Platform);
47 
51  const PluginPtr &Plugin);
52 
53  ~device_impl();
54 
59  cl_device_id get() const;
60 
67  if (MIsHostDevice)
68  throw invalid_object_error("This instance of device is a host instance",
69  PI_ERROR_INVALID_DEVICE);
70 
71  return MDevice;
72  }
73 
80  if (MIsHostDevice)
81  throw invalid_object_error("This instance of device is a host instance",
82  PI_ERROR_INVALID_DEVICE);
83 
84  return MDevice;
85  }
86 
90  bool is_host() const { return MIsHostDevice; }
91 
95  bool is_cpu() const { return (!is_host() && (MType == PI_DEVICE_TYPE_CPU)); }
96 
100  bool is_gpu() const { return (!is_host() && (MType == PI_DEVICE_TYPE_GPU)); }
101 
105  bool is_accelerator() const {
106  return (!is_host() && (MType == PI_DEVICE_TYPE_ACC));
107  }
108 
113 
124  platform get_platform() const;
125 
127  const PluginPtr &getPlugin() const { return MPlatform->getPlugin(); }
128 
133  bool has_extension(const std::string &ExtensionName) const;
134 
135  std::vector<device>
136  create_sub_devices(const cl_device_partition_property *Properties,
137  size_t SubDevicesCount) const;
138 
149  std::vector<device> create_sub_devices(size_t ComputeUnits) const;
150 
160  std::vector<device>
161  create_sub_devices(const std::vector<size_t> &Counts) const;
162 
174  std::vector<device>
176 
185  std::vector<device> create_sub_devices() const;
186 
193 
202  template <typename Param> typename Param::return_type get_info() const;
203 
210  bool
212 
216  pi_native_handle getNative() const;
217 
222  //
224  bool has(aspect Aspect) const;
225 
229  static std::shared_ptr<device_impl> getHostDeviceImpl();
230 
231  bool isAssertFailSupported() const;
232 
233  bool isRootDevice() const { return MRootDevice == nullptr; }
234 
235  std::string getDeviceName() const;
236 
237  bool
239  return Arch == getDeviceArch();
240  }
241 
244  uint64_t getCurrentDeviceTime();
245 
247 
249 
251  backend getBackend() const { return MPlatform->getBackend(); }
252 
255  PlatformImplPtr getPlatformImpl() const { return MPlatform; }
256 
258  std::string
260 
263 
264 private:
265  explicit device_impl(pi_native_handle InteropDevice,
267  PlatformImplPtr Platform, const PluginPtr &Plugin);
268  sycl::detail::pi::PiDevice MDevice = 0;
270  sycl::detail::pi::PiDevice MRootDevice = nullptr;
271  bool MIsHostDevice;
272  PlatformImplPtr MPlatform;
273  bool MIsAssertFailSupported = false;
274  mutable std::string MDeviceName;
275  mutable std::once_flag MDeviceNameFlag;
276  mutable ext::oneapi::experimental::architecture MDeviceArch{};
277  mutable std::once_flag MDeviceArchFlag;
278  std::pair<uint64_t, uint64_t> MDeviceHostBaseTime{0, 0};
279 }; // class device_impl
280 
281 } // namespace detail
282 } // namespace _V1
283 } // namespace sycl
std::vector< device > create_sub_devices() const
Partition device into sub devices.
sycl::detail::pi::PiDeviceType get_device_type() const
Return device type.
bool has(aspect Aspect) const
Indicates if the SYCL device has the given feature.
bool is_host() const
Check if SYCL device is a host device.
Definition: device_impl.hpp:90
Param::return_type get_info() const
Queries this SYCL device for information requested by the template parameter param.
platform get_platform() const
Get associated SYCL platform.
bool isGetDeviceAndHostTimerSupported()
Check clGetDeviceAndHostTimer is available for fallback profiling.
bool extOneapiArchitectureIs(ext::oneapi::experimental::architecture Arch) const
std::string get_device_info_string(sycl::detail::pi::PiDeviceInfo InfoCode) const
Get device info string.
device_impl()
Constructs a SYCL device instance as a host device.
Definition: device_impl.cpp:20
bool is_cpu() const
Check if device is a CPU device.
Definition: device_impl.hpp:95
backend getBackend() const
Get the backend of this device.
const PluginPtr & getPlugin() const
pi_native_handle getNative() const
Gets the native handle of the SYCL device.
bool is_gpu() const
Check if device is a GPU device.
uint64_t getCurrentDeviceTime()
Gets the current device timestamp.
std::string getDeviceName() const
sycl::detail::pi::PiDevice & getHandleRef()
Get reference to PI device.
Definition: device_impl.hpp:66
bool has_extension(const std::string &ExtensionName) const
Check SYCL extension support by device.
bool is_affinity_supported(info::partition_affinity_domain AffinityDomain) const
Check if affinity partitioning by specified domain is supported by device.
Definition: device_impl.cpp:96
cl_device_id get() const
Get instance of OpenCL device.
ext::oneapi::experimental::architecture getDeviceArch() const
Get device architecture.
const sycl::detail::pi::PiDevice & getHandleRef() const
Get constant reference to PI device.
Definition: device_impl.hpp:79
bool is_partition_supported(info::partition_property Prop) const
Check if desired partition property supported by device.
static std::shared_ptr< device_impl > getHostDeviceImpl()
Gets the single instance of the Host Device.
PlatformImplPtr getPlatformImpl() const
Get the platform impl serving this device.
bool is_accelerator() const
Check if device is an accelerator device.
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:63
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:48
std::shared_ptr< detail::platform_impl > PlatformImplPtr
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:204
_pi_device_info
Definition: pi.h:291
_pi_device_type
Definition: pi.h:259
@ PI_DEVICE_TYPE_ACC
A PI device that is a dedicated accelerator.
Definition: pi.h:265
@ PI_DEVICE_TYPE_GPU
A PI device that is a GPU.
Definition: pi.h:264
@ PI_DEVICE_TYPE_CPU
A PI device that is the host processor.
Definition: pi.h:263
C++ wrapper of extern "C" PI interfaces.