Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the new C++17 execution policies? [duplicate]

I was reading through the std::algorithm documentation at cppreference.com and I noticed a C++17 tag on a lot of cool things I haven't used yet. What got my attention most was the new execution policies. What I gathered from reading about them is that I can make any for_each loop I want multi-threaded just by specifying an execution policy.

For example, I have a program which outputs an image with a 2D graphic on it.

int main(){
    std::for_each(
        img.buffer().begin(),
        img.buffer().end(),
        renderer(
            {-0.5, 0.0, 2.666, 2.0, M_PI / 2.0, 0.0},
            img,
            16
        )
    );
    fout << img;
}

If I want to make this program multi-threaded I should be able to do it with one line.

int main(){
    std::for_each(
        std::execution::par_unseq, // c++17 feature
        img.buffer().begin(),
        img.buffer().end(),
        renderer(
            {-0.5, 0.0, 2.666, 2.0, M_PI / 2.0, 0.0},
            img,
            16
        )
    );
    fout << img;
}

However when I first tried this (with g++ -std=c++17) I got an error telling me that ‘std::execution’ has not been declared, so I tried adding #include <execution> but it says execution: No such file or directory. I've also tried #include<experimental/algorithm> instead of #include<algorithm> but I get the same result. How do I use this new feature?

like image 939
Willy Goat Avatar asked Mar 02 '17 23:03

Willy Goat


People also ask

Does C support parallel execution?

C++17 added support for parallel algorithms to the standard library, to help programs take advantage of parallel execution for improved performance.

How do you achieve parallelism in C++?

Parallel execution: By adding std::execution::par the algorithm is executed in parallel using all available threads. Vectorized parallel execution By adding std::execution::par_unseq the algorithm is executed in parallel but in addition vectorization is used.

What is parallel STL?

Parallel STL is an implementation of the C++ standard library algorithms with support for execution policies, as specified in ISO/IEC 14882:2017 standard, commonly called C++17.


2 Answers

c++17 was not yet finalized. And various compilers have not yet fully implemented it.

-std=c++17 means "give me all of C++17 you have finished", not "be a perfectly valid C++17 compiler".

This feature is not supported by your compiler and/or standard library at this point. Check back in a few weeks/months/years.

There is no generally accepted "please give me C++17 if you fully support it, and otherwise give me an error" flag you can pass to a compiler; partly because it is of little practical use. If the subset of C++17 they provide is sufficient, then you win. And if you need a fully compliant compiler, specific versions of compilers don't know if they have bugs, so you couldn't trust the flag anyhow and would have to test it against compiler versions. And if you already know what versions of the compiler have sufficiently valid C++17, you don't need a flag to tell you.

like image 109
Yakk - Adam Nevraumont Avatar answered Oct 23 '22 06:10

Yakk - Adam Nevraumont


As far as I understand from cppreference this feature is defined in document P0024R2 and it is not yet supported in any compiler.

like image 40
Vladimir Nechaev Avatar answered Oct 23 '22 06:10

Vladimir Nechaev