C++ compiler support for std::execution (parallel STL algorithms)
Asked Answered
P

2

6

I wanted to use the parallel version of std::sort where I can specify an execution policy like std::execution::par_unseq.

I'm currently using clang++-10 and g++ 7.5.0 under Ubuntu Linux, but both don't find the required include file execution, so apparently the parallel algorithm is not yet supported in these compiler versions.

Can someone please tell me which version of clang and gcc would support this feature?

Paraphrast answered 5/6, 2021 at 11:3 Comment(3)
libstdc++ 9, not in libc++ yet: en.cppreference.com/w/cpp/compiler_supportSisera
@AlanBirtles: Thanks a lot. Is the version of libstdc++ bound to a version of g++ or a version of Ubuntu? The relationship is not clear to me. It seems the include files of STL are in my case in '/usr/include/c++/7/' (and also in '/usr/include/boost/'), so apparently I have version 7 (is that used by both 'g++' and 'clang++'?).Paraphrast
it'd normally be the same version as g++ but I'm not sure it has to beSisera
G
4

C++17 execution policies are supported by GCC 10 and Clang 11.

Here is a demo example https://gcc.godbolt.org/z/xahs5x1Kx

#include <execution>

int main()
{
    int a[] = {2,1};
    std::sort(std::execution::par_unseq, std::begin(a), std::end(a) );
    return a[0];
}
Guthrey answered 30/7, 2021 at 18:23 Comment(4)
Thanks a lot. I can confirm that it is supported by g++-11.1. In our case it was also necessary to install a new version of the Intel Thread Building Blocks library (TBB-2019), see also https://mcmap.net/q/16937/-are-c-17-parallel-algorithms-implemented-already.Paraphrast
Even the current Clang 15 does not support C++17 execution policies. You can try that on the linked Godbolt Compiler website.Cashbook
@mcserep, thanks, indeed the standard library -stdlib=libc++ of Clang misses that support. But if Clang uses GNU standard library -stdlib=libstdc++ then execution policies are supported as the demo shows.Guthrey
is it supported for android NDK ?Anjaanjali
T
3

poolSTL is a single-header implementation of some parallel C++17 algorithms, including sort:

#include <poolstl/poolstl.hpp>

std::sort(poolstl::par, vec.begin(), vec.end());

It works on GCC 7 and versions of Clang missing native support.

There's also pluggable_sort to parallelize other fast sequential sorts, like pdqsort:

poolstl::pluggable_sort(poolstl::par, vec.begin(), vec.end(), pdqsort);
Terrel answered 28/11, 2023 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.