Is it Possible to Assign Aligned Memory to a *mxArray?

7 views (last 30 days)
I am trying to use an Intel Intrinsics routine in a mex function (the sandbox function call shown here is just a matrix transpose but the point of this question is not figuring out how to transpose an array in MEX, I am aware I could just mexCallMATLAB(..."transpose") if that was the issue). I am able to manage the memory of the same routine outside mexFunction with _mm_malloc & _mm_free, but when I use it inside mexFunction I get a MATLAB System Error
code snippet from caller / mexFunction:
float *c_data = (float*)_mm_malloc(sizeof(float)*lda*ldb, 64);
float *c_data_T = (float*)_mm_malloc(sizeof(float)*lda*ldb, 64);
transpose_block_SSE4x4(c_data, c_data_T, ldb, lda, lda, ldb, BLOCK_SIZE);
// update the object cdata
mxArray *newCData = mxCreateNumericMatrix(lda, ldb, mxSINGLE_CLASS, mxREAL);
mxSetData(newCData, c_data_T);
mxSetProperty(prhs[0], 0, "CData", newCData);
_mm_free(c_data);
Which fails at mxSetData:
MATLAB crash file:C:\Users\HB69954\AppData\Local\Temp\matlab_crash_dump.19388-1:
------------------------------------------------------------------------
Assertion detected at Sat Mar 30 14:08:27 2019
------------------------------------------------------------------------
Configuration:
Crash Decoding : Disabled - No sandbox or build area path
Crash Mode : continue (default)
Current Graphics Driver: Unknown hardware
Default Encoding : windows-1252
Deployed : false
Graphics card 1 : Intel Corporation ( 0x8086 ) Intel(R) HD Graphics 4600 Version 10.18.10.3540
Graphics card 2 : NVIDIA ( 0x10de ) NVIDIA Quadro K1100M Version 24.21.14.1131
Host Name : MWKS430514
MATLAB Architecture : win64
MATLAB Entitlement ID: 703359
MATLAB Root : C:\Program Files\MATLAB\R2016b
MATLAB Version : 9.1.0.441655 (R2016b)
OpenGL : hardware
Operating System : Microsoft Windows 7 Enterprise
Processor ID : x86 Family 6 Model 60 Stepping 3, GenuineIntel
Virtual Machine : Java 1.7.0_60-b19 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
Window System : Version 6.1 (Build 7601: Service Pack 1)
Fault Count: 1
Assertion in void __cdecl `anonymous-namespace'::error(const struct `anonymous-namespace'::header *const ,const unsigned __int64,const void *const ,const unsigned __int64,const class boost::basic_format<char,struct std::char_traits<char>,class std::allocator<char> > &) at b:\matlab\foundation_libraries\src\fl\mem\alignment.cpp line 323:
The pointer passed to 'vector_check' is invalid
and does not appear to have come from any of the following routines:
vector_malloc, vector_calloc, vector_realloc
mxMalloc*, mxCalloc*, mxRealloc*
This suggests one of the following has happened:
- the pointer has already been freed
- the pointer came from an incompatible allocator (e.g. new, malloc, utMalloc)
- the pointer didn't come from any allocator (e.g. the stack, uninitialized memory)
- a memory corruption destroyed the pointer or its header
THIS DATA IS FOR INTERNAL DIAGNOSTIC PURPOSES ONLY
PID: 19388
NATIVE ALIGNMENT: 16 (0x10)
REQ ALIGNMENT: 32 (0x20)
ALIGNED POINTER: 000000007EDF0080
REQ SIZE: 0 (0)
HEADER ADDRESS: 000000007EDF0070
HEADER SIZE: 16 (0x10)
UNALIGNED POINTER: 000000007EDE1293
HEADER->SIZE: 2128543856 (0x7edf0070)
HEADER->CHECK: 3991793133 (0xedededed)
HEADER->ALIGNMENT: 60909 (0xeded)
HEADER->OFFSET: 60909 (0xeded)
Register State (captured):
RAX = 00000000fe9df2e8 RBX = 00000000fe9df2e8
RCX = 0000000004025880 RDX = 0000000000000000
RSP = 0000000004025800 RBP = 00000000fe943de8
RSI = 00000000fe9df268 RDI = 0000000000000000
R8 = 000007fffffde000 R9 = 000007feed1a0000
R10 = 00000000fe9cf783 R11 = 00000000fe9cf783
R12 = 00000000fe9df2e8 R13 = 0000000004025ec0
R14 = 00000000fe9cf218 R15 = 00000000fe943de8
RIP = 00000000fe80971a EFL = 00000206
CS = 0033 FS = 0053 GS = 002b
Stack Trace (captured):
[ 0] 0x00000000fe804153 C:\Program Files\MATLAB\R2016b\bin\win64\libmwfl.dll+00082259 fl::diag::thread_context::unspecified_bool+00000051
[ 1] 0x00000000fe802a68 C:\Program Files\MATLAB\R2016b\bin\win64\libmwfl.dll+00076392 fl::diag::stacktrace_base::capture+00000024
[ 2] 0x00000000fe80682b C:\Program Files\MATLAB\R2016b\bin\win64\libmwfl.dll+00092203 fl::diag::thread_context::unspecified_bool+00009995
[ 3] 0x00000000fe805737 C:\Program Files\MATLAB\R2016b\bin\win64\libmwfl.dll+00087863 fl::diag::thread_context::unspecified_bool+00005655
[ 4] 0x00000000fe80911b C:\Program Files\MATLAB\R2016b\bin\win64\libmwfl.dll+00102683 fl::diag::terminate+00000059
[ 5] 0x00000000fe8bdba4 C:\Program Files\MATLAB\R2016b\bin\win64\libmwfl.dll+00842660 fl::mem::MObserverBase::realloc_pre+00008564
[ 6] 0x00000000fe8bd038 C:\Program Files\MATLAB\R2016b\bin\win64\libmwfl.dll+00839736 fl::mem::MObserverBase::realloc_pre+00005640
[ 7] 0x0000000180012428 C:\Program Files\MATLAB\R2016b\bin\win64\libmx.dll+00074792 matrix::detail::noninlined::mx_array_api::mxSetIr+00000776
[ 8] 0x000000018000c36d C:\Program Files\MATLAB\R2016b\bin\win64\libmx.dll+00050029 mxArray_tag::set_pdata_untrusted+00000033
[ 9] 0x000007fefa7c5dbf C:\Users\HB69954\Documents\GitHub\heavyOilToolbox\loadProcDasBin\array_mapper.mexw64+00089535
...
PS I built the MEX in Visual Studio so I could trace the errors; relevant VS Settings:
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<TargetExt>.mexw64</TargetExt>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(MATLAB_ROOT)\extern\include;</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<CompileAs>CompileAsC</CompileAs>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(MATLAB_ROOT)\extern\lib\win64\microsoft;</AdditionalLibraryDirectories>
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies);libmx.lib;libmex.lib;libmat.lib;</AdditionalDependencies>
<AdditionalOptions>/export:mexFunction %(AdditionalOptions)</AdditionalOptions>
</Link>
</ItemDefinitionGroup>
What is the correct way to manage alligned memory for mxArrays?

Accepted Answer

Guillaume
Guillaume on 30 Mar 2019
It is most likely that matlab arrays are already aligned but in any case, you can't use your own memory allocator for matlab arrays. You have to use matlab's (mxCalloc).
It's possible that under some circumstances your allocator may work but since matlab's memory management is undocumented (on purpose) it will probably break at the most inopportune time. So, I'm afraid you will have to copy your array in a mxCalloc'ed array.
  2 Comments
Peter Cook
Peter Cook on 30 Mar 2019
Edited: Peter Cook on 2 Apr 2019
I swapped the Intel pointer with a mxMalloc'd pointer and ran the intrinsics function without a crash. I am going to save a few debug dumps and compare the results. I'll let you know in a bit.
Edit: I got lucky once with a 512 x 512 input array, everything else crashed hard. I'll have to wait for future intrinsics support in MATLAB/MEX.
James Tursa
James Tursa on 2 Apr 2019
"... It's possible that under some circumstances your allocator may work ..."
Only if the memory is allocated with a MATLAB API function (mxMalloc and friends). All of the API functions that set pointers into mxArray variables (mxSetData and friends) check to see if the pointers are from a MATLAB allocator function, and if not they crash with an assertion fault.

Sign in to comment.

More Answers (0)

Products


Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!