Init
This commit is contained in:
commit
c394677d07
59 files changed
+5149
No files matched your search
@@ -0,0 +1,44 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Демо: ping-pong и замер стоимости обмена между рангами.
|
||||
|
||||
Запуск из корня курса:
|
||||
mpiexec -n 2 python lab08_mpi/examples/02_pingpong_mpi.py
|
||||
|
||||
Ранги перекидывают список чисел туда-обратно. Так измеряют латентность и
|
||||
пропускную способность канала между узлами.
|
||||
"""
|
||||
import time
|
||||
|
||||
from mpi4py import MPI
|
||||
|
||||
comm = MPI.COMM_WORLD
|
||||
rank = comm.Get_rank()
|
||||
size = comm.Get_size()
|
||||
|
||||
if size < 2:
|
||||
print("Нужны минимум 2 ранга: mpiexec -n 2 python lab08_mpi/examples/02_pingpong_mpi.py")
|
||||
raise SystemExit(0)
|
||||
|
||||
ROUNDS = 20
|
||||
for n_bytes in (1_000, 100_000, 1_000_000):
|
||||
data = list(range(n_bytes // 8)) # ~n_bytes байт целых чисел
|
||||
comm.barrier() # синхронный старт замера
|
||||
t0 = time.perf_counter()
|
||||
if rank == 0:
|
||||
for _ in range(ROUNDS):
|
||||
comm.send(data, dest=1, tag=0)
|
||||
comm.recv(source=1, tag=0)
|
||||
elif rank == 1:
|
||||
for _ in range(ROUNDS):
|
||||
comm.recv(source=0, tag=0)
|
||||
comm.send(data, dest=0, tag=0)
|
||||
dt = time.perf_counter() - t0
|
||||
|
||||
if rank == 0:
|
||||
per_round = dt / ROUNDS
|
||||
print(f"{n_bytes:>9,} байт: {ROUNDS} раундов за {dt:.3f} c "
|
||||
f"=> {per_round * 1000:.2f} мс/раунд (туда-обратно)".replace(",", " "))
|
||||
|
||||
if rank == 0:
|
||||
print("\nНа одной машине это IPC (быстро), между узлами — сеть.")
|
||||
print("Эта цена обмена и есть накладные расходы распределённых вычислений.")
|
||||
Reference in new issue
Block a user