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

Use atomic tag counter #118

Merged
merged 2 commits into from
Aug 31, 2022
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
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# History of Measurements.jl

## v2.8.0 (2022-09-01)

### New features

* The counter of the tags of `Measurement` objects is now atomic
([#118](https://github.com/JuliaPhysics/Measurements.jl/pull/118)).

## v2.7.2 (2022-05-21)

### Bug Fixes
Expand Down
9 changes: 4 additions & 5 deletions src/Measurements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ end
@generated empty_der1(x::Measurement{T}) where {T<:AbstractFloat} = Derivatives{T}()
@generated empty_der2(x::T) where {T<:AbstractFloat} = Derivatives{x}()

const tag_counters = UInt64[1]
function __init__()
nthr = Base.Threads.nthreads()
resize!(tag_counters, nthr)[:] = range(UInt64(1), step=typemax(UInt64)÷nthr, length=nthr)
# Start from 1, 0 is reserved to derived quantities
const tag_counter = Threads.Atomic{UInt64}(1)

function __init__()
@require Unitful="1986cc42-f94f-5a68-af5c-568840ba703d" include("unitful.jl")
@require QuadGK="1fd47b50-473d-5c70-9696-f719f8f3bcdc" include("quadgk.jl")
@require SpecialFunctions="276daf66-3868-5448-9aa4-cd146d93841b" include("special-functions.jl")
Expand All @@ -95,7 +94,7 @@ function measurement(val::T, err::T) where {T<:AbstractFloat}
if iszero(err)
Measurement{T}(val, err, UInt64(0), newder)
else
@inbounds tag = tag_counters[Base.Threads.threadid()] += 1
tag = Threads.atomic_add!(tag_counter, UInt64(1))
return Measurement{T}(val, err, tag, Derivatives(newder, (val, err, tag)=>one(T)))
end
end
Expand Down