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

IntelPython / dpctl / 14754782295

30 Apr 2025 12:39PM UTC coverage: 86.419%. Remained the same
14754782295

Pull #2068

github

web-flow
Merge c8700ceb2 into b7a6b67c7
Pull Request #2068: Correct a path to `cl.cfg` file

3020 of 3716 branches covered (81.27%)

Branch coverage included in aggregate %.

12195 of 13890 relevant lines covered (87.8%)

6998.91 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
{
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) ||                                       \
2,010✔
231
    defined(SYCL_EXT_ONEAPI_DEFAULT_CONTEXT)
2,010✔
232
        try {
2,010✔
233
            const auto &default_ctx = P->
2,010✔
234
#ifdef SYCL_KHR_DEFAULT_CONTEXT
235
                                      khr_get_default_context()
236
#else
237
                                      ext_oneapi_get_default_context()
2,010✔
238
#endif // SYCL_KHR_DEFAULT_CONTEXT
2,010✔
239
                ;
2,010✔
240
            return wrap<context>(new context(default_ctx));
2,010✔
241
        } catch (const std::exception &ex) {
2,010✔
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
    }
2,010✔
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
}
2,011✔
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
{
630✔
270
    if (PRef) {
630✔
271
        auto P = unwrap<platform>(PRef);
626✔
272
        std::hash<platform> hash_fn;
626✔
273
        return hash_fn(*P);
626✔
274
    }
626✔
275
    else {
4✔
276
        error_handler("Argument PRef is null.", __FILE__, __func__, __LINE__);
4✔
277
        return 0;
4✔
278
    }
4✔
279
}
630✔
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
    // handle unknown device
304
    // custom and automatic are also treated as unknown
305
    // as DPC++ would normally treat as `all`
306
    // see CMPLRLLVM-65826
307
    if (DTy == DPCTLSyclDeviceType::DPCTL_UNKNOWN_DEVICE) {
14!
308
        return wrap<vecTy>(DevicesVectorPtr);
×
309
    }
×
310

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

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

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

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

© 2025 Coveralls, Inc