Transfer data from Mat/oclMat to cl_mem (OpenCV + OpenCL)

房东的猫 提交于 2019-12-03 14:38:24

I've written a set of interop functions for the Boost.Compute library which ease the use of OpenCL and OpenCV. Take a look at the opencv_copy_mat_to_buffer() function.

There are also functions for copying from a OpenCL buffer back to the host cv::Mat and for copying cv::Mat to OpenCL image2d objects.

Calculate memory bandwidth, achieved in Host-Device interconnections.

If you get ~60% and more of maximal bandwidth, you've nothing to do, memory transfer is as fast as it can be. But if your bandwidth results are lower that 55% - 60% of theoretical maximum, try to use multiple command queues with unblocking operations (don't forget to sync at the end). Also, pay attention on avg image size. Small data transfers usually have big overhead rate.

If your Device uses shared memory, use memory mapping instead of read/write, this may dramatically save time. If Device has it's own memory, apply pinned memory technique, which is well described in NVIDIA OpenCL Best Practices Guide.

The documentation of oclMat states that there is some sort of functionality to the underlying ocl buffer data:

//! pointer to the data(OCL memory object)
uchar *data;

If you have clMat already in the device, you can simply perform a copy buffer from clMat.data to your clBuffer. But you will have to hack a little bit the memory, accessing some private members of the oclMat

Something like:

clEnqueueCopyBuffer(command_queue, (clBuffer *)oclMat.data, dst_buffer, 0, 0, size);

NOTE: Take care with the casting, maybe you have to cast another pointer.

For your comment, it's right. The oclMat can be used as cl_mem(void *) for device, since it was alloced by OpenCL device.

Additionally, you can creat svm memory(for example void* svmdata) at first, and then assign a Mat like: Mat A(rows, cols, CV_32FC1, svmdata). Now you can process the Mat A between host and device without memory copy. (PS. The svm memory is the new character of OCL, it can be created by clSVMAlloc).

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