std::execution::bulk, std::execution::bulk_chunked, std::execution::bulk_unchunked
| Defined in header <execution>
|
||
struct bulk_t { /* unspecified */ };
struct bulk_chunked_t { /* unspecified */ };
struct bulk_unchunked_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr bulk_t bulk{};
inline constexpr bulk_chunked_t bulk_chunked{};
inline constexpr bulk_unchunked_t bulk_unchunked{};
|
(2) | (since C++26) (customization point object) |
| Call signatures |
||
execution::sender auto bulk( execution::sender auto sndr,
auto&& policy,
std::integral auto shape,
/*callable*/ auto f );
|
(3) | (since C++26) |
execution::sender auto bulk_chunked( execution::sender auto sndr,
auto&& policy,
std::integral auto shape,
/*callable*/ auto f );
|
(4) | (since C++26) |
execution::sender auto bulk_unchunked( execution::sender auto sndr,
auto&& policy,
std::integral auto shape,
/*callable*/ auto f );
|
(5) | (since C++26) |
Runs a function once for every index of an index space [0, shape), after sndr has completed, so that the work can be spread over the execution resources of a scheduler. The returned sender completes when all calls have completed (or one of them has failed), and sends on the values that sndr sent.
These are the sender counterparts of a parallel loop. Because the loop body is a plain function over an index, it is the sender algorithm to use for data-parallel work: processing the elements of a container, filling in tiles of an image, or one step of a parallel scan. The three algorithms differ in how the indices are handed to the function:
bulk: calls f(i, values...) for every index i. The calls may be spread over any number of execution agents, and several calls may run on the same one.bulk_chunked: calls f(begin, end, values...) for ranges of indices. Every index is in exactly one range, and each range is non-empty. Processing a range at a time lets the function do per-range setup and publish its result once per range, and lets the implementation choose the size of the ranges to balance the load.bulk_unchunked: calls f(i, values...) for every index i, each on a distinct execution agent when the scheduler supports it. Use it when calls depend on each other, for example when they synchronize with their neighbors.All three are pipeable sender adaptors: sndr | bulk(policy, shape, f) is equivalent to bulk(sndr, policy, shape, f).
Parameters
| sndr | - | the sender whose values are passed to f after the index (or range)
|
| policy | - | an execution policy (for example std::execution::par) that says how f may be run; it is a promise about f, see below
|
| shape | - | the number of indices; the indices are [0, shape). It must be an integral type, and the indices have the same type
|
| f | - | the function to call. For bulk and bulk_unchunked it is called as f(i, values...), and for bulk_chunked as f(begin, end, values...), where values... are the values sent by sndr, passed as lvalues. It must be copy constructible
|
Return value
A sender that is started by starting sndr, and which, if sndr completes with values, calls f for all indices and then completes with the same values:
bulkandbulk_unchunkedcallf(i, values...)once for everyiin[0,shape).bulk_chunkedcallsf(begin, end, values...)zero or more times, withbegin < end, so that every index in[0,shape)is in exactly one range(begin,end).
Error and stopped completions of sndr are forwarded unchanged, and f is not called for them.
If a call of f throws, the returned sender completes with an error (set_error with a std::exception_ptr) holding that exception. Some of the other calls may already have run. The error can also be a std::bad_alloc if the implementation could not allocate what it needs, or an exception derived from std::runtime_error for other internal failures (for example, an exception that could not be transported from the execution context to the caller).
The returned sender may complete stopped if cancellation is requested, in which case only some of the calls were made; but an implementation may also ignore cancellation. These algorithms do not provide a stopped completion of their own beyond that.
The expressions are ill-formed if sndr is not a execution::sender, if policy is not an execution policy, if shape is not integral, or if f is not copy constructible. It is also diagnosed at compile time if f cannot be called with the arguments described above for some value completion of sndr.
Customization point objects
The name execution::bulk denotes a customization point object, which is a const function object of a literal semiregular class type. The cv-unqualified version of its type is a tag type denoted as execution::bulk_t. See CustomizationPointObject for details.
The execution policy
The policy states how the implementation may call f, in the same way as for the parallel algorithms (for example std::for_each): std::execution::seq means the calls must not overlap, std::execution::par that they may run concurrently on different threads, and std::execution::par_unseq that they may also be interleaved or vectorized on one thread. The usual requirements on element access functions of the parallel algorithms apply to f. Only pass a policy that is true for your function: a function that takes a lock shared between calls, for example, must not be given par_unseq.
The policy is required because the author of the function is the one who knows whether it is safe to run concurrently, and the sender algorithm cannot guess: defaulting to seq would ignore parallel hardware, and defaulting to anything else risks deadlocks or data races.
Details
- Nothing runs when the sender is built.
fis called only after the returned sender has been connected to a receiver and started, and only oncesndrhas completed with values. freceives the values ofsndras lvalues, so it can modify them in place; because several calls may run concurrently, use the usual precautions for shared state.- The calls are made on execution agents of the scheduler that the work is running on. The number of agents is not specified.
- With no customization by the scheduler (for example the default of a run_loop), the calls are made one after another on the thread where
sndrcompleted, so a program written withbulkalways works, but gains no parallelism from schedulers that do not provide it. bulkbehaves likebulk_chunkedwith a function that loops over each range and callsffor each index. It isbulk_chunkedthat a scheduler is expected to customize.
Notes
- Choose
bulkwhen each index is independent and cheap to start. Choosebulk_chunkedwhen starting a call is expensive or when results are best combined per range: it removes one function call and one synchronization per index. For example, summing100000values withbulkdoes100000atomic additions, whilebulk_chunkedlets each call add up its range privately and do one atomic addition. Choosebulk_unchunkedwhen the calls must run concurrently, because they wait for each other. bulk_unchunkedis meant for work whose calls may depend on one another. Sincebulkandbulk_chunkedmay group indices, they cannot promise more than parallel forward progress, which is not enough for calls that wait for each other.bulk_unchunkedis the algorithm for asking for one agent per index.- The proposal that added
bulk_chunkedandbulk_unchunkeddeliberately did not add an algorithm that requires concurrent forward progress (all the agents running at the same time); it left that to future work, andbulk_unchunkedonly recommends that the scheduler use a distinct execution agent for each iteration.
Example
Scaling and adding two vectors in place with bulk, and summing a vector with bulk_chunked, where each call sums its own range and publishes a single result.
The standard header is not available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec, the reference implementation.
#include <atomic>
#include <cstdint>
#include <execution>
#include <numeric>
#include <print>
#include <vector>
namespace ex = std::execution;
int main()
{
// bulk: every index is independent, so it is called once per index.
std::vector<double> x{1, 2, 3, 4};
std::vector<double> y{10, 10, 10, 10};
auto axpy = ex::just(2.0)
| ex::bulk(std::execution::par, x.size(),
[&](std::size_t i, double a) { y[i] = a * x[i] + y[i]; });
std::this_thread::sync_wait(std::move(axpy));
std::println("y = {} {} {} {}", y[0], y[1], y[2], y[3]);
// bulk_chunked: one call per range, one atomic update per call instead of one per element.
std::vector<std::uint32_t> data(100000, 1);
std::atomic<std::uint32_t> sum{0};
auto total = ex::just()
| ex::bulk_chunked(std::execution::par, data.size(),
[&](std::size_t begin, std::size_t end)
{
std::uint32_t partial = std::accumulate(
data.begin() + begin, data.begin() + end, 0u);
sum.fetch_add(partial);
});
std::this_thread::sync_wait(std::move(total));
std::println("sum = {}", sum.load());
}
Output:
y = 12 14 16 18
sum = 100000
See also
(C++26) |
chains a function onto a sender, calling it with the values the sender sends and sending its result on (customization point object) |
(C++26) |
adapts multiple input senders into a sender that completes once all of the input senders have completed (customization point object) |