OpenCL延迟的问题,牛人帮忙看看
目前我用 Intel i3 CPU + AMD Radeon RX560 显卡。
在程序中想使用overlap,即多个queue并行,发现有问题。下面的测试程序起3个queue,并行的调用3个异步写函数,分别将三块数据(YUV三个分量,各2MB左右)写进去。程序循环的写3次。
为什么clEnqueueWriteBuffer存在几十ms的延迟? 这个延迟时间太长了。
程序如下:
int main() //main_v2
{
cl_platform_id *platformIds;
cl_device_id device; //TODO: extend for multi devices on platform
//should release below after use
cl_context ocl_ctx;
cl_command_queue mvQueue;
cl_command_queue yuvQueue[4];
cl_command_queue queue; //according to CUDA's default stream ?
int err = 0;
//获取平台
platformIds = (cl_platform_id *)alloca(sizeof(cl_platform_id));//为平台列表申请栈空间
err = clGetPlatformIDs(1, platformIds, NULL);
//获取设备
err = clGetDeviceIDs(platformIds[0], CL_DEVICE_TYPE_GPU, 1, &device, NULL);
if (err != CL_SUCCESS) {
printf("can't get gpu device, try cpu...\n");
err = clGetDeviceIDs(platformIds[0], CL_DEVICE_TYPE_CPU, 1, &device, NULL);
}
//创建OpenCL Context
ocl_ctx = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
if (ocl_ctx == NULL) {
printf("create OpenCL context fail\n");
exit(EXIT_FAILURE);
}
//create command queue
cl_queue_properties props[] = {
CL_QUEUE_PROPERTIES,
CL_QUEUE_PROFILING_ENABLE,
0
};
for(int i=0; i<4; i++)
{
yuvQueue[i] = clCreateCommandQueueWithProperties(ocl_ctx, device, NULL/*props*/, &err);
if (yuvQueue[i] == NULL) {
printf("create command queue fail %d\n",err);
exit(EXIT_FAILURE);
}
}
//创建内存对象
cl_mem pCurFrameObj;
char *orig_buffer = (char *)malloc(1920 * 1080 * 3);
pCurFrameObj = clCreateBuffer(ocl_ctx, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
1920 * 1080 * 3, orig_buffer, &err);
//pCurFrameObj = clCreateBuffer(ocl_ctx, CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR,
// 1920 * 1080 * 3, NULL, &err);
int i = 0;
while(i++<3)
{
unsigned char *y = (unsigned char *)malloc(HEIGHT * WIDTH);
unsigned char *u = (unsigned char *)malloc(HEIGHT * WIDTH);
unsigned char *v = (unsigned char *)malloc(HEIGHT * WIDTH);
readFrameFromYUVFile("z:\\test_files\\test_mv.yuv", y, u, v, 0, WIDTH, HEIGHT);
err = clEnqueueWriteBuffer(yuvQueue[0], pCurFrameObj, CL_FALSE, 0, 1080*1920, (void*)y, 0, NULL, NULL);
err = clEnqueueWriteBuffer(yuvQueue[1], pCurFrameObj, CL_FALSE, 1080*1920, 1080*1920, (void*)u, 0, NULL, NULL);
err = clEnqueueWriteBuffer(yuvQueue[2], pCurFrameObj, CL_FALSE, 1080*1920 * 2, 1080*1920, (void*)v, 0, NULL, NULL);
//Sleep(10);
//free(v);
//free(u);
//free(y);
}
Sleep(60);
//free all opencl resources, like queues, memory objs, etc.
clReleaseMemObject(pCurFrameObj);
for(int i=0;i<4;i++)
clReleaseCommandQueue(yuvQueue[i]);
clReleaseContext(ocl_ctx);
printf("Press anykey to quit...");
getchar();
return 0;
}

