I am following Berkeley's Python Numerical Methods book and I am trying to run some code for the multiprocessor. The setup is straightforward (showing the time difference between serial and parallel version), but I can't get the parallel version to run. I saw one answer that said that a different library might be needed to run multithreading in Jupyter Notebooks. I have found a few errors in this book so I wouldn't be surprised that this could need some updating. I tried this in an IPython environment and also in Jupyter Lab and I didn't get either to load (AttributeError: Can't get attribute 'random_square' on <module 'main' (built-in)>).
import multiprocessing as mp
print(f"Number of cpu: {mp.cpu_count()}")
# Serial version - works fine
import numpy as np
import time
def random_square(seed):
np.random.seed(seed)
random_num = np.random.randint(0, 10)
return random_num**2
t0 = time.time()
results = []
for i in range(10000000):
results.append(random_square(i))
t1 = time.time()
print(f'Execution time {t1 - t0} s')
# Parallel version - doesn't compute
import multiprocessing as mp
t0 = time.time()
n_cpu = mp.cpu_count()
pool = mp.Pool(processes=n_cpu)
results = [pool.map(random_square, range(10000000))]
t1 = time.time()
print(f'Execution time {t1 - t0} s')