# 自动并行化

自动并行化是一种编译器技术，它无需手工改写，就能将串行程序转换为并行形式：它分析循环中相互独立的迭代，并生成多线程代码或带注解的代码。它之所以聚焦于循环，是因为程序把执行时间花在那里。<sup>[1](https://liberty.princeton.edu/Publications/asplos20_perspective.pdf)</sup> 其输出因工具而异：一些并行化编译器执行源到源转换，生成经过重构、带有表达并行性的编译指示的源代码<sup>[2](https://engineering.purdue.edu/paramnt/publications/polaris-nofront.pdf)</sup>；而主流编译器在编译过程中直接生成多线程目标代码。已发表的方法分为形式化方法（即源到源转换和自动并行化编译器）和基于人工智能的方法。<sup>[3](https://arxiv.org/html/2409.14771v1)</sup>

| 关键事实 | 详情 |
|---|---|
| 典型输出 | 带并行编译指示的重构源代码，或编译器生成的线程<sup>[2](https://engineering.purdue.edu/paramnt/publications/polaris-nofront.pdf)</sup> |
| 合法性检验 | 如果没有任何依赖跨越迭代，则该循环可并行<sup>[4](http://dimacs.rutgers.edu/~billp/pubs/1405.pdf)</sup> |
| 编译器条件 | 已知迭代次数，没有跳入或跳出循环的跳转，迭代相互独立<sup>[5](https://www.intel.com/content/www/us/en/developer/articles/technical/automatic-parallelization-with-intel-compilers.html)</sup> |
| 常用选项 | 英特尔（Intel）的 `[Q]parallel` 配合 `-par-threshold[n]`（0–100，默认 100）<sup>[5](https://www.intel.com/content/www/us/en/developer/articles/technical/automatic-parallelization-with-intel-compilers.html)</sup><sup> • </sup><sup>[6](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-1/automatic-parallelization.html)</sup> |
| 实测加速比 | Polly：在 24 核上从串行的 17.039 秒缩短到 0.757 秒，约 22.5 倍<sup>[7](https://polly.llvm.org/publications/raghesh-a-masters-thesis.pdf)</sup> |
| 机器学习编译器加速比 | TVM：相比由手工优化库支持的框架，加速 1.2 倍到 3.8 倍<sup>[8](https://ar5iv.labs.arxiv.org/html/1802.04799)</sup> |
| 可用性 | GCC 自 4.3 版起；LLVM 的 Polly 通过 Clang 提供；英特尔编译器<sup>[3](https://arxiv.org/html/2409.14771v1)</sup><sup> • </sup><sup>[5](https://www.intel.com/content/www/us/en/developer/articles/technical/automatic-parallelization-with-intel-compilers.html)</sup> |

## 工作原理

任何自动并行化器的核心都是数据依赖检测机制。<sup>[2](https://engineering.purdue.edu/paramnt/publications/polaris-nofront.pdf)</sup> 如果两个语句实例都访问同一内存位置，且至少有一个访问是写操作，那么它们就是数据依赖的；如果不同迭代中的语句实例之间不存在依赖，则该循环无需同步即可并行执行。<sup>[4](http://dimacs.rutgers.edu/~billp/pubs/1405.pdf)</sup> 用向量化的术语来说，DO 循环嵌套中的某个语句，如果在某次迭代中不需要把它在更早迭代中计算出的值作为输入，就可以被向量化。<sup>[9](https://www.mirrorservice.org/sites/www.bitsavers.org/pdf/ibm/IBM_Journal_of_Research_and_Development/302/ibmrd3002D.pdf)</sup>

基本算法确定语句之间的依赖，构造依赖图，并将不位于该图强连通区域中的语句视为可向量化。<sup>[9](https://www.mirrorservice.org/sites/www.bitsavers.org/pdf/ibm/IBM_Journal_of_Research_and_Development/302/ibmrd3002D.pdf)</sup> 将 FORTRAN 程序转换为向量形式的经典论述精确地定义了依赖，并给出了确定依赖的精确检验方法。<sup>[10](https://dl.acm.org/doi/10.1145/29873.29875)</sup> 艾伦（Allen）和肯尼迪（Kennedy）的算法根据循环是否具有层次等于循环深度的循环携带依赖，将循环标记为 DOALL（并行）循环或 DOSEQ（串行）循环，并用约简层次依赖图对迭代排序。<sup>[11](https://perso.ens-lyon.fr/frederic.vivien/Publications/Chapter-LNCS.pdf)</sup> 随后，被判定为相互独立的指令可以并行执行。<sup>[12](https://www.cs.unb.ca/tech-reports/documents/TR07-188_000.pdf)</sup>

分析栈包括依赖分析、使用-定义分析和[指针分析](https://www.edgepedia.org/zh/pointer-analysis)，而依赖测试可能需要求解关于循环下标的丢番图方程。<sup>[13](https://link.springer.com/book/10.1007/978-3-031-01736-0)</sup> 当依赖会阻碍并行性时，使能变换会消除它们：标量和数组私有化会复制数组或数组片段，使处理器不共享存储<sup>[4](http://dimacs.rutgers.edu/~billp/pubs/1405.pdf)</sup>；归约识别会为结合且可交换的运算扩展存储位置；循环偏斜会重排数组访问，将跨迭代依赖移出内层循环。<sup>[1](https://liberty.princeton.edu/Publications/asplos20_perspective.pdf)</sup>

## 实现方式

使用者通过编译器选项调用并行化器，并让流水线运行：先分析，再进行使循环更适合并行化的变换，面向共享内存多核处理器和向量处理器，而分布式内存机器则单独处理。<sup>[13](https://link.springer.com/book/10.1007/978-3-031-01736-0)</sup> 变换目录包括循环规范化、循环并行化、循环不变量代码提升、循环交换、循环融合与循环分布、条带化与分块、循环展开与展开并合、循环剥离、索引集分裂、标量替换和软件流水线。<sup>[14](https://www.ida.liu.se/~chrke55/courses/ACC/PDF-2023/Depend-Opt-Parallelization.pdf)</sup>

在英特尔编译器中，自动并行化由 `[Q]parallel` 选项触发；该选项识别包含并行性的循环，并将代码分解为线程，无需其他操作。<sup>[6](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-1/automatic-parallelization.html)</sup> `-par-threshold[n]` 选项（0 到 100，默认 100）设定循环并行化必须达到的预估收益门槛；将其降低到 99 可以显著增加被并行化的循环数量。<sup>[5](https://www.intel.com/content/www/us/en/developer/articles/technical/automatic-parallelization-with-intel-compilers.html)</sup>

源到源工具提供更多可调参数。ComPar 组合自动并行化器 AutoPar（ROSE）、Par4All（PIPS）和 Cetus，枚举并标注所有循环，并尝试它们的选项与 OpenMP 调度子句的组合，线程数从 2 到 32。<sup>[15](https://www.iwomp.org/wp-content/uploads/iwomp-2020-P19-ComPar.pdf)</sup> ComPar 通过黑盒测试验证每个生成的版本，并拒绝任何未通过正确性检验的编译器与选项组合。<sup>[15](https://www.iwomp.org/wp-content/uploads/iwomp-2020-P19-ComPar.pdf)</sup>

## 起源

向量处理器于 20 世纪 70 年代中期引入，用于提高高端科学超级计算机的计算能力；它们使用流水线算术单元，并通过按元素施加的向量指令使这些单元保持忙碌。<sup>[16](http://softlib.rice.edu/pub/CRPC-TRs/reports/CRPC-TR93364.pdf)</sup> [自动向量化](https://www.edgepedia.org/zh/automatic-vectorization)正是为应对为此类硬件编程的挑战而发展起来的；IBM 向量化器的设计建立在肯尼迪、艾伦及其同事的[依赖分析](https://www.edgepedia.org/zh/dependence-analysis)工作之上，而这些工作又建立在库克（Kuck）和班纳吉（Banerjee）更早的工作之上。<sup>[9](https://www.mirrorservice.org/sites/www.bitsavers.org/pdf/ibm/IBM_Journal_of_Research_and_Development/302/ibmrd3002D.pdf)</sup> 伊利诺伊（Illinois）的一项早期并行化编译器实现基于 PARAFRASE 编译器。<sup>[17](https://rsim.cs.uiuc.edu/arch/qual_papers/compilers/allen87.pdf)</sup> Parafrase-2 中用于并行化编译器的符号分析框架由穆罕默德·R·哈吉加特（Mohammad R. Haghighat）和康斯坦丁·D·波利赫罗诺普洛斯（Constantine D. Polychronopoulos）于 1995 年描述。<sup>[18](https://doi.org/10.1007/b102246)</sup> Polly 在 LLVM 的低级中间表示上执行多面体优化，由托比亚斯·格罗塞尔（Tobias Grosser）、阿明·格罗斯林格（Armin Groesslinger）和克里斯蒂安·伦高尔（Christian Lengauer）于 2012 年在《Parallel Processing Letters》上报告。<sup>[19](https://doi.org/10.1142/s0129626412500107)</sup> GCC 4.3.0 于 2008 年 3 月 5 日发布；自动并行支持在 2009 年被概述，其基础是为 GCC 4.4 合并的 Graphite 框架。<sup>[3](https://arxiv.org/html/2409.14771v1)</sup>

## 变体

**自动向量化**面向向量硬件：向量化器识别可向量化的语句和循环，选择能产生最快执行的向量化方案，并编译优化的向量目标代码。<sup>[9](https://www.mirrorservice.org/sites/www.bitsavers.org/pdf/ibm/IBM_Journal_of_Research_and_Development/302/ibmrd3002D.pdf)</sup> **多面体编译**将循环嵌套表示为多面体；对于每个操作，编译器确定执行日期（调度）、处理器分配和内存地址，然后生成并行代码。<sup>[20](https://www.cs.colostate.edu/~cs560/Spring2011/Notes/PolyModelChapter.pdf)</sup> Polly 的依赖分析实现在 isl 库的数据流分析之上，根据多面体描述计算精确的、非传递的流依赖、输出依赖和反依赖。<sup>[21](https://www.infosun.fim.uni-passau.de/publications/docs/GGL2012ppl.pdf)</sup> **推测式与运行时并行化**处理控制流或依赖模式取决于输入数据的循环：检查器/执行器方案和推测执行在运行时发现并行性。<sup>[4](http://dimacs.rutgers.edu/~billp/pubs/1405.pdf)</sup> **机器学习编译器卸载**将自动优化应用于深度学习工作负载：TVM 结合张量表达式语言、图重写器和基于机器学习的成本模型，该模型随着从硬件后端收集的数据增多而改进，面向 CPU、GPU 和 FPGA 加速器。<sup>[8](https://ar5iv.labs.arxiv.org/html/1802.04799)</sup>

近期发展扩展了这项技术。AutoParLLM 使用 GNN 引导的上下文生成，借助大语言模型进行零样本代码并行化。<sup>[22](https://aclanthology.org/2025.naacl-long.593.pdf)</sup> Polygeist 将 C/C++ 转换为多面体 MLIR，并使用 Pluto 调度器进行[循环分块](https://www.edgepedia.org/zh/loop-tiling)和并行化，将 `scop.parallel` 属性转换为 OpenMP 并行循环。<sup>[23](https://easychair.org/publications/preprint/Kp3L/download)</sup> 并行语义程序依赖图（PS-PDG）扩展程序依赖图，以表示来自开发者方案和编译器自身分析的并行语义；基于它构建的 LLVM 编译器 GINO 在 8 个 NAS 基准测试、56 核上，最多比开发者原始并行执行方案快 46.6%，平均快 15%。<sup>[24](https://2026.cgo.org/details/cgo-2026-papers/27/The-Parallel-Semantics-Program-Dependence-Graph-for-Parallel-Optimization)</sup> `pcall` 调用约定是一种默认串行、但之后可动态提升为完全并行执行的调用。<sup>[25](https://www.cs.cmu.edu/~swestric/24/popl24-par-manage.pdf)</sup>

## 应用

并行化编译器用于科学计算 Fortran 代码：Polaris 将 Fortran 转换为经过重构、带有编译指示注解的 Fortran，并且是同类工具中可免费获得的最先进工具之一。<sup>[2](https://engineering.purdue.edu/paramnt/publications/polaris-nofront.pdf)</sup> 主流编译器，包括 GCC、LLVM（通过 Polly 和 Clang）以及英特尔的编译器，都将自动并行化作为一种可选程序变换提供，用户必须显式启用；Polly 在标准 Clang 构建中通常不启用。<sup>[3](https://arxiv.org/html/2409.14771v1)</sup> 诸如 ComPar 的源到源工具链面向 C、C++、Fortran 和 CUDA C 生成 OpenMP。<sup>[15](https://www.iwomp.org/wp-content/uploads/iwomp-2020-P19-ComPar.pdf)</sup> 机器学习编译器将同样的思想应用于深度学习图的算子调度和硬件卸载。<sup>[8](https://ar5iv.labs.arxiv.org/html/1802.04799)</sup>

加速比取决于每个循环的工作量、负载均衡以及线程创建和同步开销；对于给定循环，加速比通常低于线程数的线性增长，线程数默认等于逻辑核数，可通过 `OMP_NUM_THREADS` 设置。<sup>[5](https://www.intel.com/content/www/us/en/developer/articles/technical/automatic-parallelization-with-intel-compilers.html)</sup> 已发表的比较表明，在有利情况下结果可与手工工作相竞争。Polly 在一台 24 核 AMD 机器上将某个基准测试从串行的 17.039 秒缩短到 0.757 秒，略优于手工 GCC 并行化的 0.796 秒；在 Intel Core 2 Duo 上达到 3.32 秒，而手工版本为 3.50 秒。<sup>[7](https://polly.llvm.org/publications/raghesh-a-masters-thesis.pdf)</sup> 一种选择 OpenMP 构造并对变量进行分类的依赖驱动方法，在 49 个基准测试中的大多数上生成的代码比三种最先进的并行化工具更快，平均加速比为 1.8 到 2.7；在已经并行的程序中重新分类变量，可将执行时间最多改善 29%。<sup>[26](https://dl.acm.org/doi/10.1145/3330345.3330375)</sup>

## 局限与替代方案

自动方法必须克服的主要挑战是粒度问题：找到足够大的并行区域，以补偿并行执行开销；这在 [MIMD](https://www.edgepedia.org/zh/mimd) 处理器上尤其重要，而向量化可以利用细粒度内层循环并行性。<sup>[16](http://softlib.rice.edu/pub/CRPC-TRs/reports/CRPC-TR93364.pdf)</sup> 别名是一种常见障碍：编译器可能无法确定两个指针或数组引用是否指向同一内存位置，而 `restrict` 关键字或 `-restrict`/`-Qrestrict` 选项可以断言不存在别名。<sup>[5](https://www.intel.com/content/www/us/en/developer/articles/technical/automatic-parallelization-with-intel-compilers.html)</sup> 英特尔编译器仅在进入循环前已知迭代次数、没有跳入或跳出循环的跳转、且迭代相互独立时，才会并行化一个循环。<sup>[5](https://www.intel.com/content/www/us/en/developer/articles/technical/automatic-parallelization-with-intel-compilers.html)</sup> 适合并行机器的粗粒度并行循环很可能包含子例程调用，用户手工消除这些调用会过于繁重。<sup>[16](http://softlib.rice.edu/pub/CRPC-TRs/reports/CRPC-TR93364.pdf)</sup> Polly 无法并行化基准测试 'adi' 和 'seidel'，因为其依赖驱动检测无法将这些基准测试的内核识别为并行。<sup>[7](https://polly.llvm.org/publications/raghesh-a-masters-thesis.pdf)</sup> 编译器也常常无法向量化带多面体注解的循环，原因包括未知的迭代次数、多重嵌套循环、函数调用、不规则控制流以及判定向量化无收益的成本模型。<sup>[23](https://easychair.org/publications/preprint/Kp3L/download)</sup> AI 驱动的并行化器会引入自身错误，产生假阳性（例如错误地并行化包含 return 语句的循环）和假阴性（错过机会）。<sup>[3](https://arxiv.org/html/2409.14771v1)</sup>

## References

1. [Perspective: A Sensible Approach to Speculative Automatic Parallelization (ASPLOS 2020)](https://liberty.princeton.edu/Publications/asplos20_perspective.pdf)
2. [Polaris: a parallelizing compiler and research infrastructure](https://engineering.purdue.edu/paramnt/publications/polaris-nofront.pdf)
3. [OMPar: Automatic Parallelization with AI-Driven Source-to-Source Compilation (survey of automatic parallelization in popular compilers)](https://arxiv.org/html/2409.14771v1)
4. [Polaris: Improving the Effectiveness of Parallelizing Compilers](http://dimacs.rutgers.edu/~billp/pubs/1405.pdf)
5. [Automatic Parallelization with Intel® Compilers](https://www.intel.com/content/www/us/en/developer/articles/technical/automatic-parallelization-with-intel-compilers.html)
6. [Automatic Parallelization (Intel Fortran Compiler guide)](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-1/automatic-parallelization.html)
7. [A Framework for Automatic OpenMP Code Generation (Polly)](https://polly.llvm.org/publications/raghesh-a-masters-thesis.pdf)
8. [TVM: An Automated End-to-End Optimizing Compiler for Deep Learning](https://ar5iv.labs.arxiv.org/html/1802.04799)
9. [IBM Journal of Research and Development 30(2), vectorizing compiler paper](https://www.mirrorservice.org/sites/www.bitsavers.org/pdf/ibm/IBM_Journal_of_Research_and_Development/302/ibmrd3002D.pdf)
10. [Automatic translation of FORTRAN programs to vector form](https://dl.acm.org/doi/10.1145/29873.29875)
11. [Chapter 5. Loop Parallelization Algorithms](https://perso.ens-lyon.fr/frederic.vivien/Publications/Chapter-LNCS.pdf)
12. [A Survey of Data Dependence Analysis Techniques for Automated Parallelization](https://www.cs.unb.ca/tech-reports/documents/TR07-188_000.pdf)
13. [Automatic Parallelization: An Overview of Fundamental Compiler Techniques (Springer)](https://link.springer.com/book/10.1007/978-3-031-01736-0)
14. [Optimization and Parallelization of Sequential Programs (lecture notes)](https://www.ida.liu.se/~chrke55/courses/ACC/PDF-2023/Depend-Opt-Parallelization.pdf)
15. [ComPar: Optimized Multi-Compiler for Automatic OpenMP S2S Parallelization](https://www.iwomp.org/wp-content/uploads/iwomp-2020-P19-ComPar.pdf)
16. [Rice CRPC technical report CRPC-TR93364 (historical review of automatic vectorization/parallelization)](http://softlib.rice.edu/pub/CRPC-TRs/reports/CRPC-TR93364.pdf)
17. [Allen 1987 compiler paper (University of Illinois)](https://rsim.cs.uiuc.edu/arch/qual_papers/compilers/allen87.pdf)
18. [Mohammad R. Haghighat, Constantine D. Polychronopoulos (1995). Symbolic Analysis for Parallelizing Compilers. .](https://doi.org/10.1007/b102246)
19. [TOBIAS GROSSER, ARMIN GROESSLINGER, CHRISTIAN LENGAUER (2012). POLLY, PERFORMING POLYHEDRAL OPTIMIZATIONS ON A LOW-LEVEL INTERMEDIATE REPRESENTATION. Parallel Processing Letters.](https://doi.org/10.1142/s0129626412500107)
20. [Dependence Analysis and Parallelizing Transformations (polyhedral model)](https://www.cs.colostate.edu/~cs560/Spring2011/Notes/PolyModelChapter.pdf)
21. [Polly - polyhedral optimization for LLVM (dependence analysis)](https://www.infosun.fim.uni-passau.de/publications/docs/GGL2012ppl.pdf)
22. [AutoParLLM: GNN-guided Context Generation for Zero-Shot Code Parallelization using LLMs (NAACL 2025)](https://aclanthology.org/2025.naacl-long.593.pdf)
23. [Extending Polygeist to Generate OpenMP SIMD and GPU MLIR Code](https://easychair.org/publications/preprint/Kp3L/download)
24. [The Parallel-Semantics Program Dependence Graph for Parallel Optimization (CGO 2026)](https://2026.cgo.org/details/cgo-2026-papers/27/The-Parallel-Semantics-Program-Dependence-Graph-for-Parallel-Optimization)
25. [Automatic Parallelism Management (POPL 2024)](https://www.cs.cmu.edu/~swestric/24/popl24-par-manage.pdf)
26. [Automatic construct selection and variable classification in OpenMP](https://dl.acm.org/doi/10.1145/3330345.3330375)

---
*Initially written Sep 29, 2026 · Reviewed: Sep 30, 2026 · Edited: Sep 30, 2026 · Last review: Sep 30, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/automatic-parallelization

English original: https://www.edgepedia.org/automatic-parallelization
