Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a vector's data aligned?

If I want to process data in a std::vector with SSE, I need 16 byte alignment. How can I achieve that? Do I need to write my own allocator? Or does the default allocator already align to 16 byte boundaries?

like image 969
fredoverflow Avatar asked Dec 10 '11 11:12

fredoverflow


People also ask

What is meant by aligning the data?

Data alignment: Data alignment means putting the data in memory at an address equal to some multiple of the word size. This increases the performance of the system due to the way the CPU handles memory.

What does it mean to be byte aligned?

A memory address a is said to be n-byte aligned when a is a multiple of n (where n is a power of 2). In this context, a byte is the smallest unit of memory access, i.e. each memory address specifies a different byte.

What is an alignment vector?

An alignment vector is a representation of the difference in retention time between an ion in one of your runs and the equivalent ion in the alignment reference run.


1 Answers

C++ standard requires allocation functions (malloc() and operator new()) to allocate memory suitably aligned for any standard type. As these functions don't receive the alignment requirement as an argument, on practice it means that the alignment for all allocations is the same and is the alignment of a standard type with the largest alignment requirement, which often is long double and/or long long (see boost max_align union).

Vector instructions, such as SSE and AVX, have stronger alignment requirements (16-byte aligned for 128-bit access and 32-byte aligned for 256-bit access) than that provided by the standard C++ allocation functions. posix_memalign() or memalign() can be used to satisfy such allocations with stronger alignment requirements.


In C++17 the allocation functions accept an additional argument of type std::align_val_t.

You can make use of it like:

#include <immintrin.h> #include <memory> #include <new>  int main() {     std::unique_ptr<__m256i[]> arr{new(std::align_val_t{alignof(__m256i)}) __m256i[32]}; } 

Moreover, in C++17 the standard allocators have been updated to respect type's alignment, so you can simply do:

#include <immintrin.h> #include <vector>  int main() {     std::vector<__m256i> arr2(32); } 

Or (no heap allocation involved and supported in C++11):

#include <immintrin.h> #include <array>  int main() {     std::array<__m256i, 32> arr3; } 
like image 145
Maxim Egorushkin Avatar answered Sep 21 '22 19:09

Maxim Egorushkin