• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IntelPython / dpctl / 12795702230

15 Jan 2025 07:34PM UTC coverage: 87.721% (+0.04%) from 87.683%
12795702230

Pull #1953

github

web-flow
Merge b1a5ecd98 into 25a53f594
Pull Request #1953: Improve interoperability between `SyclDevice` and DLPack devices

3159 of 3686 branches covered (85.7%)

Branch coverage included in aggregate %.

38 of 48 new or added lines in 5 files covered. (79.17%)

1 existing line in 1 file now uncovered.

11915 of 13498 relevant lines covered (88.27%)

7162.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.15
/libsyclinterface/helper/source/dpctl_utils_helper.cpp
1
//===- dpctl_utils_helper.cpp - Implementation of enum to string helpers   ===//
2
//
3
//                      Data Parallel Control (dpctl)
4
//
5
// Copyright 2020-2025 Intel Corporation
6
//
7
// Licensed under the Apache License, Version 2.0 (the "License");
8
// you may not use this file except in compliance with the License.
9
// You may obtain a copy of the License at
10
//
11
//    http://www.apache.org/licenses/LICENSE-2.0
12
//
13
// Unless required by applicable law or agreed to in writing, software
14
// distributed under the License is distributed on an "AS IS" BASIS,
15
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
// See the License for the specific language governing permissions and
17
// limitations under the License.
18
//
19
//===----------------------------------------------------------------------===//
20
///
21
/// \file
22
/// This file implements the helper functions defined in dpctl_utils_helper.h.
23
///
24
//===----------------------------------------------------------------------===//
25

26
#include "dpctl_utils_helper.h"
27
#include "Config/dpctl_config.h"
28
#include <sstream>
29
#include <string>
30

31
using namespace sycl;
32

33
/*!
34
 * Transforms enum info::device_type to string.
35
 */
36
std::string DPCTL_DeviceTypeToStr(info::device_type devTy)
37
{
32✔
38
    std::stringstream ss;
32✔
39
    switch (devTy) {
32✔
40
    case info::device_type::cpu:
28✔
41
        ss << "cpu";
28✔
42
        break;
28✔
43
    case info::device_type::gpu:
1✔
44
        ss << "gpu";
1✔
45
        break;
1✔
46
    case info::device_type::accelerator:
1✔
47
        ss << "accelerator";
1✔
48
        break;
1✔
49
    case info::device_type::custom:
1✔
50
        ss << "custom";
1✔
51
        break;
1✔
52
    default:
1✔
53
        ss << "unknown";
1✔
54
    }
32✔
55
    return ss.str();
32✔
56
}
32✔
57

58
/*!
59
 * Transforms string to enum info::device_type.
60
 */
61
info::device_type DPCTL_StrToDeviceType(const std::string &devTyStr)
62
{
5✔
63
    info::device_type devTy;
5✔
64
    if (devTyStr == "cpu") {
5✔
65
        devTy = info::device_type::cpu;
1✔
66
    }
1✔
67
    else if (devTyStr == "gpu") {
4✔
68
        devTy = info::device_type::gpu;
1✔
69
    }
1✔
70
    else if (devTyStr == "accelerator") {
3✔
71
        devTy = info::device_type::accelerator;
1✔
72
    }
1✔
73
    else if (devTyStr == "custom") {
2✔
74
        devTy = info::device_type::custom;
1✔
75
    }
1✔
76
    else if (devTyStr == "host") {
1!
77
        devTy = info::device_type::host;
×
78
    }
×
79
    else {
1✔
80
        // \todo handle the error
81
        throw std::runtime_error("Unknown device type.");
1✔
82
    }
1✔
83
    return devTy;
4✔
84
}
5✔
85

