运行基本的Avx512代码时获取非法指令 [英] Getting Illegal Instruction while running a basic Avx512 code
问题描述
我正在尝试学习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;
}
可能您的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=
选项.
- 在硬件不足的情况下编码
- 英特尔AVX内部函数:是否有兼容的库?(模拟在编译时而不是在运行时).
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?
- Coding on insufficient hardware
- Intel AVX intrinsics: any compatibility library out? (emulate at compile time instead of runtime).
这篇关于运行基本的Avx512代码时获取非法指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!