Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Parallel Programming in C with MPI and OpenMP Michael J. Quinn.

Similar presentations


Presentation on theme: "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Parallel Programming in C with MPI and OpenMP Michael J. Quinn."— Presentation transcript:

1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Parallel Programming in C with MPI and OpenMP Michael J. Quinn

2 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 Monte Carlo Methods

3 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Objectives Introduce Monte Carlo methods Introduce Monte Carlo methods Introduce techniques for parallel random number generation Introduce techniques for parallel random number generation

4 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Outline Monte Carlo method Monte Carlo method Sequential random number generators Sequential random number generators Parallel random number generators Parallel random number generators Generating non-uniform random numbers Generating non-uniform random numbers Monte Carlo case studies Monte Carlo case studies

5 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Monte Carlo Method Solve a problem using statistical sampling Solve a problem using statistical sampling Name comes from Monaco’s gambling resort city Name comes from Monaco’s gambling resort city First important use in development of atomic bomb during World War II First important use in development of atomic bomb during World War II

6 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Applications of Monte Carlo Method Evaluating integrals of arbitrary functions of 6+ dimensions Evaluating integrals of arbitrary functions of 6+ dimensions Predicting future values of stocks Predicting future values of stocks Solving partial differential equations Solving partial differential equations Sharpening satellite images Sharpening satellite images Modeling cell populations Modeling cell populations Finding approximate solutions to NP-hard problems Finding approximate solutions to NP-hard problems

7 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example of Monte Carlo Method Area = D 2 Area =  D 2 /4 D D

8 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example of Monte Carlo Method Area = D 2

9 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Absolute Error Absolute error is a way to measure the quality of an estimate Absolute error is a way to measure the quality of an estimate The smaller the error, the better the estimate The smaller the error, the better the estimate a: actual value a: actual value e: estimated value e: estimated value Absolute error = |e-a|/a Absolute error = |e-a|/a

10 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Increasing Sample Size Reduces Error nEstimateError 1/(2n 1/2 ) 102.400000.236060.15811 1003.360000.069520.05000 1,0003.144000.000770.01581 10,0003.139200.000760.00500 100,0003.141320.000090.00158 1,000,0003.140060.000490.00050 10,000,0003.141360.000070.00016 100,000,0003.141540.000020.00005 1,000,000,0003.141550.000010.00002

11 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Mean Value Theorem

12 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Estimating Mean Value The expected value of (1/n)(f(x 0 ) + … + f(x n-1 )) is f

13 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Why Monte Carlo Works

14 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Why Monte Carlo is Effective Error in Monte Carlo estimate decreases by the factor 1/n 1/2 Error in Monte Carlo estimate decreases by the factor 1/n 1/2 Rate of convergence independent of integrand’s dimension Rate of convergence independent of integrand’s dimension Deterministic numerical integration methods do not share this property Deterministic numerical integration methods do not share this property Hence Monte Carlo superior when integrand has 6 or more dimensions Hence Monte Carlo superior when integrand has 6 or more dimensions

15 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Parallelism in Monte Carlo Methods Monte Carlo methods often amenable to parallelism Monte Carlo methods often amenable to parallelism Find an estimate about p times faster Find an estimate about p times faster OR OR Reduce error of estimate by p 1/2 Reduce error of estimate by p 1/2

16 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Random versus Pseudo-random Virtually all computers have “random number” generators Virtually all computers have “random number” generators Their operation is deterministic Their operation is deterministic Sequences are predictable Sequences are predictable More accurately called “pseudo-random number” generators More accurately called “pseudo-random number” generators In this chapter “random” is shorthand for “pseudo- random” In this chapter “random” is shorthand for “pseudo- random” “RNG” means “random number generator” “RNG” means “random number generator”

17 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Properties of an Ideal RNG Uniformly distributed Uniformly distributed Uncorrelated Uncorrelated Never cycles Never cycles Satisfies any statistical test for randomness Satisfies any statistical test for randomness Reproducible Reproducible Machine-independent Machine-independent Changing “seed” value changes sequence Changing “seed” value changes sequence Easily split into independent subsequences Easily split into independent subsequences Fast Fast Limited memory requirements Limited memory requirements

