Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does parallel STL handle insert iterators such as std::back_insert_iterator?

Tags:

Are parallel STL algorithms compliant with a std::back_insert_iterator??

I might be misinterpreting the difference between std::par and std::par_vec, does std::par_vec mean that the output range is required to be pre-allocated?

Code example:

auto numbers = {1,2,3,4,5,6};
auto squared = std::vector<int>{};
std::transform(
  **std::par/std::par_vec,**
  numbers.begin(),
  numbers.end(),
  std::back_inserter(squared),
  [](auto val) { 
    return val*val; 
  }
);

Update

Simplified questions as my first question was a result of misreading an article.

like image 480
Viktor Sehr Avatar asked Jun 27 '17 19:06

Viktor Sehr


1 Answers

Are parallel STL algorithms compliant with a std::back_insert_iterator??

N4659 specifies (28.6.4) as follows.

The older, pre-execution-policy, overload of std::transform, uses input iterators and output iterators.

template<class InputIterator, class OutputIterator, class UnaryOperation>
OutputIterator transform(InputIterator first, InputIterator last,
    OutputIterator result, UnaryOperation op);

the newer overload, using execution policies, uses forward iterators:

template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class UnaryOperation>
ForwardIterator2 transform(ExecutionPolicy&& exec,
    ForwardIterator1 first, ForwardIterator1 last,
    ForwardIterator2 result, UnaryOperation op);

So it seems like you can't use back_insert_iterator with the new overload, as it doesn't meet the forward iterators concept requirements.

does std::par_vec mean that the output range is required to be pre-allocated?

std::par_vec refers to vectorization in the sense of vectorization vs. parallelization. It specifies that both can be used.

like image 68
Ami Tavory Avatar answered Sep 30 '22 14:09

Ami Tavory