86
backend DPCTL_DPCTLBackendTypeToSyclBackend(DPCTLSyclBackendType BeTy)
87
{
10✔
88
    switch (BeTy) {
10✔
89
    case DPCTLSyclBackendType::DPCTL_CUDA:
2✔
90
        return backend::ext_oneapi_cuda;
2✔
91
    case DPCTLSyclBackendType::DPCTL_LEVEL_ZERO:
2✔
92
        return backend::ext_oneapi_level_zero;
2✔
93
    case DPCTLSyclBackendType::DPCTL_OPENCL:
2✔
94
        return backend::opencl;
2✔
95
    case DPCTLSyclBackendType::DPCTL_ALL_BACKENDS:
1✔
96
        return backend::all;
1✔
97
    case DPCTLSyclBackendType::DPCTL_HIP:
2✔
98
        return backend::ext_oneapi_hip;
2✔
99
    default:
1✔
100
        throw std::runtime_error("Unsupported backend type");
1✔
101
    }
10✔
102
}
10✔
103

104
DPCTLSyclBackendType DPCTL_SyclBackendToDPCTLBackendType(backend B)
105
{
1,731✔
106
    switch (B) {
1,731✔
107
    case backend::ext_oneapi_cuda:
1✔
108
        return DPCTLSyclBackendType::DPCTL_CUDA;
1✔
109
    case backend::ext_oneapi_level_zero:
1✔
110
        return DPCTLSyclBackendType::DPCTL_LEVEL_ZERO;
1✔
111
    case backend::opencl:
1,727✔
112
        return DPCTLSyclBackendType::DPCTL_OPENCL;
1,727✔
113
    case backend::ext_oneapi_hip:
1✔
114
        return DPCTLSyclBackendType::DPCTL_HIP;
1✔
115
    default:
1✔
116
        return DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND;
1✔
117
    }
1,731✔
118
}
1,731✔
119

120
info::device_type DPCTL_DPCTLDeviceTypeToSyclDeviceType(DPCTLSyclDeviceType DTy)
121
{
11✔
122
    switch (DTy) {
11✔
123
    case DPCTLSyclDeviceType::DPCTL_ACCELERATOR:
2✔
124
        return info::device_type::accelerator;
2✔
125
    case DPCTLSyclDeviceType::DPCTL_ALL:
2✔
126
        return info::device_type::all;
2✔
127
    case DPCTLSyclDeviceType::DPCTL_AUTOMATIC:
1✔
128
        return info::device_type::automatic;
1✔
129
    case DPCTLSyclDeviceType::DPCTL_CPU:
3✔
130
        return info::device_type::cpu;
3✔
131
    case DPCTLSyclDeviceType::DPCTL_CUSTOM:
1✔
132
        return info::device_type::custom;
1✔
133
    case DPCTLSyclDeviceType::DPCTL_GPU:
2✔
134
        return info::device_type::gpu;
2✔
135
    default:
×
136
        throw std::runtime_error("Unsupported device type");
×
137
    }
11✔
138
}
11✔
139

140
DPCTLSyclDeviceType DPCTL_SyclDeviceTypeToDPCTLDeviceType(info::device_type D)
141
{
1,715✔
142
    switch (D) {
1,715✔
143
    case info::device_type::accelerator:
1✔
144
        return DPCTLSyclDeviceType::DPCTL_ACCELERATOR;
1✔
145
    case info::device_type::all:
1✔
146
        return DPCTLSyclDeviceType::DPCTL_ALL;
1✔
147
    case info::device_type::automatic:
1✔
148
        return DPCTLSyclDeviceType::DPCTL_AUTOMATIC;
1✔
149
    case info::device_type::cpu:
1,710✔
150
        return DPCTLSyclDeviceType::DPCTL_CPU;
1,710✔
151
    case info::device_type::custom:
1✔
152
        return DPCTLSyclDeviceType::DPCTL_CUSTOM;
1✔
153
    case info::device_type::gpu:
1✔
154
        return DPCTLSyclDeviceType::DPCTL_GPU;
1✔
155
    default:
×
156
        return DPCTLSyclDeviceType::DPCTL_UNKNOWN_DEVICE;
×
157
    }
1,715✔
158
}
1,715✔
159

160
/*!
161
 * Transforms sycl::aspect to string.
162
 */
