Jeff Taylor
Jeff Taylor

Reputation: 326

Is Struct Derefencing Valid OpenCL?

I tend to write OpenCL code like the following:

void do_something(float4 *thefoo)
{
    thefoo->s0++;
}

__kernel void MyKernel(__global float4 myfloat)
{
    do_something(&myfloat);
}

Of course, it's a toy example. It works as expected under Nvidida's OpenCL SDK (which I have access to), but my friend/project-partner is running AMD APP SDK, and he complains that this doesn't compile. It gives:

error: expression must have pointer-to-struct-or-union type

Using (*thefoo).s0++ works on his system, but it makes the code very ugly.

I cannot find any mention of the -> operator in the OpenCL spec, but I also don't think I'm violating any of the pointer restrictions. Is what I've written valid OpenCL?

Additionally, I intend to write a lot more code using structs in the above context instead of vector types. If this is a mistake on my part, am I going to encounter the same gotcha when he pulls my code?

Upvotes: 3

Views: 311

Answers (1)

Kyle Lutz
Kyle Lutz

Reputation: 8046

Because float4 is not a struct; it is a built-in vector type.

You must use the . operator to access components of the vector. This is why (*thefoo).s0++ will work on all conforming OpenCL implementations while thefoo->s0++ does not.

Refer to section 6.1.7 ("Vector Components") in the OpenCL specification for more information.

Upvotes: 3

Related Questions