对齐缓存行并了解缓存行大小 [英] Aligning to cache line and knowing the cache line size

查看:32
本文介绍了对齐缓存行并了解缓存行大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了防止错误共享,我想将数组的每个元素与缓存行对齐.所以首先我需要知道缓存行的大小,所以我为每个元素分配了一定数量的字节.其次,我希望数组的开头与缓存行对齐.

To prevent false sharing, I want to align each element of an array to a cache line. So first I need to know the size of a cache line, so I assign each element that amount of bytes. Secondly I want the start of the array to be aligned to a cache line.

我使用的是 Linux 和 8 核 x86 平台.首先我如何找到缓存行大小.其次,我如何与 C 中的缓存行对齐.我正在使用 gcc 编译器.

I am using Linux and 8-core x86 platform. First how do I find the cache line size. Secondly, how do I align to a cache line in C. I am using the gcc compiler.

因此结构如下,假设缓存线大小为 64.

So the structure would be following for example, assuming a cache line size of 64.

element[0] occupies bytes 0-63
element[1] occupies bytes 64-127
element[2] occupies bytes 128-191

依此类推,当然假设 0-63 与缓存行对齐.

and so on, assuming of-course that 0-63 is aligned to a cache line.

推荐答案

要知道尺寸,您需要使用处理器的文档来查找,因为没有程序化的方法可以做到.然而,从好的方面来说,大多数缓存线都是基于英特尔标准的标准大小.在 x86 缓存线上是 64 字节,但是,为了防止错误共享,您需要遵循您所针对的处理器的指南(英特尔对其基于 netburst 的处理器有一些特殊说明),通常您需要为此对齐到 64 字节(英特尔声明您还应该避免跨越 16 字节边界).

To know the sizes, you need to look it up using the documentation for the processor, afaik there is no programatic way to do it. On the plus side however, most cache lines are of a standard size, based on intels standards. On x86 cache lines are 64 bytes, however, to prevent false sharing, you need to follow the guidelines of the processor you are targeting (intel has some special notes on its netburst based processors), generally you need to align to 64 bytes for this (intel states that you should also avoid crossing 16 byte boundries).

要在 C 或 C++ 中执行此操作,您需要使用标准的 aligned_alloc 函数或编译器特定说明符之一,例如 __attribute__((align(64)))__declspec(align(64)).要在结构中的成员之间填充以将它们拆分到不同的缓存行,您需要插入一个足够大的成员以将其与下一个 64 字节边界对齐

To do this in C or C++ requires that you use the standard aligned_alloc function or one of the compiler specific specifiers such as __attribute__((align(64))) or __declspec(align(64)). To pad between members in a struct to split them onto different cache lines, you need on insert a member big enough to align it to the next 64 byte boundery

这篇关于对齐缓存行并了解缓存行大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