OpenCL内存缓冲区传输提供不正确的数据

我是openCL的新手,我试图从一个非常简单的内核开始.现在我只是试图将数据传输到内核并返回,这样我就可以验证正在设置的数据是否正确.但是,我的行为非常奇怪.这是我的主机代码:

string PROGRAM_FILE = "C:\\Projects\\AnimatLabSDK\\OpenNeuronCL\\Libraries\\OpenNeuronCL\\Kernels\\FastSpikingNeuron.cl";
string KERNEL_FUNC = "FastSpikingNeuron";

std::vector<cl::Platform> platforms;
std::vector<cl::Device> devices;  
int i;

// Data
cl::NDRange ndGlobal(DATA_SIZE);  
cl::NDRange ndLocal(LOCAL_SIZE);

cl_float aryVmIn[DATA_SIZE], aryVmOut[DATA_SIZE];
cl_float aryVahp[DATA_SIZE], aryTestOut[DATA_SIZE];

try {
  // Initialize data
  for(i=0; i<DATA_SIZE; i++) {
     aryVmIn[i] = i*0.1f; //0.0f;
     aryVahp[i] = i*0.1f; //0.0f;
  }

  // Place the GPU devices of the first platform into a context
  cl::Platform::get(&platforms);
  platforms[0].getDevices(CL_DEVICE_TYPE_GPU, &devices);
  cl::Context context(devices);

  // Create kernel
  std::ifstream programFile(PROGRAM_FILE);
  std::string programString(std::istreambuf_iterator<char>(programFile),
        (std::istreambuf_iterator<char>()));
  cl::Program::Sources source(1, std::make_pair(programString.c_str(),
        programString.length()+1));
  cl::Program program(context, source);

  //std::cout << "Program kernel: " << std::endl << programString << std::endl;

  try
  {
    program.build(devices);
  }
  catch(cl::Error e)
  {
      std::cout << e.what() << ": Error code " << e.err() << std::endl;   
      string buildlog;
      program.getBuildInfo( devices[0], (cl_program_build_info)CL_PROGRAM_BUILD_LOG, &buildlog );
      std::cout << "Error: " << buildlog << std::endl;   
      throw e;
  }

  cl::Kernel kernel(program, KERNEL_FUNC.c_str());

  // Create buffers
  cl::Buffer bufferVmIn(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(aryVmIn), aryVmIn);
  cl::Buffer bufferVmOut(context, CL_MEM_WRITE_ONLY, sizeof(aryVmOut), NULL);
  cl::Buffer bufferVahp(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(aryVahp), aryVahp);
  cl::Buffer bufferTestOut(context, CL_MEM_WRITE_ONLY, sizeof(aryTestOut), NULL);

  // Set kernel arguments
  kernel.setArg(0, bufferVmIn);
  kernel.setArg(1, bufferVmOut);
  kernel.setArg(2, bufferVahp);
  kernel.setArg(3, bufferTestOut);

  // Create queue and enqueue kernel-execution command
  cl::CommandQueue queue(context, devices[0]);

  queue.enqueueWriteBuffer(bufferVmIn, CL_TRUE, 0, sizeof(aryVmIn),  aryVmIn, NULL, NULL);
  queue.enqueueWriteBuffer(bufferVahp, CL_TRUE, 0, sizeof(aryVahp),  aryVahp, NULL, NULL);

  queue.enqueueNDRangeKernel(kernel, NULL, ndGlobal, ndLocal);

  queue.enqueueReadBuffer(bufferVmOut, CL_TRUE, 0, sizeof(aryVmOut),  aryVmOut, NULL, NULL);
  queue.enqueueReadBuffer(bufferTestOut, CL_TRUE, 0, sizeof(aryTestOut),  aryTestOut, NULL, NULL);

  // Display updated buffer
  for(i=0; i<10; i++) 
  {
      printf("%6.5f, %6.5f", aryVmOut[i], aryTestOut[i]);
      printf("\n");
  }  
}
catch(cl::Error e) 
{
  std::cout << e.what() << ": Error code " << e.err() << std::endl;   
}

这是我的内核:

__kernel void FastSpikingNeuron(__global float *aryVmIn, __global float *aryVmOut, __global float *aryVahp, __global float *aryTestOut)
{
int gid = get_global_id(0);
local float fltVahp;

fltVahp = aryVahp[gid];

aryVmOut[gid] = fltVahp;
aryTestOut[gid] = fltVahp;
}

我只是设置一些数据,然后尝试读出Vmout和TestOut,然后查看它们的一些内容.当我运行此应用程序时,我得到以下输出:

0.00000, 0.90000
0.10000, 0.90000
0.20000, 0.90000
0.30000, 0.90000
0.40000, 0.90000
0.50000, 0.90000
0.60000, 0.90000
0.70000, 0.90000
0.80000, 0.90000
0.90000, 0.90000

到目前为止,这对我没有任何意义.在内核中,我将VmOut和TestOut设置为完全相同的变量,但当我将其读回主机时,我得到了不同的结果.我在这里做错了,但我还没弄清楚它是什么.任何更有经验的opencl的帮助都会非常感激.

谢谢
大卫

最佳答案 你的内核中存在竞争条件,这是因为有多个工作项将不同的值存储到同一个内存位置,这就是你得到奇怪结果的原因.

提醒 – 本地地址空间的定义是:

The __local or local address space name is used to describe variables that need to be
allocated in local memory and are shared by all work-items of a work-group.

如果要为每个工作项设置不同的值,只需省略局部修饰符.

点赞