std::execution::parallel_scheduler_replacement::parallel_scheduler_backend
| Defined in header <execution>
|
||
struct parallel_scheduler_backend
{
virtual ~parallel_scheduler_backend() = default;
virtual void schedule( receiver_proxy&, std::span<std::byte> ) noexcept = 0;
virtual void schedule_bulk_chunked( std::size_t, bulk_item_receiver_proxy&,
std::span<std::byte> ) noexcept = 0;
virtual void schedule_bulk_unchunked( std::size_t, bulk_item_receiver_proxy&,
std::span<std::byte> ) noexcept = 0;
};
|
(since C++26) | |
parallel_scheduler_backend is the interface through which parallel_scheduler hands work over to its execution resource. The execution resource provided by the standard library is reached through an object of a class derived from it. A program provides its own execution resource by deriving a class from parallel_scheduler_backend, overriding the three member functions, and returning an object of that class from its replacement of query_parallel_scheduler_backend.
parallel_scheduler calls the member functions when work that runs on it is started, or, for the bulk algorithms, when the previous sender completes. Each call receives a reference r to a receiver proxy, an object that stands for the receiver of the operation (see receiver_proxy and bulk_item_receiver_proxy), and a storage s, a range of bytes that the backend may use as temporary storage for the operation. An override must meet the following requirements:
- Eventually, exactly one of the following is called on
r:r.set_value(), if the work is successful;r.set_error(eptr), whereeptris a std::exception_ptr, if an error occurs;r.set_stopped(), if the work is canceled.
r.set_value()is called on an execution agent of the execution resource of the backend.set_errorandset_stoppedcan be called anywhere, including before the member function returns.
The object referred to by r, the storage s, and the backend object itself are guaranteed to exist until one of the completion functions is called on r, and may be destroyed as soon as that call begins.
Member functions
parallel_scheduler_replacement::parallel_scheduler_backend::schedule
virtual void schedule( receiver_proxy& r, std::span<std::byte> s ) noexcept = 0;
|
(since C++26) | |
Called by parallel_scheduler for the sender returned by schedule. An override arranges for r.set_value() to be called on an execution agent of the execution resource.
parallel_scheduler_replacement::parallel_scheduler_backend::schedule_bulk_chunked
virtual void schedule_bulk_chunked( std::size_t n, bulk_item_receiver_proxy& r,
std::span<std::byte> s ) noexcept = 0;
|
(since C++26) | |
Called by parallel_scheduler for bulk_chunked, and for bulk, which is replaced with bulk_chunked. An override divides the range [0, n) into subranges and calls r.execute(b, e) for each subrange [b, e), where {{{1}}}, b < e, and {{{1}}}:
- no index is in more than one subrange, and if the operation completes with
r.set_value(), every index of[0, n)is in exactly one of them; - all calls to
executehappen before the completion function is called; - all calls to
execute, and the call toset_value, are made on execution agents of the execution resource.
Calls to execute can be made at the same time on different execution agents. If n is 0, execute is not called.
r.execute(b, e) calls the function given to bulk_chunked for the subrange [b, e) if the execution policy is execution::par or execution::par_unseq. For the other standard execution policies, n is 1, and r.execute(0, 1) calls the function once for the whole range of the algorithm.
parallel_scheduler_replacement::parallel_scheduler_backend::schedule_bulk_unchunked
virtual void schedule_bulk_unchunked( std::size_t n, bulk_item_receiver_proxy& r,
std::span<std::byte> s ) noexcept = 0;
|
(since C++26) | |
Called by parallel_scheduler for bulk_unchunked. The requirements are the same as for schedule_bulk_chunked, except that every call to execute is r.execute(i, i + 1) for an index i in [0, n).
r.execute(i, i + 1) calls the function given to bulk_unchunked for the index i if the execution policy is par or par_unseq. For the other standard execution policies, n is 1, and r.execute(0, 1) calls the function for each index of the algorithm, one after another.
Notes
- The member functions are
noexcept. A backend that cannot run the work, for example because it cannot start a thread or allocate memory, reports it by callingr.set_errorwith an std::exception_ptr. - The member functions usually return as soon as they have handed the work over to the execution resource, before the work runs.
- Since the receiver proxy, the storage, and the backend object itself may be destroyed as soon as a completion function is called, a backend must not use any of them after calling it, not even its own data members.
- A backend can obtain the stop token of the receiver with
r.try_query<std::inplace_stop_token>(execution::get_stop_token), and callr.set_stopped()instead of running the work if stop was requested. Whether it does so is up to the backend. - The size and the alignment of the storage
sare unspecified, and it can be empty. A backend that uses it, for example to construct its own record of the operation there instead of allocating memory, must check that it is large enough and suitably aligned. - The namespace
parallel_scheduler_replacementwas calledsystem_context_replaceabilityuntil it was renamed by P4154R0, before C++26 was published. Older implementations and articles may show the earlier name.
Example
A backend that starts new threads for its work, instead of using a thread pool. bulk_chunked divides 1000 iterations into four chunks, which run at the same time on four threads, so the order of the output can vary.
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 <cstddef>
#include <exception>
#include <execution>
#include <memory>
#include <print>
#include <span>
#include <thread>
#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 that starts new threads for all work: simple, but much slower
// than a thread pool
struct thread_backend : psr::parallel_scheduler_backend
{
// Runs f on a new thread, or completes with an error if it cannot start
static void run(psr::receiver_proxy& r, auto f) noexcept
{
try { std::thread(std::move(f)).detach(); }
catch (...) { r.set_error(std::current_exception()); }
}
void schedule(psr::receiver_proxy& r, storage) noexcept override
{
run(r, [&r] { r.set_value(); });
}
// Divides the iterations into chunks that run on up to 4 threads, and
// completes when all of them have finished
void schedule_bulk_chunked(std::size_t n, bulk_proxy& r,
storage) noexcept override
{
run(r, [n, &r]
{
std::size_t k = n < 4 ? n : 4;
std::vector<std::jthread> workers;
for (std::size_t t = 0; t != k; ++t)
workers.emplace_back([=, &r]
{
r.execute(n * t / k, n * (t + 1) / k);
});
workers.clear(); // joins the threads
r.set_value();
});
}
// Runs the iterations one after another, on one thread
void schedule_bulk_unchunked(std::size_t n, bulk_proxy& r,
storage) noexcept override
{
run(r, [n, &r]
{
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<thread_backend>();
return backend;
}
int main()
{
auto work = ex::schedule(ex::get_parallel_scheduler())
| ex::bulk_chunked(ex::par, 1000, [](int begin, int end)
{
std::println("Chunk [{}, {})", begin, end);
});
std::this_thread::sync_wait(std::move(work));
}
Possible output:
Chunk [0, 250)
Chunk [250, 500)
Chunk [500, 750)
Chunk [750, 1000)
See also
returns the backend object that every parallel_scheduler uses; a program can replace it to provide its own backend (function) | |
interface through which a parallel_scheduler_backend completes an operation and queries its receiver (class) | |
used for bulk_chunked and bulk_unchunked customizations that will also receive notifications from implementations of parallel_scheduler_backend corresponding to different iterations (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) |