Created
January 16, 2020 11:54
-
-
Save jhidding/e08c3096b5c54bf2138ca248625de029 to your computer and use it in GitHub Desktop.
Compute Pi using Numba and Dask
This file contains hidden or 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
# requirements: python3, numba, dask | |
import random | |
import numba | |
import dask | |
@dask.delayed | |
@numba.jit(nopython=True, nogil=True) | |
def calc_pi(N): | |
M = 0 | |
for i in range(N): | |
# Simulate impact coordinates | |
x = random.uniform(-1, 1) | |
y = random.uniform(-1, 1) | |
# True if impact happens inside the circle | |
if x**2 + y**2 < 1.0: | |
M += 1 | |
return 4 * M / N | |
@dask.delayed | |
def mean(*args): | |
return sum(args) / len(args) | |
if __name__ == "__main__": | |
import argparse | |
parser = argparse.ArgumentParser(description="Compute pi.") | |
parser.add_argument('-N', default="10**7", help="number of samples per thread") | |
parser.add_argument('-M', type=int, default=10, help="number of threads") | |
args = parser.parse_args() | |
N = eval(args.N) | |
M = args.M | |
pi_d = mean(*[calc_pi(N) for i in range(M)]) | |
print(pi_d.compute()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment