Taskflow
2.7.0
|
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 std::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.
A key point to notice is a running taskflow must remain alive during its execution. It is your responsibility to ensure a taskflow not being destructed when it is running. For example, the code below can result undefined behavior.
Similarly, you should avoid touching a taskflow while it is running.
A rule of thumb is to always keep a taskflow alive in your function scope while it is participating in an execution.
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.
It is your responsibility to ensure all taskflows from different threads remain alive during their executions; or it can result unexpected behavior or program crash.
In addition to run a taskflow, the executor provides a STL-styled method, tf::Executor::async, for you to run a callable object (e.g., function) asynchronously. A common use case of the method is using the executor as a thread pool.
The method, tf::Executor::async, is thread-safe and can be called from any threads or from the execution of a task. Our scheduler autonomously detects whether the async task is submitted from an external thread or a worker thread and executes it in the work-stealing loop.
You can observe thread activities in an executor when a worker thread participates in executing a task and leaves the execution using tf::ObserverInterface. tf::ObserverInterface is 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. 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.