163
std::string DPCTL_AspectToStr(aspect aspectTy)
164
{
×
165
    std::stringstream ss;
×
166
    switch (aspectTy) {
×
167
    case aspect::cpu:
×
168
        ss << "cpu";
×
169
        break;
×
170
    case aspect::gpu:
×
171
        ss << "gpu";
×
172
        break;
×
173
    case aspect::accelerator:
×
174
        ss << "accelerator";
×
175
        break;
×
176
    case aspect::custom:
×
177
        ss << "custom";
×
178
        break;
×
179
    case aspect::fp16:
×
180
        ss << "fp16";
×
181
        break;
×
182
    case aspect::fp64:
×
183
        ss << "fp64";
×
184
        break;
×
185
    case aspect::atomic64:
×
186
        ss << "atomic64";
×
187
        break;
×
188
    case aspect::image:
×
189
        ss << "image";
×
190
        break;
×
191
    case aspect::online_compiler:
×
192
        ss << "online_compiler";
×
193
        break;
×
194
    case aspect::online_linker:
×
195
        ss << "online_linker";
×
196
        break;
×
197
    case aspect::queue_profiling:
×
198
        ss << "queue_profiling";
×
199
        break;
×
200
    case aspect::usm_device_allocations:
×
201
        ss << "usm_device_allocations";
×
202
        break;
×
203
    case aspect::usm_host_allocations:
×
204
        ss << "usm_host_allocations";
×
205
        break;
×
206
    case aspect::usm_shared_allocations:
×
207
        ss << "usm_shared_allocations";
×
208
        break;
×
209
    case aspect::usm_system_allocations:
×
210
        ss << "usm_system_allocations";
×
211
        break;
×
212
    case aspect::usm_atomic_host_allocations:
×
213
        ss << "usm_atomic_host_allocations";
×
214
        break;
×
215
    case aspect::usm_atomic_shared_allocations:
×
216
        ss << "usm_atomic_shared_allocations";
×
217
        break;
×
218
    case aspect::host_debuggable:
×
219
        ss << "host_debuggable";
×
220
        break;
×
221
    case aspect::emulated:
×
222
        ss << "emulated";
×
223
        break;
×
224
    default:
×
225
        throw std::runtime_error("Unsupported aspect type");
×
226
    }
×
227
    return ss.str();
×
228
}
×
229

230
/*!
231
 * Transforms string to sycl::aspect.
232
 */
233
aspect DPCTL_StrToAspectType(const std::string &aspectTyStr)
234
{
107✔
235
    aspect aspectTy;
107✔
236
    if (aspectTyStr == "cpu") {
107✔
237
        aspectTy = aspect::cpu;
1✔
238
    }
1✔
239
    else if (aspectTyStr == "gpu") {
106✔
240
        aspectTy = aspect::gpu;
1✔
241
    }
1✔
242
    else if (aspectTyStr == "accelerator") {
105✔
243
        aspectTy = aspect::accelerator;
1✔
244
    }
1✔
245
    else if (aspectTyStr == "custom") {
104✔
246
        aspectTy = aspect::custom;
1✔
247
    }
1✔
248
    else if (aspectTyStr == "fp16") {
103✔
249
        aspectTy = aspect::fp16;
17✔
250
    }
17✔
251
    else if (aspectTyStr == "fp64") {
86✔
252
        aspectTy = aspect::fp64;
18✔
253
    }
18✔
254
    else if (aspectTyStr == "atomic64") {
68✔
255
        aspectTy = aspect::atomic64;
1✔
256
    }
1✔
257
    else if (aspectTyStr == "image") {
67✔
258
        aspectTy = aspect::image;
56✔
259
    }
56✔
260
    else if (aspectTyStr == "online_compiler") {
11✔
261
        aspectTy = aspect::online_compiler;
1✔
262
    }
1✔
263
    else if (aspectTyStr == "online_linker") {
10✔
264
        aspectTy = aspect::online_linker;
1✔
265
    }
1✔
266
    else if (aspectTyStr == "queue_profiling") {
9✔
267
        aspectTy = aspect::queue_profiling;
1✔
268
    }
1✔
269
    else if (aspectTyStr == "usm_device_allocations") {
8✔
270
        aspectTy = aspect::usm_device_allocations;
1✔
271
    }
1✔
272
    else if (aspectTyStr == "usm_host_allocations") {
7✔
273
        aspectTy = aspect::usm_host_allocations;
1✔
274
    }
1✔
275
    else if (aspectTyStr == "usm_shared_allocations") {
6✔
276
        aspectTy = aspect::usm_shared_allocations;
1✔
277
    }
1✔
278
    else if (aspectTyStr == "usm_system_allocations") {
5✔
279
        aspectTy = aspect::usm_system_allocations;
1✔
280
    }
1✔
281
    else if (aspectTyStr == "usm_atomic_host_allocations") {
4✔
282
        aspectTy = aspect::usm_atomic_host_allocations;
1✔
283
    }
1✔
284
    else if (aspectTyStr == "usm_atomic_shared_allocations") {
3✔
285
        aspectTy = aspect::usm_atomic_shared_allocations;
1✔
286
    }
1✔
287
    else if (aspectTyStr == "host_debuggable") {
2✔
288
        aspectTy = aspect::host_debuggable;
1✔
289
    }
1✔
290
    else if (aspectTyStr == "emulated") {
1!
291
        aspectTy = aspect::emulated;
1✔
292
    }
1✔
293
    else {
×
294
        // \todo handle the error
295
        throw std::runtime_error("Unsupported aspect type");
×
296
    }
×
297
    return aspectTy;
107✔
298
}
107✔
299

