![]() |
Taskflow
3.2.0-Master-Branch
|
After you create a task dependency graph, you need to submit it to threads for execution. In this chapter, we will show you how to execute a task dependency graph.
To execute a taskflow, you need to create an executor of type tf::Executor. An executor is a thread-safe object that manages a set of worker threads and executes tasks through an efficient work-stealing algorithm. Issuing a call to run a taskflow creates a topology, a data structure to keep track of the execution status of a running graph. tf::Executor takes an unsigned integer to construct with N
worker threads. The default value is std::thread::hardware_concurrency.
An executor can be reused to execute multiple taskflows. In most workloads, you may need only one executor to run multiple taskflows where each taskflow represents a part of a parallel decomposition.
tf::Executor provides a set of run_*
methods, tf::Executor::run, tf::Executor::run_n, and tf::Executor::run_until to run a taskflow for one time, multiple times, or until a given predicate evaluates to true. All methods accept an optional callback to invoke after the execution completes, and return a tf::Future for users to access the execution status. The code below shows several ways to run a taskflow.
Debrief:
Issuing multiple runs on the same taskflow will automatically synchronize to a sequential chain of executions in the order of run calls.
Similarly, you should avoid touching a taskflow while it is running.
You must always keep a taskflow alive and must not modify it while it is running on an executor.
You can transfer the ownership of a taskflow to an executor and run it without wrangling with the lifetime issue of that taskflow. Each run_*
method discussed in the previous section comes with an overload that takes a moved taskflow object.
However, you should avoid moving a running taskflow which can result in undefined behavior.
The correct way to submit a taskflow with moved ownership to an executor is to ensure all previous runs have completed. The executor will automatically release the resources of a moved taskflow right after its execution completes.
Likewise, you cannot move a taskflow that is running on an executor. You must wait until all the previous fires of runs on that taskflow complete before calling move.
All run_*
methods are thread-safe. You can have multiple threads call these methods from an executor to run different taskflows. However, the order which taskflow runs first is non-deterministic and is up to the runtime.
Each worker in an executor has an unique integer identifier in the range [0, N)
that can be queried by the caller thread using tf::Executor::this_worker_id. If the caller thread is not a worker in the executor, -1
is returned. This method is convenient for users to maintain a one-to-one mapping between a worker and its application data structure.
You can observe thread activities in an executor when a worker thread participates in executing a task and leaves the execution using tf::ObserverInterface – an interface class that provides a set of methods for you to define what to do when a thread enters and leaves the execution context of a task.
There are three methods you must define in your derived class, tf::ObserverInterface::set_up, tf::ObserverInterface::on_entry, and tf::ObserverInterface::on_exit. The method, tf::ObserverInterface::set_up, is a constructor-like method that will be called by the executor when the observer is constructed. It passes an argument of the number of workers to observer in the executor. You may use it to preallocate or initialize data storage, e.g., an independent vector for each worker. The methods, tf::ObserverInterface::on_entry and tf::ObserverInterface::on_exit, are called by a worker thread before and after the execution context of a task, respectively. Both methods provide immutable access to the underlying worker and the running task using tf::WorkerView and tf::TaskView. You may use them to record timepoints and calculate the elapsed time of a task.
You can associate an executor with one or multiple observers (though one is common) using tf::Executor::make_observer. We use std::shared_ptr to manage the ownership of an observer. The executor loops through each observer and invoke the corresponding methods accordingly.
The above code produces the following output:
It is expected each line of std::cout interleaves with each other as there are four workers participating in task scheduling. However, the ready message always appears before the corresponding task message (e.g., numbers) and then the finished message.