18 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. No RNG Is Ideal Finite precision arithmetic  finite number of states  cycles Finite precision arithmetic  finite number of states  cycles  Period = length of cycle  If period > number of values needed, effectively acyclic Reproducible  correlations Reproducible  correlations Often speed versus quality trade-offs Often speed versus quality trade-offs

19 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Linear Congruential RNGs Multiplier Additive constant Modulus Sequence depends on choice of seed, X 0

20 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Period of Linear Congruential RNG Maximum period is M Maximum period is M For 32-bit integers maximum period is 2 32, or about 4 billion For 32-bit integers maximum period is 2 32, or about 4 billion This is too small for modern computers This is too small for modern computers Use a generator with at least 48 bits of precision Use a generator with at least 48 bits of precision

21 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Producing Floating-Point Numbers X i, a, c, and M are all integers X i, a, c, and M are all integers X i s range in value from 0 to M-1 X i s range in value from 0 to M-1 To produce floating-point numbers in range [0, 1), divide X i by M To produce floating-point numbers in range [0, 1), divide X i by M

22 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Defects of Linear Congruential RNGs Least significant bits correlated Least significant bits correlated  Especially when M is a power of 2 k-tuples of random numbers form a lattice k-tuples of random numbers form a lattice  Especially pronounced when k is large

23 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Lagged Fibonacci RNGs p and q are lags, p > q p and q are lags, p > q * is any binary arithmetic operation * is any binary arithmetic operation Addition modulo M Addition modulo M Subtraction modulo M Subtraction modulo M Multiplication modulo M Multiplication modulo M Bitwise exclusive or Bitwise exclusive or

24 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Properties of Lagged Fibonacci RNGs Require p seed values Require p seed values Careful selection of seed values, p, and q can result in very long periods and good randomness Careful selection of seed values, p, and q can result in very long periods and good randomness For example, suppose M has b bits For example, suppose M has b bits Maximum period for additive lagged Fibonacci RNG is (2 p -1)2 b-1 Maximum period for additive lagged Fibonacci RNG is (2 p -1)2 b-1

25 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Ideal Parallel RNGs All properties of sequential RNGs All properties of sequential RNGs No correlations among numbers in different sequences No correlations among numbers in different sequences Scalability Scalability Locality Locality

26 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Parallel RNG Designs Manager-worker Manager-worker Leapfrog Leapfrog Sequence splitting Sequence splitting Independent sequences Independent sequences

27 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Manager-Worker Parallel RNG Manager process generates random numbers Manager process generates random numbers Worker processes consume them Worker processes consume them If algorithm is synchronous, may achieve goal of consistency If algorithm is synchronous, may achieve goal of consistency Not scalable Not scalable Does not exhibit locality Does not exhibit locality

28 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Leapfrog Method Process with rank 1 of 4 processes

29 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Properties of Leapfrog Method Easy modify linear congruential RNG to support jumping by p Easy modify linear congruential RNG to support jumping by p Can allow parallel program to generate same tuples as sequential program Can allow parallel program to generate same tuples as sequential program Does not support dynamic creation of new random number streams Does not support dynamic creation of new random number streams

30 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sequence Splitting Process with rank 1 of 4 processes

31 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Properties of Sequence Splitting Forces each process to move ahead to its starting point Forces each process to move ahead to its starting point Does not support goal of reproducibility Does not support goal of reproducibility May run into long-range correlation problems May run into long-range correlation problems Can be modified to support dynamic creation of new sequences Can be modified to support dynamic creation of new sequences

32 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Independent Sequences Run sequential RNG on each process Run sequential RNG on each process Start each with different seed(s) or other parameters Start each with different seed(s) or other parameters Example: linear congruential RNGs with different additive constants Example: linear congruential RNGs with different additive constants Works well with lagged Fibonacci RNGs Works well with lagged Fibonacci RNGs Supports goals of locality and scalability Supports goals of locality and scalability

