The Magic of Compilers


One of the issues that plagues weather data processing–like any other data–is speed…we want our science processed and we want it processed fast!

I refactored some of my algorithm code today that is in FORTRAN–specifically the parts where I look at static output flags specified at compilation time that determine what variables I need to calculate. A thought that has been kicking around in my head is the time hit my program takes whenever it checks the output flags every time in a 175,000 iteration loop. You would think I could simply move this to something outside of the loop, but to do so would unfortunately require a 2,000,000 x 175,000 sized real array. I normally use the -O3 flag when compiling to provide the strictest optimization though I was not completely sure whether that was a high enough optimization to take these sections. So, I decided to run some tests and see if the compiler removed the null flagged sections within the loop.

(These tests are run on a 24 core Intel Xeon X5650 2.67 GHz, 50 GB RAM machine.)

I constructed a program that would basically make it obvious whether or not a null flagged if statement is removed. It consists of a simple loop inside an if flag statement inside of another simple loop:

PROGRAM TEST
IMPLICIT NONE
INTEGER           :: flag, i, j
REAL, ALLOCATABLE :: a(:,:)
REAL              :: start, finish

flag = 0

ALLOCATE(a(1000000, 30))

CALL CPU_TIME(start)
PRINT *, start

DO i = 1, 1000000000
    IF (flag .EQ. 1) THEN
        DO j = 1, 1000000
            a(i,4) = RAND(0)
        END DO
    END IF
END DO

CALL CPU_TIME(finish)
PRINT *, finish

END PROGRAM TEST

I compiled this program twice, once without and once with the the optimization flag:

$ gfortran test.f90 -o non-optimized
$ gfortran -O3 test.f90 -o optimized

I then ran the two tests to see what the result was:

$ ./non-optimized
   0.0000000    
   2.6605940
$ ./optimized
   0.0000000
   0.0000000

So, it looks like the compiler completely removed the if block.

[top]