Ping Pong
Write a program in which two processes (rank 0 and rank 1) repeatedly exchange messages in the form of an integer array back and forth. Use arrays from the NumPy library. The program works like tennis
rank 0 sends a message to rank 1
rank 1 receives this message and sends the received message back to rank 0
rank 0 receives the message from rank 1 and returns it
# -*- coding: utf-8 -*- from mpi4py import MPI import numpy comm = MPI.COMM_WORLD rank = MPI.COMM_WORLD.Get_rank() size = MPI.COMM_WORLD.Get_size() name = MPI.Get_processor_name() if size != 2: print("Uporabi mpirun -np 2 python pingpong.py") exit(1) # predalociramo polje bufsize = 1*1024*1024 if rank == 0: data = numpy.arange(bufsize, dtype='i') elif rank == 1: data = numpy.empty(bufsize, dtype='i') loops = 1000 for x in range(loops): if rank == 0: comm.Send([data, MPI.INT], dest = 1, tag=0xdead) comm.Recv([data, MPI.INT], source=1, tag=0xbeef) else: comm.Recv([data, MPI.INT], source=0, tag=0xdead) comm.Send([data, MPI.INT], dest = 0, tag=0xbeef) if rank == 0: print "Pretočeno %d MB podatkov" % (2*bufsize*loops/1024/1024)
Modify the program so that it also works if there are more processors than needed. In that case the other processors should do nothing.
Insert MPI.Wtime() calls to measure time. Several repetitions will be needed, especially for smaller messages, to get relevant results. Produce a table in the following form:
size (bytes)
# of iterations
Total time (s)
Time per message
Transfer rate (MB/s)
Ping Ping results
Observe how the time changes with message size. What is the asymptotic transfer rate?
Plot a graph against message size to determine the latency (the time needed for a zero-size message).
Write a program in which the master process sends a message to all other processes in MPI.COMM_WORLD and then receives the messages back. How does the required message time change with the number of processes?
Check the communication speed between two nodes over Infiniband (OPENIB) and Ethernet (TCP) with the commands
sbatch --partition=rome --ntasks=2 --ntasks-per-node=1 --output=pp-oib.txt --wrap="mpirun --mca btl ^tcp python pingpong.py" sbatch --partition=rome --ntasks=2 --ntasks-per-node=1 --output=pp-tcp.txt --wrap="mpirun --mca btl ^openib python pingpong.py"
Let’s write a program that computes a global sum by passing random numbers around a ring. Each process must know the numbers of its neighbours in the ring.
from mpi4py import MPI from random import randint comm = MPI.COMM_WORLD rank = MPI.COMM_WORLD.Get_rank() size = MPI.COMM_WORLD.Get_size() name = MPI.Get_processor_name() counter = 0 data = randint(0,100) for x in xrange(size): comm.send(data, dest=(rank+1)%size, tag=7) data = comm.recv(source=(rank+size-1)%size, tag=7) counter += data print("[%d] Skupna vsota: %d" % (rank,counter))
If instead of random numbers we initialise the local values of all P processors to (rank+1)**2, do you get the correct result P(P+1)(2P+1)/6?
Measure the time needed for the global sum. How much is needed to process an individual message?
Use the sendrecv() command instead of the send/recv pair.
Ping Pong
Napiši program v katerem si dva prrocesa (rank 0 in rank 1) ponavljajoče izmenjujeta sporočila v obliki celoštevičnega polja sem in tja. Uporabite polja iz knjižnice NumPy. Program deluje tako pri tenisu
rank 0 pošlje sporočilo na rank 1
rank 1 sprejme to sporočilo in pošlje sprejeto sporočilo nazaj na rank 0
rank 0 sprejme sporočilo od rank 1 in ga vrne
# -*- coding: utf-8 -*- from mpi4py import MPI import numpy comm = MPI.COMM_WORLD rank = MPI.COMM_WORLD.Get_rank() size = MPI.COMM_WORLD.Get_size() name = MPI.Get_processor_name() if size != 2: print("Uporabi mpirun -np 2 python pingpong.py") exit(1) # predalociramo polje bufsize = 1*1024*1024 if rank == 0: data = numpy.arange(bufsize, dtype='i') elif rank == 1: data = numpy.empty(bufsize, dtype='i') loops = 1000 for x in range(loops): if rank == 0: comm.Send([data, MPI.INT], dest = 1, tag=0xdead) comm.Recv([data, MPI.INT], source=1, tag=0xbeef) else: comm.Recv([data, MPI.INT], source=0, tag=0xdead) comm.Send([data, MPI.INT], dest = 0, tag=0xbeef) if rank == 0: print "Pretočeno %d MB podatkov" % (2*bufsize*loops/1024/1024)
Program popravite tako, da bo delaloval tudi če bo več procesorjev, kot je potrebno. V tem primeru naj ostali procesorji ne delajo nič.
Vstavite ukaze MPI.Wtime() za merjenje časa. Potrebno bo več ponovitev, še posebej pri manjših sporočilih, da boste dobili relevantne rezultate. Izdelajte tabelo v naslednji obliki:
velikost (byte)
# iteracij
Skupen čas (s)
Čas na sporočilo
Hitrost prenosa (MB/s)
Rezultati Ping Pinga
Opazujte kako se čas preminja z velikostjo sporočila. Kaj je asimptotična hitrost prenosa?
Izriši graf glede na velikost sporočila, da določite latenco (čas potreben za sporočilo nične velikosti).
Napišite program v katerem glavni proces pošilja sporočilo vsem ostalim procesom v MPI.COMM_WORLD in nato sprejme sporočila nazaj. Kako se spreminja potreben čas sporočila glede na število procesov?
Preveri hitrost komunikacije na dveh vozliščih preko infinibanda (OPENIB) in etherneta (TCP) z ukazoma
sbatch --partition=rome --ntasks=2 --ntasks-per-node=1 --output=pp-oib.txt --wrap="mpirun --mca btl ^tcp python pingpong.py" sbatch --partition=rome --ntasks=2 --ntasks-per-node=1 --output=pp-tcp.txt --wrap="mpirun --mca btl ^openib python pingpong.py"
Napišimo program, ki bo seštel globalno vsoto z uporabo posredovanja naključnih števil okoli obroča. Vsak proces mora poznati številko svojih sosedov v obroču.
from mpi4py import MPI from random import randint comm = MPI.COMM_WORLD rank = MPI.COMM_WORLD.Get_rank() size = MPI.COMM_WORLD.Get_size() name = MPI.Get_processor_name() counter = 0 data = randint(0,100) for x in xrange(size): comm.send(data, dest=(rank+1)%size, tag=7) data = comm.recv(source=(rank+size-1)%size, tag=7) counter += data print("[%d] Skupna vsota: %d" % (rank,counter))
Če namesto naključnih števil inicializiramo lokalne vrednosti vseh P procesorjev na (rank+1)**2, ali dobite pravilen rezultat P(P+1)(2P+1)/6?
Izmerite čas potreben ya globalno vsoto in koliko je potrebno za obdelavo posameznega sporočila?
Uporabite sendrecv() ukaz namesto para send/recv.