PDA

View Full Version : Can the vector types be made more C++ friendly?



coleb
05-19-2009, 11:07 AM
The OpenCL vector types defined in cl_platform.h do not play well with C++. You can not do something this simple:

#include <vector>
typedef float cl_float4[4] __attribute__((aligned(16)));
int main()
{
std::vector<cl_float4> vals;

cl_float4 a = {1.0f, 2.0f, 3.0f, 4.0f};
vals.push_back(a);
}

Because "ISO C++ forbids assignment of arrays". If the definition would be changed to a struct like the following:
typedef struct cl_float4
{
float x,y,z,w;
} cl_float4 __attribute__((aligned(16)));

Not only would this play nicer with C++, it would also mirror the behaviour of float4 in device code, which is a win-win in my book.

-Brian

See also:
http://groups.google.com/group/comp.lan ... 01707c9cc7 (http://groups.google.com/group/comp.lang.c++/browse_thread/thread/524bbcc0060b32dc/75bce401707c9cc7)
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40192

bgaster
06-23-2009, 10:59 PM
It is worth noting that cl_platform.h is only an example and a particular vendor is free to implement this file as they feel necessary. That said we have also run into this issue but after consideration we feel it makes more sense to implement the host vector types using the compiler's extension for vector types.

For example cl_float4 with Visual Studio 2008 could be implemented as:

typedef union __declspec(intrin_type) _CRT_ALIGN(16) cl_float4
{
cl_float f32[4];
} cl_float4;

and with __GNUC__ defined, this is what we have:

typedef cl_float cl_float4 __attribute__ ((__vector_size__ (16), __may_alias__));

As in both cases these define a non-array type it is valid to do assignment and so your example works.