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)
In a nutshell, segmentation fault refers to errors due to a process's attempts to access memory regions that it shouldn't. When the kernel detects odd memory access behaviors, it terminates the process issuing a segmentation violation signal (SIGSEGV).
Overview. A segmentation fault (aka segfault) is a common condition that causes programs to crash; they are often associated with a file named core . Segfaults are caused by a program trying to read or write an illegal memory location.
On a Unix operating system such as Linux, a "segmentation violation" (also known as "signal 11", "SIGSEGV", "segmentation fault" or, abbreviated, "sig11" or "segfault") is a signal sent by the kernel to a process when the system has detected that the process was attempting to access a memory address that does not ...
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With