def kirchhoff_integral(front, back): rank = multi_process.Get_rank() source_count = front.source.source_count num = process_number p0 = source_count % (num - 1) p1 = source_count // (num - 1) parameters = list() if p0 == 0: for i in range(num - 1): parameters.append(range(p1 * i, p1 * (i + 1))) else: for i in range(num - 1): if i + 1 != num - 1: parameters.append(range(p1 * i, p1 * (i + 1))) else: parameters.append(range(p1 * (num - 2), p1 * (num - 1) + p0)) if rank == 0: for i in range(num - 1): multi_process.Send([np.array(i), mpi.INT], dest=i + 1, tag=i + 1) for i in range(num - 1): isize = len(parameters[i]) wavefronts = np.zeros( (isize, int(2 * back.size[1] + 1), int(2 * back.size[0] + 1)), dtype=np.complex128) multi_process.Recv([wavefronts, mpi.COMPLEX], source=i + 1, tag=100 * i + 1) for k in range(isize): back.wavefront.append(wavefronts[k, :, :]) back.intensity = np.sum(np.abs(np.array(back.wavefront))**2, 0) else: index = np.array(int()) multi_process.Recv([index, mpi.INT], source=0, tag=rank) results = [ _propagate._point(i, front, back) for i in parameters[index] ] multi_process.Send([np.array(results), mpi.COMPLEX], dest=0, tag=100 * index + 1)
def test_send_recv(snd, rcv, data_type): src = np.array([1, 2, 3, 4, 5], dtype=data_type) dst_tst = np.empty(5, dtype=data_type) dst_exp = np.empty(5, dtype=data_type) if mpi.rank() == 0: snd(src, dest=1, tag=11) COMM_WORLD.Send(src, dest=1, tag=22) elif mpi.rank() == 1: rcv(dst_tst, source=0, tag=11) COMM_WORLD.Recv(dst_exp, source=0, tag=22) assert np.all(dst_tst == src) assert np.all(dst_tst == dst_exp)
if rank == 0: data = {'a': 7, 'b': 3.14} CW.send(data, dest=1, tag=11) elif rank == 1: data = CW.recv(source=0, tag=11) print("On rank", rank, "data =", data) CW.Barrier() #But if your sending Numpy arrays, this method is much faster! data = np.array([]) if rank == 0: data = np.arange(100, dtype=np.float64) CW.Send(data, dest=1, tag=13) elif rank == 1: data = np.empty(100, dtype=np.float64) CW.Recv(data, source=0, tag=13) print("On rank", rank, "data =", data) CW.Barrier() #But what if you want to send someone from one rank to everyone else? Well you can braodcast: if rank == 0: data = {'key1': [7, 2.72, 2 + 3j], 'key2': ('abc', 'xyz')} else: data = None data = CW.bcast(data, root=0)