Cache lines & Cache coherence & False sharing

本文介绍了计算机系统中缓存的基本原理,包括缓存行的概念及其大小、缓存一致性如何维持以及现代处理器采用的MESI协议。此外,还详细探讨了虚假共享问题,即不同线程虽然不共享数据,但因数据位于同一缓存行而引发的竞争问题。
Cache lines

The data in a cache is grouped into blocks called cache-lines, which are typically 64 or 128 bytes wide. These are the smallest units of memory that can be read from, or written to, main memory. This works well in most programs as data that is close in memory is often needed close in time by a particular thread. However, this is the root of the false sharing problem.

Cache coherence

When a program writes a value to memory, it goes firstly to the cache of the core that ran the code. If any other caches hold a copy of that cache line, their copy is marked as invalid and cannot be used. The new value is written to main memory, and the other caches must re-read it from there if they need it. Although this synchronization is implemented in hardware, it still takes time. And, of course, reading from main memory takes a few hundred clock cycles by itself.

Modern processors use the MESI protocol to implement cache coherence. This basically means eachcache line can be in one of four states:

  • M odified
  • E xclusive
  • S hared
  • I nvalid

When a core modifies any data in a cache line, it transitions to "Modified", and any other caches that hold a copy of the same cache line are forced to "Invalid". The other cores must then read the data from main memory next time they need it. That's all I need to say about this here. A detailed explanation is available on Wikipedia[^], if you're interested.

False sharing

Imagine two different variables are being used by two different threads on two different cores. This appears to be embarrassingly parallel, as the different threads are using isolated data. However, if the two variables are located in the same cache line and at least one is being written, then there will be contention for that cache line. This is false sharing.

It is called false sharing because even though the different threads are not sharing data, they are, unintentionally, sharing a cache line.


原文出处:http://www.codeproject.com/Articles/51553/Concurrency-Hazards-False-Sharing

### 缓存一致性的定义与重要性 缓存一致性(Cache Coherence)是指在多处理器或多核系统中,当多个处理器访问共享数据时,确保这些数据在不同处理器的缓存之间保持一致的状态。如果某个处理器修改了一个内存位置的数据,则其他处理器看到的该位置的数据也应反映这一修改[^1]。 在计算机体系结构和多处理器系统中,缓存一致性是一个核心问题,因为现代计算架构通常依赖于高速缓存来提高性能。然而,在多处理器环境中,每个处理器可能拥有自己的私有缓存,这可能导致同一份数据的不同副本存在于不同的缓存中。如果没有有效的机制来维护一致性,程序的行为可能会变得不可预测甚至错误。 --- ### 解决缓存一致性问题的方法 为了应对缓存一致性挑战,设计者提出了多种协议和技术: #### 1. **写入策略** - **Write-through**: 数据被写入到本地缓存的同时也会立即更新到主存储器中。这种方法简单易实现,但会增加主存带宽的压力[^2]。 - **Write-back**: 修改后的数据仅保存在本地缓存中,直到替换算法决定将其刷新回主存为止。这种方式减少了不必要的写操作次数,从而提高了效率。 #### 2. **监听协议(Snooping Protocols)** 这种方法通过让所有处理器监控总线上发生的事务活动来检测潜在冲突并采取相应措施。例如,“MESI”是一种常见的基于状态转换的监听协议,其中每条缓存行可以处于四种状态之一:Modified、Exclusive、Shared 或 Invalid。 #### 3. **目录协议(Directory-Based Protocols)** 不同于监听方式需要广播消息给整个系统的每一个节点,目录协议利用中心化的元数据记录哪些处理单元持有特定地址范围内的拷贝以及它们当前所处的状态。这样能够减少全局通信开销,尤其适合大规模分布式环境下的应用需求。 #### 4. **硬件支持 vs 软件管理** 大多数情况下,缓存一致性由底层硬件自动完成;但在某些特殊场景下也可能借助操作系统或者应用程序层面的支持来进行更精细控制——比如采用Copy-on-write技术以节省实际物理资源消耗[^4]。 --- ### 实际案例分析 考虑Intel Core i7 和 NVIDIA Tesla 的对比情况可以看出两者对于cache coherence有着截然不同的设计理念[^3]: - CPU倾向于强一致模型,即任何时刻读取相同变量都应该返回最新版本; - GPU则允许一定程度上的弱一致性以便换取更高的吞吐量优势(如warps调度延迟隐藏),并且其thread blocks间不存在传统意义上的coherent cache hierarchy关系而是依靠global/shared memories协作完成任务分配执行流程. --- ```c++ // 示例代码展示如何手动同步跨设备间的缓冲区 cudaMemcpy(devPtr, hostPtr, size, cudaMemcpyHostToDevice); kernel<<<blocksPerGrid, threadsPerBlock>>>(devPtr); cudaDeviceSynchronize(); // Ensure all operations are completed before proceeding further. ``` 上述片段体现了即使是在异构计算框架内也需要程序员显式调用API函数保障最终结果准确性不受中间过程干扰影响. ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值