# examples/04-sendrecv.jl
using MPI
MPI.Init()
comm = MPI.COMM_WORLD
rank = MPI.Comm_rank(comm)
size = MPI.Comm_size(comm)
dst = mod(rank+1, size)
src = mod(rank-1, size)
N = 4
send_mesg = Array{Float64}(undef, N)
recv_mesg = Array{Float64}(undef, N)
fill!(send_mesg, Float64(rank))
rreq = MPI.Irecv!(recv_mesg, comm; source=src, tag=src+32)
print("$rank: Sending $rank -> $dst = $send_mesg\n")
sreq = MPI.Isend(send_mesg, comm; dest=dst, tag=rank+32)
stats = MPI.Waitall([rreq, sreq])
print("$rank: Received $src -> $rank = $recv_mesg\n")
MPI.Barrier(comm)
> mpiexecjl -n 4 julia examples/04-sendrecv.jl
0: Sending 0 -> 1 = [0.0, 0.0, 0.0, 0.0]
3: Sending 3 -> 0 = [3.0, 3.0, 3.0, 3.0]
2: Sending 2 -> 3 = [2.0, 2.0, 2.0, 2.0]
1: Sending 1 -> 2 = [1.0, 1.0, 1.0, 1.0]
0: Received 3 -> 0 = [3.0, 3.0, 3.0, 3.0]
3: Received 2 -> 3 = [2.0, 2.0, 2.0, 2.0]
2: Received 1 -> 2 = [1.0, 1.0, 1.0, 1.0]
1: Received 0 -> 1 = [0.0, 0.0, 0.0, 0.0]