300
aspect DPCTL_DPCTLAspectTypeToSyclAspect(DPCTLSyclAspectType AspectTy)
301
{
374,336✔
302
    switch (AspectTy) {
374,336✔
303
    case DPCTLSyclAspectType::cpu:
21✔
304
        return aspect::cpu;
21✔
305
    case DPCTLSyclAspectType::gpu:
19✔
306
        return aspect::gpu;
19✔
307
    case DPCTLSyclAspectType::accelerator:
18✔
308
        return aspect::accelerator;
18✔
309
    case DPCTLSyclAspectType::custom:
18✔
310
        return aspect::custom;
18✔
311
    case DPCTLSyclAspectType::fp16:
123,345✔
312
        return aspect::fp16;
123,345✔
313
    case DPCTLSyclAspectType::fp64:
250,616✔
314
        return aspect::fp64;
250,616✔
315
    case DPCTLSyclAspectType::atomic64:
19✔
316
        return aspect::atomic64;
19✔
317
    case DPCTLSyclAspectType::image:
73✔
318
        return aspect::image;
73✔
319
    case DPCTLSyclAspectType::online_compiler:
19✔
320
        return aspect::online_compiler;
19✔
321
    case DPCTLSyclAspectType::online_linker:
19✔
322
        return aspect::online_linker;
19✔
323
    case DPCTLSyclAspectType::queue_profiling:
19✔
324
        return aspect::queue_profiling;
19✔
325
    case DPCTLSyclAspectType::usm_device_allocations:
19✔
326
        return aspect::usm_device_allocations;
19✔
327
    case DPCTLSyclAspectType::usm_host_allocations:
19✔
328
        return aspect::usm_host_allocations;
19✔
329
    case DPCTLSyclAspectType::usm_shared_allocations:
19✔
330
        return aspect::usm_shared_allocations;
19✔
331
    case DPCTLSyclAspectType::usm_system_allocations:
19✔
332
        return aspect::usm_system_allocations;
19✔
333
    case DPCTLSyclAspectType::usm_atomic_host_allocations:
19✔
334
        return aspect::usm_atomic_host_allocations;
19✔
335
    case DPCTLSyclAspectType::usm_atomic_shared_allocations:
19✔
336
        return aspect::usm_atomic_shared_allocations;
19✔
337
    case DPCTLSyclAspectType::host_debuggable:
18✔
338
        return aspect::host_debuggable;
18✔
339
    case DPCTLSyclAspectType::emulated:
18✔
340
        return aspect::emulated;
18✔
341
    default:
×
342
        throw std::runtime_error("Unsupported aspect type");
×
343
    }
374,336✔
344
}
374,336✔
345

