You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Segmentation Faults when Running MEX files in Parallel
1 view (last 30 days)
Show older comments
I am currently running repetitions of an experiment that uses MEX files in MATLAB 2012a and occasionally running into segmentation faults that I cannot understand.
Some information about the faults
- They occur randomly
- They only occur when I run multiple repetitions of my experiment in parallel on a Linux machine using a parfor loop.
- They do not occur when I run the repeitions of my experiment in parallel on my Mac using a parfor loop.
- They do not occur when I run or do they occur whenThey do not occur when I run the repetitions sequentially.
- They appear to occur far less frequently when I run 2 experiments in parallel - as opposed to 12 experiments in parallel.
- They occur in MATLAB 2011b and 2012a...
Some information about my MEX file:
- It is written in C
- It uses the IBM CPLEX 12.4 API (this is thread-safe)
- It was compiled using GCC 4.6.3
My thoughts are that there may be some kind of file access issue. Can anyone shed any light on what might be going on? I'd be happy to provide more information as necessary.
6 Comments
Jill Reese
on 17 Apr 2012
Could you post the code from the parfor loop? Is there a stack trace from the seg fault? What steps did you go through to compile the MEX file?
Berk Ustun
on 18 Apr 2012
The code inside the parfor loop is very basic, and essentially has the following form
results = cell(1,30) % a 30 x 1 cell to store output data
settings = %a 30 x 1 cell with settings for each experiment
parfor i = 1:30
results{i} = myExperiment(settings{i})
end
Berk Ustun
on 18 Apr 2012
I'm including a Stack Trace below:
------------------------------------------------------------------------
Segmentation violation detected at Tue Apr 17 21:40:34 2012
------------------------------------------------------------------------
Configuration:
Crash Decoding : Disabled
Current Visual : None
Default Encoding: UTF-8
GNU C Library : 2.14.90 development
MATLAB Root : /home/software/matlab/matlab-2012a
MATLAB Version : 7.14.0.739 (R2012a)
Operating System: Linux 3.3.1-3.fc16.x86_64 #1 SMP Wed Apr 4 18:08:51 UTC 2012 x86_64
Processor ID : x86 Family 6 Model 44 Stepping 2, GenuineIntel
Virtual Machine : Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
Window System : No active display
Fault Count: 1
Abnormal termination:
Segmentation violation
Register State (from fault):
RAX = 0000000000000000 RBX = 0000000000000011
RCX = 0000000000000010 RDX = 0000000000000011
RSP = 00007f32661f2f08 RBP = 00007f32661f2f80
RSI = 00007f32661f2f90 RDI = 00007f319f9ada40
R8 = 000000000000ffff R9 = 0000000000000060
R10 = 00000032f0089fa0 R11 = 0000000000000011
R12 = 00007f32661f2f90 R13 = 00007f319f9ada40
R14 = 0000000000000050 R15 = 00000032f03b4598
RIP = 00000032f0088acc EFL = 0000000000010283
CS = 0033 FS = 0000 GS = 0000
Stack Trace (from fault):
[ 0] 0x00007f327e62c92e /home/software/matlab/matlab-2012a/bin/glnxa64/libmwfl.so+00370990 _ZN2fl4diag15stacktrace_base7captureERKNS0_14thread_contextEm+000158
[ 1] 0x00007f327e62f7d0 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwfl.so+00382928
[ 2] 0x00007f327e62fb3b /home/software/matlab/matlab-2012a/bin/glnxa64/libmwfl.so+00383803 _ZN2fl4diag13terminate_logEPKcRKNS0_14thread_contextE+000171
[ 3] 0x00007f327d511203 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcr.so+01253891 _ZN2fl4diag13terminate_logEPKcPK8ucontext+000067
[ 4] 0x00007f327d50e0fd /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcr.so+01241341
[ 5] 0x00007f327d50f79d /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcr.so+01247133
[ 6] 0x00007f327d50f925 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcr.so+01247525
[ 7] 0x00007f327d50ff01 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcr.so+01249025
[ 8] 0x00007f327d5103f5 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcr.so+01250293
[ 9] 0x00000032f0c0f500 /lib64/libpthread.so.0+00062720
[ 10] 0x00000032f0088acc /lib64/libc.so.6+00559820
[ 11] 0x00000032f003915a /lib64/libc.so.6+00233818
[ 12] 0x00000032f003903d /lib64/libc.so.6+00233533 putenv+000109
[ 13] 0x00007f31a71fc676 /home/software/cplex/cplex-12.4/cplex/matlab/cplexlink124.mexa64+02860662
[ 14] 0x00007f31a7008434 /home/software/cplex/cplex-12.4/cplex/matlab/cplexlink124.mexa64+00812084
[ 15] 0x00007f31a7007af4 /home/software/cplex/cplex-12.4/cplex/matlab/cplexlink124.mexa64+00809716
[ 16] 0x00007f31a700730c /home/software/cplex/cplex-12.4/cplex/matlab/cplexlink124.mexa64+00807692
[ 17] 0x00007f31a6fc0c09 /home/software/cplex/cplex-12.4/cplex/matlab/cplexlink124.mexa64+00519177
[ 18] 0x00007f31a6fc0146 /home/software/cplex/cplex-12.4/cplex/matlab/cplexlink124.mexa64+00516422 mexFunction+000448
[ 19] 0x00007f3277749cca /home/software/matlab/matlab-2012a/bin/glnxa64/libmex.so+00109770 mexRunMexFile+000090
[ 20] 0x00007f3277745f79 /home/software/matlab/matlab-2012a/bin/glnxa64/libmex.so+00094073
[ 21] 0x00007f3277746de1 /home/software/matlab/matlab-2012a/bin/glnxa64/libmex.so+00097761
[ 22] 0x00007f327d1b9063 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_dispatcher.so+00479331 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000515
[ 23] 0x00007f327ca7f476 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01987702
[ 24] 0x00007f327ca30426 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01664038
[ 25] 0x00007f327ca34be4 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01682404
[ 26] 0x00007f327ca31333 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01667891
[ 27] 0x00007f327ca32037 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01671223
[ 28] 0x00007f327ca9b690 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+02102928
[ 29] 0x00007f327d1b9063 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_dispatcher.so+00479331 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000515
[ 30] 0x00007f3276366a6f /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01522287
[ 31] 0x00007f3276302124 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01110308
[ 32] 0x00007f3276302bab /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01113003
[ 33] 0x00007f3276302ebe /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01113790
[ 34] 0x00007f327630418d /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01118605
[ 35] 0x00007f32763042ad /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01118893
[ 36] 0x00007f327630454c /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01119564 _Z27omConstructObjectWithClientN4mcos9COSNameIDEiPPK11mxArray_tagPKNS_9COSClientE+000476
[ 37] 0x00007f327636f1bd /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01556925
[ 38] 0x00007f32763e9563 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+02057571
[ 39] 0x00007f327d1736a1 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_dispatcher.so+00194209 _ZN13Mfh_MATLAB_fn11dispatch_fhEiPP11mxArray_tagiS2_+000481
[ 40] 0x00007f327ca7f476 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01987702
[ 41] 0x00007f327ca30426 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01664038
[ 42] 0x00007f327ca34be4 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01682404
[ 43] 0x00007f327ca31333 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01667891
[ 44] 0x00007f327ca32037 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01671223
[ 45] 0x00007f327ca9b690 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+02102928
[ 46] 0x00007f327d1b9063 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_dispatcher.so+00479331 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000515
[ 47] 0x00007f3276361388 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01500040
[ 48] 0x00007f3276302c22 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01113122
[ 49] 0x00007f3276302ebe /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01113790
[ 50] 0x00007f3276304c7c /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01121404
[ 51] 0x00007f327634d236 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01417782 _Z19omCallLoadobjMethodRN4mcos15COSInterfacePtrENS_8COSValueENS_14COSDataTypePtrE+000694
[ 52] 0x00007f327634d937 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01419575
[ 53] 0x00007f3276287676 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+00607862
[ 54] 0x00007f327633f0eb /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01360107
[ 55] 0x00007f3276330f41 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01302337
[ 56] 0x00007f32763313fe /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01303550
[ 57] 0x00007f3276332944 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01308996
[ 58] 0x00007f3276332758 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01308504
[ 59] 0x00007f3276332944 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01308996
[ 60] 0x00007f3276332758 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01308504
[ 61] 0x00007f3276332758 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01308504
[ 62] 0x00007f3276332944 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01308996
[ 63] 0x00007f3276334238 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01315384
[ 64] 0x00007f327633f021 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01359905
[ 65] 0x00007f3276330f41 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01302337
[ 66] 0x00007f32763313fe /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01303550
[ 67] 0x00007f3276332944 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01308996
[ 68] 0x00007f3276334238 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01315384
[ 69] 0x00007f327633f021 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01359905
[ 70] 0x00007f3276330f41 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01302337
[ 71] 0x00007f32763313fe /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01303550
[ 72] 0x00007f32763323f8 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcos.so+01307640
[ 73] 0x00007f327e37e654 /home/software/matlab/matlab-2012a/bin/glnxa64/libmx.so+00419412
[ 74] 0x00007f327e38057a /home/software/matlab/matlab-2012a/bin/glnxa64/libmx.so+00427386
[ 75] 0x00007f327e37fb18 /home/software/matlab/matlab-2012a/bin/glnxa64/libmx.so+00424728
[ 76] 0x00007f327e37fb18 /home/software/matlab/matlab-2012a/bin/glnxa64/libmx.so+00424728
[ 77] 0x00007f327e380017 /home/software/matlab/matlab-2012a/bin/glnxa64/libmx.so+00426007
[ 78] 0x00007f327e37fb18 /home/software/matlab/matlab-2012a/bin/glnxa64/libmx.so+00424728
[ 79] 0x00007f327e380017 /home/software/matlab/matlab-2012a/bin/glnxa64/libmx.so+00426007
[ 80] 0x00007f327e380017 /home/software/matlab/matlab-2012a/bin/glnxa64/libmx.so+00426007
[ 81] 0x00007f327e380017 /home/software/matlab/matlab-2012a/bin/glnxa64/libmx.so+00426007
[ 82] 0x00007f327e380017 /home/software/matlab/matlab-2012a/bin/glnxa64/libmx.so+00426007
[ 83] 0x00007f327e380a23 /home/software/matlab/matlab-2012a/bin/glnxa64/libmx.so+00428579 miGetCurrentItem+000387
[ 84] 0x00007f327e3631db /home/software/matlab/matlab-2012a/bin/glnxa64/libmx.so+00307675 mxDeserializeWithTag+000315
[ 85] 0x00007f31bd5ba1e0 /home/software/matlab/matlab-2012a/toolbox/distcomp/distcomp/distcompdeserialize.mexa64+00008672 mexFunction+000080
[ 86] 0x00007f3277749cca /home/software/matlab/matlab-2012a/bin/glnxa64/libmex.so+00109770 mexRunMexFile+000090
[ 87] 0x00007f3277745f79 /home/software/matlab/matlab-2012a/bin/glnxa64/libmex.so+00094073
[ 88] 0x00007f3277746de1 /home/software/matlab/matlab-2012a/bin/glnxa64/libmex.so+00097761
[ 89] 0x00007f327d1b9063 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_dispatcher.so+00479331 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000515
[ 90] 0x00007f327ca7f476 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01987702
[ 91] 0x00007f327ca30426 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01664038
[ 92] 0x00007f327ca34be4 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01682404
[ 93] 0x00007f327ca31333 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01667891
[ 94] 0x00007f327ca32037 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01671223
[ 95] 0x00007f327ca9b690 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+02102928
[ 96] 0x00007f327d1b9063 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_dispatcher.so+00479331 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000515
[ 97] 0x00007f327ca5e65f /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01853023
[ 98] 0x00007f327ca5dd49 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01850697
[ 99] 0x00007f327c9bf2ec /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01200876 inCallFcnWithTrap+000092
[100] 0x00007f327ca24b6b /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01616747
[101] 0x00007f327c9be438 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwm_interpreter.so+01197112 _Z28inCallFcnWithTrapInDesiredWSiPP11mxArray_tagiS1_PKcbP15inWorkSpace_tag+000104
[102] 0x00007f3277db2d79 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwiqm.so+02485625 _ZN3iqm15BaseFEvalPlugin7executeEP15inWorkSpace_tagRN5boost10shared_ptrIN14cmddistributor17IIPCompletedEventEEE+000457
[103] 0x00007f32546bf09d /home/software/matlab/matlab-2012a/bin/glnxa64/libnativejmi.so+00696477 _ZN9nativejmi14JmiFEvalPlugin7executeEP15inWorkSpace_tagRN5boost10shared_ptrIN14cmddistributor17IIPCompletedEventEEE+000173
[104] 0x00007f32546f16e5 /home/software/matlab/matlab-2012a/bin/glnxa64/libnativejmi.so+00902885 _ZN3mcr3mvm27McrSwappingIqmPluginAdapterIN9nativejmi14JmiFEvalPluginEE7executeEP15inWorkSpace_tagRN5boost10shared_ptrIN14cmddistributor17IIPCompletedEventEEE+000629
[105] 0x00007f3277d9b482 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwiqm.so+02389122
[106] 0x00007f3277d8c264 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwiqm.so+02327140
[107] 0x00007f327d77dafc /home/software/matlab/matlab-2012a/bin/glnxa64/libmwbridge.so+00125692 _Z10ioReadLinebP8_IO_FILEPcS1_iPbRKN5boost8optionalIKP15inWorkSpace_tagEEb+000508
[108] 0x00007f327d77e165 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwbridge.so+00127333
[109] 0x00007f327d782d0a /home/software/matlab/matlab-2012a/bin/glnxa64/libmwbridge.so+00146698
[110] 0x00007f327d783165 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwbridge.so+00147813
[111] 0x00007f327d7839ce /home/software/matlab/matlab-2012a/bin/glnxa64/libmwbridge.so+00149966 mnParser+000702
[112] 0x00007f327d4f5de2 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcr.so+01142242 _ZN11mcrInstance30mnParser_on_interpreter_threadEv+000034
[113] 0x00007f327d4d851a /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcr.so+01021210
[114] 0x00007f327d4d8598 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcr.so+01021336
[115] 0x00007f327dd0b053 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwservices.so+00823379
[116] 0x00007f327dd0b315 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwservices.so+00824085
[117] 0x00007f327dd09bc1 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwservices.so+00818113
[118] 0x00007f3273e73605 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwuix.so+00505349
[119] 0x00007f327dd8f9a1 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwservices.so+01366433 _ZSt8for_eachIN9__gnu_cxx17__normal_iteratorIPN5boost8weak_ptrIN4sysq10ws_ppeHookEEESt6vectorIS6_SaIS6_EEEENS4_8during_FIS6_NS2_10shared_ptrIS5_EEEEET0_T_SH_SG_+000081
[120] 0x00007f327dd90aab /home/software/matlab/matlab-2012a/bin/glnxa64/libmwservices.so+01370795
[121] 0x00007f327dd8e5f9 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwservices.so+01361401 _Z25svWS_ProcessPendingEventsiib+000665
[122] 0x00007f327d4d776f /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcr.so+01017711
[123] 0x00007f327d4d7c3b /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcr.so+01018939
[124] 0x00007f327d4d7d97 /home/software/matlab/matlab-2012a/bin/glnxa64/libmwmcr.so+01019287
[125] 0x00000032f0c07d90 /lib64/libpthread.so.0+00032144
[126] 0x00000032f00f0f5d /lib64/libc.so.6+00986973 clone+000109
This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for information
on debugging MEX-files.
If this problem is reproducible, please submit a Service Request via:
http://www.mathworks.com/support/contact_us/
A technical support engineer might contact you with further information.
Thank you for your help.
Edric Ellis
on 18 Apr 2012
Do you see the problem if you manually launch two MATLAB processes and run your MEX file? The workers that are running the body of your PARFOR loop are essentially ordinary MATLAB processes, but launched with the '-singleCompThread' option.
Berk Ustun
on 20 Apr 2012
After sending in the Stack Trace to MATLAB. It turns out that segmentation faults do originate from the thread safety, but that the function belongs to another API package that I was using in the MEX file.
Thanks for the help everyone!
jian shulei
on 8 Jan 2016
Edited: jian shulei
on 8 Jan 2016
I meet the same question,can you tell me how to do?
Answers (2)
James Tursa
on 17 Apr 2012
Do you use any MATLAB API functions that allocate or change memory, such as mxCreateDoubleMatrix, mxMalloc, etc.? These are not thread-safe in general ... I don't know specifically about how parfor deals with this. What kind of file access are you doing?
Berk Ustun
on 17 Apr 2012
I'm actually using malloc and free instead of the MATLAB API functions so I think I should be fine on that regard.
The only file that is being accessed is the MEX file - could there be something going wrong there?
8 Comments
James Tursa
on 17 Apr 2012
Whether malloc and free are thread-safe may depend on your implementation and compiler settings. I am not familiar with GCC.
Berk Ustun
on 18 Apr 2012
Quick question: if malloc and free are thread-safe, does that imply that mxMalloc and mxFree are thread-safe?
James Tursa
on 18 Apr 2012
Not necessarily. mxMalloc and mxFree *may* use malloc and friends in the background. But even if they do there is something else going on with mxMalloc and friends. MATLAB maintains a garbage collection list in the background of the addresses returned by mxMalloc & friends. The question is whether or not this garbage collection process is thread-safe or not. So the overall answer may not hinge 100% on just the thread-safeness of malloc etc.
Also, there seems to be plenty of evidence that the memory allocation/changing functions in the API are *not* thread-safe in an OpenMP environment. So the only way I can see them being thread-safe in a parfor situation is if parfor does something special in the background to interact with the mex API stuff to make it thread-safe. I really have no idea. I don't have the Parallel Toolbox to play with.
Walter Roberson
on 18 Apr 2012
No, mxMalloc and mxFree are not necessarily thread-safe even when malloc and free are. mxMalloc generally works with memory that already belongs to the process and could do its housekeeping in a way that is not thread safe. mxMalloc might call malloc() when it needs more memory, but the part before and the part after that might not be thread safe.
Berk Ustun
on 18 Apr 2012
Thanks for these.
My understanding is that it's generally a better idea to use malloc() and free() instead of mxMalloc and mxFree (especially if the C library is thread-safe). Is there right?
I started thinking about the other mx functions that I was using, and I'm wondering whether mxCreateDoubleMatrix / mxCreateDoubleScalar are free? If not, could I replace these functions with other functions from the C library?
James Tursa
on 18 Apr 2012
If you want the MATLAB memory manager to do garbage collection for you, then you need to use mxMalloc and mxFree. Otherwise you risk memory leaks for abnormal mex function exits unless you set up your own garbage collection scheme manually.
If you are creating MATLAB mxArray variables, officially speaking you must use the API functions. While it may be possible to create an mxArray variable from scratch using malloc and hacking into the mxArray structure itself, it would be of very limited use. E.g., you couldn't return such a variable back to a MATLAB workspace ... to do so would likely crash MATLAB when it tried to free this memory with its own memory manager when in fact the memory never came from this memory manager in the first place. There would also be a host of other problems as well that I won't go into here (variable sharing, etc). You really don't want to go down this path.
Since you have the Parallel Toolbox, I would check the doc to see if there is any mention of thread-safeness of the mex MATLAB API functions in a parfor setting. If not, maybe contact technical support to see if they can give you the answer.
James Tursa
on 18 Apr 2012
Another thought: Let's assume for the purposes of discussion that the mex API creation functions are not thread-safe. What you could do is create the "output" variables at the m-file level before calling the mex routine. Then pass these in as arguments and modify them in-place in the mex routine. That completely bypasses the thread-safeness issues of the mex API functions.
See Also
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)