Skip to content

Commit c0a4a56

Browse files
[SYCL] aspect_selector implementation (#7068)
This implements the SYCL 2020 `aspect_selector`, with the aspect_selector returning a SYCL2020 Callable device selector. Derived from original work at #5696 . Tests here: intel/llvm-test-suite#1327
1 parent 77b1e9d commit c0a4a56

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

sycl/include/sycl/device_selector.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <sycl/detail/defines.hpp>
1212
#include <sycl/detail/export.hpp>
1313

14+
#include <vector>
15+
1416
// 4.6.1 Device selection class
1517

1618
namespace sycl {
@@ -117,6 +119,17 @@ static constexpr int REJECT_DEVICE_SCORE = -1;
117119

118120
using DSelectorInvocableType = std::function<int(const sycl::device &)>;
119121

122+
template <typename LastT>
123+
void fill_aspect_vector(std::vector<aspect> &V, LastT L) {
124+
V.emplace_back(L);
125+
}
126+
127+
template <typename FirstT, typename... OtherTs>
128+
void fill_aspect_vector(std::vector<aspect> &V, FirstT F, OtherTs... O) {
129+
V.emplace_back(F);
130+
fill_aspect_vector(V, O...);
131+
}
132+
120133
#if __cplusplus >= 201703L
121134

122135
// Enable if DeviceSelector callable has matching signature, but
@@ -136,6 +149,27 @@ select_device(const DSelectorInvocableType &DeviceSelectorInvocable);
136149
__SYCL_EXPORT device
137150
select_device(const DSelectorInvocableType &DeviceSelectorInvocable,
138151
const context &SyclContext);
152+
139153
} // namespace detail
154+
155+
__SYCL_EXPORT detail::DSelectorInvocableType
156+
aspect_selector(const std::vector<aspect> &RequireList,
157+
const std::vector<aspect> &DenyList = {});
158+
159+
template <typename... AspectListT>
160+
detail::DSelectorInvocableType aspect_selector(AspectListT... AspectList) {
161+
std::vector<aspect> RequireList;
162+
RequireList.reserve(sizeof...(AspectList));
163+
164+
detail::fill_aspect_vector(RequireList, AspectList...);
165+
166+
return aspect_selector(RequireList, {});
167+
}
168+
169+
template <aspect... AspectList>
170+
detail::DSelectorInvocableType aspect_selector() {
171+
return aspect_selector({AspectList...}, {});
172+
}
173+
140174
} // __SYCL_INLINE_VER_NAMESPACE(_V1)
141175
} // namespace sycl

sycl/source/device_selector.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,31 @@ int host_selector::operator()(const device &dev) const {
247247
return detail::REJECT_DEVICE_SCORE;
248248
}
249249

250+
__SYCL_EXPORT detail::DSelectorInvocableType
251+
aspect_selector(const std::vector<aspect> &RequireList,
252+
const std::vector<aspect> &DenyList /* ={} */) {
253+
return [=](const sycl::device &Dev) {
254+
auto DevHas = [&](const aspect &Asp) -> bool { return Dev.has(Asp); };
255+
256+
// All aspects from require list are required.
257+
if (!std::all_of(RequireList.begin(), RequireList.end(), DevHas))
258+
return detail::REJECT_DEVICE_SCORE;
259+
260+
// No aspect from deny list is allowed
261+
if (std::any_of(DenyList.begin(), DenyList.end(), DevHas))
262+
return detail::REJECT_DEVICE_SCORE;
263+
264+
if (RequireList.size() > 0) {
265+
return 1000 + detail::getDevicePreference(Dev);
266+
} else {
267+
// No required aspects specified.
268+
// SYCL 2020 4.6.1.1 "If no aspects are passed in, the generated selector
269+
// behaves like default_selector."
270+
return default_selector_v(Dev);
271+
}
272+
};
273+
}
274+
250275
// -------------- SYCL 1.2.1
251276

252277
// SYCL 1.2.1 device_selector class and sub-classes

