Fortran vs. Matlab on linear algebra

This is a discussion on Fortran vs. Matlab on linear algebra within the Fortran forums in Programming Languages category; 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); ...

Go Back   Application Development Forum > Programming Languages > Fortran

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-05-2008, 09:45 PM
Gib Bogle
Guest
 
Default Fortran vs. Matlab on linear algebra

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.
------------ And now a word from our sponsor ------------------
Do your users want the best web-email gateway? Don't let your
customers drift off to free webmail services install your own
web gateway!
-- See http://netwinsite.com/sponsor/sponsor_webmail.htm ----
Reply With Quote
  #2  
Old 08-05-2008, 10:56 PM
rusi_pathan
Guest
 
Default Re: Fortran vs. Matlab on linear algebra

On Aug 5, 9:45 pm, Gib Bogle <g.bo...@auckland.no.spam.ac.nz> 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.
> ------------ And now a word from our sponsor ------------------
> Do your users want the best web-email gateway? Don't let your
> customers drift off to free webmail services install your own
> web gateway!
> -- Seehttp://netwinsite.com/sponsor/sponsor_webmail.htm ----


Comparisons like this dont mean anything. Matlab uses optimized BLAS
(probably written in Fortran). The compiler vendor on the other hand
may not have tried hard enough to optimize matmul.

How about comparing Matlabs performance with the vendor supplied BLAS
(MKL in this case) and testing it for a range of matrix sizes (say
from 50-20000).

I would certainly expect the MKL to outperform Matlab unless Intel
messed something up. Moreover the MKL also has multithreaded BLAS 3
and hence can utilize the extra cores/processors (not sure if that is
possible in the new Matlabs)
Reply With Quote
  #3  
Old 08-06-2008, 11:14 AM
Ken.Fairfield@gmail.com
Guest
 
Default Re: Fortran vs. Matlab on linear algebra

On Aug 5, 6:45*pm, Gib Bogle <g.bo...@auckland.no.spam.ac.nz> 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


Forgive me for not knowing Matlab syntax, but the
above (commented) Matlab code sure *looks* like
A and B get 500 random numbers. At that point,
how does Matlab know A and B are 2 dimensional
and that 250,000 random numbers are required
for each (which is what the Fortran code clearly
does)? Yes, I see the usage *later* referencing
A(1,1), but that's the only clue in the whole script
that A, B and C are 2-D.

-Ken

Reply With Quote
  #4  
Old 08-06-2008, 11:53 AM
mecej4
Guest
 
Default Re: Fortran vs. Matlab on linear algebra

Ken.Fairfield@gmail.com wrote:
> On Aug 5, 6:45 pm, Gib Bogle <g.bo...@auckland.no.spam.ac.nz> 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

>
> Forgive me for not knowing Matlab syntax, but the
> above (commented) Matlab code sure *looks* like
> A and B get 500 random numbers. At that point,
> how does Matlab know A and B are 2 dimensional
> and that 250,000 random numbers are required
> for each (which is what the Fortran code clearly
> does)? Yes, I see the usage *later* referencing
> A(1,1), but that's the only clue in the whole script
> that A, B and C are 2-D.
>
> -Ken
>

Many Matlab functions return a square matrix of size equal to the single
argument. To obtain a one dimensional array, one has to write

v=rand(500,1);

-- mecej4
Reply With Quote
  #5  
Old 08-06-2008, 05:20 PM
dpb
Guest
 
Default Re: Fortran vs. Matlab on linear algebra

Ken.Fairfield@gmail.com wrote:
....
>> ! Matlab code
>> !function speed
>> !A = rand(500);

....
> Forgive me for not knowing Matlab syntax, but the
> above (commented) Matlab code sure *looks* like
> A and B get 500 random numbers. At that point,
> how does Matlab know A and B are 2 dimensional
> and that 250,000 random numbers are required
> for each (which is what the Fortran code clearly
> does)? Yes, I see the usage *later* referencing
> A(1,1), but that's the only clue in the whole script
> that A, B and C are 2-D.


That's the definition of rand() in Matlab that rand(N) returns a square
NxN matrix.

Matlab does the allocation of A on the fly and knows its size thereon as
part of the language feature behind the scenes.

To get a 1D vector, one would specify rand(500,1). (No reason, just
policy consistent w/ most (but not all) other Matlab builtin data
generation functions ).


>> help randn


RANDN Normally distributed random numbers.
RANDN(N) is an N-by-N matrix with random entries, chosen from
a normal distribution with mean zero, variance one and standard
deviation one.
RANDN(M,N) and RANDN([M,N]) are M-by-N matrices with random entries.
RANDN(M,N,P,...) or RANDN([M,N,P...]) generate random arrays.
RANDN with no arguments is a scalar whose value changes each time it
is referenced. RANDN(SIZE(A)) is the same size as A.
>>


--
Reply With Quote
  #6  
Old 08-06-2008, 05:30 PM
Gib Bogle
Guest
 
Default Re: Fortran vs. Matlab on linear algebra

