forked from bashbaug/SimpleOpenCLSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
312 lines (263 loc) · 9.3 KB
/
main.cpp
File metadata and controls
312 lines (263 loc) · 9.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
/*
// Copyright (c) 2022-2026 Ben Ashbaugh
//
// SPDX-License-Identifier: MIT
*/
#include <popl/popl.hpp>
#include <CL/opencl.hpp>
#include <cinttypes>
#include "util.hpp"
#if defined(cl_khr_command_buffer_mutable_dispatch)
#if !defined(CL_MUTABLE_DISPATCH_ASSERTS_KHR)
typedef cl_bitfield cl_mutable_dispatch_asserts_khr;
#define CL_COMMAND_BUFFER_MUTABLE_DISPATCH_ASSERTS_KHR 0x12B7
#define CL_MUTABLE_DISPATCH_ASSERTS_KHR 0x12B8
#define CL_MUTABLE_DISPATCH_ASSERT_NO_ADDITIONAL_WORK_GROUPS_KHR (1 << 0)
#endif // !defined(CL_MUTABLE_DISPATCH_ASSERTS_KHR)
const size_t gwx = 1024;
const size_t lwx = 16;
static const char kernelString[] = R"CLC(
kernel void CopyBuffer( global uint* dst, global uint* src )
{
uint id = get_global_id(0);
dst[id] = src[id];
}
)CLC";
int main(
int argc,
char** argv )
{
int platformIndex = 0;
int deviceIndex = 0;
bool noCmdBufAssert = false;
bool noCmdAssert = false;
{
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>("", "noCmdBufAssert", "Skip Command Buffer Assert", &noCmdBufAssert);
op.add<popl::Switch>("", "noCmdAssert", "Skip Command Assert", &noCmdAssert);
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: mutablecommandbufferasserts [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;
}
bool has_cl_khr_command_buffer_mutable_dispatch =
checkDeviceForExtension(devices[deviceIndex], CL_KHR_COMMAND_BUFFER_MUTABLE_DISPATCH_EXTENSION_NAME);
if (has_cl_khr_command_buffer_mutable_dispatch) {
printf("Device supports " CL_KHR_COMMAND_BUFFER_MUTABLE_DISPATCH_EXTENSION_NAME ".\n");
} else {
printf("Device does not support " CL_KHR_COMMAND_BUFFER_MUTABLE_DISPATCH_EXTENSION_NAME ", exiting.\n");
return -1;
}
cl_mutable_dispatch_fields_khr mutableCaps = 0;
clGetDeviceInfo(
devices[deviceIndex](),
CL_DEVICE_MUTABLE_DISPATCH_CAPABILITIES_KHR,
sizeof(mutableCaps),
&mutableCaps,
NULL );
if (!(mutableCaps & CL_MUTABLE_DISPATCH_GLOBAL_SIZE_KHR)) {
printf("Device does not support modifying the global work size, exiting.\n");
return -1;
}
printf("Adding Command Buffer Assert? %s\n", noCmdBufAssert ? "No" : "Yes");
printf("Adding Command Assert? %s\n", noCmdAssert ? "No" : "Yes");
cl::Context context{devices[deviceIndex]};
cl::CommandQueue commandQueue{context, devices[deviceIndex]};
cl::Program program{ context, kernelString };
program.build();
cl::Kernel kernel = cl::Kernel{ program, "CopyBuffer" };
cl::Buffer deviceMemSrc = 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 ) };
// initialization
{
cl_uint* pSrc = (cl_uint*)commandQueue.enqueueMapBuffer(
deviceMemSrc,
CL_TRUE,
CL_MAP_WRITE_INVALIDATE_REGION,
0,
gwx * sizeof(cl_uint) );
for( size_t i = 0; i < gwx; i++ )
{
pSrc[i] = (cl_uint)(i);
}
commandQueue.enqueueUnmapMemObject(
deviceMemSrc,
pSrc );
}
const cl_command_buffer_properties_khr cbprops[] = {
CL_COMMAND_BUFFER_FLAGS_KHR,
CL_COMMAND_BUFFER_MUTABLE_KHR,
CL_COMMAND_BUFFER_MUTABLE_DISPATCH_ASSERTS_KHR,
noCmdBufAssert
? 0
: (cl_command_buffer_properties_khr)
CL_MUTABLE_DISPATCH_ASSERT_NO_ADDITIONAL_WORK_GROUPS_KHR,
0,
};
cl_command_buffer_khr cmdbuf = clCreateCommandBufferKHR(
1,
&commandQueue(),
cbprops,
NULL);
kernel.setArg(0, deviceMemDst);
kernel.setArg(1, deviceMemSrc);
const cl_command_properties_khr cmdprops[] = {
CL_MUTABLE_DISPATCH_ASSERTS_KHR,
noCmdAssert
? 0
: (cl_command_properties_khr)
CL_MUTABLE_DISPATCH_ASSERT_NO_ADDITIONAL_WORK_GROUPS_KHR,
0,
};
const size_t gwx_x2 = gwx * 2;
cl_sync_point_khr sync_point;
cl_mutable_command_khr command;
clCommandNDRangeKernelKHR(
cmdbuf, // command_buffer
NULL, // command_queue - note NULL!
cmdprops, // properties
kernel(), // kernel
1, // work_dim
NULL, // global_work_offset
&gwx_x2, // global_work_size
&lwx, // local_work_size
0, // num_sync_points_in_wait_list
NULL, // sync_point_wait_list
&sync_point,// sync_point
&command); // mutable_handle
clFinalizeCommandBufferKHR(cmdbuf);
// mutate the command buffer, adding work-groups.
// This should generate an error with mutable dispatch asserts.
{
const size_t gwx_x4 = gwx * 4;
cl_mutable_dispatch_config_khr dispatchConfig = {};
dispatchConfig.command = command;
dispatchConfig.global_work_size = &gwx_x4;
const cl_uint updateCount = 1;
const cl_command_buffer_update_type_khr updateTypes[updateCount] = {
CL_STRUCTURE_TYPE_MUTABLE_DISPATCH_CONFIG_KHR,
};
const void* updateConfigs[updateCount] = {
&dispatchConfig,
};
cl_int check = clUpdateMutableCommandsKHR(
cmdbuf,
updateCount,
updateTypes,
updateConfigs );
printf("clUpdateMutableCommandsKHR() to increase work-groups returned %s.\n",
check == CL_SUCCESS ? "SUCCESS" : "an ERROR");
}
// mutate the command buffer, reducing work-groups.
// This should not generate an error even with mutable dispatch asserts.
{
cl_mutable_dispatch_config_khr dispatchConfig = {};
dispatchConfig.command = command;
dispatchConfig.global_work_size = &gwx;
const cl_uint updateCount = 1;
const cl_command_buffer_update_type_khr updateTypes[updateCount] = {
CL_STRUCTURE_TYPE_MUTABLE_DISPATCH_CONFIG_KHR,
};
const void* updateConfigs[updateCount] = {
&dispatchConfig,
};
cl_int check = clUpdateMutableCommandsKHR(
cmdbuf,
updateCount,
updateTypes,
updateConfigs );
printf("clUpdateMutableCommandsKHR() to reduce work-groups returned %s.\n",
check == CL_SUCCESS ? "SUCCESS" : "an ERROR");
}
clEnqueueCommandBufferKHR(
0,
NULL,
cmdbuf,
0,
NULL,
NULL);
// 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++ )
{
if( pDst[i] != i )
{
if( mismatches < 16 )
{
fprintf(stderr, "MisMatch! dst[%d] == %08X, want %08X\n",
(unsigned int)i,
pDst[i],
(unsigned int)i );
}
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 );
}
clReleaseCommandBufferKHR(cmdbuf);
return 0;
}
#else
#pragma message("mutablecommandbuffers: cl_khr_command_buffer_mutable_dispatch not found. Please update your OpenCL headers.")
int main()
{
printf("mutablecommandbuffers: cl_khr_command_buffer_mutable_dispatch not found. Please update your OpenCL headers.\n");
return 0;
};
#endif