1

in a quick attampt, I tried to store AVX2-data (type __m256i) in a vector:

__m256i values= _mm256_set_epi32(2, 4, 6, 8, 10, 12, 14, 16);

std::vector< __m256i > vecValues;

for ( int k = 0; k < static_cast< int >(100); k++ )
{
    vecValues.push_back( values);
}

Using VS2012, this works in debug mode, but give an "Unhandled exception / access violation" when running in release mode.

Can someone explain, why, and how to store the data correctly?

S.H
  • 875
  • 2
  • 11
  • 27

1 Answers1

4

The __m256i type guarantees that a specific alignment will be respected when allocating space for the value on the stack. However, that alignment annotation is ignored* when you put them in a std::vector. You will need to use a custom allocator that respects the alignment requirements. That will likely involve the use of the _mm_malloc intrinsic or calling _aligned_malloc in MSVC.

__m256i requires a 32-byte alignment, because it represents the 256-bit AVX2 registers. By default, MSVC gives you only an 8-byte alignment (for 32-bit builds) or 16-byte alignment (for 64-bit builds), which is insufficient. This is causing the access violation crash when you try to run the build.

Donny-Dont has shared a Gist that provides an example of how you might write a custom alignment-aware allocator for storing SIMD types in an STL container. You should be adapt this to your own purposes. More examples can be found in the answers to this Stack Overflow question.

* GCC will emit a compile-time warning for this:
warning: ignoring attributes on template argument '__m256i {aka __vector(4) long long int}'
if you use the -Wignored-attributes option (which is implied by -Wall). I see no such warning in Clang, however.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574