Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Can't MEX my functions

Subject: Can't MEX my functions

From: Chuck37

Date: 25 Jul, 2011 22:42:08

Message: 1 of 5

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?

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.

Subject: Can't MEX my functions

From: Rune Allnor

Date: 26 Jul, 2011 05:28:14

Message: 2 of 5

On Jul 26, 12:42 am, "Chuck37 " <chuck3...@yahooremovethis.com> wrote:
> 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?  
>
> 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.

You are.

The matlab compiler does *not* convert the code to optimal or even
decent C. It merely wraps the matlab interpreter and some other
essentials into a stand-alone bundle that allows you to distribute
the application you wrote in matlab to people who do not have
access to a matlab lisence themselves.

Apart from the lisence issue, there is no difference between
code run as m or p files directly under matlab, and 'compiled
matlab'.

That is what you have. Which is not MEX.

MEX is about linking code written in other languages like
Fortran, C or C++, to be executed more or less like other
matlab code.

There are several issues with this:

1) You need to write the code to be MEX'ed in the compiled
   language, not in matlab.
2) You need access to a 3rd party compiler to actually compile
   the code
3) You need to integrate the MEX code with your other matlab
   code.

Items 1) and 2) require that you are a proficient user of the
compiled language. You need to know your C, Fortran or whatever,
to get any benefits from MEX. There is no Royal Road to fast
code. Matlab is easy to learn for the uninitated. That's all.

Item 3) is the ciller, what run-time performance is concerned.
Matlab has a lot of overhead in any number if issues. A MEX'ed
function can be very efficient compared to m code, if it is
small and self-contained. If the MEX function is only one
component of a larger system, then any gains optained by
MEXing might easily be lost to overhead when matlab is in
control.

So the bottom line is that it is very hard to get fast matlab
code. I routinely obtain speed improvments on the order 10x -
50x when I write C++ instead of matlab. The downside is that
I no longer have access to a lot of the nifty math functions
matlab have.

Ah, yes. You might want to try to get a refund on your matlab
compiler.

Rune

Subject: Can't MEX my functions

From: Bruno Luong

Date: 26 Jul, 2011 07:13:09

Message: 3 of 5

"Chuck37" wrote in message <j0krg0$fcg$1@newscl01ah.mathworks.com>...
> 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?
>
> 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.

The package you need is MATLAB CODER rather than MATLAB COMPILER.

Bruno

Subject: Can't MEX my functions

From: Chuck37

Date: 26 Jul, 2011 14:05:10

Message: 4 of 5

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j0lpe4$p2d$1@newscl01ah.mathworks.com>...
> "Chuck37" wrote in message <j0krg0$fcg$1@newscl01ah.mathworks.com>...
> > 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?
> >
> > 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.
>
> The package you need is MATLAB CODER rather than MATLAB COMPILER.
>
> Bruno

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?

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.

Will Mathworks have mercy on me and provide a refund or exchange I wonder?

Subject: Can't MEX my functions

From: Steven_Lord

Date: 26 Jul, 2011 14:53:43

Message: 5 of 5



"Chuck37 " <chuck3737@yahooremovethis.com> wrote in message
news:j0mhim$mai$1@newscl01ah.mathworks.com...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> <j0lpe4$p2d$1@newscl01ah.mathworks.com>...
>> "Chuck37" wrote in message <j0krg0$fcg$1@newscl01ah.mathworks.com>...
>> > 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? 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.
>>
>> The package you need is MATLAB CODER rather than MATLAB COMPILER.
>>
>> Bruno
>
> 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?
>
> 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.
>
> Will Mathworks have mercy on me and provide a refund or exchange I wonder?

That's a question you'll need to ask of our official Customer Service
department; you can contact them using the Contact Us link on
http://www.mathworks.com.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us