rusi_pathan wrote:
> On Aug 5, 9:45 pm, Gib Bogle <g.bo...@auckland.no.spam.ac.nz> 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.
>> ------------ And now a word from our sponsor ------------------
>> Do your users want the best web-email gateway? Don't let your
>> customers drift off to free webmail services install your own
>> web gateway!
>> -- Seehttp://netwinsite.com/sponsor/sponsor_webmail.htm ----

>
> Comparisons like this dont mean anything. Matlab uses optimized BLAS
> (probably written in Fortran). The compiler vendor on the other hand
> may not have tried hard enough to optimize matmul.
>
> How about comparing Matlabs performance with the vendor supplied BLAS
> (MKL in this case) and testing it for a range of matrix sizes (say
> from 50-20000).
>
> I would certainly expect the MKL to outperform Matlab unless Intel
> messed something up. Moreover the MKL also has multithreaded BLAS 3
> and hence can utilize the extra cores/processors (not sure if that is
> possible in the new Matlabs)


I will see if I can the comparison with MKL. The reason I did this
quick test is that I have a student who is solving a nonlinear system of
PDEs using Matlab, and this problem requires extremely long execution
times. I've always had the idea (prejudice?) that Matlab was slow
compared with Fortran, and thought that in the last resort he could
recode it in Fortran (with his level of programming skill, not a
decision to take lightly). What this test tells me is that for this
kind of matrix operation Matlab is much better than I'd realized. I do
understand that it is inefficient on code with nested loops etc.

BTW, it occurs to me that someone reading this ng might be familiar with
the problem that the student is tackling. As part of his larger
problem, he is solving the Poisson-Nernst-Planck equations for two ionic
species in solution with an applied potential field. I'd be happy to
hear of any tips or tricks to speed up this inherently very
CPU-intensive computation.
Reply With Quote
  #7  
Old 08-06-2008, 05:42 PM
dpb
Guest
 
Default Re: Fortran vs. Matlab on linear algebra

Gib Bogle wrote:
....
> I will see if I can the comparison with MKL. The reason I did this
> quick test is that I have a student who is solving a nonlinear system of
> PDEs using Matlab, and this problem requires extremely long execution
> times. I've always had the idea (prejudice?) that Matlab was slow
> compared with Fortran, and thought that in the last resort he could
> recode it in Fortran (with his level of programming skill, not a
> decision to take lightly). What this test tells me is that for this
> kind of matrix operation Matlab is much better than I'd realized. I do
> understand that it is inefficient on code with nested loops etc.

....
If you're looking to optimize an existing solution in Matlab, you'll get
lots of suggestions from comp.soft-sys.matlab regarding any particular
coding style.

And, of course, there's always the possibility once algorithmic and/or
obvious recoding is done of using the compiler to compile the Matlab code.

If one is dealing w/ a relatively modern version of Matlab there have
been significant improvements in it in some areas as well as for the
efficiency of the code in jit-compilation, etc., so that even loops are
no longer strictly interpreted.

But, as has been noted, once one gets to the core routines in ML it's
likely they're about as good as it gets w/o really serious work unless
have multi-processor and can take advantage of them.

--
Reply With Quote
  #8  
Old 08-06-2008, 06:04 PM
none
Guest
 
Default Re: Fortran vs. Matlab on linear algebra

On Wed, 06 Aug 2008 13:45:21 +1200, Gib Bogle 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.


On my PC ifort 9.1 took 2.95 s, while Matlab 7.1 took 2.27 s.
Replacing MATMUL with an explicit set of DO loops took 13 s.

The difference would seem likely to be that Intel are not offering their
best MATMUL as standard with their compiler.

Reply With Quote
  #9  
Old 08-06-2008, 06:45 PM
James Giles
Guest
 
Default Re: Fortran vs. Matlab on linear algebra

dpb wrote:
....
> But, as has been noted, once one gets to the core routines in ML it's
> likely they're about as good as it gets w/o really serious work unless
> have multi-processor and can take advantage of them.



I understand you intend ML to be an abbreviation for Matlab in
this context. But there is a language called ML. Probably better
not to confuse them.

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare

"Simplicity is prerequisite for reliability" -- E. W. Dijkstra


Reply With Quote
  #10  
Old 08-06-2008, 06:52 PM
Richard Maine
Guest
 
Default Re: Fortran vs. Matlab on linear algebra

James Giles <jamesgiles@worldnet.att.net> wrote:

> dpb wrote:
> ...
> > But, as has been noted, once one gets to the core routines in ML it's
> > likely they're about as good as it gets w/o really serious work unless
> > have multi-processor and can take advantage of them.

>
> I understand you intend ML to be an abbreviation for Matlab in
> this context. But there is a language called ML. Probably better
> not to confuse them.


Is that Mock Lisp or something else? Hmm. A quick google answers my own
question. It appears to be something else. But that just reinforces your
point about the confusion, as Mock Lisp was one of the things that
occurred to me when I read your post.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:03 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.