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

Thread safety of printing and matmul #26562

Merged
merged 6 commits into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 10 additions & 4 deletions base/grisu/grisu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
module Grisu

export print_shortest
export DIGITS, grisu
export DIGITS, DIGITSs, grisu

const SHORTEST = 1
const FIXED = 2
const PRECISION = 3

const DIGITS = Vector{UInt8}(undef, 309+17)

# thread-safe code should use a per-thread DIGITS buffer DIGITSs[Threads.threadid()]
const DIGITSs = [DIGITS]
function __init__()
Threads.resize_nthreads!(DIGITSs)
end

include(joinpath("grisu", "float.jl"))
include(joinpath("grisu", "fastshortest.jl"))
include(joinpath("grisu", "fastprecision.jl"))
Expand All @@ -20,7 +26,7 @@ include(joinpath("grisu", "bignum.jl"))

const BIGNUMS = [Bignums.Bignum(),Bignums.Bignum(),Bignums.Bignum(),Bignums.Bignum()]

function grisu(v::AbstractFloat,mode,requested_digits,buffer=DIGITS,bignums=BIGNUMS)
function grisu(v::AbstractFloat,mode,requested_digits,buffer=@inbounds(DIGITSs[Threads.threadid()]),bignums=BIGNUMS)
if signbit(v)
neg = true
v = -v
Expand Down Expand Up @@ -65,7 +71,7 @@ function _show(io::IO, x::AbstractFloat, mode, n::Int, typed, compact)
return
end
typed && isa(x,Float16) && print(io, "Float16(")
(len,pt,neg),buffer = grisu(x,mode,n),DIGITS
(len,pt,neg),buffer = grisu(x,mode,n),@inbounds(DIGITSs[Threads.threadid()])
pdigits = pointer(buffer)
if mode == PRECISION
while len > 1 && buffer[len] == 0x30
Expand Down Expand Up @@ -153,7 +159,7 @@ function _print_shortest(io::IO, x::AbstractFloat, dot::Bool, mode, n::Int)
isnan(x) && return print(io, "NaN")
x < 0 && print(io,'-')
isinf(x) && return print(io, "Inf")
(len,pt,neg),buffer = grisu(x,mode,n),DIGITS
(len,pt,neg),buffer = grisu(x,mode,n),@inbounds(DIGITSs[Threads.threadid()])
pdigits = pointer(buffer)
e = pt-len
k = -9<=e<=9 ? 1 : 2
Expand Down
Loading