Play with the sum Numbers Program

The sum of the first N integer puzzle

In this section you are invited to play with a simple program (sum_numbers.f90) that performs the sum of the first N integer numbers. Program is written in F90 and is placed in the sum_number directory. Goal of this exercise is to understand if there is any difference in computing this sum in three different way:

  1. using the well known formula Gauss discovered in primary school

(see http://www.sigmaxi.org/amscionline/gauss-snippets.html for a collection)

  1. summing from the smallest one to largest one
  2. summing the other way around: from the largest one to the smallest one

All the three methods are implemented using integer and float representation. Method one is just a simple formula:

...
   gauss_formula_float=(real(k)*real(k+1))/2.0
   gauss_formula_int  =(k*(k+1))/2.0
   write(*,'(a,i20,g25.15)') '
   #the mathematical correct result:[n(n+1)/2*]=',gauss_formula_int,gauss_formula_float
... 

gauss_formula_float stores the result computed using floating point numbers while gauss_formula_int stores result using integer numbers.

The two sums are performed in the following loop again using integer

(i_dir and irev) and real (directly/reversly) variable

...
 do i=1,nsteps
     i_dir = i_dir + i
     ilarge=nsteps +1-i
     i_rev = i_rev + ilarge
     directly = directly + real(i)
     reversly = reversly + real(ilarge)
   end do
... 

As you can see this is a trivial program but results can be surprising. Please familiarize with it, taking a look at the code then compile it and play a little bit with the three different executable. Try then to answer the questions proposed at the end of the exercise.

Task to perform

  • Compilation (straight forward)
$ cd ..
$ cd sum_numbers 

$ gfortan sum_numbers.f90 -o sum_numbers.x 

We also provide a Makefile (feel free to improve it) to compile in a single shot three different executable: one for each compiler:

$ make clean; make all

$ make clean
/bin/rm -f sum_gfortran.x  sum_intel.x   sum_pgi.x
/bin/rm -f *.s *.w2f.f

$ make all
gfortran -o sum_gfortran.x   \
        sum_numbers.f90
ifort -o sum_intel.x   \
        sum_numbers.f90
pgf90 -o sum_pgi.x   \
        sum_numbers.f90
NOTE: your trial license will expire in 9 days, 11.8 hours.
NOTE: your trial license will expire in 9 days, 11.8 hours.

or straight forward without using Makefile

$ ifort -O0 -o sum_intel.x    sum_numbers.f90
$ pgf90 -O0 -o sum_pgi.x    sum_numbers.f90
$ gfortran -O0 -o sum_gfortran.x    sum_numbers.f90

$ ls *.x
sum_gfortran.x  sum_intel.x  sum_pgi.x
  • Run the program:

Here below a typical session in using the program: it will ask for an integer and it will print some results:

$ ./sum_gfortran.x 
 #This program computes the sum of the first N numbers
 #It performs the operation using Integer numbers and FP numbers
 #and prints out the results of both Integer and FP sum
 #Operation is performed in two ways:
 #  1.summing from 1 to N (from the smallest to the largest)
 #  2.summing from N to 1 (from the largest  to the smallest)
 # enter N: 
5600
#the mathematical correct result:[n(n+1)/2*]=            15682800     15682800.0000000    
 #summing integers:
#direct=          000000015682800reverse=            15682800
 #summing floating point  numbers
#direct=          000000015682800reverse=            15682800
 differences
   0.000000       0.000000
  • Play with the program with some suggested number:
    • N=5792
    • N=5793
    • N=48000

Observe and report the behavior of three compilers on these numbers Be sure to compile without any compilation flag.

recompile with -O0 ( no optimization enabled) for all the compiler (see Makefile for suggestion)

report the behavior in this case as well

Try to think a way to avoid problems you run into.