Skip to content

Commit 861d95d

Browse files
authored
Merge pull request #1085 from szadam/limit_devices_count
[UR][Tests] Add CTS options to set platforms and devices.
2 parents ce4acbc + 371bd3c commit 861d95d

File tree

5 files changed

+138
-12
lines changed

5 files changed

+138
-12
lines changed

test/conformance/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55

66
set(UR_CONFORMANCE_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR})
7+
option(UR_TEST_DEVICES_COUNT "Count of devices on which CTS will be run" 1)
8+
option(UR_TEST_PLATFORMS_COUNT "Count of platforms on which CTS will be run" 1)
79

810
function(add_test_adapter name adapter)
911
set(TEST_TARGET_NAME test-${name})
@@ -12,7 +14,7 @@ function(add_test_adapter name adapter)
1214
add_test(NAME ${TEST_NAME}
1315
COMMAND ${CMAKE_COMMAND}
1416
-D TEST_FILE=${Python3_EXECUTABLE}
15-
-D TEST_ARGS="${UR_CONFORMANCE_TEST_DIR}/cts_exe.py --test_command ${CMAKE_BINARY_DIR}/bin/${TEST_TARGET_NAME}"
17+
-D TEST_ARGS="${UR_CONFORMANCE_TEST_DIR}/cts_exe.py --test_command ${CMAKE_BINARY_DIR}/bin/${TEST_TARGET_NAME} --test_devices_count=${UR_TEST_DEVICES_COUNT} --test_platforms_count=${UR_TEST_PLATFORMS_COUNT}"
1618
-D MODE=stdout
1719
-D MATCH_FILE=${CMAKE_CURRENT_SOURCE_DIR}/${name}_${adapter}.match
1820
-P ${PROJECT_SOURCE_DIR}/cmake/match.cmake

test/conformance/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,15 @@ In the future, when all bugs are fixed, and the tests pass,
88
this solution will no longer be necessary.
99
When you fix any test, the match file must be updated
1010
Empty match files indicate that there are no failing tests
11-
in a particular group for the corresponding adapter.
11+
in a particular group for the corresponding adapter.
12+
13+
## How to set test device/platform name or limit the test devices/platforms count
14+
15+
To limit how many devices/platforms you want to run the CTS on,
16+
use CMake option UR_TEST_DEVICES_COUNT or
17+
UR_TEST_PLATFORMS_COUNT. If you want to run the tests on
18+
all available devices/platforms, set 0. The default value is 1.
19+
If you run binaries for the tests, you can use the parameter
20+
`--platforms_count=COUNT` or `--devices_count=COUNT`.
21+
To set test device/platform name you want to run the CTS on, use
22+
parameter `--platform=NAME` or `--device=NAME`.

test/conformance/cts_exe.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@
2020

2121
parser = ArgumentParser()
2222
parser.add_argument("--test_command", help="Ctest test case")
23+
parser.add_argument("--test_devices_count", type=str, help="Number of devices on which tests will be run")
24+
parser.add_argument("--test_platforms_count", type=str, help="Number of platforms on which tests will be run")
2325
args = parser.parse_args()
2426

25-
result = subprocess.Popen([args.test_command, '--gtest_brief=1'], stdout = subprocess.PIPE, text = True) # nosec B603
27+
result = subprocess.Popen([args.test_command, '--gtest_brief=1', f'--devices_count={args.test_devices_count}',
28+
f'--platforms_count={args.test_platforms_count}'],
29+
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) # nosec B603
2630

2731
pat = re.compile(r'\[( )*FAILED( )*\]')
2832
output_list = []

test/conformance/source/environment.cpp

Lines changed: 108 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See LICENSE.TXT
44
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55

6+
#include <algorithm>
67
#include <cstring>
78
#include <fstream>
89

@@ -41,6 +42,23 @@ std::ostream &operator<<(std::ostream &out,
4142
return out;
4243
}
4344

45+
std::ostream &operator<<(std::ostream &out, const ur_device_handle_t &device) {
46+
size_t size;
47+
urDeviceGetInfo(device, UR_DEVICE_INFO_NAME, 0, nullptr, &size);
48+
std::vector<char> name(size);
49+
urDeviceGetInfo(device, UR_DEVICE_INFO_NAME, size, name.data(), nullptr);
50+
out << name.data();
51+
return out;
52+
}
53+
54+
std::ostream &operator<<(std::ostream &out,
55+
const std::vector<ur_device_handle_t> &devices) {
56+
for (auto device : devices) {
57+
out << "\n * \"" << device << "\"";
58+
}
59+
return out;
60+
}
61+
4462
uur::PlatformEnvironment::PlatformEnvironment(int argc, char **argv)
4563
: platform_options{parsePlatformOptions(argc, argv)} {
4664
instance = this;
@@ -100,14 +118,16 @@ uur::PlatformEnvironment::PlatformEnvironment(int argc, char **argv)
100118
}
101119

102120
if (platform_options.platform_name.empty()) {
103-
if (platforms.size() == 1) {
121+
122+
if (platforms.size() == 1 || platform_options.platforms_count == 1) {
104123
platform = platforms[0];
105124
} else {
106125
std::stringstream ss_error;
107126
ss_error << "Select a single platform from below using the "
108127
"--platform=NAME "
109128
"command-line option:"
110-
<< platforms;
129+
<< platforms << std::endl
130+
<< "or set --platforms_count=1.";
111131
error = ss_error.str();
112132
return;
113133
}
@@ -136,7 +156,8 @@ uur::PlatformEnvironment::PlatformEnvironment(int argc, char **argv)
136156
<< "\" not found. Select a single platform from below "
137157
"using the "
138158
"--platform=NAME command-line options:"
139-
<< platforms;
159+
<< platforms << std::endl
160+
<< "or set --platforms_count=1.";
140161
error = ss_error.str();
141162
return;
142163
}
@@ -177,6 +198,10 @@ PlatformEnvironment::parsePlatformOptions(int argc, char **argv) {
177198
arg, "--platform=", sizeof("--platform=") - 1) == 0) {
178199
options.platform_name =
179200
std::string(&arg[std::strlen("--platform=")]);
201+
} else if (std::strncmp(arg, "--platforms_count=",
202+
sizeof("--platforms_count=") - 1) == 0) {
203+
options.platforms_count = std::strtoul(
204+
&arg[std::strlen("--platforms_count=")], nullptr, 10);
180205
}
181206
}
182207

