OpenCL和std :: vector <bool>不兼容</bool>

时间:2015-03-18 06:50:30

标签: c++ c++11 vector boolean opencl

我有一个C ++输入,形成一个std :: vector,我想把它传递给我的OpenCL内核(Nvidia平台)。

但是执行以下操作后,我一直遇到分段错误:

queue.enqueueReadBuffer(dev_input, CL_TRUE, 0, sizeof(bool) * input.size(), &input);

因此,我尝试将std::vector<bool>复制到bool[],一切都很完美。但是,将向量转换为C数组(&input[0], input.data())的常用方法根本不起作用。

您对ReadBuffer或快速分配到C数组有什么建议吗?

谢谢!

3 个答案:

答案 0 :(得分:6)

两个问题。

  1. 实现可以(可选)优化std::vector<bool>以提高空间效率,可能通过将值打包到各个内存位中。这与bool

  2. 数组的布局不匹配
  3. 您无法传递矢量的地址,就好像它是一个数组一样。

    std::vector<bool> input;
    queue.enqueueReadBuffer(
        dev_input, CL_TRUE, 0,
        sizeof(bool) * input.size(), &input);
    //                               ^^^^^^ wrong
    

    如果要将矢量作为指针传递,则必须使用input.data()&input[0]之类的东西。这些不起作用的原因是因为std::vector<bool>旨在阻止它,因为可能会优化实施(参见第1点)。

  4. 这基本上是一个&#34;疣&#34;在C ++库中已经及时完成了。

    解决这个问题很痛苦。

    // One way to do things...
    struct Bool { bool value; }
    std::vector<Bool> input;
    
    // Another way to do things...
    // (You have to choose the right type here, it depends
    // on your platform)
    std::vector<int> input;
    
    // Yet another way...
    bool *input = new bool[N];
    

    这就是为什么这是一个如此大的臭屁。

答案 1 :(得分:1)

一种可能性是使用copy()提供的Boost.Compute算法。

基本上,你可以这样做:

// vector of bools on the host
std::vector<bool> host_vec = { ... };

// vector of bools on the device (each represented with a uchar)
boost::compute::vector<uchar_> gpu_vec(host_vec.size(), ctx);

// copy bool values on host to the device
boost::compute::copy(host_vec.begin(), host_vec.end(), gpu_vec.begin(), queue);

Boost.Compute将负责将数据正确复制到计算设备。

答案 2 :(得分:1)

还要补充一点:

OpenCL内核中的bool类型不保证与任何主机端类型匹配。通常,应避免在主机 - 设备互连中使用布尔类型。

相关问题