http://www.mathworks.com/matlabcentral/newsreader/view_thread/310899
MATLAB Central Newsreader  Can't MEX my functions
Feed for thread: Can't MEX my functions
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 25 Jul 2011 22:42:08 +0000
Can't MEX my functions
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310899#846739
Chuck37
I have a slow function and we just bought a license for the MATLAB compiler with the intention of converting it to MEX for speedup. So when I try to do it I get a message that x is no longer an option because there is no advantage due to JIT, whatever that is. I find it inconceivable that my nested for loops written in MATLAB are as fast as they would be written in compiled C. What the heck is going on? <br>
<br>
Talk about a kick in the teeth; no speedup and I need to explain to my boss why I just wasted $5000. I hope I'm missing something.

Tue, 26 Jul 2011 05:28:14 +0000
Re: Can't MEX my functions
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310899#846767
Rune Allnor
On Jul 26, 12:42 am, "Chuck37 " <chuck3...@yahooremovethis.com> wrote:<br>
> I have a slow function and we just bought a license for the MATLAB compiler with the intention of converting it to MEX for speedup. So when I try to do it I get a message that x is no longer an option because there is no advantage due to JIT, whatever that is. I find it inconceivable that my nested for loops written in MATLAB are as fast as they would be written in compiled C. What the heck is going on? <br>
><br>
> Talk about a kick in the teeth; no speedup and I need to explain to my boss why I just wasted $5000. I hope I'm missing something.<br>
<br>
You are.<br>
<br>
The matlab compiler does *not* convert the code to optimal or even<br>
decent C. It merely wraps the matlab interpreter and some other<br>
essentials into a standalone bundle that allows you to distribute<br>
the application you wrote in matlab to people who do not have<br>
access to a matlab lisence themselves.<br>
<br>
Apart from the lisence issue, there is no difference between<br>
code run as m or p files directly under matlab, and 'compiled<br>
matlab'.<br>
<br>
That is what you have. Which is not MEX.<br>
<br>
MEX is about linking code written in other languages like<br>
Fortran, C or C++, to be executed more or less like other<br>
matlab code.<br>
<br>
There are several issues with this:<br>
<br>
1) You need to write the code to be MEX'ed in the compiled<br>
language, not in matlab.<br>
2) You need access to a 3rd party compiler to actually compile<br>
the code<br>
3) You need to integrate the MEX code with your other matlab<br>
code.<br>
<br>
Items 1) and 2) require that you are a proficient user of the<br>
compiled language. You need to know your C, Fortran or whatever,<br>
to get any benefits from MEX. There is no Royal Road to fast<br>
code. Matlab is easy to learn for the uninitated. That's all.<br>
<br>
Item 3) is the ciller, what runtime performance is concerned.<br>
Matlab has a lot of overhead in any number if issues. A MEX'ed<br>
function can be very efficient compared to m code, if it is<br>
small and selfcontained. If the MEX function is only one<br>
component of a larger system, then any gains optained by<br>
MEXing might easily be lost to overhead when matlab is in<br>
control.<br>
<br>
So the bottom line is that it is very hard to get fast matlab<br>
code. I routinely obtain speed improvments on the order 10x <br>
50x when I write C++ instead of matlab. The downside is that<br>
I no longer have access to a lot of the nifty math functions<br>
matlab have.<br>
<br>
Ah, yes. You might want to try to get a refund on your matlab<br>
compiler.<br>
<br>
Rune

Tue, 26 Jul 2011 07:13:09 +0000
Re: Can't MEX my functions
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310899#846776
Bruno Luong
"Chuck37" wrote in message <j0krg0$fcg$1@newscl01ah.mathworks.com>...<br>
> I have a slow function and we just bought a license for the MATLAB compiler with the intention of converting it to MEX for speedup. So when I try to do it I get a message that x is no longer an option because there is no advantage due to JIT, whatever that is. I find it inconceivable that my nested for loops written in MATLAB are as fast as they would be written in compiled C. What the heck is going on? <br>
> <br>
> Talk about a kick in the teeth; no speedup and I need to explain to my boss why I just wasted $5000. I hope I'm missing something.<br>
<br>
The package you need is MATLAB CODER rather than MATLAB COMPILER.<br>
<br>
Bruno