@@ -192,10 +217,31 @@ PlatformEnvironment::parsePlatformOptions(int argc, char **argv) {
192217
return options;
193218
}
194219

220+
DevicesEnvironment::DeviceOptions
221+
DevicesEnvironment::parseDeviceOptions(int argc, char **argv) {
222+
DeviceOptions options;
223+
for (int argi = 1; argi < argc; ++argi) {
224+
const char *arg = argv[argi];
225+
if (!(std::strcmp(arg, "-h") && std::strcmp(arg, "--help"))) {
226+
// TODO - print help
227+
break;
228+
} else if (std::strncmp(arg, "--device=", sizeof("--device=") - 1) ==
229+
0) {
230+
options.device_name = std::string(&arg[std::strlen("--device=")]);
231+
} else if (std::strncmp(arg, "--devices_count=",
232+
sizeof("--devices_count=") - 1) == 0) {
233+
options.devices_count = std::strtoul(
234+
&arg[std::strlen("--devices_count=")], nullptr, 10);
235+
}
236+
}
237+
return options;
238+
}
239+
195240
DevicesEnvironment *DevicesEnvironment::instance = nullptr;
196241

197242
DevicesEnvironment::DevicesEnvironment(int argc, char **argv)
198-
: PlatformEnvironment(argc, argv) {
243+
: PlatformEnvironment(argc, argv),
244+
device_options(parseDeviceOptions(argc, argv)) {
199245
instance = this;
200246
if (!error.empty()) {
201247
return;
@@ -209,11 +255,64 @@ DevicesEnvironment::DevicesEnvironment(int argc, char **argv)
209255
error = "Could not find any devices associated with the platform";
210256
return;
211257
}
212-
devices.resize(count);
213-
if (urDeviceGet(platform, UR_DEVICE_TYPE_ALL, count, devices.data(),
214-
nullptr)) {
215-
error = "urDeviceGet() failed to get devices.";
216-
return;
258+
259+
// Get the argument (devices_count) to limit test devices count.
260+
// In case, the devices_count is "0", the variable count will not be changed.
261+
// The CTS will run on all devices.
262+
if (device_options.device_name.empty()) {
263+
if (device_options.devices_count >
264+
(std::numeric_limits<uint32_t>::max)()) {
265+
error = "Invalid devices_count argument";
266+
return;
267+
} else if (device_options.devices_count > 0) {
268+
count = (std::min)(
269+
count, static_cast<uint32_t>(device_options.devices_count));
270+
}
271+
devices.resize(count);
272+
if (urDeviceGet(platform, UR_DEVICE_TYPE_ALL, count, devices.data(),
273+
nullptr)) {
274+
error = "urDeviceGet() failed to get devices.";
275+
return;
276+
}
277+
} else {
278+
devices.resize(count);
279+
if (urDeviceGet(platform, UR_DEVICE_TYPE_ALL, count, devices.data(),
280+
nullptr)) {
281+
error = "urDeviceGet() failed to get devices.";
282+
return;
283+
}
284+
for (u_long i = 0; i < count; i++) {
285+
size_t size;
286+
if (urDeviceGetInfo(devices[i], UR_DEVICE_INFO_NAME, 0, nullptr,
287+
&size)) {
288+
error = "urDeviceGetInfo() failed";
289+
return;
290+
}
291+
std::vector<char> device_name(size);
292+
if (urDeviceGetInfo(devices[i], UR_DEVICE_INFO_NAME, size,
293+
device_name.data(), nullptr)) {
294+
error = "urDeviceGetInfo() failed";
295+
return;
296+
}
297+
if (device_options.device_name == device_name.data()) {
298+
device = devices[i];
299+
devices.clear();
300+
devices.resize(1);
301+
devices[0] = device;
302+
break;
303+
}
304+
}
305+
if (!device) {
306+
std::stringstream ss_error;
307+
ss_error << "Device \"" << device_options.device_name
308+
<< "\" not found. Select a single device from below "
309+
"using the "
310+
"--device=NAME command-line options:"
311+
<< devices << std::endl
312+
<< "or set --devices_count=COUNT.";
313+
error = ss_error.str();
314+
return;
315+
}
217316
}
218317
}
219318

test/conformance/testing/include/uur/environment.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct PlatformEnvironment : ::testing::Environment {
1717

1818
struct PlatformOptions {
1919
std::string platform_name;
20+
unsigned long platforms_count;
2021
};
2122

2223
PlatformEnvironment(int argc, char **argv);
@@ -36,17 +37,26 @@ struct PlatformEnvironment : ::testing::Environment {
3637

3738
struct DevicesEnvironment : PlatformEnvironment {
3839

40+
struct DeviceOptions {
41+
std::string device_name;
42+
unsigned long devices_count;
43+
};
44+
3945
DevicesEnvironment(int argc, char **argv);
4046
virtual ~DevicesEnvironment() override = default;
4147

4248
virtual void SetUp() override;
4349
virtual void TearDown() override;
4450

51+
DeviceOptions parseDeviceOptions(int argc, char **argv);
52+
4553
inline const std::vector<ur_device_handle_t> &GetDevices() const {
4654
return devices;
4755
}
4856

57+
DeviceOptions device_options;
4958
std::vector<ur_device_handle_t> devices;
59+
ur_device_handle_t device = nullptr;
5060
static DevicesEnvironment *instance;
5161
};
5262

0 commit comments

Comments
 (0)