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:
Compiling Matlab for speed optimization

Subject: Compiling Matlab for speed optimization

From: Pat Finder

Date: 1 Aug, 2013 01:43:07

Message: 1 of 10

I have three matlab files that run, and test all possible combinations of several parameters.
(Yes, I need to test all combinations to validate my thesis work.)

I am running 2011b on MacOSX/64 bit.

I know that there are several compilation options: mex files, generating C code, deploying applications, and probably others.

I am considering something along the lines of:
1. Converting the *.m file into *.cc files. (Two are Matlab classes, one is the main.)
2. Compiling them for optimization.
3. Linking the mess together.

I've heard that this might not actually make the code run any faster because if you aren't careful you generate the byte codes that are interpreted by the Matlab runtime.

Questions:
1. Can I save time or not by converting the Matlab objects to C++?
2. Does anyone have any recommendations for paths through mcc to do this on Mac OSX/64 bit?

Subject: Compiling Matlab for speed optimization

From: dpb

Date: 1 Aug, 2013 02:25:48

Message: 2 of 10

On 7/31/2013 8:43 PM, Pat Finder wrote:
> I have three matlab files that run, and test all possible combinations
> of several parameters.
> (Yes, I need to test all combinations to validate my thesis work.)
>
> I am running 2011b on MacOSX/64 bit.
>
> I know that there are several compilation options: mex files, generating
> C code, deploying applications, and probably others.
>
> I am considering something along the lines of:
> 1. Converting the *.m file into *.cc files. (Two are Matlab classes, one
> is the main.)
> 2. Compiling them for optimization.
> 3. Linking the mess together.
>
> I've heard that this might not actually make the code run any faster
> because if you aren't careful you generate the byte codes that are
> interpreted by the Matlab runtime.
>
> Questions:
> 1. Can I save time or not by converting the Matlab objects to C++?
> 2. Does anyone have any recommendations for paths through mcc to do this
> on Mac OSX/64 bit?

1) Who knows...only after you've tried it will you be able to tell...

2) Not I; never run Mac

But, methinks you're putting the cart before the horse here--first
profile what you've got to see where the bottleneck is. There's no
point in worrying about optimizing what isn't the primary slowdown.

I'd suspect that the better chance of getting some speedup relatively
easily would be to take the results of the above and then

a) look at those sections for improvement in implementation, and
b) mex those functions that are the largest culprits and see how well
that addresses the problem.

The kicker in time-saving is almost always in figuring out better
algorithms, however, altho depending on what the cause of the time-use
is in Matlab it's certainly possible that simply mex-ing a portion can
have large benefits.

--

Subject: Compiling Matlab for speed optimization

From: Steven_Lord

Date: 1 Aug, 2013 14:00:49

Message: 3 of 10



"Pat Finder" <thomas@netacc.net> wrote in message
news:ktcefb$2e3$1@newscl01ah.mathworks.com...
> I have three matlab files that run, and test all possible combinations of
> several parameters.
> (Yes, I need to test all combinations to validate my thesis work.)
>
> I am running 2011b on MacOSX/64 bit.
>
> I know that there are several compilation options: mex files, generating C
> code, deploying applications, and probably others.
>
> I am considering something along the lines of:
> 1. Converting the *.m file into *.cc files. (Two are Matlab classes, one
> is the main.)
> 2. Compiling them for optimization.
> 3. Linking the mess together.
>
> I've heard that this might not actually make the code run any faster
> because if you aren't careful you generate the byte codes that are
> interpreted by the Matlab runtime.

MATLAB Compiler generates applications that use the MATLAB Common Runtime
(MCR) to execute code "as though" you were running it in MATLAB.

MATLAB Coder can generate standalone C or C++ code from a subset of the
MATLAB language. Neither MATLAB Compiler nor MATLAB Coder support all of the
MATLAB language and all the toolboxes, but MATLAB Compiler supports more
than MATLAB Coder does.

> Questions:
> 1. Can I save time or not by converting the Matlab objects to C++?

It depends on what you're doing. If you're using something that's not
supported by either MATLAB Compiler or MATLAB Coder (like symbolic
calculations using Symbolic Math Toolbox) then probably not. [Symbolic Math
Toolbox DOES have a function to convert individual symbolic expressions into
C code, CCODE, but you couldn't just throw a MATLAB program file into CCODE
and say "Generate this." It's more 'targeted' in a sense.]

> 2. Does anyone have any recommendations for paths through mcc to do this
> on Mac OSX/64 bit?

Before trying this, I recommend:

1) Using MATLAB Profiler to determine where the bottlenecks in your code
(for a smaller subset of your parameter sweep) are located and see if you
can optimize those bottlenecks.
2) Once you've identified the bottlenecks, if you can't figure out how to
improve their performance, describe what you're doing and show the group
some code. There are a lot of people here who enjoy "MATLAB golf" and
"MATLAB code racing" (making MATLAB code short and fast respectively.)

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

