Example of IMSL use: Monte Carlo Calculation of π
By:
Igor Senderovich
See also: C version of this problem, with Condor submission instructions.
The Problem and Code
Consider Monte Carlo calculation of
π;
incorporating IMSL's random number generator.
The following Fortran program implements random sampling of points
withing a square bounding a circle.
(The probability of landing inside the circle can be shown to be
π/4)
! Request use of IMSL rand_gen_int library
use rand_gen_int
implicit none
integer i
integer, parameter :: n=5000000
real(kind(1e0)), parameter :: one=1e0, zero=0e0
real(kind(1e0)) x(n),y(n),count,pi
! Obtain random x,y coordinates in range [0,1]
call rand_gen(x)
call rand_gen(y)
! Count those that fell within a radius of 1
count=0
do i=1,n
if (x(i)*x(i)+y(i)*y(i) < 1.0) count=count+1
end do
pi=4.0*count/(n+1.0)
write (*,'(F15.10)') pi
end
Compilation and Execution
Saving this program as
calcpi_imsl.f90
and compiling with the
following command (based on standard environment variables configured on the cluster)
$F90 calcpi_imsl.f90 $F90FLAGS $LINK_FNL -o calcpi_imsl
where,
pgf90
stands in for
$F90
and, depending on the version of libraries, the the flags
may look like:
$F90FLAGS: -w -mp -tp k8-64 -Kieee -module /usr/local/vni/imsl/fnl600/rdhpg715x64/include
$LINK_FNL: -R/usr/local/vni/imsl/fnl600/rdhpg715x64/lib -L/usr/local/vni/imsl/fnl600/rdhpg715x64/lib -Bdynamic -limsl -limslsuperlu -limslscalar -limslblas -ldl -R/usr/local/pgi/linux86-64/7.1/libso
Of course, the flags may be taylored to the actual dependencies of the code, but having such standard sets of flags saved in the environment variables is convenient. The compiled binary can now be executed:
./calcpi_imsl
Independent runs should yield values like:
3.1415688992
3.1418640614
3.1421930790
3.1415233612
Note that while the IMSL libraries are available on all statistics cluster nodes
(labeled
stat0
through
stat31
) compilation with
PGI Fortran compilers is restricted to
stat31,
a.k.a stats.phys.uconn.edu
See Also