本文最后更新于:2024年1月14日 晚上

Taskflow 可以帮助您在现代 C + + 中快速编写并行和异构的任务程序 。

简介

下载使用

  • Taskflow 使用需要源码

下载

Release
Clone Repository
  • 也可以直接获取整个仓库:

    1
    git clone https://github.com/taskflow/taskflow
  • 下载仓库后切换到 v3.5.0 版本

    1
    git checkout v3.5.0

使用

  • 使用时仅需引入源码目录中的 taskflow/taskflow.hpp 即可

  • Taskflow 源码目录添加到系统环境变量,在项目中添加引用目录,代码中直接 include 就可以使用了

    1
    #include <taskflow/taskflow.hpp>  // Taskflow is header-only
  • 命名空间叫 tf

    1
    using namespace tf;

C++环境

  • 注意:Taskflow 需要 C++17 编译环境
g++
  • 如果使用 g++ 需要在命令行添加参数 -std=c++17

    1
    g++ -std=c++17
Visual Studio
  • 如果是在 Visual Studio 下需要开启 C++17 环境,需要在项目属性 -> C/C++ -> 语言 -> C++ 语言标准 中修改:

入门用法

Demo

  • 官网的 Demo 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <taskflow/taskflow.hpp>  // 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;
}
  • 输出:
1
2
3
4
TaskA
TaskC
TaskB
TaskD

流程图

  • Taskflow 可以输出代码运行拓扑图
  • 为了不让用户额外安装流程图绘制的工具, Taskflow 提供了解释流程图的网页工具
  • 使用时,在代码中加入 taskflow.dump(std::cout); 可以输出流程图代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <taskflow/taskflow.hpp>  // Taskflow is header-only


int main()
{

tf::Executor executor;
tf::Taskflow taskflow;

tf::Task A = taskflow.emplace([]() {}).name("A");
tf::Task B = taskflow.emplace([]() {}).name("B");
tf::Task C = taskflow.emplace([]() {}).name("C");
tf::Task D = taskflow.emplace([]() {}).name("D");
tf::Task E = taskflow.emplace([]() {}).name("E");
A.precede(B, C, E);
C.precede(D);
B.precede(D, E);

executor.run(taskflow).wait();
taskflow.dump(std::cout);
return 0;
}
  • 上述代码输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
digraph Taskflow {
subgraph cluster_p000000B14C93F2A8 {
label="Taskflow: p000000B14C93F230";
p0000022B29D89270[label="A" ];
p0000022B29D89270 -> p0000022B29D89380;
p0000022B29D89270 -> p0000022B29D89490;
p0000022B29D89270 -> p0000022B29D896B0;
p0000022B29D89380[label="B" ];
p0000022B29D89380 -> p0000022B29D895A0;
p0000022B29D89380 -> p0000022B29D896B0;
p0000022B29D89490[label="C" ];
p0000022B29D89490 -> p0000022B29D895A0;
p0000022B29D895A0[label="D" ];
p0000022B29D896B0[label="E" ];
}
}
  • 上述代码在 GraphViz 类工具中都可以识别为流程图
  • 官方提供了访问流程图绘制工具网页:https://dreampuf.github.io/GraphvizOnline/
  • 将代码粘贴到网页中,就可以看到流程图了

Profile

  • Taskflow 还提供了 Profile 工具 (需要较高的 Taskflow 版本)
  • 使用时需要设置系统环境变量 TF_ENABLE_PROFILER 指向一个 json 文件位置(路径可以相对、可以绝对)

  • 这样在运行程序时会向目标位置输出 json 文件,类似:
1
2
3
[
{"executor":"0","data":[{"worker":10,"level":0,"data":[{"span":[81,83],"name":"B","type":"static"},{"span":[84,84],"name":"E","type":"static"}]},{"worker":11,"level":0,"data":[{"span":[68,73],"name":"A","type":"static"},{"span":[80,80],"name":"C","type":"static"},{"span":[85,85],"name":"D","type":"static"}]}]}
]

原始论文

《Taskflow:A Lightweight Parallel and Heterogeneous Task Graph Computing System》

参考资料



文章链接:
https://www.zywvvd.com/notes/coding/cpp/taskflow/taskflow/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

C++ Taskflow QuickStart
https://www.zywvvd.com/notes/coding/cpp/taskflow/taskflow/
作者
Yiwei Zhang
发布于
2023年1月16日
许可协议