JuliaSIMD / ThreadingUtilities.jl

Utilities for low overhead threading in Julia.
MIT License
17 stars 4 forks source link

THREADPOOL: Use the runtime value of `Sys.CPU_THREADS` #6

Closed DilumAluthge closed 3 years ago

DilumAluthge commented 3 years ago

Fixes #4

codecov-io commented 3 years ago

Codecov Report

Merging #6 (c947be1) into master (23434f6) will increase coverage by 1.02%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master       #6      +/-   ##
==========================================
+ Coverage   96.93%   97.95%   +1.02%     
==========================================
  Files           3        3              
  Lines          98       98              
==========================================
+ Hits           95       96       +1     
+ Misses          3        2       -1     
Impacted Files Coverage Δ
src/threadtasks.jl 96.42% <ø> (+3.32%) :arrow_up:
src/ThreadingUtilities.jl 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 23434f6...dd65cfb. Read the comment docs.

chriselrod commented 3 years ago

What does @code_warntype taskpointer(1) and @codetyped task pointer(1) look like before and after?

DilumAluthge commented 3 years ago

What does @code_warntype taskpointer(1) and @code_typed task pointer(1) look like before and after?

Good question. One sec, I'll run it.

DilumAluthge commented 3 years ago

With ThreadingUtilities#master:

julia> using ThreadingUtilities

julia> @code_warntype ThreadingUtilities.taskpointer(1)
Variables
  #self#::Core.Const(ThreadingUtilities.taskpointer)
  tid::Int64

Body::Ptr{UInt64}
1 ─      nothing
│   %2 = Base.getindex(ThreadingUtilities.THREADPOOL, tid)::ThreadingUtilities.ThreadTask
│   %3 = ThreadingUtilities.pointer(%2)::Ptr{UInt64}
└──      return %3
julia> using ThreadingUtilities

julia> @code_typed ThreadingUtilities.taskpointer(1)
CodeInfo(
1 ─ %1 = ThreadingUtilities.THREADPOOL::Core.Const((ThreadingUtilities.ThreadTask(Base.RefValue{NTuple{32, UInt64}}((0x0000000000000001, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000))), ThreadingUtilities.ThreadTask(Base.RefValue{NTuple{32, UInt64}}((0x0000000000000001, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000))), ThreadingUtilities.ThreadTask(Base.RefValue{NTuple{32, UInt64}}((0x0000000000000001, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000)))))
│   %2 = Base.getfield(%1, tid, $(Expr(:boundscheck)))::ThreadingUtilities.ThreadTask
│   %3 = Base.getfield(%2, :memory)::Base.RefValue{NTuple{32, UInt64}}
│   %4 = $(Expr(:foreigncall, :(:jl_value_ptr), Ptr{Nothing}, svec(Any), 0, :(:ccall), :(%3)))::Ptr{Nothing}
│   %5 = Base.bitcast(Ptr{UInt64}, %4)::Ptr{UInt64}
└──      return %5
) => Ptr{UInt64}

With https://github.com/DilumAluthge/ThreadingUtilities.jl#dpa/relocatable:

julia> using ThreadingUtilities

julia> @code_warntype ThreadingUtilities.taskpointer(1)
Variables
  #self#::Core.Const(ThreadingUtilities.taskpointer)
  tid::Int64

Body::Ptr{UInt64}
1 ─      nothing
│   %2 = Base.getindex(ThreadingUtilities.THREADPOOL, tid)::ThreadingUtilities.ThreadTask
│   %3 = ThreadingUtilities.pointer(%2)::Ptr{UInt64}
└──      return %3
julia> using ThreadingUtilities

julia> @code_typed ThreadingUtilities.taskpointer(1)
CodeInfo(
1 ─ %1 = ThreadingUtilities.THREADPOOL::Core.Const((ThreadingUtilities.ThreadTask(Base.RefValue{NTuple{32, UInt64}}((0x0000000000000001, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000))), ThreadingUtilities.ThreadTask(Base.RefValue{NTuple{32, UInt64}}((0x0000000000000001, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000))), ThreadingUtilities.ThreadTask(Base.RefValue{NTuple{32, UInt64}}((0x0000000000000001, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000)))))
│   %2 = Base.getfield(%1, tid, $(Expr(:boundscheck)))::ThreadingUtilities.ThreadTask
│   %3 = Base.getfield(%2, :memory)::Base.RefValue{NTuple{32, UInt64}}
│   %4 = $(Expr(:foreigncall, :(:jl_value_ptr), Ptr{Nothing}, svec(Any), 0, :(:ccall), :(%3)))::Ptr{Nothing}
│   %5 = Base.bitcast(Ptr{UInt64}, %4)::Ptr{UInt64}
└──      return %5
) => Ptr{UInt64}
DilumAluthge commented 3 years ago

Some of the lines in the @code_typed output is really long. You'll need to scroll pretty far to the right to see it all.

chriselrod commented 3 years ago

Great, I was worried it being defined later would cause problems.