PDA

View Full Version : Fortran 90 Vs Matlab


EliotHijano
Apr8-10, 12:07 PM
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.

minger
Apr8-10, 01:19 PM
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.

EliotHijano
Apr8-10, 01:41 PM
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.

jtbell
Apr8-10, 04:51 PM
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.

EliotHijano
Apr9-10, 01:35 AM
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?

Holmz
Apr9-10, 03:58 AM
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...