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

IntelPython / dpctl / 13402228542

18 Feb 2025 11:40PM UTC coverage: 88.169% (-0.06%) from 88.224%
13402228542

Pull #1992

github

web-flow
Merge f60d32ac2 into df4f4b16c
Pull Request #1992: Add `get_devices` method to `dpctl.SyclPlatform` class

3163 of 3658 branches covered (86.47%)

Branch coverage included in aggregate %.

52 of 71 new or added lines in 2 files covered. (73.24%)

1 existing line in 1 file now uncovered.

11980 of 13517 relevant lines covered (88.63%)

7149.61 hits per line

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

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

59
} // end of anonymous namespace
60

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

114
void DPCTLPlatform_Delete(__dpctl_take DPCTLSyclPlatformRef PRef)
115
{
5,028✔
116
    auto P = unwrap<platform>(PRef);
5,028✔
117
    delete P;
5,028✔
118
}
5,028✔
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,702✔
138
    auto P = unwrap<platform>(PRef);
4,702✔
139
    if (P) {
4,702✔
140
        try {
4,701✔
141
            auto name = P->get_info<info::platform::name>();
4,701✔
142
            return dpctl::helper::cstring_from_string(name);
4,701✔
143
        } catch (std::exception const &e) {
4,701✔
144
            error_handler(e, __FILE__, __func__, __LINE__);
×
145
            return nullptr;
×
146
        }
×
147
    }
4,701✔
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,702✔
154

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

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

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

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

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

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

221
    // the wrap function is defined inside dpctl_vector_templ.cpp
222
    return wrap<vecTy>(Platforms);
16✔
223
}
16✔
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
#ifdef SYCL_EXT_ONEAPI_DEFAULT_CONTEXT
2,010✔
231
        try {
2,010✔
232
            const auto &default_ctx = P->ext_oneapi_get_default_context();
2,010✔
233
            return wrap<context>(new context(default_ctx));
2,010✔
234
        } catch (const std::exception &ex) {
2,010✔
235
            error_handler(ex, __FILE__, __func__, __LINE__);
×
236
            return nullptr;
×
237
        }
×
238
#else
239
        return nullptr;
240
#endif
241
    }
2,010✔
242
    else {
1✔
243
        error_handler(
1✔
244
            "Default platform cannot be obtained up for a NULL platform.",
1✔
245
            __FILE__, __func__, __LINE__);
1✔
246
        return nullptr;
1✔
247
    }
1✔
248
}
2,011✔
249

250
bool DPCTLPlatform_AreEq(__dpctl_keep const DPCTLSyclPlatformRef PRef1,
251
                         __dpctl_keep const DPCTLSyclPlatformRef PRef2)
252
{
18✔
253
    auto P1 = unwrap<platform>(PRef1);
18✔
254
    auto P2 = unwrap<platform>(PRef2);
18✔
255
    if (P1 && P2)
18!
256
        return *P1 == *P2;
14✔
257
    else
4✔
258
        return false;
4✔
259
}
18✔
260

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

274
__dpctl_give DPCTLDeviceVectorRef
275
DPCTLPlatform_GetDevices(__dpctl_keep const DPCTLSyclPlatformRef PRef,
276
                         DPCTLSyclDeviceType DTy)
277
{
13✔
278
    auto P = unwrap<platform>(PRef);
13✔
279
    if (!P) {
13!
NEW
280
        error_handler("Cannot retrieve devices from DPCTLSyclPlatformRef as "
×
NEW
281
                      "input is a nullptr.",
×
NEW
282
                      __FILE__, __func__, __LINE__);
×
NEW
283
        return nullptr;
×
NEW
284
    }
×
285

286
    using vecTy = std::vector<DPCTLSyclDeviceRef>;
13✔
287
    vecTy *DevicesVectorPtr = nullptr;
13✔
288
    try {
13✔
289
        DevicesVectorPtr = new vecTy();
13✔
290
    } catch (std::exception const &e) {
13✔
NEW
291
        delete DevicesVectorPtr;
×
NEW
292
        error_handler(e, __FILE__, __func__, __LINE__);
×
NEW
293
        return nullptr;
×
NEW
294
    }
×
295

296
    // handle unknown device
297
    if (DTy == DPCTLSyclDeviceType::DPCTL_UNKNOWN_DEVICE) {
13!
NEW
298
        return wrap<vecTy>(DevicesVectorPtr);
×
NEW
299
    }
×
300

301
    try {
13✔
302
        auto SyclDTy = DPCTL_DPCTLDeviceTypeToSyclDeviceType(DTy);
13✔
303
        auto Devices = P->get_devices(SyclDTy);
13✔
304
        DevicesVectorPtr->reserve(Devices.size());
13✔
305
        for (const auto &Dev : Devices) {
13✔
306
            DevicesVectorPtr->emplace_back(
9✔
307
                wrap<device>(new device(std::move(Dev))));
9✔
308
        }
9✔
309
        return wrap<vecTy>(DevicesVectorPtr);
13✔
310
    } catch (std::exception const &e) {
13✔
NEW
311
        delete DevicesVectorPtr;
×
NEW
312
        error_handler(e, __FILE__, __func__, __LINE__);
×
NEW
313
        return nullptr;
×
NEW
314
    }
×
315
}
13✔
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