问题
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;
}
回答1:
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?
- Coding on insufficient hardware
- Intel AVX intrinsics: any compatibility library out? (emulate at compile time instead of runtime).
来源:https://stackoverflow.com/questions/56621809/getting-illegal-instruction-while-running-a-basic-avx512-code