DPC++ Runtime
Runtime libraries for oneAPI DPC++
context_impl.hpp
Go to the documentation of this file.
1 //==---------------- context_impl.hpp - SYCL context ------------*- C++-*---==//
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 #include <detail/device_impl.hpp>
12 #include <detail/platform_impl.hpp>
14 #include <sycl/detail/common.hpp>
15 #include <sycl/detail/os_util.hpp>
16 #include <sycl/detail/pi.hpp>
17 #include <sycl/exception_list.hpp>
18 #include <sycl/info/info_desc.hpp>
19 #include <sycl/property_list.hpp>
20 #include <sycl/stl.hpp>
21 
22 #include <map>
23 #include <memory>
24 
25 namespace sycl {
27 // Forward declaration
28 class device;
29 namespace detail {
30 using PlatformImplPtr = std::shared_ptr<detail::platform_impl>;
31 class context_impl {
32 public:
42  context_impl(const device &Device, async_handler AsyncHandler,
43  const property_list &PropList);
44 
57  context_impl(const std::vector<sycl::device> DeviceList,
58  async_handler AsyncHandler, const property_list &PropList);
59 
70  const plugin &Plugin);
71 
72  ~context_impl();
73 
77  template <typename propertyT> bool has_property() const noexcept {
78  return MPropList.has_property<propertyT>();
79  }
80 
87  template <typename propertyT> propertyT get_property() const {
88  return MPropList.get_property<propertyT>();
89  }
90 
94  cl_context get() const;
95 
99  bool is_host() const;
100 
104  const async_handler &get_async_handler() const;
105 
107  const plugin &getPlugin() const { return MPlatform->getPlugin(); }
108 
110  PlatformImplPtr getPlatformImpl() const { return MPlatform; }
111 
115  template <typename Param> typename Param::return_type get_info() const;
116 
125  RT::PiContext &getHandleRef();
126 
135  const RT::PiContext &getHandleRef() const;
136 
139  const std::vector<device> &getDevices() const { return MDevices; }
140 
142  std::map<std::pair<DeviceLibExt, RT::PiDevice>, RT::PiProgram>;
143 
158  return {MCachedLibPrograms, MCachedLibProgramsMutex};
159  }
160 
161  KernelProgramCache &getKernelProgramCache() const;
162 
164  bool hasDevice(std::shared_ptr<detail::device_impl> Device) const;
165 
171  // OpenCL does not support using descendants of context members within that
172  // context yet.
173  // TODO remove once this limitation is lifted
174  if (!is_host() && getPlugin().getBackend() == backend::opencl)
175  return hasDevice(Device);
176 
177  while (!hasDevice(Device)) {
178  if (Device->isRootDevice())
179  return false;
181  Device->get_info<info::device::parent_device>());
182  }
183 
184  return true;
185  }
186 
189  DeviceImplPtr findMatchingDeviceImpl(RT::PiDevice &DevicePI) const;
190 
194  pi_native_handle getNative() const;
195 
196  // Returns true if buffer_location property is supported by devices
197  bool isBufferLocationSupported() const;
198 
199  enum PropertySupport { NotSupported = 0, Supported = 1, NotChecked = 2 };
200 
201 private:
202  async_handler MAsyncHandler;
203  std::vector<device> MDevices;
204  RT::PiContext MContext;
205  PlatformImplPtr MPlatform;
206  property_list MPropList;
207  bool MHostContext;
208  CachedLibProgramsT MCachedLibPrograms;
209  std::mutex MCachedLibProgramsMutex;
210  mutable KernelProgramCache MKernelProgramCache;
211  mutable PropertySupport MSupportBufferLocationByDevices;
212 };
213 
214 } // namespace detail
215 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
216 } // namespace sycl
Represents a reference to value with appropriate lock acquired.
Definition: locked.hpp:23
propertyT get_property() const
Gets the specified property of this context_impl.
bool isDeviceValid(DeviceImplPtr Device)
Returns true if and only if the device can be used within this context.
Locked< CachedLibProgramsT > acquireCachedLibPrograms()
In contrast to user programs, which are compiled from user code, library programs come from the SYCL ...
const std::vector< device > & getDevices() const
Unlike ‘get_info<info::context::devices>’, this function returns a reference.
Param::return_type get_info() const
Queries this context for information.
const plugin & getPlugin() const
std::map< std::pair< DeviceLibExt, RT::PiDevice >, RT::PiProgram > CachedLibProgramsT
bool has_property() const noexcept
Checks if this context_impl has a property of type propertyT.
PlatformImplPtr getPlatformImpl() const
The plugin class provides a unified interface to the underlying low-level runtimes for the device-agn...
Definition: plugin.hpp:90
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:47
Objects of the property_list class are containers for the SYCL properties.
#define __SYCL_INLINE_VER_NAMESPACE(X)
constexpr tuple_element< I, tuple< Types... > >::type & get(sycl::detail::tuple< Types... > &Arg) noexcept
Definition: tuple.hpp:199
::pi_device PiDevice
Definition: pi.hpp:110
const plugin & getPlugin()
Definition: pi.cpp:509
::pi_context PiContext
Definition: pi.hpp:114
::pi_program PiProgram
Definition: pi.hpp:115
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: common.hpp:240
std::shared_ptr< detail::platform_impl > PlatformImplPtr
std::shared_ptr< device_impl > DeviceImplPtr
std::function< void(sycl::exception_list)> async_handler
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
uintptr_t pi_native_handle
Definition: pi.h:109
C++ wrapper of extern "C" PI interfaces.
@ Device