Skip to content

Commit 60c299e

Browse files
authored
Merge pull request #1064 from dklochkov-emb/current-device-tests
Add tests for sycl_ext_oneapi_current_device
2 parents 84f961d + 557cb61 commit 60c299e

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ add_cts_option(SYCL_CTS_ENABLE_EXT_ONEAPI_LOCAL_MEMORY_TESTS
122122
"Enable extension oneAPI local_memory tests" OFF
123123
FORCE_ON ${SYCL_CTS_ENABLE_EXT_ONEAPI_TESTS})
124124

125+
add_cts_option(SYCL_CTS_ENABLE_EXT_ONEAPI_CURRENT_DEVICE_TESTS
126+
"Enable extension oneAPI current_device tests" OFF
127+
FORCE_ON ${SYCL_CTS_ENABLE_EXT_ONEAPI_TESTS})
128+
125129
add_cts_option(SYCL_CTS_ENABLE_KHR_DEFAULT_CONTEXT_TESTS
126130
"Enable extension Khronos default_context tests" OFF
127131
FORCE_ON ${SYCL_CTS_ENABLE_KHR_TESTS})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if(SYCL_CTS_ENABLE_EXT_ONEAPI_CURRENT_DEVICE_TESTS)
2+
file(GLOB test_cases_list *.cpp)
3+
4+
add_cts_test(${test_cases_list})
5+
endif()
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*******************************************************************************
2+
//
3+
// SYCL 2020 Conformance Test Suite
4+
//
5+
// Copyright (c) 2025 The Khronos Group Inc.
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+
#include "../../common/common.h"
22+
#include <thread>
23+
24+
namespace current_device::tests {
25+
26+
TEST_CASE(
27+
"Test for \"get_current_device\" gets default device, calls function and "
28+
"compares devices") {
29+
#ifndef SYCL_EXT_ONEAPI_CURRENT_DEVICE
30+
SKIP(
31+
"The sycl_ext_oneapi_current_device extension is not supported "
32+
"by this implementation");
33+
#else
34+
if (sycl::device::get_devices().size() < 1) {
35+
SKIP("Test requires at least one device");
36+
}
37+
const sycl::device default_device{sycl::default_selector_v};
38+
const auto current_device =
39+
sycl::ext::oneapi::experimental::this_thread::get_current_device();
40+
CHECK(
41+
std::is_same_v<std::remove_cv_t<decltype(current_device)>, sycl::device>);
42+
CHECK(default_device == current_device);
43+
#endif
44+
}
45+
46+
TEST_CASE(
47+
"Test for \"set_current_device and get_current_device\" checks that "
48+
"get_current_device function returns the device set by "
49+
"set_current_device function") {
50+
#ifndef SYCL_EXT_ONEAPI_CURRENT_DEVICE
51+
SKIP(
52+
"The sycl_ext_oneapi_current_device extension is not supported by this "
53+
"implementation");
54+
#else
55+
for (const auto& device : sycl::device::get_devices()) {
56+
sycl::ext::oneapi::experimental::this_thread::set_current_device(device);
57+
const auto current_device =
58+
sycl::ext::oneapi::experimental::this_thread::get_current_device();
59+
CHECK(std::is_same_v<std::remove_cv_t<decltype(current_device)>,
60+
sycl::device>);
61+
CHECK(device == current_device);
62+
}
63+
#endif
64+
}
65+
66+
void thread_callback(const sycl::device& device_to_set,
67+
sycl::device& current_device) {
68+
sycl::ext::oneapi::experimental::this_thread::set_current_device(
69+
device_to_set);
70+
current_device =
71+
sycl::ext::oneapi::experimental::this_thread::get_current_device();
72+
}
73+
74+
TEST_CASE(
75+
"Test for calling \"set_current_device and get_current_device in "
76+
"different threads\", checks that each thread has its own current device") {
77+
#ifndef SYCL_EXT_ONEAPI_CURRENT_DEVICE
78+
SKIP(
79+
"The sycl_ext_oneapi_current_device extension is not supported "
80+
"by this implementation");
81+
#else
82+
const auto devices = sycl::device::get_devices();
83+
if (devices.size() < 2) {
84+
SKIP("Test requires at least two devices");
85+
}
86+
const auto t1_device_to_set = devices[0];
87+
const auto t2_device_to_set = devices[1];
88+
sycl::device t1_current_device;
89+
sycl::device t2_current_device;
90+
91+
std::thread t1([&t1_device_to_set, &t1_current_device]() {
92+
sycl::ext::oneapi::experimental::this_thread::set_current_device(
93+
t1_device_to_set);
94+
t1_current_device =
95+
sycl::ext::oneapi::experimental::this_thread::get_current_device();
96+
});
97+
std::thread t2([&t2_device_to_set, &t2_current_device]() {
98+
sycl::ext::oneapi::experimental::this_thread::set_current_device(
99+
t2_device_to_set);
100+
t2_current_device =
101+
sycl::ext::oneapi::experimental::this_thread::get_current_device();
102+
});
103+
104+
t1.join();
105+
t2.join();
106+
107+
CHECK(t1_current_device == t1_device_to_set);
108+
CHECK(t2_current_device == t2_device_to_set);
109+
CHECK(t1_current_device != t2_current_device);
110+
111+
#endif
112+
}
113+
} // namespace current_device::tests

0 commit comments

Comments
 (0)