Stream: helpdesk (published)

Topic: ✔ Most straightforward way to add threads to a `map` call.


view this post on Zulip Júlio Hoffimann (Dec 26 2023 at 19:23):

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?

view this post on Zulip jar (Dec 26 2023 at 19:29):

Use ThreadsX.map

view this post on Zulip Júlio Hoffimann (Dec 26 2023 at 19:29):

(without a package)

view this post on Zulip Júlio Hoffimann (Dec 26 2023 at 19:30):

I can use Transducers.tcollect, but was wondering if I could avoid the dependency.

view this post on Zulip jar (Dec 26 2023 at 19:30):

I'm sure you can do it with @sync @spawn or whatever, I just don't see the point

view this post on Zulip Júlio Hoffimann (Dec 26 2023 at 19:31):

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.

view this post on Zulip Mason Protter (Dec 26 2023 at 19:36):

do you know the output type of f(::typeof(x))?

view this post on Zulip Júlio Hoffimann (Dec 26 2023 at 19:42):

Yes, I have this a priori. In this context, x is a point in R3R^3, represented by Point struct, and the result is a Float64

view this post on Zulip Mason Protter (Dec 26 2023 at 19:51):

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

view this post on Zulip Júlio Hoffimann (Dec 26 2023 at 19:52):

Thank you @Mason Protter , I will mark as solved.

view this post on Zulip Notification Bot (Dec 26 2023 at 19:52):

Júlio Hoffimann has marked this topic as resolved.

view this post on Zulip Mason Protter (Dec 26 2023 at 19:53):

but please test it in case I fucked up

view this post on Zulip Sukera (Dec 26 2023 at 21:22):

partition assumes indexability, no?

view this post on Zulip Mason Protter (Dec 26 2023 at 21:23):

Yes?

view this post on Zulip Sukera (Dec 26 2023 at 21:23):

just a caveat that this means the solution doesn't work if xs _isn't_ indexable

view this post on Zulip Mason Protter (Dec 26 2023 at 21:24):

But arrays are indexable..

view this post on Zulip Sukera (Dec 26 2023 at 21:24):

right, noone mentioned that the input is arrays though

view this post on Zulip Sukera (Dec 26 2023 at 21:25):

the clean general solution would be the same as for https://github.com/JuliaLang/julia/pull/52096 - I hope it gets merged soon

view this post on Zulip Mason Protter (Dec 26 2023 at 21:25):

The function is constrained to only take arrays

view this post on Zulip Sukera (Dec 26 2023 at 21:25):

your tmap is constrained, the original task function isn't


Last updated: Nov 22 2024 at 04:41 UTC