Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speedup Matlab to C++ Conversion

I have some Matlab image processing code which runs pretty slowly and I'm prepared to convert it over to C/C++. I don't really know much about how matlab works and how code is executed but I'm just interested to hear what kind of speedups I might expect. Clearly there are many variables that will affect this but I'm just looking for a guide perhaps from your own experiences.

Thanks

Zenna

like image 698
zenna Avatar asked Aug 04 '09 21:08

zenna


People also ask

How can I speed up my Matlab code?

Matlab has a tool called "Matlab Coder" which can convert your matlab file to C code or mex file. My code is relatively simple so it works fine. Speed up gain is about 10 times faster. This saves me time coding a few hundreds lines.

How do I generate C code from MATLAB code?

To generate C code, the entry point must be a function. You do not have to generate code for the entire MATLAB application. If you have specific portions that are computationally intensive, generate code from these portions in order to speed up your algorithm.

Is it possible to convert a MATLAB program to C++?

@ChinmayKanchi No, it is certainly answerable. You can automatically convert some Matlab programs into C or C++ using Matlab Coder. The first thing you need to realise is that porting code from one language to another (especially languages as different as Matlab and C++) is generally non-trivial and time-consuming.

How to speed up a matrix element in MATLAB?

The golden rule for MATLAB is to always assign the index of the faster loop to the most left part of the matrix dimensions and the slowest to the most right part. This rule also applies to tensors. For example, in the following matrix elements assignment, we obtain a speedup of nearly x6 when using the column-major order.


3 Answers

It mostly depends on the tightness of your loops in Matlab. If you are simply calling a series of built-in Matlab image processing functions, you will most likely not be able to improve performance (most likely you will hurt it). If you are looping over image pixels or doing some kind of block processing, you may see big improvements. If you are doing some looping, but the amount of processing within each iteration is substantial, you may only see little or no improvement.

The way I look at Matlab is that every executed line has some amount of overhead. If you can put your solution into the form of a matrix multiply, or some other vector/matrix operation, you only suffer that overhead once and it is negligible. However, with loops, you suffer that overhead every time the loop iterates. Also, most of Matlab's image processing functions are just making calls out to optimized libraries, so don't try to recreate them unless you know for sure where they can be improved.

I found that the best approach is to use a combination of C and Matlab. I use Matlab when the operation can be easily vectorized (put in terms of vector/matrix operations). This may mean coming at the solution from a different angle than what seems the most straightforward. Also, it is hard to beat Matlab's plotting and visualization so I would definitely not move to an all C/C++ solution unless you have a plan for how to display with C/C++ (if that is part of your project).

If I can't come up with a relatively easy way to vectorize, I just implement the part of processing that needs tight loops in a C mex function that can be called from Matlab. I tend to use C instead of C++ in this case since the process should be relatively small and not need a lot of complicated data abstraction, but C++ would work fine too. Make sure you access image data in column-major order to maximize cache hits since this is how Matlab organizes its matrices.

like image 170
Jason B Avatar answered Nov 03 '22 07:11

Jason B


It really depends on the quality of your Matlab code and what it is you are doing. Idiomatic Matlab code written by a Matlab expert is going to be hard to beat, particularly if you're not an optimization guru and purely expecting a speed up due to the switch of language. For instance, I found even some of the more well regarded C-based FFT libraries were no match for Matlab's FFT.

That said, comparing a poorly written Matlab program to an averagely written c++ one, I'd say you're looking at an order of magnitude in my experience.

like image 31
maxaposteriori Avatar answered Nov 03 '22 07:11

maxaposteriori


The short answer to the question of what kind of speedups you may get is "it depends".

Matlab is an interpreter, so overall it is much slower than native c++ code. However, many matlab functions are well optimized, and recent versions include JIT. So you would have to decide whether to rewrite all of your matlab code in C, rewrite only the critical parts, or to optimize the matlab code itself to run faster.

I would suggest that you start by using Matlab's built-in profiling tools to find the performance bottlenecks in your application. It may be the case that you may tweak the matlab code to get better performance. The rule of thumb is to avoid loops by using vectorized array operations instead of iterating one element at a time.

like image 25
Dima Avatar answered Nov 03 '22 07:11

Dima