forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubbuffer_interop.cpp
More file actions
339 lines (273 loc) · 10.3 KB
/
subbuffer_interop.cpp
File metadata and controls
339 lines (273 loc) · 10.3 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
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
// REQUIRES: opencl
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out -L %opencl_libs_dir -lOpenCL
// RUN: %CPU_RUN_PLACEHOLDER %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t.out
// RUN: %ACC_RUN_PLACEHOLDER %t.out
//==------------ subbuffer_interop.cpp - SYCL buffer basic test ------------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <CL/sycl.hpp>
#include <cassert>
#include <memory>
#include <numeric>
using namespace cl::sycl;
const std::string clKernelSourceCodeSimple = "\
__kernel void test(__global int* a, __global int* b) {\
int i = get_global_id(0);\
if (i < 256) { \
b[i] = 0; \
} \
}";
const std::string clKernelSourceCodeNonOverlap = "\
__kernel void test(__global int* a, __global int* b, __global int *c) {\
int i = get_global_id(0);\
if (i < 128) { \
b[i] = 0; \
} \
if (i >= 256 && i < 384) { \
c[i - 256] = 0; \
} \
}";
const std::string clKernelSourceCodeOverlap = "\
__kernel void test1(__global int* a, __global int* b) {\
int i = get_global_id(0);\
if (i < 256) { \
b[i] = 0; \
} \
} \
__kernel void test2(__global int* a, __global int* b) {\
int i = get_global_id(0);\
if (i < 128) { \
b[i] = 1; \
} \
}";
int main() {
bool Failed = false;
{
// Test if we can write into subbufer from OpenCL code.
const size_t NSize = 512;
cl_int Error = CL_SUCCESS;
int AMem[NSize];
for (size_t i = 0; i < NSize; i++) {
AMem[i] = i;
}
try {
queue TestQueue;
const char *SrcString[] = {clKernelSourceCodeSimple.c_str()};
const size_t SrcStringSize = clKernelSourceCodeSimple.size();
cl_context clContext = TestQueue.get_context().get();
cl_device_id clDevice = TestQueue.get_device().get();
cl_program clProgram =
clCreateProgramWithSource(clContext, 1, SrcString, &SrcStringSize, &Error);
CHECK_OCL_CODE(Error);
Error = clBuildProgram(clProgram, 1, &clDevice, NULL, NULL, NULL);
CHECK_OCL_CODE(Error);
cl_kernel clKernel = clCreateKernel(clProgram, "test", &Error);
CHECK_OCL_CODE(Error);
buffer<int, 1> BufA(AMem, cl::sycl::range<1>(NSize));
buffer<int, 1> BufB(BufA, NSize / 2, NSize / 2);
kernel TestKernel(clKernel, TestQueue.get_context());
TestQueue.submit([&](handler &cgh) {
auto a_acc = BufA.get_access<access::mode::read>(cgh);
auto b_acc = BufB.get_access<access::mode::write>(cgh);
cgh.set_arg(0, a_acc);
cgh.set_arg(1, b_acc);
cgh.parallel_for(range<1>(NSize), TestKernel);
});
clReleaseKernel(clKernel);
clReleaseProgram(clProgram);
} catch (exception &ex) {
std::cout << ex.what() << std::endl;
}
for (int i = 0; i < NSize; ++i) {
if (i < NSize / 2 && AMem[i] != i) {
std::cout << " array[" << i << "] is " << AMem[i] << " expected " << i
<< std::endl;
assert(false);
Failed = true;
} else if (i >= NSize / 2 && AMem[i] != 0) {
std::cout << " array[" << i << "] is " << AMem[i] << " expected " << 0
<< std::endl;
assert(false);
Failed = true;
}
}
}
{
// Test if we can use two sub buffers, pointing to one buffer, from OpenCL.
const size_t NSize = 512;
cl_int Error = CL_SUCCESS;
int AMem[NSize];
for (size_t i = 0; i < NSize; i++) {
AMem[i] = i;
}
try {
queue TestQueue;
const char *SrcString[] = {clKernelSourceCodeNonOverlap.c_str()};
const size_t SrcStringSize = clKernelSourceCodeNonOverlap.size();
cl_context clContext = TestQueue.get_context().get();
cl_device_id clDevice = TestQueue.get_device().get();
cl_program clProgram =
clCreateProgramWithSource(clContext, 1, SrcString, &SrcStringSize, &Error);
CHECK_OCL_CODE(Error);
Error = clBuildProgram(clProgram, 1, &clDevice, NULL, NULL, NULL);
CHECK_OCL_CODE(Error);
cl_kernel clKernel = clCreateKernel(clProgram, "test", &Error);
CHECK_OCL_CODE(Error);
buffer<int, 1> BufA(AMem, cl::sycl::range<1>(NSize));
buffer<int, 1> BufB(BufA, 0, NSize / 4);
buffer<int, 1> BufC(BufA, 2 * NSize / 4, NSize / 4);
kernel TestKernel(clKernel, TestQueue.get_context());
TestQueue.submit([&](handler &cgh) {
auto a_acc = BufA.get_access<access::mode::read>(cgh);
auto b_acc = BufB.get_access<access::mode::write>(cgh);
auto c_acc = BufC.get_access<access::mode::write>(cgh);
cgh.set_arg(0, a_acc);
cgh.set_arg(1, b_acc);
cgh.set_arg(2, c_acc);
cgh.parallel_for(range<1>(NSize), TestKernel);
});
clReleaseKernel(clKernel);
clReleaseProgram(clProgram);
} catch (exception &ex) {
std::cout << ex.what() << std::endl;
}
for (int i = 0; i < NSize; ++i) {
if (i < NSize / 4 && AMem[i] != 0) {
std::cout << " array[" << i << "] is " << AMem[i] << " expected " << 0
<< std::endl;
assert(false);
Failed = true;
} else if (i >= NSize / 4 && i < 2 * NSize / 4 && AMem[i] != i) {
std::cout << " array[" << i << "] is " << AMem[i] << " expected " << i
<< std::endl;
assert(false);
Failed = true;
} else if (i >= 2 * NSize / 4 && i < 3 * NSize / 4 && AMem[i] != 0) {
std::cout << " array[" << i << "] is " << AMem[i] << " expected " << 0
<< std::endl;
assert(false);
Failed = true;
} else if (i >= 3 * NSize / 4 && AMem[i] != i) {
std::cout << " array[" << i << "] is " << AMem[i] << " expected " << i
<< std::endl;
assert(false);
Failed = true;
}
}
}
{
// Test if we can use two sub buffers, pointing to one buffer, from
// two different OpenCL kernels.
const size_t NSize = 512;
cl_int Error = CL_SUCCESS;
int AMem[NSize];
for (size_t i = 0; i < NSize; i++) {
AMem[i] = i;
}
try {
queue TestQueue;
const char *SrcString[] = {clKernelSourceCodeOverlap.c_str()};
const size_t SrcStringSize = clKernelSourceCodeOverlap.size();
cl_context clContext = TestQueue.get_context().get();
cl_device_id clDevice = TestQueue.get_device().get();
cl_program clProgram =
clCreateProgramWithSource(clContext, 1, SrcString, &SrcStringSize, &Error);
CHECK_OCL_CODE(Error);
Error = clBuildProgram(clProgram, 1, &clDevice, NULL, NULL, NULL);
CHECK_OCL_CODE(Error);
cl_kernel clKernel1 = clCreateKernel(clProgram, "test1", &Error);
CHECK_OCL_CODE(Error);
cl_kernel clKernel2 = clCreateKernel(clProgram, "test2", &Error);
CHECK_OCL_CODE(Error);
buffer<int, 1> BufA(AMem, cl::sycl::range<1>(NSize));
buffer<int, 1> BufB(BufA, 0, NSize / 2);
buffer<int, 1> BufC(BufA, NSize / 4, NSize / 4);
kernel TestKernel1(clKernel1, TestQueue.get_context());
kernel TestKernel2(clKernel2, TestQueue.get_context());
TestQueue.submit([&](handler &cgh) {
auto a_acc = BufA.get_access<access::mode::read>(cgh);
auto b_acc = BufB.get_access<access::mode::write>(cgh);
cgh.set_arg(0, a_acc);
cgh.set_arg(1, b_acc);
cgh.parallel_for(range<1>(NSize), TestKernel1);
});
TestQueue.submit([&](handler &cgh) {
auto a_acc = BufA.get_access<access::mode::read>(cgh);
auto c_acc = BufC.get_access<access::mode::write>(cgh);
cgh.set_arg(0, a_acc);
cgh.set_arg(1, c_acc);
cgh.parallel_for(range<1>(NSize), TestKernel2);
});
clReleaseKernel(clKernel1);
clReleaseKernel(clKernel2);
clReleaseProgram(clProgram);
} catch (exception &ex) {
std::cout << ex.what() << std::endl;
}
for (int i = 0; i < NSize; ++i) {
if (i < NSize / 4 && AMem[i] != 0) {
std::cout << " array[" << i << "] is " << AMem[i] << " expected " << 0
<< std::endl;
assert(false);
Failed = true;
} else if (i >= NSize / 4 && i < 2 * NSize / 4 && AMem[i] != 1) {
std::cout << " array[" << i << "] is " << AMem[i] << " expected " << i
<< std::endl;
assert(false);
Failed = true;
} else if (i >= 2 * NSize / 4 && AMem[i] != i) {
std::cout << " array[" << i << "] is " << AMem[i] << " expected " << i
<< std::endl;
assert(false);
Failed = true;
}
}
}
const char *cl_src = "kernel void test(global int *p) { "
" printf(\"offset on device = \%d\\n\", *p);"
" if (p) *p *= 3;"
"}";
{
cl::sycl::queue Q;
// Create OpenCL program
cl_int err;
auto context_cl = Q.get_context().get();
auto device_cl = Q.get_device().get();
cl_program program_cl =
clCreateProgramWithSource(context_cl, 1, &cl_src, nullptr, &err);
err = clBuildProgram(program_cl, 1, &device_cl, nullptr, nullptr, nullptr);
cl_kernel kernel_cl = clCreateKernel(program_cl, "test", &err);
cl::sycl::kernel kernel_sycl(kernel_cl, Q.get_context());
// Create buffer
constexpr int N = 256;
std::vector<int> v(2 * N);
std::iota(v.begin(), v.end(), 0);
cl::sycl::buffer<int, 1> buf(v.data(), v.size());
cl::sycl::buffer<int, 1> subbuf(buf, N, N);
auto subbuf_copy =
new cl::sycl::buffer<int, 1>(subbuf.reinterpret<int, 1>(N));
// Test offsets
{
auto host_acc = subbuf_copy->get_access<cl::sycl::access::mode::read>();
std::cout << "On host: offset = " << host_acc[0] << std::endl;
assert(host_acc[0] == 256 && "Invalid subbuffer origin");
}
Q.submit([&](cl::sycl::handler &cgh) {
auto acc = subbuf_copy->get_access<cl::sycl::access::mode::write>(cgh);
cgh.set_args(acc);
cgh.single_task(kernel_sycl);
});
Q.wait_and_throw();
{
auto host_acc = subbuf_copy->get_access<cl::sycl::access::mode::read>();
std::cout << "On host: offset = " << host_acc[0] << std::endl;
assert(host_acc[0] == 256 * 3 && "Invalid subbuffer origin");
}
}
return Failed;
}