Introduction to MPI, Monte Carlo integration and some parallel

Introduction to MPI, Monte Carlo integration
and some parallel software
Benson Muite
[email protected]
http://kodu.ut.ee/˜benson
http://en.wikibooks.org/wiki/Parallel_Spectral_Numerical_Methods
1 April 2015
Outline
• Parallel Programs, Hello World
• Monte Carlo Method
• Fast Fourier Transform Libraries
• Some Simulation Packages
• References
Parallel Computing
• Due to limitations in device physics to get faster results,
instead of increasing the clock frequency, want to have
many devices at not took high frequency. That is many
under clocked chips rather than a few over clocked chips
• For more on this, take parallel computing course. We give
a brief glimpse here.
• Will use Rocket, Fortran (primarily Fortran90 though
knowledge of Fortran77 is still useful) and MPI, though
similar ideas and methods work on other parallel
computers such as your cell phone, laptop, video game
console etc.
Parallel Computing
• Main idea is to split up work into independent tasks with as
few synchronisations as possible
• Kalev, Kamau and Khruschev (or Akhmatova, Algi and
Atieno) need to make 99 salads
• Possible organizational options:
• Kalev, Kamau and Khruschev each make 33 salads.
• Simultaneously Kalev chops beetroots, Kamau chops
tomatoes, Khruschev chops carrots. They then mix the
ingredients in one bowl.
• Can you come up with others? What is the fastest
assuming each of the three people are identically good at
all the tasks? What if some are better at some tasks than
others?
Parallel Computing: MPI
• MPI (Message Passing Interface) - one way of making a
program parallel
• Standard can be found at:
http://www.mpi-forum.org
• Use a library to allow computers to talk to each other by
sending messages and having some explicit co-ordination
• MPI works for C and Fortran programs. Some unofficial
bindings available for other programs, such as Python:
http://mpi4py.scipy.org/
http://pythonprogramming.net/
learning-use-mpi-python-mpi4py-module/
• Many online resources available including:
https://computing.llnl.gov/tutorials/mpi/
http://www.shodor.org/refdesk/Resources/
Tutorials/BasicMPI/
Hello World Example Program - Sage + Python +
MPI4PY
from mpi4py import MPI
comm = MPI .COMM WORLD
print ( ” h e l l o world ” )
p r i n t ( ”my rank i s : %d ”%comm. rank )
Listing 1: A Python program which demonstrates parallelizm using MPI.
Hello World Example Rocket Submission Script Sage + Python + MPI4PY
# ! / b i n / bash
#SBATCH −N 1
#SBATCH −−ntasks−per−node=5
#SBATCH −t 0 0 : 1 0: 0 0
module l o a d sage −6.1.1
mpirun sage −python mpi 1 . py
Listing 2: An example submission script for use on Rocket.
Hello World Example Program - Fortran
https://bitbucket.org/bkmbitbucket/
parallelintrorocket/src/
9ebbb5407c524f170ca5c32b8a794bf7c7ff0036/
HelloworldMPI/helloworld.f90?at=master
Hello World Example Rocket Submission Script Fortran
# ! / b i n / bash
#SBATCH −N 2
#SBATCH −−tasks−per−node=20
#SBATCH −t 0 0 : 1 0: 0 0
module l o a d i n t e l p a r a l l e l s t u d i o x e 2 0 1 5
export I MPI PMI LIBRARY = / u s r / l i b 6 4 / l i b p m i . so
srun h e l l o
# c l e a n up
rm h e l l o
Listing 3: An example submission script for use on Rocket.
Monte Carlo Method: A Probabilistic Way to Calculate
Integrals
• Recall
¯f =
1
b−a
Z
b
f (x) dx
a
• Hence given ¯
f
Z
b
f (x)dx = (b − a)¯f
a
• Doing the same in 2 dimensions and estimating the error
using the standard deviation
s
ZZ
f (x, y ) dA ≈ A(R)¯f ± A(R)
f 2 − (¯f )2
,
N
R
• Approximate ¯
f by random sampling
¯f ≈
PN
i=1 f (xi , yi )
N
PN
and
f2
≈
2
i=1 (f (xi , yi ))
N
Monte Carlo Method: Python Program
”””
A program t o approximate an i n t e g r a l u s i n g a Monte C a r l o method
T h i s c o u l d be made f a s t e r by u s i n g v e c t o r i z a t i o n , however i t i s
k e p t as s i m p l e as p o s s i b l e f o r c l a r i t y and ease o f t r a n s l a t i o n i n t o
o t h e r languages
”””
import math
import numpy
import t i m e
numpoints =65536 # number o f random sample p o i n t s
I 2 d =0.0
# i n i t i a l i z e value
I2dsquare =0.0
# i n i t i a l i z e to allow f o r c a l c u l a t i o n of variance
f o r n i n xrange ( numpoints ) :
x=numpy . random . u n i f o r m ( )
y =4.0∗numpy . random . u n i f o r m ( )
I 2 d = I 2 d +x∗x +2.0∗ y∗y
I2dsquare = I2dsquare +( x∗x +2.0∗ y∗y)∗∗2
# we s c a l e t h e i n t e g r a l by t h e t o t a l area and d i v i d e by t h e number o f
# p o i n t s used
I 2 d = I 2 d / numpoints
I2dsquare = I2dsquare / numpoints
E s t i m E r r o r =4.0∗numpy . s q r t ( ( I2dsquare − I 2 d ∗∗2)/ numpoints ) # e s t i m a t e d e r r o r
I 2 d = I 2 d ∗4.0
p r i n t ” Value : %f ” %I 2 d
p r i n t ” E r r o r e s t i m a t e : %f ” %E s t i m E r r o r
Listing 4: A Python program which demonstrates how to use the
Monte Carlo method to calculate the volume below z = x 2 + 2y 2 ,
with (x, y ) ∈ (0, 1) × (0, 4).
Sample Results of Monte Carlo Program
N
16
256
4096
65536
∞
Value
37.09
44.49
44.50
43.80
44
Error Estimate
+/- 10.89
+/- 2.40
+/- 0.60
+/- 0.14
0.0
See http://en.wikibooks.org/wiki/Parallel_
Spectral_Numerical_Methods/Introduction_to_
Parallel_Programming for more information
Monte Carlo Method: Serial Fortran Program
https://bitbucket.org/bkmbitbucket/
parallelintrorocket/src/
9ebbb5407c524f170ca5c32b8a794bf7c7ff0036/
montecarloserial/montecarloserial.f90?at=
master
Monte Carlo Method: MPI Fortran Program
https://bitbucket.org/bkmbitbucket/
parallelintrorocket/src/
9ebbb5407c524f170ca5c32b8a794bf7c7ff0036/
montecarloparallel/montecarloparallel.f90?at=
master
Parallel Fast Fourier Transform Libraries
• FFTW http://www.fftw.org/
• 2decomp&fft 2decomp&fft.org
• p3dfft https://code.google.com/p/p3dfft/
• pfft https://github.com/mpip/pfft
Simulation Packages
• Lattice Boltzmann
• OpenLB http://optilb.org/openlb/
• Finite Volume
• Fire Dynamics Simulator
https://code.google.com/p/fds-smv/
• OpenFOAM http://www.openfoam.org/index.php
• PyClaw http:
//www.clawpack.org/doc/pyclaw/index.html
• Finite Element
• Deal II http://www.dealii.org/
• Elmer https://www.csc.fi/web/elmer
• Fenics http://fenicsproject.org/
• FreeFEM++
http://www.freefem.org/ff++/index.htm
• Moose http://mooseframework.com/
New Key Concepts
• Parallel Computing – MPI
• Monte Carlo Method – method of calculating integrals
• Many parallel packages freely available – become part of a
community
References
• Barth, T. and Ohlberger, M. “Finite Volume Methods: Foundation and Analysis”
https://archive.org/details/nasa_techdoc_20030020790
• Boyd, J.P. “Chebyshev and Fourier Spectral Methods” 2nd. edition http:
//www-personal.umich.edu/˜jpboyd/BOOK_Spectral2000.html
• Chen G., Cloutier B., Li N., Muite B.K., Rigge P. and Balakrishnan S., Souza A.,
•
•
•
•
•
•
•
•
West J. “Parallel Spectral Numerical Methods” http://shodor.org/
petascale/materials/UPModules/Parallel_Spectral_Methods/
Corral M. Vector Calculus http://www.mecmath.net/
Cooley J.W. and Tukey J.W. “An algorithm for the machine calculation of complex
Fourier series” Math. Comput. 19, 297–301, (1965)
¨ T. and Herbin, R. “Finite Volume Methods”
Eymard, R. Gallouet,
http://www.cmi.univ-mrs.fr/˜herbin/BOOK/bookevol.pdf
Greenbaum A. and Chartier T.P. Numerical Methods Princeton University Press
(2012)
Heideman M.T., Johnson D.H. and Burrus C.S. “Gauss and the History of the
Fast Fourier Transform” IEEE ASSP Magazine 1, 14–21, (1984)
Ketcheson, D. “Hyperpython” https://github.com/ketch/HyperPython/
Lawler, G.F. “Random Walk and the Heat Equation” American Mathematical
Society (2010)
Macqueron, C. “Computational Fluid Dynamics Modeling of a wood-burning
stove-heated sauna using NIST’s Fire Dynamics Simulator”
http://arxiv.org/abs/1404.6774
References
• McGrattan, K. et al. Fire Dynamics Simulator
http://www.nist.gov/el/fire_research/fds_smokeview.cfm
• Petersen W.P. and Arbenz P. Introduction to Parallel Computing Oxford University
Press (2004)
• Quarteroni, A. Sacco, R. and Saleri, F. “Numerical Mathematics”
http://link.springer.com/book/10.1007%2Fb98885
• Sauer T. Numerical Analysis Pearson (2012)
• Suli,
¨ E. “Finite element methods for partial differential equations”
people.maths.ox.ac.uk/suli/fem.pdf
• Trefethen, L.N. “Finite Difference and Spectral Methods for Ordinary and Partial
Differential Equations”
http://people.maths.ox.ac.uk/trefethen/pdetext.html
• Vainikko E. “Scientific Computing Lectures”
https://courses.cs.ut.ee/2013/scicomp/fall/Main/Lectures
¨
• Vainikko E. Fortran 95 Ja MPI Tartu Ulikool
Kirjastus (2004)
http://kodu.ut.ee/˜eero/PC/F95jaMPI.pdf
Acknowledgements
• Oleg Batra˘sev
• Leonid Dorogin
• Michael Quell
• Eero Vainikko
• The Blue Waters Undergraduate Petascale Education Program
administered by the Shodor foundation
• The Division of Literature, Sciences and Arts at the University of
Michigan
Extra Slides
Fun with Fourier Series
• A Fourier series can represent a regular k-gon in the
complex plane
P
−2 exp [i(1 + kn)t]
• f (t) = +∞
n=−∞ (1 + kn)
t ∈ (−π, π)
Robert, A “Fourier Series of Polygons” Amer. Math. Monthly 101(5) pp. 420-428 (1994)
Schonberg, IJ “The Finite Fourier Series And Elementary Geometry” Amer. Math. Monthly 57(6) pp. 390-404 (1950)