# 任务并行

任务并行是一种[并行计算](https://www.edgepedia.org/zh/parallel-computing)方法，它将不同的、独立的任务或函数分配到各个处理器上同时执行，这与数据并行不同——数据并行是对不同的数据块施加相同的操作。这两种风格各有取舍：任务并行可以达到理想效率，但会遭遇负载不均衡的问题；而数据并行易于实现负载均衡和调度，却会触及可扩展性上限。<sup>[1](https://www.netlib.org/lapack/lawnspdf/lawn97.pdf)</sup> 由于协调独立任务被认为比数据并行更容易出错，Cilk、Chapel、X10、Habanero-Java、OpenMP 和 [OpenCL](https://www.edgepedia.org/zh/opencl) 等语言设计提供了专门的同步与互斥构造。<sup>[2](https://hgpu.org/?p=8404)</sup> 任务并行贯穿于高性能计算的各个领域，从分治算法到异构 CPU/GPU 运行时。<sup>[3](https://doi.org/10.1145/3840389)</sup>

| 关键事实 | 内容 |
|---|---|
| 执行模型 | 任务及其依赖关系构成有向无环图（即任务图）<sup>[4](https://hpc2n.github.io/Task-based-parallelism/branch/master/motivation/)</sup> |
| 核心量 | 工作量 \( T_{1} \)（单处理器时间）、关键路径长度 \( T_{\infty} \)（无限处理器时间）<sup>[5](https://people.csail.mit.edu/bradley/papers/BlumofeJoKu96.pdf)</sup> |
| 调度上界 | 在 \( P \) 个工作者上进行贪心调度时，\( T_{P} \le T_{1}/P + T_{\infty} \)<sup>[6](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3872.pdf)</sup> |
| 主流负载均衡方式 | 工作窃取，被 MIT Cilk、Intel Cilk Plus、Intel TBB、Microsoft PPL 和 OpenMP 任务化采用<sup>[6](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3872.pdf)</sup> |
| 粒度经验法则 | 一个 TBB 任务的执行时间应超过 1 微秒，以摊销开销<sup>[7](https://link.springer.com/chapter/10.1007/979-8-8688-1270-5_6)</sup> |
| 效率阈值 | 多数 OpenMP/TBB 实现需要 O(100k) 条指令（数十微秒）的任务才能达到高效率；最好的实现可处理 O(10k) 条指令<sup>[8](https://www.osti.gov/servlets/purl/1808781)</sup> |
| 主流采用情况 | OpenMP 任务化自 3.0 版（2008 年 5 月）起提供，依赖关系自 4.0 版（2013 年）起支持<sup>[9](https://upcommons.upc.edu/server/api/core/bitstreams/d0a5db2a-2efc-489c-b1a5-e6dbe50bec1f/content)</sup> |

## 工作原理

任务并行计算被表示为一个有向无环图，其中节点是任务，边是依赖关系；运行时可以按照任何尊重这些依赖关系的顺序执行任务。<sup>[4](https://hpc2n.github.io/Task-based-parallelism/branch/master/motivation/)</sup> 在 Cilk 中，计算是一个动态展开的线程有向无环图，过程构成一棵派生树。<sup>[5](https://people.csail.mit.edu/bradley/papers/BlumofeJoKu96.pdf)</sup> 有两个量可以预测性能：工作量 \( T_{1} \)，即在单个处理器上执行整个计算所需的时间；以及关键路径长度 \( T_{\infty} \)，即拥有无限多处理器时所需的时间。<sup>[5](https://people.csail.mit.edu/bradley/papers/BlumofeJoKu96.pdf)</sup> 关键路径是执行时间的下界，无论使用多少个核心<sup>[4](https://hpc2n.github.io/Task-based-parallelism/branch/master/motivation/)</sup>；任何 \( P \) 个处理器上的执行都至少需要 \( \max(T_{1}/P,\, T_{\infty}) \) 的时间。<sup>[5](https://people.csail.mit.edu/bradley/papers/BlumofeJoKu96.pdf)</sup>

贪心调度在理想机器上可达到 \( T_{P} \le T_{1}/P + T_{\infty} \)，这一上界被称为 Brent 引理。<sup>[6](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3872.pdf)</sup> Blumofe 和 Leiserson 针对带依赖关系的多线程计算给出了首个可证明良好的[工作窃取](https://www.edgepedia.org/zh/work-stealing)调度器，其期望运行时间为 \( T_{1}/P + O(T_{\infty}) \)，其中包含调度开销。<sup>[10](https://dl.acm.org/doi/pdf/10.1145/324133.324234)</sup>

## 实现方式

实践者将程序分解为任务，并以隐式或显式的方式表达依赖关系。运行时系统可以从顺序执行顺序以及输入输出变量中推断依赖关系（隐式构造），或者由程序员手工构建任务图。<sup>[4](https://hpc2n.github.io/Task-based-parallelism/branch/master/motivation/)</sup> 在 OpenMP 中，任务通过 `#pragma omp task` 声明，依赖关系通过子句表达，任务可以在 `#pragma omp taskyield` 处挂起和恢复，之后可能由另一个线程接手。<sup>[11](https://www.nm.ifi.lmu.de/teaching/Seminare/2017ws/HPCTrends/doc/final/Schneider_Task-based.Programming.in.HPC.pdf)</sup> 诸如基于任务的范式等库方法让用户定义具有输入、输出和依赖关系的原子任务，将调度和数据迁移留给运行时，作为 MPI 的一种替代方案。<sup>[12](https://pmc.ncbi.nlm.nih.gov/articles/PMC7302835/)</sup>

粒度是核心的实践决策。粗粒度任务意味着低开销但就绪任务少、负载均衡差；细粒度任务意味着高开销但就绪任务多，因此粒度必须权衡。<sup>[4](https://hpc2n.github.io/Task-based-parallelism/branch/master/motivation/)</sup> TBB 的经验法则是任务执行时间应超过 1 微秒<sup>[7](https://link.springer.com/chapter/10.1007/979-8-8688-1270-5_6)</sup>，而实测阈值更大：最好的 OpenMP 和 TBB 实现处理 O(10k) 条指令的任务，但多数需要每个任务 O(100k) 条指令才能达到高效率。<sup>[8](https://www.osti.gov/servlets/purl/1808781)</sup> 程序应创建远多于线程数的任务，让调度器选择映射方式；oneTBB 的调度器刻意采用非公平策略，将任务延迟到能够取得有效进展时再执行。<sup>[13](https://www.intel.com/content/www/us/en/docs/onetbb/developer-guide-api-reference/2022-1/task-based-programming.html)</sup>

## 起源

任务并行的理论基础是 Robert D. Blumofe 和 Charles E. Leiserson 的工作窃取调度器，于 1999 年发表在《美国计算机学会杂志》上。<sup>[14](https://doi.org/10.1145/324133.324234)</sup> 其主要载体是 Cilk，这是一个基于 C 的运行时系统，用于多线程并行编程，由 Robert D. Blumofe 及其同事于 1996 年在《并行与分布式计算杂志》上描述。<sup>[15](https://doi.org/10.1006/jpdc.1996.0107)</sup> Cilk 本身从更早的前身——并行延续机（PCM）——演变而来，PCM 是一个基于延续传递线程的简单运行时；Cilk-1 系统随后加入了可证明良好的工作窃取调度器，其空间、时间和通信上界均在最优值的常数因子范围内。<sup>[16](https://dspace.mit.edu/bitstream/handle/1721.1/149833/MIT-LCS-TR-701.pdf)</sup> Blumofe 和 Leiserson 指出，工作窃取的思想至少可以追溯到函数式程序并行执行的研究以及 Multilisp 的实现。<sup>[10](https://dl.acm.org/doi/pdf/10.1145/324133.324234)</sup> 任务并行作为语言层面的关注点，也在 Ian Foster 及其同事 1996 年关于任务并行与高性能语言的 Fortran M 工作中得到处理。<sup>[17](https://doi.org/10.2172/205045)</sup> 主流采用随之而来：OpenMP 任务并行于 2006 年提出，随 OpenMP 3.0 于 2008 年 5 月发布<sup>[11](https://www.nm.ifi.lmu.de/teaching/Seminare/2017ws/HPCTrends/doc/final/Schneider_Task-based.Programming.in.HPC.pdf)</sup>，带依赖关系的任务化在 4.0 版（2013 年）中提供<sup>[9](https://upcommons.upc.edu/server/api/core/bitstreams/d0a5db2a-2efc-489c-b1a5-e6dbe50bec1f/content)</sup>，而 Intel Cilk Plus 和 Intel TBB 成为业界标准的任务并行库。<sup>[18](https://link.springer.com/article/10.1007/s11227-018-2238-4)</sup>

## 变体

工作窃取是调度 fork-join 并行（任务分裂为子任务后再汇合的风格）的首选方法，但不同实现在窃取对象上有所差异。TBB 和 PPL 使用子任务窃取，Cilk 使用延续窃取（窃取任务的剩余工作，而非其子任务），OpenMP 默认使用子任务窃取；延续窃取将空间膨胀限制在 \( P \) 倍以内，而子任务窃取的空间膨胀是无界的。<sup>[6](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3872.pdf)</sup> 更一般地说，工作窃取将任务保存在各处理器的队列中，空闲处理器从其他队列窃取；而工作共享通过集中式任务池将每个派生任务迁移到新的工作者，因此迁移发生得更频繁。<sup>[18](https://link.springer.com/article/10.1007/s11227-018-2238-4)</sup>

框架版图涵盖为众核处理器构建的共享内存运行时，如 Qthreads 和 Argobots，以及面向异构硬件的 StarPU<sup>[18](https://link.springer.com/article/10.1007/s11227-018-2238-4)</sup>；分布式设计将任务与全局地址空间模型结合，包括 Chapel 和 X10（专门设计的语言）以及 HPX 和 Charm++（库实现的异步运行时）。<sup>[18](https://link.springer.com/article/10.1007/s11227-018-2238-4)</sup> 数据流运行时如 StarPU、PaRSEC 和 OmpSs-2 将模型扩展到异构架构，在 CPU 和 GPU 之间实现自动资源管理。<sup>[19](https://arxiv.org/html/2602.21897v1)</sup> 在 C++ 中，标准化委员会的 P2300R10（2024 年）提出了一个标准 C++ 异步模型，建立在三个抽象——调度器、发送者和接收者——以及一组可定制的异步算法之上。<sup>[20](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2300r10.html)</sup>

## 应用

基于任务的实现适合递归划分的树状模式：分治和分支限界并行模式是经典案例。<sup>[7](https://link.springer.com/chapter/10.1007/979-8-8688-1270-5_6)</sup> 在异构系统上，应用被表示为 DAG，其节点是主机任务或加速器内核，运行时在任务的输入数据就绪时立即调度该任务，重叠计算、通信和 I/O。<sup>[19](https://arxiv.org/html/2602.21897v1)</sup> 任务并行与数据并行的混合兼顾两种风格的优势：对于均衡的分治树，在数据并行和任务并行之间进行一次简单的切换就能获得一般混合并行的大部分好处，而当通信缓慢或处理器数量很大时，混合并行表现最佳。<sup>[1](https://www.netlib.org/lapack/lawnspdf/lawn97.pdf)</sup>

## 局限与替代方案

任务并行的失效模式始于粒度和开销，上文已量化。<sup>[8](https://www.osti.gov/servlets/purl/1808781)</sup> 负载不均衡由穿过最慢任务的关键路径决定，程序员通过拆分慢任务或在内部将其并行化来解决瓶颈。<sup>[21](https://patterns.eecs.berkeley.edu/?page_id=609)</sup> 任务图的无环特性消除了任务之间死锁的可能性，前提是任务确实具有原子性；隐藏的依赖关系仍可能导致死锁或竞态。<sup>[21](https://patterns.eecs.berkeley.edu/?page_id=609)</sup> 任务化还会遭遇更差的 NUCA/NUMA 行为，因为操作同一数据块的任务可能在不同插槽和芯片粒上的不同线程中执行<sup>[9](https://upcommons.upc.edu/server/api/core/bitstreams/d0a5db2a-2efc-489c-b1a5-e6dbe50bec1f/content)</sup>；贪心调度可能导致函数在与其被调用时不同的线程上返回，破坏线程局部存储，并可能破坏某些互斥锁实现。<sup>[6](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3872.pdf)</sup>

调度器行为依赖于应用。对六种运行时（Intel Cilk、Intel TBB、Intel OpenMP、GCC OpenMP、Qthreads 和 HPX）在四种派生模式下的比较发现，除 Qthreads 外，调度器对应用结构高度敏感，没有哪种运行时在所有情况下都提供最佳性能。<sup>[22](https://www.osti.gov/servlets/purl/1088054)</sup> 与替代方案相比：流水线具有固定的线性形状，关注数据流而非任务依赖关系；主/工作者和分治使用子/父关系而非前驱/后继关系。<sup>[21](https://patterns.eecs.berkeley.edu/?page_id=609)</sup> 对于大规模的不规则、自适应或数据驱动工作负载，异步多任务（AMT）运行时——包括 Charm++、HPX、Legion、PaRSEC、Uintah、Chapel 和 StarPU——通过支持动态任务生成、显式依赖管理和重叠计算与通信的异步执行，解决了 MPI+X 等批量同步模型的局限。<sup>[3](https://doi.org/10.1145/3840389)</sup>

## References

1. [Mixed task and data parallelism (LAPACK Working Note 97)](https://www.netlib.org/lapack/lawnspdf/lawn97.pdf)
2. [Task Parallelism and Data Distribution: An Overview of Explicit Parallel Programming Languages](https://hgpu.org/?p=8404)
3. [Joseph Schuchart and colleagues (2026). A Survey of Distributed Asynchronous Many-Task Models and Their Applications. ACM Computing Surveys.](https://doi.org/10.1145/3840389)
4. [Introduction to task-based parallelism (HPC2N training documentation)](https://hpc2n.github.io/Task-based-parallelism/branch/master/motivation/)
5. [Cilk: An Efficient Multithreaded Runtime System (Blumofe, Joerg, Kuszmaul, Leiserson, Randall, Zhou)](https://people.csail.mit.edu/bradley/papers/BlumofeJoKu96.pdf)
6. [A Primer on Scheduling Fork-Join Parallelism with Work Stealing (WG21 N3872)](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3872.pdf)
7. [Tasks and Task Group (TBB book chapter, Springer)](https://link.springer.com/chapter/10.1007/979-8-8688-1270-5_6)
8. [Evaluating the Efficiency of OpenMP Tasking for Unbalanced Computation on Diverse CPU Architectures](https://www.osti.gov/servlets/purl/1808781)
9. [On the Benefits of Tasking with OpenMP](https://upcommons.upc.edu/server/api/core/bitstreams/d0a5db2a-2efc-489c-b1a5-e6dbe50bec1f/content)
10. [Scheduling Multithreaded Computations by Work Stealing (Journal of the ACM, 1999)](https://dl.acm.org/doi/pdf/10.1145/324133.324234)
11. [Task-based Programming Models in HPC](https://www.nm.ifi.lmu.de/teaching/Seminare/2017ws/HPCTrends/doc/final/Schneider_Task-based.Programming.in.HPC.pdf)
12. [A Current Task-Based Programming Paradigms Analysis](https://pmc.ncbi.nlm.nih.gov/articles/PMC7302835/)
13. [Intel oneTBB Developer Guide: Task-Based Programming](https://www.intel.com/content/www/us/en/docs/onetbb/developer-guide-api-reference/2022-1/task-based-programming.html)
14. [Robert D. Blumofe, Charles E. Leiserson (1999). Scheduling multithreaded computations by work stealing. Journal of the ACM.](https://doi.org/10.1145/324133.324234)
15. [Robert D. Blumofe and colleagues (1996). Cilk: An Efficient Multithreaded Runtime System. Journal of Parallel and Distributed Computing.](https://doi.org/10.1006/jpdc.1996.0107)
16. [Cilk: Efficient Multithreaded Computing (MIT LCS Technical Report / PhD thesis)](https://dspace.mit.edu/bitstream/handle/1721.1/149833/MIT-LCS-TR-701.pdf)
17. [Argonne National Lab., IL (United States), I Foster, USDOE, Washington, DC (United States) (1996). Task parallelism and high-performance languages. .](https://doi.org/10.2172/205045)
18. [A taxonomy of task-based parallel programming technologies for high-performance computing](https://link.springer.com/article/10.1007/s11227-018-2238-4)
19. [A Task-based Data-flow Methodology for Programming Heterogeneous Systems with Multiple Accelerator APIs](https://arxiv.org/html/2602.21897v1)
20. [P2300R10: std::execution](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2300r10.html)
21. [Task Graph | Our Pattern Language (UC Berkeley)](https://patterns.eecs.berkeley.edu/?page_id=609)
22. [A Comparative Critical Analysis of Modern Task-Parallel Runtimes (Technical Report)](https://www.osti.gov/servlets/purl/1088054)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/task-parallelism

English original: https://www.edgepedia.org/task-parallelism
