forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocl_ext_context.cpp
More file actions
261 lines (216 loc) · 7.72 KB
/
ocl_ext_context.cpp
File metadata and controls
261 lines (216 loc) · 7.72 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
/*******************************************************
* Copyright (c) 2015, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <arrayfire.h>
#include <gtest/gtest.h>
#include <testHelpers.hpp>
#if defined(AF_OPENCL)
#include <af/opencl.h>
#include <iostream>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
#pragma GCC diagnostic ignored "-Wignored-attributes"
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#if __GNUC__ >= 8
#pragma GCC diagnostic ignored "-Wcatch-value="
#endif
#define CL_HPP_MINIMUM_OPENCL_VERSION 120
#define CL_HPP_TARGET_OPENCL_VERSION 120
#define CL_HPP_ENABLE_EXCEPTIONS 1
#include <CL/cl2.hpp>
#pragma GCC diagnostic pop
using af::allocV2;
using af::array;
using af::constant;
using af::freeV2;
using af::getDeviceCount;
using af::info;
using af::randu;
using af::setDevice;
using std::endl;
using std::vector;
inline void checkErr(cl_int err, const char *name) {
if (err != CL_SUCCESS) {
std::cerr << "ERROR: " << name << " (" << err << ")" << endl;
exit(EXIT_FAILURE);
}
}
class OCLExtContext : public ::testing::Test {
public:
cl_device_id deviceId = NULL;
cl_context context = NULL;
cl_command_queue queue = NULL;
void SetUp() override {
cl_platform_id platformId = NULL;
cl_uint numPlatforms;
cl_uint numDevices;
cl_int errorCode = 0;
checkErr(clGetPlatformIDs(1, &platformId, &numPlatforms),
"Get Platforms failed");
checkErr(clGetDeviceIDs(platformId, CL_DEVICE_TYPE_DEFAULT, 1,
&deviceId, &numDevices),
"Get cl_device_id failed");
context = clCreateContext(NULL, 1, &deviceId, NULL, NULL, &errorCode);
checkErr(errorCode, "Context creation failed");
#ifdef CL_VERSION_2_0
queue = clCreateCommandQueueWithProperties(context, deviceId, 0,
&errorCode);
#else
queue = clCreateCommandQueue(context, deviceId, 0, &errorCode);
#endif
checkErr(errorCode, "Command queue creation failed");
}
void TearDown() override {
checkErr(clReleaseCommandQueue(queue), "clReleaseCommandQueue");
checkErr(clReleaseContext(context), "clReleaseContext");
checkErr(clReleaseDevice(deviceId), "clReleaseDevice");
}
};
TEST_F(OCLExtContext, PushAndPop) {
int dCount = getDeviceCount();
info();
afcl::addDevice(deviceId, context, queue);
ASSERT_EQ(true, dCount + 1 == getDeviceCount());
afcl::deleteDevice(deviceId, context);
ASSERT_EQ(true, dCount == getDeviceCount());
info();
}
TEST_F(OCLExtContext, set) {
int dCount = getDeviceCount(); // Before user device addition
setDevice(0);
info();
array t = randu(5, 5);
af_print(t);
afcl::addDevice(deviceId, context, queue);
info();
setDevice(
dCount); // In 0-based index, dCount is index of newly added device
info();
const int x = 5;
const int y = 5;
const int s = x * y;
array a = constant(1, x, y);
vector<float> host(s);
a.host((void *)host.data());
for (int i = 0; i < s; ++i) ASSERT_EQ(host[i], 1.0f);
setDevice(0);
info();
af_print(t);
}
TEST(OCLCheck, DeviceType) {
afcl::deviceType devType = afcl::getDeviceType();
cl_device_type type = -100;
clGetDeviceInfo(afcl::getDeviceId(), CL_DEVICE_TYPE, sizeof(cl_device_type),
&type, NULL);
ASSERT_EQ(type, (cl_device_type)devType);
}
TEST(OCLCheck, DevicePlatform) {
afcl::platform platform = afcl::getPlatform();
ASSERT_NE(platform, AFCL_PLATFORM_UNKNOWN);
}
#else
TEST(OCLExtContext, NoopCPU) {}
#endif
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
TEST(Memory, AfAllocDeviceOpenCL) {
/// Tests to see if the pointer returned can be used by opencl functions
float gold_val = 5;
void *alloc_ptr;
ASSERT_SUCCESS(af_alloc_device(&alloc_ptr, sizeof(float)));
// af_alloc_device returns a cl::Buffer object from alloc unfortunately
cl::Buffer *bptr = static_cast<cl::Buffer *>(alloc_ptr);
ASSERT_EQ(2, bptr->getInfo<CL_MEM_REFERENCE_COUNT>());
cl_command_queue queue;
afcl_get_queue(&queue, true);
cl::CommandQueue cq(queue);
cl::Buffer gold(cq, &gold_val, &gold_val + 1, false);
cq.enqueueCopyBuffer(gold, *bptr, 0, 0, sizeof(float));
float host;
cq.enqueueReadBuffer(*bptr, CL_TRUE, 0, sizeof(float), &host);
ASSERT_SUCCESS(af_free_device(alloc_ptr));
ASSERT_EQ(gold_val, host);
}
#pragma GCC diagnostic pop
TEST(Memory, AfAllocDeviceV2OpenCLC) {
/// Tests to see if the pointer returned can be used by opencl functions
float gold_val = 5;
void *alloc_ptr;
ASSERT_SUCCESS(af_alloc_device_v2(&alloc_ptr, sizeof(float)));
{
cl::Buffer bptr(static_cast<cl_mem>(alloc_ptr), true);
ASSERT_EQ(3, bptr.getInfo<CL_MEM_REFERENCE_COUNT>());
cl_command_queue queue;
afcl_get_queue(&queue, true);
cl::CommandQueue cq(queue);
cl::Buffer gold(cq, &gold_val, &gold_val + 1, false);
cq.enqueueCopyBuffer(gold, bptr, 0, 0, sizeof(float));
float host;
cq.enqueueReadBuffer(bptr, CL_TRUE, 0, sizeof(float), &host);
ASSERT_EQ(gold_val, host);
}
ASSERT_SUCCESS(af_free_device_v2(alloc_ptr));
}
TEST(Memory, AfAllocDeviceV2OpenCLCPP) {
/// Tests to see if the pointer returned can be used by opencl functions
float gold_val = 5;
cl_mem alloc_ptr = static_cast<cl_mem>(allocV2(sizeof(float)));
{
cl::Buffer bptr(alloc_ptr, true);
ASSERT_EQ(3, bptr.getInfo<CL_MEM_REFERENCE_COUNT>());
cl_command_queue queue;
afcl_get_queue(&queue, true);
cl::CommandQueue cq(queue);
cl::Buffer gold(cq, &gold_val, &gold_val + 1, false);
cq.enqueueCopyBuffer(gold, bptr, 0, 0, sizeof(float));
float host;
cq.enqueueReadBuffer(bptr, CL_TRUE, 0, sizeof(float), &host);
ASSERT_EQ(gold_val, host);
}
freeV2(alloc_ptr);
}
TEST(Memory, SNIPPET_AllocOpenCL) {
// clang-format off
//! [ex_alloc_v2_opencl]
cl_command_queue queue;
afcl_get_queue(&queue, true);
cl_context context;
afcl_get_context(&context, true);
void *alloc_ptr = allocV2(sizeof(float));
cl_mem mem = static_cast<cl_mem>(alloc_ptr);
// Map memory from the device to the System memory
cl_int map_err_code;
void *mapped_ptr = clEnqueueMapBuffer(
queue, // command queueu
mem, // buffer
CL_TRUE, // is blocking
CL_MAP_READ | CL_MAP_WRITE, // map type
0, // offset
sizeof(float), // size
0, // num_events_in_wait_list
nullptr, // event_wait_list
nullptr, // event
&map_err_code); // error code
float *float_ptr = static_cast<float *>(mapped_ptr);
float_ptr[0] = 5.0f;
// Unmap buffer after we are done using it
cl_int unmap_err_code =
clEnqueueUnmapMemObject(queue, // command queue
mem, // buffer
mapped_ptr, // mapped pointer
0, // num_events_in_wait_list
nullptr, // event_wait_list
nullptr); // event
freeV2(alloc_ptr);
//! [ex_alloc_v2_opencl]
// clang-format on
ASSERT_EQ(CL_SUCCESS, map_err_code);
ASSERT_EQ(CL_SUCCESS, unmap_err_code);
}