Example 08: Parallel Monte Carlo Simulation¶
Overview¶
This example demonstrates how to accelerate Monte Carlo uncertainty propagation calculations using parallel processing. It compares the performance of sequential vs. parallelized Monte Carlo simulations for the same mass flow calculation from Example 03.
Purpose¶
The primary goal is to show how to reduce computation time for large Monte Carlo simulations by:
Utilizing multiple CPU cores through batch processing
Splitting simulations into equal-sized batches across available processors
Comparing execution times and validating that parallel results match sequential results
Description¶
The script performs uncertainty analysis for mass flow calculation using an orifice meter (based on ISO 5167 equations), but runs it as a batch calculation using Python’s multiprocessing module.
The script:
Loads input parameters with uncertainties from CSV file
Runs a baseline sequential Monte Carlo simulation (1,000,000 perturbations)
Runs a parallelized batch Monte Carlo simulation (1,000,000 perturbations split across CPU cores)
Compares execution times and speedup metrics
Validates that statistical results (mean, std dev) are equivalent between both methods
Key Features¶
Multiprocessing.Pool: Uses all available CPU cores for parallel execution
Batch Processing: Divides total simulations equally across processors
Independent random streams: Each batch gets its own seed from
numpy.random.SeedSequence(SEED).spawn(...)Performance Metrics: Calculates speedup factor and percentage time reduction
Statistical Validation: Confirms parallel results match baseline within expected Monte Carlo variability
Random Number Streams¶
uncertaintylib draws its Monte Carlo samples from numpy’s global random generator (np.random.normal / np.random.uniform). That generator is not automatically independent between worker processes:
With the
forkstart method (the default on Linux up to Python 3.13), every worker inherits a copy of the parent’s generator state. Without seeding, all batches would then contain exactly the same random samples, so the parallel result would effectively use onlyN / num_processesindependent samples.With the
spawnstart method (Windows, macOS), each worker seeds itself from the operating system. The batches are independent, but the results cannot be reproduced.
The script therefore spawns one child seed per batch from a top-level SEED with numpy.random.SeedSequence(SEED).spawn(num_processes). Each batch calls np.random.seed(...) with its own seed before sampling. This gives statistically independent batches with every start method, and the results are reproducible for a given SEED and number of processes. The sequential baseline is seeded with SEED as well.
Dependencies¶
pandas
numpy
multiprocessing (standard library)
uncertaintylib.uncertainty_functions (custom library)
Input Data¶
example_03_input.csv: CSV file containing input parameters and their uncertainties (same as Example 03)
Units in the CSV: C and epsilon [-], D and d [mm], deltaP [mbar], P [bara], m/Z [kg/mol], T [K]. The output qm is in kg/s.
m/Z = 0.021019 kg/mol is the molar mass divided by the compressibility factor (M = 18.125 g/mol, Z = 0.86232), calculated with pvtlib 1.16.1, AGA8 GERG-2008, at P = 100 bara and T = 323.15 K for the dry natural gas composition in pvtlib example “01 Gas Properties from AGA8” (mol%: N2 1.0, CO2 1.0, C1 90.0, C2 5.0, C3 2.0, iC4 0.5, nC4 0.5).
C = 0.60282 and epsilon = 0.99935 are calculated with pvtlib 1.16.1 according to ISO 5167-2:2022 for an orifice plate with corner tappings (pvtlib default), D = 300 mm, d = 150 mm (beta = 0.5), deltaP = 250.2 mbar and the same gas and state as above. epsilon uses calculate_expansibility_orifice with isentropic exponent kappa = 1.4236 from AGA8 GERG-2008. C uses the Reader-Harris/Gallagher equation, solved iteratively with the Reynolds number in calculate_flow_orifice (Re_D = 6.37e6). The density is 78.230 kg/m3 from GERG-2008, and the viscosity is 1.450e-5 Pa s from pvtlib natural_gas_viscosity_Lee_et_al (Lee, Gonzalez and Eakin, 1966).
Expected Results¶
The script outputs:
Baseline Statistics: Mean and standard deviation from sequential Monte Carlo
Baseline Execution Time: Time taken for sequential simulation
Parallel Statistics: Mean and standard deviation from parallel Monte Carlo
Parallel Execution Time: Time taken for parallel simulation
Comparison Metrics:
Speedup factor (varies significantly by CPU - see Performance Considerations below)
Percentage reduction in computation time
Statistical differences (should be minimal, within Monte Carlo noise)
Note: The speedup factor is highly CPU-dependent. Results may range from 0.8x (slower) to 4x (faster) depending on your hardware.
Performance Considerations¶
Important: Performance improvements from parallel processing are highly dependent on CPU architecture and system configuration.
CPU Architecture Matters¶
Different CPUs show vastly different parallelization benefits:
Older/Desktop CPUs (e.g., Intel i5-4670K, i7-4790K)¶
Architecture: Homogeneous cores (all cores have equal performance)
TDP: Higher power budget (65-95W), no aggressive throttling
Expected speedup: 30-50% improvement with 4 processes
Why it works well: All cores run at similar frequencies, minimal overhead
Modern Hybrid CPUs (e.g., Intel 12th+ gen, Core Ultra series)¶
Architecture: P-cores (Performance) + E-cores (Efficiency)
Performance variation: P-cores are 2-3x faster than E-cores
Expected speedup: 0-30% improvement, sometimes slower than sequential
Challenges:
Windows may schedule processes to slow E-cores
Process creation overhead can exceed parallelization benefit
Single P-core turbo boost (up to 5+ GHz) may outperform multiple cores at lower sustained frequencies
Mobile/Laptop CPUs (especially Ultra-Low Power)¶
TDP: Low power budget (15-28W), aggressive thermal throttling
Single-core boost: Very high single-core turbo, lower multi-core sustained frequency
Expected speedup: May see no improvement or slower performance
Why:
1 core at 5 GHz often beats 4 cores at 3 GHz for moderate workloads
Thermal limits kick in faster with multiple cores active
Windows multiprocessing overhead is significant
General Guidelines¶
Overhead: Multiprocessing has significant overhead on Windows; most beneficial for large N (>100,000 simulations)
Memory: Each process needs memory; very large N may require consideration of available RAM
Process count: More processes ≠ better performance. For N=1,000,000, limiting to 4 processes often yields best results
Platform: Linux/Unix systems typically show better parallelization due to more efficient
fork()vs Windowsspawn()
Recommendation¶
Always benchmark on your specific hardware! What works well on one CPU may not work on another. The example is designed to demonstrate the technique, but actual performance gains will vary based on:
Your CPU architecture (homogeneous vs hybrid)
Power settings (high performance vs balanced)
Thermal conditions (desktop vs laptop cooling)
Operating system (Windows vs Linux)
Workload size (N value)
When to Use Parallel Processing¶
Parallel processing is most beneficial when:
Running large numbers of Monte Carlo simulations (N > 100,000)
The calculation function is computationally intensive
Multiple CPU cores are available
Execution time of sequential approach is prohibitively long
For smaller simulations (N < 10,000), the overhead of multiprocessing may exceed the benefits.
Comparison with Example 03¶
Example 03: Sequential Monte Carlo with 10,000 perturbations + visualization
Example 08: Parallel Monte Carlo with 1,000,000 perturbations + performance comparison
Both use the same orifice meter mass flow calculation and input data, but Example 08 focuses on computational efficiency for large-scale simulations.
Source code¶
"""MIT License
Copyright (c) 2025 Equinor
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Monte Carlo Batch Processing Comparison Script
This script compares the performance of sequential vs. parallelized Monte Carlo simulations
for uncertainty propagation in mass flow calculations.
Comparison:
-----------
- Sequential: Single-process Monte Carlo simulation
- Parallel: Multi-process batch Monte Carlo simulation using {num_processes} CPU cores
- Number of perturbations: {N} simulations per method
The script performs the following:
1. Loads input parameters with uncertainties from a CSV file
2. Runs a baseline sequential Monte Carlo simulation
3. Runs a parallelized batch Monte Carlo simulation
4. Compares execution times and statistical results (mean, std dev)
The mass flow calculation is based on ISO 5167 orifice plate equations,
propagating uncertainties through the calculation using Monte Carlo methods.
Key Features:
-------------
- Utilizes multiprocessing.Pool for parallel execution
- Splits simulations into equal-sized batches across available CPU cores
- Provides detailed timing and statistical comparison metrics
- Validates parallel results against baseline sequential results
Dependencies:
-------------
- pandas, numpy, multiprocessing
- uncertaintylib.uncertainty_functions (custom library)
Input:
------
- example_03_input.csv: CSV file containing input parameters and their uncertainties
Output:
-------
- Baseline and parallel Monte Carlo statistics (mean, std dev, etc.)
- Execution time comparison and speedup metrics
- Statistical validation comparing both methods
"""
import os
import pandas as pd
import numpy as np
import math
import time
from multiprocessing import Pool, cpu_count
from uncertaintylib import uncertainty_functions
def calculate_massflow(input_dict):
R = 8.314 #J/molK
C = input_dict['C']
epsilon = input_dict['epsilon']
D = input_dict['D']/1000 #m
d = input_dict['d']/1000 #m
deltaP = input_dict['deltaP']*100 #Pa
P1 = input_dict['P'] * 10**5 #Pa
T = input_dict['T']
m_div_Z = input_dict['m/Z'] #kg/mol
qm = (C/np.sqrt(1-((d/D)**4)))*(epsilon*math.pi*(d**2)/4)*np.sqrt((2*P1*deltaP*m_div_Z)/(R*T))
output_dict = {'qm' : qm}
return output_dict
# Function to run a single batch
def run_batch(batch_size, seed_sequence):
"""Run Monte Carlo simulation for a single batch with its own random stream"""
# uncertaintylib draws from numpy's global random generator (np.random.normal/uniform).
# Seed it per batch, so batches never share a random stream (with 'fork' every worker
# would otherwise inherit the same generator state) and results are reproducible.
np.random.seed(seed_sequence.generate_state(4))
# Load input parameters inside the function to avoid pickling issues
csv_path = os.path.join(os.path.dirname(__file__), 'example_03_input.csv')
mc_input = pd.read_csv(csv_path).set_index('input_name').to_dict()
return uncertainty_functions.monte_carlo_simulation(mc_input, calculate_massflow, batch_size)
if __name__ == '__main__':
# Load input parameters from CSV file in the same folder as the script
csv_path = os.path.join(os.path.dirname(__file__), 'example_03_input.csv')
mc_input = pd.read_csv(csv_path).set_index('input_name').to_dict()
N = 1000000
SEED = 12345 # Top-level seed: makes both the baseline and the parallel results reproducible
SEP = "="*80 # Separator line for output formatting
print(SEP)
print("BASELINE: Sequential Monte Carlo Simulation")
print(SEP)
# Step 1: Run Monte Carlo simulation to propagate input uncertainties
np.random.seed(SEED)
start_time = time.time()
mc_res = uncertainty_functions.monte_carlo_simulation(mc_input, calculate_massflow, N)
mc_stats = uncertainty_functions.calculate_monte_carlo_statistics(mc_res)
end_time = time.time()
baseline_time = end_time - start_time
# Step 2: Print Monte Carlo statistics
print("\nBaseline Statistics:")
print(mc_stats)
print(f"\nBaseline Execution time: {baseline_time:.2f} seconds")
print("\n" + SEP)
print("PARALLEL: Batch Monte Carlo Simulation")
print(SEP)
# Parallel processing
num_processes = cpu_count() # Use all available CPU cores
if num_processes>4:
num_processes=4
batch_size = N // num_processes
print(f"\nNumber of processes: {num_processes}")
print(f"Batch size per process: {batch_size}")
print(f"Total simulations: {num_processes * batch_size}")
# Prepare arguments for parallel execution: batch size and an independent seed per batch
batch_seeds = np.random.SeedSequence(SEED).spawn(num_processes)
batch_args = [(batch_size, batch_seeds[i]) for i in range(num_processes)]
# Run parallel batches
start_time_parallel = time.time()
with Pool(processes=num_processes) as pool:
batch_results = pool.starmap(run_batch, batch_args)
end_time_parallel = time.time()
# Concatenate results
mc_res_parallel = pd.concat(batch_results, ignore_index=True)
mc_stats_parallel = uncertainty_functions.calculate_monte_carlo_statistics(mc_res_parallel)
parallel_time = end_time_parallel - start_time_parallel
print("\nParallel Statistics:")
print(mc_stats_parallel)
print(f"\nParallel Execution time: {parallel_time:.2f} seconds")
print("\n" + SEP)
print("COMPARISON")
print(SEP)
print(f"\nComparing sequential vs parallel Monte Carlo simulation:")
print(f" - Total perturbations (N): {N}")
print(f" - Number of parallel processes: {num_processes}")
print(f" - Perturbations per process: {batch_size}")
print("\nPerformance Metrics:")
print(SEP)
print(f"Baseline time: {baseline_time:.2f} seconds")
print(f"Parallel time: {parallel_time:.2f} seconds")
print(f"Difference: {baseline_time - parallel_time:.2f} seconds")
print(f"Speedup: {baseline_time / parallel_time:.2f}x")
print(f"Percentage reduction in time: {(1 - parallel_time/baseline_time)*100:.1f}%")
print("\nStatistics Comparison:")
print(f"Baseline mean: {mc_stats.loc['qm', 'mean']:.6f}")
print(f"Parallel mean: {mc_stats_parallel.loc['qm', 'mean']:.6f}")
print(f"Difference: {abs(mc_stats.loc['qm', 'mean'] - mc_stats_parallel.loc['qm', 'mean']):.6e}")
print(f"\nBaseline std: {mc_stats.loc['qm', 'std_dev']:.6f}")
print(f"Parallel std: {mc_stats_parallel.loc['qm', 'std_dev']:.6f}")
print(f"Difference: {abs(mc_stats.loc['qm', 'std_dev'] - mc_stats_parallel.loc['qm', 'std_dev']):.6e}")