Sequential Program

Open the sample file: mcpi_sequential.f90 (the sample file is located in the src folder for command-line builds). The named constants for integer and real kinds are declared using SELECTED_INT_KIND and SELECTED_REAL_KIND. Select an integer kind that can hold large integers. The num_trials is declared as the number of trials. It is set to 6,000,000 in this example. The variable total counts the number of points that are found within the circle.

The Fortran standard intrinsic RANDOM_NUMBER is used to generate the points for testing. The standard does not say if the random sequence is different for each run of the program, so it is called RANDOM_SEED with no arguments. Intel® Fortran uses the time-of-day clock to initialize the random number generator.

The main body of the program is this loop:

! Run the trials. Get a random X and Y and see if the position
! is within a circle of radius 1. If it is, add one to the subtotal
do bigi=1_K_BIGINT,num_trials
    call RANDOM_NUMBER(x)
    call RANDOM_NUMBER(y)
    if ((x*x)+(y*y) <= 1.0_K_SINGLE) total = total + 1_K_BIGINT
end do

At the end of the trials, divide the total by the number of trials and then multiply by four:

! total/num_trials is an approximation of pi/4
print *, "Computed value of pi is",&
    REAL(4_K_BIGINT*total,K_DOUBLE)/REAL(num_trials,K_DOUBLE)

Note

The REAL intrinsic is used to convert the integers to double precision before dividing.

The program includes code to show the elapsed time for the application.

For more complete information about compiler optimizations, see our Optimization Notice.