33 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Other Distributions Analytical transformations Analytical transformations Box-Muller Transformation Box-Muller Transformation Rejection method Rejection method

34 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Analytical Transformation

35 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Exponential Distribution 1.0

36 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example 1: Produce four samples from an exponential distribution with mean 3 Produce four samples from an exponential distribution with mean 3 Uniform sample: 0.540, 0.619, 0.452, 0.095 Uniform sample: 0.540, 0.619, 0.452, 0.095 Take natural log of each value and multiply by -3 Take natural log of each value and multiply by -3 Exponential sample: 1.850, 1.440, 2.317, 7.072 Exponential sample: 1.850, 1.440, 2.317, 7.072

37 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example 2: Simulation advances in time steps of 1 second Simulation advances in time steps of 1 second Probability of an event happening is from an exponential distribution with mean 5 seconds Probability of an event happening is from an exponential distribution with mean 5 seconds What is probability that event will happen in next second? What is probability that event will happen in next second? 1/5 1/5 Use uniform random number to test for occurrence of event Use uniform random number to test for occurrence of event

38 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Box-Muller Transformation Cannot invert cumulative distribution function to produce formula yielding random numbers from normal (gaussian) distribution Cannot invert cumulative distribution function to produce formula yielding random numbers from normal (gaussian) distribution Box-Muller transformation produces a pair of standard deviates g 1 and g 2 from a pair of normal deviates u 1 and u 2 Box-Muller transformation produces a pair of standard deviates g 1 and g 2 from a pair of normal deviates u 1 and u 2

39 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Box-Muller Transformation repeat v 1  2u 1 - 1 v 2  2u 2 - 1 r  v 1 2 + v 2 2 until r > 0 and r < 1 f  sqrt (-2 ln r /r ) g 1  f v 1 g 2  f v 2

40 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example Produce four samples from a normal distribution with mean 0 and standard deviation 1 Produce four samples from a normal distribution with mean 0 and standard deviation 1 u1u1u1u1 u2u2u2u2 v1v1v1v1 v2v2v2v2rf g1g1g1g1 g2g2g2g2 0.2340.784-0.5320.5680.6051.290-0.6860.732 0.8240.0390.648-0.9211.269 0.4300.176-0.140-0.6480.4391.935-0.271-1.254

41 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Different Mean, Std. Dev. g 1  s f v 1 + m g 2  s f v 2 + m Standard deviationMean

42 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Rejection Method

43 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example Generate random variables from this probability density function Generate random variables from this probability density function

44 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example (cont.) So  h(x)  f(x) for all x

45 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example (cont.) xixixixi uiuiuiui u i  h(x i ) f(x i ) Outcome 0.8600.9750.6890.681Reject 1.5180.3570.2520.448Accept 0.3570.9200.6500.349Reject 1.3060.2720.1920.523Accept Two samples from f(x) are 1.518 and 1.306

46 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Case Studies (Topics Introduced) Neutron transport (Monte Carlo time) Neutron transport (Monte Carlo time) Temperature inside a 2-D plate (Random walk) Temperature inside a 2-D plate (Random walk) Two-dimensional Ising model (Metropolis algorithm) Two-dimensional Ising model (Metropolis algorithm) Room assignment problem (Simulated annealing) Room assignment problem (Simulated annealing) Parking garage (Monte Carlo time) Parking garage (Monte Carlo time) Traffic circle (Simulating queues) Traffic circle (Simulating queues)

47 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Neutron Transport

48 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example D (0-  ) Angleu(0-1)L (-ln u) LcosD Dist.Absorb?(0-1) 0.000.00.201.591.591.59 0.41 (no) 1.5589.20.341.080.011.60 0.84 (no) 0.4224.00.271.311.202.80 0.57 (no) 0.3319.40.600.520.493.29 Monte Carlo Time 3.0

49 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Temperature Inside a 2-D Plate Random walk

50 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example of Random Walk 13121322

