Use coder to convert parfor-loop to mex, but mex version is much slower

1 view (last 30 days)
I convert a function containing a parfor-loop into a mex file to get some speed up. But the mex version of this function is 5x times slower than the matlab's version.
Before running, I used parpool(20) to setup a parallel pool mannully. When mex version of function is running, the CPU load is increased but the parallel workers are not working (the pool status in the left-bottom corner of matlab is blue instead of green) . Besides, the pool automatically shutdown when the mex function is still running.
It seems the parfor-loop after codegen does not work. Is there any way that I can check if these workers are running?
When I use mex-version of parfor, there is a matlab process that have a CPU usage of 4000%, but when I use the m-code version of parfor, there are 20 processes with CPU usage of 100%. It seems parfor after codegen uses multi-thread instead of multi-process. And this multi-thread is much slower than multi-process.
Xingwang Yong
Xingwang Yong on 29 Apr 2021
Edited: Xingwang Yong on 29 Apr 2021
Thanks Ji.
Before I post a question here, I've read the doc you mentioned and it does not help much.
Basically, I am trying to fit thousands of curves inside the parfor using lsqcurvefit(). I profiled the code before and after using coder.
Before, I used m-code of lsqcurvefit() and converted the objective function to mex. Fitting a curve would take 1.6s and nearly 1.2 seconds was used by objective function evaluation.
After, I used the codegen'd lsqcurvefit (the objective function is converted to mex automatically). The fitting take 2.6s and 1.4 seconds was used by objective evaluation. That's to say, 1.2s were used by other parts of lsqcurvefit(). This is far more than the m-code of lsqcurvefit.
From this initial experience, this slow-down may be atrributed to lsqcurvefit() instead of parfor. But I can not say it for sure. Because I tried to fit a simple exponential decay using codegen'd lsqcurvefit(), it indeed gives some speed-up. This slow-down may be related to lsqcurvefit() and the objective function (my objective function is highly sophiscated, which does not have an analytical formula and relies on numerical simulation).
I am trying to extract a minimal reproducible snippet from my code to demonstrate this issue. I would post it here if I managed to do that.

Sign in to comment.

Answers (0)




Community Treasure Hunt

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

Start Hunting!