Fortran 90 vs Matlab: Comparing Program Speed

  • Context: MATLAB 
  • Thread starter Thread starter EliotHijano
  • Start date Start date
  • Tags Tags
    Fortran Matlab
Click For Summary

Discussion Overview

The discussion centers on the performance comparison between Fortran 90 and MATLAB when executing a specific numerical program. Participants explore reasons for the observed slower execution speed of the Fortran code compared to its MATLAB counterpart, focusing on coding practices, compiler optimizations, and potential improvements.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that the Fortran variable 'Yr' is defined as complex, suggesting it should be a real variable to potentially improve speed.
  • Another participant questions the use of the FORALL intrinsic, recommending the use of nested DO loops instead.
  • A participant reports that changes made to the Fortran code improved speed slightly but it remains slower than MATLAB.
  • There is a suggestion that the Fortran compiler may not be optimizing the code by default, prompting a request for information about the compiler being used.
  • One participant provides an alternative Fortran code implementation, emphasizing the use of array operations to leverage Fortran 90's capabilities.
  • Concerns are raised about the overall effectiveness of the Fortran program, with a participant commenting that it is not performing significant computations.

Areas of Agreement / Disagreement

Participants express differing opinions on the reasons for the performance discrepancy, with no consensus on the most effective solution or the underlying cause of the slower execution in Fortran.

Contextual Notes

Participants discuss various coding practices and compiler settings, but there are unresolved aspects regarding the optimization capabilities of the Fortran compiler and the specific configurations used by participants.

EliotHijano
Messages
16
Reaction score
0
Hello.
I am trying to translate a program written in MATLAB to a fortran code and I have found out that the fortran code is slower. I couldn't believe it so I decided to do a simple program in fortran 90 and MATLAB in order to be sure. The code in MATLAB is:

function D2D()
Nx=2^11;
Ny=2^9;
Yr=ones(Ny,Nx);
for i=1:3000
Yr=Yr.*Yr;
end
'done'
end


and the code in fortran 90 is:

program D2D
IMPLICIT NONE
integer, parameter::NxL=11
integer, parameter::NyL=9
integer, parameter::Nx=(2**NxL) ! Grid size X
integer, parameter::Ny=(2**NyL) ! Grid size Y
INTEGER::i,IC,JC
complex*8, dimension(Ny,Nx):: Yr

DO i=1,3000
FORALL(IC=1:Ny,JC=1:Nx) Yr(IC,JC)=Yr(IC,JC)*Yr(IC,JC)
ENDDO
WRITE(*,*) 'done'

end program D2D


The fortran code is twice slower. Any idea why? What am I coding wrong in fortran?
thx.
 
Physics news on Phys.org
For starters you have Yr defined as a complex variable, it should simply be a REAL. I will go out on a limb and say that will speed up your code a lot.

Secondly, I'm not familiar with the FORALL intrinsic, but perhaps simply use nested DO loops, making sure that they are in the right order.

If your Matlab code is running faster than your F90, then somethings afoot.
 
Thanks for the answer minger. I have tried those changes, and althought they sightly improve the speed, it is still a lot slower than matlab. The code in fortran now looks like this:

program D2D
IMPLICIT NONE
integer, parameter::NxL=11
integer, parameter::NyL=9
integer, parameter::Nx=(2**NxL) ! Grid size X
integer, parameter::Ny=(2**NyL) ! Grid size Y
INTEGER::i,IC,JC
REAL, dimension(Ny,Nx):: Yr

DO i=1,3000
DO IC=1,Nx,1
DO JC=1,Ny,1
Yr(JC,IC)=Yr(JC,IC)*Yr(JC,IC)
ENDDO
ENDDO
ENDDO
WRITE(*,*) 'done'

end program D2D


What do you mean with If your Matlab code is running faster than your F90, then somethings afoot. ?

thanks in advance.
 
Perhaps your Fortran compiler does not optimize its code, by default, and you need to "turn on" optimization somehow. If you tell us which compiler you're using, and which environment (Unix/Linux command line, etc.), perhaps someone can give more information about this.
 
I am using the compiler Plato. I have also tried with Scite.
My operating system is Windows Vista Home Premium Service Pack 1. (64 bits).
Any suggestion?
 
EliotHijano said:
Thanks for the answer minger. I have tried those changes, and althought they sightly improve the speed, it is still a lot slower than matlab. The code in fortran now looks like this:

program D2D
IMPLICIT NONE
integer, parameter::NxL=11
integer, parameter::NyL=9
integer, parameter::Nx=(2**NxL) ! Grid size X
integer, parameter::Ny=(2**NyL) ! Grid size Y
INTEGER::i,IC,JC
REAL, dimension(Ny,Nx):: Yr

DO i=1,3000
DO IC=1,Nx,1
DO JC=1,Ny,1
Yr(JC,IC)=Yr(JC,IC)*Yr(JC,IC)
ENDDO
ENDDO
ENDDO
WRITE(*,*) 'done'

end program D2D


What do you mean with If your Matlab code is running faster than your F90, then somethings afoot. ?

thanks in advance.

The advantage of F90 is not being used... Try this code.

program D2D
IMPLICIT NONE
INTEGER(KIND=4), PARAMETER :: NxL=11
INTEGER(KIND=4), PARAMETER :: NyL=9
INTEGER(KIND=4), PARAMETER :: Nx=(2**NxL) ! Grid size X
INTEGER(KIND=4), PARAMETER :: Ny=(2**NyL) ! Grid size Y
INTEGER(KIND=4) ::i !,IC,JC
REAL(KIND=8), dimension(Ny,Nx) :: Yr
REAL(KIND=8), dimension(Ny,Nx) :: Zr
LOGICAL(KIND=8), dimension(Ny,Nx) :: Mask
LOGICAL(KIND=8) :: Ken_Oath = .TRUE.

DO i=1,3000
! DO IC=1,Nx,1
! DO JC=1,Ny,1
! Yr(JC,IC)=Yr(JC,IC)*Yr(JC,IC)
Yr(:,:)=Yr(:,:)*Yr(:,:)
! ENDDO
! ENDDO
ENDDO
WRITE(*,*) 'done 1'!-or-
Zr(:,:) = 0.0E0
Zr(:,:)=Yr(:,:)*Yr(:,:)
WRITE(*,*) 'done 2'

!-or-
Zr(:,:) = 0.0E0
Zr(:,:)=Yr(:,:)**2
WRITE(*,*) 'done 3'
!-or-
Mask(:,:) = Ken_Oath
WHERE MASK(:,:)
Zr(:,:) = Yr(:,:)**2
ENDHWERE

end program D2D


The program is not doing much...
Yr should always be zero...
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K