C++17 STL Parallel Algorithms - with GCC 9.1 and Intel TBB on Linux and macOS
Posted on May 9, 2019 by Paul
Thanks to Bartlomiej Filipek for the suggestion to try C++17 parallel algorithms with GCC 9 and Intel TBB on Linux.
GCC 9.1 has support for C++17 parallel algorithms by using the Intel TBB library. In this article, I will show you how to build Intel TBB from sources on your machine and how to sort a vector of random numbers in parallel using C++17 std::sort.
At the time of this writing, the latest stable version of Intel TBB is version 2019 Update 8. You can find the latest release of TBB on their GitHubrepo. If a newer version was released, change the version number from the next instructions accordingly.
I have instructions about building GCC 9.1 from source on Linux or macOS on other articles on my website.
Let’s start by downloading Intel TBB:
Next, we are going to duplicate one of the existing build files and change the compiler from generic GCC to GCC 9.1:
At about line 15 you will find these two lines:
change them to:
save and close the file (with nano you need to press CTRL-X and answer Y when asked if you want to save it).
Next, we can build the library:
Once the build is finished, you can install it with the next instructions:
At this point, you should be able to use the C++17 parallel algorithms.
Here is a small C++17 test program, that will fill a vector with 5 millions random doubles, with values from 0 to 1. Next, we sort the vector sequentially and in parallel. Each test is repeated 10 times and the elapsed time, in milliseconds, is printed on the screen:
You can build the code, assuming that you saved the file as t0.cpp, by using one of the next lines:
in the above, t0_opt is optimized for speed, while t0 uses the default compiler settings.
Here is what I see on my machine (macOS MacBook Air 2018):
I’ve seen similar results on a Linux machine. The parallel, optimized version, is 2 - 2.5x faster than the serial version. For the non optimized version the difference between the parallel and the sequential version is slightly larger.