运行MATLAB/MEX文件的性能不一致

运行MATLAB/MEX文件的性能不一致,matlab,mex,Matlab,Mex,我已经写了一个MEX文件。此文件编译成功,没有问题。出于某些原因,我真的很困惑,这个MEX文件不再正常工作。一切都没有改变。我使用相同的MEX代码和相同的参数来执行MEX文件。但它失败了。我卸载并重新安装了MATLAB,但没有任何帮助 我跟踪了这个问题,当它到达mexCallMATLAB时,似乎会导致崩溃 mxArray *x_locations = ( cellCentered ) ? mxCreateDoubleMatrix( 1, NZ-1, mxREAL ) : mxCreateDoub

我已经写了一个
MEX
文件。此文件编译成功,没有问题。出于某些原因,我真的很困惑,这个
MEX
文件不再正常工作。一切都没有改变。我使用相同的
MEX
代码和相同的参数来执行
MEX
文件。但它失败了。我卸载并重新安装了
MATLAB
,但没有任何帮助

我跟踪了这个问题,当它到达
mexCallMATLAB
时,似乎会导致崩溃

mxArray *x_locations = ( cellCentered ) ? mxCreateDoubleMatrix( 1, NZ-1, mxREAL ) : mxCreateDoubleMatrix( 1, NZ, mxREAL );
mxArray *y_locations = ( cellCentered ) ? mxCreateDoubleMatrix( 1, NY-1, mxREAL ) : mxCreateDoubleMatrix( 1, NY, mxREAL );
mxArray *z_locations = ( cellCentered ) ? mxCreateDoubleMatrix( 1, NX-1, mxREAL ) : mxCreateDoubleMatrix( 1, NX, mxREAL );

// A loop for filling the values for x_locations, y_locations, z_locations 
// ...
// End of the loop

// Create a meshgrid based on the values stored in x_locations, y_locations, z_locations 
mxArray *IN[3];
mxArray *OUT[3];
IN[0] = x_locations;
IN[1] = y_locations;
IN[2] = z_locations;
mexPrintf("Line Before mexCallMATLAB\n");
mexCallMATLAB(3, OUT, 3, IN, "meshgrid");
mexPrintf("Line After mexCallMATLAB\n");
当我运行MEX文件时,只打印mexCallMATLAB之前的
行,并且代码不会到达mexCallMATLAB
之后的
行。这是日志

MATLAB crash file:C:\Users\A\AppData\Local\Temp\matlab_crash_dump.7712-1:


------------------------------------------------------------------------
       Segmentation violation detected at Wed Jun 25 15:51:21 2014
------------------------------------------------------------------------

Configuration:
  Crash Decoding  : Disabled
  Default Encoding: windows-1252
  MATLAB Root     : C:\Program Files\MATLAB\R2012b
  MATLAB Version  : 8.0.0.783 (R2012b)
  Operating System: Microsoft Windows 7
  Processor ID    : x86 Family 6 Model 26 Stepping 5, GenuineIntel
  Virtual Machine : Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
  Window System   : Version 6.1 (Build 7601: Service Pack 1)

Fault Count: 1


Abnormal termination:
Segmentation violation

Register State (from fault):
  RAX = 0000000060de7cc0  RBX = 000009a28967bdb0
  RCX = 000000ffffffffff  RDX = 0000000060ea08e0
  RSP = 0000000004106360  RBP = 0000000000000000
  RSI = 0000000006fa0000  RDI = 0000000060ea08f0

   R8 = 0000000060ea08f0   R9 = 32525c4501cd000a
  R10 = 00000000166f3660  R11 = 00000000740faf60
  R12 = 0000000004106610  R13 = 00000000041064e0
  R14 = 00000000002dc6c0  R15 = 0000000000000000

  RIP = 0000000076d33290  EFL = 00010202

   CS = 0033   FS = 0053   GS = 002b

