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

IntelPython / dpctl / 25077376641

28 Apr 2026 08:59PM UTC coverage: 75.476%. Remained the same
25077376641

push

github

web-flow
Merge pull request #2303 from IntelPython/remove-platform-get-devices-work-around

Remove work-around in `dpctl.SyclPlatform.get_devices` for `custom` and `automatic` device types

824 of 1144 branches covered (72.03%)

Branch coverage included in aggregate %.

3183 of 4165 relevant lines covered (76.42%)

273.15 hits per line

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

78.7
/libsyclinterface/source/dpctl_sycl_platform_interface.cpp
1
//=== dpctl_sycl_platform_interface.cpp - Implements C API for sycl::platform //
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 functions declared in
23
/// dpctl_sycl_platform_interface.h.
24
///
25
//===----------------------------------------------------------------------===//
26

27
#include "dpctl_sycl_platform_interface.h"
28
#include "Config/dpctl_config.h"
29
#include "dpctl_device_selection.hpp"
30
#include "dpctl_error_handlers.h"
31
#include "dpctl_string_utils.hpp"
32
#include "dpctl_sycl_enum_types.h"
33
#include "dpctl_sycl_type_casters.hpp"
34
#include "dpctl_utils_helper.h"
35
#include <iomanip>
36
#include <iostream>
37
#include <set>
38
#include <sstream>
39
#include <stddef.h>
40
#include <sycl/sycl.hpp>
41
#include <utility>
42
#include <vector>
43

44
using namespace sycl;
45

46
namespace
47
{
48
static_assert(__SYCL_COMPILER_VERSION >= __SYCL_COMPILER_VERSION_REQUIRED,
49
              "The compiler does not meet minimum version requirement");
50

51
using namespace dpctl::syclinterface;
52

53
platform *new_platform_from_selector(const dpctl_device_selector *sel)
54
{
332✔
55
    return new platform(
332✔
56
        [=](const device &d) -> int { return sel->operator()(d); });
332✔
57
}
332✔
58

59
} // end of anonymous namespace
60

61
__dpctl_give DPCTLSyclPlatformRef
62
DPCTLPlatform_Copy(__dpctl_keep const DPCTLSyclPlatformRef PRef)
63
{
45✔
64
    auto Platform = unwrap<platform>(PRef);
45✔
65
    if (!Platform) {
45✔
66
        error_handler("Cannot copy DPCTLSyclPlatformRef as input is a nullptr.",
5✔
67
                      __FILE__, __func__, __LINE__);
5✔
68
        return nullptr;
5✔
69
    }
5✔
70
    try {
40✔
71
        auto CopiedPlatform = new platform(*Platform);
40✔
72
        return wrap<platform>(CopiedPlatform);
40✔
73
    } catch (std::exception const &e) {
40✔
74
        error_handler(e, __FILE__, __func__, __LINE__);
×
75
        return nullptr;
×
76
    }
×
77
}
40✔
78

79
__dpctl_give DPCTLSyclPlatformRef DPCTLPlatform_Create()
80
{
22✔
81
    DPCTLSyclPlatformRef PRef = nullptr;
22✔
82
    try {
22✔
83
        auto P = new platform();
22✔
84
        PRef = wrap<platform>(P);
22✔
85
    } catch (std::exception const &e) {
22✔
86
        error_handler(e, __FILE__, __func__, __LINE__);
×
87
    }
×
88
    return PRef;
22✔
89
}
22✔
90

91
__dpctl_give DPCTLSyclPlatformRef DPCTLPlatform_CreateFromSelector(
92
    __dpctl_keep const DPCTLSyclDeviceSelectorRef DSRef)
