gpt4 book ai didi

c++ - OpenCL 的 clEnqueueReadBufferRect 适用于 int 但不适用于 double 数据类型

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:53 24 4
gpt4 key购买 nike

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

我写了一个小测试题(参见下面的可编译代码),从长度为 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.2 official reference * :

region

  • The (width, height, depth) in bytes of the 2D or 3D rectangle being read or written. For a 2D rectangle copy, the depth value given by region[2] should be 1.

但这充其量只是一种误导,根本不起作用。此参数的正确格式如 1.2 official specification 中所写[第 77 页] 是:

region defines the (width in bytes, height in rows, depth in slices)of the 2D or 3D rectangle being read or written. For a 2D rectanglecopy, the depth value given by region[2] should be 1. The values inregion cannot be 0.

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

* 2.0 official reference显示正确的格式。 2.1 也是,但我认为 1.2 被大量使用,也许应该更正。

关于c++ - OpenCL 的 clEnqueueReadBufferRect 适用于 int 但不适用于 double 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55524325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com