Skip to content

Commit ae4419f

Browse files
nyallocbader
authored andcommitted
[SYCL] Initial PI API unit test setup (#407)
This patch introduces the initial structure for unit testing the PI API. Tests are provided for piPlatformsGet and piPlatformsGetInfo. Test stubs are defined for the piProgram functions. Signed-off-by: Stuart Adams <stuart.adams@codeplay.com>
1 parent 90333c3 commit ae4419f

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed

Diff for: sycl/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,5 @@ add_custom_target( sycl-toolchain
124124
)
125125

126126
add_subdirectory( test )
127+
add_subdirectory( unittests )
127128
add_subdirectory( tools )

Diff for: sycl/include/CL/sycl/detail/pi.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ namespace pi {
5151
using PiMemImageChannelOrder = ::pi_image_channel_order;
5252
using PiMemImageChannelType = ::pi_image_channel_type;
5353

54+
// Get a string representing a _pi_platform_info enum
55+
std::string platformInfoToString(pi_platform_info info);
56+
5457
// Report error and no return (keeps compiler happy about no return statements).
5558
[[noreturn]] void piDie(const char *Message);
5659
void piAssert(bool Condition, const char *Message = nullptr);

Diff for: sycl/source/detail/pi.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ namespace sycl {
1616
namespace detail {
1717
namespace pi {
1818

19+
std::string platformInfoToString(pi_platform_info info) {
20+
switch (info) {
21+
case PI_PLATFORM_INFO_PROFILE:
22+
return "PI_PLATFORM_INFO_PROFILE";
23+
case PI_PLATFORM_INFO_VERSION:
24+
return "PI_PLATFORM_INFO_VERSION";
25+
case PI_PLATFORM_INFO_NAME:
26+
return "PI_PLATFORM_INFO_NAME";
27+
case PI_PLATFORM_INFO_VENDOR:
28+
return "PI_PLATFORM_INFO_VENDOR";
29+
case PI_PLATFORM_INFO_EXTENSIONS:
30+
return "PI_PLATFORM_INFO_EXTENSIONS";
31+
default:
32+
piDie("Unknown pi_platform_info value passed to "
33+
"cl::sycl::detail::pi::platformInfoToString");
34+
}
35+
}
36+
1937
// Check for manually selected BE at run-time.
2038
bool piUseBackend(PiBackend Backend) {
2139
static const char *GetEnv = std::getenv("SYCL_BE");

Diff for: sycl/unittests/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_custom_target(PiUnitTests)
2+
set_target_properties(PiUnitTests PROPERTIES FOLDER "Tests")
3+
4+
function(add_llvm_unittest test_dirname)
5+
add_unittest(PiUnitTests ${test_dirname} ${ARGN})
6+
endfunction()
7+
8+
function(add_llvm_unittest_with_input_files test_dirname)
9+
add_unittest_with_input_files(PiUnitTests ${test_dirname} ${ARGN})
10+
endfunction()
11+
12+
add_subdirectory(pi)

Diff for: sycl/unittests/pi/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
set(CMAKE_CXX_STANDARD 11)
2+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
3+
set(CMAKE_CXX_EXTENSIONS OFF)
4+
5+
# Enable exception handling for these unit tests
6+
set(LLVM_REQUIRES_EH 1)
7+
add_llvm_unittest(PiTests
8+
PlatformTest.cpp
9+
)
10+
11+
add_dependencies(PiTests sycl)
12+
target_link_libraries(PiTests PRIVATE sycl LLVMTestingSupport)

Diff for: sycl/unittests/pi/PlatformTest.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//==---- PlatformTest.cpp --- PI unit tests --------------------------------==//
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 "CL/sycl/detail/pi.hpp"
10+
#include <CL/sycl.hpp>
11+
#include <gtest/gtest.h>
12+
#include <memory>
13+
14+
using namespace cl::sycl;
15+
16+
namespace pi {
17+
class PlatformTest : public ::testing::Test {
18+
protected:
19+
20+
constexpr static size_t out_string_size =
21+
8192u; // Using values from OpenCL CTS clGetPlatforms test
22+
23+
PlatformTest() { detail::pi::piInitialize(); }
24+
25+
~PlatformTest() = default;
26+
};
27+
28+
TEST_F(PlatformTest, piPlatformsGet) {
29+
pi_uint32 platformCount = 0;
30+
31+
ASSERT_EQ(PI_CALL_RESULT(RT::piPlatformsGet(0, 0, &platformCount)),
32+
PI_SUCCESS)
33+
<< "piPlatformsGet failed";
34+
35+
ASSERT_GT(platformCount, 0) << "piPlatformsGet found 0 platforms.\n";
36+
37+
std::vector<pi_platform> platforms(platformCount);
38+
39+
ASSERT_EQ(PI_CALL_RESULT(
40+
RT::piPlatformsGet(platformCount, platforms.data(), nullptr)),
41+
PI_SUCCESS)
42+
<< "piPlatformsGet failed with nullptr for return size.\n";
43+
}
44+
45+
TEST_F(PlatformTest, piPlatformGetInfo) {
46+
auto get_info_test = [](char *out_string, pi_platform platform,
47+
_pi_platform_info info) {
48+
49+
auto info_name = detail::pi::platformInfoToString(info);
50+
51+
size_t reported_string_length = 0;
52+
memset(out_string, 0, out_string_size);
53+
54+
ASSERT_EQ(PI_CALL_RESULT(RT::piPlatformGetInfo(platform, info,
55+
out_string_size, out_string,
56+
&reported_string_length)),
57+
PI_SUCCESS)
58+
<< "piPlatformGetInfo for " << info_name << " failed.\n";
59+
60+
auto returned_string_length = strlen(out_string) + 1;
61+
62+
EXPECT_EQ(returned_string_length, reported_string_length)
63+
<< "Returned string length " << returned_string_length
64+
<< " does not equal reported string length " << reported_string_length
65+
<< ".\n";
66+
};
67+
68+
pi_uint32 platformCount = 0;
69+
PI_CALL(RT::piPlatformsGet(0, 0, &platformCount));
70+
std::vector<pi_platform> platforms(platformCount);
71+
PI_CALL(RT::piPlatformsGet(platformCount, platforms.data(), nullptr));
72+
73+
auto out_string_buffer = std::unique_ptr<char[]>(new char[out_string_size]);
74+
auto out_string = out_string_buffer.get();
75+
76+
for (auto i = 0u; i < platformCount; ++i) {
77+
const auto &platform = platforms[i];
78+
get_info_test(out_string, platform, PI_PLATFORM_INFO_NAME);
79+
get_info_test(out_string, platform, PI_PLATFORM_INFO_VENDOR);
80+
get_info_test(out_string, platform, PI_PLATFORM_INFO_PROFILE);
81+
get_info_test(out_string, platform, PI_PLATFORM_INFO_VERSION);
82+
get_info_test(out_string, platform, PI_PLATFORM_INFO_EXTENSIONS);
83+
}
84+
}
85+
} // Namespace

0 commit comments

Comments
 (0)