字符数组 __attribute__ 对齐
char array __attribute__ aligned
我对以下代码行有疑问:
char buffer[256] __attribute__((aligned(4096)));
全局数组 "buffer" 的内容是字符串,我从标准输入中获取。
我已阅读 https://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Type-Attributes.html(gcc/gnu 在线文档)。我知道这个属性以字节为单位指定了变量的最小对齐方式。
我的问题是为什么我需要对 char 数组进行这样的对齐?
只是因为性能原因?
也许使用常量不是最好的主意,至少在没有很好地解释目标的情况下(看起来示例的页面大小是 4096)。一些架构有特定的指令来复制大块内存(例如整个页面),这可能会更快地完成这个过程:
GCC also provides a target specific macro BIGGEST_ALIGNMENT, which is the largest
alignment ever used for any data type on the target machine you are compiling for. For
example, you could write:
short array[3] attribute ((aligned (BIGGEST_ALIGNMENT)));
The compiler automatically sets the alignment for the declared variable or field to
BIGGEST_ALIGNMENT. Doing this can often make copy operations more efficient,
because the compiler can use whatever instructions copy the biggest chunks of memory when performing copies to or from the variables or fields that you have aligned this way. Note that the value of BIGGEST_ALIGNMENT may change depending on command-line options.
[...]
Note that the effectiveness of aligned attributes may be limited by inherent limitations in your linker. On many systems, the linker is only able to arrange for variables to be aligned up to a certain maximum alignment. (For some linkers, the maximum supported alignment may be very very small.) If your linker is only able to align variables up to a maximum of 8-byte alignment, then specifying aligned(16) in an attribute still only provides you with 8-byte alignment. See your linker documentation for further information.
为了完整起见,要保证数组设置在一页中,以提高性能。
此 link 解释了对齐页面数据(在这种情况下,使用 aligned_malloc
)如何改进代码:https://software.intel.com/en-us/articles/getting-the-most-from-opencl-12-how-to-increase-performance-by-minimizing-buffer-copies-on-intel-processor-graphics
我猜某个特定的硬件希望它以这种方式对齐。 (大容量存储 reader、DMA 等)。
我必须说很少见如此大的对齐。
我对以下代码行有疑问:
char buffer[256] __attribute__((aligned(4096)));
全局数组 "buffer" 的内容是字符串,我从标准输入中获取。 我已阅读 https://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Type-Attributes.html(gcc/gnu 在线文档)。我知道这个属性以字节为单位指定了变量的最小对齐方式。
我的问题是为什么我需要对 char 数组进行这样的对齐?
只是因为性能原因?
也许使用常量不是最好的主意,至少在没有很好地解释目标的情况下(看起来示例的页面大小是 4096)。一些架构有特定的指令来复制大块内存(例如整个页面),这可能会更快地完成这个过程:
GCC also provides a target specific macro BIGGEST_ALIGNMENT, which is the largest alignment ever used for any data type on the target machine you are compiling for. For example, you could write: short array[3] attribute ((aligned (BIGGEST_ALIGNMENT)));
The compiler automatically sets the alignment for the declared variable or field to BIGGEST_ALIGNMENT. Doing this can often make copy operations more efficient, because the compiler can use whatever instructions copy the biggest chunks of memory when performing copies to or from the variables or fields that you have aligned this way. Note that the value of BIGGEST_ALIGNMENT may change depending on command-line options.
[...]
Note that the effectiveness of aligned attributes may be limited by inherent limitations in your linker. On many systems, the linker is only able to arrange for variables to be aligned up to a certain maximum alignment. (For some linkers, the maximum supported alignment may be very very small.) If your linker is only able to align variables up to a maximum of 8-byte alignment, then specifying aligned(16) in an attribute still only provides you with 8-byte alignment. See your linker documentation for further information.
为了完整起见,要保证数组设置在一页中,以提高性能。
此 link 解释了对齐页面数据(在这种情况下,使用 aligned_malloc
)如何改进代码:https://software.intel.com/en-us/articles/getting-the-most-from-opencl-12-how-to-increase-performance-by-minimizing-buffer-copies-on-intel-processor-graphics
我猜某个特定的硬件希望它以这种方式对齐。 (大容量存储 reader、DMA 等)。 我必须说很少见如此大的对齐。