Добавить lec1/mc_parallel.py

This commit is contained in:
2026-10-17 08:47:37 +02:00
parent 53b31bea26
commit 5c1a08daf6
+10
View File
@@ -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