3

Is it possible to copy arrays of custom structures using boost::compute? E.g.

struct A { float a; };
struct AB { float a; float b; };

BOOST_COMPUTE_ADAPT_STRUCT(A, A, (a))
BOOST_COMPUTE_ADAPT_STRUCT(AB, AB, (a, b))

boost::compute::vector<A> va(100);
boost::compute::vector<AB> vab(100);
boost::compute::copy(va.begin(), va.end(), vab.begin());
DikobrAz
  • 3,557
  • 4
  • 35
  • 53

1 Answers1

4

Yes, look at this example from Boost.Compute tests. Remember that:

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).

Source: boost/compute/types/struct.hpp

haahh
  • 321
  • 1
  • 2
  • I see I can copy arrays of same structures, from array of structure to POD array. But I don't see how I can copy arrays of different structures. Is the only way to write your own boost compute function to convert one struct to another? – DikobrAz Nov 23 '16 at 17:50
  • Oh, I'm sorry for not understanding your question... Now when I look at this code I think it's impossible to copy vectors of different structures using just `boost::compute::copy()`. I think it should be possible to copy them using `boost::compute::transform()` and passing a custom boost compute function which converts one to the another. – haahh Nov 24 '16 at 21:14
  • Thanks @haahh, probably custom function is a way to go. Somewhat difficult if your types are templates though. – DikobrAz Nov 27 '16 at 22:17
  • 1
    You should be able to create a function template which produced custom Boost.Compute function based on the template parameters. See [test_function.cpp#L160](https://github.com/boostorg/compute/blob/master/test/test_function.cpp#L160). – haahh Nov 30 '16 at 16:51