1

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/)

user667804
  • 740
  • 6
  • 25
  • Insufficient information. Provide a complete, minimal, reproducible, compilable example demonstrating the problem, instead of randomly chosen snippets of your code. With C++, a bug can result in memory corruption in one part of your code, that doesn't show up until the affected memory is accessed elsewhere, triggering a segfault. Just because you are segfaulting at a particular place, says absolutely nothing about that specific part of the code. Even though you posted minimal useful information, it looks fairly likely that your bug is somewhere else. Welcome to C++. – Sam Varshavchik Jun 04 '15 at 01:22
  • You'll need to post more info in regards to this: `__m256d entry = _mm256_set1_pd(1.0);` .. more specifically, what is a `__m256d` and what does the function `_mm256_set1_pd` look like/do ?? – txtechhelp Jun 04 '15 at 01:29
  • 1
    __mm256d is a standard type in AVX, and _mm256_set1_pd is a standard intrinsic. He tagged the question AVX, and shouldn't need to explain the fundamentals. If you are inexperienced with something, don't blame the poster. – MuertoExcobito Jun 04 '15 at 01:55

2 Answers2

5

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.

Community
  • 1
  • 1
MuertoExcobito
  • 9,741
  • 2
  • 37
  • 78
  • thanks, although that looks like it is going to be painful to implement... Will accept your answer as soon as I get it working on my machine – user667804 Jun 04 '15 at 01:47
  • 1
    AVX actually *doesn't* require aligned data, unless the compiler generates `vmovaps` instead of `vmovups`. Memory operands to AVX non-move instructions don't have to be aligned. Only the aligned versions of the load & store move instructions will segfault. In some ways, it's nice that compilers will save you from the somewhat-slower performance of unaligned data by making it an error, though. – Peter Cordes Jul 08 '15 at 20:40
0

With C++17, std::vector guarantee the elements will be aligned properly.

How is a vector's data aligned?

Bensuperpc
  • 1,275
  • 1
  • 14
  • 21