Skip to content
Back to search

taskflow

A General-purpose Task-parallel Programming System in C++

Safety scan

A static text scan, not a guarantee. Select the badge to see what was scanned and every finding.

Checked today
Badge for your README
findsafeskills safety rating

Markdown

[![findsafeskills safety rating](/api/listings/taskflow--taskflow/badge.svg)](/listings/taskflow--taskflow)

HTML

<a href="/listings/taskflow--taskflow"><img src="/api/listings/taskflow--taskflow/badge.svg" alt="findsafeskills safety rating" /></a>

Install

i

What was scanned

The files the safety scan read, as written by the repository's author and shown as plain text. Lines with a finding are marked.

README.md · README · 304 lines

View this file on GitHub (read at the repository's current version: the scan did not record which commit it read)

  1. # Taskflow <img align="right" width="10%" src="images/taskflow_logo.png">
  2. [![Ubuntu](https://github.com/taskflow/taskflow/workflows/Ubuntu/badge.svg)](https://github.com/taskflow/taskflow/actions?query=workflow%3AUbuntu)
  3. [![macOS](https://github.com/taskflow/taskflow/workflows/macOS/badge.svg)](https://github.com/taskflow/taskflow/actions?query=workflow%3AmacOS)
  4. [![Windows](https://github.com/taskflow/taskflow/workflows/Windows/badge.svg)](https://github.com/taskflow/taskflow/actions?query=workflow%3AWindows)
  5. [![Wiki](images/api-doc.svg)][documentation]
  6. [![TFProf](images/tfprof.svg)](https://taskflow.github.io/tfprof/)
  7. [![Cite](images/cite-tpds.svg)][TPDS22]
  8. Taskflow helps you quickly write high-performance task-parallel programs with high programming productivity.
  9. It is faster, more expressive, fewer lines of code, and easier for drop-in integration
  10. than many existing task programming libraries.
  11. ## Start Your First Taskflow Program
  12. The following program (`simple.cpp`) creates four tasks `A`, `B`, `C`, and `D`,
  13. where `A` runs before `B` and `C`, and `D` runs after `B` and `C`.
  14. When `A` finishes, `B` and `C` run in parallel.
  15. Try it live on [Compiler Explorer](https://godbolt.org/z/j8hx3xnnx)!
  16. ```cpp
  17. #include <taskflow/taskflow.hpp> // Taskflow is header-only
  18. int main(){
  19. tf::Executor executor;
  20. tf::Taskflow taskflow;
  21. auto [A, B, C, D] = taskflow.emplace( // create four tasks
  22. [] () { std::cout << "TaskA\n"; },
  23. [] () { std::cout << "TaskB\n"; },
  24. [] () { std::cout << "TaskC\n"; },
  25. [] () { std::cout << "TaskD\n"; }
  26. );
  27. A.precede(B, C); // A runs before B and C
  28. D.succeed(B, C); // D runs after B and C
  29. executor.run(taskflow).wait();
  30. return 0;
  31. }
  32. ```
  33. Taskflow is *header-only* and there is no struggle with installation.
  34. To compile the program, clone the Taskflow project and
  35. tell the compiler to include the headers under `taskflow/`.
  36. ```bash
  37. ~$ git clone https://github.com/taskflow/taskflow.git # clone it only once
  38. ~$ g++ -std=c++20 simple.cpp -I taskflow/ -O2 -pthread -o simple
  39. ~$ ./simple
  40. TaskA
  41. TaskC
  42. TaskB
  43. TaskD
  44. ```
  45. Taskflow comes with a built-in profiler, [TFProf](https://taskflow.github.io/tfprof/),
  46. for you to profile and visualize taskflow programs in an easy-to-use web-based interface.
  47. ```bash
  48. # run the program with the environment variable TF_ENABLE_PROFILER enabled
  49. ~$ TF_ENABLE_PROFILER=simple.tfp ./simple
  50. # drag the simple.tfp file to https://taskflow.github.io/tfprof/
  51. ```
  52. ![](doxygen/images/tfprof_overview.png)
  53. ## Create a Subflow Graph
  54. Taskflow supports *recursive tasking* for you to create a subflow graph
  55. from the execution of a task to perform recursive parallelism.
  56. The following program spawns a task dependency graph parented at task `B`.
  57. ```cpp
  58. tf::Task A = taskflow.emplace([](){}).name("A");
  59. tf::Task C = taskflow.emplace([](){}).name("C");
  60. tf::Task D = taskflow.emplace([](){}).name("D");
  61. tf::Task B = taskflow.emplace([] (tf::Subflow& subflow) { // subflow task B
  62. tf::Task B1 = subflow.emplace([](){}).name("B1");
  63. tf::Task B2 = subflow.emplace([](){}).name("B2");
  64. tf::Task B3 = subflow.emplace([](){}).name("B3");
  65. B3.succeed(B1, B2); // B3 runs after B1 and B2
  66. }).name("B");
  67. A.precede(B, C); // A runs before B and C
  68. D.succeed(B, C); // D runs after B and C
  69. ```
  70. ## Integrate Control Flow into a Task Graph
  71. Taskflow supports *conditional tasking* for you to make rapid
  72. control-flow decisions across dependent tasks to implement cycles
  73. and conditions in an end-to-end task graph.
  74. ```cpp
  75. tf::Task init = taskflow.emplace([](){}).name("init");
  76. tf::Task stop = taskflow.emplace([](){}).name("stop");
  77. // creates a condition task that returns a random binary
  78. tf::Task cond = taskflow.emplace([](){ return std::rand() % 2; }).name("cond");
  79. // creates a feedback loop {0: cond, 1: stop}
  80. init.precede(cond);
  81. cond.precede(cond, stop); // moves on to 'cond' on returning 0, or 'stop' on 1
  82. ```
  83. ## Compose Task Graphs
  84. Taskflow is composable. You can create large parallel graphs through
  85. composition of modular and reusable blocks that are easier to optimize
  86. at an individual scope.
  87. ```cpp
  88. tf::Taskflow f1, f2;
  89. // create taskflow f1 of two tasks
  90. tf::Task f1A = f1.emplace([]() { std::cout << "Task f1A\n"; }).name("f1A");
  91. tf::Task f1B = f1.emplace([]() { std::cout << "Task f1B\n"; }).name("f1B");
  92. // create taskflow f2 with one module task composed of f1
  93. tf::Task f2A = f2.emplace([]() { std::cout << "Task f2A\n"; }).name("f2A");
  94. tf::Task f2B = f2.emplace([]() { std::cout << "Task f2B\n"; }).name("f2B");
  95. tf::Task f2C = f2.emplace([]() { std::cout << "Task f2C\n"; }).name("f2C");
  96. tf::Task f1_module_task = f2.composed_of(f1).name("module");
  97. f1_module_task.succeed(f2A, f2B)
  98. .precede(f2C);
  99. ```
  100. ## Launch Asynchronous Tasks
  101. Taskflow supports *asynchronous* tasking.
  102. You can launch tasks asynchronously to dynamically explore task graph parallelism.
  103. ```cpp
  104. tf::Executor executor;
  105. // create asynchronous tasks directly from an executor
  106. std::future<int> future = executor.async([](){
  107. std::cout << "async task returns 1\n";
  108. return 1;
  109. });
  110. executor.silent_async([](){ std::cout << "async task does not return\n"; });
  111. // create asynchronous tasks with dynamic dependencies
  112. tf::AsyncTask A = executor.silent_dependent_async([](){ printf("A\n"); });
  113. tf::AsyncTask B = executor.silent_dependent_async([](){ printf("B\n"); }, A);
  114. tf::AsyncTask C = executor.silent_dependent_async([](){ printf("C\n"); }, A);
  115. tf::AsyncTask D = executor.silent_dependent_async([](){ printf("D\n"); }, B, C);
  116. executor.wait_for_all();
  117. ```
  118. ## Leverage Standard Parallel Algorithms
  119. Taskflow defines algorithms for you to quickly express common parallel patterns
  120. using standard C++ syntaxes, such as parallel iterations, reductions, and sort.
  121. ```cpp
  122. tf::Task task1 = taskflow.for_each( // assign each element to 100 in parallel
  123. first, last, [] (auto& i) { i = 100; }
  124. );
  125. tf::Task task2 = taskflow.reduce( // reduce a range of items in parallel
  126. first, last, init, [] (auto a, auto b) { return a + b; }
  127. );
  128. tf::Task task3 = taskflow.sort( // sort a range of items in parallel
  129. first, last, [] (auto a, auto b) { return a < b; }
  130. );
  131. ```
  132. Taskflow also provides composable graph building blocks for common parallel
  133. algorithms such as parallel pipeline.
  134. ```cpp
  135. // create a pipeline to propagate five tokens through three serial stages
  136. tf::Pipeline pl(num_lines,
  137. tf::Pipe{tf::PipeType::SERIAL, [](tf::Pipeflow& pf) {
  138. if(pf.token() == 5) pf.stop();
  139. }},
  140. tf::Pipe{tf::PipeType::SERIAL, [](tf::Pipeflow& pf) {
  141. printf("stage 2: input buffer[%zu] = %d\n", pf.line(), buffer[pf.line()]);
  142. }},
  143. tf::Pipe{tf::PipeType::SERIAL, [](tf::Pipeflow& pf) {
  144. printf("stage 3: input buffer[%zu] = %d\n", pf.line(), buffer[pf.line()]);
  145. }}
  146. );
  147. taskflow.composed_of(pl);
  148. executor.run(taskflow).wait();
  149. ```
  150. ## Offload Tasks to a GPU
  151. Taskflow supports GPU tasking for you to accelerate scientific computing
  152. applications by harnessing the power of CPU-GPU collaborative computing
  153. using Nvidia CUDA Graph.
  154. ```cpp
  155. tf::Task cudaflow = taskflow.emplace([&]() {
  156. tf::cudaGraph cg;
  157. tf::cudaTask h2d_x = cg.copy(dx, hx.data(), N);
  158. tf::cudaTask h2d_y = cg.copy(dy, hy.data(), N);
  159. tf::cudaTask d2h_x = cg.copy(hx.data(), dx, N);
  160. tf::cudaTask d2h_y = cg.copy(hy.data(), dy, N);
  161. tf::cudaTask saxpy = cg.kernel((N+255)/256, 256, 0, saxpy, N, 2.0f, dx, dy);
  162. saxpy.succeed(h2d_x, h2d_y)
  163. .precede(d2h_x, d2h_y);
  164. tf::cudaGraphExec exec(cg);
  165. tf::cudaStream stream;
  166. stream.run(exec).synchronize();
  167. }).name("CUDA Graph Task");
  168. ```
  169. ## Run a Taskflow through an Executor
  170. The executor provides several *thread-safe* methods to run a taskflow.
  171. You can run a taskflow once, multiple times, or until a stopping criteria is met.
  172. These methods are non-blocking with a `tf::Future<void>` return
  173. to let you query the execution status.
  174. ```cpp
  175. // runs the taskflow once
  176. tf::Future<void> run_once = executor.run(taskflow);
  177. // wait on this run to finish
  178. run_once.get();
  179. // run the taskflow four times
  180. executor.run_n(taskflow, 4);
  181. // runs the taskflow five times
  182. executor.run_until(taskflow, [counter=5](){ return --counter == 0; });
  183. // blocks the executor until all submitted taskflows complete
  184. executor.wait_for_all();
  185. ```
  186. ## Visualize a Taskflow Graph
  187. You can dump a taskflow graph to a DOT format and visualize it
  188. using free [GraphViz][GraphViz] tools.
  189. ```cpp
  190. // dump the taskflow graph to a DOT format through std::cout
  191. taskflow.dump(std::cout);
  192. ```
  193. ## Supported Compilers
  194. To use Taskflow v4.0.0, you need a compiler that supports C++20:
  195. + GNU C++ Compiler at least v11.0 with -std=c++20
  196. + Clang C++ Compiler at least v12.0 with -std=c++20
  197. + Microsoft Visual Studio at least v19.29 (VS 2019) with /std:c++20
  198. + Apple Clang (Xcode) at least v13.0 with -std=c++20
  199. + NVIDIA CUDA Toolkit and Compiler (nvcc) at least v12.0 with host compiler supporting C++20
  200. + Intel oneAPI DPC++/C++ Compiler at least v2022.0 with -std=c++20
  201. Taskflow works on Linux, Windows, and Mac OS X.
  202. ## Get Involved
  203. Visit our [project website][Project Website] and [documentation][documentation]
  204. to learn more about Taskflow. To get involved:
  205. + See [release notes][release notes] to stay up-to-date with newest versions
  206. + Read the step-by-step tutorial at [cookbook][cookbook]
  207. + Submit an issue at [GitHub issues][GitHub issues]
  208. + Learn more about our technical details at [references][references]
  209. + Watch our [technical talks](https://www.youtube.com/watch?v=u4vaY0cjzos) on YouTube
  210. We are committed to support trustworthy developments for
  211. both academic and industrial research projects in parallel
  212. and heterogeneous computing.
  213. If you are using Taskflow, please cite the following paper we published at 2022 IEEE TPDS:
  214. + Tsung-Wei Huang, Dian-Lun Lin, Chun-Xun Lin, and Yibo Lin, &quot;[Taskflow: A Lightweight Parallel and Heterogeneous Task Graph Computing System](https://tsung-wei-huang.github.io/papers/tpds21-taskflow.pdf),&quot; *IEEE Transactions on Parallel and Distributed Systems (TPDS)*, vol. 33, no. 6, pp. 1303-1320, June 2022
  215. More importantly, we appreciate all Taskflow [contributors][contributors] and
  216. the following organizations for sponsoring the Taskflow project!
  217. | | | | |
  218. |:---:|:---:|:---:|:---:|
  219. |<img src="doxygen/images/utah-ece-logo.png">|<img src="doxygen/images/nsf.png">|<img src="doxygen/images/darpa.png">|<img src="doxygen/images/NumFocus.png">|
  220. |<img src="doxygen/images/nvidia-logo.png">|<img src="doxygen/images/uw-madison-ece-logo.png">|||
  221. ## License
  222. Taskflow is open-source under the permissive [MIT License](./LICENSE).
  223. You are completely free to use, modify, and redistribute any work derived from Taskflow.
  224. * * *
  225. [GraphViz]: https://www.graphviz.org/
  226. [Project Website]: https://taskflow.github.io/
  227. [contributors]: https://taskflow.github.io/taskflow/contributors.html
  228. [documentation]: https://taskflow.github.io/taskflow/index.html
  229. [release notes]: https://taskflow.github.io/taskflow/Releases.html
  230. [cookbook]: https://taskflow.github.io/taskflow/pages.html
  231. [references]: https://taskflow.github.io/taskflow/References.html
  232. [GitHub issues]: https://github.com/taskflow/taskflow/issues
  233. [TPDS22]: https://tsung-wei-huang.github.io/papers/tpds21-taskflow.pdf

Ratings and comments

Sign in to rate this skill.

Sign in

Free to use. If it’s helped, pass it on.