1 /*
2  * Copyright (C) 2018-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #include "shared/source/helpers/get_info.h"
9 #include "shared/source/helpers/string.h"
10 
11 #include "opencl/source/cl_device/cl_device_info_map.h"
12 #include "opencl/test/unit_test/fixtures/cl_device_fixture.h"
13 #include "opencl/test/unit_test/mocks/mock_platform.h"
14 #include "opencl/test/unit_test/mocks/ult_cl_device_factory.h"
15 
16 #include "gtest/gtest.h"
17 
18 #include <memory>
19 
20 namespace NEO {
21 extern const char *latestConformanceVersionPassed;
22 } // namespace NEO
23 
24 using namespace NEO;
25 
26 struct GetDeviceInfoSize : public ::testing::TestWithParam<std::pair<uint32_t /*cl_device_info*/, size_t>> {
SetUpGetDeviceInfoSize27     void SetUp() override {
28         param = GetParam();
29     }
30 
31     std::pair<uint32_t, size_t> param;
32 };
33 
TEST_P(GetDeviceInfoSize,GivenParamWhenGettingDeviceInfoThenSizeIsValid)34 TEST_P(GetDeviceInfoSize, GivenParamWhenGettingDeviceInfoThenSizeIsValid) {
35     auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
36 
37     size_t sizeReturned = GetInfo::invalidSourceSize;
38     auto retVal = device->getDeviceInfo(
39         param.first,
40         0,
41         nullptr,
42         &sizeReturned);
43     if (CL_SUCCESS != retVal) {
44         ASSERT_EQ(CL_SUCCESS, retVal) << " param = " << param.first;
45     }
46     ASSERT_NE(GetInfo::invalidSourceSize, sizeReturned);
47     EXPECT_EQ(param.second, sizeReturned);
48 }
49 
50 std::pair<uint32_t, size_t> deviceInfoParams2[] = {
51     {CL_DEVICE_ADDRESS_BITS, sizeof(cl_uint)},
52     {CL_DEVICE_AVAILABLE, sizeof(cl_bool)},
53     //    {CL_DEVICE_BUILT_IN_KERNELS_WITH_VERSION, sizeof(cl_name_version[])},
54     {CL_DEVICE_COMPILER_AVAILABLE, sizeof(cl_bool)},
55     {CL_DEVICE_DOUBLE_FP_CONFIG, sizeof(cl_device_fp_config)},
56     {CL_DEVICE_ENDIAN_LITTLE, sizeof(cl_bool)},
57     {CL_DEVICE_ERROR_CORRECTION_SUPPORT, sizeof(cl_bool)},
58     {CL_DEVICE_EXECUTION_CAPABILITIES, sizeof(cl_device_exec_capabilities)},
59     //    {CL_DEVICE_EXTENSIONS, sizeof(char[])},
60     //    {CL_DEVICE_EXTENSIONS_WITH_VERSION, sizeof(cl_name_version[])},
61     {CL_DEVICE_GLOBAL_MEM_CACHE_SIZE, sizeof(cl_ulong)},
62     {CL_DEVICE_GLOBAL_MEM_CACHE_TYPE, sizeof(cl_device_mem_cache_type)},
63     {CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE, sizeof(cl_uint)},
64     {CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(cl_ulong)},
65     {CL_DEVICE_GLOBAL_VARIABLE_PREFERRED_TOTAL_SIZE, sizeof(size_t)},
66     {CL_DEVICE_ILS_WITH_VERSION, sizeof(cl_name_version[1])},
67     {CL_DEVICE_IL_VERSION, sizeof(char[12])},
68     {CL_DEVICE_IMAGE_SUPPORT, sizeof(cl_bool)},
69     {CL_DEVICE_LATEST_CONFORMANCE_VERSION_PASSED, strlen(latestConformanceVersionPassed) + 1},
70     {CL_DEVICE_LINKER_AVAILABLE, sizeof(cl_bool)},
71     {CL_DEVICE_LOCAL_MEM_SIZE, sizeof(cl_ulong)},
72     {CL_DEVICE_LOCAL_MEM_TYPE, sizeof(cl_device_local_mem_type)},
73     {CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(cl_uint)},
74     {CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint)},
75     {CL_DEVICE_MAX_CONSTANT_ARGS, sizeof(cl_uint)},
76     {CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE, sizeof(cl_ulong)},
77     {CL_DEVICE_MAX_GLOBAL_VARIABLE_SIZE, sizeof(size_t)},
78     {CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(cl_ulong)},
79     {CL_DEVICE_MAX_NUM_SUB_GROUPS, sizeof(cl_uint)},
80     {CL_DEVICE_MAX_ON_DEVICE_EVENTS, sizeof(cl_uint)},
81     {CL_DEVICE_MAX_ON_DEVICE_QUEUES, sizeof(cl_uint)},
82     {CL_DEVICE_MAX_PARAMETER_SIZE, sizeof(size_t)},
83     {CL_DEVICE_MAX_PIPE_ARGS, sizeof(cl_uint)},
84     {CL_DEVICE_MAX_SAMPLERS, sizeof(cl_uint)},
85     {CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t)},
86     {CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(cl_uint)},
87     {CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(size_t[3])},
88     {CL_DEVICE_MEM_BASE_ADDR_ALIGN, sizeof(cl_uint)},
89     //    {CL_DEVICE_NAME, sizeof(char[])},
90     {CL_DEVICE_NATIVE_VECTOR_WIDTH_CHAR, sizeof(cl_uint)},
91     {CL_DEVICE_NATIVE_VECTOR_WIDTH_SHORT, sizeof(cl_uint)},
92     {CL_DEVICE_NATIVE_VECTOR_WIDTH_INT, sizeof(cl_uint)},
93     {CL_DEVICE_NATIVE_VECTOR_WIDTH_LONG, sizeof(cl_uint)},
94     {CL_DEVICE_NATIVE_VECTOR_WIDTH_FLOAT, sizeof(cl_uint)},
95     {CL_DEVICE_NATIVE_VECTOR_WIDTH_DOUBLE, sizeof(cl_uint)},
96     {CL_DEVICE_NATIVE_VECTOR_WIDTH_HALF, sizeof(cl_uint)},
97     {CL_DEVICE_NUMERIC_VERSION, sizeof(cl_version)},
98     //    {CL_DEVICE_OPENCL_C_ALL_VERSIONS, sizeof(cl_name_version[])},
99     //    {CL_DEVICE_OPENCL_C_FEATURES, sizeof(cl_name_version[])},
100     //    {CL_DEVICE_OPENCL_C_VERSION, sizeof(char[])},
101     {CL_DEVICE_PARENT_DEVICE, sizeof(cl_device_id)},
102     {CL_DEVICE_PARTITION_AFFINITY_DOMAIN, sizeof(cl_device_affinity_domain)},
103     {CL_DEVICE_PARTITION_MAX_SUB_DEVICES, sizeof(cl_uint)},
104     {CL_DEVICE_PIPE_MAX_ACTIVE_RESERVATIONS, sizeof(cl_uint)},
105     {CL_DEVICE_PIPE_MAX_PACKET_SIZE, sizeof(cl_uint)},
106     {CL_DEVICE_PLATFORM, sizeof(cl_platform_id)},
107     {CL_DEVICE_PREFERRED_GLOBAL_ATOMIC_ALIGNMENT, sizeof(cl_uint)},
108     {CL_DEVICE_PREFERRED_INTEROP_USER_SYNC, sizeof(cl_bool)},
109     {CL_DEVICE_PREFERRED_LOCAL_ATOMIC_ALIGNMENT, sizeof(cl_uint)},
110     {CL_DEVICE_PREFERRED_PLATFORM_ATOMIC_ALIGNMENT, sizeof(cl_uint)},
111     {CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint)},
112     {CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT, sizeof(cl_uint)},
113     {CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint)},
114     {CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG, sizeof(cl_uint)},
115     {CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT, sizeof(cl_uint)},
116     {CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE, sizeof(cl_uint)},
117     {CL_DEVICE_PREFERRED_VECTOR_WIDTH_HALF, sizeof(cl_uint)},
118     {CL_DEVICE_PREFERRED_WORK_GROUP_SIZE_MULTIPLE, sizeof(size_t)},
119     {CL_DEVICE_PRINTF_BUFFER_SIZE, sizeof(size_t)},
120     //    {CL_DEVICE_PROFILE, sizeof(char[])},
121     {CL_DEVICE_PROFILING_TIMER_RESOLUTION, sizeof(size_t)},
122     {CL_DEVICE_QUEUE_ON_DEVICE_MAX_SIZE, sizeof(cl_uint)},
123     {CL_DEVICE_QUEUE_ON_DEVICE_PREFERRED_SIZE, sizeof(cl_uint)},
124     {CL_DEVICE_QUEUE_ON_DEVICE_PROPERTIES, sizeof(cl_command_queue_properties)},
125     {CL_DEVICE_QUEUE_ON_HOST_PROPERTIES, sizeof(cl_command_queue_properties)},
126     {CL_DEVICE_REFERENCE_COUNT, sizeof(cl_uint)},
127     {CL_DEVICE_SINGLE_FP_CONFIG, sizeof(cl_device_fp_config)},
128     //    {CL_DEVICE_SPIR_VERSIONS, sizeof(char[])},
129     {CL_DEVICE_SUB_GROUP_INDEPENDENT_FORWARD_PROGRESS, sizeof(cl_bool)},
130     {CL_DEVICE_SVM_CAPABILITIES, sizeof(cl_device_svm_capabilities)},
131     //    {CL_DEVICE_TERMINATE_CAPABILITY_KHR, sizeof(cl_device_terminate_capability_khr)},
132     {CL_DEVICE_TYPE, sizeof(cl_device_type)},
133     //    {CL_DEVICE_VENDOR, sizeof(char[])},
134     {CL_DEVICE_VENDOR_ID, sizeof(cl_uint)},
135     //    {CL_DEVICE_VERSION, sizeof(char[])},
136     //    {CL_DRIVER_VERSION, sizeof(char[])},
137 };
138 
139 INSTANTIATE_TEST_CASE_P(
140     Device_,
141     GetDeviceInfoSize,
142     testing::ValuesIn(deviceInfoParams2));
143 
144 struct GetDeviceInfoForImage : public GetDeviceInfoSize {};
145 
TEST_P(GetDeviceInfoForImage,GivenParamWhenGettingDeviceInfoThenSizeIsValid)146 TEST_P(GetDeviceInfoForImage, GivenParamWhenGettingDeviceInfoThenSizeIsValid) {
147     auto device = std::make_unique<ClDevice>(*MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr), platform());
148     if (!device->getSharedDeviceInfo().imageSupport) {
149         GTEST_SKIP();
150     }
151     size_t sizeReturned = 0;
152     auto retVal = device->getDeviceInfo(
153         param.first,
154         0,
155         nullptr,
156         &sizeReturned);
157     if (CL_SUCCESS != retVal) {
158         ASSERT_EQ(CL_SUCCESS, retVal) << " param = " << param.first;
159     }
160     ASSERT_NE(0u, sizeReturned);
161     EXPECT_EQ(param.second, sizeReturned);
162 }
163 
TEST_P(GetDeviceInfoForImage,whenImageAreNotSupportedThenClSuccessAndSizeofCluintIsReturned)164 TEST_P(GetDeviceInfoForImage, whenImageAreNotSupportedThenClSuccessAndSizeofCluintIsReturned) {
165     auto device = std::make_unique<ClDevice>(*MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr), platform());
166     if (device->getSharedDeviceInfo().imageSupport) {
167         GTEST_SKIP();
168     }
169     size_t sizeReturned = 0;
170     auto retVal = device->getDeviceInfo(
171         param.first,
172         0,
173         nullptr,
174         &sizeReturned);
175     EXPECT_EQ(CL_SUCCESS, retVal);
176     EXPECT_EQ(param.second, sizeReturned);
177 }
178 
TEST_P(GetDeviceInfoForImage,givenInfoImageParamsWhenCallGetDeviceInfoForImageThenSizeIsValidAndTrueReturned)179 TEST_P(GetDeviceInfoForImage, givenInfoImageParamsWhenCallGetDeviceInfoForImageThenSizeIsValidAndTrueReturned) {
180     auto device = std::make_unique<ClDevice>(*MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr), platform());
181     size_t srcSize = 0;
182     size_t retSize = 0;
183     const void *src = nullptr;
184     auto retVal = device->getDeviceInfoForImage(
185         param.first,
186         src,
187         srcSize,
188         retSize);
189     EXPECT_TRUE(retVal);
190     ASSERT_NE(0u, srcSize);
191     EXPECT_EQ(param.second, srcSize);
192     EXPECT_EQ(param.second, retSize);
193 }
194 
TEST(GetDeviceInfoForImage,givenNotImageParamWhenCallGetDeviceInfoForImageThenSizeIsNotValidAndFalseReturned)195 TEST(GetDeviceInfoForImage, givenNotImageParamWhenCallGetDeviceInfoForImageThenSizeIsNotValidAndFalseReturned) {
196     auto device = std::make_unique<ClDevice>(*MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr), platform());
197     size_t srcSize = 0;
198     size_t retSize = 0;
199     const void *src = nullptr;
200     cl_device_info notImageParam = CL_DEVICE_ADDRESS_BITS;
201     size_t paramSize = sizeof(cl_uint);
202     auto retVal = device->getDeviceInfoForImage(
203         notImageParam,
204         src,
205         srcSize,
206         retSize);
207     EXPECT_FALSE(retVal);
208     EXPECT_EQ(0u, srcSize);
209     EXPECT_NE(paramSize, srcSize);
210     EXPECT_NE(paramSize, retSize);
211 }
212 
213 std::pair<uint32_t, size_t> deviceInfoImageParams[] = {
214     {CL_DEVICE_IMAGE2D_MAX_HEIGHT, sizeof(size_t)},
215     {CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof(size_t)},
216     {CL_DEVICE_IMAGE3D_MAX_DEPTH, sizeof(size_t)},
217     {CL_DEVICE_IMAGE3D_MAX_HEIGHT, sizeof(size_t)},
218     {CL_DEVICE_IMAGE3D_MAX_WIDTH, sizeof(size_t)},
219     {CL_DEVICE_IMAGE_BASE_ADDRESS_ALIGNMENT, sizeof(cl_uint)},
220     {CL_DEVICE_IMAGE_MAX_ARRAY_SIZE, sizeof(size_t)},
221     {CL_DEVICE_IMAGE_MAX_BUFFER_SIZE, sizeof(size_t)},
222     {CL_DEVICE_IMAGE_PITCH_ALIGNMENT, sizeof(cl_uint)},
223     {CL_DEVICE_MAX_READ_IMAGE_ARGS, sizeof(cl_uint)},
224     {CL_DEVICE_MAX_READ_WRITE_IMAGE_ARGS, sizeof(cl_uint)},
225     {CL_DEVICE_MAX_WRITE_IMAGE_ARGS, sizeof(cl_uint)},
226 };
227 
228 INSTANTIATE_TEST_CASE_P(
229     Device_,
230     GetDeviceInfoForImage,
231     testing::ValuesIn(deviceInfoImageParams));
232 
TEST(DeviceInfoTests,givenDefaultDeviceWhenQueriedForDeviceVersionThenProperSizeIsReturned)233 TEST(DeviceInfoTests, givenDefaultDeviceWhenQueriedForDeviceVersionThenProperSizeIsReturned) {
234     auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
235     size_t sizeReturned = 0;
236     auto retVal = device->getDeviceInfo(
237         CL_DEVICE_VERSION,
238         0,
239         nullptr,
240         &sizeReturned);
241 
242     EXPECT_EQ(CL_SUCCESS, retVal);
243     EXPECT_EQ(16u, sizeReturned);
244     std::unique_ptr<char[]> deviceVersion(new char[sizeReturned]);
245 
246     retVal = device->getDeviceInfo(
247         CL_DEVICE_VERSION,
248         sizeReturned,
249         deviceVersion.get(),
250         nullptr);
251 
252     EXPECT_EQ(CL_SUCCESS, retVal);
253 }
254 
TEST(DeviceInfoTests,givenDefaultDeviceWhenQueriedForBuiltInKernelsWithVersionThenProperSizeIsReturned)255 TEST(DeviceInfoTests, givenDefaultDeviceWhenQueriedForBuiltInKernelsWithVersionThenProperSizeIsReturned) {
256     UltClDeviceFactory deviceFactory{1, 0};
257     auto pClDevice = deviceFactory.rootDevices[0];
258     size_t sizeReturned = 0;
259     auto retVal = pClDevice->getDeviceInfo(
260         CL_DEVICE_BUILT_IN_KERNELS_WITH_VERSION,
261         0,
262         nullptr,
263         &sizeReturned);
264 
265     EXPECT_EQ(CL_SUCCESS, retVal);
266     EXPECT_EQ(pClDevice->getDeviceInfo().builtInKernelsWithVersion.size() * sizeof(cl_name_version), sizeReturned);
267 }
268 
TEST(DeviceInfoTests,givenDefaultDeviceWhenQueriedForOpenclCAllVersionsThenProperSizeIsReturned)269 TEST(DeviceInfoTests, givenDefaultDeviceWhenQueriedForOpenclCAllVersionsThenProperSizeIsReturned) {
270     UltClDeviceFactory deviceFactory{1, 0};
271     auto pClDevice = deviceFactory.rootDevices[0];
272     size_t sizeReturned = 0;
273     auto retVal = pClDevice->getDeviceInfo(
274         CL_DEVICE_OPENCL_C_ALL_VERSIONS,
275         0,
276         nullptr,
277         &sizeReturned);
278 
279     EXPECT_EQ(CL_SUCCESS, retVal);
280     EXPECT_EQ(pClDevice->getDeviceInfo().openclCAllVersions.size() * sizeof(cl_name_version), sizeReturned);
281 }
282 
TEST(DeviceInfoTests,givenDefaultDeviceWhenQueriedForOpenclCFeaturesThenProperSizeIsReturned)283 TEST(DeviceInfoTests, givenDefaultDeviceWhenQueriedForOpenclCFeaturesThenProperSizeIsReturned) {
284     UltClDeviceFactory deviceFactory{1, 0};
285     auto pClDevice = deviceFactory.rootDevices[0];
286     size_t sizeReturned = 0;
287     auto retVal = pClDevice->getDeviceInfo(
288         CL_DEVICE_OPENCL_C_FEATURES,
289         0,
290         nullptr,
291         &sizeReturned);
292 
293     EXPECT_EQ(CL_SUCCESS, retVal);
294     EXPECT_EQ(pClDevice->getDeviceInfo().openclCFeatures.size() * sizeof(cl_name_version), sizeReturned);
295 }
296 
TEST(DeviceInfoTests,givenDefaultDeviceWhenQueriedForExtensionsWithVersionThenProperSizeIsReturned)297 TEST(DeviceInfoTests, givenDefaultDeviceWhenQueriedForExtensionsWithVersionThenProperSizeIsReturned) {
298     UltClDeviceFactory deviceFactory{1, 0};
299     auto pClDevice = deviceFactory.rootDevices[0];
300     size_t sizeReturned = 0;
301     auto retVal = pClDevice->getDeviceInfo(
302         CL_DEVICE_EXTENSIONS_WITH_VERSION,
303         0,
304         nullptr,
305         &sizeReturned);
306 
307     EXPECT_EQ(CL_SUCCESS, retVal);
308     EXPECT_EQ(pClDevice->getDeviceInfo().extensionsWithVersion.size() * sizeof(cl_name_version), sizeReturned);
309 }
310