更新时间:2023-11-26 13:39:52
可能您的CPU根本不支持AVX512.到目前为止,几乎只有服务器芯片支持它(例如skylake-server,Cascade湖和至强皮皮).也是英特尔放弃的限量发行的 Cannon Lake笔记本电脑芯片; Ice Lake计划成为第一个支持AVX512的客户端CPU.另请参阅Wikipedia的带有AVX-512的CPU 表.
使用g++ -O3 -march=native
启用CPU支持的所有功能.
如果遇到编译错误(例如未声明的函数_mm512_loadu_ps
),则您的CPU 不不支持AVX512,因此g ++并未启用它,因此immintrin.h
不会定义该内在函数./p>
(或者另一个可能的错误是内联"目标选项不允许的内置函数的错误.)
如果要为其他CPU(而不只是要编译的计算机)制作二进制文件,请仅使用单独的-mavx512f
和-mtune=
选项.
I am trying to learn AVX instructions and while running a basic code I recieve
Illegal instruction (core dumped)
The code is mentioned below and I am compiling it using
g++ -mavx512f 1.cpp
What exactly is the problem and how to overcome it? Thank You!
#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;
}
Probably your CPU doesn't support AVX512 at all. Mostly only server chips so far support it (like skylake-server, Cascade Lake, and Xeon Phi). Also the very-limited-release Cannon Lake laptop chip that Intel is abandoning; Ice Lake is planned to be the first client CPU that supports AVX512. See also Wikipedia's CPUs with AVX-512 table.
Use 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, so immintrin.h
wouldn't define that intrinsic.
(Or another possible error is error "inlining" a builtin that target options don't allow.)
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: How to test AVX-512 instructions w/o supported hardware?