Fortran 90 vs Matlab: Comparing Program Speed

  • Context: MATLAB 
  • Thread starter Thread starter EliotHijano
  • Start date Start date
  • Tags Tags
    Fortran Matlab
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.
 
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
5K
  • · 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