class to create an executor More...
#include <taskflow/core/executor.hpp>
Public Member Functions | |
| Executor (size_t N=std::thread::hardware_concurrency(), std::shared_ptr< WorkerInterface > wif=nullptr) | |
constructs the executor with N worker threads | |
| ~Executor () | |
| destructs the executor | |
| tf::Future< void > | run (Taskflow &taskflow) |
| runs a taskflow once | |
| tf::Future< void > | run (Taskflow &&taskflow) |
| runs a moved taskflow once | |
| template<typename C> | |
| tf::Future< void > | run (Taskflow &taskflow, C &&callable) |
| runs a taskflow once and invoke a callback upon completion | |
| template<typename C> | |
| tf::Future< void > | run (Taskflow &&taskflow, C &&callable) |
| runs a moved taskflow once and invoke a callback upon completion | |
| tf::Future< void > | run_n (Taskflow &taskflow, size_t N) |
runs a taskflow for N times | |
| tf::Future< void > | run_n (Taskflow &&taskflow, size_t N) |
runs a moved taskflow for N times | |
| template<typename C> | |
| tf::Future< void > | run_n (Taskflow &taskflow, size_t N, C &&callable) |
runs a taskflow for N times and then invokes a callback | |
| template<typename C> | |
| tf::Future< void > | run_n (Taskflow &&taskflow, size_t N, C &&callable) |
runs a moved taskflow for N times and then invokes a callback | |
| template<typename P> | |
| tf::Future< void > | run_until (Taskflow &taskflow, P &&pred) |
| runs a taskflow multiple times until the predicate becomes true | |
| template<typename P> | |
| tf::Future< void > | run_until (Taskflow &&taskflow, P &&pred) |
| runs a moved taskflow and keeps running it until the predicate becomes true | |
| template<typename P, typename C> | |
| tf::Future< void > | run_until (Taskflow &taskflow, P &&pred, C &&callable) |
| runs a taskflow multiple times until the predicate becomes true and then invokes the callback | |
| template<typename P, typename C> | |
| tf::Future< void > | run_until (Taskflow &&taskflow, P &&pred, C &&callable) |
| runs a moved taskflow and keeps running it until the predicate becomes true and then invokes the callback | |
| template<typename T> | |
| void | corun (T &target) |
| runs a target graph and waits until it completes using an internal worker of this executor | |
| template<typename P> | |
| void | corun_until (P &&predicate) |
keeps running the work-stealing loop until the predicate returns true | |
| void | wait_for_all () |
| waits for all tasks to complete | |
| size_t | num_workers () const noexcept |
| queries the number of worker threads | |
| size_t | num_waiters () const noexcept |
| queries the number of workers that are in the waiting loop | |
| size_t | num_queues () const noexcept |
| queries the number of work-stealing queues used by the executor | |
| size_t | num_topologies () const |
| queries the number of running topologies at the time of this call | |
| Worker * | this_worker () |
queries pointer to the calling worker if it belongs to this executor, otherwise returns nullptr | |
| int | this_worker_id () const |
| queries the id of the caller thread within this executor | |
| template<typename Observer, typename... ArgsT> | |
| std::shared_ptr< Observer > | make_observer (ArgsT &&... args) |
| constructs an observer to inspect the activities of worker threads | |
| template<typename Observer> | |
| void | remove_observer (std::shared_ptr< Observer > observer) |
| removes an observer from the executor | |
| size_t | num_observers () const noexcept |
| queries the number of observers | |
| template<typename P, typename F> | |
| auto | async (P &¶ms, F &&func) |
| creates a parameterized asynchronous task to run the given function | |
| template<typename F> | |
| auto | async (F &&func) |
| runs a given function asynchronously | |
| template<typename P, typename F> | |
| void | silent_async (P &¶ms, F &&func) |
| similar to tf::Executor::async but does not return a future object | |
| template<typename F> | |
| void | silent_async (F &&func) |
| similar to tf::Executor::async but does not return a future object | |
| template<typename F, typename... Tasks> requires (std::same_as<std::decay_t<Tasks>, AsyncTask> && ...) | |
| tf::AsyncTask | silent_dependent_async (F &&func, Tasks &&... tasks) |
| runs the given function asynchronously when the given predecessors finish | |
| template<TaskParameters P, typename F, typename... Tasks> requires (std::same_as<std::decay_t<Tasks>, AsyncTask> && ...) | |
| tf::AsyncTask | silent_dependent_async (P &¶ms, F &&func, Tasks &&... tasks) |
| runs the given function asynchronously when the given predecessors finish | |
| template<typename F, typename I> requires (!std::same_as<std::decay_t<I>, AsyncTask>) | |
| tf::AsyncTask | silent_dependent_async (F &&func, I first, I last) |
| runs the given function asynchronously when the given range of predecessors finish | |
| template<TaskParameters P, typename F, typename I> requires (!std::same_as<std::decay_t<I>, AsyncTask>) | |
| tf::AsyncTask | silent_dependent_async (P &¶ms, F &&func, I first, I last) |
| runs the given function asynchronously when the given range of predecessors finish | |
| template<typename F, typename... Tasks> requires (std::same_as<std::decay_t<Tasks>, AsyncTask> && ...) | |
| auto | dependent_async (F &&func, Tasks &&... tasks) |
| runs the given function asynchronously when the given predecessors finish | |
| template<TaskParameters P, typename F, typename... Tasks> requires (std::same_as<std::decay_t<Tasks>, AsyncTask> && ...) | |
| auto | dependent_async (P &¶ms, F &&func, Tasks &&... tasks) |
| runs the given function asynchronously when the given predecessors finish | |
| template<typename F, typename I> requires (!std::same_as<std::decay_t<I>, AsyncTask>) | |
| auto | dependent_async (F &&func, I first, I last) |
| runs the given function asynchronously when the given range of predecessors finish | |
| template<TaskParameters P, typename F, typename I> requires (!std::same_as<std::decay_t<I>, AsyncTask>) | |
| auto | dependent_async (P &¶ms, F &&func, I first, I last) |
| runs the given function asynchronously when the given range of predecessors finish | |
| TaskGroup | task_group () |
| creates a task group that executes a collection of asynchronous tasks | |
Friends | |
| class | FlowBuilder |
| class | Subflow |
| class | Runtime |
| class | NonpreemptiveRuntime |
| class | Algorithm |
| class | TaskGroup |
class to create an executor
An tf::Executor manages a set of worker threads to run tasks using an efficient work-stealing scheduling algorithm.
Most executor methods are thread-safe. For example, you can submit multiple taskflows to an executor concurrently from different threads, while other threads simultaneously create asynchronous tasks.
|
explicit |
constructs the executor with N worker threads
| N | number of workers (default std::thread::hardware_concurrency) |
| wif | interface class instance to configure workers' behaviors |
The constructor spawns N worker threads to run tasks in a work-stealing loop. The number of workers must be greater than zero or an exception will be thrown. By default, the number of worker threads is equal to the maximum hardware concurrency returned by std::thread::hardware_concurrency.
Users can alter the worker behavior, such as changing thread affinity, via deriving an instance from tf::WorkerInterface.
| tf::Executor::~Executor | ( | ) |
destructs the executor
The destructor calls Executor::wait_for_all to wait for all submitted taskflows to complete and then notifies all worker threads to stop and join these threads.
| auto tf::Executor::async | ( | F && | func | ) |
runs a given function asynchronously
| F | callable type |
| func | callable object |
The method creates an asynchronous task to run the given function and return a std::future object that eventually will hold the result of the return value.
This member function is thread-safe.
| auto tf::Executor::async | ( | P && | params, |
| F && | func ) |
creates a parameterized asynchronous task to run the given function
| P | task parameter type satisfying tf::TaskParameters |
| F | callable type |
| params | task parameters |
| func | callable object |
The method creates a parameterized asynchronous task to run the given function and return a std::future object that eventually will hold the result of the execution.
This member function is thread-safe.
| void tf::Executor::corun | ( | T & | target | ) |
runs a target graph and waits until it completes using an internal worker of this executor
| T | target type which is either convertible to tf::Graph or has tf::Graph& T::graph() defined |
| target | the target task graph object |
The method coruns a target graph cooperatively with other workers in the same executor and block until the execution completes. Under cooperative execution, a worker is not preempted. Instead, it continues participating in the work-stealing loop, executing available tasks alongside other workers.
The method is thread-safe as long as the target is not concurrently ran by two or more threads.
| void tf::Executor::corun_until | ( | P && | predicate | ) |
keeps running the work-stealing loop until the predicate returns true
| P | predicate type |
| predicate | a boolean predicate to indicate when to stop the loop |
The method keeps the caller worker running in the work-stealing loop until the stop predicate becomes true.
The method keeps the calling worker running available tasks cooperatively with other workers in the same executor and block until the predicate return true. Under cooperative execution, a worker is not preempted. Instead, it continues participating in the work-stealing loop, executing available tasks alongside other workers.
| auto tf::Executor::dependent_async | ( | F && | func, |
| I | first, | ||
| I | last ) |
runs the given function asynchronously when the given range of predecessors finish
| F | callable type |
| I | iterator type |
| func | callable object |
| first | iterator to the beginning (inclusive) |
| last | iterator to the end (exclusive) |
The example below creates three asynchronous tasks, A, B, and C, in which task C runs after task A and task B. Task C returns a pair of its tf::AsyncTask handle and a std::future<int> that eventually will hold the result of the execution.
You can mix the use of tf::AsyncTask handles returned by tf::Executor::dependent_async and tf::Executor::silent_dependent_async when specifying task dependencies.
This member function is thread-safe.
| auto tf::Executor::dependent_async | ( | F && | func, |
| Tasks &&... | tasks ) |
runs the given function asynchronously when the given predecessors finish
| F | callable type |
| Tasks | task types convertible to tf::AsyncTask |
| func | callable object |
| tasks | asynchronous tasks on which this execution depends |
The example below creates three asynchronous tasks, A, B, and C, in which task C runs after task A and task B. Task C returns a pair of its tf::AsyncTask handle and a std::future<int> that eventually will hold the result of the execution.
You can mix the use of tf::AsyncTask handles returned by tf::Executor::dependent_async and tf::Executor::silent_dependent_async when specifying task dependencies.
This member function is thread-safe.
| auto tf::Executor::dependent_async | ( | P && | params, |
| F && | func, | ||
| I | first, | ||
| I | last ) |
runs the given function asynchronously when the given range of predecessors finish
| P | task parameters type satisfying tf::TaskParameters |
| F | callable type |
| I | iterator type |
| params | task parameters |
| func | callable object |
| first | iterator to the beginning (inclusive) |
| last | iterator to the end (exclusive) |
The example below creates three named asynchronous tasks, A, B, and C, in which task C runs after task A and task B. Task C returns a pair of its tf::AsyncTask handle and a std::future<int> that eventually will hold the result of the execution. Assigned task names will appear in the observers of the executor.
You can mix the use of tf::AsyncTask handles returned by tf::Executor::dependent_async and tf::Executor::silent_dependent_async when specifying task dependencies.
This member function is thread-safe.
| auto tf::Executor::dependent_async | ( | P && | params, |
| F && | func, | ||
| Tasks &&... | tasks ) |
runs the given function asynchronously when the given predecessors finish
| P | task parameters type satisfying tf::TaskParameters |
| F | callable type |
| Tasks | task types convertible to tf::AsyncTask |
| params | task parameters |
| func | callable object |
| tasks | asynchronous tasks on which this execution depends |
The example below creates three named asynchronous tasks, A, B, and C, in which task C runs after task A and task B. Task C returns a pair of its tf::AsyncTask handle and a std::future<int> that eventually will hold the result of the execution. Assigned task names will appear in the observers of the executor.
You can mix the use of tf::AsyncTask handles returned by tf::Executor::dependent_async and tf::Executor::silent_dependent_async when specifying task dependencies.
This member function is thread-safe.
| std::shared_ptr< Observer > tf::Executor::make_observer | ( | ArgsT &&... | args | ) |
constructs an observer to inspect the activities of worker threads
| Observer | observer type derived from tf::ObserverInterface |
| ArgsT | argument parameter pack |
| args | arguments to forward to the constructor of the observer |
Each executor manages a list of observers with shared ownership with callers. For each of these observers, the two member functions, tf::ObserverInterface::on_entry and tf::ObserverInterface::on_exit will be called before and after the execution of a task.
This member function is not thread-safe.
| size_t tf::Executor::num_topologies | ( | ) | const |
queries the number of running topologies at the time of this call
When a taskflow is submitted to an executor, a topology is created to store runtime metadata of the running taskflow. When the execution of the submitted taskflow finishes, its corresponding topology will be removed from the executor.
|
noexcept |
queries the number of workers that are in the waiting loop
A worker in the waiting loop has exhausted its local queue and made enough stealing attempts, and is now ready to be preempted and enter the waiting state.
|
noexcept |
queries the number of worker threads
Each worker represents a unique thread spawned by an executor upon its construction time.
| void tf::Executor::remove_observer | ( | std::shared_ptr< Observer > | observer | ) |
removes an observer from the executor
This member function is not thread-safe.
| tf::Future< void > tf::Executor::run | ( | Taskflow && | taskflow | ) |
runs a moved taskflow once
| taskflow | a moved tf::Taskflow object |
This member function executes a moved taskflow once and returns a tf::Future object that eventually holds the result of the execution. The executor will take care of the lifetime of the moved taskflow.
This member function is thread-safe.
| tf::Future< void > tf::Executor::run | ( | Taskflow && | taskflow, |
| C && | callable ) |
runs a moved taskflow once and invoke a callback upon completion
| taskflow | a moved tf::Taskflow object |
| callable | a callable object to be invoked after this run |
This member function executes a moved taskflow once and invokes the given callable when the execution completes. This member function returns a tf::Future object that eventually holds the result of the execution. The executor will take care of the lifetime of the moved taskflow.
This member function is thread-safe.
| tf::Future< void > tf::Executor::run | ( | Taskflow & | taskflow | ) |
runs a taskflow once
| taskflow | a tf::Taskflow object |
This member function executes the given taskflow once and returns a tf::Future object that eventually holds the result of the execution.
This member function is thread-safe.
| tf::Future< void > tf::Executor::run | ( | Taskflow & | taskflow, |
| C && | callable ) |
runs a taskflow once and invoke a callback upon completion
| taskflow | a tf::Taskflow object |
| callable | a callable object to be invoked after this run |
This member function executes the given taskflow once and invokes the given callable when the execution completes. This member function returns a tf::Future object that eventually holds the result of the execution.
This member function is thread-safe.
| tf::Future< void > tf::Executor::run_n | ( | Taskflow && | taskflow, |
| size_t | N ) |
runs a moved taskflow for N times
| taskflow | a moved tf::Taskflow object |
| N | number of runs |
This member function executes a moved taskflow N times and returns a tf::Future object that eventually holds the result of the execution. The executor will take care of the lifetime of the moved taskflow.
This member function is thread-safe.
| tf::Future< void > tf::Executor::run_n | ( | Taskflow && | taskflow, |
| size_t | N, | ||
| C && | callable ) |
runs a moved taskflow for N times and then invokes a callback
| taskflow | a moved tf::Taskflow |
| N | number of runs |
| callable | a callable object to be invoked after this run |
This member function executes a moved taskflow N times and invokes the given callable when the execution completes. This member function returns a tf::Future object that eventually holds the result of the execution.
This member function is thread-safe.
| tf::Future< void > tf::Executor::run_n | ( | Taskflow & | taskflow, |
| size_t | N ) |
runs a taskflow for N times
| taskflow | a tf::Taskflow object |
| N | number of runs |
This member function executes the given taskflow N times and returns a tf::Future object that eventually holds the result of the execution.
This member function is thread-safe.
| tf::Future< void > tf::Executor::run_n | ( | Taskflow & | taskflow, |
| size_t | N, | ||
| C && | callable ) |
runs a taskflow for N times and then invokes a callback
| taskflow | a tf::Taskflow |
| N | number of runs |
| callable | a callable object to be invoked after this run |
This member function executes the given taskflow N times and invokes the given callable when the execution completes. This member function returns a tf::Future object that eventually holds the result of the execution.
This member function is thread-safe.
| tf::Future< void > tf::Executor::run_until | ( | Taskflow && | taskflow, |
| P && | pred ) |
runs a moved taskflow and keeps running it until the predicate becomes true
| taskflow | a moved tf::Taskflow object |
| pred | a boolean predicate to return true for stop |
This member function executes a moved taskflow multiple times until the predicate returns true. This member function returns a tf::Future object that eventually holds the result of the execution. The executor will take care of the lifetime of the moved taskflow.
This member function is thread-safe.
| tf::Future< void > tf::Executor::run_until | ( | Taskflow && | taskflow, |
| P && | pred, | ||
| C && | callable ) |
runs a moved taskflow and keeps running it until the predicate becomes true and then invokes the callback
| taskflow | a moved tf::Taskflow |
| pred | a boolean predicate to return true for stop |
| callable | a callable object to be invoked after this run completes |
This member function executes a moved taskflow multiple times until the predicate returns true and then invokes the given callable when the execution completes. This member function returns a tf::Future object that eventually holds the result of the execution. The executor will take care of the lifetime of the moved taskflow.
This member function is thread-safe.
| tf::Future< void > tf::Executor::run_until | ( | Taskflow & | taskflow, |
| P && | pred ) |
runs a taskflow multiple times until the predicate becomes true
| taskflow | a tf::Taskflow |
| pred | a boolean predicate to return true for stop |
This member function executes the given taskflow multiple times until the predicate returns true. This member function returns a tf::Future object that eventually holds the result of the execution.
This member function is thread-safe.
| tf::Future< void > tf::Executor::run_until | ( | Taskflow & | taskflow, |
| P && | pred, | ||
| C && | callable ) |
runs a taskflow multiple times until the predicate becomes true and then invokes the callback
| taskflow | a tf::Taskflow |
| pred | a boolean predicate to return true for stop |
| callable | a callable object to be invoked after this run completes |
This member function executes the given taskflow multiple times until the predicate returns true and then invokes the given callable when the execution completes. This member function returns a tf::Future object that eventually holds the result of the execution.
This member function is thread-safe.
| void tf::Executor::silent_async | ( | F && | func | ) |
similar to tf::Executor::async but does not return a future object
| F | callable type |
| func | callable object |
The method creates an asynchronous task to run the given function without returning any std::future object. This member function is more efficient than tf::Executor::async and is encouraged to use when applications do not need a std::future to acquire the result or synchronize the execution.
This member function is thread-safe.
| void tf::Executor::silent_async | ( | P && | params, |
| F && | func ) |
similar to tf::Executor::async but does not return a future object
| F | callable type |
| params | task parameters |
| func | callable object |
The method creates a parameterized asynchronous task to run the given function without returning any std::future object. This member function is more efficient than tf::Executor::async and is encouraged to use when applications do not need a std::future to acquire the result or synchronize the execution.
This member function is thread-safe.
| tf::AsyncTask tf::Executor::silent_dependent_async | ( | F && | func, |
| I | first, | ||
| I | last ) |
runs the given function asynchronously when the given range of predecessors finish
| F | callable type |
| I | iterator type |
| func | callable object |
| first | iterator to the beginning (inclusive) |
| last | iterator to the end (exclusive) |
This member function is more efficient than tf::Executor::dependent_async and is encouraged to use when you do not want a std::future to acquire the result or synchronize the execution. The example below creates three asynchronous tasks, A, B, and C, in which task C runs after task A and task B.
This member function is thread-safe.
| tf::AsyncTask tf::Executor::silent_dependent_async | ( | F && | func, |
| Tasks &&... | tasks ) |
runs the given function asynchronously when the given predecessors finish
| F | callable type |
| Tasks | task types convertible to tf::AsyncTask |
| func | callable object |
| tasks | asynchronous tasks on which this execution depends |
This member function is more efficient than tf::Executor::dependent_async and is encouraged to use when you do not want a std::future to acquire the result or synchronize the execution. The example below creates three asynchronous tasks, A, B, and C, in which task C runs after task A and task B.
This member function is thread-safe.
| tf::AsyncTask tf::Executor::silent_dependent_async | ( | P && | params, |
| F && | func, | ||
| I | first, | ||
| I | last ) |
runs the given function asynchronously when the given range of predecessors finish
| F | callable type |
| I | iterator type |
| params | tasks parameters |
| func | callable object |
| first | iterator to the beginning (inclusive) |
| last | iterator to the end (exclusive) |
This member function is more efficient than tf::Executor::dependent_async and is encouraged to use when you do not want a std::future to acquire the result or synchronize the execution. The example below creates three asynchronous tasks, A, B, and C, in which task C runs after task A and task B. Assigned task names will appear in the observers of the executor.
This member function is thread-safe.
| tf::AsyncTask tf::Executor::silent_dependent_async | ( | P && | params, |
| F && | func, | ||
| Tasks &&... | tasks ) |
runs the given function asynchronously when the given predecessors finish
| F | callable type |
| Tasks | task types convertible to tf::AsyncTask |
| params | task parameters |
| func | callable object |
| tasks | asynchronous tasks on which this execution depends |
This member function is more efficient than tf::Executor::dependent_async and is encouraged to use when you do not want a std::future to acquire the result or synchronize the execution. The example below creates three asynchronous tasks, A, B, and C, in which task C runs after task A and task B. Assigned task names will appear in the observers of the executor.
This member function is thread-safe.
|
inline |
creates a task group that executes a collection of asynchronous tasks
A TaskGroup allows submitting multiple asynchronous tasks to the executor and waiting for their completion collectively using corun(). Tasks added to the group can execute in parallel and may capture local variables by value or reference, depending on your needs. This can be useful for divide-and-conquer algorithms, parallel loops, or any workflow that requires grouping related tasks.
Example (computing Fibonacci numbers in parallel):
This member function is thread-safe.
| Worker * tf::Executor::this_worker | ( | ) |
queries pointer to the calling worker if it belongs to this executor, otherwise returns nullptr
Returns a pointer to the per-worker storage associated with this executor. If the calling thread is not a worker of this executor, the function returns nullptr.
| int tf::Executor::this_worker_id | ( | ) | const |
queries the id of the caller thread within this executor
Each worker has an unique id in the range of 0 to N-1 associated with its parent executor. If the caller thread does not belong to the executor, -1 is returned.
| void tf::Executor::wait_for_all | ( | ) |
waits for all tasks to complete
This member function waits until all submitted tasks (e.g., taskflows, asynchronous tasks) to finish.