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

Yan*_*ael 1 c++ boolean vector opencl c++11

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

但是,执行以下操作时,我不断遇到分段错误:

queue.enqueueReadBuffer(dev_input, CL_TRUE, 0, sizeof(bool) * input.size(), &input);
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢!

Die*_*Epp 6

两个问题.

  1. 实现可以(可选地)优化std::vector<bool>以提高空间效率,可能通过将值打包到单独的存储器位中.这与数组的布局不匹配bool.

  2. 您不能传递矢量的地址,就像它是一个数组一样.

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

    如果要将向量作为指针传递,则必须使用input.data()或类似&input[0].这些不起作用的原因是因为std::vector<bool>它可以防止它,因为可以优化实现(参见第1点).

这基本上是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];
Run Code Online (Sandbox Code Playgroud)

这就是为什么这是一个如此大的臭疣.