346
DPCTLSyclAspectType DPCTL_SyclAspectToDPCTLAspectType(aspect Aspect)
347
{
107✔
348
    switch (Aspect) {
107✔
349
    case aspect::cpu:
1✔
350
        return DPCTLSyclAspectType::cpu;
1✔
351
    case aspect::gpu:
1✔
352
        return DPCTLSyclAspectType::gpu;
1✔
353
    case aspect::accelerator:
1✔
354
        return DPCTLSyclAspectType::accelerator;
1✔
355
    case aspect::custom:
1✔
356
        return DPCTLSyclAspectType::custom;
1✔
357
    case aspect::fp16:
17✔
358
        return DPCTLSyclAspectType::fp16;
17✔
359
    case aspect::fp64:
18✔
360
        return DPCTLSyclAspectType::fp64;
18✔
361
    case aspect::atomic64:
1✔
362
        return DPCTLSyclAspectType::atomic64;
1✔
363
    case aspect::image:
56✔
364
        return DPCTLSyclAspectType::image;
56✔
365
    case aspect::online_compiler:
1✔
366
        return DPCTLSyclAspectType::online_compiler;
1✔
367
    case aspect::online_linker:
1✔
368
        return DPCTLSyclAspectType::online_linker;
1✔
369
    case aspect::queue_profiling:
1✔
370
        return DPCTLSyclAspectType::queue_profiling;
1✔
371
    case aspect::usm_device_allocations:
1✔
372
        return DPCTLSyclAspectType::usm_device_allocations;
1✔
373
    case aspect::usm_host_allocations:
1✔
374
        return DPCTLSyclAspectType::usm_host_allocations;
1✔
375
    case aspect::usm_shared_allocations:
1✔
376
        return DPCTLSyclAspectType::usm_shared_allocations;
1✔
377
    case aspect::usm_system_allocations:
1✔
378
        return DPCTLSyclAspectType::usm_system_allocations;
1✔
379
    case aspect::usm_atomic_host_allocations:
1✔
380
        return DPCTLSyclAspectType::usm_atomic_host_allocations;
1✔
381
    case aspect::usm_atomic_shared_allocations:
1✔
382
        return DPCTLSyclAspectType::usm_atomic_shared_allocations;
1✔
383
    case aspect::host_debuggable:
1✔
384
        return DPCTLSyclAspectType::host_debuggable;
1✔
385
    case aspect::emulated:
1✔
386
        return DPCTLSyclAspectType::emulated;
1✔
387
    default:
×
388
        throw std::runtime_error("Unsupported aspect type");
×
389
    }
107✔
390
}
107✔
391

392
info::partition_affinity_domain DPCTL_DPCTLPartitionAffinityDomainTypeToSycl(
393
    DPCTLPartitionAffinityDomainType PartitionAffinityDomainTy)
394
{
×
395
    switch (PartitionAffinityDomainTy) {
×
396
    case DPCTLPartitionAffinityDomainType::not_applicable:
×
397
        return info::partition_affinity_domain::not_applicable;
×
398
    case DPCTLPartitionAffinityDomainType::numa:
×
399
        return info::partition_affinity_domain::numa;
×
400
    case DPCTLPartitionAffinityDomainType::L4_cache:
×
401
        return info::partition_affinity_domain::L4_cache;
×
402
    case DPCTLPartitionAffinityDomainType::L3_cache:
×
403
        return info::partition_affinity_domain::L3_cache;
×
404
    case DPCTLPartitionAffinityDomainType::L2_cache:
×
405
        return info::partition_affinity_domain::L2_cache;
×
406
    case DPCTLPartitionAffinityDomainType::L1_cache:
×
407
        return info::partition_affinity_domain::L1_cache;
×
408
    case DPCTLPartitionAffinityDomainType::next_partitionable:
×
409
        return info::partition_affinity_domain::next_partitionable;
×
410
    default:
×
411
        throw std::runtime_error("Unsupported partition_affinity_domain type");
×
412
    }
×
413
}
×
414

415
DPCTLPartitionAffinityDomainType DPCTL_SyclPartitionAffinityDomainToDPCTLType(
416
    sycl::info::partition_affinity_domain PartitionAffinityDomain)
