Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can std::transform or std::generate without ExecutionPolicy be parallel?

In C++17 parallel std algorithms were introduced (overloads with ExecutionPolicy arguments), where strict rules of execution order, interleaving and paralelization were defined, for example ([algorithm.parallel.exec/3]):

The invocations of element access functions in parallel algorithms invoked with an execution policy object of type execution::sequenced_policy all occur in the calling thread of execution. [ Note: The invocations are not interleaved; see 4.6. — end note ]

(same thing in current draft)

The problem is that I can't find any such requirement for old, non-parallel overloads of these algorithms.

Question: Can this mean that library implementers can, since C++11 when thread of execution term was introduced, implement std::transform and std::generate using SIMD/multithreading/other(?)? Is there a reason for that?

like image 995
Michał Łoś Avatar asked Jan 08 '18 15:01

Michał Łoś


1 Answers

[res.on.data.races]/8 Unless otherwise specified, C++ standard library functions shall perform all operations solely within the current thread if those operations have effects that are visible (4.7) to users.

This precludes any kind of behind-the-scenes multithreading that touches any user-defined entities.

I suppose, in principle, something like std::sort working on a vector<int> can prove that no user-defined class is involved, and send work to multiple threads. That's rather far-fetched, it's difficult to imagine any implementation doing this in practice.

like image 148
Igor Tandetnik Avatar answered Oct 08 '22 23:10

Igor Tandetnik