views:

455

answers:

2

I have just rewritten a Matlab program in c++ as a mex-function to speed things up, with fantastic results. This optimization decision was a very very good idea, with up to a factor 20 speed up without threading. It still left me curious about what the mex-function was spending time on and wanting to identify possible bottlenecks.

I'm looking for a way to profile mex-functions. The matlab profiler is not much use, and the other profilers I've downloaded (both free and trial) all want an executable to run. I'm no mex-guru, but as far as I've understood the only way to run a mex is from within Matlab. The mex-function is compiled into a dll, but is called .mex64. So this problem should be similar to profiling a dll. To write the c++ mex-function I used a single-user VS2005 (i.e., not the team version), and am running on a x64-platform.

Does anyone know of a good way to profile a mex-function? What tool should I use and how do I use it when I start from within Matlab? Or is there any other way to profile the c++-code?

+2  A: 

the only way i've managed to do this is to separate out the function doing the work and writing a separate wrapper (instead of mexFunction) that loads .mat files with test data and runs as a standalone executable. this can then be profiled using e.g. gprof

second
Thanks for the tip. It's a good approach. My only problem is that the input to the mexFunction is a large bunch of large matlab structs. I need to write code to create all this stuff in the wrapper to be able to run, which sort of prevents me from going at it at one and without any thought.
AnnaR
don't follow. can't you create all your structs inside matlab and save them to a .mat file? (ie anything that was previously an input argument to your mex funtion is now save in a .mat file and read in by the new wrapper from disk.
second
+1  A: 

Is there a way to run the whole thing under a VC IDE. Like you could say "debug DLL Foo.dll, using Matlab as the startup app".

Then, if the function Bar you're using in Foo.dll runs in < 1 sec, make sure Matlab calls it a lot of times, or add a wrapper function in the DLL to call it a lot of times.

If you can get to that point, you can use the manual call stack sampling technique, that really works in spite of not being popular or requiring installation of a tool.

Mike Dunlavey