【发布时间】:2020-08-23 02:32:21
【问题描述】:
我们正在分析在主机和设备上的 NVidia GPU 上运行的 OpenCL 应用程序。我们惊讶地发现(基于 gperftools)主机在 clGetPlatformInfo 上花费了 44% 的时间,这个方法在我们自己的代码中只调用了一次。它由clEnqueueCopyBuffer_hid、clEnqueueWriteBuffer_hid 和clEnqueueNDRangeKernel_hid 调用(可能还有所有其他clEnqueue 方法,但它们在我们的代码中很少被调用)。由于这占用了我们的主机时间,而且我们现在似乎受到主机速度的限制,我需要知道是否有办法消除这些额外的调用。
为什么每次 OpenCL 调用都会调用它? (大概是可以存储在上下文中的静态信息?)我们是否可能错误地初始化了上下文?
编辑:我被要求提供 MWE:
#include <CL/opencl.h>
#include <vector>
using namespace std;
int main ()
{
cl_uint numPlatforms;
clGetPlatformIDs (0, nullptr, &numPlatforms);
vector<cl_platform_id> platformIdArray (numPlatforms);
clGetPlatformIDs (numPlatforms, platformIdArray.data (), nullptr);
// Assume the NVidia GPU is the first platform
cl_platform_id platformId = platformIdArray[0];
cl_uint numDevices;
clGetDeviceIDs (platformId, CL_DEVICE_TYPE_GPU, 0, nullptr, &numDevices);
vector<cl_device_id> deviceArray (numDevices);
clGetDeviceIDs (platformId, CL_DEVICE_TYPE_GPU, numDevices, deviceArray.data (), nullptr);
// Assume the NVidia GPU is the first device
cl_device_id deviceId = deviceArray[0];
cl_context context = clCreateContext (
nullptr,
1,
&deviceId,
nullptr,
nullptr,
nullptr);
cl_command_queue commandQueue = clCreateCommandQueue (context, deviceId, {}, nullptr);
cl_mem mem = clCreateBuffer (context, CL_MEM_READ_WRITE, sizeof(cl_int),
nullptr, nullptr);
cl_int i = 0;
while (true)
{
clEnqueueWriteBuffer (
commandQueue,
mem,
CL_TRUE,
0,
sizeof (i),
&i,
0,
nullptr,
nullptr);
++i;
}
}
【问题讨论】:
标签: performance profiling opencl