问题
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;
}
Compiling: g++ -mavx t2.cpp -o t2
It's exiting when it hit the first AVX instruction. I just want someone to review my code.
Here is gdb back trace
(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)
回答1:
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).
来源:https://stackoverflow.com/questions/33373318/avx-segmentation-fault-on-linux