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/detail/ur.hpp>
14 #include <sycl/device.hpp>
15 #include <sycl/device_selector.hpp>
16 #include <sycl/image.hpp>
17 #include <sycl/info/info_desc.hpp>
18 #include <sycl/platform.hpp>
19 
20 namespace sycl {
21 inline namespace _V1 {
22 
24 
25 platform::platform(cl_platform_id PlatformId) {
26  auto Plugin = sycl::detail::ur::getPlugin<backend::opencl>();
27  ur_platform_handle_t UrPlatform = nullptr;
28  Plugin->call<detail::UrApiKind::urPlatformCreateWithNativeHandle>(
29  detail::ur::cast<ur_native_handle_t>(PlatformId), Plugin->getUrAdapter(),
30  /* pProperties = */ nullptr, &UrPlatform);
31  impl = detail::platform_impl::getOrMakePlatformImpl(UrPlatform, Plugin);
32 }
33 
34 // protected constructor for internal use
35 platform::platform(const device &Device) { *this = Device.get_platform(); }
36 
37 platform::platform(const device_selector &dev_selector) {
38  *this = dev_selector.select_device().get_platform();
39 }
40 
41 cl_platform_id platform::get() const { return impl->get(); }
42 
43 bool platform::has_extension(detail::string_view ExtName) const {
44  return impl->has_extension(ExtName.data());
45 }
46 
47 std::vector<device> platform::get_devices(info::device_type DeviceType) const {
48  return impl->get_devices(DeviceType);
49 }
50 
51 std::vector<platform> platform::get_platforms() {
53 }
54 
55 std::vector<platform> platform::get_unsupported_platforms() {
57 }
58 
59 backend platform::get_backend() const noexcept { return impl->getBackend(); }
60 
61 template <typename Param>
64 platform::get_info_impl() const {
65  return detail::convert_to_abi_neutral(impl->template get_info<Param>());
66 }
67 
68 ur_native_handle_t platform::getNative() const { return impl->getNative(); }
69 
70 bool platform::has(aspect Aspect) const { return impl->has(Aspect); }
71 
72 #define __SYCL_PARAM_TRAITS_SPEC(DescType, Desc, ReturnT, PiCode) \
73  template __SYCL_EXPORT detail::ABINeutralT_t<ReturnT> \
74  platform::get_info_impl<info::platform::Desc>() const;
75 
76 #include <sycl/info/platform_traits.def>
77 #undef __SYCL_PARAM_TRAITS_SPEC
78 
79 template <typename Param>
82  return impl->get_backend_info<Param>();
83 }
84 
85 #define __SYCL_PARAM_TRAITS_SPEC(DescType, Desc, ReturnT, Picode) \
86  template __SYCL_EXPORT ReturnT \
87  platform::get_backend_info<info::DescType::Desc>() const;
88 
89 #include <sycl/info/sycl_backend_traits.def>
90 
91 #undef __SYCL_PARAM_TRAITS_SPEC
92 
95  throw std::runtime_error("SYCL default contexts are not enabled");
96 
97  // Keeping the default context for platforms in the global cache to avoid
98  // shared_ptr based circular dependency between platform and context classes
99  std::unordered_map<detail::PlatformImplPtr, detail::ContextImplPtr>
100  &PlatformToDefaultContextCache =
102 
103  std::lock_guard<std::mutex> Lock{
106 
107  auto It = PlatformToDefaultContextCache.find(impl);
108  if (PlatformToDefaultContextCache.end() == It)
109  std::tie(It, std::ignore) = PlatformToDefaultContextCache.insert(
110  {impl, detail::getSyclObjImpl(context{get_devices()})});
111 
112  return detail::createSyclObjFromImpl<context>(It->second);
113 }
114 
115 std::vector<device> platform::ext_oneapi_get_composite_devices() const {
116  // Only GPU architectures can be composite devices.
117  auto GPUDevices = get_devices(info::device_type::gpu);
118  // Using ZE_FLAT_DEVICE_HIERARCHY=COMBINED, we receive tiles as devices, which
119  // are component devices. Thus, we need to get the composite device for each
120  // of the component devices, and filter out duplicates.
121  std::vector<device> Composites;
122  std::vector<device> Result;
123  for (auto &Dev : GPUDevices) {
124  if (!Dev.has(sycl::aspect::ext_oneapi_is_component))
125  continue;
126 
127  auto Composite = Dev.get_info<
128  sycl::ext::oneapi::experimental::info::device::composite_device>();
129  if (std::find(Composites.begin(), Composites.end(), Composite) ==
130  Composites.end())
131  Composites.push_back(Composite);
132  }
133  for (const auto &Composite : Composites) {
134  auto Components = Composite.get_info<
135  sycl::ext::oneapi::experimental::info::device::component_devices>();
136  // Checking whether Components are GPU device is not enough, we need to
137  // check if they are in the list of available devices returned by
138  // `get_devices()`, because we cannot return a Composite device unless all
139  // of its components are available too.
140  size_t ComponentsFound = std::count_if(
141  Components.begin(), Components.end(), [&](const device &d) {
142  return std::find(GPUDevices.begin(), GPUDevices.end(), d) !=
143  GPUDevices.end();
144  });
145  if (ComponentsFound == Components.size())
146  Result.push_back(Composite);
147  }
148  return Result;
149 }
150 
151 namespace detail {
152 
154  const char *StringVal = Val ? "1" : "0";
156  StringVal);
157 }
158 
159 } // namespace detail
160 } // namespace _V1
161 } // 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::vector< platform > get_platforms()
Returns all available SYCL platforms in the system.
static std::vector< platform > get_unsupported_platforms()
Returns all unsupported (non-SYCL) platforms in the system.
static std::shared_ptr< platform_impl > getOrMakePlatformImpl(ur_platform_handle_t UrPlatform, const PluginPtr &Plugin)
Queries the cache to see if the specified UR platform has been seen before.
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:81
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:115
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:70
backend get_backend() const noexcept
Returns the backend associated with this platform.
Definition: platform.cpp:59
context ext_oneapi_get_default_context() const
Return this platform's default context.
Definition: platform.cpp:93
platform()
Constructs a SYCL platform using the default device.
Definition: platform.cpp:23
static std::vector< platform > get_platforms()
Returns all available SYCL platforms in the system.
Definition: platform.cpp:51
detail::is_backend_info_desc< Param >::return_type get_backend_info() const
Queries this SYCL platform for SYCL backend-specific info.
Definition: platform.cpp:81
static std::vector< platform > get_unsupported_platforms()
Returns all unsupported (non-SYCL) platforms in the system.
Definition: platform.cpp:55
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
decltype(Obj::impl) const & getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:31
void enable_ext_oneapi_default_context(bool Val)
Allows to enable/disable "Default Context" extension.
Definition: platform.cpp:153
auto tie(Ts &...Args)
Definition: tuple.hpp:39
typename ABINeutralT< T >::type ABINeutralT_t
Definition: util.hpp:85
int default_selector_v(const device &dev)
Definition: access.hpp:18
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324
C++ utilities for Unified Runtime integration.