taskflow/taskflow
GitHub: taskflow/taskflow
一个轻量级的 C++20 头文件式任务并行编程框架,通过有向无环图表达任务依赖关系,支持静态与动态任务图、条件控制流、异构 CPU-GPU 协同计算以及可组合的模块化并行流水线。
Stars: 11866 | Forks: 1388
# Taskflow
[](https://github.com/taskflow/taskflow/actions?query=workflow%3AUbuntu)
[](https://github.com/taskflow/taskflow/actions?query=workflow%3AmacOS)
[](https://github.com/taskflow/taskflow/actions?query=workflow%3AWindows)
[][documentation]
[](https://taskflow.github.io/tfprof/)
[][TPDS22]
Taskflow 帮助您使用现代 C++ 快速编写任务并行程序
# 为什么选择 Taskflow?
在处理复杂的并行工作负载时,与许多现有的任务编程框架相比,Taskflow 速度更快、更具表现力且更容易集成。

Taskflow 让您能够快速实现任务分解策略,
这些策略结合了规则和不规则的计算模式,
并搭配高效的*工作窃取* (work-stealing) 调度器来优化您的多线程性能。
| [静态任务](#start-your-first-taskflow-program) | [子流任务](#create-a-subflow-graph) |
| :------------: | :-------------: |
|  |
|
Taskflow 支持条件任务,让您能够跨依赖任务快速做出控制流决策,
从而实现循环和条件,这些在现有工具中通常难以做到。
| [条件任务](#integrate-control-flow-to-a-task-graph) |
| :-----------------: |
|  |
Taskflow 是可组合的。您可以通过组合模块化且可重用的块来创建大型并行图,
这使得在各个独立作用域内更容易进行优化。
| [Taskflow 组合](#compose-task-graphs) |
| :---------------: |
||
Taskflow 支持异构任务,让您能够利用 CPU-GPU 协同计算的能力,
加速广泛的科学计算应用程序。
| [并发 CPU-GPU 任务](#offload-a-task-to-a-gpu) |
| :-----------------: |
|  |
Taskflow 提供了分析和可视化 Taskflow 程序所需的可视化工具。
| [Taskflow 分析器](https://taskflow.github.io/tfprof) |
| :-----------------: |
|  |
我们致力于支持并行计算领域学术和工业研究项目的可信开发。
请查看 [谁在使用 Taskflow](https://taskflow.github.io/#tag_users) 以及我们用户的评价:
+ *"Taskflow 是我见过的最简洁的 Task API。" [Damien Hocking @Corelium Inc](http://coreliuminc.com)*
+ *"Taskflow 具有非常简单优雅的任务接口。性能的扩展性也非常好。" [Glen Fraser][totalgee]*
+ *"Taskflow 让我能够以聪明的方式处理并行计算。" [Hayabusa @Learning](https://cpp-learning.com/cpp-taskflow/)*
+ *"Taskflow 仅凭几个小时的编码就提升了我们图引擎的吞吐量。" [Jean-Michaël @KDAB](https://ossia.io/)*
+ *"开源并行编程库最佳海报奖。" [Cpp Conference 2018][Cpp Conference 2018]*
+ *"开源软件竞赛二等奖。" [ACM Multimedia Conference 2019](https://tsung-wei-huang.github.io/img/mm19-ossc-award.jpg)*
请观看下方的快速海报展示,
并访问[文档][documentation]以了解更多关于 Taskflow 的信息。
技术细节可参考我们的 [IEEE TPDS 论文][TPDS22]。

# 开始您的第一个 Taskflow 程序
以下程序 (`simple.cpp`) 创建了一个包含四个任务 `A`、`B`、`C` 和 `D` 的任务流,其中 `A` 在 `B` 和 `C` 之前运行,而 `D` 在 `B` 和 `C` 之后运行。
当 `A` 完成后,`B` 和 `C` 可以并行运行。
在 [Compiler Explorer (godbolt)](https://godbolt.org/z/j8hx3xnnx) 上在线尝试!
```
#include // Taskflow is header-only
int main(){
tf::Executor executor;
tf::Taskflow taskflow;
auto [A, B, C, D] = taskflow.emplace( // create four tasks
[] () { std::cout << "TaskA\n"; },
[] () { std::cout << "TaskB\n"; },
[] () { std::cout << "TaskC\n"; },
[] () { std::cout << "TaskD\n"; }
);
A.precede(B, C); // A runs before B and C
D.succeed(B, C); // D runs after B and C
executor.run(taskflow).wait();
return 0;
}
```
Taskflow 是*仅包含头文件*的库,无需为安装而烦恼。
要编译该程序,请克隆 Taskflow 项目并告诉编译器包含[头文件](./taskflow/)。
```
~$ git clone https://github.com/taskflow/taskflow.git # clone it only once
~$ g++ -std=c++20 examples/simple.cpp -I. -O2 -pthread -o simple
~$ ./simple
TaskA
TaskC
TaskB
TaskD
```
# 可视化您的第一个 Taskflow 程序
Taskflow 附带了一个内置的分析器,
[TFProf](https://taskflow.github.io/tfprof/),
让您能够在易于使用的 Web 界面中分析和可视化任务流程序。

```
# 在启用环境变量 TF_ENABLE_PROFILER 的情况下运行程序
~$ TF_ENABLE_PROFILER=simple.json ./simple
~$ cat simple.json
[
{"executor":"0","data":[{"worker":0,"level":0,"data":[{"span":[172,186],"name":"0_0","type":"static"},{"span":[187,189],"name":"0_1","type":"static"}]},{"worker":2,"level":0,"data":[{"span":[93,164],"name":"2_0","type":"static"},{"span":[170,179],"name":"2_1","type":"static"}]}]}
]
# 将性能分析的 json 数据粘贴到 https://taskflow.github.io/tfprof/
```
除了执行图之外,您还可以将图导出为 DOT 格式,
并使用多种免费的 [GraphViz][GraphViz] 工具对其进行可视化。
```
// dump the taskflow graph to a DOT format through std::cout
taskflow.dump(std::cout);
```
future = executor.async([](){
std::cout << "async task returns 1\n";
return 1;
});
executor.silent_async([](){ std::cout << "async task does not return\n"; });
// create asynchronous tasks with dynamic dependencies
tf::AsyncTask A = executor.silent_dependent_async([](){ printf("A\n"); });
tf::AsyncTask B = executor.silent_dependent_async([](){ printf("B\n"); }, A);
tf::AsyncTask C = executor.silent_dependent_async([](){ printf("C\n"); }, A);
tf::AsyncTask D = executor.silent_dependent_async([](){ printf("D\n"); }, B, C);
executor.wait_for_all();
```
## 执行 Taskflow
执行器提供了多种*线程安全*的方法来运行任务流。
您可以运行任务流一次、多次,或者直到满足停止条件为止。
这些方法是非阻塞的,并返回 `tf::Future` 以便您查询执行状态。
```
// runs the taskflow once
tf::Future run_once = executor.run(taskflow);
// wait on this run to finish
run_once.get();
// run the taskflow four times
executor.run_n(taskflow, 4);
// runs the taskflow five times
executor.run_until(taskflow, [counter=5](){ return --counter == 0; });
// block the executor until all submitted taskflows complete
executor.wait_for_all();
```
## 利用标准并行算法
Taskflow 定义了多种算法,让您能够使用标准的 C++ 语法快速表达常见的并行模式,
例如并行迭代、并行规约和并行排序。
```
tf::Task task1 = taskflow.for_each( // assign each element to 100 in parallel
first, last, [] (auto& i) { i = 100; }
);
tf::Task task2 = taskflow.reduce( // reduce a range of items in parallel
first, last, init, [] (auto a, auto b) { return a + b; }
);
tf::Task task3 = taskflow.sort( // sort a range of items in parallel
first, last, [] (auto a, auto b) { return a < b; }
);
```
此外,Taskflow 还提供了可组合的图构建块,
帮助您高效实现常见的并行算法,例如并行流水线。
```
// create a pipeline to propagate five tokens through three serial stages
tf::Pipeline pl(num_parallel_lines,
tf::Pipe{tf::PipeType::SERIAL, [](tf::Pipeflow& pf) {
if(pf.token() == 5) {
pf.stop();
}
}},
tf::Pipe{tf::PipeType::SERIAL, [](tf::Pipeflow& pf) {
printf("stage 2: input buffer[%zu] = %d\n", pf.line(), buffer[pf.line()]);
}},
tf::Pipe{tf::PipeType::SERIAL, [](tf::Pipeflow& pf) {
printf("stage 3: input buffer[%zu] = %d\n", pf.line(), buffer[pf.line()]);
}}
);
taskflow.composed_of(pl)
executor.run(taskflow).wait();
```
# 支持的编译器
要使用 Taskflow v4.0.0,您需要一个支持 C++20 的编译器:
+ GNU C++ Compiler 至少 v11.0 且使用 -std=c++20
+ Clang C++ Compiler 至少 v12.0 且使用 -std=c++20
+ Microsoft Visual Studio 至少 v19.29 (VS 2019) 且使用 /std:c++20
+ Apple Clang (Xcode) 至少 v13.0 且使用 -std=c++20
+ NVIDIA CUDA Toolkit 和 Compiler (nvcc) 至少 v12.0 且宿主编译器支持 C++20
+ Intel oneAPI DPC++/C++ Compiler 至少 v2022.0 且使用 -std=c++20
Taskflow 可在 Linux、Windows 和 Mac OS X 上运行。
# 了解更多关于 Taskflow 的信息
访问我们的[项目网站][Project Website]和[文档][documentation]
以了解更多关于 Taskflow 的信息。参与方式:
+ 查看[发布说明][release notes]以随时了解最新版本
+ 阅读 [cookbook][cookbook] 中的分步教程
+ 在 [GitHub issues][GitHub issues] 提交问题
+ 在 [references][references] 了解我们的技术细节
+ 在 YouTube 观看我们的技术讲座
[](https://www.youtube.com/watch?v=u4vaY0cjzos)
我们致力于为并行和异构计算领域的
学术和工业研究项目提供可信的开发支持。
如果您正在使用 Taskflow,请引用我们在 2021 IEEE TPDS 发表的以下论文:
+ Tsung-Wei Huang, Dian-Lun Lin, Chun-Xun Lin, and Yibo Lin, "[Taskflow: A Lightweight Parallel and Heterogeneous Task Graph Computing System](https://tsung-wei-huang.github.io/papers/tpds21-taskflow.pdf)," IEEE Transactions on Parallel and Distributed Systems (TPDS), vol. 33, no. 6, pp. 1303-1320, June 2022
更重要的是,我们感谢所有 Taskflow [贡献者][contributors] 和
以下组织对 Taskflow 项目的赞助!
| | | | |
|:-------------------------:|:-------------------------:|:-------------------------:|:-------------------------:|
|
|
|
|
|
|
|
| | |
# 许可证
Taskflow 采用 [MIT 许可证](./LICENSE) 授权。
您可以完全自由地分发基于 Taskflow 衍生的作品。
[](https://github.com/taskflow/taskflow/actions?query=workflow%3AUbuntu)
[](https://github.com/taskflow/taskflow/actions?query=workflow%3AmacOS)
[](https://github.com/taskflow/taskflow/actions?query=workflow%3AWindows)
[][documentation]
[](https://taskflow.github.io/tfprof/)
[][TPDS22]
Taskflow 帮助您使用现代 C++ 快速编写任务并行程序
# 为什么选择 Taskflow?
在处理复杂的并行工作负载时,与许多现有的任务编程框架相比,Taskflow 速度更快、更具表现力且更容易集成。

Taskflow 让您能够快速实现任务分解策略,
这些策略结合了规则和不规则的计算模式,
并搭配高效的*工作窃取* (work-stealing) 调度器来优化您的多线程性能。
| [静态任务](#start-your-first-taskflow-program) | [子流任务](#create-a-subflow-graph) |
| :------------: | :-------------: |
|  |
|
|
|
|
|
|
| | |
# 许可证
Taskflow 采用 [MIT 许可证](./LICENSE) 授权。
您可以完全自由地分发基于 Taskflow 衍生的作品。
标签:C++, DAG任务图, DNS解析, Header-Only库, Taskflow, Vectored Exception Handling, 任务并行, 任务编排, 任务调度, 分布式系统基础, 动态任务, 多线程编程, 子流, 工作窃取, 并发库, 并行工作负载, 并行框架, 并行计算, 开源项目, 异步编程, 性能优化, 数据并行, 数据擦除, 条件任务, 检测绕过, 流处理, 现代C++, 计算图, 静态任务, 高性能计算