-
Notifications
You must be signed in to change notification settings - Fork 32
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
Remove use of threadid
#429
Comments
Generally, I feel the general use of The more general use with mutable varinfo objects could be fixed, I assume, by restricting access with a lock. But maybe that's not the most efficient way. |
So it should still be functional for evaluation because, yes, even though the returned For the immutable varinfos we actually have the opportunity to even parallelize sampling by just implementing a
Seems like a good idea 👍 |
Btw, regarding this, how can It seems like vi.logps[Threads.threadid()] += logp and then another But there's no way the above line will actually cause the Oooor am I misunderstanding something here? |
True, it seems when opening the issue I thought it's worse than it actually is. Generally though, IMO using |
Haha, this is exactly the explanation I was looking at that made me question your original worry 😅 But no, I agree with you that the usage of |
Ah neat! Something like Dictionaries.jl would probably also be useful if you already know the return-type and the it's consistent across tasks. EDIT: On second thought, usage of Dictionaries.jl is probably not a great idea as it probably expands the underlying |
How bad of an idea is it to create a random buffer of julia> Threads.nthreads()
16
julia> f(x) = log(x)
f (generic function with 1 method)
julia> g(n) = sum(f, 1:n)
g (generic function with 1 method)
julia> function g_t(n; buffer_size=Threads.nthreads())
buffers = [Threads.Atomic{Float64}(0) for _ = 1:buffer_size]
Threads.@threads for i = 1:n
buffer_idx = rand(1:buffer_size)
Threads.atomic_add!(buffers[buffer_idx], f(i))
end
return sum(deref, buffers)
end
g_t (generic function with 1 method)
julia> @btime $g($(10_000_000));
74.448 ms (0 allocations: 0 bytes)
julia> @btime $g_t($(10_000_000); buffer_size=$(1));
613.850 ms (101 allocations: 8.56 KiB)
julia> @btime $g_t($(10_000_000); buffer_size=$(2));
386.434 ms (101 allocations: 8.55 KiB)
julia> @btime $g_t($(10_000_000); buffer_size=$(4));
351.702 ms (102 allocations: 8.56 KiB)
julia> @btime $g_t($(10_000_000); buffer_size=$(16));
152.913 ms (114 allocations: 8.84 KiB)
julia> @btime $g_t($(10_000_000); buffer_size=$(32));
91.895 ms (130 allocations: 9.22 KiB)
julia> @btime $g_t($(10_000_000); buffer_size=$(64));
88.032 ms (162 allocations: 9.98 KiB)
julia> @btime $g_t($(10_000_000); buffer_size=$(128));
83.266 ms (227 allocations: 11.52 KiB)
julia> @btime $g_t($(10_000_000); buffer_size=$(1024));
67.109 ms (1122 allocations: 32.55 KiB) 🙃 EDIT: A probably better solution would be to have |
Oh and another thing: Atomics won't work once you put AD in there, rigth? E.g. |
Just noticed that
ThreadSafeVarInfo
makes heavy use ofthreadid
:DynamicPPL.jl/src/threadsafe.jl
Lines 22 to 29 in 715526f
That should be changed since it is broken in Julia >= 1.8 if users don't use
@threads :static for ...
(see e.g. https://discourse.julialang.org/t/behavior-of-threads-threads-for-loop/76042).The text was updated successfully, but these errors were encountered: