feature. See also
. The project being documented here (as the example) is the Zig library itself.
blake3.buildMerkleTreeLayerParallel
fn buildMerkleTreeLayerParallel(
input_cvs: [][8]u32,
output_cvs: [][8]u32,
key: [8]u32,
flags: Flags,
io: Io,
) Io.Cancelable!void
File
Code
fn buildMerkleTreeLayerParallel(
input_cvs: [][8]u32,
output_cvs: [][8]u32,
key: [8]u32,
flags: Flags,
io: Io,
) Io.Cancelable!void {
const num_parents = input_cvs.len / 2;
// Tree layers shrink quickly, so only parallelize the first few large layers
if (num_parents <= 1024) {
processParentBatchSIMD(ParentBatchContext{
.input_cvs = input_cvs,
.output_cvs = output_cvs,
.start_idx = 0,
.end_idx = num_parents,
.key = key,
.flags = flags,
});
return;
}
const num_workers = Thread.getCpuCount() catch 1;
const parents_per_worker = (num_parents + num_workers - 1) / num_workers;
var group: Io.Group = .init;
defer group.cancel(io);
for (0..num_workers) |worker_id| {
const start_idx = worker_id * parents_per_worker;
if (start_idx >= num_parents) break;
group.async(io, processParentBatchSIMD, .{ParentBatchContext{
.input_cvs = input_cvs,
.output_cvs = output_cvs,
.start_idx = start_idx,
.end_idx = @min(start_idx + parents_per_worker, num_parents),
.key = key,
.flags = flags,
}});
}
try group.await(io);
}