sycl/test/abi/sycl_symbols_linux.dump

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3634,6 +3634,7 @@ _ZN4sycl3_V114exception_list8PushBackEONSt15__exception_ptr13exception_ptrE
36343634
_ZN4sycl3_V114exception_list8PushBackERKNSt15__exception_ptr13exception_ptrE
36353635
_ZN4sycl3_V114get_kernel_idsEv
36363636
_ZN4sycl3_V114gpu_selector_vERKNS0_6deviceE
3637+
_ZN4sycl3_V115aspect_selectorERKSt6vectorINS0_6aspectESaIS2_EES6_
36373638
_ZN4sycl3_V115make_error_codeENS0_4errcE
36383639
_ZN4sycl3_V116get_pointer_typeEPKvRKNS0_7contextE
36393640
_ZN4sycl3_V118aligned_alloc_hostEmmRKNS0_5queueERKNS0_13property_listERKNS0_6detail13code_locationE

sycl/test/abi/sycl_symbols_windows.dump

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,9 @@
412412
??0gpu_selector@_V1@sycl@@QEAA@$$QEAV012@@Z
413413
??0gpu_selector@_V1@sycl@@QEAA@AEBV012@@Z
414414
??0gpu_selector@_V1@sycl@@QEAA@XZ
415+
??0half@host_half_impl@detail@_V1@sycl@@QEAA@$$QEAV01234@@Z
415416
??0half@host_half_impl@detail@_V1@sycl@@QEAA@AEBM@Z
417+
??0half@host_half_impl@detail@_V1@sycl@@QEAA@AEBV01234@@Z
416418
??0half@host_half_impl@detail@_V1@sycl@@QEAA@G@Z
417419
??0handler@_V1@sycl@@AEAA@V?$shared_ptr@Vqueue_impl@detail@_V1@sycl@@@std@@00_N@Z
418420
??0handler@_V1@sycl@@AEAA@V?$shared_ptr@Vqueue_impl@detail@_V1@sycl@@@std@@_N@Z
@@ -708,6 +710,7 @@
708710
?allocateMemBuffer@MemoryManager@detail@_V1@sycl@@SAPEAXV?$shared_ptr@Vcontext_impl@detail@_V1@sycl@@@std@@PEAVSYCLMemObjI@234@PEAX_N_KAEBV?$shared_ptr@Vevent_impl@detail@_V1@sycl@@@6@AEBV56@AEBVproperty_list@34@AEAPEAU_pi_event@@@Z
709711
?allocateMemImage@MemoryManager@detail@_V1@sycl@@SAPEAXV?$shared_ptr@Vcontext_impl@detail@_V1@sycl@@@std@@PEAVSYCLMemObjI@234@PEAX_N_KAEBU_pi_image_desc@@AEBU_pi_image_format@@AEBV?$shared_ptr@Vevent_impl@detail@_V1@sycl@@@6@AEBV56@AEBVproperty_list@34@AEAPEAU_pi_event@@@Z
710712
?allocateMemSubBuffer@MemoryManager@detail@_V1@sycl@@SAPEAXV?$shared_ptr@Vcontext_impl@detail@_V1@sycl@@@std@@PEAX_K2V?$range@$02@34@V?$vector@V?$shared_ptr@Vevent_impl@detail@_V1@sycl@@@std@@V?$allocator@V?$shared_ptr@Vevent_impl@detail@_V1@sycl@@@std@@@2@@6@AEAPEAU_pi_event@@@Z
713+
?aspect_selector@_V1@sycl@@YA?AV?$function@$$A6AHAEBVdevice@_V1@sycl@@@Z@std@@AEBV?$vector@W4aspect@_V1@sycl@@V?$allocator@W4aspect@_V1@sycl@@@std@@@4@0@Z
711714
?assertion@pi@detail@_V1@sycl@@YAX_NPEBD@Z
712715
?associateWithHandler@detail@_V1@sycl@@YAXAEAVhandler@23@PEAVAccessorBaseHost@123@W4target@access@23@@Z
713716
?associateWithHandler@handler@_V1@sycl@@AEAAXPEAVAccessorBaseHost@detail@23@W4target@access@23@@Z

0 commit comments

Comments
 (0)