%autosave 60
import numpy as np
from threading import Thread
from functools import partial
import multiprocessing
import multiprocessing.pool
N_CPU = multiprocessing.cpu_count()
REPEAT = 16 * N_CPU
from time import sleep
def f():
sleep(3)
print('hello')
t = Thread(target=f)
t.start()
print('world')
t.join()
print('!')
A = np.random.normal(0, 1, (64, 64, 5, 5))
%timeit np.linalg.eigvals(A)
%%timeit A = np.random.normal(0, 1, (64, 64, 5, 5))
with multiprocessing.pool.ThreadPool() as pool:
pool.map(lambda _: np.linalg.eigvals(A), range(REPEAT))
class Eigen:
def __init__(self):
self.A = np.random.normal(0, 1, (64, 64, 5, 5))
def __call__(self, x):
A = self.A.copy()
A[0, 0] += x
vals = np.linalg.eigvals(A)
return np.linalg.norm(vals)
f = Eigen()
%%timeit
with multiprocessing.pool.ThreadPool() as pool:
pool.map(f, range(REPEAT))
%timeit [f(x) for x in range(REPEAT)]
def golden_ratio(n):
s = 1.0
for _ in range(n):
s = 1.0 + 1.0 / s
return s
%timeit [golden_ratio(100_000) for _ in range(REPEAT)]
%%timeit
with multiprocessing.pool.ThreadPool() as pool:
pool.map(lambda _: golden_ratio(100_000), range(REPEAT))
# GIL