Skip to content

About

Microbenchmark for different parallel-for / fork-join implementations in C++

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

bench-parallel-for

Microbenchmark for different parallel-for / fork-join implementations in C++.

Each "method" receives a std::span<float> and a kernel void(size_t idx, float& data, size_t n) and applies the kernel to every element. The benchmark runs every registered method on every registered kernel across doubling sizes N = 1, 2, 4, …, max_n, times each run, and reports the median of 5 repetitions per point. One SVG time-chart and one throughput-chart are generated per kernel from the CSV output.

Each kernel has an individual global element cap, and can override that cap on a per-method basis (so slow methods can be skipped at large N without holding up the rest of the run).

Structure

src/
├── helper.hh            — steady-clock timer
├── kernels.hh           — scalar kernels (idx, float&, n)
├── kernel_config.hh     — per-kernel max N + per-method cap overrides
├── main.cc              — benchmark driver, CSV writer
└── methods/
    ├── method.hh               — IParallelFor base (name() + run(span, kernel_fn))
    ├── singlethreaded.hh
    ├── openmp.hh               — openmp_static, openmp_dynamic, openmp_static_chunk, openmp_dynamic_chunk, openmp_guided
    ├── naive_fork_join.hh      — std::thread spawn-per-run
    ├── persistent_pool.hh      — T persistent workers, CV-dispatched
    ├── work_stealing.hh        — per-thread deque, mutex-guarded Chase-Lev–lite
    ├── atomic_counter.hh       — lock-free fetch_add chunk dispatch
    ├── std_par.hh              — std::for_each(par_unseq)
    ├── tbb.hh                  — tbb::parallel_for
    └── windows_threadpool.hh   — Win32 ThreadPoolAPI (Windows only)
create-charts.py         — one chart-pair per kernel (chart_time_{k}.svg / chart_throughput_{k}.svg)
build.py / run.py / run-debug.py  — thin CMake wrappers; build.py auto-wires vcpkg if present
vcpkg.json               — manifest (tbb)
CMakeLists.txt           — requires OpenMP, Threads, TBB

Adding a new method is a matter of dropping a new header into src/methods/ that inherits IParallelFor, and registering a std::make_unique of it in main.cc. Adding a new kernel is a free function void(size_t idx, float& data, size_t n) in src/kernels.hh plus an entry in the kernels vector at the top of main().

Kernels

name description
kernel_sin single std::sin call — modest work, original reference kernel.
kernel_muladd one fused mul-add per element; memory-bandwidth bound at large N.
kernel_heavy fixed 16 iterations of sin/exp — exposes parallel speedup ceiling.
kernel_divergent rng-seeded iteration count in 1..50 — unpredictable per-index work.
kernel_lopsided iteration count 1..1000, quadratic in idx/n — exposes load imbalance with static scheduling.

Methods

name description
singlethreaded Reference baseline, no parallelism.
openmp-static #pragma omp parallel for schedule(static).
openmp-dynamic schedule(dynamic).
openmp-static-{64,1024} schedule(static, chunk) with explicit chunk.
openmp-dynamic-{64,1024} schedule(dynamic, chunk) with explicit chunk.
openmp-guided schedule(guided).
naive-fork-join-{T} Spawn T std::threads per run, join immediately.
persistent-pool-{T} T long-lived workers dispatched via condition variable.
work-stealing-{T} Per-thread deque; owner pops LIFO, thieves steal FIFO.
atomic-counter-{chunk} Single std::atomic<size_t> handing out chunks via fetch_add.
std-par-unseq std::for_each(std::execution::par_unseq, …).
tbb-parallel-for tbb::parallel_for with default auto_partitioner.
windows-threadpool Win32 CreateThreadpoolWork / SubmitThreadpoolWork (Windows only).

Requirements

  • C++23 compiler (MSVC 19.44+, GCC 14+, Clang 18+)
  • CMake ≥ 3.25
  • OpenMP
  • vcpkg as a git submodule at ./vcpkg (for Intel TBB, which also backs some STL parallel algorithms). Setup:
    git submodule add https://github.com/microsoft/vcpkg.git vcpkg
    ./vcpkg/bootstrap-vcpkg.bat   # or ./bootstrap-vcpkg.sh on Unix
    build.py auto-detects this and passes the vcpkg toolchain to CMake.
  • Python 3.11+ with uv for the helper scripts (dependencies are pinned inline via PEP 723)

Usage

uv run build.py         # configure + build (release)
uv run run.py           # build + run + create-charts; writes result.csv
uv run create-charts.py # reads result.csv, writes one chart-pair per kernel
uv run run-debug.py     # debug build + run variant (does not create charts)

TODO — more methods to explore

  • NUMA-aware variant — pin threads per socket, partition the buffer to local memory (mostly relevant for N ≫ L3).
  • Hyper-threading vs. physical-core-only — same as naive fork-join but spawning hardware_concurrency()/2 threads, to see when SMT helps/hurts this kernel.
  • barrier-based SPMD — all workers stay resident, drive work via a shared phase counter and a barrier (model for BSP-style parallelism).
  • GCD — Apple's libdispatch as a macOS-side counterpart to windows-threadpool.

Make runs with timeBeginPeriod(1); and without.

About

Microbenchmark for different parallel-for / fork-join implementations in C++

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages