DPC++ Runtime
Runtime libraries for oneAPI DPC++
platform.cpp
Go to the documentation of this file.
1 //==----------- platform.cpp -----------------------------------------------==//
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 
10 #include <detail/config.hpp>
12 #include <detail/platform_impl.hpp>
13 #include <sycl/device.hpp>
14 #include <sycl/device_selector.hpp>
15 #include <sycl/info/info_desc.hpp>
16 #include <sycl/platform.hpp>
17 
18 namespace sycl {
19 inline namespace _V1 {
20 
22 
23 platform::platform(cl_platform_id PlatformId) {
25  detail::pi::cast<sycl::detail::pi::PiPlatform>(PlatformId),
26  sycl::detail::pi::getPlugin<backend::opencl>());
27 }
28 
29 // protected constructor for internal use
30 platform::platform(const device &Device) { *this = Device.get_platform(); }
31 
32 platform::platform(const device_selector &dev_selector) {
33  *this = dev_selector.select_device().get_platform();
34 }
35 
36 cl_platform_id platform::get() const { return impl->get(); }
37 
38 bool platform::has_extension(const std::string &ExtensionName) const {
39  return impl->has_extension(ExtensionName);
40 }
41 
42 bool platform::is_host() const {
43  bool IsHost = impl->is_host();
44  assert(!IsHost &&
45  "platform::is_host should not be called in implementation.");
46  return IsHost;
47 }
48 
49 std::vector<device> platform::get_devices(info::device_type DeviceType) const {
50  return impl->get_devices(DeviceType);
51 }
52 
53 std::vector<platform> platform::get_platforms() {
55 }
56 
57 backend platform::get_backend() const noexcept { return impl->getBackend(); }
58 
59 template <typename Param>
62 platform::get_info_impl() const {
63  return detail::convert_to_abi_neutral(impl->template get_info<Param>());
64 }
65 
66 pi_native_handle platform::getNative() const { return impl->getNative(); }
67 
68 bool platform::has(aspect Aspect) const { return impl->has(Aspect); }
69 
70 #define __SYCL_PARAM_TRAITS_SPEC(DescType, Desc, ReturnT, PiCode) \
71  template __SYCL_EXPORT detail::ABINeutralT_t<ReturnT> \
72  platform::get_info_impl<info::platform::Desc>() const;
73 
74 #include <sycl/info/platform_traits.def>
75 #undef __SYCL_PARAM_TRAITS_SPEC
76 
77 template <typename Param>
80  return impl->get_backend_info<Param>();
81 }
82 
83 #define __SYCL_PARAM_TRAITS_SPEC(DescType, Desc, ReturnT, Picode) \
84  template __SYCL_EXPORT ReturnT \
85  platform::get_backend_info<info::DescType::Desc>() const;
86 
87 #include <sycl/info/sycl_backend_traits.def>
88 
89 #undef __SYCL_PARAM_TRAITS_SPEC
90 
93  throw std::runtime_error("SYCL default contexts are not enabled");
94 
95  // Keeping the default context for platforms in the global cache to avoid
96  // shared_ptr based circular dependency between platform and context classes
97  std::unordered_map<detail::PlatformImplPtr, detail::ContextImplPtr>
98  &PlatformToDefaultContextCache =
100 
101  std::lock_guard<std::mutex> Lock{
104 
105  auto It = PlatformToDefaultContextCache.find(impl);
106  if (PlatformToDefaultContextCache.end() == It)
107  std::tie(It, std::ignore) = PlatformToDefaultContextCache.insert(
109 
110  return detail::createSyclObjFromImpl<context>(It->second);
111 }
112 
113 std::vector<device> platform::ext_oneapi_get_composite_devices() const {
114  // Only GPU architectures can be composite devices.
115  auto GPUDevices = get_devices(info::device_type::gpu);
116  // Using ZE_FLAT_DEVICE_HIERARCHY=COMBINED, we receive tiles as devices, which
117  // are component devices. Thus, we need to get the composite device for each
118  // of the component devices, and filter out duplicates.
119  std::vector<device> Composites;
120  std::vector<device> Result;
121  for (auto &Dev : GPUDevices) {
122  if (!Dev.has(sycl::aspect::ext_oneapi_is_component))
123  continue;
124 
125  auto Composite = Dev.get_info<
126  sycl::ext::oneapi::experimental::info::device::composite_device>();
127  if (std::find(Result.begin(), Result.end(), Composite) == Result.end())
128  Composites.push_back(Composite);
129  }
130  for (const auto &Composite : Composites) {
131  auto Components = Composite.get_info<
132  sycl::ext::oneapi::experimental::info::device::component_devices>();
133  // Checking whether Components are GPU device is not enough, we need to
134  // check if they are in the list of available devices returned by
135  // `get_devices()`, because we cannot return a Composite device unless all
136  // of its components are available too.
137  size_t ComponentsFound = std::count_if(
138  Components.begin(), Components.end(), [&](const device &d) {
139  return std::find(GPUDevices.begin(), GPUDevices.end(), d) !=
140  GPUDevices.end();
141  });
142  if (ComponentsFound == Components.size())
143  Result.push_back(Composite);
144  }
145  return Result;
146 }
147 
148 namespace detail {
149 
151  const char *StringVal = Val ? "1" : "0";
153  StringVal);
154 }
155 
156 } // namespace detail
157 } // namespace _V1
158 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:50
std::unordered_map< PlatformImplPtr, ContextImplPtr > & getPlatformToDefaultContextCache()
std::mutex & getPlatformToDefaultContextCacheMutex()
static GlobalHandler & instance()
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.
static std::vector< platform > get_platforms()
Returns all available SYCL platforms in the system.
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:64
platform get_platform() const
Get associated SYCL platform.
Definition: device.cpp:85
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:99
std::vector< device > ext_oneapi_get_composite_devices() const
Definition: platform.cpp:113
bool has(aspect Aspect) const __SYCL_WARN_IMAGE_ASPECT(Aspect)
Indicates if all of the SYCL devices on this platform have the given feature.
Definition: platform.cpp:68
backend get_backend() const noexcept
Returns the backend associated with this platform.
Definition: platform.cpp:57
context ext_oneapi_get_default_context() const
Return this platform's default context.
Definition: platform.cpp:91
platform()
Constructs a SYCL platform using the default device.
Definition: platform.cpp:21
static std::vector< platform > get_platforms()
Returns all available SYCL platforms in the system.
Definition: platform.cpp:53
std::vector< device > get_devices(info::device_type DeviceType=info::device_type::all) const
Returns all SYCL devices associated with this platform.
Definition: platform.cpp:49
detail::is_backend_info_desc< Param >::return_type get_backend_info() const
Queries this SYCL platform for SYCL backend-specific info.
Definition: platform.cpp:79
class __SYCL2020_DEPRECATED("Host device is no longer supported.") host_selector int default_selector_v(const device &dev)
Selects SYCL host device.
constexpr tuple_element< I, tuple< Types... > >::type & get(sycl::detail::tuple< Types... > &Arg) noexcept
Definition: tuple.hpp:198
auto convert_to_abi_neutral(ParamT &&Info)
Definition: platform.hpp:58
void enable_ext_oneapi_default_context(bool Val)
Allows to enable/disable "Default Context" extension.
Definition: platform.cpp:150
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:30
auto tie(Ts &...Args)
Definition: tuple.hpp:39
typename ABINeutralT< T >::type ABINeutralT_t
Definition: util.hpp:85
bool has_extension(const sycl::platform &SyclPlatform, const std::string &Extension)
Definition: opencl.cpp:54
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:217
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324