如何在OpenCV(3.0.0)OCL中启动自定义OpenCL内核?

Mic*_*uso 6 c++ opencv gpgpu opencl

我可能会误用OpenCV作为官方OpenCL C++绑定的包装器,以便我可以启动自己的内核.

但是,OpenCV确实有Program,ProgramSource,Kernel,Queue等类,它们似乎告诉我,我可以使用OpenCV启动自己的(甚至是非基于图像的)内核.我无法在这些课程中找到文档,更不用说示例了.所以,到目前为止,我对它进行了一次尝试:

#include <fstream>
#include <iostream>

#include "opencv2/opencv.hpp"
#include "opencv2/core/ocl.hpp"

#define ARRAY_SIZE 128

using namespace std;
using namespace cv;

int main(int, char)
{
    std::ifstream file("kernels.cl");
    std::string kcode(std::istreambuf_iterator<char>(file),
        (std::istreambuf_iterator<char>()));

    cv::ocl::ProgramSource * programSource;
    programSource = new cv::ocl::ProgramSource(kcode.c_str());

    cv::String errorMessage;
    cv::ocl::Program * program;
    program = new cv::ocl::Program(*programSource, NULL, errorMessage);

    cv::ocl::Kernel * kernel;
    kernel = new cv::ocl::Kernel("simple_add", *program);
    /* I'm stuck here at the args. */

    size_t globalSize[2] = { ARRAY_SIZE, 1 };
    size_t localSize[2] = { ARRAY_SIZE, 1 };    
    kernel->run(ARRAY_SIZE, globalSize, localSize, true);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

请注意,我还没有设置主机变量.我被困住了kernel->args(...).有15个重载,它们都没有指定我应该指定的顺序,每个参数:

  1. 参数索引,所以我按照内核中给出的顺序手动匹配参数.
  2. 主机变量本身.
  3. 主变量的数组大小 - 通常我会说类似的sizeof(int) * ARRAY_SIZE,虽然我曾经在普通的OpenCL中指定clEnqueueWriteBuffer函数.
  4. 设备缓冲区内存访问,例如CL_MEM_READ_ONLY

它看起来不像我调用enqueueWriteBufer(...),enqueueNDRangeKernel(...)或enqueueReadBuffer(...)因为(我猜)kernel-> run()在引擎盖下为我做了所有这些.我假设kernel-> run()会将新值写入我的输出参数.

我没有指定命令队列,设备或上下文.我认为只有一个命令队列和一个上下文,以及默认设备 - 所有这些都是在引擎盖下创建的,可以从这些类中访问.

那么,我如何使用内核的args函数呢?

小智 8

虽然我不是百分百肯定,但我找到了一种方法来做到这一点.此示例包含有关如何使用cv :: UMat,基本类型(例如int/float/uchar)和Image2D向/从自定义内核传递/检索数据的提示.

#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <opencv2/opencv.hpp>
#include <opencv2/core/ocl.hpp>

using namespace std;

void main()
{
    if (!cv::ocl::haveOpenCL())
    {
        cout << "OpenCL is not avaiable..." << endl;
        return;
    }
    cv::ocl::Context context;
    if (!context.create(cv::ocl::Device::TYPE_GPU))
    {
        cout << "Failed creating the context..." << endl;
        return;
    }

    // In OpenCV 3.0.0 beta, only a single device is detected.
    cout << context.ndevices() << " GPU devices are detected." << endl;
    for (int i = 0; i < context.ndevices(); i++)
    {
        cv::ocl::Device device = context.device(i);
        cout << "name                 : " << device.name() << endl;
        cout << "available            : " << device.available() << endl;
        cout << "imageSupport         : " << device.imageSupport() << endl;
        cout << "OpenCL_C_Version     : " << device.OpenCL_C_Version() << endl;
        cout << endl;
    }

    // Select the first device
    cv::ocl::Device(context.device(0));

    // Transfer Mat data to the device
    cv::Mat mat_src = cv::imread("Lena.png", cv::IMREAD_GRAYSCALE);
    mat_src.convertTo(mat_src, CV_32F, 1.0 / 255);
    cv::UMat umat_src = mat_src.getUMat(cv::ACCESS_READ, cv::USAGE_ALLOCATE_DEVICE_MEMORY);
    cv::UMat umat_dst(mat_src.size(), CV_32F, cv::ACCESS_WRITE, cv::USAGE_ALLOCATE_DEVICE_MEMORY);

    std::ifstream ifs("shift.cl");
    if (ifs.fail()) return;
    std::string kernelSource((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
    cv::ocl::ProgramSource programSource(kernelSource);

    // Compile the kernel code
    cv::String errmsg;
    cv::String buildopt = cv::format("-D dstT=%s", cv::ocl::typeToStr(umat_dst.depth())); // "-D dstT=float"
    cv::ocl::Program program = context.getProg(programSource, buildopt, errmsg);

    cv::ocl::Image2D image(umat_src);
    float shift_x = 100.5;
    float shift_y = -50.0;
    cv::ocl::Kernel kernel("shift", program);
    kernel.args(image, shift_x, shift_y, cv::ocl::KernelArg::ReadWrite(umat_dst));

    size_t globalThreads[3] = { mat_src.cols, mat_src.rows, 1 };
    //size_t localThreads[3] = { 16, 16, 1 };
    bool success = kernel.run(3, globalThreads, NULL, true);
    if (!success){
        cout << "Failed running the kernel..." << endl;
        return;
    }

    // Download the dst data from the device (?)
    cv::Mat mat_dst = umat_dst.getMat(cv::ACCESS_READ);

    cv::imshow("src", mat_src);
    cv::imshow("dst", mat_dst);
    cv::waitKey();
}
Run Code Online (Sandbox Code Playgroud)

下面是一个"shift.cl"文件.

__constant sampler_t samplerLN = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_LINEAR;
__kernel void shift(
   __global const image2d_t src,
   float shift_x,
   float shift_y,
   __global uchar* dst,
   int dst_step, int dst_offset, int dst_rows, int dst_cols)
{
   int x = get_global_id(0);
   int y = get_global_id(1);
   if (x >= dst_cols) return;
   int dst_index = mad24(y, dst_step, mad24(x, (int)sizeof(dstT), dst_offset));
   __global dstT *dstf = (__global dstT *)(dst + dst_index);
   float2 coord = (float2)((float)x+0.5f+shift_x, (float)y+0.5f+shift_y);
   dstf[0] = (dstT)read_imagef(src, samplerLN, coord).x;
}
Run Code Online (Sandbox Code Playgroud)

重点是使用UMat.我们使用KernelArg :: ReadOnly(umat)在内核中接收了5个参数(*data_ptr,int step,int offset,int rows,int cols); 3(*data_ptr,int step,int offset)与KernelArg :: ReadOnlyNoSize(umat); 并且只有1(*data_prt)与KernelArg :: PtrReadOnly(umat).WriteOnly和ReadWrite的规则相同.

访问数据阵列时需要步进和偏移,因为由于存储器地址对齐,UMat可能不是密集矩阵.

cv :: ocl :: Image2D可以从UMat实例构造,并且可以直接传递给kernel.args().使用image2D_t和sampler_t,我们可以从GPU的硬件纹理单元中受益,用于线性插值采样(具有实值像素坐标).

请注意,"-D xxx = yyy"build-option在内核代码中提供从xxx到yyy的文本替换.

您可以在我的帖子中找到更多代码:http://qiita.com/tackson5/items/8dac6b083071d31baf00

  • @ max0r在我的例子中,我通过`read_only image2d_t src`替换:`__global const image2d_t src`解决了这个问题.当我开始学习OpenCL时,不确定它是否是正确的方法. (2认同)