c - gcc -march= native 。检测为错误架构构建的二进制文件的方法?

标签 c linux ubuntu gcc pthreads

我的云服务器上的底层架构最近发生了变化,看起来像 我在使用 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/

相关文章:

c - malloc 与 C 中的数组

.net - 自 0001 年 1 月 1 日起,如何将日/月/年日期转换为 .NET 样式的 100ns 刻度?

regex - 如何仅提取包含子域的 URL

php - 授予执行 .c 文件的权限

c++ - 优化 O_DIRECT 写入

c - 将可变大小的多维 C 数组初始化为零

c# - 我可以在 CentOS 上安装适用于 Linux 的 .NET Core 和 Npgsql 吗?

java - Java 中的 Hello World 程序未运行

django - 无法连接到 WSGI 守护进程 'sampleapp'

c++ - OpenCV C++ 代码在终端中运行良好,但从 Eclipse 运行时出错 - Ubuntu 12.04