93
{
360✔
94
    if (DSRef) {
360✔
95
        auto DS = unwrap<dpctl_device_selector>(DSRef);
332✔
96
        platform *P = nullptr;
332✔
97
        try {
332✔
98
            P = new_platform_from_selector(DS);
332✔
99
            return wrap<platform>(P);
332✔
100
        } catch (std::exception const &e) {
332✔
101
            delete P;
240✔
102
            error_handler(e, __FILE__, __func__, __LINE__);
240✔
103
            return nullptr;
240✔
104
        }
240✔
105
    }
332✔
106
    else {
28✔
107
        error_handler("Device selector pointer cannot be NULL.", __FILE__,
28✔
108
                      __func__, __LINE__);
28✔
109
    }
28✔
110

111
    return nullptr;
28✔
112
}
360✔
113

114
void DPCTLPlatform_Delete(__dpctl_take DPCTLSyclPlatformRef PRef)
115
{
476✔
116
    auto P = unwrap<platform>(PRef);
476✔
117
    delete P;
476✔
118
}
476✔
119

120
DPCTLSyclBackendType
121
DPCTLPlatform_GetBackend(__dpctl_keep const DPCTLSyclPlatformRef PRef)
122
{
13✔
123
    DPCTLSyclBackendType BTy = DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND;
13✔
124
    auto P = unwrap<platform>(PRef);
13✔
125
    if (P) {
13✔
126
        BTy = DPCTL_SyclBackendToDPCTLBackendType(P->get_backend());
12✔
127
    }
12✔
128
    else {
1✔
129
        error_handler("Backend cannot be looked up for a NULL platform.",
1✔
130
                      __FILE__, __func__, __LINE__);
1✔
131
    }
1✔
132
    return BTy;
13✔
133
}
13✔
134

135
__dpctl_give const char *
136
DPCTLPlatform_GetName(__dpctl_keep const DPCTLSyclPlatformRef PRef)
137
{
116✔
138
    auto P = unwrap<platform>(PRef);
116✔
139
    if (P) {
116✔
140
        try {
115✔
141
            auto name = P->get_info<info::platform::name>();
115✔
142
            return dpctl::helper::cstring_from_string(name);
115✔
143
        } catch (std::exception const &e) {
115✔
144
            error_handler(e, __FILE__, __func__, __LINE__);
×
145
            return nullptr;
×
146
        }
×
147
    }
115✔
148
    else {
1✔
149
        error_handler("Name cannot be looked up for a NULL platform.", __FILE__,
1✔
150
                      __func__, __LINE__);
1✔
151
        return nullptr;
1✔
152
    }
1✔
153
}
116✔
154

155
__dpctl_give const char *
156
DPCTLPlatform_GetVendor(__dpctl_keep const DPCTLSyclPlatformRef PRef)
157
{
116✔
158
    auto P = unwrap<platform>(PRef);
116✔
159
    if (P) {
116✔
160
        try {
115✔
161
            auto vendor = P->get_info<info::platform::vendor>();
115✔
162
            return dpctl::helper::cstring_from_string(vendor);
115✔
163
        } catch (std::exception const &e) {
115✔
164
            error_handler(e, __FILE__, __func__, __LINE__);
×
165
            return nullptr;
×
166
        }
×
167
    }
115✔
168
    else {
1✔
169
        error_handler("Vendor cannot be looked up for a NULL platform.",
1✔
170
                      __FILE__, __func__, __LINE__);
1✔
171
        return nullptr;
1✔
172
    }
1✔
173
}
116✔
174

175
__dpctl_give const char *
176
DPCTLPlatform_GetVersion(__dpctl_keep const DPCTLSyclPlatformRef PRef)
177
{
116✔
178
    auto P = unwrap<platform>(PRef);
116✔
179
    if (P) {
116✔
180
        try {
115✔
181
            auto driver = P->get_info<info::platform::version>();
115✔
182
            return dpctl::helper::cstring_from_string(driver);
115✔
183
        } catch (std::exception const &e) {
115✔
184
            error_handler(e, __FILE__, __func__, __LINE__);
×
185
            return nullptr;
×
186
        }
×
187
    }
115✔
188
    else {
1✔
189
        error_handler("Driver version cannot be looked up for a NULL platform.",
1✔
190
                      __FILE__, __func__, __LINE__);
1✔
191
        return nullptr;
1✔
192
    }
1✔
193
}
116✔
194

