DPC++ Runtime
Runtime libraries for oneAPI Data Parallel C++
device.cpp
Go to the documentation of this file.
1 //==------------------- device.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 
11 #include <CL/sycl/device.hpp>
14 #include <detail/backend_impl.hpp>
15 #include <detail/config.hpp>
16 #include <detail/device_impl.hpp>
17 #include <detail/force_device.hpp>
18 
20 namespace sycl {
21 namespace detail {
23  if (t == info::device_type::all) {
24  t = ft;
25  } else if (ft != info::device_type::all && t != ft) {
26  throw cl::sycl::invalid_parameter_error("No device of forced type.",
28  }
29 }
30 } // namespace detail
31 
32 device::device() : impl(detail::device_impl::getHostDeviceImpl()) {}
33 
34 device::device(cl_device_id DeviceId) {
35  // The implementation constructor takes ownership of the native handle so we
36  // must retain it in order to adhere to SYCL 1.2.1 spec (Rev6, section 4.3.1.)
37  detail::RT::PiDevice Device;
38  auto Plugin = detail::RT::getPlugin<backend::opencl>();
40  detail::pi::cast<pi_native_handle>(DeviceId), nullptr, &Device);
41  auto Platform =
43  impl = Platform->getOrMakeDeviceImpl(Device, Platform);
44  clRetainDevice(DeviceId);
45 }
46 
47 device::device(const device_selector &deviceSelector) {
48  *this = deviceSelector.select_device();
49 }
50 
51 std::vector<device> device::get_devices(info::device_type deviceType) {
52  std::vector<device> devices;
53  detail::device_filter_list *FilterList =
55  // Host device availability should depend on the forced type
56  bool includeHost = false;
57  // If SYCL_DEVICE_FILTER is set, we don't automatically include it.
58  // We will check if host devices are specified in the filter below.
59  if (FilterList) {
60  if (deviceType != info::device_type::host &&
61  deviceType != info::device_type::all)
62  includeHost = false;
63  else
64  includeHost = FilterList->containsHost();
65  } else {
66  includeHost = detail::match_types(deviceType, info::device_type::host);
67  }
69  // Exclude devices which do not match requested device type
70  if (detail::match_types(deviceType, forced_type)) {
71  detail::force_type(deviceType, forced_type);
72  for (const auto &plt : platform::get_platforms()) {
73  // If SYCL_BE is set then skip platforms which doesn't have specified
74  // backend.
76  if (ForcedBackend)
77  if (!plt.is_host() && plt.get_backend() != *ForcedBackend)
78  continue;
79  // If SYCL_DEVICE_FILTER is set, skip platforms that is incompatible
80  // with the filter specification.
81  if (FilterList && !FilterList->backendCompatible(plt.get_backend()))
82  continue;
83 
84  if (includeHost && plt.is_host()) {
85  std::vector<device> host_device(
86  plt.get_devices(info::device_type::host));
87  if (!host_device.empty())
88  devices.insert(devices.end(), host_device.begin(), host_device.end());
89  } else {
90  std::vector<device> found_devices(plt.get_devices(deviceType));
91  if (!found_devices.empty())
92  devices.insert(devices.end(), found_devices.begin(),
93  found_devices.end());
94  }
95  }
96  }
97  return devices;
98 }
99 
100 cl_device_id device::get() const { return impl->get(); }
101 
102 bool device::is_host() const { return impl->is_host(); }
103 
104 bool device::is_cpu() const { return impl->is_cpu(); }
105 
106 bool device::is_gpu() const { return impl->is_gpu(); }
107 
108 bool device::is_accelerator() const { return impl->is_accelerator(); }
109 
110 platform device::get_platform() const { return impl->get_platform(); }
111 
112 template <info::partition_property prop>
113 std::vector<device> device::create_sub_devices(size_t ComputeUnits) const {
114  return impl->create_sub_devices(ComputeUnits);
115 }
116 
117 template __SYCL_EXPORT std::vector<device>
118 device::create_sub_devices<info::partition_property::partition_equally>(
119  size_t ComputeUnits) const;
120 
121 template <info::partition_property prop>
122 std::vector<device>
123 device::create_sub_devices(const std::vector<size_t> &Counts) const {
124  return impl->create_sub_devices(Counts);
125 }
126 
127 template __SYCL_EXPORT std::vector<device>
128 device::create_sub_devices<info::partition_property::partition_by_counts>(
129  const std::vector<size_t> &Counts) const;
130 
131 template <info::partition_property prop>
132 std::vector<device> device::create_sub_devices(
133  info::partition_affinity_domain AffinityDomain) const {
134  return impl->create_sub_devices(AffinityDomain);
135 }
136 
137 template __SYCL_EXPORT std::vector<device> device::create_sub_devices<
139  info::partition_affinity_domain AffinityDomain) const;
140 
141 bool device::has_extension(const std::string &extension_name) const {
142  return impl->has_extension(extension_name);
143 }
144 
145 template <info::device param>
148  return impl->template get_info<param>();
149 }
150 
151 #define __SYCL_PARAM_TRAITS_SPEC(param_type, param, ret_type) \
152  template __SYCL_EXPORT ret_type device::get_info<info::param_type::param>() \
153  const;
154 
155 #include <CL/sycl/info/device_traits.def>
156 
157 #undef __SYCL_PARAM_TRAITS_SPEC
158 
159 backend device::get_backend() const noexcept { return getImplBackend(impl); }
160 
161 pi_native_handle device::getNative() const { return impl->getNative(); }
162 
163 bool device::has(aspect Aspect) const { return impl->has(Aspect); }
164 
165 } // namespace sycl
166 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::backend
backend
Definition: backend_types.hpp:21
cl::sycl::device_selector
The device_selector class provides ability to choose the best SYCL device based on heuristics specifi...
Definition: device_selector.hpp:28
cl::sycl::device::create_sub_devices
std::vector< device > create_sub_devices(size_t ComputeUnits) const
Partition device into sub devices.
Definition: device.cpp:113
cl::sycl::detail::SYCLConfig::get
static const char * get()
Definition: config.hpp:108
cl::sycl::info::device
device
Definition: info_desc.hpp:49
cl::sycl::device_selector::select_device
virtual device select_device() const
Definition: device_selector.cpp:65
cl::sycl::info::param_traits
Definition: info_desc.hpp:297
device_filter.hpp
PI_INVALID_OPERATION
@ PI_INVALID_OPERATION
Definition: pi.h:84
config.hpp
cl::sycl::detail::device_impl
Definition: device_impl.hpp:32
cl::sycl::device::device
device()
Constructs a SYCL device instance as a host device.
Definition: device.cpp:32
cl::sycl::device::is_cpu
bool is_cpu() const
Check if device is a CPU device.
Definition: device.cpp:104
device_selector.hpp
device.hpp
cl::sycl::device::is_gpu
bool is_gpu() const
Check if device is a GPU device.
Definition: device.cpp:106
cl::sycl::detail::pi::PiDevice
::pi_device PiDevice
Definition: pi.hpp:102
cl::sycl::device::get_devices
static std::vector< device > get_devices(info::device_type deviceType=info::device_type::all)
Query available SYCL devices.
Definition: device.cpp:51
cl::sycl::device::get_platform
platform get_platform() const
Get associated SYCL platform.
Definition: device.cpp:110
cl::sycl::device::has_extension
bool has_extension(const std::string &extension_name) const
Check SYCL extension support by device.
Definition: device.cpp:141
cl::sycl::info::device_type
device_type
Definition: info_desc.hpp:170
piextDeviceCreateWithNativeHandle
pi_result piextDeviceCreateWithNativeHandle(pi_native_handle nativeHandle, pi_platform platform, pi_device *device)
Creates PI device object from a native handle.
Definition: pi_esimd_emulator.cpp:613
device_impl.hpp
cl::sycl::detail::platform_impl::getPlatformFromPiDevice
static std::shared_ptr< platform_impl > getPlatformFromPiDevice(RT::PiDevice PiDevice, const plugin &Plugin)
Queries the cache for the specified platform based on an input device.
Definition: platform_impl.cpp:60
cl::sycl::device::is_accelerator
bool is_accelerator() const
Check if device is an accelerator device.
Definition: device.cpp:108
cl::sycl::detail::getImplBackend
backend getImplBackend(const T &Impl)
Definition: backend_impl.hpp:16
export.hpp
cl::sycl::device::get_backend
backend get_backend() const noexcept
Returns the backend associated with this device.
Definition: device.cpp:159
cl::sycl::detail::get_forced_type
info::device_type get_forced_type()
Definition: force_device.cpp:24
cl::sycl::detail::device_filter_list::backendCompatible
bool backendCompatible(backend Backend)
Definition: device_filter.cpp:133
cl::sycl::info::device_type::all
@ all
cl::sycl::detail::device_filter_list
Definition: device_filter.hpp:37
force_device.hpp
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::platform::get_platforms
static std::vector< platform > get_platforms()
Returns all available SYCL platforms in the system.
Definition: platform.cpp:46
cl::sycl::aspect
aspect
Definition: aspects.hpp:15
cl::sycl::detail::force_type
void force_type(info::device_type &t, const info::device_type &ft)
Definition: device.cpp:22
pi_native_handle
uintptr_t pi_native_handle
Definition: pi.h:72
std::get
constexpr tuple_element< I, tuple< Types... > >::type & get(cl::sycl::detail::tuple< Types... > &Arg) noexcept
Definition: tuple.hpp:199
cl::sycl::device::get_info
info::param_traits< info::device, param >::return_type get_info() const
Queries this SYCL device for information requested by the template parameter param.
Definition: device.cpp:147
cl::sycl::info::device_type::host
@ host
cl::sycl::detail::device_filter_list::containsHost
bool containsHost()
Definition: device_filter.cpp:160
cl::sycl::platform
Encapsulates a SYCL platform on which kernels may be executed.
Definition: platform.hpp:33
backend_impl.hpp
cl::sycl::all
detail::enable_if_t< detail::is_sigeninteger< T >::value, int > all(T x) __NOEXC
Definition: builtins.hpp:1282
info_desc.hpp
cl::sycl::detail::match_types
bool match_types(const info::device_type &l, const info::device_type &r)
Definition: force_device.cpp:20
cl::sycl::device::has
bool has(aspect Aspect) const
Indicates if the SYCL device has the given feature.
Definition: device.cpp:163
cl::sycl::info::partition_property::partition_by_affinity_domain
@ partition_by_affinity_domain
cl::sycl::device::is_host
bool is_host() const
Get instance of device.
Definition: device.cpp:102
cl::sycl::info::partition_affinity_domain
partition_affinity_domain
Definition: info_desc.hpp:188
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12