-
Notifications
You must be signed in to change notification settings - Fork 100
Add missing parallel=:threads
implementations
#429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,51 @@ | ||
# used in shortest path calculations | ||
|
||
function eccentricity( | ||
g::AbstractGraph, | ||
vs=vertices(g), | ||
distmx::AbstractMatrix{T}=weights(g); | ||
parallel=:distributed, | ||
) where {T<:Number} | ||
return if parallel === :threads | ||
threaded_eccentricity(g, vs, distmx) | ||
elseif parallel === :distributed | ||
distr_eccentricity(g, vs, distmx) | ||
else | ||
error( | ||
"Unsupported parallel argument '$(repr(parallel))' (supported: ':threads' or ':distributed')", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also add a test for the case that someone uses the wrong argument. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We repeat the same error multiple times - Perhaps we can throw a custom error type instead? |
||
) | ||
end | ||
end | ||
|
||
function distr_eccentricity( | ||
g::AbstractGraph, vs=vertices(g), distmx::AbstractMatrix{T}=weights(g) | ||
) where {T<:Number} | ||
vlen = length(vs) | ||
eccs = SharedVector{T}(vlen) | ||
@sync @distributed for i in 1:vlen | ||
eccs[i] = maximum(Graphs.dijkstra_shortest_paths(g, vs[i], distmx).dists) | ||
d′ = Graphs.dijkstra_shortest_paths(g, vs[i], distmx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am a bit unsure about the usage of |
||
eccs[i] = maximum(d′.dists) | ||
end | ||
d = sdata(eccs) | ||
maximum(d) == typemax(T) && @warn("Infinite path length detected") | ||
return d | ||
end | ||
|
||
function eccentricity(g::AbstractGraph, distmx::AbstractMatrix) | ||
return eccentricity(g, vertices(g), distmx) | ||
function threaded_eccentricity( | ||
g::AbstractGraph, vs=vertices(g), distmx::AbstractMatrix{T}=weights(g) | ||
) where {T<:Number} | ||
vlen = length(vs) | ||
eccs = Vector{T}(undef, vlen) | ||
Base.Threads.@threads for i in 1:vlen | ||
d = Graphs.dijkstra_shortest_paths(g, vs[i], distmx) | ||
eccs[i] = maximum(d.dists) | ||
end | ||
maximum(eccs) == typemax(T) && @warn("Infinite path length detected") | ||
return eccs | ||
end | ||
|
||
function eccentricity(g::AbstractGraph, distmx::AbstractMatrix; parallel=:distributed) | ||
return eccentricity(g, vertices(g), distmx; parallel) | ||
end | ||
|
||
function diameter(g::AbstractGraph, distmx::AbstractMatrix=weights(g)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,29 @@ | ||
function random_greedy_color(g::AbstractGraph{T}, reps::Integer) where {T<:Integer} | ||
function random_greedy_color( | ||
g::AbstractGraph{T}, reps::Integer; parallel=:distributed | ||
) where {T<:Integer} | ||
return if parallel === :threads | ||
threaded_random_greedy_color(g, reps) | ||
elseif parallel === :distributed | ||
distr_random_greedy_color(g, reps) | ||
else | ||
error( | ||
"Unsupported parallel argument '$(repr(parallel))' (supported: ':threads' or ':distributed')", | ||
) | ||
end | ||
end | ||
|
||
function threaded_random_greedy_color(g::AbstractGraph{T}, reps::Integer) where {T<:Integer} | ||
local_best = Any[nothing for _ in 1:reps] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to use Vector{Graphs.Coloring{T}}(undef, reps) or Vector{Union{Nothing, Graphs.Coloring{T}}}(nothing, reps) ? |
||
Base.Threads.@threads for i in 1:reps | ||
seq = shuffle(vertices(g)) | ||
local_best[i] = Graphs.perm_greedy_color(g, seq) | ||
end | ||
best = reduce(Graphs.best_color, local_best) | ||
|
||
return convert(Graphs.Coloring{T}, best) | ||
end | ||
|
||
function distr_random_greedy_color(g::AbstractGraph{T}, reps::Integer) where {T<:Integer} | ||
best = @distributed (Graphs.best_color) for i in 1:reps | ||
seq = shuffle(vertices(g)) | ||
Graphs.perm_greedy_color(g, seq) | ||
|
@@ -8,11 +33,11 @@ | |
end | ||
|
||
function greedy_color( | ||
g::AbstractGraph{U}; sort_degree::Bool=false, reps::Integer=1 | ||
g::AbstractGraph{U}; sort_degree::Bool=false, reps::Integer=1, parallel=:distributed | ||
) where {U<:Integer} | ||
return if sort_degree | ||
Graphs.degree_greedy_color(g) | ||
else | ||
Parallel.random_greedy_color(g, reps) | ||
Parallel.random_greedy_color(g, reps; parallel) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,31 +7,43 @@ | |
distmx1 = [Inf 2.0 Inf; 2.0 Inf 4.2; Inf 4.2 Inf] | ||
distmx2 = [Inf 2.0 Inf; 3.2 Inf 4.2; 5.5 6.1 Inf] | ||
|
||
for g in testgraphs(a1) | ||
z = @inferred(Graphs.eccentricity(g, distmx1)) | ||
y = @inferred(Parallel.eccentricity(g, distmx1)) | ||
@test isapprox(y, z) | ||
@test @inferred(Graphs.diameter(y)) == | ||
@inferred(Parallel.diameter(g, distmx1)) == | ||
6.2 | ||
@test @inferred(Graphs.periphery(y)) == | ||
@inferred(Parallel.periphery(g, distmx1)) == | ||
[1, 3] | ||
@test @inferred(Graphs.radius(y)) == @inferred(Parallel.radius(g, distmx1)) == 4.2 | ||
@test @inferred(Graphs.center(y)) == @inferred(Parallel.center(g, distmx1)) == [2] | ||
for parallel in [:threads, :distributed] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might make sense to write the test as @testset "Parallel.Distance" for parallel in [:threads, :distributed]
[...]
end |
||
for g in testgraphs(a1) | ||
z = @inferred(Graphs.eccentricity(g, distmx1)) | ||
y = @inferred(Parallel.eccentricity(g, distmx1; parallel)) | ||
@test isapprox(y, z) | ||
@test @inferred(Graphs.diameter(y)) == | ||
@inferred(Parallel.diameter(g, distmx1)) == | ||
6.2 | ||
@test @inferred(Graphs.periphery(y)) == | ||
@inferred(Parallel.periphery(g, distmx1)) == | ||
[1, 3] | ||
@test @inferred(Graphs.radius(y)) == | ||
@inferred(Parallel.radius(g, distmx1)) == | ||
4.2 | ||
@test @inferred(Graphs.center(y)) == | ||
@inferred(Parallel.center(g, distmx1)) == | ||
[2] | ||
end | ||
end | ||
|
||
for g in testdigraphs(a2) | ||
z = @inferred(Graphs.eccentricity(g, distmx2)) | ||
y = @inferred(Parallel.eccentricity(g, distmx2)) | ||
@test isapprox(y, z) | ||
@test @inferred(Graphs.diameter(y)) == | ||
@inferred(Parallel.diameter(g, distmx2)) == | ||
6.2 | ||
@test @inferred(Graphs.periphery(y)) == | ||
@inferred(Parallel.periphery(g, distmx2)) == | ||
[1] | ||
@test @inferred(Graphs.radius(y)) == @inferred(Parallel.radius(g, distmx2)) == 4.2 | ||
@test @inferred(Graphs.center(y)) == @inferred(Parallel.center(g, distmx2)) == [2] | ||
for parallel in [:threads, :distributed] | ||
for g in testdigraphs(a2) | ||
z = @inferred(Graphs.eccentricity(g, distmx2)) | ||
y = @inferred(Parallel.eccentricity(g, distmx2; parallel)) | ||
@test isapprox(y, z) | ||
@test @inferred(Graphs.diameter(y)) == | ||
@inferred(Parallel.diameter(g, distmx2)) == | ||
6.2 | ||
@test @inferred(Graphs.periphery(y)) == | ||
@inferred(Parallel.periphery(g, distmx2)) == | ||
[1] | ||
@test @inferred(Graphs.radius(y)) == | ||
@inferred(Parallel.radius(g, distmx2)) == | ||
4.2 | ||
@test @inferred(Graphs.center(y)) == | ||
@inferred(Parallel.center(g, distmx2)) == | ||
[2] | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know we haven't done that in other places - but perhaps to make this code more future proof would it make sense to use a specific structure to specify the parallel policy? Then we could later add things such as the number of threads/cores used.