我刚开始接触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用户提供帮助。
谢谢, David