Fortran compiler strangeness

Tobias Burnus burnus@net-b.de
Wed May 9 15:51:00 GMT 2007


Hi Stefano,

I can not reproduce your timings. Here, the Fortran and C program take
essentially equally long, the float/real(8) version being about twice as
fast.

Notes:
a) I had to use "volatile" in C otherwise the optimizer would have
optimized the loop way.
(Unfortunally, the loops are not optimized away in Fortran!)

b) I changed the loop order. In C:
  for(i = 0; i < NX; i++)
    for(j = 0; j < NY; j++)
      A[i][j] = sinf(3.0f+i+j);
in Fortran:
      do j=1,Ny
        do i=1,Nx
          A(i,j)=sin(1.d0+i+j);

Results with today's gcc/gfortran 4.3 on a x86-64 (Athlon64) with the
options "-O2 -ftree-vectorize":

Double precision argument to SIN:
C:  real    0m11.993s
Fortran: real    0m10.718s

Single precision argument to SIN:
C: real    0m6.302s
Fortran: real    0m5.689s

Tobias


C program used:
-----------------------

#include <math.h>
#define NX 10000
#define NY 10000
int main()
{
  volatile double A[NX][NY], X[NX],Y[NY];
  int i, j;
  for(i = 0; i < NX; i++)
    X[i] = 2./(NX+1)*i;

  for(i = 0; i < NY; i++)
    Y[i] = 2./(NY+1)*i;

  for(i = 0; i < NX; i++)
    for(j = 0; j < NY; j++)
      A[i][j] = sin(3.0+i+j);
//      A[i][j] = sinf(3.0f+i+j);

  return 0;
}

-----------------------

Fortran program used:
-----------------------

      program prova
      implicit none
      integer :: i, j, Nx, Ny
      parameter(Nx=10000,Ny=10000)
      real(8), volatile :: A(Nx,Ny),X(Nx),Y(Ny)
      do i=1,Nx
       X(i)=2./(Nx+1)*i-1
      end do
      do i=1,Ny
       Y(i)=2./(Ny+1)*i-1
      end do
      do j=1,Ny
        do i=1,Nx
          A(i,j)=sin(1.d0+i+j);
!          A(i,j)=sin(1.e0+i+j);
        end do
      end do
      end program

-----------------------



More information about the Fortran mailing list