Fortran 90 vs Matlab: Comparing Program Speed

In summary: What is Ken_Oath?I am using the compiler Plato. I have also tried with Scite.I would recommend trying one of the following:-Using a different compiler-Using a different grid size-Using a different mask
  • #1
EliotHijano
18
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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?
 
  • #6
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...
 

Related to Fortran 90 vs Matlab: Comparing Program Speed

What is Fortran 90 and Matlab?

Fortran 90 and Matlab are both programming languages commonly used in scientific and engineering applications. Fortran 90 (short for Formula Translation 90) is a high-level, compiled language primarily used for numerical and scientific computing. Matlab (short for Matrix Laboratory) is an interactive, interpreted language designed for numerical analysis and data visualization.

How do Fortran 90 and Matlab differ in terms of program speed?

In general, Fortran 90 is faster than Matlab in terms of program speed. This is because Fortran 90 is a compiled language, meaning that the code is translated into machine code before execution, while Matlab is an interpreted language, meaning that the code is translated and executed line by line during runtime.

What are the advantages of using Fortran 90 over Matlab?

Some advantages of using Fortran 90 over Matlab include faster program speed, more control over memory management, and better support for parallel processing. Fortran 90 also has a longer history in scientific computing and a larger community of users.

What are the advantages of using Matlab over Fortran 90?

Some advantages of using Matlab over Fortran 90 include a more user-friendly interface and built-in functions for data analysis and visualization. Matlab also has a larger library of third-party tools and a more intuitive syntax, making it easier for beginners to learn.

Which language should I use for my scientific or engineering project?

The choice between Fortran 90 and Matlab depends on the specific needs and requirements of your project. If speed is a top priority and you have experience in programming, Fortran 90 may be a better choice. If your project involves data analysis and visualization, or you are new to programming, Matlab may be a better option. It is also possible to combine the two languages for different parts of your project based on their strengths.

Similar threads

  • Programming and Computer Science
Replies
4
Views
634
  • Programming and Computer Science
Replies
1
Views
945
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
956
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top