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

Using enqueueFillBuffer in openCL 2.0 produces garbage

我使用了下面的代码

 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--^^^^^^

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