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>
16 #include <sycl/kernel_bundle.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 
207  template <typename Param>
208  typename Param::return_type get_backend_info() const;
209 
216  bool
218 
222  pi_native_handle getNative() const;
223 
228  //
230  bool has(aspect Aspect) const;
231 
235  static std::shared_ptr<device_impl> getHostDeviceImpl();
236 
237  bool isAssertFailSupported() const;
238 
239  bool isRootDevice() const { return MRootDevice == nullptr; }
240 
241  std::string getDeviceName() const;
242 
243  bool
245  return Arch == getDeviceArch();
246  }
247 
250  std::optional<ext::oneapi::experimental::architecture> CategoryMinArch =
252  std::optional<ext::oneapi::experimental::architecture> CategoryMaxArch =
254  if (CategoryMinArch.has_value() && CategoryMaxArch.has_value())
255  return CategoryMinArch <= getDeviceArch() &&
256  getDeviceArch() <= CategoryMaxArch;
257  return false;
258  }
259 
261 
264  uint64_t getCurrentDeviceTime();
265 
267 
269 
271  backend getBackend() const { return MPlatform->getBackend(); }
272 
275  PlatformImplPtr getPlatformImpl() const { return MPlatform; }
276 
278  std::string
280 
283 
284 private:
285  explicit device_impl(pi_native_handle InteropDevice,
287  PlatformImplPtr Platform, const PluginPtr &Plugin);
288  sycl::detail::pi::PiDevice MDevice = 0;
290  sycl::detail::pi::PiDevice MRootDevice = nullptr;
291  bool MIsHostDevice;
292  PlatformImplPtr MPlatform;
293  bool MIsAssertFailSupported = false;
294  mutable std::string MDeviceName;
295  mutable std::once_flag MDeviceNameFlag;
296  mutable ext::oneapi::experimental::architecture MDeviceArch{};
297  mutable std::once_flag MDeviceArchFlag;
298  std::pair<uint64_t, uint64_t> MDeviceHostBaseTime{0, 0};
299 }; // class device_impl
300 
301 } // namespace detail
302 } // namespace _V1
303 } // namespace sycl
Param::return_type get_backend_info() const
Queries SYCL queue for SYCL backend-specific information.
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 extOneapiArchitectureIs(ext::oneapi::experimental::arch_category Category) const
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 extOneapiCanCompile(ext::oneapi::experimental::source_language Language)
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:99
static constexpr std::optional< ext::oneapi::experimental::architecture > get_category_min_architecture(ext::oneapi::experimental::arch_category Category)
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:48
std::shared_ptr< detail::platform_impl > PlatformImplPtr
static constexpr std::optional< ext::oneapi::experimental::architecture > get_category_max_architecture(ext::oneapi::experimental::arch_category Category)
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:217
_pi_device_info
Definition: pi.h:304
_pi_device_type
Definition: pi.h:272
@ PI_DEVICE_TYPE_ACC
A PI device that is a dedicated accelerator.
Definition: pi.h:278
@ PI_DEVICE_TYPE_GPU
A PI device that is a GPU.
Definition: pi.h:277
@ PI_DEVICE_TYPE_CPU
A PI device that is the host processor.
Definition: pi.h:276
C++ wrapper of extern "C" PI interfaces.