Subject: Compiling Matlab for speed optimization

From: Pat Finder

Date: 1 Aug, 2013 22:33:10

Message: 4 of 10

Thank you both for your replies. I'm running the profiler now.

The gist of what I'm trying to do is combinatorial selection. Given 40 things, which three are the best ones to combine? I have an algorithm that works well, but in order to compare my algorithm to "exhaustive search" I actually have to compare all possible combinations of 3 things, drawn from 40.

For example, of 40 genes which 3 genes best indicate the condition...

Thank you again.

Subject: Compiling Matlab for speed optimization

From: Pat Finder

Date: 2 Aug, 2013 12:00:14

Message: 5 of 10

For completeness:

The profiler revealed that most of the time was spent in one function. At the end of the function there was a test to be sure that the results applied.

By moving the "idiot test" to the start of the function, and changing the return values I arrived at a situation where:
1. The early termination test was applied first.
2. Non-sense situations were ignored and not processed.
3. The rest of the program recognized when the function did not run, and skipped that condition.

The results are running now, but it looks like I've got about a 2X speedup, because about half the conditions are rejected as nonsense.

Subject: Compiling Matlab for speed optimization

From: Steven_Lord

Date: 2 Aug, 2013 14:12:48

Message: 6 of 10



"Pat Finder" <thomas@netacc.net> wrote in message
news:ktg70e$bj3$1@newscl01ah.mathworks.com...
> For completeness:
>
> The profiler revealed that most of the time was spent in one function. At
> the end of the function there was a test to be sure that the results
> applied.
>
> By moving the "idiot test" to the start of the function, and changing the
> return values I arrived at a situation where: 1. The early termination
> test was applied first.
> 2. Non-sense situations were ignored and not processed.
> 3. The rest of the program recognized when the function did not run, and
> skipped that condition.
> The results are running now, but it looks like I've got about a 2X
> speedup, because about half the conditions are rejected as nonsense.

Good. Now check and see if you can vectorize your processing function to
work on multiple conditions at once. For example, create this file:

% begin compareLoopAndVectorizedCode.m
function compareLoopAndVectorizedCode(n)
if nargin == 0
    n = 1e5;
end
z = randn(1, n);
z2 = zeros(1, n);
tic
for k = 1:n
    z2(k) = squareRoot1(z(k));
end
toc

tic
z3 = squareRoot2(z);
toc
isequaln(z2, z3)

function y = squareRoot1(x)
if x < 0
    y = NaN;
else
    y = x^(1/2);
end

function y = squareRoot2(x)
y = NaN(size(x));
y(x >= 0) = x(x >= 0).^(1/2);
% end compareLoopAndVectorizedCode

When n is small, the squareRoot1 approach takes less time. As n gets larger,
though, there comes a point where squareRoot2 becomes more efficient.
Perhaps by vectorizing some or all of your analysis function you can avoid
calling it so often. If not, then you could ask here for help on optimizing
the new bottleneck in your code or implementing that bottleneck piece as a
MEX-file.

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

Subject: Compiling Matlab for speed optimization

From: Pat Finder

Date: 2 Aug, 2013 17:06:10

Message: 7 of 10

Thank you Steve.

The code is very well vectorized right now. At this point, the biggest issues are:
1. One call to sort( data, 'ascend' )
2. The usual issues related to Matlab checking for imaginary numbers and out-of-range references, etc...

I think my code would really benefit from using the matlab compiler because I could tell the compiler to only worry about real numbers and not to worry about out-of-range references.

Unfortunately, I am having trouble figuring out how to:
1. Compile a half dozen *.m files into one library. (Should these be mex files or C++?)
Two of the main files are fully contained classdef files.
2. Get the main routine to use the generated library.
The variety of possibilities makes it difficult to find what I want in the documentation.

So, suppose I have two classdef files: bar.m and starbucks.m.
And I have a foo.m file that calls bar.m and starbucks.m.

It seems to me that after running mbuild -setup I should be able to run two
mcc or mbuild commands so that afterwards I can just run the application faster.

Can you provide examples of what those two commands might be? Or, at least guidance on if I want to generate mex files or code files?

Best Regards,

Subject: Compiling Matlab for speed optimization

From: Marc

Date: 3 Aug, 2013 02:08:10

Message: 8 of 10

"Pat Finder" <thomas@netacc.net> wrote in message <ktgou2$39h$1@newscl01ah.mathworks.com>...
> Thank you Steve.
>
> The code is very well vectorized right now. At this point, the biggest issues are:
> 1. One call to sort( data, 'ascend' )
> 2. The usual issues related to Matlab checking for imaginary numbers and out-of-range references, etc...
>
> I think my code would really benefit from using the matlab compiler because I could tell the compiler to only worry about real numbers and not to worry about out-of-range references.
>
> Unfortunately, I am having trouble figuring out how to:
> 1. Compile a half dozen *.m files into one library. (Should these be mex files or C++?)
> Two of the main files are fully contained classdef files.
> 2. Get the main routine to use the generated library.
> The variety of possibilities makes it difficult to find what I want in the documentation.
>
> So, suppose I have two classdef files: bar.m and starbucks.m.
> And I have a foo.m file that calls bar.m and starbucks.m.
>
> It seems to me that after running mbuild -setup I should be able to run two
> mcc or mbuild commands so that afterwards I can just run the application faster.
>
> Can you provide examples of what those two commands might be? Or, at least guidance on if I want to generate mex files or code files?
>
> Best Regards,

Just to understand you, your 40 genes, they are either "on" or "off"?

Have you tried setting up the experiments and use parallel computing to plow through them?

Subject: Compiling Matlab for speed optimization

From: Pat Finder

Date: 4 Aug, 2013 01:38:07

Message: 9 of 10

"Marc" wrote in message <kthoma$lnk$1@newscl01ah.mathworks.com>
[snip]
> Just to understand you, your 40 genes, they are either "on" or "off"?
>
> Have you tried setting up the experiments and use parallel computing to plow through them?

Yes I am exhaustively searching all possible combinations, which is why speed is so important.

I have 40 genes and I am looking at them 3 at a time. That's 40 choose 3, or 9880 possible combinations. Your question gives me a great idea for a 6X speed improvement. Previously in my code I was disallowing the same set of genes to be used again, but I threw that code out without realizing the implications for speed.

Since order is not important for me, I can realize a factor of 6 speed improvement by re-installing the filter code that avoids redundantly treating gene combinations: (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1) as all different. So, because I stopped testing for the same set of genes, I caused myself a factor of 6 speed decrease.

Thanks for your question, because it jostled a few neurons loose.

In any case, I would still like to compile the mess on OS/X, somehow, so that it can run faster. Any help on compiling for speed on OS/X (while still using the Matlab command line interface) would be helpful.

Subject: Compiling Matlab for speed optimization

From: Steven_Lord

Date: 5 Aug, 2013 15:05:27

Message: 10 of 10



"Pat Finder" <thomas@netacc.net> wrote in message
news:ktkb9v$ddu$1@newscl01ah.mathworks.com...
> "Marc" wrote in message <kthoma$lnk$1@newscl01ah.mathworks.com>
> [snip]
>> Just to understand you, your 40 genes, they are either "on" or "off"?
>>
>> Have you tried setting up the experiments and use parallel computing to
>> plow through them?
>
> Yes I am exhaustively searching all possible combinations, which is why
> speed is so important.

Is this necessary? Have you considered using an optimization routine like
PATTERNSEARCH from Global Optimization Toolbox?

> I have 40 genes and I am looking at them 3 at a time. That's 40 choose 3,
> or 9880 possible combinations. Your question gives me a great idea for a
> 6X speed improvement. Previously in my code I was disallowing the same
> set of genes to be used again, but I threw that code out without realizing
> the implications for speed.
> Since order is not important for me, I can realize a factor of 6 speed
> improvement by re-installing the filter code that avoids redundantly
> treating gene combinations: (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2),
> and (3,2,1) as all different. So, because I stopped testing for the same
> set of genes, I caused myself a factor of 6 speed decrease.

Are you generating them yourself in a FOR loop (or a triple FOR loop) or are
you using NCHOOSEK?

http://www.mathworks.com/help/matlab/ref/nchoosek.html

If you're doing this in a triple nested FOR loop, and you're calling your
filtering function (for invalid combinations) inside the loop, that's many
calls to your filtering function. If your filtering function is vectorized,
you could generate the combinations with NCHOOSEK and call your filtering
function once then use one FOR loop to iterate through the rows of the
result. Depending how fast your filtering function is, the reduced number of
calls may help.

[The entry in the Limitation section that mentions this is only effective
for length(v) less than 15 seems out of date. If the number of combinations
was high (k near length(v)/2) then you could run into problems. But since k
is so small, the two-input syntax should work fine. I tried it on my machine
and it took a fraction of a second.]

> Thanks for your question, because it jostled a few neurons loose.
>
> In any case, I would still like to compile the mess on OS/X, somehow, so
> that it can run faster. Any help on compiling for speed on OS/X (while
> still using the Matlab command line interface) would be helpful.

Compiling with MATLAB Compiler shouldn't make it faster. Compiling with
MATLAB Coder _may_ make it faster, but again that supports a more limited
subset of the MATLAB language.

If you want to try MATLAB Coder, take a look at the Examples on the MATLAB
Coder product page:

http://www.mathworks.com/products/matlab-coder/

There are also tutorials in the Getting Started section of the MATLAB Coder
documentation:

http://www.mathworks.com/help/coder/getting-started-with-matlab-coder.html

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

Tags for 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