forked from KhronosGroup/SYCL-CTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_base_opencl.cpp
321 lines (269 loc) · 10.7 KB
/
test_base_opencl.cpp
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
309
310
311
312
313
314
315
316
317
318
319
320
321
/*******************************************************************************
//
// SYCL 2020 Conformance Test Suite
//
// Copyright (c) 2017-2022 Codeplay Software LTD. All Rights Reserved.
// Copyright (c) 2022 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 "test_base_opencl.h"
#include "../tests/common/cts_selector.h"
#include "../tests/common/get_cts_object.h"
#include "../tests/common/macros.h"
#ifdef _MSC_VER
#include <windows.h>
#else
#include <unistd.h>
#endif
#ifdef SYCL_BACKEND_OPENCL
// conformance test suite namespace
namespace sycl_cts {
namespace util {
/** constructor which explicitly sets the OpenCL objects
* to nullptrs
*/
test_base_opencl::test_base_opencl()
: m_cl_platform_id(nullptr),
m_cl_device_id(nullptr),
m_cl_context(nullptr),
m_cl_command_queue(nullptr),
m_openKernels(),
m_openPrograms() {}
/** called before this test is executed
* @param log for emitting test notes and results
*/
bool test_base_opencl::setup(logger &log) {
/* get the OpenCLHelper object */
auto queue = util::get_cts_object::queue();
if (queue.get_backend() != sycl::backend::opencl) {
WARN(
"OpenCL interoperability part is not supported on non-OpenCL backend "
"types");
return false;
}
cl_int error = CL_SUCCESS;
const auto ctsContext = util::get_cts_object::context(cts_selector);
if (ctsContext.get_devices().empty()) {
FAIL(log, "Unable to retrieve list of devices via cts_selector");
return false;
}
const auto ctsDevice = ctsContext.get_devices()[0];
m_cl_platform_id =
sycl::get_native<sycl::backend::opencl>(ctsDevice.get_platform());
m_cl_device_id = sycl::get_native<sycl::backend::opencl>(ctsDevice);
cl_context_properties properties[3] = {
CL_CONTEXT_PLATFORM, (cl_context_properties)m_cl_platform_id, 0};
m_cl_context =
clCreateContext(properties, 1, &m_cl_device_id, nullptr, nullptr, &error);
if (!CHECK_CL_SUCCESS(log, error)) return false;
// No special queue properties wanted
m_cl_command_queue =
clCreateCommandQueue(m_cl_context, m_cl_device_id, 0, &error);
if (!CHECK_CL_SUCCESS(log, error)) return false;
return true;
}
bool test_base_opencl::online_compiler_supported(cl_device_id clDeviceId,
logger &log) {
cl_uint available;
cl_int error = clGetDeviceInfo(clDeviceId, CL_DEVICE_COMPILER_AVAILABLE,
sizeof(available), &available, NULL);
if (!CHECK_CL_SUCCESS(log, error)) return false;
if (available == 0) {
return false;
}
return true;
}
bool test_base_opencl::get_exec_dir(char *path, size_t max_path_len) {
assert(max_path_len > 0 && "No space to store the path");
#ifdef _MSC_VER
HMODULE hMod = GetModuleHandle(NULL);
if (!GetModuleFileNameA(hMod, path, max_path_len)) {
return false;
}
constexpr char pathDelim = '\\';
#else
ssize_t n = readlink("/proc/self/exe", path, max_path_len - 1);
if (n < 0) {
return false;
}
path[n] = '\0';
constexpr char pathDelim = '/';
#endif
// Replace all characters with '\0' from back until '/' or '\\'
for (size_t i = strnlen(path, max_path_len) - 1;
i > 0 && path[i] != pathDelim; --i) {
path[i] = '\0';
}
return true;
}
bool test_base_opencl::create_compiled_program(const std::string &source,
cl_program &out_program,
logger &log) {
return this->create_compiled_program(source, m_cl_context, m_cl_device_id,
out_program, log);
}
bool test_base_opencl::create_compiled_program(const std::string &source,
cl_context clContext,
cl_device_id clDeviceId,
cl_program &out_program,
logger &log) {
assert(!source.empty());
const char *source_c = source.c_str();
const size_t sourceSize = source.length();
assert(source_c != nullptr);
cl_int error = CL_SUCCESS;
out_program =
clCreateProgramWithSource(clContext, 1, &source_c, &sourceSize, &error);
if (!CHECK_CL_SUCCESS(log, error)) return false;
error = clCompileProgram(out_program, 1, &clDeviceId, nullptr, 0, nullptr,
nullptr, nullptr, nullptr);
if (!CHECK_CL_SUCCESS(log, error)) return false;
m_openPrograms.push_back(out_program);
return true;
}
bool test_base_opencl::create_built_program(const std::string &source,
cl_program &out_program,
logger &log) {
return this->create_built_program(source, m_cl_context, m_cl_device_id,
out_program, log);
}
bool test_base_opencl::create_built_program(const std::string &source,
cl_context clContext,
cl_device_id clDeviceId,
cl_program &out_program,
logger &log) {
assert(!source.empty());
const char *source_c = source.c_str();
const size_t sourceSize = source.length();
assert(source_c != nullptr);
cl_int error = CL_SUCCESS;
out_program =
clCreateProgramWithSource(clContext, 1, &source_c, &sourceSize, &error);
if (!CHECK_CL_SUCCESS(log, error)) return false;
error =
clBuildProgram(out_program, 1, &clDeviceId, nullptr, nullptr, nullptr);
if (!CHECK_CL_SUCCESS(log, error)) return false;
m_openPrograms.push_back(out_program);
return true;
}
bool test_base_opencl::create_program_with_binary(const std::string &filename,
cl_program &out_program,
logger &log) {
return this->create_program_with_binary(filename, m_cl_context,
m_cl_device_id, out_program, log);
}
bool test_base_opencl::create_program_with_binary(const std::string &filename,
cl_context clContext,
cl_device_id clDeviceId,
cl_program &out_program,
logger &log) {
assert(!filename.empty());
size_t maxPathLen = 512;
std::vector<char> pathToExe(maxPathLen);
if (!get_exec_dir(pathToExe.data(), maxPathLen)) {
FAIL(log, "couldn't get path to executable");
}
// Expecting to find the OpenCL program binary file next to the executable
std::string fullFilePath(pathToExe.data());
fullFilePath.append(filename);
std::ifstream binary_file(fullFilePath, std::ios::binary);
if (!binary_file) {
FAIL(log, "Failed to open program binary file");
return false;
}
binary_file.seekg(0, binary_file.end);
size_t length = binary_file.tellg();
assert(length);
binary_file.seekg(0, binary_file.beg);
std::vector<char> binary(length);
binary_file.read(binary.data(), length);
const unsigned char *binary_ptr =
reinterpret_cast<const unsigned char *>(binary.data());
binary_file.close();
cl_int error = CL_SUCCESS;
out_program = clCreateProgramWithBinary(clContext, 1, &clDeviceId, &length,
&binary_ptr, nullptr, &error);
if (!CHECK_CL_SUCCESS(log, error)) return false;
error =
clBuildProgram(out_program, 1, &clDeviceId, nullptr, nullptr, nullptr);
if (!CHECK_CL_SUCCESS(log, error)) return false;
m_openPrograms.push_back(out_program);
return true;
}
/**
*/
bool test_base_opencl::create_kernel(const cl_program &clProgram,
const std::string &name,
cl_kernel &out_kernel, logger &log) {
assert(clProgram != nullptr);
assert(!name.empty());
cl_int error = CL_SUCCESS;
out_kernel = clCreateKernel(clProgram, name.c_str(), &error);
if (!CHECK_CL_SUCCESS(log, error)) return false;
assert(out_kernel);
m_openKernels.push_back(out_kernel);
return true;
}
bool test_base_opencl::create_sampler(cl_sampler &outSampler, logger &log) {
cl_int error = CL_SUCCESS;
outSampler = clCreateSampler(m_cl_context, 0, CL_ADDRESS_REPEAT,
CL_FILTER_LINEAR, &error);
return CHECK_CL_SUCCESS(log, error);
}
bool test_base_opencl::create_buffer(cl_mem &outBuffer, size_t size,
logger &log) {
m_openBufferHostPtrs.push_back(std::shared_ptr<uint8_t>(
new uint8_t[size], std::default_delete<uint8_t[]>()));
cl_int error = CL_SUCCESS;
outBuffer = clCreateBuffer(
m_cl_context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, size,
m_openBufferHostPtrs[m_openBufferHostPtrs.size() - 1].get(), &error);
if (!CHECK_CL_SUCCESS(log, error)) return false;
assert(outBuffer);
m_openBuffers.push_back(outBuffer);
return true;
}
bool test_base_opencl::create_image(cl_mem &outImage,
const cl_image_format &format,
const cl_image_desc &desc, logger &log) {
cl_int error = CL_SUCCESS;
outImage =
clCreateImage(m_cl_context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR,
&format, &desc, nullptr, &error);
if (!CHECK_CL_SUCCESS(log, error)) return false;
assert(outImage);
m_openImages.push_back(outImage);
return true;
}
/** called after this test has executed
*/
void test_base_opencl::cleanup() {
size_t i = 0;
for (i = 0; i < m_openBuffers.size(); i++)
clReleaseMemObject(m_openBuffers[i]);
m_openBuffers.clear();
for (i = 0; i < m_openImages.size(); i++) clReleaseMemObject(m_openImages[i]);
m_openImages.clear();
clReleaseCommandQueue(m_cl_command_queue);
clReleaseContext(m_cl_context);
for (i = 0; i < m_openKernels.size(); i++) clReleaseKernel(m_openKernels[i]);
m_openKernels.clear();
for (i = 0; i < m_openPrograms.size(); i++)
clReleaseProgram(m_openPrograms[i]);
m_openPrograms.clear();
}
} // namespace util
} // namespace sycl_cts
#endif