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

IntelPython / dpctl / 14330076724

08 Apr 2025 09:29AM UTC coverage: 86.381% (+0.003%) from 86.378%
14330076724

Pull #2042

github

web-flow
Merge 3a12589f2 into e39b01393
Pull Request #2042: Use `khr_default_context()` per DPC++ compiler deprecation warning

3017 of 3714 branches covered (81.23%)

Branch coverage included in aggregate %.

7 of 7 new or added lines in 1 file covered. (100.0%)

9 existing lines in 1 file now uncovered.

12116 of 13805 relevant lines covered (87.77%)

7039.86 hits per line

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

78.62
/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
{
311✔
55
    return new platform(
311✔
56
        [=](const device &d) -> int { return sel->operator()(d); });
311✔
57
}
311✔
58

59
} // end of anonymous namespace
60

61
__dpctl_give DPCTLSyclPlatformRef
62
DPCTLPlatform_Copy(__dpctl_keep const DPCTLSyclPlatformRef PRef)
63
{
2,339✔
64
    auto Platform = unwrap<platform>(PRef);
2,339✔
65
    if (!Platform) {
2,339✔
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 {
2,334✔
71
        auto CopiedPlatform = new platform(*Platform);
2,334✔
72
        return wrap<platform>(CopiedPlatform);
2,334✔
73
    } catch (std::exception const &e) {
2,334✔
74
        error_handler(e, __FILE__, __func__, __LINE__);
×
75
        return nullptr;
×
76
    }
×
77
}
2,334✔
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
{
339✔
94
    if (DSRef) {
339✔
95
        auto DS = unwrap<dpctl_device_selector>(DSRef);
311✔
96
        platform *P = nullptr;
311✔
97
        try {
311✔
98
            P = new_platform_from_selector(DS);
311✔
99
            return wrap<platform>(P);
311✔
100
        } catch (std::exception const &e) {
311✔
101
            delete P;
219✔
102
            error_handler(e, __FILE__, __func__, __LINE__);
219✔
103
            return nullptr;
219✔
104
        }
219✔
105
    }
311✔
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
}
339✔
113

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

120
DPCTLSyclBackendType
121
DPCTLPlatform_GetBackend(__dpctl_keep const DPCTLSyclPlatformRef PRef)
122
{
12✔
123
    DPCTLSyclBackendType BTy = DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND;
12✔
124
    auto P = unwrap<platform>(PRef);
12✔
125
    if (P) {
12✔
126
        BTy = DPCTL_SyclBackendToDPCTLBackendType(P->get_backend());
11✔
127
    }
11✔
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;
12✔
133
}
12✔
134

135
__dpctl_give const char *
136
DPCTLPlatform_GetName(__dpctl_keep const DPCTLSyclPlatformRef PRef)
137
{
4,704✔
138
    auto P = unwrap<platform>(PRef);
4,704✔
139
    if (P) {
4,704✔
140
        try {
4,703✔
141
            auto name = P->get_info<info::platform::name>();
4,703✔
142
            return dpctl::helper::cstring_from_string(name);
4,703✔
143
        } catch (std::exception const &e) {
4,703✔
144
            error_handler(e, __FILE__, __func__, __LINE__);
×
145
            return nullptr;
×
146
        }
×
147
    }
4,703✔
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
}
4,704✔
154

155
__dpctl_give const char *
156
DPCTLPlatform_GetVendor(__dpctl_keep const DPCTLSyclPlatformRef PRef)
157
{
4,704✔
158
    auto P = unwrap<platform>(PRef);
4,704✔
159
    if (P) {
4,704✔
160
        try {
4,703✔
161
            auto vendor = P->get_info<info::platform::vendor>();
4,703✔
162
            return dpctl::helper::cstring_from_string(vendor);
4,703✔
163
        } catch (std::exception const &e) {
4,703✔
164
            error_handler(e, __FILE__, __func__, __LINE__);
×
165
            return nullptr;
×
166
        }
×
167
    }
4,703✔
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
}
4,704✔
174

175
__dpctl_give const char *
176
DPCTLPlatform_GetVersion(__dpctl_keep const DPCTLSyclPlatformRef PRef)
177
{
4,704✔
178
    auto P = unwrap<platform>(PRef);
4,704✔
179
    if (P) {
4,704✔
180
        try {
4,703✔
181
            auto driver = P->get_info<info::platform::version>();
4,703✔
182
            return dpctl::helper::cstring_from_string(driver);
4,703✔
183
        } catch (std::exception const &e) {
4,703✔
184
            error_handler(e, __FILE__, __func__, __LINE__);
×
185
            return nullptr;
×
186
        }
×
187
    }
4,703✔
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
}
4,704✔
194

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

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

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

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

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

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

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

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

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

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

302
    // handle unknown device
303
    // custom and automatic are also treated as unknown
304
    // as DPC++ would normally treat as `all`
305
    // see CMPLRLLVM-65826
306
    if (DTy == DPCTLSyclDeviceType::DPCTL_UNKNOWN_DEVICE) {
14!
UNCOV
307
        return wrap<vecTy>(DevicesVectorPtr);
×
308
    }
×
309

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

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

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

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