Reputation: 391
I know that it wont always be the case, but aren't MEX functions supposed to increase efficiency in code, especially with calculations?
I have used the coder toolkit to MEX the expm
function in MATLAB. I was expecting to see a good increase in efficiency since that function performs a lot of matrix calculations.
However, I don't see much of an improvement in time, as the picture below shows:
X-Axis: Another iteration of expm
function
Y-Axis: Run time for that function
Blue Line: Regular expm
function call
Red Line: MEX'ed expm
function call
Is there a reason that the MEX version is so similar to the regular version? Is there a way to increase the speed? I used tic
and toc
to gather data about run time.
Upvotes: 0
Views: 1749
Reputation: 30579
Here's how a call to expm(A)
breaks down where A = rand(500,500);
.
The time is split evenly between a matrix multiplication (F = F*F;
) and a call to the child function PadeApproximantOfDegree
. The matrix multiplication is built in, using very fast LAPACK functions in mkl.dll (MATLAB's linear algebra functions using Intel MKL).
Here's where all the time is spent in PadeApproximantOfDegree
:
That's not in a loop. All calls to built-in matrix math functions. If there were iterations, then I'd expect MATLAB to be a bit slower, but it's just a few lines with 1 call each taking up all the time. Only the matrix multiplication in the parent (F*F
) is called more than once.
In fact, I wouldn't be surprised if the MEX version were slower, if Coder were not able to use the optimized multi-threaded libraries that MATLAB has access to. Apparently Coder manages.
Upvotes: 2
Reputation: 18562
Most matlab functions are simple wrappers that call FORTRAN library code, most of which come from LAPACK / EISPACK / LINPACK. In other words, a built-in matlab function call is already invoking compiled code, not interpreted matlab code (.m sources). So, there isn't much that MEX can do to improve the performance of those function calls.
Compiling your code in matlab will only provide benefits when you have a lot of work being done in matlab source code directly. If your code is mostly invoking a bunch of built-in matlab functions, especially matrix functions (which are all LAPACK functions), then you won't see much improvement, it is only when you have "manual" code, i.e., if you were implementing something like "expm" entirely in matlab code (in a .m file), then it would be much faster when compiled with MEX. You will only see the benefit of compilation of matlab code if you have a significant amount of matlab code of your own, not if you're just making a few calls to built-in functions.
Just think about it, why would Mathworks ship Matlab with built-in functions implemented as interpreted matlab code? Even if they implement some of their built-in functions in matlab code (which is only a small fraction, as most is it FORTRAN / C / C++), they will compile those functions before shipping, so that you, as a user, get the best performance out of them.
Upvotes: 3