在程序中想使用overlap,即多个queue并行,发现有问题。下面的测试程序起3个queue,并行的调用3个异步写函数,分别将三块数据(YUV三个分量,各2MB左右)写进去。程序循环的写3次。
为什么clEnqueueWriteBuffer存在几十ms的延迟? 这个延迟时间太长了。
程序如下:
int main() //main_v2
{
cl_platform_id *platformIds;
cl_device_id device; //TODO: extend for multi devices on platform
//should release below after use
cl_context ocl_ctx;
cl_command_queue mvQueue;
cl_command_queue yuvQueue[4];
cl_command_queue queue; //according to CUDA's default stream ?
int err = 0;
//获取平台
platformIds = (cl_platform_id *)alloca(sizeof(cl_platform_id));//为平台列表申请栈空间
err = clGetPlatformIDs(1, platformIds, NULL);
//获取设备
err = clGetDeviceIDs(platformIds[0], CL_DEVICE_TYPE_GPU, 1, &device, NULL);
if (err != CL_SUCCESS) {
printf("can't get gpu device, try cpu...\n");
err = clGetDeviceIDs(platformIds[0], CL_DEVICE_TYPE_CPU, 1, &device, NULL);
}
//创建OpenCL Context
ocl_ctx = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
if (ocl_ctx == NULL) {
printf("create OpenCL context fail\n");
exit(EXIT_FAILURE);
}
//create command queue
cl_queue_properties props[] = {
CL_QUEUE_PROPERTIES,
CL_QUEUE_PROFILING_ENABLE,
0
};
for(int i=0; i<4; i++)
{
yuvQueue[i] = clCreateCommandQueueWithProperties(ocl_ctx, device, NULL/*props*/, &err);
if (yuvQueue[i] == NULL) {
printf("create command queue fail %d\n",err);
exit(EXIT_FAILURE);
}
}
//创建内存对象
cl_mem pCurFrameObj;
char *orig_buffer = (char *)malloc(1920 * 1080 * 3);
pCurFrameObj = clCreateBuffer(ocl_ctx, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
1920 * 1080 * 3, orig_buffer, &err);
//pCurFrameObj = clCreateBuffer(ocl_ctx, CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR,
// 1920 * 1080 * 3, NULL, &err);
int i = 0;
while(i++<3)
{
unsigned char *y = (unsigned char *)malloc(HEIGHT * WIDTH);
unsigned char *u = (unsigned char *)malloc(HEIGHT * WIDTH);
unsigned char *v = (unsigned char *)malloc(HEIGHT * WIDTH);
readFrameFromYUVFile("z:\\test_files\\test_mv.yuv", y, u, v, 0, WIDTH, HEIGHT);
err = clEnqueueWriteBuffer(yuvQueue[0], pCurFrameObj, CL_FALSE, 0, 1080*1920, (void*)y, 0, NULL, NULL);
err = clEnqueueWriteBuffer(yuvQueue[1], pCurFrameObj, CL_FALSE, 1080*1920, 1080*1920, (void*)u, 0, NULL, NULL);
err = clEnqueueWriteBuffer(yuvQueue[2], pCurFrameObj, CL_FALSE, 1080*1920 * 2, 1080*1920, (void*)v, 0, NULL, NULL);
//Sleep(10);
//free(v);
//free(u);
//free(y);
}
Sleep(60);
//free all opencl resources, like queues, memory objs, etc.
clReleaseMemObject(pCurFrameObj);
for(int i=0;i<4;i++)
clReleaseCommandQueue(yuvQueue[i]);
clReleaseContext(ocl_ctx);
printf("Press anykey to quit...");
getchar();
return 0;
}

...全文
相关推荐
OpenCL中文入门完整教程
##### 8.3 OpenCL优化Nbody通过使用OpenCL优化NBody问题的计算,可以采取以下几种策略:- **线程映射**:合理地映射线程到硬件资源上,减少内存访问冲突。
OpenCV3.1 使用GPU及OpenCL加速的教程
为了避免不必要的延迟,可以使用`cv::cuda::copyTo()`或`cv::cuda::upload()`函数进行高效的数据传输。3.
altera_opencl.rar_Altera OpenCL_FPGA OpenCL_OpenCL FPGA_fpga 加速
- **低延迟与高吞吐量**:FPGA能够实现接近于硬件级别的速度,对于实时计算和大数据处理有显著优势。
OpenCL规范
**高级特性**:介绍了更复杂的主题,如图像处理、采样器和原子操作,以及如何利用这些特性来解决特定问题。
OpenCL on ZYNQ
通过OpenCL在ZYNQ上进行开发,可以充分利用ZYNQ的硬件资源,实现高性能、低延迟的应用。


