5

我用 AVX 内部函数编写了一个程序,它使用 Ubuntu 12.4 LTS 和 GCC 4.6 以及以下编译行运行良好: g++ -g -Wall -mavx ProgramName.cc -o ProgramName

当我将编译器更新到 4.7 和 4.8.1 版本以支持 16 位 AVX2 内在函数时,问题就开始了,这在 gcc 4.6 中不受支持

目前,更新的 gcc 版本可以正确编译 AVX 和 AVX2 程序。但是,当我运行程序时,它给了我以下错误:非法指令(核心转储),虽然它在 gcc 4.6 上工作

我的问题是:编译和运行 AVX 和 AVX2 内在函数的完美方法是什么

4

2 回答 2

8

If you tell gcc to use AVX2, it will do so, regardless of whether your CPU supports them or not. That can be useful for cross-compiling or for examining gcc's code generation, but it's not particularly helpful for running programs. If your program crashes with an illegal instruction exception, it is most likely that your CPU does not support the AVX2 extension.

On i386 and x86-64 platforms (and in certain other circumstances), you can specify the gcc option -march=native to generate code for the host machines instruction code. The compiled code might not work on another machine with fewer capabilities, but it should allow you to use all the features of your machine.

While -march=native is a good solution for generating executables, it does not actually help much with writing code; you still need to tailor the instrinsics for the target's architecture, and writing code which can take advantage of CPU features without relying on them gets complicated. I don't know of a good C solution, but there are several C++ template frameworks available.

于 2014-12-24T15:47:15.960 回答
1

升级到 gcc 4.8 可能会引入 AVX512,因此您需要将生成的 instr mix 限制为仅适用于您的机器的 AVX2。

于 2019-04-29T04:29:24.913 回答