std::execution::parallel_scheduler_replacement::bulk_item_receiver_proxy
| Defined in header <execution>
|
||
struct bulk_item_receiver_proxy : receiver_proxy
{
virtual void execute( std::size_t, std::size_t ) noexcept = 0;
};
|
(since C++26) | |
bulk_item_receiver_proxy stands for the receiver of a bulk operation that parallel_scheduler hands over to a parallel_scheduler_backend. When the sender before bulk_chunked or bulk_unchunked completes with values, the scheduler calls schedule_bulk_chunked(n, r, s) or schedule_bulk_unchunked(n, r, s) on its backend, where r is a bulk_item_receiver_proxy. The backend runs the iterations of the algorithm by calling execute on r, and then completes the operation with one of the member functions inherited from receiver_proxy. If it calls set_value, the receiver receives the values that the previous sender sent, as with any bulk algorithm.
As with receiver_proxy, the objects are created by parallel_scheduler, not by the backend.
Member functions
parallel_scheduler_replacement::bulk_item_receiver_proxy::execute
virtual void execute( std::size_t b, std::size_t e ) noexcept = 0;
|
(since C++26) | |
Runs the iterations [b, e) of the bulk algorithm. Let f be the function given to the algorithm, shape its shape, and args... lvalues that refer to the values sent by the previous sender.
- For
bulk_chunked, callsf(b, e, args...). - For
bulk_unchunked, the backend callsexecute(i, i + 1), and it callsf(i, args...).
This applies if the execution policy of the algorithm is execution::par or execution::par_unseq. For the other standard execution policies, the backend receives the shape 1, and execute(0, 1) runs the whole algorithm on one execution agent: it calls f(0, shape, args...) for bulk_chunked, and f(i, args...) for each i in [0, shape), one after another, for bulk_unchunked.
The backend chooses the subranges, under the rules described on the parallel_scheduler_backend page: each index is in at most one of them, every index is in one if the operation completes with set_value, and all calls to execute are made on execution agents of the execution resource and happen before the completion function is called. Calls can run at the same time on different execution agents.
Notes
- Since all calls to
executemust happen before the completion function is called, a backend that runs the subranges on several threads must wait until all of them have finished before callingset_value. executeisnoexcept, so an exception thrown byfdoes not reach the backend. The standard specifiesexecuteas equivalent to callingf, so such an exception calls std::terminate. stdexec instead keeps the first exception, and completes the operation withset_errorwhen the backend callsset_value.bulkis replaced withbulk_chunked, so it reaches the backend throughschedule_bulk_chunked.- The member function
executewas calledstartin early revisions of the proposal, and the namespaceparallel_scheduler_replacementwas calledsystem_context_replaceabilityuntil it was renamed by P4154R0, before C++26 was published. Older implementations and articles may show the earlier names.
Example
A backend for a platform without threads divides bulk work into chunks of up to 4 iterations. With execution::par, it receives the shape of the algorithm and calls execute for each chunk, and the vector filled by the function is sent on to sync_wait. With execution::seq, it receives the shape 1, and the function is called once for the whole range.
The execution control library components may not be available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec (one of the reference implementations).
#include <algorithm>
#include <cstddef>
#include <execution>
#include <memory>
#include <print>
#include <span>
#include <vector>
namespace ex = std::execution;
namespace psr = std::execution::parallel_scheduler_replacement;
using bulk_proxy = psr::bulk_item_receiver_proxy;
using storage = std::span<std::byte>;
// A backend for a platform without threads: it runs all work at once, on the
// thread that starts it, and divides bulk work into chunks of up to 4
struct chunking_backend : psr::parallel_scheduler_backend
{
void schedule(psr::receiver_proxy& r, storage) noexcept override
{
r.set_value();
}
void schedule_bulk_chunked(std::size_t n, bulk_proxy& r,
storage) noexcept override
{
std::println("schedule_bulk_chunked({})", n);
for (std::size_t b = 0; b < n; b += 4)
r.execute(b, std::min(b + 4, n));
r.set_value();
}
void schedule_bulk_unchunked(std::size_t n, bulk_proxy& r,
storage) noexcept override
{
for (std::size_t i = 0; i != n; ++i)
r.execute(i, i + 1);
r.set_value();
}
};
std::shared_ptr<psr::parallel_scheduler_backend>
psr::query_parallel_scheduler_backend()
{
static auto backend = std::make_shared<chunking_backend>();
return backend;
}
int main()
{
auto squares = [](const auto& policy)
{
return ex::schedule(ex::get_parallel_scheduler())
| ex::then([] { return std::vector<int>(10); })
| ex::bulk_chunked(policy, 10, [](int b, int e, auto& v)
{
std::println("Chunk [{}, {})", b, e);
for (int i = b; i != e; ++i)
v[i] = i * i;
});
};
auto [v] = std::this_thread::sync_wait(squares(ex::par)).value();
std::println("{}", v);
std::this_thread::sync_wait(squares(ex::seq));
}
Output:
schedule_bulk_chunked(10)
Chunk [0, 4)
Chunk [4, 8)
Chunk [8, 10)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
schedule_bulk_chunked(1)
Chunk [0, 10)
See also
interface through which a parallel_scheduler_backend completes an operation and queries its receiver (class) | |
serves as the abstraction bridge between the user-facing parallel_scheduler and the backend execution resource (such as a OS-provided system thread pool) (class) | |
| represents a handle of a shared global execution resource (such as a OS-provided system thread pool) optimized for parallel execution (class) | |
(C++26)(C++26)(C++26) |
runs a function for every index in a range after a sender completes, so the work can be spread over a scheduler's execution resources (customization point object) |