I'm trying to transform a sequence of numbers in parallel in C++17 and store the results in a vector. But so far I'm unable to find a way to represent the sequence without explicitly filling an array with it, like so:
void transformRange(size_t N)
{
// Want to replace nums with a generator (an iterator that is not associated with a container)
std::vector<size_t> nums(N);
std::iota(nums.begin(), nums.end(), 0);
std::vector<size_t> a(N);
std::transform(std::execution::par, nums.begin(), nums.end(), a.begin(), fun);
}
I want this to be doable in parallel (hence the std::execution::par), and the above transform does work in parallel, but the iota does not and it's 3X the memory bandwidth.
I'm also open to deriving the sequence number from the reference to the value being transformed, but I can't get the syntax right. Something like:
void transformRange2(size_t N)
{
std::vector<size_t> a(N);
std::transform(std::execution::par, a.begin(), a.end(), a.begin(), [&](auto & i) {fun(&i - a.begin()); });
}