0
votes

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

2
You should be using kernel.setArg(0, filter_kernel) rather than using inp the pointer to the memory in the host code. - Neapolitan
Ohh yes, I wrote it wrong here by mistake, but in my code I wrote it like you have mentioned. I corrected it now. - Akash
If you provide a complete minimal example that demonstrates the failure, others may be able to reproduce it and diagnose the problem more easily. - Neapolitan
I don't think so, Actually its link with one whole library so it will not be possible. Otherwise would have provided it. - Akash

2 Answers

1
votes

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?

0
votes

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!