问题
I have created a vector with some values. Then Created a cl_buffer for that vector and pass it to the OpenCL kernel using kernel Arguments. Like this:
In host Code:
std::vector<cl_double> inp;
inp.resize(1024);
for( int i = 0; i<1024;i++)
{
inp[i] = i;
}
filter_kernel = cl::Buffer(context,CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR,sizeof(cl_double)*inp.size(),(void*)&inp[0],&err); // also tried (void*)inp.data()
kernel.setArg(0, filter_kernel);
In Kernel Code:
__kernel void test(__global double* inp)
{
for(int m = 0;m<10;m++)
{
printf("inp values are : %d \n",inp[m]);
}
}
This is just and example of showing, how I am passing values in the vector to OpenCL kernel in my program. Is there something wrong with it? As when I print the values, I am getting some random garbage values every time.
I am using MacOS system and Xcode. Device is Intel HD graphics 4000
回答1:
If you are using CL_MEM_USE_HOST_PTR you'll need to map the memory region so that the device can see it. Check https://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/clEnqueueMapBuffer.html . Also I would recommned reading the Intel tutorial on the subject.
Also HD 4000 shouldn't have double support. Are you sure that the code is running on the GPU and not CPU?
回答2:
I am just compiling the answer, as all the points(in comments) made it work together.
1) change the data type to float from double as Intel does not work with double properly and gives incorrect result.
2) Other mistake was of size, while passing the size in buffer and during readbuffer, it should be inp*sizeof(float) as we are using float and not double now!
来源:https://stackoverflow.com/questions/40719063/passing-values-of-vector-in-the-opencl-kernel