Tue, 26 Jul 2011 14:05:10 +0000
Re: Can't MEX my functions
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310899#846820
Chuck37
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j0lpe4$p2d$1@newscl01ah.mathworks.com>...<br>
> "Chuck37" wrote in message <j0krg0$fcg$1@newscl01ah.mathworks.com>...<br>
> > I have a slow function and we just bought a license for the MATLAB compiler with the intention of converting it to MEX for speedup. So when I try to do it I get a message that x is no longer an option because there is no advantage due to JIT, whatever that is. I find it inconceivable that my nested for loops written in MATLAB are as fast as they would be written in compiled C. What the heck is going on? <br>
> > <br>
> > Talk about a kick in the teeth; no speedup and I need to explain to my boss why I just wasted $5000. I hope I'm missing something.<br>
> <br>
> The package you need is MATLAB CODER rather than MATLAB COMPILER.<br>
> <br>
> Bruno<br>
<br>
Thanks for the replies. I used the MATLAB compiler in this way some years ago (to get speedup). It must be they separated it into the "coder" for what I was expecting and the "compiler" for making standalones?<br>
<br>
Regarding having to actually write it in C in the first place, I don't believe it. I can write a nested for loop in MATLAB that will run very slowly compared to its C equivalent, but there is no magic to the conversion. The compiler (I mean "coder") should be able to do it just fine and see the speedup. I have some rally simple routines that I wrote manually in MEX that are about 5x faster but the coding is totally trivial, like literally almost no change from MATLAB to C.<br>
<br>
Will Mathworks have mercy on me and provide a refund or exchange I wonder?

Tue, 26 Jul 2011 14:53:43 +0000
Re: Can't MEX my functions
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310899#846822
Steven_Lord
<br>
<br>
"Chuck37 " <chuck3737@yahooremovethis.com> wrote in message <br>
news:j0mhim$mai$1@newscl01ah.mathworks.com...<br>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <br>
> <j0lpe4$p2d$1@newscl01ah.mathworks.com>...<br>
>> "Chuck37" wrote in message <j0krg0$fcg$1@newscl01ah.mathworks.com>...<br>
>> > I have a slow function and we just bought a license for the MATLAB <br>
>> > compiler with the intention of converting it to MEX for speedup. So <br>
>> > when I try to do it I get a message that x is no longer an option <br>
>> > because there is no advantage due to JIT, whatever that is. I find it <br>
>> > inconceivable that my nested for loops written in MATLAB are as fast as <br>
>> > they would be written in compiled C. What the heck is going on? Talk <br>
>> > about a kick in the teeth; no speedup and I need to explain to my boss <br>
>> > why I just wasted $5000. I hope I'm missing something.<br>
>><br>
>> The package you need is MATLAB CODER rather than MATLAB COMPILER.<br>
>><br>
>> Bruno<br>
><br>
> Thanks for the replies. I used the MATLAB compiler in this way some years <br>
> ago (to get speedup). It must be they separated it into the "coder" for <br>
> what I was expecting and the "compiler" for making standalones?<br>
><br>
> Regarding having to actually write it in C in the first place, I don't <br>
> believe it. I can write a nested for loop in MATLAB that will run very <br>
> slowly compared to its C equivalent, but there is no magic to the <br>
> conversion. The compiler (I mean "coder") should be able to do it just <br>
> fine and see the speedup. I have some rally simple routines that I wrote <br>
> manually in MEX that are about 5x faster but the coding is totally <br>
> trivial, like literally almost no change from MATLAB to C.<br>
><br>
> Will Mathworks have mercy on me and provide a refund or exchange I wonder?<br>
<br>
That's a question you'll need to ask of our official Customer Service <br>
department; you can contact them using the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>.<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>