# 循环分块

循环分块（又称循环阻塞或缓存分块）是一种程序变换，它将循环嵌套的迭代空间划分为若干分块——即一起执行的小块迭代——使得载入快速存储层级的数据在被逐出之前得到复用。计算不再针对单个矩阵元素进行，而是针对子矩阵进行，同样的思路可应用于存储层级的任何层次，包括虚拟内存、缓存、向量寄存器和标量寄存器。<sup>[1](https://www.cse.iitd.ac.in/~nagaraju/TA/2004-2005/SDS-04/project/loop_tiling.pdf)</sup> 分块将程序组织为：将一块数据载入L1缓存，对该块执行所有读写操作，丢弃该块，然后载入下一块。<sup>[2](https://csapp.cs.cmu.edu/public/waside/waside-blocking.pdf)</sup> 它是稠密线性代数、基于模板的模拟以及将迭代点分组为原子执行分块的编译器框架中的标准局部性优化。<sup>[3](https://www.cs.colostate.edu/~cs553/ClassNotes/lecture16-tiling.ppt.pdf)</sup>

| 关键事实 | 数值 |
|---|---|
| 矩阵乘法中寄存器级和缓存级分块的加速比 | DECStation 3100上为4.3倍；IBM RS/6000上为3.0倍<sup>[1](https://www.cse.iitd.ac.in/~nagaraju/TA/2004-2005/SDS-04/project/loop_tiling.pdf)</sup> |
| 分块三维点积的载入减少量 | 从A·B·N次主存载入降至约(A+B)·N次<sup>[4](https://www.intel.com/content/www/us/en/developer/articles/technical/efficient-use-of-tiling.html)</sup> |
| 矩阵乘法中经典的缺失率最小化分块尺寸 | 对于容量为\( C \)个元素的缓存，\( B = \sqrt{C} \)<sup>[5](https://engineering.purdue.edu/~milind/docs/ieee08.pdf)</sup> |
| 最优固定块尺寸占缓存的比例 | 通常低于10%<sup>[1](https://www.cse.iitd.ac.in/~nagaraju/TA/2004-2005/SDS-04/project/loop_tiling.pdf)</sup> |
| 手动机制 | 条带挖掘加循环重排序<sup>[6](https://teaching.wence.uk/comp52315/notes/tiling/)</sup> |
| 模板代码的收益 | OPS驱动应用中为2倍；编译器模板分块框架平均1.58，最高5.06<sup>[7](https://people.maths.ox.ac.uk/gilesm/files/TPDS_2018.pdf)</sup><sup> • </sup><sup>[8](https://dl.acm.org/doi/10.1145/1034774.1034777)</sup> |

## 工作原理

其机制是通过调度强制实现的时间局部性。缓存行大小因架构而异；数据一旦载入，分块可以改善复用并减少缺失，但缓存冲突和容量压力仍可能在数据被充分利用之前将其逐出。<sup>[9](https://www.intel.com/content/www/us/en/developer/articles/technical/loop-optimizations-where-blocks-are-required.html)</sup> 从数量上看，节省的幅度很大：分块可将所需的载入和存储次数减少至原次数的平方根左右，对于分块到L1和L2的三维点积问题，主存总载入量从A·B·N降至约(A+B)·N，这可能使算法从受限于内存转变为受限于计算。<sup>[4](https://www.intel.com/content/www/us/en/developer/articles/technical/efficient-use-of-tiling.html)</sup>

容量并非唯一的敌人：冲突缺失同样重要。就分块而言，冲突缺失被分类为自冲突缺失（由同一分块的数组元素引起）或交叉冲突缺失（由不同分块的元素引起）。<sup>[10](https://people.cs.rutgers.edu/~uli/JournalOfSupercomputing03.pdf)</sup>

## 实现方式

手动实现分块通过条带挖掘和循环重排序：将嵌套中的每个循环拆分为一个在分块间步进的外层循环和一个在分块内步进的内层循环。<sup>[6](https://teaching.wence.uk/comp52315/notes/tiling/)</sup>

合法性由数据依赖决定。矩形分块在完全可置换循环上是合法的，合法性通过验证变换后的数据依赖为字典序正来检查，从而确保分块之间不存在依赖环。<sup>[3](https://www.cs.colostate.edu/~cs553/ClassNotes/lecture16-tiling.ppt.pdf)</sup> 编译器可自动完成相同步骤。英特尔编译器提供BLOCK_LOOP指令，仅在O3级别启用；使用该指令后，一个示例运行时间为0.45秒，与手动分块代码相当。<sup>[9](https://www.intel.com/content/www/us/en/developer/articles/technical/loop-optimizations-where-blocks-are-required.html)</sup> 多面体编译器如Pluto——一个实用的自动多面体并行化和局部性优化器——可自动生成分块代码。<sup>[11](https://doi.org/10.1145/1379022.1375595)</sup> 在GPU上，分块通过工作组来表达：工作组大小必须与分块大小匹配，以确保输出正确性。<sup>[12](https://rocm-handbook.amd.com/projects/amd-rocm-programming-guide/en/docs-7.2.4/tutorial/hip-performance-optimization/tiling-matrix-multiply.html)</sup>

矩阵乘法的经典结果是：对于容量为\( C \)的缓存，使缺失率最小的分块尺寸\( B \)为\( B = \sqrt{C} \)。<sup>[5](https://engineering.purdue.edu/~milind/docs/ieee08.pdf)</sup> 在Goto风格的dgemm中，限制打包面板大小的往往是TLB可寻址的数据量，而非仅仅是缓存容量，而TLB缺失导致的CPU停顿是预取无法掩盖的。<sup>[13](https://doi.org/10.1145/1356052.1356053)</sup> 一个警示框定了整个问题：使用整个缓存或固定比例缓存的传统智慧是错误的；最优固定块尺寸通常占据不到缓存的10%。<sup>[1](https://www.cse.iitd.ac.in/~nagaraju/TA/2004-2005/SDS-04/project/loop_tiling.pdf)</sup> 由于每次载入都会逐出数据，且虚拟到缓存的映射具有一定随机性，实践者往往通过实验来寻找最佳步长，借助Intel Advisor的Roofline分析或VTune内存分析等性能分析工具。<sup>[4](https://www.intel.com/content/www/us/en/developer/articles/technical/efficient-use-of-tiling.html)</sup><sup> • </sup><sup>[9](https://www.intel.com/content/www/us/en/developer/articles/technical/loop-optimizations-where-blocks-are-required.html)</sup>

## 起源

已发表的记录集中于20世纪90年代一系列关于自动分块的工作。[杰克·唐加拉](https://www.edgepedia.org/zh/jack-dongarra)（Jack Dongarra）和罗伯特·施赖伯（Robert Schreiber）的《嵌套循环的自动分块》（1990年），通过NASA技术报告服务器发表，研究了在一般设定下为分块变换选择近乎最优的变换索引集的问题。<sup>[14](https://www.netlib.org/utk/people/JackDongarra/PAPERS/autoblock.pdf)</sup> 斯蒂芬妮·科尔曼（Stephanie Coleman）和凯瑟琳·S·麦金利（Kathryn S. McKinley）的TSS算法《使用缓存组织和数据布局的分块尺寸选择》（1995年），发表于ACM SIGPLAN Notices，使分块尺寸选择成为编译器需要专门处理的问题。<sup>[15](https://doi.org/10.1145/223428.207162)</sup> 薛京灵（Jingling Xue）的专著《面向并行性的循环分块》（2000年）整合了相关理论。<sup>[16](https://doi.org/10.1007/978-1-4615-4337-4)</sup> 在这些文献中，“分块”（tiling）和“阻塞”（blocking）互换使用，同时还有“缓存分块”和“循环阻塞”等说法。<sup>[6](https://teaching.wence.uk/comp52315/notes/tiling/)</sup>

## 变体

若干有名称的变体处理跨时间步的依赖关系，这是普通矩形分块无法合法分块的。

**时间偏斜**结合了数据域和时间域的分块；旺纳科特（Wonnacott）的表述要求循环嵌套中消费的所有数据值来自前一时间步迭代，且每条语句嵌套在相同数量的循环中。<sup>[17](https://doi.org/10.1023/a:1015460304860)</sup><sup> • </sup><sup>[18](https://dl.acm.org/doi/10.1145/582034.582077)</sup> 一个基于偏斜的迭代模板循环编译器框架以均匀偏斜因子对时间步上的内层循环进行偏斜。<sup>[8](https://dl.acm.org/doi/10.1145/1034774.1034777)</sup> **递归棱柱时间偏斜**将迭代模板迭代空间划分为跨越空间和时间维度的偏斜棱柱，将偏斜与递归分块相结合，并在保证能放入一级缓存的基准尺寸处截断递归。<sup>[18](https://dl.acm.org/doi/10.1145/582034.582077)</sup>

**菱形分块**和**六边形分块**最大化模板计算的并行性；两者密切相关。<sup>[19](https://doi.org/10.1109/tpds.2016.2615094)</sup><sup> • </sup><sup>[20](https://doi.org/10.1142/s0129626414410023)</sup> OPS框架之所以选择**运行时偏斜分块**而非菱形分块，是因为它更易于实现和验证。<sup>[7](https://people.maths.ox.ac.uk/gilesm/files/TPDS_2018.pdf)</sup> **参数化（对等感知）分块**使用PrimeTile以参数化而非编译期常量的分块尺寸对循环进行分块，使分块尺寸能够在运行时响应共运行应用带来的缓存压力。<sup>[21](https://cuihuimin.github.io/papers/Revisiting%20Loop%20Tiling%20for%20Datacenters%20Live%20and%20Let%20Live.pdf)</sup> 在GPU上，**LDS分块**协作地将A和B的分块载入本地数据存储，而**寄存器分块**让每个线程计算一个小的输出分块，通常为4×4，保存在寄存器中。<sup>[12](https://rocm-handbook.amd.com/projects/amd-rocm-programming-guide/en/docs-7.2.4/tutorial/hip-performance-optimization/tiling-matrix-multiply.html)</sup>

## 应用

编译器生成的分块与手写分块之间的差距确实存在，但在性能最高的实现中并不大：英特尔编译器的分块三循环矩阵乘法在安腾2上达到约92%的峰值性能，而手写的Goto BLAS几乎达到99%。<sup>[5](https://engineering.purdue.edu/~milind/docs/ieee08.pdf)</sup>

在模板代码中，Song和Li框架在16个测试程序上实现了平均1.58倍、最高5.06倍的加速。<sup>[8](https://dl.acm.org/doi/10.1145/1034774.1034777)</sup> OPS运行时分块在包括CloverLeaf、TeaLeaf和OpenSBLI在内的大型应用中实现了2倍加速。<sup>[7](https://people.maths.ox.ac.uk/gilesm/files/TPDS_2018.pdf)</sup> 即使简单的转置也能受益：朴素转置在矩阵超出缓存后性能急剧下降，而分块版本可恢复到流带宽的约60%至70%。<sup>[6](https://teaching.wence.uk/comp52315/notes/tiling/)</sup>

## 局限与替代方案

分块有开销和失效模式。对于小数组，额外的循环簿记使分块版本更慢；教科书研究中存在约\( n = 100 \)的交叉点，而在酷睿i7上存在与最佳分块版本相当的未分块矩阵乘法版本，因此分块并非在所有系统上都能提升性能。<sup>[2](https://csapp.cs.cmu.edu/public/waside/waside-blocking.pdf)</sup> 分块启发式在病态数组尺寸下不稳定，会导致糟糕的分块尺寸选择；数组填充是一种互补优化，以额外内存为代价稳定性能。<sup>[10](https://people.cs.rutgers.edu/~uli/JournalOfSupercomputing03.pdf)</sup> 形状极端在两个方向上都有害：极宽的分块可能导致严重的TLB抖动，而极高或方形的分块可能缓存利用率低，小分块则增加每次迭代的循环开销。<sup>[10](https://people.cs.rutgers.edu/~uli/JournalOfSupercomputing03.pdf)</sup> 分块后，子矩阵的读写均不连续，这限制了大矩阵上的性能，除非使用写缓存。<sup>[22](https://tvm.d2l.ai/chapter_cpu_schedules/block_matmul.html)</sup>

在替代方案中，预取隐藏延迟但不减少内存带宽需求，这就是为什么在带宽受限的内核上分块更优。<sup>[1](https://www.cse.iitd.ac.in/~nagaraju/TA/2004-2005/SDS-04/project/loop_tiling.pdf)</sup> 缓存无关的递归算法完全避免针对硬件调优，<sup>[4](https://www.intel.com/content/www/us/en/developer/articles/technical/efficient-use-of-tiling.html)</sup> 但在大多数架构上，递归微内核的性能显著差于迭代微内核，因此对于稠密内核，显式调优的分块往往胜出。<sup>[5](https://engineering.purdue.edu/~milind/docs/ieee08.pdf)</sup> 展开并合并等价于分块后接内层循环展开。<sup>[3](https://www.cs.colostate.edu/~cs553/ClassNotes/lecture16-tiling.ppt.pdf)</sup>

## References

1. [The Cache Performance and Optimizations of Blocked Algorithms (Lam, Rothberg, Wolf, ASPLOS 1991)](https://www.cse.iitd.ac.in/~nagaraju/TA/2004-2005/SDS-04/project/loop_tiling.pdf)
2. [CS:APP2e Web Aside MEM:BLOCKING: Using Blocking to Increase Temporal Locality (Bryant & O'Hallaron)](https://csapp.cs.cmu.edu/public/waside/waside-blocking.pdf)
3. [Tiling: A Data Locality Optimizing Algorithm (CS553 lecture, Colorado State)](https://www.cs.colostate.edu/~cs553/ClassNotes/lecture16-tiling.ppt.pdf)
4. [Efficient use of Tiling (Intel, Bevin Brett)](https://www.intel.com/content/www/us/en/developer/articles/technical/efficient-use-of-tiling.html)
5. [Self-Optimizing Dense Linear Algebra (Yotov et al., Proceedings of the IEEE, May 2008)](https://engineering.purdue.edu/~milind/docs/ieee08.pdf)
6. [Cache blocking/tiling (COMP52315 Performance Engineering, Durham)](https://teaching.wence.uk/comp52315/notes/tiling/)
7. [Loop Tiling in Large-Scale Stencil Codes at Run-Time with OPS (TPDS 2018)](https://people.maths.ox.ac.uk/gilesm/files/TPDS_2018.pdf)
8. [Automatic tiling of iterative stencil loops (Song and Li, ACM)](https://dl.acm.org/doi/10.1145/1034774.1034777)
9. [Loop Optimizations Where Blocks are Required (Intel Developer Articles)](https://www.intel.com/content/www/us/en/developer/articles/technical/loop-optimizations-where-blocks-are-required.html)
10. [newPad: tile selection with array padding (Hsu & Kremer, J. Supercomputing 2003)](https://people.cs.rutgers.edu/~uli/JournalOfSupercomputing03.pdf)
11. [Uday Bondhugula and colleagues (2008). A practical automatic polyhedral parallelizer and locality optimizer. ACM SIGPLAN Notices.](https://doi.org/10.1145/1379022.1375595)
12. [Tiling and reuse: matrix multiplication (AMD ROCm programming guide)](https://rocm-handbook.amd.com/projects/amd-rocm-programming-guide/en/docs-7.2.4/tutorial/hip-performance-optimization/tiling-matrix-multiply.html)
13. [Kazushige Goto, Robert A. van de Geijn (2008). Anatomy of high-performance matrix multiplication. ACM Transactions on Mathematical Software.](https://doi.org/10.1145/1356052.1356053)
14. [Automatic Blocking of Nested Loops (Dongarra et al., Rice/UTK)](https://www.netlib.org/utk/people/JackDongarra/PAPERS/autoblock.pdf)
15. [Stephanie Coleman, Kathryn S. McKinley (1995). Tile size selection using cache organization and data layout. ACM SIGPLAN Notices.](https://doi.org/10.1145/223428.207162)
16. [Jingling Xue (2000). Loop Tiling for Parallelism. .](https://doi.org/10.1007/978-1-4615-4337-4)
17. [David Wonnacott (2002). Achieving Scalable Locality with Time Skewing. International Journal of Parallel Programming.](https://doi.org/10.1023/a:1015460304860)
18. [Increasing temporal locality with skewing and recursive blocking (Jin, Mellor-Crummey, Fowler, SC 2001)](https://dl.acm.org/doi/10.1145/582034.582077)
19. [Uday Bondhugula, Vinayaka Bandishti, Irshad Pananilath (2016). Diamond Tiling: Tiling Techniques to Maximize Parallelism for Stencil Computations. IEEE Transactions on Parallel and Distributed Systems.](https://doi.org/10.1109/tpds.2016.2615094)
20. [Tobias Grosser and colleagues (2014). The Relation Between Diamond Tiling and Hexagonal Tiling. Parallel Processing Letters.](https://doi.org/10.1142/s0129626414410023)
21. [Revisiting Loop Tiling for Datacenters: Live and Let Live (peer-aware tiling)](https://cuihuimin.github.io/papers/Revisiting%20Loop%20Tiling%20for%20Datacenters%20Live%20and%20Let%20Live.pdf)
22. [Improve Cache Efficiency by Blocking (Dive into Deep Learning Compiler, TVM)](https://tvm.d2l.ai/chapter_cpu_schedules/block_matmul.html)

---
*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/loop-tiling

English original: https://www.edgepedia.org/loop-tiling
