c++ - 在openCL 2.0中使用enqueueFillBuffer会产生垃圾

标签 c++ queue buffer opencl

我用下面的代码

 cl::Buffer intermediateBuffer = cl::Buffer(context, CL_MEM_READ_WRITE,
                    distDeviceWidth * distDeviceHeight * distDeviceDepth * sizeof(T));

  cl::Event copyEvent;

  T value = 0;
   try
  {
    queue.enqueueFillBuffer(intermediateBuffer, &value, 0,
                        distDeviceWidth * distDeviceHeight * distDeviceDepth * sizeof(T),
                        NULL, &copyEvent);
  }catch (const cl::Error& error)
  {
    std::cout << "  -> Prolongation class, Problem in enqueue fill buffer" << std::endl;
    std::cout << "  -> " << getErrorString(error) << std::endl;
    exit(0);
  }

  try
  {
    queue.finish();
  }catch (const cl::Error& error)
  {
    std::cout << "  -> Prolongation class, Problem in finishing fill buffer" << std::endl;
    std::cout << "  -> " << getErrorString(error) << std::endl;
    exit(0);
  }
  copyEvent.wait();

但是,当我从设备读取数据并将其打印在主机上时,函数enqueueFillBuffer会生成垃圾。我不知道为什么需要提及的是我使用openCL 2.0构建数据。

最佳答案

在OpenCL C API的原始clEnqueueFillBuffer()函数中,该模式作为一对指针和大小传递。在C++包装器enqueueFillBuffer()中,模式被简化为一个参数,其处理方式如下:

template<typename PatternType>
cl_int enqueueFillBuffer(
    const Buffer& buffer,
    PatternType pattern,
    ::size_t offset,
    ::size_t size,
    const VECTOR_CLASS<Event>* events = NULL,
    Event* event = NULL) const
{
    cl_event tmp;
    cl_int err = detail::errHandler(
        ::clEnqueueFillBuffer(
            object_, 
            buffer(),
            static_cast<void*>(&pattern),
            sizeof(PatternType), 

注意包装器如何在内部获取传递的模式数据的地址(&pattern)并自动推断出大小。 (sizeof(PatternType))

这意味着如果将指针值传递给enqueueFillBuffer(),它将使用指针值作为模式,因为指向指针的指针将传递给OpenCL。这正是您的代码似乎正在做的事情:
T value = 0;
//…
queue.enqueueFillBuffer(intermediateBuffer, &value, 0,
//               don't pass a pointer here--^^^^^^

删除&应该可以解决问题。

关于c++ - 在openCL 2.0中使用enqueueFillBuffer会产生垃圾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61517334/

相关文章:

c# - BufferedWaveProvider 的问题

c# - 使用它的指针从托管 C# 中释放非托管内存

oracle - 如何测试调整后的 oracle sql 以及如何清除系统/硬件缓冲区?

c++ - 如何在 C++ 中的 boost::future 中添加回调

c++ - 为什么我的 UWP 游戏在发布时比在 Debug模式下慢?

javascript - 队列列表输出没有像应有的那样输出

php - Laravel 队列只运行一个作业

algorithm - 连续子数组的最大和不大于 k

c++ - boost::container::small_vector 似乎没有就地分配

c++ - 实现模板,我有问题 "matching function definitions to existing declarations"