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:
41  const std::shared_ptr<plugin> &APlugin)
42  : MPlatform(APlatform), MPlugin(APlugin) {
43 
44  // Find out backend of the platform
46  APlugin->call_nocheck<PiApiKind::piPlatformGetInfo>(
48  sizeof(sycl::detail::pi::PiPlatformBackend), &PiBackend, nullptr);
49  MBackend = convertBackend(PiBackend);
50  }
51 
52  ~platform_impl() = default;
53 
58  bool has_extension(const std::string &ExtensionName) const;
59 
64  bool supports_usm() const;
65 
75  std::vector<device>
77 
81  template <typename Param> typename Param::return_type get_info() const;
82 
86  template <typename Param>
87  typename Param::return_type get_backend_info() const;
88 
90  backend getBackend(void) const { return MBackend; }
91 
93  void getBackendOption(const char *frontend_option,
94  const char **backend_option) const {
95  const auto &Plugin = getPlugin();
97  Plugin->call_nocheck<PiApiKind::piPluginGetBackendOption>(
98  MPlatform, frontend_option, backend_option);
99  Plugin->checkPiResult(Err);
100  }
101 
103  cl_platform_id get() const { return pi::cast<cl_platform_id>(MPlatform); }
104 
112  const sycl::detail::pi::PiPlatform &getHandleRef() const { return MPlatform; }
113 
121  static std::vector<platform> get_platforms();
122 
123  // \return the Plugin associated with this platform.
124  const PluginPtr &getPlugin() const { return MPlugin; }
125 
131  MPlugin = PluginPtr;
132  MBackend = Backend;
133  }
134 
138  pi_native_handle getNative() const;
139 
148  bool has(aspect Aspect) const;
149 
156  std::shared_ptr<device_impl>
158 
168  std::shared_ptr<device_impl>
170  const std::shared_ptr<platform_impl> &PlatformImpl);
171 
179  static std::shared_ptr<platform_impl>
181  const PluginPtr &Plugin);
182 
192  static std::shared_ptr<platform_impl>
194  const PluginPtr &Plugin);
195 
196  // when getting sub-devices for ONEAPI_DEVICE_SELECTOR we may temporarily
197  // ensure every device is a root one.
198  bool MAlwaysRootDevice = false;
199 
200 private:
201  std::shared_ptr<device_impl>
202  getDeviceImplHelper(sycl::detail::pi::PiDevice PiDevice);
203 
204  // Helper to filter reportable devices in the platform
205  template <typename ListT, typename FilterT>
206  std::vector<int>
207  filterDeviceFilter(std::vector<sycl::detail::pi::PiDevice> &PiDevices,
208  ListT *FilterList) const;
209 
210  sycl::detail::pi::PiPlatform MPlatform = 0;
211  backend MBackend;
212 
213  PluginPtr MPlugin;
214  std::vector<std::weak_ptr<device_impl>> MDeviceCache;
215  std::mutex MDeviceMapMutex;
216 };
217 
218 } // namespace detail
219 } // namespace _V1
220 } // namespace sycl
pi_native_handle getNative() const
Gets the native handle of the SYCL platform.
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.
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:105
::pi_platform PiPlatform
Definition: pi.hpp:103
::pi_platform_backend PiPlatformBackend
Definition: pi.hpp:104
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:267
_pi_result
Definition: pi.h:274
@ PI_EXT_PLATFORM_INFO_BACKEND
Definition: pi.h:295
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:333
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.