Play With the Inverse Program
In this simple exercise we will check what happens when a real number cannot be represented correctly by a floating point one and some approximations and rounding are therefore introduced. In the following program we will compute the inverse of the first N numbers and we then multiply it by the number itself. You will discover that using floating point arithmetic the result will not be one for all the numbers.
If you're not already there, go to the directory where you downloaded the school material and (eg. /home/myname/hpc-2009) and type
$ cd inverse
In this directory you will find the code for the exercise in a file called inverse.f90 which you can see also here
program inverse program inverse !a simple program to check how many inverse are not accurate. real*4 :: X,Y,Z integer:: I i=0 do j=1,100,1 X=real(j) Y=1.0/X Z=Y*X IF (Z.ne.1.0) then write(*,'(A,F28.26)') "this is not correct=", Z i=i+1 end if end do write(*,*)"found", I end program inverse
Take a look at it and then do the following task:
- step1: Use single precision and identify how many numbers are wrong..
- You are invited to play with gfortran compiler first using the following options:
- gfortran (default: no explicit option)
- gfortran -Ox with x=1,2,3 (standard optimization)
- gfortran -ffloat-store (strictly follow IEEE standard)
- You can then try Intel compiler:
- ifort (default: no explicit option)
- ifort -Ox with x=1,2,3 (standard optimization)
- ifort -mp -O3 (strictly follow IEEE standard)
- and the pgf90 compiler
- pgf90 (default: no explicit option)
- pgf90 -Ox with x=1,2,3 (standard optimization)
- pgf90 -Kieee -O3 (strictly follow IEEE standard)
- You are invited to play with gfortran compiler first using the following options:
Optional: If you have installed the g95 compiler check its manual and try to understand which are the equivalents to the flags you passed to the other compilers and then repeat the exercise with it.
- step2: edit the file and change the type of XYZ from real*4 to real*8
- Modify the source code and repeat step 1. Remember NOT to paste the content of this wiki in your files, but use ONLY the files you downloaded via svn.
Repeat the same operation as in step1 using the three different F90 compilers (gfortran/pgf90/ifort) and with the same flags as above.
At the end of your exercises and tests try to answer the following questions:
- How many inverses are not correctly computed in real*4 precision ? and with real*8 ?
- Does the number depend on the level of optimization ?
- Do you have any explanation about the Intel results with respect of what you can observe gfortran/pgf90 ?