OpenCL延迟的问题,牛人帮忙看看

猫大技师 2019-12-05 11:38:53
目前我用 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;
}
...全文
196 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

601

社区成员

发帖
与我相关
我的任务
社区描述
异构开发技术
社区管理员
  • OpenCL和异构编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