-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplatform_impl.cpp
More file actions
308 lines (243 loc) · 10.1 KB
/
platform_impl.cpp
File metadata and controls
308 lines (243 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//==----------- platform_impl.cpp ------------------------------------------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <CL/sycl/device.hpp>
#include <detail/config.hpp>
#include <detail/device_impl.hpp>
#include <detail/platform_impl.hpp>
#include <detail/platform_info.hpp>
#include <algorithm>
#include <cstring>
#include <regex>
__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
namespace detail {
vector_class<platform> platform_impl::get_platforms() {
vector_class<platform> Platforms;
vector_class<plugin> Plugins = RT::initialize();
info::device_type ForcedType = detail::get_forced_type();
for (unsigned int i = 0; i < Plugins.size(); i++) {
pi_uint32 NumPlatforms = 0;
Plugins[i].call<PiApiKind::piPlatformsGet>(0, nullptr, &NumPlatforms);
if (NumPlatforms) {
vector_class<RT::PiPlatform> PiPlatforms(NumPlatforms);
Plugins[i].call<PiApiKind::piPlatformsGet>(NumPlatforms,
PiPlatforms.data(), nullptr);
for (const auto &PiPlatform : PiPlatforms) {
platform Platform = detail::createSyclObjFromImpl<platform>(
std::make_shared<platform_impl>(PiPlatform, Plugins[i]));
// Skip platforms which do not contain requested device types
if (!Platform.get_devices(ForcedType).empty())
Platforms.push_back(Platform);
}
}
}
// The host platform should always be available.
Platforms.emplace_back(platform());
return Platforms;
}
struct DevDescT {
const char *devName = nullptr;
int devNameSize = 0;
const char *devDriverVer = nullptr;
int devDriverVerSize = 0;
const char *platformName = nullptr;
int platformNameSize = 0;
const char *platformVer = nullptr;
int platformVerSize = 0;
};
static std::vector<DevDescT> getAllowListDesc() {
const char *str = SYCLConfig<SYCL_DEVICE_ALLOWLIST>::get();
if (!str)
return {};
std::vector<DevDescT> decDescs;
const char devNameStr[] = "DeviceName";
const char driverVerStr[] = "DriverVersion";
const char platformNameStr[] = "PlatformName";
const char platformVerStr[] = "PlatformVersion";
decDescs.emplace_back();
while ('\0' != *str) {
const char **valuePtr = nullptr;
int *size = nullptr;
// -1 to avoid comparing null terminator
if (0 == strncmp(devNameStr, str, sizeof(devNameStr) - 1)) {
valuePtr = &decDescs.back().devName;
size = &decDescs.back().devNameSize;
str += sizeof(devNameStr) - 1;
} else if (0 ==
strncmp(platformNameStr, str, sizeof(platformNameStr) - 1)) {
valuePtr = &decDescs.back().platformName;
size = &decDescs.back().platformNameSize;
str += sizeof(platformNameStr) - 1;
} else if (0 == strncmp(platformVerStr, str, sizeof(platformVerStr) - 1)) {
valuePtr = &decDescs.back().platformVer;
size = &decDescs.back().platformVerSize;
str += sizeof(platformVerStr) - 1;
} else if (0 == strncmp(driverVerStr, str, sizeof(driverVerStr) - 1)) {
valuePtr = &decDescs.back().devDriverVer;
size = &decDescs.back().devDriverVerSize;
str += sizeof(driverVerStr) - 1;
}
if (':' != *str)
throw sycl::runtime_error("Malformed device allowlist", PI_INVALID_VALUE);
// Skip ':'
str += 1;
if ('{' != *str || '{' != *(str + 1))
throw sycl::runtime_error("Malformed device allowlist", PI_INVALID_VALUE);
// Skip opening sequence "{{"
str += 2;
*valuePtr = str;
// Increment until closing sequence is encountered
while (('\0' != *str) && ('}' != *str || '}' != *(str + 1)))
++str;
if ('\0' == *str)
throw sycl::runtime_error("Malformed device allowlist", PI_INVALID_VALUE);
*size = str - *valuePtr;
// Skip closing sequence "}}"
str += 2;
if ('\0' == *str)
break;
// '|' means that the is another filter
if ('|' == *str)
decDescs.emplace_back();
else if (',' != *str)
throw sycl::runtime_error("Malformed device allowlist", PI_INVALID_VALUE);
++str;
}
return decDescs;
}
static void filterAllowList(vector_class<RT::PiDevice> &PiDevices,
RT::PiPlatform PiPlatform, const plugin &Plugin) {
const std::vector<DevDescT> AllowList(getAllowListDesc());
if (AllowList.empty())
return;
const string_class PlatformName =
sycl::detail::get_platform_info<string_class, info::platform::name>::get(
PiPlatform, Plugin);
const string_class PlatformVer =
sycl::detail::get_platform_info<string_class,
info::platform::version>::get(PiPlatform,
Plugin);
int InsertIDx = 0;
for (RT::PiDevice Device : PiDevices) {
const string_class DeviceName =
sycl::detail::get_device_info<string_class, info::device::name>::get(
Device, Plugin);
const string_class DeviceDriverVer = sycl::detail::get_device_info<
string_class, info::device::driver_version>::get(Device, Plugin);
for (const DevDescT &Desc : AllowList) {
if (nullptr != Desc.platformName &&
!std::regex_match(PlatformName,
std::regex(std::string(Desc.platformName,
Desc.platformNameSize))))
continue;
if (nullptr != Desc.platformVer &&
!std::regex_match(
PlatformVer,
std::regex(std::string(Desc.platformVer, Desc.platformVerSize))))
continue;
if (nullptr != Desc.devName &&
!std::regex_match(DeviceName, std::regex(std::string(
Desc.devName, Desc.devNameSize))))
continue;
if (nullptr != Desc.devDriverVer &&
!std::regex_match(DeviceDriverVer,
std::regex(std::string(Desc.devDriverVer,
Desc.devDriverVerSize))))
continue;
PiDevices[InsertIDx++] = Device;
break;
}
}
PiDevices.resize(InsertIDx);
}
// @return True if the device is invalid for the current backend preferences
static bool isDeviceInvalidForBe(const device &Device) {
if (Device.is_host())
return false;
// Retrieve Platform version to identify CUDA OpenCL platform
// String: OpenCL 1.2 CUDA <version>
const platform platform = Device.get_info<info::device::platform>();
const std::string platformVersion =
platform.get_info<info::platform::version>();
const bool HasOpenCL = (platformVersion.find("OpenCL") != std::string::npos);
const bool HasCUDA = (platformVersion.find("CUDA") != std::string::npos);
backend *PrefBackend = detail::SYCLConfig<detail::SYCL_BE>::get();
auto DeviceBackend = detail::getSyclObjImpl(Device)->getPlugin().getBackend();
// Reject the NVIDIA OpenCL implementation
if (DeviceBackend == backend::opencl && HasCUDA && HasOpenCL)
return true;
// If no preference, assume OpenCL and reject CUDA
if (DeviceBackend == backend::cuda && !PrefBackend) {
return true;
} else if (!PrefBackend)
return false;
// If using PI_OPENCL, reject the CUDA backend
if (DeviceBackend == backend::cuda && *PrefBackend == backend::opencl)
return true;
return false;
}
vector_class<device>
platform_impl::get_devices(info::device_type DeviceType) const {
vector_class<device> Res;
if (is_host() && (DeviceType == info::device_type::host ||
DeviceType == info::device_type::all)) {
Res.resize(1); // default device constructor creates host device
}
// If any DeviceType other than host was requested for host platform,
// an empty vector will be returned.
if (is_host() || DeviceType == info::device_type::host)
return Res;
pi_uint32 NumDevices;
const detail::plugin &Plugin = getPlugin();
Plugin.call<PiApiKind::piDevicesGet>(
MPlatform, pi::cast<RT::PiDeviceType>(DeviceType), 0,
pi::cast<RT::PiDevice *>(nullptr), &NumDevices);
if (NumDevices == 0)
return Res;
vector_class<RT::PiDevice> PiDevices(NumDevices);
// TODO catch an exception and put it to list of asynchronous exceptions
Plugin.call<PiApiKind::piDevicesGet>(MPlatform,
pi::cast<RT::PiDeviceType>(DeviceType),
NumDevices, PiDevices.data(), nullptr);
// Filter out devices that are not present in the allowlist
if (SYCLConfig<SYCL_DEVICE_ALLOWLIST>::get())
filterAllowList(PiDevices, MPlatform, this->getPlugin());
std::transform(PiDevices.begin(), PiDevices.end(), std::back_inserter(Res),
[this](const RT::PiDevice &PiDevice) -> device {
return detail::createSyclObjFromImpl<device>(
std::make_shared<device_impl>(
PiDevice, std::make_shared<platform_impl>(*this)));
});
Res.erase(std::remove_if(Res.begin(), Res.end(), isDeviceInvalidForBe),
Res.end());
return Res;
}
bool platform_impl::has_extension(const string_class &ExtensionName) const {
if (is_host())
return false;
string_class AllExtensionNames =
get_platform_info<string_class, info::platform::extensions>::get(
MPlatform, getPlugin());
return (AllExtensionNames.find(ExtensionName) != std::string::npos);
}
template <info::platform param>
typename info::param_traits<info::platform, param>::return_type
platform_impl::get_info() const {
if (is_host())
return get_platform_info_host<param>();
return get_platform_info<
typename info::param_traits<info::platform, param>::return_type,
param>::get(this->getHandleRef(), getPlugin());
}
#define PARAM_TRAITS_SPEC(param_type, param, ret_type) \
template ret_type platform_impl::get_info<info::param_type::param>() const;
#include <CL/sycl/info/platform_traits.def>
#undef PARAM_TRAITS_SPEC
} // namespace detail
} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)