Skip to content

Commit

Permalink
Use atomic tag counter (#118)
Browse files Browse the repository at this point in the history
* Use task-local storage for the tag, instead of thread-local

* Use atomic tag counter
  • Loading branch information
giordano authored Aug 31, 2022
1 parent 5e84abe commit c9218d2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
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

0 comments on commit c9218d2

Please sign in to comment.