417
{
56✔
418
    switch (PartitionAffinityDomain) {
56✔
419
    case info::partition_affinity_domain::not_applicable:
8✔
420
        return DPCTLPartitionAffinityDomainType::not_applicable;
8✔
421
    case info::partition_affinity_domain::numa:
8✔
422
        return DPCTLPartitionAffinityDomainType::numa;
8✔
423
    case info::partition_affinity_domain::L4_cache:
8✔
424
        return DPCTLPartitionAffinityDomainType::L4_cache;
8✔
425
    case info::partition_affinity_domain::L3_cache:
8✔
426
        return DPCTLPartitionAffinityDomainType::L3_cache;
8✔
427
    case info::partition_affinity_domain::L2_cache:
8✔
428
        return DPCTLPartitionAffinityDomainType::L2_cache;
8✔
429
    case info::partition_affinity_domain::L1_cache:
8✔
430
        return DPCTLPartitionAffinityDomainType::L1_cache;
8✔
431
    case info::partition_affinity_domain::next_partitionable:
8✔
432
        return DPCTLPartitionAffinityDomainType::next_partitionable;
8✔
433
    default:
×
434
        throw std::runtime_error("Unsupported partition_affinity_domain type");
×
435
    }
56✔
436
}
56✔
437

438
int64_t DPCTL_GetRelativeDeviceId(const device &Device)
439
{
35✔
440
    auto relid = -1;
35✔
441
    auto p = Device.get_platform();
35✔
442
    auto be = p.get_backend();
35✔
443
    auto dt = Device.get_info<sycl::info::device::device_type>();
35✔
444
    auto dev_vec = device::get_devices(dt);
35✔
445
    int64_t id = 0;
35✔
446
    for (const auto &d_i : dev_vec) {
35!
447
        if (Device == d_i) {
35!
448
            relid = id;
35✔
449
            break;
35✔
450
        }
35✔
451
        if (d_i.get_platform().get_backend() == be)
×
452
            ++id;
×
453
    }
×
454
    return relid;
35✔
455
}
35✔
456

457
std::string DPCTL_GetDeviceFilterString(const device &Device)
458
{
26✔
459
    std::stringstream ss;
26✔
460
    static constexpr const char *filter_string_separator = ":";
26✔
461

462
    auto be = Device.get_platform().get_backend();
26✔
463

464
    switch (be) {
26✔
465
    case backend::ext_oneapi_level_zero:
×
466
        ss << "level_zero";
×
467
        break;
×
468
    case backend::ext_oneapi_cuda:
×
469
        ss << "cuda";
×
470
        break;
×
471
    case backend::opencl:
26!
472
        ss << "opencl";
26✔
473
        break;
26✔
474
    case backend::ext_oneapi_hip:
×
475
        ss << "hip";
×
476
        break;
×
477
    default:
×
478
        ss << "unknown";
×
479
    };
26✔
480

481
    ss << filter_string_separator;
26✔
482
    ss << DPCTL_DeviceTypeToStr(Device.get_info<info::device::device_type>());
26✔
483
    ss << filter_string_separator;
26✔
484
    ss << DPCTL_GetRelativeDeviceId(Device);
26✔
485

486
    return ss.str();
26✔
487
}
26✔
488

489
DPCTLSyclEventStatusType
490
DPCTL_SyclEventStatusToDPCTLEventStatusType(info::event_command_status E)
491
{
6✔
492
    switch (E) {
6✔
493
    case info::event_command_status::submitted:
2✔
494
        return DPCTLSyclEventStatusType::DPCTL_SUBMITTED;
2✔
495
    case info::event_command_status::running:
1✔
496
        return DPCTLSyclEventStatusType::DPCTL_RUNNING;
1✔
497
    case info::event_command_status::complete:
3✔
498
        return DPCTLSyclEventStatusType::DPCTL_COMPLETE;
3✔
499
    default:
×
500
        return DPCTLSyclEventStatusType::DPCTL_UNKNOWN_STATUS;
×
501
    }
6✔
502
}
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc