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>
17 #include <sycl/kernel_bundle.hpp>
18 
19 #include <memory>
20 #include <mutex>
21 #include <utility>
22 
23 namespace sycl {
24 inline namespace _V1 {
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:
40 
42  explicit device_impl(pi_native_handle, const PluginPtr &Plugin);
43 
47  PlatformImplPtr Platform);
48 
52  const PluginPtr &Plugin);
53 
54  ~device_impl();
55 
60  cl_device_id get() const;
61 
68 
74  const sycl::detail::pi::PiDevice &getHandleRef() const { return MDevice; }
75 
79  bool is_cpu() const { return MType == PI_DEVICE_TYPE_CPU; }
80 
84  bool is_gpu() const { return MType == PI_DEVICE_TYPE_GPU; }
85 
89  bool is_accelerator() const { return MType == PI_DEVICE_TYPE_ACC; }
90 
95 
106  platform get_platform() const;
107 
109  const PluginPtr &getPlugin() const { return MPlatform->getPlugin(); }
110 
115  bool has_extension(const std::string &ExtensionName) const;
116 
117  std::vector<device>
118  create_sub_devices(const cl_device_partition_property *Properties,
119  size_t SubDevicesCount) const;
120 
131  std::vector<device> create_sub_devices(size_t ComputeUnits) const;
132 
142  std::vector<device>
143  create_sub_devices(const std::vector<size_t> &Counts) const;
144 
156  std::vector<device>
158 
167  std::vector<device> create_sub_devices() const;
168 
175 
184  template <typename Param> typename Param::return_type get_info() const;
185 
189  template <typename Param>
190  typename Param::return_type get_backend_info() const;
191 
198  bool
200 
204  pi_native_handle getNative() const;
205 
210  //
212  bool has(aspect Aspect) const;
213 
214  bool isAssertFailSupported() const;
215 
216  bool isRootDevice() const { return MRootDevice == nullptr; }
217 
218  std::string getDeviceName() const;
219 
220  bool
222  return Arch == getDeviceArch();
223  }
224 
227  std::optional<ext::oneapi::experimental::architecture> CategoryMinArch =
229  std::optional<ext::oneapi::experimental::architecture> CategoryMaxArch =
231  if (CategoryMinArch.has_value() && CategoryMaxArch.has_value())
232  return CategoryMinArch <= getDeviceArch() &&
233  getDeviceArch() <= CategoryMaxArch;
234  return false;
235  }
236 
238 
239  // Returns all guarantees that are either equal to guarantee or weaker than
240  // it. E.g if guarantee == parallel, it returns the vector {weakly_parallel,
241  // parallel}.
242  template <typename ReturnT>
245  const int forwardProgressGuaranteeSize = 3;
246  int guaranteeVal = static_cast<int>(guarantee);
247  ReturnT res;
248  res.reserve(forwardProgressGuaranteeSize - guaranteeVal);
249  for (int currentGuarantee = forwardProgressGuaranteeSize - 1;
250  currentGuarantee >= guaranteeVal; --currentGuarantee) {
251  res.emplace_back(
253  currentGuarantee));
254  }
255  return res;
256  }
257 
262 
266  ext::oneapi::experimental::execution_scope coordinationScope) const;
267 
271  ext::oneapi::experimental::execution_scope coordinationScope) const;
272 
275  ext::oneapi::experimental::execution_scope coordination_scope) const;
276 
279  uint64_t getCurrentDeviceTime();
280 
282 
284 
286  backend getBackend() const { return MPlatform->getBackend(); }
287 
290  PlatformImplPtr getPlatformImpl() const { return MPlatform; }
291 
293  std::string
295 
298 
299 private:
300  explicit device_impl(pi_native_handle InteropDevice,
302  PlatformImplPtr Platform, const PluginPtr &Plugin);
303  sycl::detail::pi::PiDevice MDevice = 0;
305  sycl::detail::pi::PiDevice MRootDevice = nullptr;
306  PlatformImplPtr MPlatform;
307  bool MIsAssertFailSupported = false;
308  mutable std::string MDeviceName;
309  mutable std::once_flag MDeviceNameFlag;
310  mutable ext::oneapi::experimental::architecture MDeviceArch{};
311  mutable std::once_flag MDeviceArchFlag;
312  std::pair<uint64_t, uint64_t> MDeviceHostBaseTime{0, 0};
313 }; // class device_impl
314 
315 } // namespace detail
316 } // namespace _V1
317 } // 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.
Definition: device_impl.hpp:94
bool has(aspect Aspect) const
Indicates if the SYCL device has the given feature.
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.
bool is_cpu() const
Check if device is a CPU device.
Definition: device_impl.hpp:79
backend getBackend() const
Get the backend of this device.
static ReturnT getProgressGuaranteesUpTo(ext::oneapi::experimental::forward_progress_guarantee guarantee)
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.
Definition: device_impl.hpp:84
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:67
bool has_extension(const std::string &ExtensionName) const
Check SYCL extension support by device.
bool extOneapiArchitectureIs(ext::oneapi::experimental::arch_category Category) const
static sycl::ext::oneapi::experimental::forward_progress_guarantee getHostProgressGuarantee(sycl::ext::oneapi::experimental::execution_scope threadScope, sycl::ext::oneapi::experimental::execution_scope coordinationScope)
sycl::ext::oneapi::experimental::forward_progress_guarantee getProgressGuarantee(ext::oneapi::experimental::execution_scope threadScope, ext::oneapi::experimental::execution_scope coordinationScope) 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:88
cl_device_id get() const
Get instance of OpenCL device.
Definition: device_impl.cpp:95
ext::oneapi::experimental::architecture getDeviceArch() const
Get device architecture.
bool supportsForwardProgress(ext::oneapi::experimental::forward_progress_guarantee guarantee, ext::oneapi::experimental::execution_scope threadScope, ext::oneapi::experimental::execution_scope coordinationScope) const
ext::oneapi::experimental::forward_progress_guarantee getImmediateProgressGuarantee(ext::oneapi::experimental::execution_scope coordination_scope) const
const sycl::detail::pi::PiDevice & getHandleRef() const
Get constant reference to PI device.
Definition: device_impl.hpp:74
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.
PlatformImplPtr getPlatformImpl() const
Get the platform impl serving this device.
bool is_accelerator() const
Check if device is an accelerator device.
Definition: device_impl.hpp:89
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:47
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:258
_pi_device_info
Definition: pi.h:346
_pi_device_type
Definition: pi.h:313
@ PI_DEVICE_TYPE_ACC
A PI device that is a dedicated accelerator.
Definition: pi.h:319
@ PI_DEVICE_TYPE_GPU
A PI device that is a GPU.
Definition: pi.h:318
@ PI_DEVICE_TYPE_CPU
A PI device that is the host processor.
Definition: pi.h:317
C++ wrapper of extern "C" PI interfaces.