Home  >  Article  >  Backend Development  >  Are C 17 Parallel Algorithms Really Ready for Prime Time?

Are C 17 Parallel Algorithms Really Ready for Prime Time?

Barbara Streisand
Barbara StreisandOriginal
2024-10-27 07:43:31481browse

 Are C  17 Parallel Algorithms Really Ready for Prime Time?

Is C 17 Parallel Algorithms Implementation Ready?

Despite the promise of parallelized versions of popular algorithms in C 17, the availability and implementation of these features can be confusing.

Initially, these features were anticipated as early as 2017, but practical implementations have taken longer to materialize. As of now, the situation is as follows:

GCC 9 with TBB Required

GCC 9, the default compiler in Ubuntu 19.10, supports parallel algorithms through Thread Building Blocks (TBB). TBB must be installed separately, but the process is straightforward.

sudo apt install gcc libtbb-dev
g++ -ggdb3 -O3 -std=c++17 -Wall -Wextra -pedantic -o main.out main.cpp -ltbb
./main.out

Ubuntu 18.04 Installation

Ubuntu 18.04 requires a more involved installation process, as TBB is not available in a compatible version through the default repositories. The commands below provide automated, tested instructions.

# Install GCC 9
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-9 g++-9

# Compile libtbb from source.
sudo apt-get build-dep libtbb-dev
git clone https://github.com/intel/tbb
cd tbb
git checkout 2019_U9
make -j `nproc`
TBB="$(pwd)"
TBB_RELEASE="${TBB}/build/linux_intel64_gcc_cc7.4.0_libc2.27_kernel4.15.0_release"

# Use them to compile our test program.
g++-9 -ggdb3 -O3 -std=c++17 -Wall -Wextra -pedantic -I "${TBB}/include" -L 
"${TBB_RELEASE}" -Wl,-rpath,"${TBB_RELEASE}" -o main.out main.cpp -ltbb
./main.out

Test Program Analysis

To demonstrate the effectiveness of parallel algorithms, a test program comparing the sorting speeds of parallel and serial implementations has been provided. On Ubuntu 19.10, sorting 100 million numbers showed a significant performance improvement for the parallel version.

./main.out 100000000
parallel 2.00886 s
serial 9.37583 s

Error Messages

In case of missing dependencies or an outdated TBB version, the following error messages may be encountered:

  1. Missing TBB:

    fatal error: tbb/blocked_range.h: No such file or directory
  2. Outdated TBB:

    #error Intel(R) Threading Building Blocks 2018 is required; older versions are not supported.

The above is the detailed content of Are C 17 Parallel Algorithms Really Ready for Prime Time?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn