-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathtest_opencl_c_versions.cpp
More file actions
308 lines (258 loc) · 10.7 KB
/
test_opencl_c_versions.cpp
File metadata and controls
308 lines (258 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//
// Copyright (c) 2020 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "testBase.h"
#include "harness/featureHelpers.h"
#include <vector>
static const char* test_kernel = R"CLC(
__kernel void test(__global int* dst) {
dst[0] = 0;
}
)CLC";
// This sub-test checks that CL_DEVICE_OPENCL_C_VERSION meets any API
// requirements and that programs can be built for the reported OpenCL C version
// and all previous versions.
static int test_CL_DEVICE_OPENCL_C_VERSION(cl_device_id device,
cl_context context)
{
const Version latest_version = Version(3, 0);
const Version api_version = get_device_cl_version(device);
const Version clc_version = get_device_cl_c_version(device);
if (api_version > latest_version)
{
log_info("CL_DEVICE_VERSION is %s, which is bigger than %s.\n"
"Need to update the opencl_c_versions test!\n",
api_version.to_string().c_str(),
latest_version.to_string().c_str());
}
if (clc_version > latest_version)
{
log_info("CL_DEVICE_OPENCL_C_VERSION is %s, which is bigger than %s.\n"
"Need to update the opencl_c_versions test!\n",
clc_version.to_string().c_str(),
latest_version.to_string().c_str());
}
// For OpenCL 3.0, the minimum required OpenCL C version is OpenCL C 1.2.
// For OpenCL 2.x, the minimum required OpenCL C version is OpenCL C 2.0.
// For other OpenCL versions, the minimum required OpenCL C version is
// the same as the API version.
const Version min_clc_version = api_version == Version(3, 0) ? Version(1, 2)
: api_version >= Version(2, 0) ? Version(2, 0)
: api_version;
if (clc_version < min_clc_version)
{
log_error("The minimum required OpenCL C version for API version %s is "
"%s (got %s)!\n",
api_version.to_string().c_str(),
min_clc_version.to_string().c_str(),
clc_version.to_string().c_str());
return TEST_FAIL;
}
log_info(" testing compilation based on CL_DEVICE_OPENCL_C_VERSION\n");
struct TestCase
{
Version version;
const char* buildOptions;
};
std::vector<TestCase> tests;
tests.push_back({ Version(1, 1), "-cl-std=CL1.1" });
tests.push_back({ Version(1, 2), "-cl-std=CL1.2" });
tests.push_back({ Version(2, 0), "-cl-std=CL2.0" });
tests.push_back({ Version(3, 0), "-cl-std=CL3.0" });
for (const auto& testcase : tests)
{
if (clc_version >= testcase.version)
{
clProgramWrapper program;
clKernelWrapper kernel;
cl_int error = create_single_kernel_helper(
context, &program, &kernel, 1, &test_kernel, "test",
testcase.buildOptions);
test_error(error, "Unable to build program!");
log_info(" successfully built program with build options '%s'\n",
testcase.buildOptions);
}
}
return TEST_PASS;
}
// This sub-test checks that CL_DEVICE_OPENCL_C_ALL_VERSIONS includes any
// requirements for the API version, and that programs can be built for all
// reported versions.
static int test_CL_DEVICE_OPENCL_C_ALL_VERSIONS(cl_device_id device,
cl_context context)
{
// For now, the required OpenCL C version is the same as the API version.
const Version api_version = get_device_cl_version(device);
bool found_api_version = false;
log_info(
" testing compilation based on CL_DEVICE_OPENCL_C_ALL_VERSIONS\n");
cl_int error = CL_SUCCESS;
size_t sz = 0;
error =
clGetDeviceInfo(device, CL_DEVICE_OPENCL_C_ALL_VERSIONS, 0, NULL, &sz);
test_error(error, "Unable to query CL_DEVICE_OPENCL_C_ALL_VERSIONS size");
std::vector<cl_name_version> clc_versions(sz / sizeof(cl_name_version));
error = clGetDeviceInfo(device, CL_DEVICE_OPENCL_C_ALL_VERSIONS, sz,
clc_versions.data(), NULL);
test_error(error, "Unable to query CL_DEVICE_OPENCL_C_FEATURES");
for (const auto& clc_version : clc_versions)
{
const unsigned major = CL_VERSION_MAJOR(clc_version.version);
const unsigned minor = CL_VERSION_MINOR(clc_version.version);
if (strcmp(clc_version.name, "OpenCL C") == 0)
{
if (api_version == Version(major, minor))
{
found_api_version = true;
}
if (major == 1 && minor == 0)
{
log_info(
" skipping OpenCL C 1.0, there is no -cl-std=CL1.0.\n");
continue;
}
std::string buildOptions = "-cl-std=CL";
buildOptions += std::to_string(major);
buildOptions += ".";
buildOptions += std::to_string(minor);
clProgramWrapper program;
clKernelWrapper kernel;
error = create_single_kernel_helper(context, &program, &kernel, 1,
&test_kernel, "test",
buildOptions.c_str());
test_error(error, "Unable to build program!");
log_info(" successfully built program with build options '%s'\n",
buildOptions.c_str());
}
else
{
log_error(" unknown OpenCL C name '%s'.\n", clc_version.name);
return TEST_FAIL;
}
}
if (!found_api_version)
{
log_error(" didn't find required OpenCL C version '%s'!\n",
api_version.to_string().c_str());
return TEST_FAIL;
}
return TEST_PASS;
}
// This sub-test checks that any required features are present for a specific
// CL_DEVICE_OPENCL_C_VERSION.
static int test_CL_DEVICE_OPENCL_C_VERSION_features(cl_device_id device,
cl_context context)
{
log_info(" testing for OPENCL_C_VERSION required features\n");
OpenCLCFeatures features;
int error = get_device_cl_c_features(device, features);
if (error)
{
log_error("Couldn't query OpenCL C features for the device!\n");
return TEST_FAIL;
}
const Version clc_version = get_device_cl_c_version(device);
if (clc_version >= Version(2, 0))
{
bool has_all_OpenCL_C_20_features =
features.supports__opencl_c_atomic_order_acq_rel
&& features.supports__opencl_c_atomic_order_seq_cst
&& features.supports__opencl_c_atomic_scope_device
&& features.supports__opencl_c_atomic_scope_all_devices
&& features.supports__opencl_c_device_enqueue
&& features.supports__opencl_c_generic_address_space
&& features.supports__opencl_c_pipes
&& features.supports__opencl_c_program_scope_global_variables
&& features.supports__opencl_c_work_group_collective_functions;
if (features.supports__opencl_c_images)
{
has_all_OpenCL_C_20_features = has_all_OpenCL_C_20_features
&& features.supports__opencl_c_3d_image_writes
&& features.supports__opencl_c_read_write_images;
}
test_assert_error(
has_all_OpenCL_C_20_features,
"At least one required OpenCL C 2.0 feature is missing!");
}
return TEST_PASS;
}
// This sub-test checks that all required OpenCL C versions are present for a
// specific CL_DEVICE_OPENCL_C_VERSION.
static int test_CL_DEVICE_OPENCL_C_VERSION_versions(cl_device_id device,
cl_context context)
{
log_info(" testing for OPENCL_C_VERSION required versions\n");
const Version device_clc_version = get_device_cl_c_version(device);
std::vector<Version> test_clc_versions;
test_clc_versions.push_back(Version(1, 0));
test_clc_versions.push_back(Version(1, 1));
test_clc_versions.push_back(Version(1, 2));
test_clc_versions.push_back(Version(2, 0));
test_clc_versions.push_back(Version(3, 0));
cl_int error = CL_SUCCESS;
size_t sz = 0;
error =
clGetDeviceInfo(device, CL_DEVICE_OPENCL_C_ALL_VERSIONS, 0, NULL, &sz);
test_error(error, "Unable to query CL_DEVICE_OPENCL_C_ALL_VERSIONS size");
std::vector<cl_name_version> device_clc_versions(sz
/ sizeof(cl_name_version));
error = clGetDeviceInfo(device, CL_DEVICE_OPENCL_C_ALL_VERSIONS, sz,
device_clc_versions.data(), NULL);
test_error(error, "Unable to query CL_DEVICE_OPENCL_C_FEATURES");
for (const auto& test_clc_version : test_clc_versions)
{
if (device_clc_version >= test_clc_version)
{
bool found = false;
for (const auto& check : device_clc_versions)
{
const unsigned major = CL_VERSION_MAJOR(check.version);
const unsigned minor = CL_VERSION_MINOR(check.version);
if (strcmp(check.name, "OpenCL C") == 0
&& test_clc_version == Version(major, minor))
{
found = true;
break;
}
}
if (found)
{
log_info(" found OpenCL C version '%s'\n",
test_clc_version.to_string().c_str());
}
else
{
log_error("Didn't find OpenCL C version '%s'!\n",
test_clc_version.to_string().c_str());
return TEST_FAIL;
}
}
}
return TEST_PASS;
}
REGISTER_TEST(opencl_c_versions)
{
check_compiler_available(device);
const Version version = get_device_cl_version(device);
int result = TEST_PASS;
result |= test_CL_DEVICE_OPENCL_C_VERSION(device, context);
if (version >= Version(3, 0))
{
result |= test_CL_DEVICE_OPENCL_C_ALL_VERSIONS(device, context);
result |= test_CL_DEVICE_OPENCL_C_VERSION_features(device, context);
result |= test_CL_DEVICE_OPENCL_C_VERSION_versions(device, context);
}
return result;
}