cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

adm271828
Journeyman III

Passing a NULL buffer to a kernel

According to OpenCL 1.1 (§ 5.7.2) it is possible to pass a null buffer to a kernel: "If the argument is a buffer object, the arg_value pointer can be NULL or point to a NULL value in which case a NULL value will be used as the value for the argument declared as a pointer to __global or __constant memory in the kernel."

However, I can't see it working. Here is what I get for various attempts (SDK 2.4).

Regards,

   Antoine

 

err = clSetKernelArg (k, 0, 0, 0); // -> err = CL_INVALID_ARG_VALUE cl_mem buf = 0; err = clSetKernelArg (k, 0, sizeof(cl_mem), &buf); // -> err = CL_INVALID_MEM_OBJECT err = clSetKernelArg (k, 0, 0, &buf); // -> err = CL_INVALID_ARG_SIZE

Tags (1)
0 Likes
12 Replies
himanshu_gautam
Grandmaster

Passing a NULL buffer to a kernel

Can you please post a test case showing the issue. How do you create the buffer object?

0 Likes
adm271828
Journeyman III

Passing a NULL buffer to a kernel

Hi,

the test case is the code already provided. It shall be clear I don't create a buffer, since I want to pass a NULL, and my understanding from the OpenCL spec (quoted in initial message) is that it is sufficient to pass a NULL arg_value, in which case the kernel will receive a null pointer.

Do you suggest I shall create a buffer with zero size and the kernel would receive a null pointer? I didn't try, but if it is the case, it doesn't seem to conform to OpenCL, and it would waste a buffer ressource (in case clCreateBuffer succeeds with null size).

Maybe I missunderstood the spec?

Best regards,

    Antoine

0 Likes
nou
Exemplar

Passing a NULL buffer to a kernel

IMHO clSetKernelArg(k, 0, sizeof(cl_mem), 0); is correct way to specify NULL pointer. (but return CL_INVALID_ARG_VALUE) OpenCL specification is little contradiction in this area.

CL_INVALID_ARG_VALUE if arg_value specified is NULL for an argument that is not declared with the __local qualifier or vice-versa.

so you can specify NULL pointer but you get error if do so

0 Likes
tonyo_au
Journeyman III

Passing a NULL buffer to a kernel

I do

size_t localMemBufferSize = xxx;

clSetKernelArg(k,0,localMemBufferSize,NULL);

 

this works on my NVidia cards - I haven't tried it on my ATI yet.

 

I have had problems with local memory like this

 

clSetKernelArg(k,0,sizeof(cl_mem),p1);

clSetKernelArg(k,1,localMemBufferSize,NULL);

when p1 structure wasn't an acceptable multiple of 16 bytes. I haven't resolved this issue except by trial and error with filler fields

0 Likes
adm271828
Journeyman III

Passing a NULL buffer to a kernel

clSetKernelArg (k, 0, size, NULL), with size > 0 is for local memory, as explained int the spec. It works on my ATI card.

Once again, I want to pass a null pointer to global or constant memory (as described in the spec...). Seems to be broken (but not critical).

0 Likes
himanshu_gautam
Grandmaster

Passing a NULL buffer to a kernel

hi adm,

This appears to be a snack in the opencl spec. So it is better to report at khronos forums.

Meanwhile, i was interested if you were having some specific purpose for allocating a NULL buffer or were just experimenting.

Thanks

0 Likes
adm271828
Journeyman III

Passing a NULL buffer to a kernel

Hi Himanshu,

I'm not just experimenting. I'd like to pass an optional output buffer to collect extra information from a kernel.

On the kernel side, I'd have written: kernel k (__global int4* out) { ... if (out != 0) { ... write extra info here } ... }.

Of course I can do it by having this optional buffer+code enabled/disabled conditionnaly at compile time. But since I will select each version at runtime in a non predictible pattern, it means I will have two kernels loaded at the same time, wasting resources (the extra code executed when out != 0 is about 10 % of the whole kernel).

This is not critical however (having intrinsics that will help me not to generate 2 instructions when only a single one is necessary is much more critical... for instance)

As for OpenCL spec, as an end-user, I can't say the spec is incorrect (if this is what you mean). Somebody wrote this sentence, that seems to make sense. If during approval process, it was decided to remove it and this was not done, then it's up to people that make products compliant to the spec to say it to the OpenCL committee. Especialy if they are part of the committee.

Best regards,

Antoine

0 Likes
hduregger
Journeyman III

Passing a NULL buffer to a kernel

I agree, that part of the specification caught me aswell.

Apart from creating a dummy 'NULL' buffer to bind when that kernel argument is not used, I also passed an int used as a boolean, to indicate whether the kernel should output data into the buffer or not. This way you don't have to create a second kernel.

0 Likes
ajk
Journeyman III

Re: Passing a NULL buffer to a kernel

Hello. This issue is still unresolved. clSetKernelArg fails when passing NULL as arg_value.

There was the same bug in Intel OpenCL: https://software.intel.com/en-us/forums/topic/281206 and it was resolved.

0 Likes