I want to create a MEX function for MATLAB 2014a which uses AVX intrinsics, and the memory used needs to be 32 byte aligned.
Looking at the documentation for the mex memory functions I can't see any specific alignment which is provided by mxCalloc or mxMalloc.
However, running the following example under MATLAB 2014a 64-bit in Linux:
#include <mex.h>#include <matrix.h>void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ int len; void *data; size_t data_addr; size_t alignment; alignment = 0; for (len = 1; len < 0x10001; len++) { data = mxMalloc (len); data_addr = (size_t) data; alignment |= data_addr; } mexPrintf ("mxMalloc alignment=%lx\n", alignment); alignment = 0; for (len = 1; len < 0x10001; len++) { data = mxCalloc (len, 1); data_addr = (size_t) data; alignment |= data_addr; } mexPrintf ("mxCalloc alignment=%lx\n", alignment); }
Appears to show that the addresses returned by mxMalloc and mxCalloc have 32 byte alignment (in that the least significant 5 bits of the addresses are always zero):
>> mex c_align_test.cBuilding with 'gcc'.MEX completed successfully.>> c_align_testmxMalloc alignment=7f85ffffffe0mxCalloc alignment=7f85ffffffe0
Is the guaranteed memory alignment provided by mxCalloc and mxAlloc documented anywhere?
Best Answer