Is the memory alignment by mxCalloc and/or mxMalloc defined
Mostrar comentarios más antiguos
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.c
Building with 'gcc'.
MEX completed successfully.
>> c_align_test
mxMalloc alignment=7f85ffffffe0
mxCalloc alignment=7f85ffffffe0
Is the guaranteed memory alignment provided by mxCalloc and mxAlloc documented anywhere?
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!