import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.plot( np.linspace(0,1))
np.sqrt(2.2)
np.sqrt(22.222222222222222222222)
import ray, time
# A regular Python function.
def regular_function():
return 1
# A Ray remote function.
@ray.remote
def remote_function():
return 1
ray.init(num_cpus=4, ignore_reinit_error=True, include_webui=False)
This is how I run the function and get the results. For more complex functions it will be the same, just the function code will be different.
ray.get(remote_function.remote())
from numba import jit
import numba
import random
print(numba.__version__)
def monte_carlo_pi_ton(nsamples):
'''
from https://numba.pydata.org/
'''
acc = 0
for i in range(nsamples):
x = random.random()
y = random.random()
if (x ** 2 + y ** 2) < 1.0:
acc += 1
return 4.0 * acc / nsamples
The nopython=True
option requires that the function be fully compiled (so that the Python interpreter calls are completely removed), otherwise an exception is raised. These exceptions usually indicate places in the function that need to be modified in order to achieve better-than-Python performance. We strongly recommend always using nopython=True
.
@jit(nopython=True)
def monte_carlo_pi(nsamples):
acc = 0
for i in range(nsamples):
x = random.random()
y = random.random()
if (x ** 2 + y ** 2) < 1.0:
acc += 1
return 4.0 * acc / nsamples
This first time the function was called, a new version of the function was compiled and executed. If we call it again, the previously generated function executions without another compilation step.
%timeit monte_carlo_pi(10000)
%timeit monte_carlo_pi(10000)
%timeit monte_carlo_pi(10000)
%timeit monte_carlo_pi_ton(10000)
%timeit monte_carlo_pi(1000000)
%timeit monte_carlo_pi_ton(1000000)
%timeit monte_carlo_pi(100000000)
%timeit monte_carlo_pi_ton(100000000)
A factor 30 speed-up from compilation
# A Ray remote function.
# structure from https://github.com/numba/numba/issues/4256
@ray.remote
def monte_carlo_pi_numba_ray(N=10**6):
return monte_carlo_pi(N)
#ray.init()
%timeit ray.get([monte_carlo_pi_numba_ray.remote() for i in range(2)])
#ray.init()
%timeit ray.get([monte_carlo_pi_numba_ray.remote() for i in range(4)])
ray.shutdown()
ray.init(num_cpus=20, ignore_reinit_error=True, include_webui=False)
#ray.init()
%timeit ray.get([monte_carlo_pi_numba_ray.remote() for i in range(2)])
#ray.init()
%timeit ray.get([monte_carlo_pi_numba_ray.remote() for i in range(4)])
#ray.init()
%timeit ray.get([monte_carlo_pi_numba_ray.remote() for i in range(12)])
#ray.init()
%timeit ray.get([monte_carlo_pi_numba_ray.remote() for i in range(16)])
#ray.init()
%timeit ray.get([monte_carlo_pi_numba_ray.remote() for i in range(48)])
#ray.init()
%timeit ray.get([monte_carlo_pi_numba_ray.remote() for i in range(96)])