Skip to content

<blake3pp/parallel_io.hpp>

File hashing driven by a scheduler: the intersection of io.hpp, which windows a file at storage speed, and parallel.hpp, which hashes subtrees across threads. It is its own header because that intersection is the only part of the file API needing an execution provider. io.hpp on its own compiles against the standard library alone, and parallel.hpp on its own knows nothing about files.

Every full window is a power-of-2, subtree-aligned run of chunks, so its chaining values drop into the hasher through the same push_subtree_cv seam the in-memory parallel engine uses. The final window (which may be partial and contains the message end) goes through hasher::update to keep ROOT finalization correct.

Same shape as io.hpp: update_file() is the primitive, hash_file() the one-shot convenience, each with a throwing and a std::error_code form. update_stream() feeds a hasher from a pipe, a socket or standard input through the same pipeline.

stream_io_options

struct blake3pp::stream_io_options;

What update_stream() takes.

Data members

window_bytes

std::size_t window_bytes = detail::default_window_bytes;

Bytes per window, 8 MiB by default; rounded down to a power-of-2 multiple of chunk_size, minimum 64 KiB.

windows

unsigned windows = 0;

Window objects the run allocates: one being filled, the rest on the pool or waiting to reduce. Zero takes the default of four; any other value is clamped to [2, 256].

A stream fills one window at a time, so it has no device queue for a depth to set; blake3ppsum's --qd sets this field for a stream.

stream_buffer_bytes

std::size_t stream_buffer_bytes = 1u <<20;

The stream buffer to ask the OS for, in bytes; zero leaves it alone. Linux only (F_SETPIPE_SZ, which only a pipe takes): the request is clamped to INT_MAX and to the system's ceiling, /proc/sys/fs/pipe-max-size for an unprivileged process. Other platforms ignore it. The 1 MiB default keeps an 8 MiB window from being 128 hand-offs through Linux's default 64 KiB pipe. The buffer in force comes back in stream_report::stream_buffer_bytes.

trace

trace_buffer * trace = nullptr;

Where to record per-window timing, or null for none; the same buffer file_io_options::trace takes.

stream_report

struct blake3pp::stream_report;

What one update_stream() run did.

Data members

bytes

std::uint64_t bytes = 0;

Bytes the stream delivered.

windows

std::uint64_t windows = 0;

Windows the stream filled, the last one included.

short_flushes

std::uint64_t short_flushes = 0;

Of those, the ones that ended early because the producer ran dry before filling them. Zero is a producer that kept up with the grid; anything else says the run was waiting on the producer. Always zero where early_hand_over is false.

window_bytes

std::size_t window_bytes = 0;

The window the run used, after rounding: the figure diagnose_read() takes as its window.

windows_planned

unsigned windows_planned = 0;

Window objects the run allocated: the figure diagnose_read() takes as its planned count.

stream_buffer_bytes

std::size_t stream_buffer_bytes = 0;

The stream buffer in force after the request, in bytes, as the OS reported it (Linux: what F_GETPIPE_SZ returned). Zero where nothing was asked (stream_io_options::stream_buffer_bytes of zero), where the platform has no such knob, and where the handle is not a pipe.

early_hand_over

bool early_hand_over = false;

Whether a window could end when the stream ran dry with at least half a window in hand: true on POSIX, false on Windows. False means every window ended full or at the end of the stream.

watch

struct blake3pp::stream_report::watch_report watch;

read_ask_bound_bytes

std::size_t read_ask_bound_bytes = 0;

The most one read may ask for, as it stood at the end of the run. Windows only, where the bound follows what earlier reads returned. Zero means unbounded: every read asks for the whole window remainder, which is always the case on POSIX and on Windows for a producer that filled every ask.

watch_report

struct blake3pp::stream_report::watch_report;

What the readiness watch before each read did. POSIX platforms other than Darwin and Emscripten watch an empty stream before blocking on it; Windows, Darwin and Emscripten block in the read, and report zero counters.

Data members

probes

std::uint64_t probes = 0;

Readiness probes made, the one that opens every wait included. A handle in non-blocking mode reads before it waits, so a run on one can end without a probe.

caught

std::uint64_t caught = 0;

Waits that a probe ended before the budget ran out.

slept

std::uint64_t slept = 0;

Waits that reached the sleep.

budget

std::chrono::nanoseconds budget {};

The watch budget in force when the run ended.

Functions

write_file

template <class Scheduler, class Fill>
write_report write_file(const std::filesystem::path &path, std::uint64_t size, Fill &&fill, Scheduler &&sched, const file_write_options &opts={});

Generates a file of exactly size bytes, filling each window over a scheduler and writing it through the platform's fastest path. fill is called as fill(std::span<std::byte> out, std::uint64_t offset) on the scheduler's agents, once per window, and must fill every byte of out. Windows are filled and written concurrently and in no particular order, so a generator must depend on its offset and nothing else.

fill may throw. The exception stops the run and comes back out of this call, and the file is left as far as it got, as after an I/O failure. The std::error_code overload lets it through as it was thrown.

On success the file is exactly size bytes. On failure the file is left as far as it got: what lies past the last landed window is unspecified, and it is NOT truncated, because a length invented for bytes that were never produced would hide the failure.

Parameter
Scheduler Any std::execution-style scheduler.
path The file to create or truncate.
size How many bytes to generate.
fill The generator, called per window on the pool.
sched Where the fills run.
opts The geometry.

Returns The bytes written (size) and the rung the file was written through, as write_report says.

write_file

template <class Scheduler, class Fill>
write_report write_file(const std::filesystem::path &path, std::uint64_t size, Fill &&fill, Scheduler &&sched, std::error_code &ec, const file_write_options &opts={});

write_file() over a scheduler, reporting failure through ec instead of throwing. An I/O failure arrives as its own error code and an allocation failure as errc::not_enough_memory. What the generator throws propagates as thrown, whatever its type; so does what the execution provider raises, except a std::system_error, std::bad_alloc or std::logic_error of its own, which is mapped like the library's.

Parameter
path The file to create or truncate.
size How many bytes to generate.
fill The generator, called per window on the pool.
sched Where the fills run.
ec Cleared on success; the failure otherwise.
opts The geometry.

Returns The report, or a default one when ec is set.

update_file

template <stack_budget Budget Budget default_stack_budget, class Scheduler>
void update_file(hasher &h, const std::filesystem::path &path, Scheduler &&sched, const file_io_options &opts={});

Streams a file into a hasher with every complete window fanned out over a scheduler; the final window is absorbed by h itself. Same contract as io.hpp's update_file(): h keeps its mode, stays open for more input and for every finalize form, and files hash in sequence. A hasher already part-way through a message is brought back onto a window boundary by one short first window, whose head goes through h.update() and whose remainder fans out like any other; every window after it is offloaded whole. The digest is the same wherever the hasher started, and only how much of the first window is parallel varies.

Parameter
Budget What a window's part table may take; see stack_budget.
Scheduler Any std::execution-style scheduler.
h The hasher to stream into.
path The file to read.
sched Where the subtree reductions run.
opts The pipeline knobs.

update_file

template <stack_budget Budget Budget default_stack_budget, class Scheduler>
void update_file(hasher &h, const std::filesystem::path &path, Scheduler &&sched, std::error_code &ec, const file_io_options &opts={});

Streams a file into a hasher over a scheduler, reporting failure through ec instead of throwing.

Parameter
Budget What a window's part table may take; see stack_budget.
Scheduler Any std::execution-style scheduler.
h The hasher to stream into.
path The file to read.
sched Where the subtree reductions run.
ec Cleared on success; the error otherwise.
opts The pipeline knobs.

hash_file

template <stack_budget Budget Budget default_stack_budget, class Scheduler>
digest hash_file(const std::filesystem::path &path, Scheduler &&sched, const hash_file_options &opts={});

One-shot digest of a file over the full pipeline: a hasher shaped by opts (SIMD variant, and plain, keyed or key-derivation mode), the file streamed through it multi-core, finalized.

auto d = blake3pp::hash_file(path, pool.get_scheduler(),
                             {.window_bytes = 16 * 1024 * 1024,
                              .queue_depth = 8});

Parameter
Budget What a window's part table may take; see stack_budget.
Scheduler Any std::execution-style scheduler.
path The file to hash.
sched Where the subtree reductions run.
opts The hasher's variant and mode, and the pipeline knobs.

hash_file

template <stack_budget Budget Budget default_stack_budget, class Scheduler>
digest hash_file(const std::filesystem::path &path, Scheduler &&sched, std::error_code &ec, const hash_file_options &opts={});

One-shot digest of a file over the full pipeline, reporting failure through ec instead of throwing.

Parameter
Budget What a window's part table may take; see stack_budget.
Scheduler Any std::execution-style scheduler.
path The file to hash.
sched Where the subtree reductions run.
ec Cleared on success; the error otherwise.
opts The hasher's variant and mode, and the pipeline knobs.

Returns The digest, or an all-zero digest when ec is set.

update_stream

template <stack_budget Budget Budget default_stack_budget, class Scheduler>
stream_report update_stream(hasher &h, stream_handle handle, Scheduler &&sched, const stream_io_options &opts={});

Streams an unseekable source -- a pipe, a socket, standard input -- into a hasher with every window fanned out over a scheduler. Same contract as update_file() in every respect the hasher can see: h keeps its mode, stays open for more input and for every finalize form, and the digest is the one the same bytes produce through any other path. The geometry differs: a stream's length is known only when it ends, so a window is however much of it arrived.

The calling thread reads one window while the pool hashes the ones before it. Every read blocks until the producer has written something. A window ends when it is full or when the stream ends, and on POSIX also when the producer runs dry with at least half a window in hand, so the pool is fed while the producer is slow (stream_report::early_hand_over). On Linux the pipe buffer is widened first (stream_io_options::stream_buffer_bytes), and on POSIX platforms other than Darwin and Emscripten an empty stream is watched for a few microseconds before the read blocks (stream_report::watch). The handle is not closed, and its blocking mode is left as found.

Parameter
Budget What a window's part table may take; see stack_budget.
Scheduler Any std::execution-style scheduler.
h The hasher to stream into.
handle The stream to read; standard_input() for stdin.
sched Where the subtree reductions run.
opts The geometry.

Returns What the run did; see stream_report.

update_stream

template <stack_budget Budget Budget default_stack_budget, class Scheduler>
stream_report update_stream(hasher &h, stream_handle handle, Scheduler &&sched, std::error_code &ec, const stream_io_options &opts={});

update_stream() reporting failure through ec instead of throwing. A read failure arrives as its own error code, an allocation failure as errc::not_enough_memory, and a hasher off the window grid as errc::invalid_argument. What the execution provider raises propagates as thrown, except a std::system_error, std::bad_alloc or std::logic_error of its own, which is mapped like the library's.

Parameter
h The hasher to stream into.
handle The stream to read.
sched Where the subtree reductions run.
ec Cleared on success; the error otherwise.
opts The geometry.

Returns What the run did, or a default report when ec is set.

update_file

template <stack_budget Budget Budget default_stack_budget, detail::foreign_path P P, class Scheduler>
void update_file(hasher &h, const P &path, Scheduler &&sched, const file_io_options &opts={});

update_file() over a scheduler for a foreign path type (see io.hpp).

Parameter
h The hasher to stream into.
path The file to read.
sched Where the subtree reductions run.
opts The pipeline knobs.

update_file

template <stack_budget Budget Budget default_stack_budget, detail::foreign_path P P, class Scheduler>
void update_file(hasher &h, const P &path, Scheduler &&sched, std::error_code &ec, const file_io_options &opts={});

update_file() over a scheduler for a foreign path type, reporting through ec.

Parameter
h The hasher to stream into.
path The file to read.
sched Where the subtree reductions run.
ec Cleared on success; the error otherwise.
opts The pipeline knobs.

hash_file

template <stack_budget Budget Budget default_stack_budget, detail::foreign_path P P, class Scheduler>
digest hash_file(const P &path, Scheduler &&sched, const hash_file_options &opts={});

hash_file() over a scheduler for a foreign path type.

Parameter
path The file to hash.
sched Where the subtree reductions run.
opts The hasher's variant and mode, and the pipeline knobs.

hash_file

template <stack_budget Budget Budget default_stack_budget, detail::foreign_path P P, class Scheduler>
digest hash_file(const P &path, Scheduler &&sched, std::error_code &ec, const hash_file_options &opts={});

hash_file() over a scheduler for a foreign path type, reporting through ec.

Parameter
path The file to hash.
sched Where the subtree reductions run.
ec Cleared on success; the error otherwise.
opts The hasher's variant and mode, and the pipeline knobs.

Returns The digest, or an all-zero digest when ec is set.

write_file

template <detail::foreign_path P P, class Scheduler, class Fill>
write_report write_file(const P &path, std::uint64_t size, Fill &&fill, Scheduler &&sched, const file_write_options &opts={});

write_file() over a scheduler for a foreign path type.

Parameter
path The file to create or truncate.
size How many bytes to generate.
fill The generator, called per window on the pool.
sched Where the fills run.
opts The geometry.

write_file

template <detail::foreign_path P P, class Scheduler, class Fill>
write_report write_file(const P &path, std::uint64_t size, Fill &&fill, Scheduler &&sched, std::error_code &ec, const file_write_options &opts={});

write_file() over a scheduler for a foreign path type, reporting through ec.

Parameter
path The file to create or truncate.
size How many bytes to generate.
fill The generator, called per window on the pool.
sched Where the fills run.
ec Cleared on success; the failure otherwise.
opts The geometry.