备案 控制台
学习
实践
活动
专区
工具
TVP
写文章
专栏首页 又见苍岚 C++ Taskflow QuickStart
3 0

海报分享

C++ Taskflow QuickStart

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 代码:

123456789101112131415161718192021

#include <taskflow/taskflow.hpp> // Taskflow is header-onlyint 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;}

  • 输出:

1234

TaskATaskCTaskBTaskD

流程图

  • Taskflow 可以输出代码运行拓扑图
  • 为了不让用户额外安装流程图绘制的工具, Taskflow 提供了解释流程图的网页工具
  • 使用时,在代码中加入 taskflow.dump(std::cout); 可以输出流程图代码

12345678910111213141516171819202122

#include <taskflow/taskflow.hpp> // Taskflow is header-onlyint 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;}

  • 上述代码输出:

12345678910111213141516

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" ];}}

Profile

  • Taskflow 还提供了 Profile 工具 (需要较高的 Taskflow 版本)
  • 使用时需要设置系统环境变量 TF_ENABLE_PROFILER 指向一个 json 文件位置(路径可以相对、可以绝对)
  • 这样在运行程序时会向目标位置输出 json 文件,类似:

123

[{"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 复制
如有侵权,请联系 cloudcommunity@tencent.com 删除。