
Chapter 27
Parallel Programming
Real-world problems often take too long to solve on a single processor because
of their size or computational demands. Parallel processing is a family of
mechanisms for coordinating multiple tasks that work in parallel to solve a
single problem. In this chapter, we explore the use of parallel programming
with the example from Chapter 10.
Listing 27.1: Monte Carlo Integration (Parallel Version)
1 # montecarlo.py
2
3 from random import uniform
4 from math import exp
5 import multiprocessing
6
7 def count_hits(f, a, b, m, n):
8 hits = 0
9 for i in range(n):
10 x = uniform(a, b)
11 y = uniform(0, m)
12 if y <= f(x):
13 hits += 1
14