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?
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.
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.
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.
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; }
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