Добавить lec1/mc_parallel.py
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
from concurrent.futures import ProcessPoolExecutor
|
||||
from mc import count_hits
|
||||
def estimate_pi_parallel(n_points, pool, n_workers):
|
||||
"""Делит работу на n_workers порций и считает их в пуле процессов."""
|
||||
chunk = n_points // n_workers
|
||||
sizes = [chunk] * n_workers
|
||||
sizes[-1] += n_points - chunk * n_workers # остаток — последней порции
|
||||
seeds = range(1, n_workers + 1) # разные зёрна у порций
|
||||
hits = sum(pool.map(count_hits, sizes, seeds))
|
||||
return 4 * hits / n_points
|
||||
Reference in New Issue
Block a user