Skip to content

Commit 7521b2f

Browse files
authored
Update docs/examples/09-graph_communication.jl (#703)
This patch updates the graph communication example. In particular, it shows how to use `MPI.Dist_graph_neighbors_count` and `MPI.Dist_graph_neighbors!`.
1 parent cd6a6b4 commit 7521b2f

File tree

1 file changed

+21
-57
lines changed

1 file changed

+21
-57
lines changed

docs/examples/09-graph_communication.jl

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -41,85 +41,49 @@ end
4141
source = Cint[rank]
4242
graph_comm = MPI.Dist_graph_create(comm, source, degree, dest)
4343

44+
# Query number of ranks that point to this rank, and number of ranks this rank point to
45+
indegree, outdegree, _ = MPI.Dist_graph_neighbors_count(graph_comm)
46+
47+
# Query which ranks that point to this rank, and which ranks this rank point to
48+
inranks = Vector{Cint}(undef, indegree)
49+
outranks = Vector{Cint}(undef, outdegree)
50+
MPI.Dist_graph_neighbors!(graph_comm, inranks, outranks)
51+
4452
#
4553
# Now send the rank across the edges.
4654
#
4755
# Version 1: use allgather primitive
4856
#
4957

50-
send = [rank]
51-
if rank == 0
52-
recv = [-1, -1, -1]
53-
elseif rank == 1
54-
recv = [-1, -1, -1]
55-
elseif rank == 2
56-
recv = [-1]
57-
elseif rank == 3
58-
recv = [-1, -1]
59-
end
58+
send = Cint[rank]
59+
recv = Vector{Cint}(undef, indegree)
6060

6161
MPI.Neighbor_allgather!(send, recv, graph_comm);
6262

63-
println("rank = $(rank): $(recv)")
63+
print("rank = $(rank): $(recv)\n")
6464

6565
#
6666
# Version 2: use alltoall primitive
6767
#
6868

69-
if rank == 0
70-
send = [rank, rank]
71-
recv = [-1, -1, -1]
72-
elseif rank == 1
73-
send = [rank]
74-
recv = [-1, -1, -1]
75-
elseif rank == 2
76-
send = [rank, rank, rank]
77-
recv = [-1]
78-
elseif rank == 3
79-
send = [rank, rank, rank]
80-
recv = [-1, -1]
81-
end
69+
send = fill(Cint(rank), outdegree)
70+
recv = Vector{Cint}(undef, indegree)
8271

8372
MPI.Neighbor_alltoall!(UBuffer(send,1), UBuffer(recv,1), graph_comm);
8473

85-
println("rank = $(rank): $(recv)")
74+
print("rank = $(rank): $(recv)\n")
8675

8776
#
88-
# Now send the rank exactly rank times across the edges.
89-
#
77+
# Now send the this rank "destination rank"+1 times across the edges.
9078
# Rank i receives i+1 values from each adjacent process
91-
if rank == 0
92-
send = [rank, rank,
93-
rank, rank, rank, rank]
94-
send_count = [2, 4]
95-
96-
recv = [-1, -1, -1]
97-
recv_count = [1, 1, 1]
98-
elseif rank == 1
99-
send = [rank]
100-
send_count = [1]
101-
102-
recv = [-1, -1, -1, -1, -1, -1]
103-
recv_count = [2, 2, 2]
104-
elseif rank == 2
105-
send = [rank, rank, rank, rank,
106-
rank,
107-
rank,rank]
108-
send_count = [4, 1, 2]
109-
110-
recv = [-1, -1, -1]
111-
recv_count = [3]
112-
elseif rank == 3
113-
send = [rank,
114-
rank, rank,rank,
115-
rank, rank]
116-
send_count = [1, 3, 2]
79+
#
11780

118-
recv = [-1, -1, -1, -1, -1, -1, -1, -1]
119-
recv_count = [4, 4]
120-
end
81+
send_count = outranks .+ Cint(1)
82+
send = fill(Cint(rank), sum(send_count))
83+
recv_count = fill(Cint(rank + 1), length(inranks))
84+
recv = Vector{Cint}(undef, sum(recv_count))
12185

12286
MPI.Neighbor_alltoallv!(VBuffer(send,send_count), VBuffer(recv,recv_count), graph_comm);
123-
println("rank = $(rank): $(recv)")
87+
print("rank = $(rank): $(recv)\n")
12488

12589
MPI.Finalize()

0 commit comments

Comments
 (0)