-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathdpctl_sycl_device_manager.cpp
More file actions
391 lines (347 loc) · 12.8 KB
/
Copy pathdpctl_sycl_device_manager.cpp
File metadata and controls
391 lines (347 loc) · 12.8 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//===-------- dpctl_sycl_device_manager.cpp - helpers for sycl devices ------=//
//
// Data Parallel Control (dpctl)
//
// Copyright 2020-2025 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file implements the functions declared in dpctl_sycl_device_manager.h.
///
//===----------------------------------------------------------------------===//
#include "dpctl_sycl_device_manager.h"
#include "dpctl_error_handlers.h"
#include "dpctl_string_utils.hpp"
#include "dpctl_sycl_enum_types.h"
#include "dpctl_sycl_type_casters.hpp"
#include "dpctl_utils_helper.h"
#include <Config/dpctl_config.h> /* Config */
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stddef.h>
#include <sycl/sycl.hpp> /* SYCL headers */
#include <unordered_map>
#include <utility>
#include <vector>
using namespace sycl;
namespace
{
static_assert(__SYCL_COMPILER_VERSION >= __SYCL_COMPILER_VERSION_REQUIRED,
"The compiler does not meet minimum version requirement");
using namespace dpctl::syclinterface;
/*
* Helper function to print the metadata for a sycl::device.
*/
std::string get_device_info_str(const device &Device)
{
std::stringstream ss;
static constexpr const char *_endl = "\n";
ss << std::setw(4) << " " << std::left << std::setw(16) << "Name"
<< Device.get_info<info::device::name>() << _endl << std::setw(4) << " "
<< std::left << std::setw(16) << "Driver version"
<< Device.get_info<info::device::driver_version>() << _endl
<< std::setw(4) << " " << std::left << std::setw(16) << "Vendor"
<< Device.get_info<info::device::vendor>() << _endl << std::setw(4)
<< " " << std::left << std::setw(16) << "Filter string"
<< DPCTL_GetDeviceFilterString(Device) << _endl;
return ss.str();
}
/*!
* @brief Canonicalizes a device identifier bit flag to have a valid (i.e., not
* UNKNOWN) backend and device type bits.
*
* The device id is bit flag that indicates the backend and device type, both
* of which are optional, that are to be queried. The function makes sure if a
* device identifier only provides a device type value the backend is set to
* DPCTL_ALL_BACKENDS. Similarly, if only backend is provided the device type
* is set to DPCTL_ALL.
*
* @param device_id A bit flag storing a backend and a device type value.
* @return Canonicalized bit flag that makes sure neither backend nor device
* type is UNKNOWN (0). For cases where the input device id does not provide
* either one of the values, we set the value to ALL.
*/
int to_canonical_device_id(int device_id)
{ // If the identifier is 0 (UNKNOWN_DEVICE) return 0.
if (!device_id)
return 0;
// Check if the device identifier has a backend specified. If not, then
// toggle all backend specifier bits, i.e. set the backend to
// DPCTL_ALL_BACKENDS.
if (!(device_id & DPCTL_ALL_BACKENDS))
device_id |= DPCTL_ALL_BACKENDS;
// Check if a device type was specified. If not, set device type to ALL.
if (!(device_id & ~DPCTL_ALL_BACKENDS))
device_id |= DPCTL_ALL;
return device_id;
}
struct DeviceCacheBuilder
{
using DeviceCache = std::unordered_map<device, context>;
/* This function implements a workaround to the current lack of a
* default context per root device in DPC++. The map stores a "default"
* context for each root device, and the QMgrHelper uses the map
* whenever it creates a new queue for a root device. By doing so, we
* avoid the performance overhead of context creation for every queue.
*
* The singleton pattern implemented here ensures that the map is
* created once in a thread-safe manner. Since, the map is only read
* post-creation we do not need any further protection to ensure
* thread-safety.
*/
static const DeviceCache &getDeviceCache()
{
static DeviceCache *cache = new DeviceCache([] {
DeviceCache cache_l{};
dpctl_default_selector mRanker;
std::vector<platform> Platforms{};
try {
Platforms = platform::get_platforms();
} catch (std::exception const &e) {
error_handler(e, __FILE__, __func__, __LINE__);
return cache_l;
}
for (const auto &P : Platforms) {
auto Devices = P.get_devices();
for (const auto &D : Devices) {
if (mRanker(D) < 0)
continue;
try {
// Per https://github.com/intel/llvm/blob/sycl/sycl/doc/
// extensions/supported/sycl_ext_oneapi_default_context.asciidoc
// sycl::queue(D) would create default platform context
// for capable compiler, sycl::context(D) otherwise
auto Q = queue(D);
auto Ctx = Q.get_context();
cache_l.emplace(D, Ctx);
} catch (const std::exception &e) {
// Nothing is added to the cache_l by guarantees of
// emplace
error_handler(e, __FILE__, __func__, __LINE__);
}
}
}
return cache_l;
}());
return *cache;
}
};
} // namespace
#undef EL
#undef EL_SYCL_TYPE
#define EL Device
#define EL_SYCL_TYPE sycl::device
#include "dpctl_vector_templ.cpp"
#undef EL
#undef EL_SYCL_TYPE
DPCTLSyclContextRef
DPCTLDeviceMgr_GetCachedContext(__dpctl_keep const DPCTLSyclDeviceRef DRef)
{
DPCTLSyclContextRef CRef = nullptr;
auto Device = unwrap<device>(DRef);
if (!Device) {
error_handler("Cannot create device from DPCTLSyclDeviceRef"
"as input is a nullptr.",
__FILE__, __func__, __LINE__);
return CRef;
}
using CacheT = typename DeviceCacheBuilder::DeviceCache;
CacheT const &cache = DeviceCacheBuilder::getDeviceCache();
if (cache.empty()) {
// an exception was caught and logged by getDeviceCache
return nullptr;
}
const auto &entry = cache.find(*Device);
if (entry != cache.end()) {
context *ContextPtr = nullptr;
try {
ContextPtr = new context(entry->second);
CRef = wrap<context>(ContextPtr);
} catch (std::exception const &e) {
error_handler(e, __FILE__, __func__, __LINE__);
delete ContextPtr;
CRef = nullptr;
}
}
else {
error_handler("No cached default context for device.", __FILE__,
__func__, __LINE__);
}
return CRef;
}
__dpctl_give DPCTLDeviceVectorRef
DPCTLDeviceMgr_GetDevices(int device_identifier)
{
using vecTy = std::vector<DPCTLSyclDeviceRef>;
vecTy *Devices = nullptr;
device_identifier = to_canonical_device_id(device_identifier);
try {
Devices = new std::vector<DPCTLSyclDeviceRef>();
} catch (std::exception const &e) {
delete Devices;
error_handler(e, __FILE__, __func__, __LINE__);
return nullptr;
}
if (!device_identifier)
return wrap<vecTy>(Devices);
std::vector<device> root_devices;
try {
root_devices = device::get_devices();
} catch (std::exception const &e) {
delete Devices;
error_handler(e, __FILE__, __func__, __LINE__);
return nullptr;
}
dpctl_default_selector mRanker;
for (const auto &root_device : root_devices) {
if (mRanker(root_device) < 0)
continue;
auto Bty(DPCTL_SyclBackendToDPCTLBackendType(
root_device.get_platform().get_backend()));
auto Dty(DPCTL_SyclDeviceTypeToDPCTLDeviceType(
root_device.get_info<info::device::device_type>()));
if ((device_identifier & Bty) && (device_identifier & Dty)) {
Devices->emplace_back(wrap<device>(new device(root_device)));
}
}
// the wrap function is defined inside dpctl_vector_templ.cpp
return wrap<vecTy>(Devices);
}
__dpctl_give const char *
DPCTLDeviceMgr_GetDeviceInfoStr(__dpctl_keep const DPCTLSyclDeviceRef DRef)
{
const char *cstr_info = nullptr;
auto D = unwrap<device>(DRef);
if (D) {
try {
auto infostr = get_device_info_str(*D);
cstr_info = dpctl::helper::cstring_from_string(infostr);
} catch (std::exception const &e) {
error_handler(e, __FILE__, __func__, __LINE__);
}
}
return cstr_info;
}
int DPCTLDeviceMgr_GetPositionInDevices(__dpctl_keep DPCTLSyclDeviceRef DRef,
int device_identifier)
{
static constexpr int not_found = -1;
if (!DRef) {
return not_found;
}
device_identifier = to_canonical_device_id(device_identifier);
if (!device_identifier)
return not_found;
const auto &root_devices = device::get_devices();
dpctl_default_selector mRanker;
int index = not_found;
const auto &reference_device = *(unwrap<device>(DRef));
for (const auto &root_device : root_devices) {
if (mRanker(root_device) < 0)
continue;
auto Bty(DPCTL_SyclBackendToDPCTLBackendType(
root_device.get_platform().get_backend()));
auto Dty(DPCTL_SyclDeviceTypeToDPCTLDeviceType(
root_device.get_info<info::device::device_type>()));
if ((device_identifier & Bty) && (device_identifier & Dty)) {
++index;
if (root_device == reference_device)
return index;
}
}
return not_found;
}
/*!
* Returns the number of available devices for a specific backend and device
* type combination.
*/
size_t DPCTLDeviceMgr_GetNumDevices(int device_identifier)
{
size_t nDevices = 0;
using CacheT = typename DeviceCacheBuilder::DeviceCache;
CacheT const &cache = DeviceCacheBuilder::getDeviceCache();
if (cache.empty()) {
// an exception was caught and logged by getDeviceCache
return 0;
}
device_identifier = to_canonical_device_id(device_identifier);
if (!device_identifier)
return 0;
dpctl_default_selector mRanker;
for (const auto &entry : cache) {
if (mRanker(entry.first) < 0)
continue;
auto Bty(DPCTL_SyclBackendToDPCTLBackendType(
entry.first.get_platform().get_backend()));
auto Dty(DPCTL_SyclDeviceTypeToDPCTLDeviceType(
entry.first.get_info<info::device::device_type>()));
if ((device_identifier & Bty) && (device_identifier & Dty))
++nDevices;
}
return nDevices;
}
/*!
* Prints some of the device info metadata for the device corresponding to the
* specified sycl::queue. Currently, device name, driver version, device
* vendor, and device profile are printed out. More attributed may be added
* later.
*/
void DPCTLDeviceMgr_PrintDeviceInfo(__dpctl_keep const DPCTLSyclDeviceRef DRef)
{
auto Device = unwrap<device>(DRef);
if (Device)
std::cout << get_device_info_str(*Device);
else
error_handler("Device is not valid (NULL). Cannot print device info.",
__FILE__, __func__, __LINE__);
}
int64_t DPCTLDeviceMgr_GetRelativeId(__dpctl_keep const DPCTLSyclDeviceRef DRef)
{
auto Device = unwrap<device>(DRef);
if (Device)
return DPCTL_GetRelativeDeviceId(*Device);
return -1;
}
/*!
* Returns a list of the available composite devices, or an empty list if
* there are none.
*/
__dpctl_give DPCTLDeviceVectorRef DPCTLDeviceMgr_GetCompositeDevices()
{
using vecTy = std::vector<DPCTLSyclDeviceRef>;
vecTy *Devices = nullptr;
try {
Devices = new std::vector<DPCTLSyclDeviceRef>();
} catch (std::exception const &e) {
delete Devices;
error_handler(e, __FILE__, __func__, __LINE__);
return nullptr;
}
try {
auto composite_devices =
ext::oneapi::experimental::get_composite_devices();
Devices->reserve(composite_devices.size());
for (const auto &CDev : composite_devices) {
Devices->emplace_back(wrap<device>(new device(std::move(CDev))));
}
return wrap<vecTy>(Devices);
} catch (std::exception const &e) {
delete Devices;
error_handler(e, __FILE__, __func__, __LINE__);
return nullptr;
}
}