Skip to content

Instantly share code, notes, and snippets.

@jsnyder
Created November 17, 2009 02:10
Show Gist options
  • Save jsnyder/236560 to your computer and use it in GitHub Desktop.
Save jsnyder/236560 to your computer and use it in GitHub Desktop.
import multiprocessing as mp
import numpy as np
processes = mp.cpu_count()
nsamples = int(12e6/processes)
def calcInsideNumPy(rank):
np.random.seed(rank)
totalsum = 0
# "vectorized" sample gen, col 0 = x, col 1 = y
for i in range(1000):
xy = np.random.random((nsamples/1000,2))
totalsum += 4.0*np.sum(np.sum(xy**2,1)<1)/nsamples
return totalsum
if __name__ == '__main__':
pool = mp.Pool(processes)
result = pool.map(calcInsideNumPy, range(processes))
print np.mean(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment