Timing execution of OpenCL kernels

人盡茶涼 提交于 2019-12-10 19:50:56

问题


Is this a correct way of timing kernel execution time for OpenCL? I am quite keen on using the c++ wrapper (which unfortunately does not have many examples of timings).

cl::CommandQueue queue(context, device, CL_QUEUE_PROFILING_ENABLE, &err);
checkErr(err, "Cannot create the command queue");

/* Warm-up */
for (unsigned i = 0; i < NUMBER_OF_ITERATIONS; ++i)
{
    err = queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(512), cl::NullRange, NULL, NULL);
    checkErr(err, "Cannot enqueue the kernel");
}
queue.finish();

/* Time kernels */
cl::Event start, stop;
queue.enqueueMarker(&start);
for (unsigned i = 0; i < NUMBER_OF_ITERATIONS; ++i)
{
    err = queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(512), cl::NullRange, NULL, NULL);
    checkErr(err, "Cannot enqueue the kernel");
}
queue.enqueueMarker(&stop);

stop.wait();
cl_ulong time_start, time_end;
double total_time;
start.getProfilingInfo(CL_PROFILING_COMMAND_END, &time_start);
stop.getProfilingInfo(CL_PROFILING_COMMAND_START, &time_end);
total_time = time_end - time_start;

/* Results */
cout << "Execution time in milliseconds " << total_time / (float)10e6 / NUMBER_OF_ITERATIONS << endl;

回答1:


I think your approach should work just fine (is it not?). Alternately, if you want to time each call, you can pass an event to enqueueNDRangeKernel and call getProfilingInfo on that enqueueNDRangeKernel.

cl::Event evt;
err = queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(512), cl::NullRange, NULL, &evt);
evt.wait();
elapsed += evt.getProfilingInfo<CL_PROFILING_COMMAND_END>() -
            evt.getProfilingInfo<CL_PROFILING_COMMAND_START>();


来源:https://stackoverflow.com/questions/23070933/timing-execution-of-opencl-kernels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!