Results 1 to 5 of 5

Thread: OpenCL struct alignment on host and device

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1

    OpenCL struct alignment on host and device

    I have a query regarding the byte alignment of structure in opencl. I read somewhere that I should be making sure that the size of the structure is same on host and device by making use of __attribute__ ((aligned (X))).

    My question is that is it sufficient to do something like (on both host and device) -
    struct sampleStruct
    {
    float value_ONE;
    float value_TWO;
    } __attribute__ ((aligned ());

    or do I need to do the following (align all the individual elements) -
    struct sampleStruct
    {
    float value_ONE __attribute__ ((aligned ());
    float value_TWO __attribute__ ((aligned ());
    };

    Why I ask this is because I want to understand the case where by default float on the host is assigned 8 bytes and on the device 4 bytes? Do I need to care for this kind of scenario or OpenCl compiler will handle this?

    Thanks

  2. #2
    Senior Member
    Join Date
    Sep 2002
    Location
    Santa Clara
    Posts
    105

    Re: OpenCL struct alignment on host and device

    Basic data types in OpenCL (integer and float data types) have defined sizes and alignment requirements (refer to section 6.1.5 of the OpenCL 1.1 specification). To make sure the alignment matches between the host & OpenCL kernel that is using these data types on the device, use the cl_<type name> for declaring structs that will be shared by the host and device (refer to table after table 6.1 in the 1.1 spec)

    Note that this only works for integer & floating-point data types. The alignment rules do not work for pointers. This is one reason why pointers cannot be embedded inside a struct in OpenCL.

  3. #3
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: OpenCL struct alignment on host and device

    Affie, isn't it still possible for both the host and the device compiler to introduce additional padding between struct members and at the end of the struct (which is important for AoS)? Section 6.7.2.1 p13 and p15 of the C99 spec talks about this.
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

  4. #4

    Re: OpenCL struct alignment on host and device

    Affie, isn't it still possible for both the host and the device compiler to introduce additional padding between struct members and at the end of the struct (which is important for AoS)? Section 6.7.2.1 p13 and p15 of the C99 spec talks about this.
    The problem is, how do you guarantee that the host and device compiler will introduce the additional padding in the same place? Note that the spec says that padding may be added. Compilers will then choose to add padding as they believe appropriate, and there is not even a guarantee that two different host compilers will add padding in the same place, let alone a host and device compiler which are working on effectively different architectures.

    This is exactly why hand-specifying the alignment (and thus forcing the compiler to pad as appropriate) is needed.

  5. #5
    Senior Member
    Join Date
    Dec 2011
    Posts
    150

    Re: OpenCL struct alignment on host and device

    One technique is to put the largest data members first, then work your way down in size. Otherwise, when you mix data sizes, you can get different padding on different platforms.

Similar Threads

  1. Running kernel on host vs device
    By jimmyz500 in forum OpenCL
    Replies: 7
    Last Post: 12-11-2012, 08:32 AM
  2. memory alignment for struct members
    By fangq in forum OpenCL
    Replies: 4
    Last Post: 04-12-2011, 02:00 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
  •