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

IntelPython / dpctl / 47374860882

04 Dec 2026 04:20PM UTC coverage: 75.771% (+0.6%) from 75.182%
47374860882

Pull #2420

github

web-flow
Merge 0475d42fc into 7fb677ad7
Pull Request #2420: Implement filter selector strings without the DPC++ extension

1162 of 1602 branches covered (72.53%)

Branch coverage included in aggregate %.

101 of 102 new or added lines in 1 file covered. (99.02%)

4292 of 5596 relevant lines covered (76.7%)

405.33 hits per line

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

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

28
#include "dpctl_device_selection.hpp"
29
#include "Config/dpctl_config.h"
30
#include <charconv>
31
#include <cstddef>
32
#include <optional>
33
#include <stdexcept>
34
#include <string>
35
#include <sycl/sycl.hpp>
36
#include <utility>
37
#include <vector>
38

39
namespace
40
{
41
#ifndef __ADAPTIVECPP__
42
static_assert(__SYCL_COMPILER_VERSION >= __SYCL_COMPILER_VERSION_REQUIRED,
43
              "The compiler does not meet minimum version requirement");
44
#endif
45

46
/*! Splits s on delim, dropping empty tokens. */
47
std::vector<std::string> tokenize(const std::string &s, char delim)
48
{
13,589 ✔
49
    std::vector<std::string> tokens;
13,589 ✔
50
    std::size_t start = 0;
13,589 ✔
51
    while (start <= s.size()) {
32,964 ✔
52
        auto end = s.find(delim, start);
19,375 ✔
53
        if (end == std::string::npos)
19,375 ✔
54
            end = s.size();
13,589 ✔
55
        if (end > start)
19,375 ✔
56
            tokens.emplace_back(s, start, end - start);
19,334 ✔
57
        start = end + 1;
19,375 ✔
58
    }
19,375 ✔
59
    return tokens;
13,589 ✔
60
}
13,589 ✔
61

62
std::optional<sycl::backend> backend_from_token(const std::string &tok)
63
{
12,518 ✔
64
#ifndef __ADAPTIVECPP__
12,518 ✔
65
    if (tok == "opencl")
12,518 ✔
66
        return sycl::backend::opencl;
2,919 ✔
67
    if (tok == "level_zero")
9,599 ✔
68
        return sycl::backend::ext_oneapi_level_zero;
1,453 ✔
69
    if (tok == "cuda")
8,146 ✔
70
        return sycl::backend::ext_oneapi_cuda;
29 ✔
71
    if (tok == "hip")
8,117 ✔
72
        return sycl::backend::ext_oneapi_hip;
7 ✔
73
#else
74
    if (tok == "opencl")
75
        return sycl::backend::ocl;
76
    if (tok == "level_zero")
77
        return sycl::backend::level_zero;
78
    if (tok == "cuda")
79
        return sycl::backend::cuda;
80
    if (tok == "hip")
81
        return sycl::backend::hip;
82
    if (tok == "openmp")
83
        return sycl::backend::omp;
84
#endif
85
    return std::nullopt;
8,110 ✔
86
}
8,117 ✔
87

88
std::optional<sycl::info::device_type>
89
device_type_from_token(const std::string &tok)
90
{
8,115 ✔
91
    if (tok == "cpu")
8,115 ✔
92
        return sycl::info::device_type::cpu;
1,862 ✔
93
    if (tok == "gpu")
6,253 ✔
94
        return sycl::info::device_type::gpu;
3,308 ✔
95
    if (tok == "accelerator")
2,945 !
NEW
96
        return sycl::info::device_type::accelerator;
×
97
    if (tok == "custom")
2,945 ✔
98
        return sycl::info::device_type::custom;
3 ✔
99
    return std::nullopt;
2,942 ✔
100
}
2,945 ✔
101

102
/*! Parses a non-negative decimal int; nullopt on any other input. */
103
std::optional<int> device_num_from_token(const std::string &tok)
104
{
2,947 ✔
105
    for (char c : tok) {
2,979 ✔
106
        if (c < '0' || c > '9')
2,979 ✔
107
            return std::nullopt;
80 ✔
108
    }
2,979 ✔
109
    int num = 0;
2,867 ✔
110
    const char *first = tok.data();
2,867 ✔
111
    const char *last = first + tok.size();
2,867 ✔
112
    auto [ptr, ec] = std::from_chars(first, last, num);
2,867 ✔
113
    if (ec != std::errc{} || ptr != last)
2,867 !
114
        return std::nullopt;
3 ✔
115
    return num;
2,864 ✔
116
}
2,867 ✔
117

118
bool matches_backend_and_type(const std::optional<sycl::backend> &be,
119
                              const std::optional<sycl::info::device_type> &dt,
120
                              const sycl::device &d)
121
{
7,617 ✔
122
    if (be && d.get_backend() != *be)
7,617 ✔
123
        return false;
1,484 ✔
124
    if (dt && d.get_info<sycl::info::device::device_type>() != *dt)
6,133 ✔
125
        return false;
2,284 ✔
126
    return true;
3,849 ✔
127
}
6,133 ✔
128

129
[[noreturn]] void throw_invalid_filter(const std::string &fs)
130
{
106 ✔
131
    throw std::invalid_argument(
106 ✔
132
        "Invalid filter string '" + fs +
106 ✔
133
        "'. Filters are comma-separated and have the form "
106 ✔
134
        "backend:device_type:device_num, where each component is optional.");
106 ✔
135
}
106 ✔
136
} // namespace
137