195
__dpctl_give DPCTLPlatformVectorRef DPCTLPlatform_GetPlatforms()
196
{
18✔
197
    using vecTy = std::vector<DPCTLSyclPlatformRef>;
18✔
198
    vecTy *Platforms = nullptr;
18✔
199

200
    std::vector<platform> platforms;
18✔
201
    try {
18✔
202
        platforms = platform::get_platforms();
18✔
203
    } catch (std::exception const &e) {
18✔
204
        error_handler(e, __FILE__, __func__, __LINE__);
×
205
        return nullptr;
×
206
    }
×
207

208
    try {
18✔
209
        Platforms = new vecTy();
18✔
210
        Platforms->reserve(platforms.size());
18✔
211
    } catch (std::exception const &e) {
18✔
212
        error_handler(e, __FILE__, __func__, __LINE__);
×
213
        return nullptr;
×
214
    }
×
215

216
    // populate the vector
217
    for (const auto &P : platforms) {
18✔
218
        Platforms->emplace_back(wrap<platform>(new platform(P)));
18✔
219
    }
18✔
220

221
    // the wrap function is defined inside dpctl_vector_templ.cpp
222
    return wrap<vecTy>(Platforms);
18✔
223
}
18✔
224

225
__dpctl_give DPCTLSyclContextRef
226
DPCTLPlatform_GetDefaultContext(__dpctl_keep const DPCTLSyclPlatformRef PRef)
227
{
15✔
228
    auto P = unwrap<platform>(PRef);
15✔
229
    if (P) {
15✔
230
#if defined(SYCL_KHR_DEFAULT_CONTEXT) ||                                       \
14✔
231
    defined(SYCL_EXT_ONEAPI_DEFAULT_CONTEXT)
14✔
232
        try {
14✔
233
            const auto &default_ctx = P->
14✔
234
#ifdef SYCL_KHR_DEFAULT_CONTEXT
14✔
235
                                      khr_get_default_context()
14✔
236
#else
237
                                      ext_oneapi_get_default_context()
238
#endif // SYCL_KHR_DEFAULT_CONTEXT
239
                ;
14✔
240
            return wrap<context>(new context(default_ctx));
14✔
241
        } catch (const std::exception &ex) {
14✔
242
            error_handler(ex, __FILE__, __func__, __LINE__);
×
243
            return nullptr;
×
244
        }
×
245
#else
246
        return nullptr;
247
#endif // SYCL_KHR_DEFAULT_CONTEXT || SYCL_EXT_ONEAPI_DEFAULT_CONTEXT
248
    }
14✔
249
    else {
1✔
250
        error_handler(
1✔
251
            "Default platform cannot be obtained up for a NULL platform.",
1✔
252
            __FILE__, __func__, __LINE__);
1✔
253
        return nullptr;
1✔
254
    }
1✔
255
}
15✔
256

257
bool DPCTLPlatform_AreEq(__dpctl_keep const DPCTLSyclPlatformRef PRef1,
258
                         __dpctl_keep const DPCTLSyclPlatformRef PRef2)
259
{
18✔
260
    auto P1 = unwrap<platform>(PRef1);
18✔
261
    auto P2 = unwrap<platform>(PRef2);
18✔
262
    if (P1 && P2)
18!
263
        return *P1 == *P2;
14✔
264
    else
4✔
265
        return false;
4✔
266
}
18✔
267

268
size_t DPCTLPlatform_Hash(__dpctl_keep const DPCTLSyclPlatformRef PRef)
269
{
32✔
270
    if (PRef) {
32✔
271
        auto P = unwrap<platform>(PRef);
28✔
272
        std::hash<platform> hash_fn;
28✔
273
        return hash_fn(*P);
28✔
274
    }
28✔
275
    else {
4✔
276
        error_handler("Argument PRef is null.", __FILE__, __func__, __LINE__);
4✔
277
        return 0;
4✔
278
    }
4✔
279
}
32✔
280

