C++ standard parallelism (stdpar)¶
C++17 introduced parallel versions of many standard library algorithms (std::for_each, std::transform, std::reduce, ...), which are selected through an execution policy such as std::execution::par or std::execution::par_unseq. C++20 and C++23 extended the set of algorithms and policies. For more information see the C++ reference on execution policies.
Stdpar with accelerators¶
With stdpar there is no explicit data management and no vendor-specific language extension: the code is plain ISO C++. The compiler is free to execute an algorithm with an execution policy serially, in parallel on the CPU or on a GPU. Offloading to GPUs therefore requires compiler support and a memory model in which host and device can access the same data (unified or managed memory):
- The NVIDIA HPC SDK compiles stdpar algorithms for NVIDIA GPUs with
-stdpar=gpu. - AMD ROCm supports offloading to AMD GPUs.
- The GNU Compiler Collection, the LLVM Compiler and the Intel Compiler execute the algorithms on the CPU only.
Because no pragmas or special syntax are used, the code stays portable and runs unchanged on systems without a GPU.
Supported compilers and hardware¶
Compilers: NVIDIA HPC SDK (nvc++) for GPUs; GNU Compiler Collection, LLVM Compiler and Intel oneAPI for CPUs
Hardware: CPUs, NVIDIA GPUs, AMD GPUs (ROCm)
Code sample¶
The following simple C++ program shows the typical sequence of a stdpar program:
C++ source code: exampleStdpar.cpp
#include <algorithm> // for_each
#include <err.h>
#include <execution> // execution::par_unseq
#include <iostream>
#include <ranges> // views::iota
using std::cout, std::endl;
int main() {
const std::size_t size = 1 << 16;
cout << "* Allocate memory on the host" << endl;
auto a = new (std::nothrow) double[size];
if (a == nullptr) {
errx(1, "malloc a[] failed");
}
cout << "* Pre-process / initialize data on the host" << endl;
cout << " e.g. read data from storage" << endl;
for (std::size_t i = 0; i < size; i++) {
a[i] = 1.;
}
cout << "* Automatically allocate memory on the device" << endl;
cout << "* Automatically copy data from the host to the device" << endl;
cout << "* Compute on the device" << endl;
// Without access to vector index
std::for_each_n(
std::execution::par_unseq, // parallel, unsequenced order
a, size,
// GPU kernel expressed as lambda expression
[](double &a_i) {
a_i++;
});
// With access to vector index
auto range = std::ranges::iota_view<std::size_t, std::size_t>{0, size};
std::for_each(
std::execution::par_unseq, // parallel, unsequenced order
range.begin(), range.end(),
// GPU kernel expressed as lambda expression
[&a](const auto &i) {
a[i]++;
});
cout << "* Automatically transfer data back from the device to the host" << endl;
cout << "* Automatically delete data on the device" << endl;
cout << "* Post-process data on the host" << endl;
cout << " e.g. write data to storage" << endl;
for (std::size_t i = 0; i < size; i++) {
if (a[i] != 3.) {
cout << "a[" << i << "] = " << a[i] << endl;
errx(2, "Computation on GPU failed");
}
}
cout << "* Free memory on the host" << endl;
delete[] a;
}
Download C++ source code: exampleStdpar.cpp
Compiling and running stdpar code¶
Excerpt of the submit scripts
The snippets below show only the part of the submit scripts that loads the modules, compiles and runs the example. The #SBATCH header (partition, number of nodes, tasks, CPUs, GPUs, time limit, output file) is omitted. Please use the download links to get the complete submit scripts.
# Load modules
module load nvidia-compilers/26.9-CUDA-13.3.0
# Compile source code
nvc++ -O2 -tp=host -std=c++20 -Minform=inform -Minfo=stdpar -Mneginfo=stdpar -fast -stdpar=gpu -gpu=ccnative exampleStdpar.cpp -o exampleStdpar
./exampleStdpar
Download complete submit script: HoreKa Jade, HoreKa Teal, HoreKa Ruby
The submit scripts for HoreKa Jade (gpu-b200), HoreKa Teal (gpu-h100) and HoreKa Ruby (gpu-h200) differ only in the partition. The examples are compiled inside the job on the GPU node, so that the compilers can optimize for the target architecture (-march=native, --offload-arch=native, -gpu=ccnative).