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
sycl::_V1::detail::platform_impl
Definition: platform_impl.hpp:34
sycl::_V1::detail::platform_impl::get
cl_platform_id get() const
Definition: platform_impl.hpp:98
sycl::_V1::detail::platform_impl::platform_impl
platform_impl()
Constructs platform_impl for a SYCL host platform.
Definition: platform_impl.hpp:37
_pi_platform_backend
_pi_platform_backend
Definition: pi.h:264
sycl::_V1::backend
backend
Definition: backend_types.hpp:18
sycl::_V1::detail::platform_impl::getNative
pi_native_handle getNative() const
Gets the native handle of the SYCL platform.
Definition: platform_impl.cpp:624
sycl::_V1::detail::platform_impl::get_platforms
static std::vector< platform > get_platforms()
Returns all available SYCL platforms in the system.
Definition: platform_impl.cpp:108
stl.hpp
sycl::_V1::detail::convertBackend
backend convertBackend(pi_platform_backend PiBackend)
Definition: backend.cpp:49
_pi_result
_pi_result
Definition: pi.h:205
common.hpp
sycl::_V1::detail::pi::PiDevice
::pi_device PiDevice
Definition: pi.hpp:131
piPlatformGetInfo
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:30
sycl::_V1::detail::platform_impl::has_extension
bool has_extension(const std::string &ExtensionName) const
Checks if this platform supports extension.
Definition: platform_impl.cpp:614
detail
---— Error handling, matching OpenCL plugin semantics.
Definition: common.hpp:44
sycl::_V1::detail::platform_impl::get_info
Param::return_type get_info() const
Queries this SYCL platform for info.
Definition: platform_impl.cpp:633
sycl::_V1::detail::platform_impl::getHostPlatformImpl
static std::shared_ptr< platform_impl > getHostPlatformImpl()
Static functions that help maintain platform uniquess and equality of comparison.
Definition: platform_impl.cpp:33
sycl
Definition: access.hpp:18
plugin.hpp
_pi_platform
Definition: pi_cuda.hpp:44
sycl::_V1::detail::pi::PiPlatform
::pi_platform PiPlatform
Definition: pi.hpp:129
sycl::_V1::detail::platform_impl::setPlugin
void setPlugin(PluginPtr &PluginPtr, backend Backend)
Sets the platform implementation to use another plugin.
Definition: platform_impl.hpp:141
pi.hpp
sycl::_V1::detail::platform_impl::has
bool has(aspect Aspect) const
Indicates if all of the SYCL devices on this platform have the given feature.
Definition: platform_impl.cpp:641
sycl::_V1::detail::platform_impl::getPlugin
const PluginPtr & getPlugin() const
Definition: platform_impl.hpp:132
cl.h
device_selector
sycl::_V1::detail::platform_impl::platform_impl
platform_impl(sycl::detail::pi::PiPlatform APlatform, const std::shared_ptr< plugin > &APlugin)
Constructs platform_impl from a plug-in interoperability platform handle.
Definition: platform_impl.hpp:44
sycl::_V1::detail::platform_impl::is_host
bool is_host() const
Definition: platform_impl.hpp:82
sycl::_V1::detail::platform_impl::getPlatformFromPiDevice
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.
Definition: platform_impl.cpp:65
sycl::_V1::detail::platform_impl::getBackendOption
void getBackendOption(const char *frontend_option, const char **backend_option) const
Get backend option.
Definition: platform_impl.hpp:88
sycl::_V1::detail::platform_impl::getDeviceImpl
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...
Definition: platform_impl.cpp:334
sycl::_V1::access::target::device
@ device
platform_info.hpp
sycl::_V1::info::device_type
device_type
Definition: info_desc.hpp:51
pi_native_handle
uintptr_t pi_native_handle
Definition: pi.h:198
sycl::_V1::detail::platform_impl::getBackend
backend getBackend(void) const
Returns the backend of this platform.
Definition: platform_impl.hpp:85
sycl::_V1::info::device_type::all
@ all
backend_types.hpp
sycl::_V1::detail::platform_impl::getOrMakePlatformImpl
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.
Definition: platform_impl.cpp:40
PI_EXT_PLATFORM_INFO_BACKEND
@ PI_EXT_PLATFORM_INFO_BACKEND
Definition: pi.h:226
backend.hpp
sycl::_V1::detail::platform_impl::get_devices
std::vector< device > get_devices(info::device_type DeviceType=info::device_type::all) const
Returns all SYCL devices associated with this platform.
Definition: platform_impl.cpp:509
sycl::_V1::detail::pi::PiPlatformBackend
::pi_platform_backend PiPlatformBackend
Definition: pi.hpp:130
piPluginGetBackendOption
pi_result piPluginGetBackendOption(pi_platform platform, const char *frontend_option, const char **backend_option)
API to get backend specific option.
Definition: pi_cuda.cpp:51
sycl::_V1::detail::platform_impl::MAlwaysRootDevice
bool MAlwaysRootDevice
Definition: platform_impl.hpp:218
info_desc.hpp
sycl::_V1::detail::platform_impl::~platform_impl
~platform_impl()=default
sycl::_V1::detail::platform_impl::getHandleRef
const sycl::detail::pi::PiPlatform & getHandleRef() const
Returns raw underlying plug-in platform handle.
Definition: platform_impl.hpp:114
sycl::_V1::detail::platform_impl::getOrMakeDeviceImpl
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 ...
Definition: platform_impl.cpp:339
_pi_device
Definition: pi_cuda.hpp:48
sycl::_V1::detail::PluginPtr
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:48