Re: Fortran vs. Matlab on linear algebra



On Aug 6, 3:45 am, Gib Bogle <g.bo...@xxxxxxxxxxxxxxxxxxxxxx> wrote:
I have revisited a topic from 2001, using ifort 10.1.011 and Matlab 7.5.0, to compare the speed of
matmul() with the speed of a matrix multiply in Matlab.

Here is the code:

! To compare speed of Matlab and Fortran on matrix algebra
program speed

REAL(8) :: a(500,500), b(500,500), c(500,500), t1, t2
integer, parameter :: N = 20
integer :: i

call RANDOM_NUMBER(a)
call RANDOM_NUMBER(b)

call cpu_time(t1)

do i = 1,N
     a(1,1) = a(1,1) + 0.1
     c = MATMUL(a,b)
     write(*,*) c(1,1)
enddo
call cpu_time(t2)

write(*,*) 'Time: ',t2-t1
end program

! Matlab code
!function speed
!A = rand(500);
!B = rand(500);
!C = zeros(500);
!tic
!for i = 1:20
!    A(1,1) = A(1,1) + 0.1;
!    C = A * B;
!    C(1,1)
!end
!toc

I was a bit surprised to find that Matlab takes about 0.92 sec while ifort takes 2.73 sec (this is
the speed with O0, O1, or O2, while O3 takes 12.6 sec !?!)
I am running on a recent Intel quad core Windows PC.

Hi,

I've tried it on my laptop with an intel core 2 duo processor,
2GHz running linux, playing Pink Floyd - the Wall in the background :)

ifort: 10.1.017
mkl: 10.0.3.020
matlab: 7.4.0.287 (R2007a)
octave: 3.0.0

My results are:

Fortran using matmul:
time [sec] optimization
18.9 -O0
5.0 -O1
2.4 -O2
1.0 -O3

matlab:
time [sec]
0.76->0.81

octave (w. default atlas?):
time [sec]
1.5

Fortran using blas95 (gemm)
time [sec] optimization
0.77->0.84 -O0,-O1,-O2,-O3

So, for this program on my laptop, Matlab and fortran+mkl are
practically the same, which is not surprising as they both use mkl.

I have however seen fortran beat matlab by a factor of 1000 on a
nontrivial problem (25 minutes vs 1.5 sec).


Wim
.



Relevant Pages

  • Re: MATLAB <-> FORTRAN
    ... When you do A\b it just calls the basic solvers Matlab knows, ... in some cases Gauss Elimination. ... In this case you can find a Gauss Solver, ... I've bitten the bullet and converted to Fortran ...
    (comp.soft-sys.matlab)
  • Re: Matlab Vectorisation Speed - How is it done in c++?
    ... Beating the performance of vectorized Matlab code is very ... Matlab makes calls to optimized C and Fortran libraries ... Use optimization level 3 on numerical code and level 2 on non- ...
    (comp.soft-sys.matlab)
  • Re: Calling MATLAB from Fortran
    ... Fortran saves the data which Matlab m-file needs and then Matlab would read them as input from the file and after calculation save then in a file for Fortran. ... returns mxArray * ...
    (comp.soft-sys.matlab)
  • Re: pass 1D vector from matlab to fortran
    ... matlab to fortran: ... %input: timeseries ts and constants ... polynomial in seasonal smoothing ...
    (comp.soft-sys.matlab)
  • Re: Problems when calling functions from a .dll
    ... > I have quite some problems with calling functions from a .dll. ... originally written in Fortran and compiled by ... > PRECISION (double in Matlab); ... > as a basic for my header file, which is based on the example file ...
    (comp.soft-sys.matlab)