Segmentation Faults when Running MEX files in Parallel

2 views (last 30 days)
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
Berk Ustun
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
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?

Sign in to comment.

Answers (2)

James Tursa
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
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
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.
Berk Ustun
Berk Ustun on 23 Apr 2012
That's a great idea! Will try that out as well.

Sign in to comment.

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!