There are a few options for acquiring an aligned block of memory but they're very similar and the issue mostly boils down to what language standard and platforms you're targeting.
C11
void * aligned_alloc (size_t alignment, size_t size)
POSIX
int posix_memalign (void **memptr, size_t alignment, size_t size)
Windows
void * _aligned_malloc(size_t size, size_t alignment);
And of course it's also always an option to align by hand.
Intel offers another option.
Intel
void* _mm_malloc (int size, int align)
void _mm_free (void *p)
Based on source code released by Intel, this seems to be the method of allocating aligned memory their engineers prefer but I can't find any documentation comparing it to other methods. The closest I found simply acknowledges that other aligned memory allocation routines exist.
https://software.intel.com/en-us/articles/memory-management-for-optimal-performance-on-intel-xeon-phi-coprocessor-alignment-and
To dynamically allocate a piece of aligned memory, use posix_memalign,
which is supported by GCC as well as the Intel Compiler. The benefit
of using it is that you don’t have to change the memory disposal API.
You can use free() as you always do. But pay attention to the
parameter profile:
??int posix_memalign (void **memptr, size_t align, size_t size);
The Intel Compiler also provides another set of memory allocation
APIs. C/C++ programmers can use _mm_malloc and _mm_free to allocate
and free aligned blocks of memory. For example, the following
statement requests a 64-byte aligned memory block for 8 floating point
elements.
??farray = (float *)__mm_malloc(8*sizeof(float), 64);
Memory that is allocated using _mm_malloc must be freed using
_mm_free. Calling free on memory allocated with _mm_malloc or calling _mm_free on memory allocated with malloc will result in unpredictable behavior.
The clear differences from a user perspective is that _mm_malloc
requires direct CPU and compiler support and memory allocated with _mm_malloc
must be freed with _mm_free
. Given these drawbacks, what is the reason for ever using _mm_malloc?
Can it have a slight performance advantage? Historical accident?
See Question&Answers more detail:
os