Skip to content

Commit 1e6f3e5

Browse files
authored
simplify topology code, use MPI.UNWEIGHTED instead of nothing (#709)
* simplify topology code, use MPI.UNWEIGHTED instead of nothing * clean up docs
1 parent 2b34b68 commit 1e6f3e5

File tree

3 files changed

+80
-68
lines changed

3 files changed

+80
-68
lines changed

docs/src/reference/topology.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Topology
22

3+
## Cartesian
34
```@docs
45
MPI.Dims_create
56
MPI.Cart_create
@@ -9,6 +10,12 @@ MPI.Cart_rank
910
MPI.Cart_shift
1011
MPI.Cart_sub
1112
MPI.Cartdim_get
13+
```
14+
15+
## Graph topology
16+
17+
```@docs
18+
MPI.UNWEIGHTED
1219
MPI.Dist_graph_create
1320
MPI.Dist_graph_create_adjacent
1421
MPI.Dist_graph_neighbors_count

src/topology.jl

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ end
175175
struct Unweighted
176176
end
177177
Base.cconvert(::Type{Ptr{Cint}}, ::Unweighted) = API.MPI_UNWEIGHTED[]
178+
"""
179+
MPI.UNWEIGHTED :: MPI.Unweighted
180+
181+
This is used to indicate that a graph topology is unweighted. It can be supplied
182+
as an argument to [`Dist_graph_create_adjacent`](@ref),
183+
[`Dist_graph_create`](@ref), and [`Dist_graph_neighbors!`](@ref); or obtained as
184+
the return value from [`Dist_graph_neighbors`](@ref).
185+
"""
178186
const UNWEIGHTED = Unweighted()
179187

180188
struct WeightsEmpty
@@ -183,16 +191,19 @@ Base.cconvert(::Type{Ptr{Cint}}, ::WeightsEmpty) = API.MPI_WEIGHTS_EMPTY[]
183191
const WEIGHTS_EMPTY = WeightsEmpty()
184192

185193
"""
186-
graph_comm = Dist_graph_create_adjacent(comm::Comm, sources::Vector{Cint}, destinations::Vector{Cint}; source_weights::Union{Vector{Cint}, Unweighted, WeightsEmpty}=UNWEIGHTED, destination_weights::Union{Vector{Cint}, Unweighted, WeightsEmpty}=UNWEIGHTED, reorder=false, infokws...)
194+
graph_comm = Dist_graph_create_adjacent(comm::Comm,
195+
sources::Vector{Cint}, destinations::Vector{Cint};
196+
source_weights::Union{Vector{Cint}, Unweighted, WeightsEmpty}=UNWEIGHTED, destination_weights::Union{Vector{Cint}, Unweighted, WeightsEmpty}=UNWEIGHTED,
197+
reorder=false, infokws...)
187198
188199
Create a new communicator from a given directed graph topology, described by local incoming and outgoing edges on an existing communicator.
189200
190201
# Arguments
191202
- `comm::Comm`: The communicator on which the distributed graph topology should be induced.
192203
- `sources::Vector{Cint}`: The local, incoming edges on the rank of the calling process.
193204
- `destinations::Vector{Cint}`: The local, outgoing edges on the rank of the calling process.
194-
- `source_weights::Union{Vector{Cint}, Unweighted, WeightsEmpty}=MPI.UNWEIGHTED`: The edge weights of the local, incoming edges.
195-
- `destinations_weights::Union{Vector{Cint}, Unweighted, WeightsEmpty}=MPI.UNWEIGHTED`: The edge weights of the local, outgoing edges.
205+
- `source_weights::Union{Vector{Cint}, Unweighted, WeightsEmpty}`: The edge weights of the local, incoming edges. The default is [`MPI.UNWEIGHTED`](@ref).
206+
- `destinations_weights::Union{Vector{Cint}, Unweighted, WeightsEmpty}`: The edge weights of the local, outgoing edges. The default is [`MPI.UNWEIGHTED`](@ref).
196207
- `reorder::Bool=false`: If set true, then the MPI implementation can reorder the source and destination indices.
197208
198209
# Example
@@ -232,7 +243,7 @@ Create a new communicator from a given directed graph topology, described by inc
232243
- `degrees::Vector{Cint}`: An array with the number of outgoing edges for each entry in the sources array.
233244
- `destinations::Vector{Cint}`: An array containing with lenght of the sum of the entries in the degrees array
234245
describing the ranks towards the edges point.
235-
- `weights::Union{Vector{Cint}, Unweighted, WeightsEmpty}=MPI.UNWEIGHTED`: The edge weights of the specified edges.
246+
- `weights::Union{Vector{Cint}, Unweighted, WeightsEmpty}`: The edge weights of the specified edges. The default is [`MPI.UNWEIGHTED`](@ref).
236247
- `reorder::Bool=false`: If set true, then the MPI implementation can reorder the source and destination indices.
237248
238249
# Example
@@ -291,32 +302,49 @@ function Dist_graph_neighbors_count(graph_comm::Comm)
291302
end
292303

293304
"""
294-
Dist_graph_neighbors!(graph_comm::Comm, sources::Vector{Cint}, source_weights::Vector{Cint}, destinations::Vector{Cint}, destination_weights::Vector{Cint})
305+
Dist_graph_neighbors!(graph_comm::MPI.Comm,
306+
sources::Vector{Cint}, source_weights::Union{Vector{Cint}, Unweighted},
307+
destinations::Vector{Cint}, destination_weights::Union{Vector{Cint}, Unweighted},
308+
)
309+
Dist_graph_neighbors!(graph_comm::Comm, sources::Vector{Cint}, destinations::Vector{Cint})
295310
296-
Return the neighbors and edge weights of the calling process in a distributed graph topology.
311+
Query the neighbors and edge weights (optional) of the calling process in a
312+
distributed graph topology.
297313
298314
# Arguments
299315
- `graph_comm::Comm`: The communicator of the distributed graph topology.
300-
- `sources::Vector{Cint}`: A preallocated vector, which will be filled with the ranks
301-
of the processes whose edges pointing towards the calling process. The
302-
length is exactly the indegree returned by [`MPI.Dist_graph_neighbors_count`](@ref).
303-
- `source_weights::Vector{Cint}`: A preallocated vector, which will be filled with the weights
304-
associated to the edges pointing towards the calling process. The
305-
length is exactly the indegree returned by [`MPI.Dist_graph_neighbors_count`](@ref).
306-
- `destinations::Vector{Cint}`: A preallocated vector, which will be filled with the ranks
307-
of the processes towards which the edges of the calling process point. The
308-
length is exactly the outdegree returned by [`MPI.Dist_graph_neighbors_count`](@ref).
309-
- `destination_weights::Vector{Cint}`: A preallocated vector, which will be filled with the weights
310-
associated to the edges of the outgoing edges of the calling process point. The
311-
length is exactly the outdegree returned by [`MPI.Dist_graph_neighbors_count`](@ref).
316+
- `sources`: A preallocated `Vector{Cint}`, which will be filled with the ranks
317+
of the processes whose edges pointing towards the calling process.
318+
The length is exactly the indegree returned by
319+
[`MPI.Dist_graph_neighbors_count`](@ref).
320+
- `source_weights`: A preallocated `Vector{Cint}`, which will be filled with the
321+
weights associated to the edges pointing towards the calling
322+
process. The length is exactly the indegree returned by
323+
[`MPI.Dist_graph_neighbors_count`](@ref). Alternatively,
324+
[`MPI.UNWEIGHTED`](@ref) can be used if weight information is not required.
325+
- `destinations`: A preallocated `Vector{Cint}``, which will be filled with the
326+
ranks of the processes towards which the edges of the calling
327+
process point. The length is exactly the outdegree returned by
328+
[`MPI.Dist_graph_neighbors_count`](@ref).
329+
- `destination_weights`: A preallocated `Vector{Cint}`, which will be filled
330+
with the weights associated to the edges of the outgoing edges of
331+
the calling process point. The length is exactly the outdegree
332+
returned by [`MPI.Dist_graph_neighbors_count`](@ref). Alternatively,
333+
[`MPI.UNWEIGHTED`](@ref) can be used if weight information is not required.
312334
313335
# Example
314-
Let us assume the following graph `0 <-3-> 1 -4-> 2`, then the process with rank 1 will require to
315-
preallocate a sources vector of length 1 and a destination vector of length 2. The call will fill
316-
the vectors as follows:
336+
Let us assume the following graph:
337+
```
338+
rank 0 <-----> rank 1 ------> rank 2
339+
weights: 3 4
340+
```
341+
then then the process with rank 1 will need to preallocate `sources` and
342+
`source_weights` as vectors of length 1, and a `destinations` and `destination_weights` as vectors of length 2.
343+
344+
The call will fill the vectors as follows:
317345
318346
```julia-repl
319-
julia> Dist_graph_neighbors!(graph_comm, sources, source_weights, destinations, destination_weights);
347+
julia> MPI.Dist_graph_neighbors!(graph_comm, sources, source_weights, destinations, destination_weights);
320348
julia> sources
321349
[0]
322350
julia> source_weights
@@ -327,63 +355,41 @@ julia> destination_weights
327355
[3,4]
328356
```
329357
330-
Note that the edge between ranks 0 and 1 can have a different weight depending on wether it is the
331-
incoming edge "`(0,1)"` or the outgoing one "`(1,0)"`.
358+
Note that the edge between ranks 0 and 1 can have a different weight depending
359+
on whether it is the incoming edge `0 --> 1` or the outgoing one `0 <-- 1`.
360+
361+
# See also
362+
- [`Dist_graph_neighbors`](@ref)
332363
333364
# External links
334365
$(_doc_external("MPI_Dist_graph_neighbors"))
335366
"""
336-
function Dist_graph_neighbors!(graph_comm::Comm, sources::Vector{Cint}, source_weights::Vector{Cint}, destinations::Vector{Cint}, destination_weights::Vector{Cint})
367+
function Dist_graph_neighbors!(graph_comm::Comm,
368+
sources::Vector{Cint}, source_weights::Union{Vector{Cint}, Unweighted},
369+
destinations::Vector{Cint}, destination_weights::Union{Vector{Cint}, Unweighted},
370+
)
371+
@assert source_weights isa Unweighted || length(sources) == length(source_weights)
372+
@assert destination_weights isa Unweighted || length(destinations) == length(destination_weights)
373+
337374
# int MPI_Dist_graph_neighbors(MPI_Comm comm,
338375
# int maxindegree, int sources[], int sourceweights[],
339376
# int maxoutdegree, int destinations[], int destweights[])
340377
API.MPI_Dist_graph_neighbors(graph_comm,
341378
length(sources), sources, source_weights,
342-
length(destinations), destinations, destination_weights)
379+
length(destinations), destinations, destination_weights,
380+
)
381+
return sources, source_weights, destinations, destination_weights
343382
end
383+
Dist_graph_neighbors!(graph_comm::Comm, sources::Vector{Cint}, destinations::Vector{Cint}) =
384+
Dist_graph_neighbors!(graph_comm, sources::Vector{Cint}, UNWEIGHTED, destinations::Vector{Cint}, UNWEIGHTED)
344385

345-
"""
346-
Dist_graph_neighbors!(graph_comm::Comm, sources::Vector{Cint}, destinations::Vector{Cint})
347-
348-
Return the neighbors of the calling process in a distributed graph topology without edge weights.
349-
350-
# Arguments
351-
- `graph_comm::Comm`: The communicator of the distributed graph topology.
352-
- `sources::Vector{Cint}`: A preallocated vector, which will be filled with the ranks of the
353-
processes whose edges pointing towards the calling process. The
354-
length is exactly the indegree returned by [`MPI.Dist_graph_neighbors_count`](@ref).
355-
- `destinations::Vector{Cint}`: A preallocated vector, which will be filled with the ranks
356-
of the processes towards which the edges of the calling process point. The
357-
length is exactly the outdegree returned by [`MPI.Dist_graph_neighbors_count`](@ref).
358-
359-
# Example
360-
Let us assume the following graph `0 <--> 1 --> 2`, then the process with rank 1 will require to
361-
preallocate a sources vector of length 1 and a destination vector of length 2. The call will fill
362-
the vectors as follows:
363-
364-
```julia-repl
365-
julia> Dist_graph_neighbors!(graph_comm, sources, destinations);
366-
julia> sources
367-
[0]
368-
julia> destinations
369-
[0,2]
370-
```
371-
372-
# External links
373-
$(_doc_external("MPI_Dist_graph_neighbors"))
374-
"""
375-
function Dist_graph_neighbors!(graph_comm::Comm, sources::Vector{Cint}, destinations::Vector{Cint})
376-
source_weights = Array{Cint}(undef,0)
377-
destination_weights = Array{Cint}(undef,0)
378-
Dist_graph_neighbors!(graph_comm, sources::Vector{Cint}, source_weights, destinations::Vector{Cint}, destination_weights)
379-
end
380386

381387
"""
382-
Dist_graph_neighbors(graph_comm::Comm)
388+
sources, source_weights, destinations, destination_weights = Dist_graph_neighbors(graph_comm::MPI.Comm)
383389
384390
Return `(sources, source_weights, destinations, destination_weights)` of the graph
385391
communicator `graph_comm`. For unweighted graphs `source_weights` and `destination_weights`
386-
are `nothing`.
392+
are returned as [`MPI.UNWEIGHTED`](@ref).
387393
388394
This function is a wrapper around [`MPI.Dist_graph_neighbors_count`](@ref) and
389395
[`MPI.Dist_graph_neighbors!`](@ref) that automatically handles the allocation of the result
@@ -396,10 +402,9 @@ function Dist_graph_neighbors(graph_comm::Comm)
396402
if weighted
397403
source_weights = Vector{Cint}(undef, indegree)
398404
destination_weights = Vector{Cint}(undef, outdegree)
399-
Dist_graph_neighbors!(graph_comm, sources, source_weights, destinations, destination_weights)
400-
return sources, source_weights, destinations, destination_weights
401405
else
402-
Dist_graph_neighbors!(graph_comm, sources, destinations)
403-
return sources, nothing, destinations, nothing
406+
source_weights = UNWEIGHTED
407+
destination_weights = UNWEIGHTED
404408
end
409+
return Dist_graph_neighbors!(graph_comm, sources, source_weights, destinations, destination_weights)
405410
end

test/test_neighbor_comm.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let
3030
src2, srcw2, dst2, dstw2 = MPI.Dist_graph_neighbors(graph_comm)
3131
@test src == src2 == Cint[prev_rank]
3232
@test dst == dst2 == Cint[next_rank]
33-
@test srcw2 === dstw2 === nothing
33+
@test srcw2 === dstw2 === MPI.UNWEIGHTED
3434
end
3535

3636
# Weighted graph

0 commit comments

Comments
 (0)