c++ - AVX segmentation fault on linux -
i trying run code , says segmentation fault when run it. compiles good. here 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 = 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 hit first avx instruction. want review code.
here gdb 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)
it data alignment issue. _mm256_load_ps requires 256-bit (32-bytes) aligned memory. default allocator std::vector doesn't meet requirement. you'll need supply aligned allocator or use instruction less stringent alignment requirement (such _mm256_loadu_ps).
Comments
Post a Comment