Cpp-Taskflow
2.4-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 taskflkow, you need to create an executor from 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.
tf::Executor is thread-safe. Touching an executor from multiple threads is acceptable. You can have multiple threads call the same executor to run different taskflows.
Inspecting the thread activities is very important for performance analysis. It allows you to know when each task starts and ends participating in the task scheduling. Cpp-Taskflow provides a default observer class tf::ExecutorObserver for this purpose. The following example shows how to create an observer from an executor.
Note that each executor can only have an observer at a time. An observer will automatically record the start and end timestamps of each executed task. Users can query, dump or remove the timestamps through the tf::ExecutorObserver::num_tasks, tf::ExecutorObserver::dump and tf::ExecutorObserver::clear methods.
Debrief:
You can visualize the timeline data in a Chrome browser:
Tasks will be categorized by the executing thread and each task is named with i_j where i is the thread id and j is the task number. You can pan or zoom in/out the timeline to get a detailed view.
You can derive your own observer from the base interface class tf::ExecutorObserverInterface to customize the observing methods.