Suppose I have a function f(x)
that takes constant time, and a map
as follows:
function task(xs)
map(xs) do x
f(x)
end
end
What is the most straightforward way to perform the computation with multiple threads in Julia v1.9?
Use ThreadsX.map
(without a package)
I can use Transducers.tcollect
, but was wondering if I could avoid the dependency.
I'm sure you can do it with @sync @spawn
or whatever, I just don't see the point
The only reason is to avoid an extra dependency, but I may end up adding Transducers.jl to the dep list at the end of the day.
do you know the output type of f(::typeof(x))
?
Yes, I have this a priori. In this context, x
is a point in , represented by Point
struct, and the result is a Float64
Probably the easiest thing would just be do to
julia> using Base.Threads: nthreads, @spawn, @sync
julia> function tmap(f, ::Type{R}, xs::Array; tasks_per_thread::Int=2) where {R}
out = Array{R}(undef, size(xs)...)
chunk_size = max(1, length(xs) ÷ (tasks_per_thread * nthreads()))
@sync for chunk ∈ Iterators.partition(enumerate(xs), chunk_size)
@spawn for (i, x) ∈ chunk
out[i] = f(x)
end
end
out
end
Thank you @Mason Protter , I will mark as solved.
Júlio Hoffimann has marked this topic as resolved.
but please test it in case I fucked up
partition
assumes indexability, no?
Yes?
just a caveat that this means the solution doesn't work if xs
_isn't_ indexable
But arrays are indexable..
right, noone mentioned that the input is arrays though
the clean general solution would be the same as for https://github.com/JuliaLang/julia/pull/52096 - I hope it gets merged soon
The function is constrained to only take arrays
your tmap
is constrained, the original task
function isn't
Last updated: Nov 06 2024 at 04:40 UTC