This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into VertexCover
- Loading branch information
Showing
6 changed files
with
106 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
generate_reduce(g, gen_func, comp, reps; parallel=:threads) | ||
Compute `gen_func(g)` `reps` times and return the instance `best` for which | ||
`comp(best, v)` is true where `v` is all the other instances of `gen_func(g)`. | ||
For example, `comp(x, y) = length(x) < length(y) ? x : y` then instance with the smallest | ||
length will be returned. | ||
""" | ||
generate_reduce(g::AbstractGraph{T}, gen_func::Function, comp::Comp, reps::Integer; parallel=:threads) where {T<:Integer,Comp} = | ||
parallel == :threads ? threaded_generate_reduce(g, gen_func, comp, reps) : distr_generate_reduce(g, gen_func, comp, reps) | ||
|
||
""" | ||
distr_generate_min_set(g, gen_func, comp, reps) | ||
Distributed implementation of [`LightGraphs.generate_reduce`](@ref). | ||
""" | ||
function distr_generate_reduce( | ||
g::AbstractGraph{T}, | ||
gen_func::Function, | ||
comp::Comp, | ||
reps::Integer | ||
) where {T<:Integer,Comp} | ||
# Type assert required for type stability | ||
min_set::Vector{T} = @distributed ((x, y)->comp(x, y) ? x : y) for _ in 1:reps | ||
gen_func(g) | ||
end | ||
return min_set | ||
end | ||
|
||
""" | ||
threaded_generate_reduce(g, gen_func, comp reps) | ||
Multi-threaded implementation of [`LightGraphs.generate_reduce`](@ref). | ||
""" | ||
function threaded_generate_reduce( | ||
g::AbstractGraph{T}, | ||
gen_func::Function, | ||
comp::Comp, | ||
reps::Integer | ||
) where {T<:Integer,Comp} | ||
n_t = Base.Threads.nthreads() | ||
is_undef = ones(Bool, n_t) | ||
min_set = [Vector{T}() for _ in 1:n_t] | ||
|
||
Base.Threads.@threads for _ in 1:reps | ||
t = Base.Threads.threadid() | ||
next_set = gen_func(g) | ||
if is_undef[t] || comp(next_set, min_set[t]) | ||
min_set[t] = next_set | ||
is_undef[t] = false | ||
end | ||
end | ||
|
||
min_ind = 0 | ||
for i in filter((j)->!is_undef[j], 1:n_t) | ||
if min_ind == 0 || comp(min_set[i], min_set[min_ind]) | ||
min_ind = i | ||
end | ||
end | ||
|
||
return min_set[min_ind] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
@testset "Parallel.Generate Reduce" begin | ||
|
||
function make_vec(g::AbstractGraph{T}) where T<:Integer | ||
return Vector{T}(undef, nv(g)) | ||
end | ||
|
||
function comp_vec(x::Vector, y::Vector) | ||
return length(x) < length(y) | ||
end | ||
|
||
g1 = StarGraph(5) | ||
|
||
for parallel in [:distributed, :threads] | ||
for g in testgraphs(g1) | ||
|
||
s = @inferred(LightGraphs.Parallel.generate_reduce(g, make_vec, comp_vec, 5; parallel=parallel)) | ||
@test length(s) == 5 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters