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 #include <sycl/stl.hpp>
20 
21 namespace sycl {
22 inline namespace _V1 {
23 
24 // Forward declaration
25 class device_selector;
26 class device;
27 enum class aspect;
28 
29 namespace detail {
30 class device_impl;
31 
32 // TODO: implement extension management for host device
33 // TODO: implement parameters treatment for host device
35 public:
37  platform_impl() : MHostPlatform(true) {}
38 
45  const std::shared_ptr<plugin> &APlugin)
46  : MPlatform(APlatform), MPlugin(APlugin) {
47 
48  // Find out backend of the platform
50  APlugin->call_nocheck<PiApiKind::piPlatformGetInfo>(
52  sizeof(sycl::detail::pi::PiPlatformBackend), &PiBackend, nullptr);
53  MBackend = convertBackend(PiBackend);
54  }
55 
56  ~platform_impl() = default;
57 
62  bool has_extension(const std::string &ExtensionName) const;
63 
73  std::vector<device>
75 
79  template <typename Param> typename Param::return_type get_info() const;
80 
82  bool is_host() const { return MHostPlatform; };
83 
85  backend getBackend(void) const { return MBackend; }
86 
88  void getBackendOption(const char *frontend_option,
89  const char **backend_option) const {
90  const auto &Plugin = getPlugin();
92  Plugin->call_nocheck<PiApiKind::piPluginGetBackendOption>(
93  MPlatform, frontend_option, backend_option);
94  Plugin->checkPiResult(Err);
95  }
96 
98  cl_platform_id get() const {
99  if (is_host()) {
100  throw invalid_object_error(
101  "This instance of platform doesn't support OpenCL interoperability.",
102  PI_ERROR_INVALID_PLATFORM);
103  }
104  return pi::cast<cl_platform_id>(MPlatform);
105  }
106 
115  if (is_host())
116  throw invalid_object_error("This instance of platform is a host instance",
117  PI_ERROR_INVALID_PLATFORM);
118 
119  return MPlatform;
120  }
121 
129  static std::vector<platform> get_platforms();
130 
131  // \return the Plugin associated with this platform.
132  const PluginPtr &getPlugin() const {
133  assert(!MHostPlatform && "Plugin is not available for Host.");
134  return MPlugin;
135  }
136 
142  assert(!MHostPlatform && "Plugin is not available for Host");
143  MPlugin = PluginPtr;
144  MBackend = Backend;
145  }
146 
150  pi_native_handle getNative() const;
151 
160  bool has(aspect Aspect) const;
161 
168  std::shared_ptr<device_impl>
170 
180  std::shared_ptr<device_impl>
182  const std::shared_ptr<platform_impl> &PlatformImpl);
183 
186 
190  static std::shared_ptr<platform_impl> getHostPlatformImpl();
191 
199  static std::shared_ptr<platform_impl>
201  const PluginPtr &Plugin);
202 
212  static std::shared_ptr<platform_impl>
214  const PluginPtr &Plugin);
215 
216  // when getting sub-devices for ONEAPI_DEVICE_SELECTOR we may temporarily
217  // ensure every device is a root one.
218  bool MAlwaysRootDevice = false;
219 
220 private:
221  std::shared_ptr<device_impl>
222  getDeviceImplHelper(sycl::detail::pi::PiDevice PiDevice);
223 
224  // Helper to filter reportable devices in the platform
225  template <typename ListT, typename FilterT>
226  std::vector<int>
227  filterDeviceFilter(std::vector<sycl::detail::pi::PiDevice> &PiDevices,
228  ListT *FilterList) const;
229 
230  bool MHostPlatform = false;
231  sycl::detail::pi::PiPlatform MPlatform = 0;
232  backend MBackend;
233 
234  PluginPtr MPlugin;
235  std::vector<std::weak_ptr<device_impl>> MDeviceCache;
236  std::mutex MDeviceMapMutex;
237 };
238 
239 } // namespace detail
240 } // namespace _V1
241 } // 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.
cl_platform_id get() const
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
platform_impl(sycl::detail::pi::PiPlatform APlatform, const std::shared_ptr< plugin > &APlugin)
Constructs platform_impl from a plug-in interoperability platform handle.
::pi_device PiDevice
Definition: pi.hpp:131
::pi_platform PiPlatform
Definition: pi.hpp:129
::pi_platform_backend PiPlatformBackend
Definition: pi.hpp:130
backend convertBackend(pi_platform_backend PiBackend)
Definition: backend.cpp:49
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:48
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:204
_pi_result
Definition: pi.h:211
@ PI_EXT_PLATFORM_INFO_BACKEND
Definition: pi.h:232
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:270
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.