51 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-D Ising Model

52 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Metropolis Algorithm Use current random sample to generate next random sample Use current random sample to generate next random sample Series of samples represents a random walk through the probability density function Series of samples represents a random walk through the probability density function Short series of samples highly correlated Short series of samples highly correlated Many samples can provide good coverage Many samples can provide good coverage

53 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Metropolis Algorithm Details Randomly select site to reverse spin Randomly select site to reverse spin If energy is lower, move to new state If energy is lower, move to new state Otherwise, move with probability  = e -  /kT Otherwise, move with probability  = e -  /kT Rejection causes current state to be recorded another time Rejection causes current state to be recorded another time

54 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Room Assignment Problem ABCDEF A035916 B302645 C520892 D968034 E149305 F652450 “Dislikes” matrix Pairing A-B, C-D, and E-F leads to total conflict value of 32.

55 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Physical Annealing Heat a solid until it melts Heat a solid until it melts Cool slowly to allow material to reach state of minimum energy Cool slowly to allow material to reach state of minimum energy Produces strong, defect-free crystal with regular structure Produces strong, defect-free crystal with regular structure

56 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Simulated Annealing Makes analogy between physical annealing and solving combinatorial optimization problem Makes analogy between physical annealing and solving combinatorial optimization problem Solution to problem = state of material Solution to problem = state of material Value of objective function = energy associated with state Value of objective function = energy associated with state Optimal solution = minimum energy state Optimal solution = minimum energy state

57 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. How Simulated Annealing Works Iterative algorithm, slowly lower T Iterative algorithm, slowly lower T Randomly change solution to create alternate solution Randomly change solution to create alternate solution Compute , the change in value of objective function Compute , the change in value of objective function If  < 0, then jump to alternate solution If  < 0, then jump to alternate solution Otherwise, jump to alternate solution with probability e -  /T Otherwise, jump to alternate solution with probability e -  /T

58 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Performance of Simulated Annealing Rate of convergence depends on initial value of T and temperature change function Rate of convergence depends on initial value of T and temperature change function Geometric temperature change functions typical; e.g., T i+1 = 0.999 T i Geometric temperature change functions typical; e.g., T i+1 = 0.999 T i Not guaranteed to find optimal solution Not guaranteed to find optimal solution Same algorithm using different random number streams can converge on different solutions Same algorithm using different random number streams can converge on different solutions Opportunity for parallelism Opportunity for parallelism

59 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Convergence Starting with higher initial temperature leads to more iterations before convergence

60 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Parking Garage Parking garage has S stalls Parking garage has S stalls Car arrivals fit Poisson distribution with mean A Car arrivals fit Poisson distribution with mean A Stay in garage fits a normal distribution with mean M and standard deviation M/S Stay in garage fits a normal distribution with mean M and standard deviation M/S

61 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Implementation Idea 101.2142.170.391.7223.1 64.2 Current Time Times Spaces Are Available 15 Car CountCars Rejected 2

62 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Traffic Circle

63 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Traffic Circle Probabilities F N0.33 E0.50 S0.25 W0.33DNESWN0.10.20.50.2 E0.30.10.20.4 S0.50.30.10.1 W0.20.40.30.1

64 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Traffic Circle Data Structures

65 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Summary (1/3) Applications of Monte Carlo methods Applications of Monte Carlo methods  Numerical integration  Simulation Random number generators Random number generators  Linear congruential  Lagged Fibonacci

66 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Summary (2/3) Parallel random number generators Parallel random number generators  Manager/worker  Leapfrog  Sequence splitting  Independent sequences Non-uniform distributions Non-uniform distributions  Analytical transformations  Box-Muller transformation  Rejection method

67 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Summary (3/3) Concepts revealed in case studies Concepts revealed in case studies  Monte Carlo time  Random walk  Metropolis algorithm  Simulated annealing  Modeling queues


Download ppt "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Parallel Programming in C with MPI and OpenMP Michael J. Quinn."

Similar presentations


Ads by Google