Thread Subject: vectorized Matlab versus C code

Subject: vectorized Matlab versus C code

From: Sean

Date: 23 Nov, 2009 21:55:23

Message: 1 of 4

Hi,

I have an image processing algorithm in Matlab that I converted to C, and the C code ended up being slower than Matlab (note that I have very little experience in writing optimized C code). My question is whether my C code being slower than Matlab code is highly surprising given the following circumstances?

The Matlab code is heavily vectorized and the basic structure is as follows (written in rough pseudocode illustrating some operations that occur inside a main iterative loop).

for i=1:iterations % number of iterations ~200
    fftshift(fft2());
    upsampling
    matrix multiplication,addition,subtraction;
    fftshift(ifft2())
end

The operations inside the loop are all vectorized operations on 2D matrices. When converting to C, I code my own matrix operations such as multiplication, addition, upsampling using for loops over each index. For fft's, I use the fftw package.

Is it unsurprising in this case that my C code is slower than Matlab? Would the C code be able to be much faster (>20 times) than Matlab code in this case if I used the BLAS and LAPACK packages instead of coding my own matrix functions?

Thanks! Any advise/opinions would be greatly appreciated.

Regards,
Sean

   

Subject: vectorized Matlab versus C code

From: Robert Orzechowski

Date: 23 Nov, 2009 22:12:18

Message: 2 of 4

Matlab is much faster than C if you use matrices.
C is faster than Matlab if you use many loops. In your example you have one loop, but several martix operations.

regards,
Robert

Subject: vectorized Matlab versus C code

From: Rune Allnor

Date: 23 Nov, 2009 22:24:32

Message: 3 of 4

On 23 Nov, 22:55, "Sean " <phido...@gmail.com> wrote:

> Is it unsurprising in this case that my C code is slower than Matlab?

Eh... I don't know if I should answer 'yes' or 'no' to that,
so let's just say that it is not at all surprising that your
C code is slower than matlab.

> Would the C code be able to be much faster (>20 times) than Matlab code in this case if I used the BLAS and LAPACK packages instead of coding my own matrix functions?

Matlab uses some of the fastest libraries available for
linear algebra and FFTs. You will need to know exactly
what you are doing to be able to come remotely close to
the performance of these libraries, if you code your own
C code. You will need to know both how to write efficient
C code (one would be required to know parallel programming),
as well as how to trick the compiler into optimizing it
for you. You might even have to hand-optimize some MMX-type
assembler code to get there.

Not for the faint-hearted.

Rune

Subject: vectorized Matlab versus C code

From: James Tursa

Date: 24 Nov, 2009 00:47:17

Message: 4 of 4

"Sean " <phidoc09@gmail.com> wrote in message <hef0cb$6ve$1@fred.mathworks.com>...
>
> for i=1:iterations % number of iterations ~200
> fftshift(fft2());
> upsampling
> matrix multiplication,addition,subtraction;
> fftshift(ifft2())
> end
>
> Is it unsurprising in this case that my C code is slower than Matlab? Would the C code be able to be much faster (>20 times) than Matlab code in this case if I used the BLAS and LAPACK packages instead of coding my own matrix functions?

Not surprising. The matrix addition and subtraction will likely run just as fast in a C loop as in a vectorized MATLAB call (doesn't use BLAS calls for this). For matrix multiplication, *usually* you can't beat the BLAS routines. You can link to them and call them directly from your C code to get the speed improvement directly, or you can use mexCallMATLAB to call the mtimes function to get the speed improvement a bit indirectly. The BLAS routines can easily outperform a hand-written matrix multiply by 20x. I have intentionally coded up a poorly written matrix multiply routine and compared it to the MATLAB BLAS DGEMM routine and it ran 160x slower. My advice is to go through the effort of calling the BLAS routine directly or use mexCallMATLAB to call mtimes.

James Tursa

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com