运行基本AVX512代码时获得非法指令

发布于 2025-01-17 14:44:13 字数 924 浏览 4 评论 0 原文

我正在尝试学习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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恏ㄋ傷疤忘ㄋ疼 2025-01-24 14:44:13

您的CPU可能根本不支持AVX512。
仅这些新一代的CPU支持AVX-512:


编译器选项

使用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制作二进制文件,而不仅仅是您要编译的机器:


相关:

  • 硬件不足上的编码
  • =“ https://stackoverflow.com/questions/53637886/53637886/coding-on-insuffitife-hardware AVX Intrinsics-任何兼容库式 - 兼容性“> intel avx intrinsics:compatibility库?(在编译时仿真而不是运行时)。

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 an always_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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文