gpt4 book ai didi

字符数组 __attribute__ 对齐

转载 作者:太空宇宙 更新时间:2023-11-03 23:59:08 25 4
gpt4 key购买 nike

我对以下代码行有疑问:

char buffer[256] __attribute__((aligned(4096)));

全局数组“缓冲区”的内容是字符串,我从标准输入中获取。我读过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.

为了完整起见,它希望确保数组设置在一页中,以提高性能。

此链接解释了对齐页面数据(在这种情况下,使用 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

关于字符数组 __attribute__ 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51856915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com