Tuấn Phạm
Tuấn Phạm

Reputation: 688

OpenCL - CL_INVALID_VALUE while reading from Buffer

because of the code is rather long, so i try to post what is important.

I allocated an array, create the buffer, pass to kernel like this.

cl_mem correspondenceRes= NULL;
int size_correspondence_result = model_voxelized->size()*3*num_angle_steps*num_shift_steps;
float* correspondence_result = new float[size_correspondence_result];
correspondenceRes = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, sizeof(size_correspondence_result),correspondence_result,&ret);
ret = clSetKernelArg(kernel,6,sizeof(correspondenceRes), &correspondenceRes);

And later on read it back from buffer :

ret =  clEnqueueReadBuffer(command_queue,correspondenceRes,CL_TRUE,0,sizeof(correspondence_result), &correspondence_result[0],0,NULL,NULL);

Then i got the code -30, speak CL_INVALID_VALUE, also could mean that I put the wrong size of the object in ClEnqueueReadBuffer. Meanwhile for another argument, I created and read like this :

cl_mem corr_result = NULL;
cl_int* corr_result_count = new cl_int[prod];
corr_result= clCreateBuffer(context, CL_MEM_READ_WRITE| CL_MEM_COPY_HOST_PTR, sizeof(corr_result_count),corr_result_count,&ret);
ret=clSetKernelArg(kernel,7,sizeof(corr_result),&corr_result);

An later on read it from Buffer with this :

ret = clEnqueueReadBuffer(command_queue,corr_result,CL_TRUE,0,sizeof(corr_result_count), &corr_result_count[0],0,NULL,NULL);

And the reading part works perfectly, BUT the values inside the array is not what i expected (All random numbers, unless my kernel code define it explicitly).

Note that I do not do any calculation inside kernel, just pass and read it back.

Upvotes: 2

Views: 3185

Answers (1)

colonel of truth
colonel of truth

Reputation: 121

For the first issue, you are using the wrong size when creating your buffer. sizeof(size_correspondence_result) should be sizeof(float) * size_correspondence_result, otherwise it's equivalent to sizeof(int). You need to use this same size when reading it back, as well. I strongly suggest you read up on how sizeof() works so you understand what value it's returning.

As for the second issue, you are allocating memory, and then immediately creating a buffer with CL_MEM_COPY_HOST_PTR, which copies your uninitialized memory into the buffer. There should be no reason to do this; either initialize the memory first, or if you only want uninitialized memory for write/read on the device itself, don't use CL_MEM_COPY_HOST_PTR (and assume the memory is uninitialized in your kernel).

Upvotes: 3

Related Questions