Linux上的AVX分段错误 [英] AVX segmentation fault on linux

查看:51
本文介绍了Linux上的AVX分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行此代码,并且在运行该代码时显示分段错误.它编译良好.这是代码.(在Windows上可以正常工作.)

I am trying to run this code and it says segmentation fault when I run it. It compiles good. Here is the code. (It works fine on windows).

#include<iostream>
#include<vector>
#include<immintrin.h>

const int size = 1000000;

std::vector<float>A(size);
std::vector<float>B(size);
std::vector<float>C(size);

void bar(int i){
    const float a = 2.0f;
    __m256 _a = _mm256_broadcast_ss(&a);
    __m256 _A = _mm256_load_ps(&A[0] + i*8);
    __m256 _B = _mm256_load_ps(&B[0] + i*8);
    __m256 _C = _mm256_add_ps(_B, _mm256_mul_ps(_a,_A));
    _mm256_store_ps(&C[0] + i*8, _C);
}


int main(){
    std::fill(A.begin(), A.end(), 1.0f);
    std::fill(B.begin(), B.end(), 2.0f);
    bar(0);

    return 0;
}

编译: g ++ -mavx t2.cpp -o t2

当它遇到第一个AVX指令时,它正在退出.我只希望有人查看我的代码.

It's exiting when it hit the first AVX instruction. I just want someone to review my code.

这是gdb追溯

(gdb) run
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400aea in bar(int) ()
Missing separate debuginfos, use: debuginfo-install glibc-2.17-78.el7.x86_64 libgcc-4.8.3-9.el7.x86_64 libstdc++-4.8.3-9.el7.x86_64
(gdb) bt
#0  0x0000000000400aea in bar(int) ()
#1  0x0000000000400b95 in main ()
(gdb)

推荐答案

这可能是数据对齐问题._mm256_load_ps需要256位(32字节)对齐的内存.std :: vector的默认分配器不满足该要求.您需要提供一个对齐的分配器,或者使用对对齐要求不太严格的另一条指令(例如_mm256_loadu_ps).

It is probably an data alignment issue. _mm256_load_ps requires 256-bit (32-bytes) aligned memory. The default allocator for std::vector doesn't meet that requirement. You'll need to supply an aligned allocator or use another instruction with less stringent alignment requirement (such as _mm256_loadu_ps).

这篇关于Linux上的AVX分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