-
Notifications
You must be signed in to change notification settings - Fork 79
/
opencl_interop_constructors.cpp
499 lines (427 loc) · 17.1 KB
/
opencl_interop_constructors.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*******************************************************************************
//
// SYCL 1.2.1 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 "../common/common.h"
#include "../../util/opencl_helper.h"
#include "../../util/test_base_opencl.h"
#define TEST_NAME opencl_interop_constructors
namespace opencl_interop_constructors__ {
using namespace sycl_cts;
class buffer_interop_constructor_kernel;
class image_interop_constructor_kernel_default_event;
class image_interop_constructor_kernel_provided_event;
class sampler_interop_constructor_kernel;
/** tests the constructors for OpenCL inter-op
*/
class TEST_NAME : public sycl_cts::util::test_base_opencl {
public:
/** return information about this test
*/
void get_info(test_base::info &out) const override {
set_test_info(out, TOSTRING(TEST_NAME), TEST_FILE);
}
/** execute this test
*/
void run(util::logger &log) override {
try {
cts_selector ctsSelector;
const auto ctsContext = util::get_cts_object::context(ctsSelector);
const auto ctsDevice = ctsContext.get_devices()[0];
if (ctsContext.is_host()) {
log.note("OpenCL interop doesn't work on host");
return;
}
cl::sycl::string_class kernelSource = R"(
__kernel void opencl_interop_constructors_kernel(__global float *input)
{
input[get_global_id(0)] = get_global_id(0);
}
)";
cl::sycl::string_class programBinaryFile =
"opencl_interop_constructors.bin";
/** check platform (cl_platform_id) constructor
*/
{
cl::sycl::platform platform(m_cl_platform_id);
cl_platform_id interopPlatformID = platform.get();
if (interopPlatformID != m_cl_platform_id) {
FAIL(log, "platform was not constructed correctly");
}
}
/** check device (cl_device_id) constructor
*/
{
cl::sycl::device device(m_cl_device_id);
cl_device_id interopDeviceID = device.get();
if (interopDeviceID != m_cl_device_id) {
FAIL(log, "device was not constructed correctly");
}
if (!CHECK_CL_SUCCESS(log, clReleaseDevice(interopDeviceID))) {
FAIL(log, "failed to release OpenCL device");
}
}
/** check context (cl_context) constructor
*/
{
cl::sycl::context context(m_cl_context);
cl_context interopContext = context.get();
if (interopContext != m_cl_context) {
FAIL(log, "context was not constructed correctly");
}
if (!CHECK_CL_SUCCESS(log, clReleaseContext(interopContext))) {
FAIL(log, "failed to release OpenCL context");
}
}
/** check context (cl_context, async_handler) constructor
*/
{
cts_async_handler asyncHandler;
cl::sycl::context context(m_cl_context, asyncHandler);
cl_context interopContext = context.get();
if (interopContext != m_cl_context) {
FAIL(log, "context was not constructed correctly");
}
if (!CHECK_CL_SUCCESS(log, clReleaseContext(interopContext))) {
FAIL(log, "failed to release OpenCL context");
}
}
/** check queue (cl_command_queue, const context&) constructor
*/
{
cl::sycl::queue queue(m_cl_command_queue, ctsContext);
cl_command_queue interopQueue = queue.get();
if (interopQueue != m_cl_command_queue) {
FAIL(log, "queue was not constructed correctly");
}
/** check that queue copy constructor preserve the same OpenCL queue
*/
cl::sycl::queue queueCopy(queue);
auto clQueueCopy = queueCopy.get();
if (interopQueue != clQueueCopy) {
FAIL(log, "queue destination was not copy constructed correctly");
}
if (!CHECK_CL_SUCCESS(log, clReleaseCommandQueue(clQueueCopy))) {
FAIL(log, "failed to release OpenCL command queue");
}
if (!CHECK_CL_SUCCESS(log, clReleaseCommandQueue(interopQueue))) {
FAIL(log, "failed to release OpenCL command queue");
}
}
/** check queue (cl_command_queue, const context&, async_handler)
* constructor
*/
{
cts_async_handler asyncHandler;
cl::sycl::queue queue(m_cl_command_queue, ctsContext, asyncHandler);
cl_command_queue interopQueue = queue.get();
if (interopQueue != m_cl_command_queue) {
FAIL(log, "queue was not constructed correctly");
}
if (!CHECK_CL_SUCCESS(log, clReleaseCommandQueue(interopQueue))) {
FAIL(log, "failed to release OpenCL command queue");
}
}
/** check program (context, cl_program) constructor
*/
{
cl_program clProgram{};
if (online_compiler_supported(ctsDevice.get(), log)) {
if (!create_built_program(kernelSource, ctsContext.get(),
ctsDevice.get(), clProgram, log)) {
FAIL(log, "create_built_program failed");
}
} else {
if (!create_program_with_binary(programBinaryFile, ctsContext.get(),
ctsDevice.get(), clProgram, log)) {
cl::sycl::string_class errorMsg =
"create_program_with_binary failed.";
errorMsg +=
" Since online compile is not supported, expecting to find " +
programBinaryFile + " in same path as the executable binary";
FAIL(log, errorMsg.c_str());
}
}
cl::sycl::program program(ctsContext, clProgram);
cl_program interopProgram = program.get();
if (interopProgram != clProgram) {
FAIL(log, "program was not constructed correctly");
}
if (!CHECK_CL_SUCCESS(log, clReleaseProgram(interopProgram))) {
FAIL(log, "failed to release OpenCL program");
}
}
/** check kernel (cl_kernel, const context&) constructor
*/
{
cl_program clProgram{};
if (online_compiler_supported(ctsDevice.get(), log)) {
if (!create_built_program(kernelSource, ctsContext.get(),
ctsDevice.get(), clProgram, log)) {
FAIL(log, "create_built_program failed");
}
} else {
if (!create_program_with_binary(programBinaryFile, ctsContext.get(),
ctsDevice.get(), clProgram, log)) {
cl::sycl::string_class errorMsg =
"create_program_with_binary failed.";
errorMsg +=
" Since online compile is not supported, expecting to find " +
programBinaryFile + " in same path as the executable binary";
FAIL(log, errorMsg.c_str());
}
}
cl_kernel clKernel{};
if (!create_kernel(clProgram, "opencl_interop_constructors_kernel",
clKernel, log)) {
FAIL(log, "create_kernel failed");
}
cl::sycl::kernel kernel(clKernel, ctsContext);
cl_kernel interopKernel = kernel.get();
if (interopKernel != clKernel) {
FAIL(log, "kernel was not constructed correctly");
}
if (!CHECK_CL_SUCCESS(log, clReleaseKernel(interopKernel))) {
FAIL(log, "failed to release OpenCL kernel");
}
}
/** check buffer (cl_mem, contex) constructor
*/
{
const size_t size = 32;
int data[size] = {0};
cl_int error = CL_SUCCESS;
auto queue = util::get_cts_object::queue(ctsSelector);
cl_mem clBuffer = clCreateBuffer(
queue.get_context().get(), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
size * sizeof(int), data, &error);
if (!CHECK_CL_SUCCESS(log, error)) {
FAIL(log, "create buffer failed");
}
cl::sycl::buffer<int, 1> buffer(clBuffer, queue.get_context());
// calculate element count, size and range for the interop buffer
cl::sycl::range<1> interopRange{size};
size_t interopSize = size * sizeof(int);
// check the buffer
if (buffer.is_sub_buffer()) {
FAIL(log,
"opencl buffer was not interop constructed properly. "
"(is_sub_buffer) ");
}
if (buffer.get_size() != interopSize) {
FAIL(log,
"opencl buffer was not interop constructed properly. (get_size) ");
}
if (buffer.get_range() != interopRange) {
FAIL(
log,
"opencl buffer was not interop constructed properly. (get_range) ");
}
if (buffer.get_count() != size) {
FAIL(
log,
"opencl buffer was not interop constructed properly. (get_count) ");
}
queue.submit([&](cl::sycl::handler &handler) {
auto accessor =
buffer.get_access<cl::sycl::access::mode::read_write,
cl::sycl::access::target::global_buffer>(
handler);
handler.single_task<class buffer_interop_constructor_kernel_no_event>([]() {});
});
error = clReleaseMemObject(clBuffer);
if (!CHECK_CL_SUCCESS(log, error)) {
FAIL(log, "failed to release OpenCL buffer");
}
queue.wait_and_throw();
}
/** check buffer (cl_mem, context, event) constructor
*/
{
const size_t size = 32;
int data[size] = {0};
cl_int error = CL_SUCCESS;
auto queue = util::get_cts_object::queue(ctsSelector);
// create an event to wait for
cl::sycl::event event = queue.submit([](cl::sycl::handler &cgh) {
cgh.single_task<class buffer_interop_event>(
[]() {}); // do not do anything here, we only need the event
});
cl_mem clBuffer = clCreateBuffer(
queue.get_context().get(), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
size * sizeof(int), data, &error);
if (!CHECK_CL_SUCCESS(log, error)) {
FAIL(log, "create buffer failed");
}
cl::sycl::buffer<int, 1> buffer(clBuffer, queue.get_context(), event);
// calculate element count, size and range for the interop buffer
cl::sycl::range<1> interopRange{size};
size_t interopSize = size * sizeof(int);
// check the buffer
if (buffer.is_sub_buffer()) {
FAIL(log,
"opencl buffer was not interop constructed properly. "
"(is_sub_buffer) ");
}
if (buffer.get_size() != interopSize) {
FAIL(log,
"opencl buffer was not interop constructed properly. (get_size) ");
}
if (buffer.get_range() != interopRange) {
FAIL(
log,
"opencl buffer was not interop constructed properly. (get_range) ");
}
if (buffer.get_count() != size) {
FAIL(
log,
"opencl buffer was not interop constructed properly. (get_count) ");
}
queue.submit([&](cl::sycl::handler &handler) {
auto accessor =
buffer.get_access<cl::sycl::access::mode::read_write,
cl::sycl::access::target::global_buffer>(
handler);
handler.single_task<class buffer_interop_constructor_kernel_with_event>([]() {});
});
error = clReleaseMemObject(clBuffer);
if (!CHECK_CL_SUCCESS(log, error)) {
FAIL(log, "failed to release OpenCL buffer");
}
queue.wait_and_throw();
}
/** check image (cl_mem, const context&, event) constructor
*/
{
auto queue = util::get_cts_object::queue(ctsSelector);
if (!queue.get_device()
.get_info<cl::sycl::info::device::image_support>()) {
log.note("Device does not support images");
} else {
constexpr size_t imageSideSize = 16;
/* Size is *4 because image data is 4 channels (RGBA) */
constexpr auto size = imageSideSize * imageSideSize * 4;
float data[size] = {0.0f};
const auto clContext = queue.get_context().get();
cl_image_format clImageFormat;
clImageFormat.image_channel_data_type = CL_FLOAT;
clImageFormat.image_channel_order = CL_RGBA;
cl_image_desc clImageDesc;
clImageDesc.image_type = CL_MEM_OBJECT_IMAGE2D;
clImageDesc.image_width = imageSideSize;
clImageDesc.image_height = imageSideSize;
clImageDesc.image_depth = 0;
clImageDesc.image_array_size = 1;
clImageDesc.image_row_pitch = 0;
clImageDesc.image_slice_pitch = 0;
clImageDesc.num_mip_levels = 0;
clImageDesc.num_samples = 0;
clImageDesc.buffer = nullptr;
cl_int error = CL_SUCCESS;
// Check constructing image with defaulted event
{
cl_mem clImage = clCreateImage(
clContext, (CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR),
&clImageFormat, &clImageDesc, data, &error);
if (!CHECK_CL_SUCCESS(log, error)) {
FAIL(log, "create image failed");
}
cl::sycl::image<2> image(clImage, queue.get_context());
queue.submit([&](cl::sycl::handler &handler) {
auto accessor =
image.get_access<cl::sycl::float4,
cl::sycl::access::mode::read>(handler);
handler.single_task<
class image_interop_constructor_kernel_default_event>(
[]() {});
});
error = clReleaseMemObject(clImage);
if (!CHECK_CL_SUCCESS(log, error)) {
FAIL(log, "failed to release OpenCL image");
}
}
// Check constructing image with specified event
{
cl::sycl::event event;
cl_mem clImage = clCreateImage(
clContext, (CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR),
&clImageFormat, &clImageDesc, data, &error);
if (!CHECK_CL_SUCCESS(log, error)) {
FAIL(log, "create image failed");
}
cl::sycl::image<2> image(clImage, queue.get_context(), event);
queue.submit([&](cl::sycl::handler &handler) {
auto accessor =
image.get_access<cl::sycl::float4,
cl::sycl::access::mode::read>(handler);
handler.single_task<
class image_interop_constructor_kernel_provided_event>(
[]() {});
});
error = clReleaseMemObject(clImage);
if (!CHECK_CL_SUCCESS(log, error)) {
FAIL(log, "failed to release OpenCL image");
}
}
queue.wait_and_throw();
}
}
/** check sampler (cl_sampler, const context&) constructor
*/
{
auto queue = util::get_cts_object::queue(ctsSelector);
if (!queue.get_device()
.get_info<cl::sycl::info::device::image_support>()) {
log.note("Device does not support images");
} else {
cl_sampler clSampler;
create_sampler(clSampler, log);
queue.submit([&](cl::sycl::handler &handler) {
cl::sycl::sampler sampler(clSampler, queue.get_context());
handler.single_task<class sampler_interop_constructor_kernel>(
[]() {});
});
cl_int error = clReleaseSampler(clSampler);
if (!CHECK_CL_SUCCESS(log, error)) {
FAIL(log, "failed to release OpenCL sampler");
}
queue.wait_and_throw();
}
}
/** check event (cl_event, const context&) constructor
*/
{
cl_event clEvent = clCreateUserEvent(ctsContext.get(), nullptr);
cl::sycl::event event(clEvent, ctsContext);
cl_event interopEvent = event.get();
if (interopEvent != clEvent) {
FAIL(log, "event was not constructed correctly");
}
}
} catch (const cl::sycl::exception &e) {
log_exception(log, e);
cl::sycl::string_class errorMsg =
"a SYCL exception was caught: " + cl::sycl::string_class(e.what());
FAIL(log, errorMsg.c_str());
}
}
};
// register this test with the test_collection
util::test_proxy<TEST_NAME> proxy;
} /* namespace opencl_interop_constructors__ */