-
Notifications
You must be signed in to change notification settings - Fork 5
/
ring.jl
39 lines (27 loc) · 950 Bytes
/
ring.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using MPI
function main()
# Initialize the MPI environment
MPI.Init()
tag = 1111
comm = MPI.COMM_WORLD
# Get the number of processes
world_size = MPI.Comm_size(comm)
# Get the rank of the process
world_rank = MPI.Comm_rank(comm)
# Print off a hello world message
println("Processor $world_rank out of $world_size processors")
next_proc = mod(world_rank + 1, world_size)
last_proc = mod(world_size + world_rank - 1, world_size)
if world_rank == 0
token = 10 # Set the token's value if you are process 0
MPI.Send(token, next_proc, tag, comm)
(token, _) = MPI.Recv(Int, last_proc, tag, comm)
else
(token, _) = MPI.Recv(Int, last_proc, tag, comm)
MPI.Send(token + 1, next_proc, tag, comm)
end
println("Process $world_rank received token $token from process $(last_proc)")
# Finalize the MPI environment.
MPI.Finalize()
end
main()