Search code examples
c++vectorsegmentation-faultintrinsicsavx

Segfault while creating a vector of avx vectors


for my current project I need to create a vector of 256bit AVX vectors. I used

myVector = vector<__m256d>(nrVars(), _mm256_set1_pd(1.0));

which worked fine once but after executing the line twice it gives me a segmentation fault. I was able to come up with the following piece of code

vector<__m256d> temp;
__m256d entry = _mm256_set1_pd(1.0);
temp = vector<__m256d>(10, entry);
temp = vector<__m256d>(10, entry);

that always produces a segmentation fault. Could you explain to me why that is the case and how I can avoid the issue in the future?

Thank you very much!

P.S. Even this would not work:

myVector.clear();
myVector.reserve(nrVars());
for (size_t i=0; i<nrVars(); ++i) {
    myVector[i] = _mm256_set1_pd(1.0);
}

And to answer the comments. This is a complete example that produces a segfault:

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

using namespace std;
int main(int argc, char **argv) {
    vector<__m256d> temp;
    __m256d entry = _mm256_set1_pd(1.0);
    temp = vector<__m256d>(10, entry);
    temp = vector<__m256d>(10, entry);
    return 0;
}

And to read up on m256d and the functions I'm using please take a look at the intel intrinsic website (https://software.intel.com/sites/landingpage/IntrinsicsGuide/)


Solution

  • AVX requires aligned data. vector does not guarantee the elements will be aligned properly. See this question (How is a vector's data aligned?) for a discussion of allocation alignment, specifically with regards to SIMD execution.