Skip to content
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

Change multi-threading behavior of caches (AbstractTypeDicts). #549

Merged
merged 1 commit into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ julia:
matrix:
allow_failures:
- julia: nightly
env:
- JULIA_NUM_THREADS=2
branches:
only:
- master
Expand Down
10 changes: 5 additions & 5 deletions src/caches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function makevalue end

function Base.getindex(c::C, ::Type{T}) where {C<:AbstractTypeDict, T}
ReturnType = valuetype(C, T)
key = (objectid(T), Threads.threadid())
key = objectid(T)
@inbounds for i in eachindex(c.keys)
if c.keys[i] === key
return c.values[i]::ReturnType
Expand Down Expand Up @@ -45,7 +45,7 @@ MechanismState{Float64, Float64, Float64, …}(…)
"""
struct StateCache{M, JointCollection} <: AbstractTypeDict
mechanism::Mechanism{M}
keys::Vector{Tuple{UInt64, Int}}
keys::Vector{UInt}
values::Vector{MechanismState}
end

Expand Down Expand Up @@ -73,7 +73,7 @@ Similar to [`StateCache`](@ref).
"""
struct DynamicsResultCache{M} <: AbstractTypeDict
mechanism::Mechanism{M}
keys::Vector{Tuple{UInt64, Int}}
keys::Vector{UInt}
values::Vector{DynamicsResult{<:Any, M}}
end

Expand All @@ -92,12 +92,12 @@ objects. Similar to [`StateCache`](@ref).
struct SegmentedVectorCache{K, KeyRange<:AbstractUnitRange{K}} <: AbstractTypeDict
ranges::IndexDict{K, KeyRange, UnitRange{Int}}
length::Int
keys::Vector{Tuple{UInt64, Int}}
keys::Vector{UInt}
values::Vector{SegmentedVector}
end

function SegmentedVectorCache(ranges::IndexDict{K, KeyRange, UnitRange{Int}}) where {K, KeyRange<:AbstractUnitRange{K}}
SegmentedVectorCache(ranges, sum(length, values(ranges)), Vector{Tuple{UInt64, Int}}(), Vector{SegmentedVector}())
SegmentedVectorCache(ranges, sum(length, values(ranges)), Vector{UInt}(), Vector{SegmentedVector}())
end

@inline function valuetype(::Type{SegmentedVectorCache{K, KeyRange}}, ::Type{T}) where {K, T, KeyRange}
Expand Down
83 changes: 56 additions & 27 deletions test/test_caches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,69 @@ function cachetest(cache, eltypefun)
@test @allocated(cache[Float64]) == 0
end

@testset "StateCache" begin
Random.seed!(1)
mechanism = randmech()
cache = StateCache(mechanism)
cachetest(cache, RigidBodyDynamics.state_vector_eltype)
end

@testset "DynamicsResultCache" begin
@testset "Basic mechanism" begin
Random.seed!(2)
@testset "caches (nthreads = $(Threads.nthreads()))" begin
@testset "StateCache" begin
Random.seed!(1)
mechanism = randmech()
cache = DynamicsResultCache(mechanism)
cachetest(cache, result -> eltype(result.v̇))
cache = StateCache(mechanism)
cachetest(cache, RigidBodyDynamics.state_vector_eltype)
end

@testset "DynamicsResultCache" begin
@testset "Basic mechanism" begin
Random.seed!(2)
mechanism = randmech()
cache = DynamicsResultCache(mechanism)
cachetest(cache, result -> eltype(result.v̇))
end

@testset "Mechanism with contact points (Issue #483)" begin
Random.seed!(3)
mechanism = randmech()
contactmodel = SoftContactModel(hunt_crossley_hertz(k = 500e3),
ViscoelasticCoulombModel(0.8, 20e3, 100.))
body = rand(bodies(mechanism))
add_contact_point!(body,
ContactPoint(Point3D(default_frame(body), 0.0, 0.0, 0.0), contactmodel))
cache = DynamicsResultCache(mechanism)
cachetest(cache, result -> eltype(result.v̇))
end
end

@testset "Mechanism with contact points (Issue #483)" begin
@testset "SegmentedVectorCache" begin
Random.seed!(3)
mechanism = randmech()
contactmodel = SoftContactModel(hunt_crossley_hertz(k = 500e3),
ViscoelasticCoulombModel(0.8, 20e3, 100.))
body = rand(bodies(mechanism))
add_contact_point!(body,
ContactPoint(Point3D(default_frame(body), 0.0, 0.0, 0.0), contactmodel))
cache = DynamicsResultCache(mechanism)
cachetest(cache, result -> eltype(result.v̇))
state = MechanismState(mechanism)
cache = SegmentedVectorCache(RigidBodyDynamics.ranges(velocity(state)))
cachetest(cache, eltype)
end
end

@testset "SegmentedVectorCache" begin
Random.seed!(3)
mechanism = randmech()
state = MechanismState(mechanism)
cache = SegmentedVectorCache(RigidBodyDynamics.ranges(velocity(state)))
cachetest(cache, eltype)
@testset "StateCache multi-threaded (#548)" begin
N = 2
mechanism = randmech()

state_caches = [StateCache(mechanism) for _ = 1 : N]
qs = let state = MechanismState(mechanism)
[(rand_configuration!(state); copy(configuration(state))) for _ = 1 : N]
end
vs = [rand(num_velocities(mechanism)) for _ = 1 : N]

Threads.@threads for i = 1 : N
state = state_caches[i][Float64]
set_configuration!(state, qs[i])
set_velocity!(state, vs[i])
end

for i = 1 : N
@test configuration(state_caches[i][Float64]) == qs[i]
@test velocity(state_caches[i][Float64]) == vs[i]
end

Threads.@threads for i = 1 : N
@test configuration(state_caches[i][Float64]) == qs[i]
@test velocity(state_caches[i][Float64]) == vs[i]
end
end
end

end # module