运行基本AVX512代码时获得非法指令
我正在尝试学习AVX说明,并且在运行基本代码时会收到
非法指令(核心倾倒)
代码将在下面提到,我正在使用
g ++ -mavx512f 1.cpp
到底是什么问题以及如何克服它? 谢谢你!
#include <immintrin.h>
#include<iostream>
using namespace std;
void add(const float a[], const float b[], float res[], int n)
{
int i = 0;
for(; i < (n&(~0x31)) ; i+=32 )
{
__m512 x = _mm512_loadu_ps( &a[i] );
__m512 y = _mm512_loadu_ps( &b[i] );
__m512 z = _mm512_add_ps(x,y);
_mm512_stream_ps(&res[i],z);
}
for(; i<n; i++) res[i] = a[i] + b[i];
}
int main()
{
int n = 100000;
float a[n], b[n], res[n];
for(int i = 0;i < n; i++)
{
a[i] = i;
b[i] = i+10;
}
add(a,b,res,n);
for(int i=0;i<n;i++) cout<<res[i]<<" ";
cout<<endl;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的CPU可能根本不支持AVX512。
仅这些新一代的CPU支持AVX-512:
wikipedia有一张漂亮的表格(包括诸如AVX512VBMI或FP16之类的特征的故障)
zen 4(及以后)。
服务器/工作站:(“ Xeon可伸缩性能”),后来,
客户端:冰湖1035G4和火箭湖桌面
(也非常有限的释放 1 。
不是奥尔德湖(第12代);英特尔对其AVX-512的支持进行了回归,并正在积极阻止人们在硅中使用AVX-512支持,该硅最初可在Ecores中使用。
。
Intel Client CPU最终将能够再次利用其硅中存在的AVX-512硬件, avx10 ,这使他们能够以所有多汁的功能(例如遮罩,
vpternlogd
,,更好的洗牌,32个矢量,32矢量)曝光AVX-512的256位矢量宽度子集寄存器,广播内存 - 源操作数等。在2023年宣布没有关于客户cpus支持它的消息(这需要他们在e内核上实施)。我以为MicroCode更新可以报告现有AVX-512 CPU上的AVX10.1/512支持,因为它在Evex Prefix中均未添加任何新事物(FP圆形模式在512以外的矢量宽度中覆盖-bit),但显然是花岗岩急流将是第一个支持Avx10.1的人。Xeon Phi Compute卡,第二代,然后( nofollow noreferrer“> knight's Landing的着陆)。 /p>
编译器选项
使用clang或
g ++ -o3 -march =本机
启用CPU支持的所有内容。如果收到编译错误(例如Undeclared函数
_MM512_LOADU_PS
),您的CPU确实支持支持AVX512,因此G ++没有启用它。 (或您要尝试使用的任何其他CPU功能。)inmintrin.h
仍然可以定义固有的,并使用__属性__((ewlance_inline,target,target(“ avx512f”))
))))因此。是关于eways_inline
函数的失败(__________ia32 _...
)的函数(__ /code>和
-mtune =
选项,如果要为其他CPU制作二进制文件,而不仅仅是您要编译的机器:相关:
MSVC和ICC do 让您在不告诉编译器目标支持它们的情况下使用内在信息,因此,这种针对CPU检查代码的方法与这些编译器不起作用。他们会很乐意让您编译当前CPU上不会运行的代码。 (因为MSVC假设您将进行运行时CPU检测和派遣,而不是分发源代码以供每个人优化自己的机器。)
更多信息
有关无avx-512 Intel 处理器名称/数字含义
skylake-client do -512,只有Skylake-Server。
intel alder lake hybrid(big.little) //www.tomshardware.com/news/intel-alder-lake-specifications-price-benchmarks-release-date“ rel =“ nofollow noreferrer”>都不会具有avx-512 ,即使在大核心。
直到Gracemont(Alder Lake E-cores)之前,像Silvermont / Tremont这样的低功率CPU甚至没有AVX1。
另请注意,AVX-512有多个扩展名,例如AVX-512VPOPCNTDQ,它引入了SIMD指令来计数每个SIMD元素中的设置位。检查 wikipedia's cpus with avx-512 cpus >查看哪个CPU有什么。 AVX-512F是“基础”,AVX-512VL允许在128和256位向量上使用凉爽的新说明。
脚注1:pentium/celeron版本的较旧的英特尔CPU甚至没有AVX,只有SSE4.2。 (也缺少BMI1/2,因为它们禁用了Vex前缀的解码)。
Probably your CPU doesn't support AVX512 at all.
Only CPUs of these and newer generations support AVX-512:
Wikipedia has a nice table (including breakdown by which feature, like AVX512VBMI or FP16)
Zen 4 (and later).
Server/workstation: Skylake-SP ("Xeon Scalable Performance") and later,
Skylake-X high-end desktop/workstation.
Client: Ice Lake / Tiger Lake e.g. i5-1035G4, and Rocket Lake desktop, e.g. i5-11600.
(Also the very-limited-release Cannon Lake laptop chip)
Celeron / Pentium versions of these have AVX2 but not AVX-5121.
Not Alder Lake (12th gen); Intel regressed their AVX-512 support, and are actively blocking people from using the AVX-512 support in the silicon, which was initially usable with the E-cores disabled.
Intel Client CPUs will eventually be able to once again take advantage of the AVX-512 hardware that exists in their silicon with AVX10, which allows them to expose the 256-bit vector-width subset of AVX-512 with all the juicy features like masking,
vpternlogd
, better shuffles, 32 vector registers, broadcast memory-source operands, etc. Announced in 2023, still no word on client CPUs supporting it (which would require them to implement it on E cores). I'd have thought microcode updates could report AVX10.1/512 support on existing AVX-512 CPUs, since it doesn't add anything new until AVX10.2 (FP rounding-mode overrides in the EVEX prefix for vector widths other than 512-bit), but instead apparently Granite Rapids will be the first to support AVX10.1.Xeon Phi compute cards, 2nd gen and later (Knight's Landing).
Compiler options
Use clang or
g++ -O3 -march=native
to enable everything your CPU supports.If you get compile errors (like undeclared function
_mm512_loadu_ps
), your CPU does not support AVX512 so g++ didn't enable it. (Or whatever other CPU feature you're trying to use.)immintrin.h
would still define the intrinsic, with__attribute__((always_inline,target("avx512f"))
. So it's required to inline, but can only do so into functions that are themselves using__attribute__((target("avx512f"))
or a similar pragma, or command line options. That's why the error message is about inlining failed for analways_inline
function (the intrinsic wrapper around the__builtin_ia32_...
) into a function with incompatible target options.Only use separate
-mavx512f
and-mtune=
options if you want to make a binary for other CPUs, not just the machine you're compiling on. Related:Related: How to test AVX-512 instructions w/o supported hardware?
MSVC and ICC do let you use intrinsics without telling the compiler the target supports them, so this method of checking your code against the CPU doesn't work with those compilers. They'll happily let you compile code that won't run on the current CPU. (Because MSVC assumes you're going to do runtime CPU detection and dispatching, instead of distributing source code for everyone to optimize for their own machine.)
More about CPUs without AVX-512
Intel processor name/number meanings
Skylake-client does not have AVX-512, only Skylake-server.
Intel Alder Lake hybrid (big.LITTLE) CPUs won't have AVX-512, only AVX2 even on the big cores.
Low-power CPUs like Silvermont / Tremont don't even have AVX1, until Gracemont (Alder Lake E-cores).
Also note, there are multiple extensions to AVX-512, like AVX-512VPOPCNTDQ that introduces SIMD instructions to count set bits in each SIMD element. Check Wikipedia's CPUs with AVX-512 table to see which CPU has what. AVX-512F is the "foundation", and AVX-512VL allows using cool new instructions on 128 and 256-bit vectors.
Footnote 1: Pentium/Celeron versions of older Intel CPUs don't even have AVX, just SSE4.2. (Also lacking BMI1/2 because they disabled decoding of VEX prefixes).