Skip to content

Commit de3a1db

Browse files
[SYCL][NFC] Introduce unit-tests for ONEAPI_DEVICE_SELECTOR (#15774)
There are E2E tests which are essentially testing the correctness of `ONEAPI_DEVICE_SELECTOR` env variable testing, but that can be done in way more efficient way which is unit-tests. Note that this PR does not aim to cover `ONEAPI_DEVICE_SELECTOR` parsing with unit-tests in full, but rather aims to replace the most obvious candidates from E2E tests with unit-tests, providing some foundation for expanding unit-tests in follow-up PRs.
1 parent d1b23df commit de3a1db

File tree

6 files changed

+76
-14
lines changed

6 files changed

+76
-14
lines changed

sycl/include/sycl/detail/device_filter.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ struct ods_target {
5353
ods_target(backend be) { Backend = be; };
5454
ods_target(){};
5555
friend std::ostream &operator<<(std::ostream &Out, const ods_target &Target);
56+
57+
#if __cplusplus >= 202002L
58+
bool operator==(const ods_target &Other) const = default;
59+
#else
60+
bool operator==(const ods_target &Other) const {
61+
return Backend == Other.Backend && DeviceType == Other.DeviceType &&
62+
HasDeviceWildCard == Other.HasDeviceWildCard &&
63+
DeviceNum == Other.DeviceNum &&
64+
HasSubDeviceWildCard == Other.HasSubDeviceWildCard &&
65+
HasSubSubDeviceWildCard == Other.HasSubSubDeviceWildCard &&
66+
SubSubDeviceNum == Other.SubSubDeviceNum &&
67+
IsNegativeTarget == Other.IsNegativeTarget &&
68+
MatchesSeen == Other.MatchesSeen;
69+
}
70+
#endif
5671
};
5772

5873
class ods_target_list {

sycl/test-e2e/OneapiDeviceSelector/backendonly_error.cpp

Lines changed: 0 additions & 6 deletions
This file was deleted.

sycl/test-e2e/OneapiDeviceSelector/case_sensitivity.cpp

Lines changed: 0 additions & 8 deletions
This file was deleted.

sycl/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ if (NOT WIN32)
5959
endif()
6060
add_subdirectory(sampler)
6161
add_subdirectory(reduction)
62+
add_subdirectory(OneAPIDeviceSelector)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_sycl_unittest(OneAPIDeviceSelectorTests OBJECT
2+
Tests.cpp
3+
)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===---------------------------- Tests.cpp -------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <sycl/detail/device_filter.hpp>
10+
#include <sycl/exception.hpp>
11+
12+
#include <gtest/gtest.h>
13+
14+
#include <vector>
15+
#include <utility>
16+
#include <string>
17+
18+
TEST(OneAPIDeviceSelector, IsCaseInsensitive) {
19+
ASSERT_EQ(sycl::detail::Parse_ONEAPI_DEVICE_SELECTOR("OPENCL:*"),
20+
sycl::detail::Parse_ONEAPI_DEVICE_SELECTOR("opencl:*"))
21+
<< " backend should be case-insensitive";
22+
23+
ASSERT_EQ(sycl::detail::Parse_ONEAPI_DEVICE_SELECTOR("*:GPU"),
24+
sycl::detail::Parse_ONEAPI_DEVICE_SELECTOR("*:gpu"))
25+
<< " device type should be case-insensitive";
26+
}
27+
28+
TEST(OneAPIDeviceSelector, EmitsErrorIfOnlyBackendIsSpecified) {
29+
try {
30+
std::ignore = sycl::detail::Parse_ONEAPI_DEVICE_SELECTOR("level_zero");
31+
FAIL() << "An exception was expected";
32+
} catch (const sycl::exception &e) {
33+
ASSERT_EQ(e.code(), sycl::errc::invalid);
34+
ASSERT_EQ(std::string(e.what()),
35+
"Incomplete selector! Try 'level_zero:*' if all "
36+
"devices under the backend was original intention.");
37+
}
38+
}
39+
40+
TEST(OneAPIDeviceSelector, EmitsErrorIfBackendStringIsInvalid) {
41+
try {
42+
std::ignore = sycl::detail::Parse_ONEAPI_DEVICE_SELECTOR("macaroni:*");
43+
FAIL() << "An exception was expected";
44+
} catch (const sycl::exception &e) {
45+
ASSERT_EQ(e.code(), sycl::errc::invalid);
46+
// FIXME: the error below could be better
47+
ASSERT_EQ(std::string(e.what()),
48+
"ONEAPI_DEVICE_SELECTOR parsing error. Backend is required but "
49+
"missing from \"macaroni:*\"");
50+
}
51+
}
52+
53+
// TODO: test case ":"
54+
// TODO: test case ":cpu"
55+
// TODO: test case "level_zero:cpu:cpu"
56+
// TODO: test case "opencl:"
57+
// TODO: other positive test cases for parsing device, sub-devices, etc.

0 commit comments

Comments
 (0)