DPC++ Runtime
Runtime libraries for oneAPI DPC++
platform_impl.hpp
Go to the documentation of this file.
1 //==-------------- platform_impl.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 <detail/platform_info.hpp>
12 #include <detail/plugin.hpp>
13 #include <sycl/detail/cl.h>
14 #include <sycl/detail/common.hpp>
15 #include <sycl/detail/pi.hpp>
16 #include <sycl/info/info_desc.hpp>
17 #include <sycl/stl.hpp>
18 
19 namespace sycl {
21 
22 // Forward declaration
23 class device_selector;
24 class device;
25 enum class aspect;
26 
27 namespace detail {
28 class device_impl;
29 
30 // TODO: implement extension management for host device
31 // TODO: implement parameters treatment for host device
33 public:
35  platform_impl() : MHostPlatform(true) {}
36 
42  explicit platform_impl(RT::PiPlatform APlatform, const plugin &APlugin)
43  : MPlatform(APlatform), MPlugin(std::make_shared<plugin>(APlugin)) {}
44 
45  explicit platform_impl(RT::PiPlatform APlatform,
46  std::shared_ptr<plugin> APlugin)
47  : MPlatform(APlatform), MPlugin(APlugin) {}
48 
49  ~platform_impl() = default;
50 
55  bool has_extension(const std::string &ExtensionName) const;
56 
66  std::vector<device>
67  get_devices(info::device_type DeviceType = info::device_type::all) const;
68 
72  template <typename Param> typename Param::return_type get_info() const;
73 
75  bool is_host() const { return MHostPlatform; };
76 
78  cl_platform_id get() const {
79  if (is_host()) {
80  throw invalid_object_error(
81  "This instance of platform doesn't support OpenCL interoperability.",
82  PI_ERROR_INVALID_PLATFORM);
83  }
84  return pi::cast<cl_platform_id>(MPlatform);
85  }
86 
94  const RT::PiPlatform &getHandleRef() const {
95  if (is_host())
96  throw invalid_object_error("This instance of platform is a host instance",
97  PI_ERROR_INVALID_PLATFORM);
98 
99  return MPlatform;
100  }
101 
109  static std::vector<platform> get_platforms();
110 
111  // \return the Plugin associated with this platform.
112  const plugin &getPlugin() const {
113  assert(!MHostPlatform && "Plugin is not available for Host.");
114  return *MPlugin;
115  }
116 
120  void setPlugin(std::shared_ptr<plugin> PluginPtr) {
121  assert(!MHostPlatform && "Plugin is not available for Host");
122  MPlugin = std::move(PluginPtr);
123  }
124 
128  pi_native_handle getNative() const;
129 
138  bool has(aspect Aspect) const;
139 
146  std::shared_ptr<device_impl> getDeviceImpl(RT::PiDevice PiDevice);
147 
157  std::shared_ptr<device_impl>
158  getOrMakeDeviceImpl(RT::PiDevice PiDevice,
159  const std::shared_ptr<platform_impl> &PlatformImpl);
160 
163 
167  static std::shared_ptr<platform_impl> getHostPlatformImpl();
168 
176  static std::shared_ptr<platform_impl>
177  getOrMakePlatformImpl(RT::PiPlatform PiPlatform, const plugin &Plugin);
178 
188  static std::shared_ptr<platform_impl>
189  getPlatformFromPiDevice(RT::PiDevice PiDevice, const plugin &Plugin);
190 
191  // when getting sub-devices for ONEAPI_DEVICE_SELECTOR we may temporarily
192  // ensure every device is a root one.
193  bool MAlwaysRootDevice = false;
194 
195 private:
196  std::shared_ptr<device_impl> getDeviceImplHelper(RT::PiDevice PiDevice);
197 
198  bool MHostPlatform = false;
199  RT::PiPlatform MPlatform = 0;
200  std::shared_ptr<plugin> MPlugin;
201  std::vector<std::weak_ptr<device_impl>> MDeviceCache;
202  std::mutex MDeviceMapMutex;
203 };
204 
205 } // namespace detail
206 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
207 } // namespace sycl
platform_impl()
Constructs platform_impl for a SYCL host platform.
void setPlugin(std::shared_ptr< plugin > PluginPtr)
Sets the platform implementation to use another plugin.
platform_impl(RT::PiPlatform APlatform, std::shared_ptr< plugin > APlugin)
platform_impl(RT::PiPlatform APlatform, const plugin &APlugin)
Constructs platform_impl from a plug-in interoperability platform handle.
const RT::PiPlatform & getHandleRef() const
Returns raw underlying plug-in platform handle.
cl_platform_id get() const
const plugin & getPlugin() const
The plugin class provides a unified interface to the underlying low-level runtimes for the device-agn...
Definition: plugin.hpp:90
#define __SYCL_INLINE_VER_NAMESPACE(X)
::pi_device PiDevice
Definition: pi.hpp:110
::pi_platform PiPlatform
Definition: pi.hpp:109
bool has_extension(const sycl::platform &SyclPlatform, const std::string &Extension)
Definition: opencl.cpp:54
detail::enable_if_t< detail::is_sigeninteger< T >::value, detail::anyall_ret_t > all(T x) __NOEXC
Definition: builtins.hpp:1243
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
uintptr_t pi_native_handle
Definition: pi.h:107
C++ wrapper of extern "C" PI interfaces.