from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() size = comm.Get_size() send_data = f"Hello, from process {rank}!" print(f"I am rank {rank}. I send to process {(rank+1)}") print(f"I am rank {rank}. I receive from process {(rank-1)}") if rank == size-1: comm.send(send_data, dest=(0)) else: comm.send(send_data, dest=(rank + 1)) recv_data = "" if rank == 0: recv_data= comm.recv(source=(size - 1)) else: recv_data= comm.recv(source=(rank - 1)) print(f"I am rank {rank}. I have received: {recv_data}")