Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

Journeyman III

Weird values when passing an array of structs as an openCL kernel argument

When passing an array of structs to my kernel as an argument, I get weird values for the items after the first (array[1], array[2], etc).  It seems to be an alignment issue maybe?

Here is the struct:

    typedef struct Sphere


              float3 color;

              float3 position;

              float3 reflectivity;

              float radius;

              int phong;

              bool isReflective;

    } Sphere;

What happens is that the color for the second Sphere struct in the array will actually have the last value of what I set color to on the host side (s3 or z), a non initialized zero value, and the first value of what I set position to on the host side (s0 or x).  I noticed that the float3 datatype actually still has a fourth value (s3) that does not get initialized.  I think that is where the non initialized zero value is coming from.  So it seems that it is an alignment issue.  I really am at a loss as to what I could do to fix it.  I was hoping maybe someone could shed some light on this problem.  I have ensured that my struct definitions are exactly the same on both sides.

2 Replies

See Section "6.9 Restrictions" in The OpenCL 1.2 Specification.

"k. Arguments to kernel functions in a program cannot be declared with the built-in scalar types bool, half, size_t, ptrdiff_t, intptr_t, and uintptr_t or a struct and/or union that contain fields declared to be one of these built-in scalar types. The size in bytes of these types except half are implementation-defined and in addition can also be different for the OpenCL device and the host processor making it difficult to allocate buffer objects to be passed as arguments to a kernel declared as pointer to these types. half is not supported as half can be used as a storage format only and is not a data type on which floating-point arithmetic can be performed."

I suggest changing your bool to int and see if that helps.  Also be careful that int is the same size on the host and device.

Adept I

First of all switch from float3 to float4 types, and from bool to int. Then try again. If now there won't be any errors, then the problem in data alignment and you need more carefully check your code.

Another thing you must do is not to use int and bool types on host side and switch to cl_int and cl_bool