From 93dcdd68d561d50e6a888cf7188ae809a9938430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Mon, 28 Mar 2022 22:33:00 +0100 Subject: [PATCH 1/2] Use task-local storage for the tag, instead of thread-local --- src/Measurements.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Measurements.jl b/src/Measurements.jl index 100797b7..3b31e4dd 100644 --- a/src/Measurements.jl +++ b/src/Measurements.jl @@ -77,10 +77,8 @@ 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) + task_local_storage("measurementsjl-tag", 1) @require Unitful="1986cc42-f94f-5a68-af5c-568840ba703d" include("unitful.jl") @require QuadGK="1fd47b50-473d-5c70-9696-f719f8f3bcdc" include("quadgk.jl") @@ -95,7 +93,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 + @inbounds tag = task_local_storage("measurementsjl-tag", task_local_storage("measurementsjl-tag") + 1) return Measurement{T}(val, err, tag, Derivatives(newder, (val, err, tag)=>one(T))) end end From ea061f83d19e75737eaae1f9b3e8d94dbe40c8e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Sat, 2 Apr 2022 18:33:41 +0100 Subject: [PATCH 2/2] Use atomic tag counter --- NEWS.md | 7 +++++++ src/Measurements.jl | 7 ++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index aba2037c..5802ce6d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/src/Measurements.jl b/src/Measurements.jl index 3b31e4dd..cdee6eac 100644 --- a/src/Measurements.jl +++ b/src/Measurements.jl @@ -77,9 +77,10 @@ end @generated empty_der1(x::Measurement{T}) where {T<:AbstractFloat} = Derivatives{T}() @generated empty_der2(x::T) where {T<:AbstractFloat} = Derivatives{x}() -function __init__() - task_local_storage("measurementsjl-tag", 1) +# 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") @@ -93,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 = task_local_storage("measurementsjl-tag", task_local_storage("measurementsjl-tag") + 1) + tag = Threads.atomic_add!(tag_counter, UInt64(1)) return Measurement{T}(val, err, tag, Derivatives(newder, (val, err, tag)=>one(T))) end end