问题
I was trying to play around with the new parallel library features proposed in the C++17 standard, but I couldn't get it to work. I tried compiling with the up-to-date versions of g++ 8.1.1
and clang++-6.0
and -std=c++17
, but neither seemed to support #include <execution>
, std::execution::par
or anything similar.
When looking at the cppreference for parallel algorithms there is a long list of algorithms, claiming
Technical specification provides parallelized versions of the following 69 algorithms from
algorithm
,numeric
andmemory
: ( ... long list ...)
which sounds like the algorithms are ready 'on paper', but not ready to use yet?
In this SO question from over a year ago the answers claim these features hadn't been implemented yet. But by now I would have expected to see some kind of implementation. Is there anything we can use already?
回答1:
You can refer https://en.cppreference.com/w/cpp/compiler_support to check all C++
feature implementation status. For your case, just search "Standardization of Parallelism TS
", and you will find only MSVC
and Intel C++
compilers support this feature now.
回答2:
Intel has released a Parallel STL library which follows the C++17 standard:
- https://github.com/intel/parallelstl
It is being merged into GCC.
回答3:
GCC 9 will have them
Mentioned at https://gcc.gnu.org/gcc-9/changes.html
Parallel algorithms and (requires Thread Building Blocks 2018 or newer).
When it gets a release tag I will give it a shot. I wonder what that "Thread Building Blocks 2018" is going to require.
回答4:
Gcc does not yet implement the Parallelism TS (see https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2017)
However libstdc++ (with gcc) has an experimental mode for some equivalent parallel algorithms. See https://gcc.gnu.org/onlinedocs/libstdc++/manual/parallel_mode.html
Getting it to work:
Any use of parallel functionality requires additional compiler and runtime support, in particular support for OpenMP. Adding this support is not difficult: just compile your application with the compiler flag -fopenmp. This will link in libgomp, the GNU Offloading and Multi Processing Runtime Library, whose presence is mandatory.
Code example
#include <vector>
#include <parallel/algorithm>
int main()
{
std::vector<int> v(100);
// ...
// Explicitly force a call to parallel sort.
__gnu_parallel::sort(v.begin(), v.end());
return 0;
}
来源:https://stackoverflow.com/questions/51031060/are-c17-parallel-algorithms-implemented-already