Skip to content

Commit b5b55a0

Browse files
committed
Add missing parallel=:threads implementations
These implementations are extremely basic, but they try to follow the patterns in the other parts of the Parallel module. My real motivation is to be able to move the Distributed implementations into an extension, so that Graphs.jl does not depend on Distributed.
1 parent 6130332 commit b5b55a0

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

src/Parallel/distance.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,44 @@
22

33
function eccentricity(
44
g::AbstractGraph, vs=vertices(g), distmx::AbstractMatrix{T}=weights(g)
5+
; parallel=:distributed,
6+
) where {T<:Number}
7+
return if parallel === :threads
8+
threaded_eccentricity(g, vs, distmx)
9+
elseif parallel === :distributed
10+
distr_eccentricity(g, vs, distmx)
11+
else
12+
error("""Unsupported parallel argument '$(repr(parallel))' (supported: ':threads' or ':distributed')""")
13+
end
14+
end
15+
16+
function distr_eccentricity(
17+
g::AbstractGraph, vs=vertices(g), distmx::AbstractMatrix{T}=weights(g)
518
) where {T<:Number}
619
vlen = length(vs)
720
eccs = SharedVector{T}(vlen)
821
@sync @distributed for i in 1:vlen
9-
eccs[i] = maximum(Graphs.dijkstra_shortest_paths(g, vs[i], distmx).dists)
22+
d = Graphs.dijkstra_shortest_paths(g, vs[i], distmx)
23+
eccs[i] = maximum(d.dists)
1024
end
1125
d = sdata(eccs)
1226
maximum(d) == typemax(T) && @warn("Infinite path length detected")
1327
return d
1428
end
1529

30+
function threaded_eccentricity(
31+
g::AbstractGraph, vs=vertices(g), distmx::AbstractMatrix{T}=weights(g)
32+
) where {T<:Number}
33+
vlen = length(vs)
34+
eccs = Vector{T}(vlen)
35+
Base.Threads.@threads for i in 1:vlen
36+
d = Graphs.dijkstra_shortest_paths(g, vs[i], distmx)
37+
eccs[i] = maximum(d.dists)
38+
end
39+
maximum(eccs) == typemax(T) && @warn("Infinite path length detected")
40+
return d
41+
end
42+
1643
function eccentricity(g::AbstractGraph, distmx::AbstractMatrix)
1744
return eccentricity(g, vertices(g), distmx)
1845
end

src/Parallel/shortestpaths/dijkstra.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,39 @@ traversal information.
1818
"""
1919
function dijkstra_shortest_paths(
2020
g::AbstractGraph{U}, sources=vertices(g), distmx::AbstractMatrix{T}=weights(g)
21+
; parallel=:distributed,
22+
) where {T<:Number} where {U}
23+
return if parallel === :threads
24+
threaded_dijkstra_shortest_paths(g, sources, distmx)
25+
elseif parallel === :distributed
26+
distr_dijkstra_shortest_paths(g, sources, distmx)
27+
else
28+
error("""Unsupported parallel argument '$(repr(parallel))' (supported: ':threads' or ':distributed')""")
29+
end
30+
end
31+
32+
function threaded_dijkstra_shortest_paths(
33+
g::AbstractGraph{U}, sources=vertices(g), distmx::AbstractMatrix{T}=weights(g)
34+
) where {T<:Number} where {U}
35+
n_v = nv(g)
36+
r_v = length(sources)
37+
38+
# TODO: remove `Int` once julialang/#23029 / #23032 are resolved
39+
dists = Matrix{T}(Int(r_v), Int(n_v))
40+
parents = Matrix{U}(Int(r_v), Int(n_v))
41+
42+
Base.Threads.@threads for i in 1:r_v
43+
state = Graphs.dijkstra_shortest_paths(g, sources[i], distmx)
44+
dists[i, :] = state.dists
45+
parents[i, :] = state.parents
46+
end
47+
48+
result = MultipleDijkstraState(dists, parents)
49+
return result
50+
end
51+
52+
function distr_dijkstra_shortest_paths(
53+
g::AbstractGraph{U}, sources=vertices(g), distmx::AbstractMatrix{T}=weights(g)
2154
) where {T<:Number} where {U}
2255
n_v = nv(g)
2356
r_v = length(sources)

src/Parallel/traversals/greedy_color.jl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
1-
function random_greedy_color(g::AbstractGraph{T}, reps::Integer) where {T<:Integer}
1+
function random_greedy_color(g::AbstractGraph{T}, reps::Integer;
2+
parallel=:distributed) where {T<:Integer}
3+
return if parallel === :threads
4+
threaded_greedy_color(g, reps)
5+
elseif parallel === :distributed
6+
distr_greedy_color(g, reps)
7+
else
8+
error("""Unsupported parallel argument '$(repr(parallel))' (supported: ':threads' or ':distributed')""")
9+
end
10+
end
11+
12+
function threaded_random_greedy_color(g::AbstractGraph{T}, reps::Integer) where {T<:Integer}
13+
local_best = Any[nothing for _ in 1:reps]
14+
Base.Threads.@threads for i in 1:reps
15+
seq = shuffle(vertices(g))
16+
local_best[t] = Graphs.perm_greedy_color(g, seq)
17+
end
18+
best = reduce(Graphs.best_color, local_best)
19+
20+
return convert(Graphs.Coloring{T}, best)
21+
end
22+
23+
function distr_random_greedy_color(g::AbstractGraph{T}, reps::Integer) where {T<:Integer}
224
best = @distributed (Graphs.best_color) for i in 1:reps
325
seq = shuffle(vertices(g))
426
Graphs.perm_greedy_color(g, seq)

0 commit comments

Comments
 (0)