Skip to content

Multi-core hashing

auto sched = blake3pp::get_parallel_scheduler();
blake3pp::digest d = blake3pp::hash(big_buffer, sched);

Try it on Compiler Explorer

Adding cores means adding a scheduler argument. Nothing else about the call changes, and the digest does not change either.

Why the digest is identical

BLAKE3 hashes a binary Merkle tree. Any power-of-two, position-aligned run of chunks reduces to one chaining value independently of everything around it, so the parallel decomposition is exact rather than approximate. The input is split into equal subtrees, the scheduler's agents take them as they come free, and the chaining values are absorbed in order. The result is the sequential result.

Where the scheduler comes from

get_parallel_scheduler() returns the process-wide scheduler in P2079's shape, which is the same call C++26 application code makes. Anyone who needs a sized or bounded pool builds their provider's pool and passes its scheduler instead:

exec::static_thread_pool pool(8);
auto d = blake3pp::hash(big_buffer, pool.get_scheduler());

Every entry point takes a scheduler the same way. The sequential and multi-core spellings sit side by side:

one-shot incremental file input extended output
sequential hash(data) hasher update_file(h, path) fill(r, out)
multi-core hash(data, sched) parallel_hasher update_file(h, path, sched) fill(r, out, sched)

Stack, and the budget that caps it

Each part's 32-byte chaining value waits on the calling thread's stack. stack_budget is a template argument that caps how much stack that table may take, defaulting to 32 KiB, which is 1024 parts:

auto d = blake3pp::hash<blake3pp::stack_budget{1024}>(buffer, sched);   // 32 parts

A budget that is not a multiple of 32 bytes, or that holds fewer than two parts, does not compile.

The budget covers that table and nothing else. Each part is reduced on the agent that took it, in a fixed staging area that does not grow with the part, so the agent threads need stack of their own. Freestanding and RTOS builds works the numbers through.

Running it

cmake --build build --target blake3pp_example_06_multi_core
./build/examples/blake3pp_example_06_multi_core

It hashes 32 MiB both ways and prints the rate of each, so the output depends on the machine. On Compiler Explorer expect a speedup near 1.2x where a laptop shows 5x, because the sandbox offers two cores. The digests are identical everywhere, which is the part worth checking.

Next

  • 07-which-kernel asks the binary what it carries and the CPU what it can run.

The whole program

// Spreading one hash over every core, and why the answer does not change.

#include <blake3pp/blake3pp.hpp>

#include <chrono>
#include <cstddef>
#include <iostream>
#include <utility>
#include <vector>

int main() {
  // 32 MiB of something to hash. Small enough that this runs inside a
  // sandbox, large enough that spreading it over cores shows.
  std::vector<std::byte> buffer(32u << 20);
  for (std::size_t i = 0; i < buffer.size(); ++i) {
    buffer[i] = static_cast<std::byte>(i);
  }

  const auto time = [&](auto&& fn) {
    const auto start = std::chrono::steady_clock::now();
    const blake3pp::digest d = fn();
    const std::chrono::duration<double> elapsed =
        std::chrono::steady_clock::now() - start;
    const double gibps =
        static_cast<double>(buffer.size()) / elapsed.count() / (1u << 30);
    return std::pair{d, gibps};
  };

  const auto [sequential, seq_rate] =
      time([&] { return blake3pp::hash(buffer); });
  std::cout << "one core\n  " << sequential.to_hex() << "\n  " << seq_rate
            << " GiB/s\n";

  // Adding cores means adding a scheduler. get_parallel_scheduler() is
  // the process-wide one in P2079's shape, which is the same call C++26
  // application code makes.
  auto sched = blake3pp::get_parallel_scheduler();

  const auto [parallel, par_rate] =
      time([&] { return blake3pp::hash(buffer, sched); });
  std::cout << "\nevery core\n  " << parallel.to_hex() << "\n  " << par_rate
            << " GiB/s\n";

  // BLAKE3's tree makes the decomposition exact rather than approximate,
  // so the parallel digest is the sequential digest. Splitting the work
  // is not an approximation of anything.
  std::cout << "\nsame digest: " << std::boolalpha
            << (sequential == parallel) << '\n';
  std::cout << "speedup: " << (par_rate / seq_rate) << " x\n";

  // The incremental counterpart, for input that arrives in pieces.
  blake3pp::parallel_hasher ph{sched};
  for (int i = 0; i < 4; ++i) {
    ph.update(buffer);
  }
  std::cout << "\n128 MiB through parallel_hasher\n  " << ph.finalize().to_hex()
            << '\n';

  // Each part's 32-byte chaining value waits on the calling thread's
  // stack, and stack_budget caps how much stack that table may take. The
  // default is 32 KiB, which is 1024 parts. A smaller budget splits the
  // input into fewer, larger parts, which is what a small thread wants.
  const blake3pp::digest thrifty =
      blake3pp::hash<blake3pp::stack_budget{1024}>(buffer, sched);
  std::cout << "\nwith a 1 KiB part table (32 parts)\n  " << thrifty.to_hex()
            << '\n';
  std::cout << "\nstill the same digest: " << (thrifty == sequential) << '\n';
}