Skip to content

Commit

Permalink
Merge pull request #118 from JuliaSIMD/limitthreads
Browse files Browse the repository at this point in the history
Strided pattern support
  • Loading branch information
chriselrod committed Sep 16, 2023
2 parents 8a5e647 + 8d1988f commit f0e3ef3
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 182 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Polyester"
uuid = "f517fe37-dbe3-4b94-8317-1923a5111588"
authors = ["Chris Elrod <[email protected]> and contributors"]
version = "0.7.5"
version = "0.7.6"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Expand Down
3 changes: 1 addition & 2 deletions src/Polyester.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ Resets the threads used by [Polyester.jl](https://github.com/JuliaSIMD/Polyester
"""
function reset_threads!()
PolyesterWeave.reset_workers!()
foreach(ThreadingUtilities.checktask,
eachindex(ThreadingUtilities.TASKS))
foreach(ThreadingUtilities.checktask, eachindex(ThreadingUtilities.TASKS))
return nothing
end

Expand Down
129 changes: 6 additions & 123 deletions src/batch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ end
@cfunction($(Expr(:$, :bc)), Cvoid, (Ptr{UInt},))
end
end
return Expr(:block, Expr(:meta,:inline), q)
return Expr(:block, Expr(:meta, :inline), q)
end
# @inline function batch_closure(f::F, args::A, ::Val{C}) where {F,A,C}
# bc = BatchClosure{F,A,C}(f)
Expand Down Expand Up @@ -108,6 +108,7 @@ function add_var!(q, argtup, gcpres, ::Type{T}, argtupname, gcpresname, k) where
push!(gcpres.args, garg_k)
end
end

@generated function _batch_no_reserve(
f!::F,
threadlocal::Val{thread_local},
Expand Down Expand Up @@ -187,104 +188,12 @@ end
push!(q.args, nothing)
q
end
# @generated function _batch_reserve(
# f!::F, threadmask_tuple::Tuple{Vararg{Any,N}}, nthreads_tuple, torelease_tuple, nbatch, unused_threads, Nr, Nd, ulen, args::Vararg{Any,K}
# ) where {F,K,N}
# q = quote
# nthread_total = nbatch - one(nbatch)
# nthread_denom = $(N == 1 ? 0 : :(sum(nthreads_tuple)))
# Ndp = Nd + one(Nd)
# nres_per = Base.udiv_int(unused_threads, nbatch)
# nres_rem = unused_threads - nres_per * nbatch
# nres_prr = nres_per + one(nres_per)
# # nbatch_per_thread =
# end
# nthread_expr = if N == 1
# quote
# nthread = nthread_total
# end
# else
# quote
# nthreads == zero(nthreads) && continue
# nthread = max(1, (nthread_total * nthreads) ÷ nthread_denom)

# end
# end
# break_cond = N == 1 ? :(i == nthread) : :((i == nthread) | (j == nthread_total))
# block = quote
# start = zero(UInt)
# i = zero(nres_rem)
# tid = 0x00000000
# j = 0x00000000
# for (threads, nthreads) ∈ zip(threadmask_tuple,nthreads_tuple)
# $nthread_expr
# threads = UnsignedIteratorEarlyStop(threadmask, nthread)
# tm = mask(threads)
# wait_mask = zero(worker_type())
# while true
# assume(tm ≠ zero(tm))
# tz = trailing_zeros(tm) % UInt32
# reserve = ifelse(i < nres_rem, nres_prr, nres_per)
# tz += 0x00000001
# stop = start + ifelse(i < Nr, Ndp, Nd)
# tid += tz
# tid_to_launch = tid
# wait_mask |= (one(wait_mask) << (tid - one(tid)))
# tm >>>= tz
# reserved_threads = zero(worker_type())
# for _ ∈ 1:reserve
# assume(tm ≠ zero(tm))
# tz = trailing_zeros(tm) % UInt32
# tz += 0x00000001
# tid += tz
# tm >>>= tz
# reserved_threads |= (one(reserved_threads) << (tid - one(tid)))
# end
# reserve_threads!(tid_to_launch, reserved_threads)
# launch_batched_thread!(cfunc, tid_to_launch, argtup, start, stop)
# i += one(i)
# start = stop
# $break_cond && break
# end
# end
# reserved_threads = zero(worker_type())
# for _ ∈ 1:nres_per
# assume(tm ≠ zero(tm))
# tz = trailing_zeros(tm) % UInt32
# tz += 0x00000001
# tid += tz
# tm >>>= tz
# reserved_threads |= (one(reserved_threads) << (tid - one(tid)))
# end
# reserve_threads!(0x00000000, reserved_threads)
# f!(arguments, (start+one(UInt)) % Int, ulen % Int)
# free_threads!(reserved_threads)
# reserve_threads!(0x00000000, zero(worker_type()))
# tid = 0x00000000
# while true
# assume(wait_mask ≠ zero(wait_mask))
# tz = (trailing_zeros(wait_mask) % UInt32) + 0x00000001
# wait_mask >>>= tz
# tid += tz
# ThreadingUtilities.wait(tid)
# iszero(wait_mask) && break
# end
# nothing
# end
# gcpr = Expr(:gc_preserve, block, :cfunc)
# argt = Expr(:tuple)
# for k ∈ 1:K
# add_var!(q, argt, gcpr, args[k], :args, :gcp, k)
# end
# push!(q.args, :(arguments = $argt), :(argtup = Reference(arguments)), :(cfunc = batch_closure(f!, argtup, Val{true}())), gcpr, :(free_local_threads!()))
# push!(q.args, nothing)
# q
# end

@inline function batch(
f!::F,
(len, nbatches)::Tuple{Vararg{Union{StaticInt,Integer},2}},
args::Vararg{Any,K}) where {F,K}
args::Vararg{Any,K},
) where {F,K}

batch(f!, Val{false}(), (len, nbatches), args...)
end
Expand All @@ -293,7 +202,7 @@ end
f!::F,
threadlocal::Val{thread_local},
(len, nbatches)::Tuple{Vararg{Union{StaticInt,Integer},2}},
args::Vararg{Any,K}
args::Vararg{Any,K},
) where {F,K,thread_local}
len > 0 || return
if (nbatches > len)
Expand Down Expand Up @@ -328,7 +237,7 @@ end
Nr,
Nd,
ulen,
args...
args...,
)
end
function batch(
Expand All @@ -338,30 +247,4 @@ function batch(
threadlocal::Val{thread_local} = Val(false),
) where {F,K,thread_local}
batch(f!, (len, nbatches), args...; threadlocal)
# ulen = len % UInt
# if nbatches > 1
# requested_threads = reserve_per_worker*nbatches
# threads, torelease = request_threads(Base.Threads.threadid(), requested_threads - one(nbatches))
# nthreads = map(length, threads)
# nthread = sum(nthreads)
# if nthread % Int32 > zero(Int32)
# total_threads = nthread + one(nthread)
# nbatch = min(total_threads % UInt32, nbatches % UInt32)

# Nd = Base.udiv_int(ulen, nbatch % UInt)
# Nr = ulen - Nd * nbatch

# unused_threads = total_threads - nbatch
# threadmasks = map(mask,threads)
# if iszero(unused_threads)
# _batch_no_reserve(f!, threadmasks, nthreads, torelease, Nr, Nd, ulen, args...)
# else

# _batch_reserve(f!, threadmasks, nthreads, torelease, nbatch, unused_threads, Nr, Nd, ulen, args...)
# end
# return nothing
# end
# end
# f!(args, one(Int), ulen%Int)
# return nothing
end
Loading

2 comments on commit f0e3ef3

@chriselrod
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/91576

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.6 -m "<description of version>" f0e3ef34bd42f04f229315e5446a54c2b8af7748
git push origin v0.7.6

Please sign in to comment.