281
__dpctl_give DPCTLDeviceVectorRef
282
DPCTLPlatform_GetDevices(__dpctl_keep const DPCTLSyclPlatformRef PRef,
283
                         DPCTLSyclDeviceType DTy)
284
{
14✔
285
    auto P = unwrap<platform>(PRef);
14✔
286
    if (!P) {
14!
287
        error_handler("Cannot retrieve devices from DPCTLSyclPlatformRef as "
×
288
                      "input is a nullptr.",
×
289
                      __FILE__, __func__, __LINE__);
×
290
        return nullptr;
×
291
    }
×
292

293
    using vecTy = std::vector<DPCTLSyclDeviceRef>;
14✔
294
    vecTy *DevicesVectorPtr = nullptr;
14✔
295
    try {
14✔
296
        DevicesVectorPtr = new vecTy();
14✔
297
    } catch (std::exception const &e) {
14✔
298
        delete DevicesVectorPtr;
×
299
        error_handler(e, __FILE__, __func__, __LINE__);
×
300
        return nullptr;
×
301
    }
×
302

303
    if (DTy == DPCTLSyclDeviceType::DPCTL_UNKNOWN_DEVICE) {
14!
304
        return wrap<vecTy>(DevicesVectorPtr);
×
305
    }
×
306

307
    try {
14✔
308
        auto SyclDTy = DPCTL_DPCTLDeviceTypeToSyclDeviceType(DTy);
14✔
309
        auto Devices = P->get_devices(SyclDTy);
14✔
310
        DevicesVectorPtr->reserve(Devices.size());
14✔
311
        for (const auto &Dev : Devices) {
14✔
312
            DevicesVectorPtr->emplace_back(
10✔
313
                wrap<device>(new device(std::move(Dev))));
10✔
314
        }
10✔
315
        return wrap<vecTy>(DevicesVectorPtr);
14✔
316
    } catch (std::exception const &e) {
14✔
317
        delete DevicesVectorPtr;
×
318
        error_handler(e, __FILE__, __func__, __LINE__);
×
319
        return nullptr;
×
320
    }
×
321
}
14✔
322

323
__dpctl_give DPCTLDeviceVectorRef
324
DPCTLPlatform_GetCompositeDevices(__dpctl_keep const DPCTLSyclPlatformRef PRef)
325
{
5✔
326
    auto P = unwrap<platform>(PRef);
5✔
327
    if (!P) {
5!
328
        error_handler("Cannot retrieve composite devices from "
×
329
                      "DPCTLSyclPlatformRef as input is a nullptr.",
×
330
                      __FILE__, __func__, __LINE__);
×
331
        return nullptr;
×
332
    }
×
333

334
    using vecTy = std::vector<DPCTLSyclDeviceRef>;
5✔
335
    vecTy *DevicesVectorPtr = nullptr;
5✔
336
    try {
5✔
337
        DevicesVectorPtr = new vecTy();
5✔
338
    } catch (std::exception const &e) {
5✔
339
        delete DevicesVectorPtr;
×
340
        error_handler(e, __FILE__, __func__, __LINE__);
×
341
        return nullptr;
×
342
    }
×
343

344
    try {
5✔
345
        auto composite_devices = P->ext_oneapi_get_composite_devices();
5✔
346
        DevicesVectorPtr->reserve(composite_devices.size());
5✔
347
        for (const auto &Dev : composite_devices) {
5!
348
            DevicesVectorPtr->emplace_back(
×
349
                wrap<device>(new device(std::move(Dev))));
×
350
        }
×
351
        return wrap<vecTy>(DevicesVectorPtr);
5✔
352
    } catch (std::exception const &e) {
5✔
353
        delete DevicesVectorPtr;
×
354
        error_handler(e, __FILE__, __func__, __LINE__);
×
355
        return nullptr;
×
356
    }
×
357
}
5✔
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

© 2026 Coveralls, Inc