Created
November 17, 2009 02:10
-
-
Save jsnyder/236560 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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