我的云服务器上的底层架构最近发生了变化,看起来像
我在使用 gcc -march=native
(Ubuntu 14.04, gcc 4.8)
它过去总是在 16 核 Intel Xeon E5-2650 v2 上运行,现在有时(我猜这取决于可用性)它会改用速度快得多的 Xeon E5-2650 v3。
二进制有效,但现在看起来无锁线程同步代码中存在奇怪的未定义行为,而它以前可以 100% 工作。我唯一能想到的是代码以某种方式为 v3 编译并在 v2 上运行(或相反)并且两者之间存在一些不兼容。
我宁愿以后避免这种情况。有没有一种好方法可以检测为错误架构编译的二进制文件?
编辑:我检查过,gcc 文档对 -march=native
非常清楚:
-march=cpu-type
Generate instructions for the machine type cpu-type. In contrast to -mtune=cpu-type, which merely tunes the generated code for the specified cpu-type, -march=cpu-type allows GCC to generate code that may not run at all on processors other than the one indicated. Specifying -march=cpu-type implies -mtune=cpu-type.‘native’
This selects the CPU to generate code for at compilation time by determining the processor type of the compiling machine. Using -march=native enables all instruction subsets supported by the local machine (hence the result might not run on different machines).
从 gcc -march=native
底层使用的标志来看,v2 和 v3 之间的差异看起来相当大(strings prg | grep march
如果 prg 是用编译的调试符号)。 v3 添加了所有这些并更改了 l2-cache-size:
-mabm -mavx2 -mbmi -mbmi2 -mfma -mlzcnt -mmovbe
如果使用这些新指令集中的任何一个,这就像为 MMX 编译一些东西并期望它在非 MMX arch 上运行......
最佳答案
GCC 将为它使用的架构定义一个预定义的宏 - 例如 -march=core2
将定义一个宏 __core2
。您可以在您的程序中包含一个命令行选项,该选项使用这些宏来显示编译它的架构 - 在这种情况下,您想要检查 __ivybridge
和 __haswell
.
或者(或同时),您可以让程序显示用于编译它的编译器标志(通过让您的构建系统向程序提供这些标志)。不过,如果您使用的是 -march=native
,那将无济于事。
听起来您的问题更有可能是程序中的潜在错误,特别是因为它涉及线程同步 - 较新的机器可能更积极地重新排序内存访问,这暴露了您的无锁算法中缺少的障碍。
关于c - gcc -march= native 。检测为错误架构构建的二进制文件的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36724853/