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/backend.hpp>
14 #include <sycl/backend_types.hpp>
15 #include <sycl/detail/cl.h>
16 #include <sycl/detail/common.hpp>
17 #include <sycl/detail/pi.hpp>
18 #include <sycl/info/info_desc.hpp>
19 
20 namespace sycl {
21 inline namespace _V1 {
22 
23 // Forward declaration
24 class device_selector;
25 class device;
26 enum class aspect;
27 
28 namespace detail {
29 class device_impl;
30 
31 // TODO: implement extension management for host device
32 // TODO: implement parameters treatment for host device
34 public:
36  platform_impl() : MHostPlatform(true) {}
37 
44  const std::shared_ptr<plugin> &APlugin)
45  : MPlatform(APlatform), MPlugin(APlugin) {
46 
47  // Find out backend of the platform
49  APlugin->call_nocheck<PiApiKind::piPlatformGetInfo>(
51  sizeof(sycl::detail::pi::PiPlatformBackend), &PiBackend, nullptr);
52  MBackend = convertBackend(PiBackend);
53  }
54 
55  ~platform_impl() = default;
56 
61  bool has_extension(const std::string &ExtensionName) const;
62 
67  bool supports_usm() const;
68 
78  std::vector<device>
80 
84  template <typename Param> typename Param::return_type get_info() const;
85 
89  template <typename Param>
90  typename Param::return_type get_backend_info() const;
91 
93  bool is_host() const { return MHostPlatform; };
94 
96  backend getBackend(void) const { return MBackend; }
97 
99  void getBackendOption(const char *frontend_option,
100  const char **backend_option) const {
101  const auto &Plugin = getPlugin();
103  Plugin->call_nocheck<PiApiKind::piPluginGetBackendOption>(
104  MPlatform, frontend_option, backend_option);
105  Plugin->checkPiResult(Err);
106  }
107 
109  cl_platform_id get() const {
110  if (is_host()) {
111  throw invalid_object_error(
112  "This instance of platform doesn't support OpenCL interoperability.",
113  PI_ERROR_INVALID_PLATFORM);
114  }
115  return pi::cast<cl_platform_id>(MPlatform);
116  }
117 
126  if (is_host())
127  throw invalid_object_error("This instance of platform is a host instance",
128  PI_ERROR_INVALID_PLATFORM);
129 
130  return MPlatform;
131  }
132 
140  static std::vector<platform> get_platforms();
141 
142  // \return the Plugin associated with this platform.
143  const PluginPtr &getPlugin() const {
144  assert(!MHostPlatform && "Plugin is not available for Host.");
145  return MPlugin;
146  }
147 
153  assert(!MHostPlatform && "Plugin is not available for Host");
154  MPlugin = PluginPtr;
155  MBackend = Backend;
156  }
157 
161  pi_native_handle getNative() const;
162 
171  bool has(aspect Aspect) const;
172 
179  std::shared_ptr<device_impl>
181 
191  std::shared_ptr<device_impl>
193  const std::shared_ptr<platform_impl> &PlatformImpl);
194 
197 
201  static std::shared_ptr<platform_impl> getHostPlatformImpl();
202 
210  static std::shared_ptr<platform_impl>
212  const PluginPtr &Plugin);
213 
223  static std::shared_ptr<platform_impl>
225  const PluginPtr &Plugin);
226 
227  // when getting sub-devices for ONEAPI_DEVICE_SELECTOR we may temporarily
228  // ensure every device is a root one.
229  bool MAlwaysRootDevice = false;
230 
231 private:
232  std::shared_ptr<device_impl>
233  getDeviceImplHelper(sycl::detail::pi::PiDevice PiDevice);
234 
235  // Helper to filter reportable devices in the platform
236  template <typename ListT, typename FilterT>
237  std::vector<int>
238  filterDeviceFilter(std::vector<sycl::detail::pi::PiDevice> &PiDevices,
239  ListT *FilterList) const;
240 
241  bool MHostPlatform = false;
242  sycl::detail::pi::PiPlatform MPlatform = 0;
243  backend MBackend;
244 
245  PluginPtr MPlugin;
246  std::vector<std::weak_ptr<device_impl>> MDeviceCache;
247  std::mutex MDeviceMapMutex;
248 };
249 
250 } // namespace detail
251 } // namespace _V1
252 } // namespace sycl
pi_native_handle getNative() const
Gets the native handle of the SYCL platform.
static std::shared_ptr< platform_impl > getHostPlatformImpl()
Static functions that help maintain platform uniquess and equality of comparison.
std::shared_ptr< device_impl > getOrMakeDeviceImpl(sycl::detail::pi::PiDevice PiDevice, const std::shared_ptr< platform_impl > &PlatformImpl)
Queries the device_impl cache to either return a shared_ptr for the device_impl corresponding to the ...
void getBackendOption(const char *frontend_option, const char **backend_option) const
Get backend option.
static std::shared_ptr< platform_impl > getOrMakePlatformImpl(sycl::detail::pi::PiPlatform PiPlatform, const PluginPtr &Plugin)
Queries the cache to see if the specified PiPlatform has been seen before.
platform_impl()
Constructs platform_impl for a SYCL host platform.
Param::return_type get_info() const
Queries this SYCL platform for info.
static std::vector< platform > get_platforms()
Returns all available SYCL platforms in the system.
backend getBackend(void) const
Returns the backend of this platform.
bool has(aspect Aspect) const
Indicates if all of the SYCL devices on this platform have the given feature.
std::shared_ptr< device_impl > getDeviceImpl(sycl::detail::pi::PiDevice PiDevice)
Queries the device_impl cache to return a shared_ptr for the device_impl corresponding to the PiDevic...
bool has_extension(const std::string &ExtensionName) const
Checks if this platform supports extension.
void setPlugin(PluginPtr &PluginPtr, backend Backend)
Sets the platform implementation to use another plugin.
std::vector< device > get_devices(info::device_type DeviceType=info::device_type::all) const
Returns all SYCL devices associated with this platform.
const sycl::detail::pi::PiPlatform & getHandleRef() const
Returns raw underlying plug-in platform handle.
static std::shared_ptr< platform_impl > getPlatformFromPiDevice(sycl::detail::pi::PiDevice PiDevice, const PluginPtr &Plugin)
Queries the cache for the specified platform based on an input device.
const PluginPtr & getPlugin() const
bool supports_usm() const
Checks if this platform supports usm.
platform_impl(sycl::detail::pi::PiPlatform APlatform, const std::shared_ptr< plugin > &APlugin)
Constructs platform_impl from a plug-in interoperability platform handle.
Param::return_type get_backend_info() const
Queries this SYCL platform for SYCL backend-specific information.
::pi_device PiDevice
Definition: pi.hpp:114
::pi_platform PiPlatform
Definition: pi.hpp:112
::pi_platform_backend PiPlatformBackend
Definition: pi.hpp:113
backend convertBackend(pi_platform_backend PiBackend)
Definition: backend.cpp:49
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:47
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:228
_pi_result
Definition: pi.h:235
@ PI_EXT_PLATFORM_INFO_BACKEND
Definition: pi.h:256
pi_result piPluginGetBackendOption(pi_platform platform, const char *frontend_option, const char **backend_option)
API to get backend specific option.
Definition: pi_cuda.cpp:56
_pi_platform_backend
Definition: pi.h:294
pi_result piPlatformGetInfo(pi_platform platform, pi_platform_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
Definition: pi_cuda.cpp:35
C++ wrapper of extern "C" PI interfaces.