138
namespace dpctl
139
{
140
namespace syclinterface
141
{
142

143
int dpctl_device_selector::operator()(const sycl::device &) const
144
{
×
145
    return REJECT_DEVICE;
×
146
}
×
147

148
int dpctl_accelerator_selector::operator()(const sycl::device &d) const
149
{
207 ✔
150
#ifndef __ADAPTIVECPP__
207 ✔
151
    return sycl::accelerator_selector_v(d);
207 ✔
152
#else
153
    // AdaptiveCpp defines is_accelerator() as !is_cpu(), which makes its
154
    // accelerator_selector_v accept GPUs as well
155
    auto score = sycl::default_selector_v(d);
156
    if (score < 0 || d.get_info<sycl::info::device::device_type>() !=
157
                         sycl::info::device_type::accelerator)
158
        return REJECT_DEVICE;
159
    return score + 1;
160
#endif
161
}
207 ✔
162

163
int dpctl_default_selector::operator()(const sycl::device &d) const
164
{
1,048 ✔
165
    auto score = sycl::default_selector_v(d);
1,048 ✔
166
    return score;
1,048 ✔
167
}
1,048 ✔
168

169
int dpctl_gpu_selector::operator()(const sycl::device &d) const
170
{
208 ✔
171
    return sycl::gpu_selector_v(d);
208 ✔
172
}
208 ✔
173

174
int dpctl_cpu_selector::operator()(const sycl::device &d) const
175
{
209 ✔
176
    return sycl::cpu_selector_v(d);
209 ✔
177
}
209 ✔
178

179
dpctl_filter_selector::dpctl_filter_selector(const std::string &fs)
180
{
6,795 ✔
181
    std::vector<std::pair<filter, std::optional<int>>> parsed;
6,795 ✔
182
    for (const auto &filter_str : tokenize(fs, ',')) {
6,795 ✔
183
        auto tokens = tokenize(filter_str, ':');
6,794 ✔
184
        if (tokens.empty() || tokens.size() > 3)
6,794 ✔
185
            throw_invalid_filter(fs);
18 ✔
186

187
        filter f;
6,794 ✔
188
        std::optional<int> device_num;
6,794 ✔
189
        for (const auto &tok : tokens) {
12,518 ✔
190
            if (auto be = backend_from_token(tok); be && !f.backend)
12,518 ✔
191
                f.backend = be;
4,403 ✔
192
            else if (auto dt = device_type_from_token(tok);
8,115 ✔
193
                     dt && !f.device_type)
8,115 ✔
194
                f.device_type = dt;
5,168 ✔
195
            else if (auto num = device_num_from_token(tok); num && !device_num)
2,947 ✔
196
                device_num = num;
2,859 ✔
197
            else
88 ✔
198
                throw_invalid_filter(fs);
88 ✔
199
        }
12,518 ✔
200
        parsed.emplace_back(std::move(f), device_num);
6,794 ✔
201
    }
6,794 ✔
202

203
    // Resolve device numbers; a filter whose device does not exist is dropped
204
    std::vector<sycl::device> devices;
6,795 ✔
205
    bool have_devices = false;
6,795 ✔
206
    for (auto &[f, device_num] : parsed) {
6,795 ✔
207
        if (device_num) {
6,683 ✔
208
            if (!have_devices) {
2,849 !
209
                devices = sycl::device::get_devices();
2,849 ✔
210
                have_devices = true;
2,849 ✔
211
            }
2,849 ✔
212
            int idx = 0;
2,849 ✔
213
            for (const auto &d : devices) {
2,849 ✔
214
                if (sycl::default_selector_v(d) < 0 ||
2,849 !
215
                    !matches_backend_and_type(f.backend, f.device_type, d))
2,849 ✔
216
                    continue;
1,710 ✔
217
                if (idx++ == *device_num) {
1,139 ✔
218
                    f.device = d;
932 ✔
219
                    break;
932 ✔
220
                }
932 ✔
221
            }
1,139 ✔
222
            if (!f.device)
2,849 ✔
223
                continue;
1,917 ✔
224
        }
2,849 ✔
225
        _filters.push_back(std::move(f));
4,766 ✔
226
    }
4,766 ✔
227
}
6,795 ✔
228

229
int dpctl_filter_selector::operator()(const sycl::device &d) const
230
{
6,691 ✔
231
    for (const auto &f : _filters) {
6,691 ✔
232
        if (matches_backend_and_type(f.backend, f.device_type, d) &&
4,768 ✔
233
            (!f.device || *f.device == d))
2,710 !
234
        {
2,710 ✔
235
#ifndef __ADAPTIVECPP__
2,710 ✔
236
            return sycl::default_selector_v(d);
2,710 ✔
237
#else
238
            // scores of matched devices are shifted to be strictly positive,
239
            // since AdaptiveCpp scores the OpenMP host device 0
240
            auto score = sycl::default_selector_v(d);
241
            return (score < 0) ? REJECT_DEVICE : score + 1;
242
#endif
243
        }
2,710 ✔
244
    }
4,768 ✔
245
    return REJECT_DEVICE;
3,981 ✔
246
}
6,691 ✔
247

248
} // namespace syclinterface
249
} // namespace dpctl
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc