DPC++ Runtime
Runtime libraries for oneAPI DPC++
platform.hpp
Go to the documentation of this file.
1 //==---------------- platform.hpp - SYCL platform --------------------------==//
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 <sycl/aspects.hpp>
12 #include <sycl/backend_types.hpp>
13 #include <sycl/context.hpp>
15 #include <sycl/detail/export.hpp>
18 #include <sycl/detail/pi.h>
19 #include <sycl/detail/string.hpp>
21 #include <sycl/detail/util.hpp>
22 #include <sycl/device_selector.hpp>
23 #include <sycl/info/info_desc.hpp>
24 
25 #ifdef __SYCL_INTERNAL_API
26 #include <sycl/detail/cl.h>
27 #endif
28 
29 #include <cstddef>
30 #include <memory>
31 #include <string>
32 #include <variant>
33 #include <vector>
34 
35 namespace sycl {
36 inline namespace _V1 {
37 // TODO: make code thread-safe
38 
39 // Forward declaration
40 class device;
41 class context;
42 
43 template <backend BackendName, class SyclObjectT>
44 auto get_native(const SyclObjectT &Obj)
45  -> backend_return_t<BackendName, SyclObjectT>;
46 namespace detail {
47 class platform_impl;
48 
56 void __SYCL_EXPORT enable_ext_oneapi_default_context(bool Val);
57 
58 template <typename ParamT> auto convert_to_abi_neutral(ParamT &&Info) {
59  using ParamNoRef = std::remove_reference_t<ParamT>;
60  if constexpr (std::is_same_v<ParamNoRef, std::string>) {
61  return detail::string{Info};
62  } else if constexpr (std::is_same_v<ParamNoRef, std::vector<std::string>>) {
63  std::vector<detail::string> Res;
64  Res.reserve(Info.size());
65  for (std::string &Str : Info) {
66  Res.push_back(detail::string{Str});
67  }
68  return Res;
69  } else {
70  return std::forward<ParamT>(Info);
71  }
72 }
73 
74 template <typename ParamT> auto convert_from_abi_neutral(ParamT &&Info) {
75  using ParamNoRef = std::remove_reference_t<ParamT>;
76  if constexpr (std::is_same_v<ParamNoRef, detail::string>) {
77  return Info.c_str();
78  } else if constexpr (std::is_same_v<ParamNoRef,
79  std::vector<detail::string>>) {
80  std::vector<std::string> Res;
81  Res.reserve(Info.size());
82  for (detail::string &Str : Info) {
83  Res.push_back(Str.c_str());
84  }
85  return Res;
86  } else {
87  return std::forward<ParamT>(Info);
88  }
89 }
90 } // namespace detail
91 namespace ext::oneapi {
92 // Forward declaration
93 class filter_selector;
94 } // namespace ext::oneapi
95 
99 class __SYCL_EXPORT platform : public detail::OwnerLessBase<platform> {
100 public:
102  platform();
103 
110 #ifdef __SYCL_INTERNAL_API
111  explicit platform(cl_platform_id PlatformId);
112 #endif
113 
121  __SYCL2020_DEPRECATED("SYCL 1.2.1 device selectors are deprecated. Please "
122  "use SYCL 2020 device selectors instead.")
123  explicit platform(const device_selector &DeviceSelector);
124 
129  template <typename DeviceSelector,
130  typename =
131  detail::EnableIfSYCL2020DeviceSelectorInvocable<DeviceSelector>>
132  explicit platform(const DeviceSelector &deviceSelector)
133  : platform(detail::select_device(deviceSelector)) {}
134 
135  platform(const platform &rhs) = default;
136 
137  platform(platform &&rhs) = default;
138 
139  platform &operator=(const platform &rhs) = default;
140 
141  platform &operator=(platform &&rhs) = default;
142 
143  bool operator==(const platform &rhs) const { return impl == rhs.impl; }
144 
145  bool operator!=(const platform &rhs) const { return !(*this == rhs); }
146 
150 #ifdef __SYCL_INTERNAL_API
151  cl_platform_id get() const;
152 #endif
153 
159  "use platform::has() function with aspects APIs instead")
160  bool has_extension(const std::string &ExtensionName) const;
161 
166  "is_host() is deprecated as the host device is no longer supported.")
167  bool is_host() const;
168 
177  std::vector<device>
178  get_devices(info::device_type DeviceType = info::device_type::all) const;
179 
183  template <typename Param>
184  typename detail::is_platform_info_desc<Param>::return_type get_info() const {
185  return detail::convert_from_abi_neutral(get_info_impl<Param>());
186  }
187 
191  template <typename Param>
193  get_backend_info() const;
194 
200  static std::vector<platform> get_platforms();
201 
205  backend get_backend() const noexcept;
206 
207 // Clang may warn about the use of diagnose_if in __SYCL_WARN_IMAGE_ASPECT, so
208 // we disable that warning as we make appropriate checks to ensure its
209 // existence.
210 // TODO: Remove this diagnostics when __SYCL_WARN_IMAGE_ASPECT is removed.
211 #if defined(__clang__)
212 #pragma clang diagnostic push
213 #pragma clang diagnostic ignored "-Wgcc-compat"
214 #endif // defined(__clang__)
215 
224  bool has(aspect Aspect) const __SYCL_WARN_IMAGE_ASPECT(Aspect);
225 
226 // TODO: Remove this diagnostics when __SYCL_WARN_IMAGE_ASPECT is removed.
227 #if defined(__clang__)
228 #pragma clang diagnostic pop
229 #endif // defined(__clang__)
230 
234  context ext_oneapi_get_default_context() const;
235 
236  std::vector<device> ext_oneapi_get_composite_devices() const;
237 
238 private:
239  pi_native_handle getNative() const;
240 
241  std::shared_ptr<detail::platform_impl> impl;
242  platform(std::shared_ptr<detail::platform_impl> impl) : impl(impl) {}
243 
244  platform(const device &Device);
245 
246  template <class T>
247  friend T detail::createSyclObjFromImpl(decltype(T::impl) ImplObj);
248  template <class Obj>
249  friend decltype(Obj::impl) detail::getSyclObjImpl(const Obj &SyclObject);
250 
251  template <backend BackendName, class SyclObjectT>
252  friend auto get_native(const SyclObjectT &Obj)
254 
255  template <typename Param>
256  typename detail::ABINeutralT_t<
258  get_info_impl() const;
259 }; // class platform
260 } // namespace _V1
261 } // namespace sycl
262 
263 namespace std {
264 template <> struct hash<sycl::platform> {
265  size_t operator()(const sycl::platform &p) const {
266  return hash<std::shared_ptr<sycl::detail::platform_impl>>()(
268  }
269 };
270 } // namespace std
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:50
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:64
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:99
platform & operator=(platform &&rhs)=default
platform & operator=(const platform &rhs)=default
bool operator!=(const platform &rhs) const
Definition: platform.hpp:145
platform(const platform &rhs)=default
platform(platform &&rhs)=default
bool operator==(const platform &rhs) const
Definition: platform.hpp:143
__SYCL2020_DEPRECATED("SYCL 1.2.1 device selectors are deprecated. Please " "use SYCL 2020 device selectors instead.") explicit platform(const device_selector &DeviceSelector)
Constructs a SYCL platform instance from an OpenCL cl_platform_id.
#define __SYCL_WARN_IMAGE_ASPECT(aspect)
auto convert_to_abi_neutral(ParamT &&Info)
Definition: platform.hpp:58
void enable_ext_oneapi_default_context(bool Val)
Allows to enable/disable "Default Context" extension.
Definition: platform.cpp:150
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:30
auto convert_from_abi_neutral(ParamT &&Info)
Definition: platform.hpp:74
T createSyclObjFromImpl(decltype(T::impl) ImplObj)
Definition: impl_utils.hpp:48
device select_device(const DSelectorInvocableType &DeviceSelectorInvocable)
typename ABINeutralT< T >::type ABINeutralT_t
Definition: util.hpp:85
std::enable_if_t< std::is_invocable_r_v< int, DeviceSelector &, const device & > &&!std::is_base_of_v< ext::oneapi::filter_selector, DeviceSelector > &&!std::is_base_of_v< device_selector, DeviceSelector > > EnableIfSYCL2020DeviceSelectorInvocable
bool has_extension(const sycl::platform &SyclPlatform, const std::string &Extension)
Definition: opencl.cpp:54
signed char __SYCL2020_DEPRECATED
Definition: aliases.hpp:94
pointer get() const
Definition: multi_ptr.hpp:544
auto get_native(const SyclObjectT &Obj) -> backend_return_t< BackendName, SyclObjectT >
Definition: backend.hpp:136
typename backend_traits< Backend >::template return_type< SyclType > backend_return_t
Definition: backend.hpp:87
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:217
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324
size_t operator()(const sycl::platform &p) const
Definition: platform.hpp:265