Skip to content

Commit f762abf

Browse files
committed
add a tester for profiling event profiling queries
1 parent b30a1be commit f762abf

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2026 Ben Ashbaugh
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
add_opencl_sample(
6+
TEST
7+
NUMBER 99
8+
TARGET profbench
9+
VERSION 120
10+
SOURCES main.cpp)

samples/99_profbench/main.cpp

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
// Copyright (c) 2026 Ben Ashbaugh
3+
//
4+
// SPDX-License-Identifier: MIT
5+
*/
6+
7+
#include <popl/popl.hpp>
8+
#include <CL/opencl.hpp>
9+
#include <chrono>
10+
#include "util.hpp"
11+
12+
static const char kernelString[] = R"CLC(
13+
kernel void inc_buffer(global int* dst)
14+
{
15+
atomic_inc(dst);
16+
}
17+
)CLC";
18+
19+
int main(
20+
int argc,
21+
char** argv )
22+
{
23+
int platformIndex = 0;
24+
int deviceIndex = 0;
25+
26+
size_t numEvents = 1024 * 1024;
27+
28+
{
29+
popl::OptionParser op("Supported Options");
30+
op.add<popl::Value<int>>("p", "platform", "Platform Index", platformIndex, &platformIndex);
31+
op.add<popl::Value<int>>("d", "device", "Device Index", deviceIndex, &deviceIndex);
32+
op.add<popl::Value<size_t>>("n", "numevents", "Number of Events", numEvents, &numEvents);
33+
34+
bool printUsage = false;
35+
try {
36+
op.parse(argc, argv);
37+
} catch (std::exception& e) {
38+
fprintf(stderr, "Error: %s\n\n", e.what());
39+
printUsage = true;
40+
}
41+
42+
if (printUsage || !op.unknown_options().empty() || !op.non_option_args().empty()) {
43+
fprintf(stderr,
44+
"Usage: profbench [options]\n"
45+
"%s", op.help().c_str());
46+
return -1;
47+
}
48+
}
49+
50+
std::vector<cl::Platform> platforms;
51+
cl::Platform::get(&platforms);
52+
53+
if (!checkPlatformIndex(platforms, platformIndex)) {
54+
return -1;
55+
}
56+
57+
printf("Running on platform: %s\n",
58+
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );
59+
60+
std::vector<cl::Device> devices;
61+
platforms[platformIndex].getDevices(CL_DEVICE_TYPE_ALL, &devices);
62+
63+
printf("Running on device: %s\n",
64+
devices[deviceIndex].getInfo<CL_DEVICE_NAME>().c_str() );
65+
66+
cl::Context context{devices[deviceIndex]};
67+
cl::CommandQueue commandQueue{context, devices[deviceIndex], CL_QUEUE_PROFILING_ENABLE};
68+
69+
cl::Program program{ context, kernelString };
70+
program.build();
71+
cl::Kernel kernel = cl::Kernel{ program, "inc_buffer" };
72+
73+
cl::Buffer buf = cl::Buffer{
74+
context,
75+
CL_MEM_ALLOC_HOST_PTR,
76+
sizeof(cl_int) };
77+
78+
kernel.setArg(0, buf);
79+
80+
const cl_int zero = 0;
81+
commandQueue.enqueueFillBuffer(buf, zero, 0, sizeof(zero));
82+
83+
std::vector<cl::Event> events;
84+
events.reserve(numEvents);
85+
86+
printf("Enqueueing kernels to create %zu events...\n", numEvents);
87+
for (int i = 0; i < numEvents; i++) {
88+
cl::Event event;
89+
commandQueue.enqueueNDRangeKernel(
90+
kernel,
91+
cl::NullRange,
92+
cl::NDRange{1},
93+
cl::NullRange,
94+
nullptr,
95+
&event);
96+
events.push_back(std::move(event));
97+
}
98+
99+
printf("Waiting for %zu kernels to complete...\n", numEvents);
100+
commandQueue.finish();
101+
102+
cl_ulong totalTimeNS = 0;
103+
printf("Querying profiling data for %zu events...\n", numEvents);
104+
105+
auto start = std::chrono::system_clock::now();
106+
107+
for (const auto& event : events) {
108+
totalTimeNS =
109+
event.getProfilingInfo<CL_PROFILING_COMMAND_END>() -
110+
event.getProfilingInfo<CL_PROFILING_COMMAND_START>();
111+
}
112+
113+
auto end = std::chrono::system_clock::now();
114+
std::chrono::duration<float> queryTimeS = end - start;
115+
printf("Querying profiling data took %f s (%f us per event)\n",
116+
queryTimeS.count(), queryTimeS.count() * 1000000 / numEvents);
117+
118+
int result = 0;
119+
commandQueue.enqueueReadBuffer(
120+
buf,
121+
CL_TRUE,
122+
0,
123+
sizeof(result),
124+
&result);
125+
126+
if (result == numEvents) {
127+
printf("Success.\n");
128+
} else {
129+
printf("Unexpected result: %d\n", result);
130+
}
131+
132+
return 0;
133+
}

samples/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ add_subdirectory( 06_ndrangekernelfromfile )
7878
add_subdirectory( 10_queueexperiments )
7979
add_subdirectory( 16_floatatomics )
8080

81+
add_subdirectory( 99_profbench )
82+
8183
set(BUILD_EXTENSION_SAMPLES TRUE)
8284
if(NOT TARGET OpenCLExt)
8385
message(STATUS "Skipping Extension Samples - OpenCL Extension Loader is not found.")

0 commit comments

Comments
 (0)