-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmain.cpp
More file actions
279 lines (236 loc) · 8.22 KB
/
main.cpp
File metadata and controls
279 lines (236 loc) · 8.22 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
/*
// Copyright (c) 2022-2026 Ben Ashbaugh
//
// SPDX-License-Identifier: MIT
*/
#include <popl/popl.hpp>
#include <CL/opencl.hpp>
#include <chrono>
#include <cinttypes>
#include "util.hpp"
static const char kernelString[] = R"CLC(
kernel void SlowFillBuffer( global uint* dst, uint pattern, uint size )
{
for (uint i = 0; i < size; i++) {
dst[i] = pattern;
}
}
kernel void AddBuffers( global uint* dst, global uint* srcA, global uint* srcB )
{
uint id = get_global_id(0);
dst[id] = srcA[id] + srcB[id];
}
)CLC";
int main(
int argc,
char** argv )
{
int platformIndex = 0;
int deviceIndex = 0;
bool useIOQ = false;
size_t iterations = 16;
size_t gwx = 1024*1024;
{
popl::OptionParser op("Supported Options");
op.add<popl::Value<int>>("p", "platform", "Platform Index", platformIndex, &platformIndex);
op.add<popl::Value<int>>("d", "device", "Device Index", deviceIndex, &deviceIndex);
op.add<popl::Switch>("", "ioq", "Use an In-Order Queue", &useIOQ);
op.add<popl::Value<size_t>>("i", "iterations", "Iterations", iterations, &iterations);
op.add<popl::Value<size_t>>("e", "elements", "Number of Elements AKA Buffer Size", gwx, &gwx);
bool printUsage = false;
try {
op.parse(argc, argv);
} catch (std::exception& e) {
fprintf(stderr, "Error: %s\n\n", e.what());
printUsage = true;
}
if (printUsage || !op.unknown_options().empty() || !op.non_option_args().empty()) {
fprintf(stderr,
"Usage: ooqcommandbuffers [options]\n"
"%s", op.help().c_str());
return -1;
}
}
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
if (!checkPlatformIndex(platforms, platformIndex)) {
return -1;
}
printf("Running on platform: %s\n",
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );
std::vector<cl::Device> devices;
platforms[platformIndex].getDevices(CL_DEVICE_TYPE_ALL, &devices);
printf("Running on device: %s\n",
devices[deviceIndex].getInfo<CL_DEVICE_NAME>().c_str() );
// device queries:
bool has_cl_khr_command_buffer =
checkDeviceForExtension(devices[deviceIndex], CL_KHR_COMMAND_BUFFER_EXTENSION_NAME);
if (has_cl_khr_command_buffer) {
printf("Device supports " CL_KHR_COMMAND_BUFFER_EXTENSION_NAME ".\n");
} else {
printf("Device does not support " CL_KHR_COMMAND_BUFFER_EXTENSION_NAME ", exiting.\n");
return -1;
}
cl_command_queue_properties cmdqprops =
devices[deviceIndex].getInfo<CL_DEVICE_QUEUE_PROPERTIES>();
if (cmdqprops & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE) {
printf("Device supports out-of-order queues.\n");
} else {
printf("Device does not support out-of-order queues, exiting.\n");
return -1;
}
cl_command_queue_properties cmdbufqueueprops =
devices[deviceIndex].getInfo<CL_DEVICE_COMMAND_BUFFER_SUPPORTED_QUEUE_PROPERTIES_KHR>();
if (cmdbufqueueprops & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE) {
printf("Device supports out-of-order command buffers.\n");
} else {
printf("Device does not support out-of-order command buffers, exiting.\n");
return -1;
}
printf("\n");
printf("Using an %s queue.\n", useIOQ ? "in-order" : "out-of-order");
printf("Executing the command buffer %zu times.\n", iterations);
printf("Buffer Size is %zu 32-bit integers.\n", gwx);
cl::Context context{devices[deviceIndex]};
cl::CommandQueue commandQueue{context, devices[deviceIndex],
useIOQ ? (cl_command_queue_properties)0 : CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE};
cl::Program program{ context, kernelString };
program.build();
cl::Kernel fillKernel = cl::Kernel{ program, "SlowFillBuffer" };
cl::Kernel addKernel = cl::Kernel{ program, "AddBuffers" };
cl::Buffer deviceMemSrcA = cl::Buffer{
context,
CL_MEM_ALLOC_HOST_PTR,
gwx * sizeof( cl_uint ) };
cl::Buffer deviceMemSrcB = cl::Buffer{
context,
CL_MEM_ALLOC_HOST_PTR,
gwx * sizeof( cl_uint ) };
cl::Buffer deviceMemDst = cl::Buffer{
context,
CL_MEM_ALLOC_HOST_PTR,
gwx * sizeof( cl_uint ) };
cl::CommandBuffer cmdbuf{clCreateCommandBufferKHR(
1,
&commandQueue(),
nullptr,
nullptr)};
const size_t one = 1;
cl_sync_point_khr writeA = 0;
fillKernel.setArg(0, deviceMemSrcA);
fillKernel.setArg(1, static_cast<cl_uint>(1));
fillKernel.setArg(2, static_cast<cl_uint>(gwx));
clCommandNDRangeKernelKHR(
cmdbuf(),
nullptr, // command queue, can be NULL to use the command buffer queue
nullptr, // command properties
fillKernel(),
1, // work dim
nullptr, // global work offset
&one, // global work size
nullptr, // local work size
0, // num sync points in wait list
nullptr, // sync point wait list
&writeA,
nullptr); // mutable handle
cl_sync_point_khr writeB = 0;
fillKernel.setArg(0, deviceMemSrcB);
fillKernel.setArg(1, static_cast<cl_uint>(2));
fillKernel.setArg(2, static_cast<cl_uint>(gwx));
clCommandNDRangeKernelKHR(
cmdbuf(),
nullptr,
nullptr,
fillKernel(),
1,
nullptr,
&one,
nullptr,
0,
nullptr,
&writeB,
nullptr);
std::vector<cl_sync_point_khr> waitList({writeA, writeB});
addKernel.setArg(0, deviceMemDst);
addKernel.setArg(1, deviceMemSrcA);
addKernel.setArg(2, deviceMemSrcB);
clCommandNDRangeKernelKHR(
cmdbuf(),
nullptr, // command queue, can be NULL to use the command buffer queue
nullptr, // command properties
addKernel(),
1, // work dim
nullptr, // global work offset
&gwx, // global work size
nullptr, // local work size
static_cast<cl_uint>(waitList.size()),
waitList.data(),
nullptr, // sync point
nullptr); // mutable handle
cmdbuf.finalize();
// Ensure the queue is empty and no processing is happening
// on the device before starting the timer.
commandQueue.finish();
auto start = std::chrono::system_clock::now();
for( size_t i = 0; i < iterations; i++ )
{
clEnqueueCommandBufferKHR(
0,
nullptr,
cmdbuf(),
0,
nullptr,
nullptr);
// Because the command buffer is executing in an out-of-
// order queue we need a command queue barrier to ensure
// this iteration is complete before beginning the next
// iteration.
commandQueue.enqueueBarrierWithWaitList();
}
// Ensure all processing is complete before stopping the timer.
commandQueue.finish();
auto end = std::chrono::system_clock::now();
std::chrono::duration<float> elapsed_seconds = end - start;
printf("Finished in %f seconds\n", elapsed_seconds.count());
// verification
{
const cl_uint* pDst = (const cl_uint*)commandQueue.enqueueMapBuffer(
deviceMemDst,
CL_TRUE,
CL_MAP_READ,
0,
gwx * sizeof(cl_uint) );
unsigned int mismatches = 0;
for( size_t i = 0; i < gwx; i++ )
{
const cl_uint got = pDst[0];
const cl_uint want = 3;
if( got != want )
{
if( mismatches < 16 )
{
fprintf(stderr, "MisMatch! dst[%d] == %08X, want %08X\n",
(unsigned int)i,
got,
want );
}
mismatches++;
}
}
if( mismatches )
{
fprintf(stderr, "Error: Found %d mismatches / %d values!!!\n",
mismatches,
(unsigned int)gwx );
}
else
{
printf("Success.\n");
}
commandQueue.enqueueUnmapMemObject(
deviceMemDst,
(void*)pDst );
commandQueue.finish();
}
return 0;
}