Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into VertexCover
Browse files Browse the repository at this point in the history
  • Loading branch information
sbromberger authored Sep 9, 2018
2 parents 2ef593b + df8d83a commit 08d8fdb
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/Parallel/Parallel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ include("distance.jl")
include("traversals/bfs.jl")
include("traversals/gdistances.jl")
include("traversals/greedy_color.jl")
include("utils.jl")

# Overload until https://github.com/JuliaLang/julia/pull/28651
import Distributed: splitrange
Expand Down
63 changes: 63 additions & 0 deletions src/Parallel/utils.jl
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
30 changes: 20 additions & 10 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,28 @@ function insorted(item, collection)
end

"""
generate_min_set(g, gen_func, Reps)
Generate a vector `Reps` times using `gen_func(g)` and return the vector with the least elements.
"""
generate_min_set(g::AbstractGraph{T}, gen_func, Reps::Integer) where T<: Integer =
mapreduce(gen_func, (x, y)->length(x)<length(y) ? x : y, Iterators.repeated(g, Reps))
unweighted_contiguous_partition(num_items, required_partitions)
Partition `1:num_items` into `required_partitions` number of partitions such that the
difference in length of the largest and smallest partition is atmost 1.
### Performance
Time: O(required_partitions)
"""
generate_max_set(g, gen_func, Reps)
Generate a vector `Reps` times using `gen_func(g)` and return the vector with the most elements.
"""
generate_max_set(g::AbstractGraph{T}, gen_func, Reps::Integer) where T<: Integer =
mapreduce(gen_func, (x, y)->length(x)>length(y) ? x : y, Iterators.repeated(g, Reps))
function unweighted_contiguous_partition(
num_items::Integer,
required_partitions::Integer
)

left = 1
part = Vector{UnitRange}(undef, required_partitions)
for i in 1:required_partitions
len = fld(num_items+i-1, required_partitions)
part[i] = left:(left+len-1)
left += len
end
return part
end


"""
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ tests = [
"shortestpaths/johnson",
"traversals/bfs",
"traversals/gdistances",
"traversals/greedy_color"
"traversals/greedy_color",
"utils"
]

@testset "LightGraphs.Parallel" begin
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/utils.jl
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
19 changes: 0 additions & 19 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,6 @@
end
end

@testset "Generate Reduce" begin

function make_vec(g::AbstractGraph{T}) where T<:Integer
return Vector{T}(undef, nv(g))
end

g1 = StarGraph(5)

for g in testgraphs(g1)
s = @inferred(LightGraphs.generate_max_set(g, make_vec, 5))
@test length(s) == 5
end

for g in testgraphs(g1)
s = @inferred(LightGraphs.generate_max_set(g, make_vec, 5))
@test length(s) == 5
end
end

@testset "Unweighted Contiguous Partition" begin

p = @inferred(LightGraphs.unweighted_contiguous_partition(4, 2))
Expand Down

0 comments on commit 08d8fdb

Please sign in to comment.