Stack Trace (from fault):
[  0] 0x0000000076d33290                      C:\Windows\SYSTEM32\ntdll.dll+00340624 ( RtlFreeHeap+000208 )
[  1] 0x0000000076be1a4a                   C:\Windows\system32\kernel32.dll+00137802 ( HeapFree+000010 )
[  2] 0x00000000725acabc C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_08e61857a83bc251\MSVCR90.dll+00314044 ( free+000028 )
[  3] 0x0000000075a23075 C:\Program Files\MATLAB\R2012b\bin\win64\libmx.dll+00012405 ( mxFree+000037 )
[  4] 0x0000000075a84b9d C:\Program Files\MATLAB\R2012b\bin\win64\libmx.dll+00412573 ( mxSubscriptedReference+000621 )
[  5] 0x000007fee4475709 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00087817 ( inIs_ws_variable+000329 )
[  6] 0x000007fee44c8cce C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00429262 ( inFunctionHandleInterface::DestroyWorkspace+241806 )
[  7] 0x000007fee44d2978 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00469368 ( inFunctionHandleInterface::DestroyWorkspace+281912 )
[  8] 0x000007fee44d5a8b C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00481931 ( inFunctionHandleInterface::DestroyWorkspace+294475 )
[  9] 0x000007fee44bde1f C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00384543 ( inFunctionHandleInterface::DestroyWorkspace+197087 )
[ 10] 0x000007fee44bdd80 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00384384 ( inFunctionHandleInterface::DestroyWorkspace+196928 )
[ 11] 0x000007fee44e197d C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00530813 ( inFunctionHandleInterface::DestroyWorkspace+343357 )
[ 12] 0x000007fee44e0271 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00524913 ( inFunctionHandleInterface::DestroyWorkspace+337457 )
[ 13] 0x000007fee44c20e9 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00401641 ( inFunctionHandleInterface::DestroyWorkspace+214185 )
[ 14] 0x0000000074e8c80d C:\Program Files\MATLAB\R2012b\bin\win64\m_dispatcher.dll+00051213 ( Mfh_file::dispatch_fh+000509 )
[ 15] 0x0000000074e8d0d6 C:\Program Files\MATLAB\R2012b\bin\win64\m_dispatcher.dll+00053462 ( Mfunction_handle::dispatch+000854 )
[ 16] 0x000007fee4482cd8 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00142552 ( MathWorks::MException::MExceptionEnvelope::MExceptionEnvelope+003336 )
[ 17] 0x000007fee453beb2 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00900786 ( inMexCallMATLAB+000738 )
[ 18] 0x000007fee453bb9e C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00899998 ( IInterpreter::GetInterpreterBridge+148286 )
[ 19] 0x000007fee453bcaf C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00900271 ( inMexCallMATLAB+000223 )
[ 20] 0x0000000061ce6989 C:\Program Files\MATLAB\R2012b\bin\win64\libmex.dll+00027017 ( mexCallMATLAB+000073 )
[ 21] 0x0000000009b02272 D:\Files\mymexfile.mexw64+00008818 ( mexFunction+004722 )
[ 22] 0x0000000061cf0f83 C:\Program Files\MATLAB\R2012b\bin\win64\libmex.dll+00069507 ( mexRunMexFile+000131 )
[ 23] 0x0000000061cefce2 C:\Program Files\MATLAB\R2012b\bin\win64\libmex.dll+00064738 ( inSwapMexfileReader+000226 )
[ 24] 0x0000000061cefe64 C:\Program Files\MATLAB\R2012b\bin\win64\libmex.dll+00065124 ( inSwapMexfileReader+000612 )
[ 25] 0x0000000074e8c80d C:\Program Files\MATLAB\R2012b\bin\win64\m_dispatcher.dll+00051213 ( Mfh_file::dispatch_fh+000509 )
[ 26] 0x0000000074e8d0d6 C:\Program Files\MATLAB\R2012b\bin\win64\m_dispatcher.dll+00053462 ( Mfunction_handle::dispatch+000854 )
[ 27] 0x000007fee44b210b C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00336139 ( inFunctionHandleInterface::DestroyWorkspace+148683 )
[ 28] 0x000007fee44c3edf C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00409311 ( inFunctionHandleInterface::DestroyWorkspace+221855 )
[ 29] 0x000007fee44e0ae8 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00527080 ( inFunctionHandleInterface::DestroyWorkspace+339624 )
[ 30] 0x000007fee44c8d94 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00429460 ( inFunctionHandleInterface::DestroyWorkspace+242004 )
[ 31] 0x000007fee44d2978 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00469368 ( inFunctionHandleInterface::DestroyWorkspace+281912 )
[ 32] 0x000007fee44e18c1 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00530625 ( inFunctionHandleInterface::DestroyWorkspace+343169 )
[ 33] 0x000007fee44e0271 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00524913 ( inFunctionHandleInterface::DestroyWorkspace+337457 )
[ 34] 0x000007fee44c20e9 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00401641 ( inFunctionHandleInterface::DestroyWorkspace+214185 )
[ 35] 0x0000000074e8c80d C:\Program Files\MATLAB\R2012b\bin\win64\m_dispatcher.dll+00051213 ( Mfh_file::dispatch_fh+000509 )
[ 36] 0x0000000074e8d0d6 C:\Program Files\MATLAB\R2012b\bin\win64\m_dispatcher.dll+00053462 ( Mfunction_handle::dispatch+000854 )
[ 37] 0x000007fee45751e5 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+01135077 ( inValidateLoadedObject+001893 )
[ 38] 0x000007fee4584595 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+01197461 ( init_cleaner+015541 )
[ 39] 0x000007fee4587b75 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+01211253 ( inSetMaxFrameCount+002261 )
[ 40] 0x000007fee4587b48 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+01211208 ( inSetMaxFrameCount+002216 )
[ 41] 0x000007fee4587b07 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+01211143 ( inSetMaxFrameCount+002151 )
[ 42] 0x000007fee4470213 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+00066067 ( inPathNotification::function_clear_notification+004019 )
[ 43] 0x000007fee457aed0 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+01158864 ( inEvalCmdWithLocalReturn+000416 )
[ 44] 0x000007fee457ae2c C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+01158700 ( inEvalCmdWithLocalReturn+000252 )
[ 45] 0x000007fee457ad71 C:\Program Files\MATLAB\R2012b\bin\win64\m_interpreter.dll+01158513 ( inEvalCmdWithLocalReturn+000065 )
[ 46] 0x0000000079fbf90d C:\Program Files\MATLAB\R2012b\bin\win64\libmwbridge.dll+00063757 ( mnDebugPrompt+000109 )
[ 47] 0x0000000079fc0471 C:\Program Files\MATLAB\R2012b\bin\win64\libmwbridge.dll+00066673 ( mnParser+000593 )
[ 48] 0x00000000749a64a4   C:\Program Files\MATLAB\R2012b\bin\win64\mcr.dll+00222372 ( mcrInstance::mnParser_on_interpreter_thread+000036 )
[ 49] 0x000000007498681f   C:\Program Files\MATLAB\R2012b\bin\win64\mcr.dll+00092191 ( mcr::setInterpreterThreadToCurrent+029887 )
[ 50] 0x0000000074986881   C:\Program Files\MATLAB\R2012b\bin\win64\mcr.dll+00092289 ( mcr::setInterpreterThreadToCurrent+029985 )
[ 51] 0x00000000063fe0b6   C:\Program Files\MATLAB\R2012b\bin\win64\uiw.dll+00385206 ( UIW_IsUserMessage+000086 )
[ 52] 0x00000000063fe7b9   C:\Program Files\MATLAB\R2012b\bin\win64\uiw.dll+00387001 ( UIW_SetGLIMUserMsg+000153 )
[ 53] 0x0000000076ae87fe                     C:\Windows\system32\USER32.dll+00165886 ( GetMenuBarInfo+000638 )
[ 54] 0x0000000076acf5fb                     C:\Windows\system32\USER32.dll+00062971 ( SystemParametersInfoW+000235 )
[ 55] 0x0000000076ad4895                     C:\Windows\system32\USER32.dll+00084117 ( IsProcessDPIAware+000465 )
[ 56] 0x0000000076d311f5                      C:\Windows\SYSTEM32\ntdll.dll+00332277 ( KiUserCallbackDispatcher+000031 )
[ 57] 0x0000000076ad908a                     C:\Windows\system32\USER32.dll+00102538 ( PeekMessageW+000186 )
[ 58] 0x0000000076ad50fe                     C:\Windows\system32\USER32.dll+00086270 ( GetKeyState+000238 )
[ 59] 0x0000000076ad3a6f                     C:\Windows\system32\USER32.dll+00080495 ( PeekMessageA+000087 )
[ 60] 0x00000000063d8f4b   C:\Program Files\MATLAB\R2012b\bin\win64\uiw.dll+00233291 ( UIW_SetCurrentDialog+000731 )
[ 61] 0x00000000063ffbe2   C:\Program Files\MATLAB\R2012b\bin\win64\uiw.dll+00392162 ( ws_FreeSystemFont+000546 )
[ 62] 0x000000000485e6a3 C:\Program Files\MATLAB\R2012b\bin\win64\libmwservices.dll+00779939 ( sysq::removeCommitDatabaseUpdatesHook+002131 )
[ 63] 0x000000000485fe18 C:\Program Files\MATLAB\R2012b\bin\win64\libmwservices.dll+00785944 ( sysq::removeCommitDatabaseUpdatesHook+008136 )
[ 64] 0x0000000004861a20 C:\Program Files\MATLAB\R2012b\bin\win64\libmwservices.dll+00793120 ( sysq::addProcessPendingEventsUnitTestHook+000240 )
[ 65] 0x0000000004861ae8 C:\Program Files\MATLAB\R2012b\bin\win64\libmwservices.dll+00793320 ( sysq::addProcessPendingEventsUnitTestHook+000440 )
[ 66] 0x0000000004861ca3 C:\Program Files\MATLAB\R2012b\bin\win64\libmwservices.dll+00793763 ( sysq::addProcessPendingEventsUnitTestHook+000883 )
[ 67] 0x0000000004861d76 C:\Program Files\MATLAB\R2012b\bin\win64\libmwservices.dll+00793974 ( sysq::addProcessPendingEventsUnitTestHook+001094 )
[ 68] 0x000000007498837e   C:\Program Files\MATLAB\R2012b\bin\win64\mcr.dll+00099198 ( mcr::setInterpreterThreadToCurrent+036894 )
[ 69] 0x0000000074988665   C:\Program Files\MATLAB\R2012b\bin\win64\mcr.dll+00099941 ( mcr::setInterpreterThreadToCurrent+037637 )
[ 70] 0x000000007497b166   C:\Program Files\MATLAB\R2012b\bin\win64\mcr.dll+00045414 ( DepfunRules::postWarning+003430 )
[ 71] 0x000000013fa48a33 C:\Program Files\MATLAB\R2012b\bin\win64\MATLAB.exe+00035379 ( ???+000000 )
[ 72] 0x000000013fa48c97 C:\Program Files\MATLAB\R2012b\bin\win64\MATLAB.exe+00035991 ( ???+000000 )
[ 73] 0x000000013fa49163 C:\Program Files\MATLAB\R2012b\bin\win64\MATLAB.exe+00037219 ( ???+000000 )
[ 74] 0x0000000076bd59ed                   C:\Windows\system32\kernel32.dll+00088557 ( BaseThreadInitThunk+000013 )
[ 75] 0x0000000076d0c541                      C:\Windows\SYSTEM32\ntdll.dll+00181569 ( RtlUserThreadStart+000033 )

有人能帮你吗?

你真的希望有人能帮你,而不知道你要传递给
mexCallMATLAB
?请发布
输入
输出
是什么,以及您是如何创建它们的。您的代码看起来不错。您的注释似乎表明该代码在循环中被重复调用?您是否在循环中调用
mxDestroyArray
,以销毁所有正在创建的mxArray(包括
IN
OUT
)?如果没有,那么可能是内存不足?实际上,循环后将调用
mexCallMATLAB
。我的困惑是,它过去工作得很好。我的机器内存约为
48GB
。这可能是因为
MATLAB
本身吗?哦,好的,我现在明白了。可能在填充3个输入mxArray时出现错误,最终损坏了
mxArray
结构,从而在
mexCallMATLAB
尝试读取时导致崩溃?如果不看代码的其余部分,很难确定。但是考虑到崩溃发生在一个MEX函数中,在怀疑MATLAB之前,我会花更多的时间仔细研究代码?NX,还是NX-1?它们是否应该反转,以便用NZ-1值初始化x_位置?