unite_python_mpi_4_py
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| unite_python_mpi_4_py [2026/04/10 12:55] – nshegunov | unite_python_mpi_4_py [2026/04/10 13:49] (current) – nshegunov | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | == MPI4py Ping-Pong Example for Cluster == | + | ====== MPI4py Ping-Pong Example for Cluster |
| - | MPI4py | + | This example shows a NumPy-based |
| + | |||
| + | ===== Instructions ===== | ||
| + | |||
| + | To run this example, create the Python script and Slurm batch script provided below. | ||
| + | |||
| + | The Python script uses the mpi4py library | ||
| + | |||
| + | Optional interactive testing: | ||
| + | <code bash> | ||
| + | srun --partition=short --ntasks=2 --gres=gpu:1 --time=02: | ||
| + | </ | ||
| + | |||
| + | Use this command to request an interactive session on a single node for experimentation. | ||
| === Python Script (pingpong_mpi4py.py) ==== | === Python Script (pingpong_mpi4py.py) ==== | ||
| <code python pingpong_mpi4py.py> | <code python pingpong_mpi4py.py> | ||
| + | # | ||
| from mpi4py import MPI | from mpi4py import MPI | ||
| import numpy as np | import numpy as np | ||
| - | import | + | import |
| comm = MPI.COMM_WORLD | comm = MPI.COMM_WORLD | ||
| Line 14: | Line 28: | ||
| size = comm.Get_size() | size = comm.Get_size() | ||
| - | if size < 2: | + | if size != 2: |
| if rank == 0: | if rank == 0: | ||
| - | print(" | + | print(" |
| - | exit() | + | |
| - | N = 1000 # max message size | + | partner |
| - | extent | + | |
| - | sbuf = np.zeros(1, dtype=' | + | nrounds |
| - | rbuf = np.zeros(1, dtype=' | + | msg_sizes = [1, 8, 64, 512, 1024, 4096, 16384, 65536, 262144] |
| + | |||
| + | for nbytes in msg_sizes: | ||
| + | nelems = max(1, nbytes // np.dtype(np.uint8).itemsize) | ||
| + | |||
| + | sendbuf | ||
| + | | ||
| + | |||
| + | comm.Barrier() | ||
| + | |||
| + | t0 = MPI.Wtime() | ||
| + | |||
| + | for i in range(nrounds): | ||
| + | if rank == 0: | ||
| + | comm.Send(sendbuf, | ||
| + | comm.Recv(recvbuf, | ||
| + | else: | ||
| + | comm.Recv(recvbuf, | ||
| + | comm.Send(recvbuf, | ||
| + | |||
| + | t1 = MPI.Wtime() | ||
| + | |||
| + | if rank == 0: | ||
| + | total_time = t1 - t0 | ||
| + | avg_rtt = total_time / nrounds | ||
| + | latency_us = (avg_rtt / 2.0) * 1.0e6 | ||
| + | bandwidth_mb_s = nbytes / latency_us | ||
| + | |||
| + | print( | ||
| + | f" | ||
| + | f" | ||
| + | f" | ||
| + | f" | ||
| + | | ||
| if rank == 0: | if rank == 0: | ||
| - | | + | print(" |
| - | for i in range(extent): | + | |
| - | size = (i + 1) * N | + | |
| - | sbuf = np.zeros(size, | + | |
| - | t1 = time.time() | + | |
| - | comm.send(sbuf, | + | |
| - | comm.Recv(rbuf, | + | |
| - | t2 = time.time() | + | |
| - | latency = (t2 - t1) * 1000 # ms | + | |
| - | bandwidth = (size * 8) / (t2 - t1) / 1e6 # MB/s | + | |
| - | | + | |
| - | total_time = time.time() | + | |
| - | print(f" | + | |
| - | elif rank == 1: | + | |
| - | for i in range(extent): | + | |
| - | rbuf = comm.recv(source=0, | + | |
| - | comm.send(rbuf, | + | |
| </ | </ | ||
| Line 60: | Line 91: | ||
| #Start the example | #Start the example | ||
| - | mpirun -np $SLURM_NTASKS | + | mpirun -np $SLURM_NTASKS |
| </ | </ | ||
| + | |||
| + | ===== Run ===== | ||
| + | |||
| + | <code bash> | ||
| + | sbatch slurm_pingpong.job | ||
| + | </ | ||
| + | |||
| + | ===== Example output ===== | ||
| + | |||
| + | <code bash> | ||
| + | Loading unite/ | ||
| + | Loading requirement: | ||
| + | |||
| + | size= 1 bytes | RTT= 10.08 us | latency= | ||
| + | size= 8 bytes | RTT= 3.80 us | latency= | ||
| + | size= 64 bytes | RTT= 4.49 us | latency= | ||
| + | size= 512 bytes | RTT= 27.33 us | latency= | ||
| + | size= 1024 bytes | RTT= 6.46 us | latency= | ||
| + | size= 4096 bytes | RTT= 8.90 us | latency= | ||
| + | size= 16384 bytes | RTT= 15.53 us | latency= | ||
| + | size= 65536 bytes | RTT= 29.86 us | latency= | ||
| + | size= 262144 bytes | RTT= 72.79 us | latency= | ||
| + | Ping-pong benchmark completed. | ||
| + | |||
| + | </ | ||
| + | |||
unite_python_mpi_4_py.1775814911.txt.gz · Last modified: 2026/04/10 12:55 by nshegunov
