OpenCL的clEnqueueReadBufferRect适用于int,但不适用于double数据类型

时间:2019-04-04 20:22:54

标签: c++ c opencl

我需要以一定的速度将一些数据从设备复制到主机。我已经有使用简单的OpenCL内核的解决方案,但是在某些情况下,我希望选择不使用内核,而是使用clEnqueueReadBufferRect(或其c ++变体cl :: CommandQueue :: enqueueReadBufferRect)进行跨步复制。 / p>

我写了一个小测试问题(请参阅下面的可编译代码),该问题每隔第二个就从长度为10的数组中复制出来,并将其连续存储在大小为5的数组中。

#include <iostream>
#define __CL_ENABLE_EXCEPTIONS
#include <CL/cl.hpp>

int main(int argc, char** argv) {

    // Set up OpenCL environment

    cl::Context context;
    cl::Device device;
    cl::CommandQueue queue;

    try {

        std::vector<cl::Platform> all_platforms;
        cl::Platform::get(&all_platforms);
        cl::Platform tauschcl_platform = all_platforms[0];

        std::vector<cl::Device> all_devices;
        tauschcl_platform.getDevices(CL_DEVICE_TYPE_ALL, &all_devices);
        device = all_devices[0];

        std::cout << "Using OpenCL device " << device.getInfo<CL_DEVICE_NAME>() << std::endl;

        // Create context and queue
        context = cl::Context({device});
        queue = cl::CommandQueue(context,device);

    } catch(cl::Error &error) {
        std::cout << "OpenCL exception caught: " << error.what() << " (" << error.err() << ")" << std::endl;
        return 1;
    }


    /*********************/
    // Thus works with int
    // but not float nor double
    typedef int buf_t;
    /*********************/

    // Start buffer, length 10, filled with integers from 1 to 10
    buf_t *buf1 = new buf_t[10]{};
    for(int i = 0; i < 10; ++i)
        buf1[i] = i+1;

    // create an opencl buffer with same content
    cl::Buffer clbuf(queue, &buf1[0], &buf1[10], true);

    // receiving buffer of length 5, initialised to zero
    buf_t *buf2 = new buf_t[5]{};

    // buffer/host offsets are both (0,0,0)
    cl::size_t<3> buffer_offset;
    buffer_offset[0] = 0; buffer_offset[1] = 0; buffer_offset[2] = 0;
    cl::size_t<3> host_offset;
    host_offset[0] = 0; host_offset[1] = 0; host_offset[2] = 0;

    // We copy 5 values (with stride of 2)
    cl::size_t<3> region;
    region[0] = 1; region[1] = 5; region[2] = 1;

    try {
        queue.enqueueReadBufferRect(clbuf,
                                    CL_TRUE,
                                    buffer_offset,
                                    host_offset,
                                    region,
                                    2*sizeof(buf_t),    // buffer stride of 2
                                    0,
                                    1*sizeof(buf_t),    // host stride of 1
                                    0,
                                    buf2);
    } catch(cl::Error &error) {
        std::cout << "OpenCL exception caught: " << error.what() << " (" << error.err() << ")" << std::endl;
        return 1;
    }

    // print result
    for(int i = 0; i < 5; ++i)
        std::cout << "#" << i << " = " << buf2[i] << " --> should be " << 2*i+1 << std::endl;

    return 0;

}

当使用int作为数据类型时,此代码可以完美地工作。但是,将第38行的int更改为floatdouble会导致似乎没有任何结果,接收主机数组buf2仍然全为零。据我发现,关于clEnqueueReadBufferRect可以使用哪种数据类型没有任何限制。

我在Intel和NVIDIA上测试了上面的代码,它们的行为方式相同。我很沮丧,不知道还有什么尝试解决这个问题的。有人有什么主意吗?

1 个答案:

答案 0 :(得分:1)

这让我有些困惑,但是我想我有一个解决方案:

根据此1.2 official reference *:

  

区域

     
      
  • 正在读取或写入的2D或3D矩形的(宽度,高度,深度)以字节为单位。对于2D矩形副本,由region [2]给出的深度值应为1。
  •   

但这充其量是一种误导,而且根本行不通。 1.2 official specification [第77页]中所写的此参数的正确格式为:

  

region 定义((以字节为单位的宽度,以行为单位的高度,以切片为单位的深度)   正在读取或写入的2D或3D矩形。对于2D矩形   复制时,由region [2]给出的深度值应为1。   区域不能为0。

region[0] = 1*sizeof(buf_t); region[1] = 5; region[2] = 1;确实可以使代码在我的Intel 630HD和NVIDIA 1050TI GPU上正确运行。

* 2.0 official reference显示正确的格式。也有2.1,但我认为1.2的使用率很高,也许应该更正。