Macro BOOST_COMPUTE_ADAPT_STRUCT

| Home | Libraries | People | FAQ | More

## Macro BOOST_COMPUTE_ADAPT_STRUCT

BOOST_COMPUTE_ADAPT_STRUCT

## Synopsis

    // In header: <boost/compute/types/struct.hpp>

    BOOST_COMPUTE_ADAPT_STRUCT(type, name, members)

## Description

The BOOST_COMPUTE_ADAPT_STRUCT() macro makes a C++ struct/class available to OpenCL kernels.

For example, to adapt a 2D particle struct with position (x, y) and velocity (dx, dy):

    // c++ struct definition
    struct Particle
    {
        float x, y;
        float dx, dy;
    };

    // adapt struct for OpenCL
    BOOST_COMPUTE_ADAPT_STRUCT(Particle, Particle, (x, y, dx, dy))

After adapting the struct it can be used in Boost.Compute containers and with Boost.Compute algorithms:

    // create vector of particles
    boost::compute::vector<Particle> particles = ...

    // function to compare particles by their x-coordinate
    BOOST_COMPUTE_FUNCTION(bool, sort_by_x, (Particle a, Particle b),
    {
        return a.x < b.x;
    });

    // sort particles by their x-coordinate
    boost::compute::sort(
        particles.begin(), particles.end(), sort_by_x, queue
    );

Due to differences in struct padding between the host compiler and the device compiler, the `BOOST_COMPUTE_ADAPT_STRUCT()` macro requires that the adapted struct is packed (i.e. no padding bytes between members).

See Also:

type_name()

Parameters:

`members` | A tuple of the struct's members.
`name` | The OpenCL name.
`type` | The C++ type.