Results 1 to 2 of 2

Thread: Can the vector types be made more C++ friendly?

  1. #1

    Can the vector types be made more C++ friendly?

    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};

    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.


    See also: ... 01707c9cc7

  2. #2
    Junior Member
    Join Date
    Apr 2009

    Re: Can the vector types be made more C++ friendly?

    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.

Similar Threads

  1. Non-power of 2 vector types
    By PaulS in forum OpenCL
    Replies: 2
    Last Post: 02-01-2010, 11:15 AM
  2. Replies: 1
    Last Post: 10-01-2009, 05:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean