I'm interfacing MATLAB with C/C++ using the MATLAB Engine API.
In my particular case MATLAB is used to calculate something and result is printed in C. However, throughout various test on both sides I noticed significant performance losses in C.
Here is an example of the MATLAB function calls:
tic;
data = predictIM(data);
toc;
On the C side I call similar functions as follows:
iMod::Timer_T<high_resolution_clock> t;
engPutVariable(ep, "data", dataContent);
engEvalString(ep, "[posture] = predictIM(data);");
UT_NOTIFY(LV_DEBUG,"The execution took "<<t.seconds());
My timer implementation in C++ looks as follows:
template< class Clock >
class Timer_T
{
typename Clock::time_point start;
public:
Timer_T() : start( Clock::now() ) {}
typename Clock::duration elapsed() const {
return Clock::now() - start;
}
double seconds() const {
return elapsed().count() *
((double)Clock::period::num/Clock::period::den);
}
};
The above MATLAB code runs at approximately 180 frames per second including setting the matrix (data
), whereas the C code only at 24 FPS. I used tic
/toc
to measure the execution time in MATLAB whereas my own timer implementation is used on the C/C++ side.
While profiling the application I noticed that the MATLAB Engine calls are the bottleneck. I know that the Linux MATLAB Engine implementation is using named pipes for interfacing with MATLAB and I was wondering if there is a way to speed up the communication of MATLAB with its Engine?
First, it's not fair to measure MATLAB engine like that. You should only time the computation time just as you did in the MATLAB, like this:
engPutVariable(ep, "data", dataContent); // you should not time this
iMod::Timer_T<high_resolution_clock> t;
engEvalString(ep, "[posture] = predictIM(data);"); // time this only for fair
UT_NOTIFY(LV_DEBUG,"The execution took "<<t.seconds());
Actually, from my experience, running MATLAB and calling its engine in C/C++ should have similar speed as they actually depend the MATLAB software itself.
Second, I do have an advice for possible speedup. You should only open single MATLAB engine throughout your C/C++ project, instead of creating one for each call. Like this:
int main()
{
// Open MATLAB engine for the whole project
Engine *ep = engOpen(NULL);
// Use it multiple times
for (int i=0; i<100; ++i){
engEvalString(ep, ...);
}
// Close MATLAB engine
engClose(ep);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With