|
| 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