JuliaFolds2 / OhMyThreads.jl

Simple multithreading in julia
https://juliafolds2.github.io/OhMyThreads.jl/
MIT License
139 stars 9 forks source link

Support for `enumerate(chunks(...))` #117

Closed carstenbauer closed 1 month ago

carstenbauer commented 1 month ago

With this PR:

julia> @tasks for (c,v) in enumerate(chunks(rand(10); n=2))
           @set chunking=false
           @show c
       end
c = 2
c = 1

julia> @tasks for (c,v) in enumerate(chunks(rand(10); n=2))
           @set chunking=false
           @set reducer=+
           c
       end
3

julia> @tasks for (c,v) in enumerate(chunks(rand(10); n=2))
           @set chunking=false
           @set collect=true
           c
       end
2-element Vector{Int64}:
 1
 2

Before this PR:

julia> @tasks for (c,v) in enumerate(chunks(rand(10); n=2))
           @set chunking=false
           @show c
       end
ERROR: MethodError: no method matching keys(::ChunkSplitters.Enumerate{ChunkSplitters.Chunk{…}})

Closest candidates are:
  keys(::Pkg.Registry.RegistryInstance)
   @ Pkg ~/.julia/juliaup/julia-1.10.5+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/Pkg/src/Registry/registry_instance.jl:447
  keys(::LibGit2.GitTree)
   @ Revise ~/.julia/packages/Revise/uvGMC/src/git.jl:52
  keys(::Core.SimpleVector)
   @ Base essentials.jl:781
  ...

Stacktrace:
 [1] eachindex(itrs::ChunkSplitters.Enumerate{ChunkSplitters.Chunk{…}})
   @ Base ./abstractarray.jl:318
 [2] _tmapreduce(f::Function, op::Function, Arrs::Tuple{…}, ::Type{…}, scheduler::DynamicScheduler{…}, mapreduce_kwargs::@NamedTuple{…})
   @ OhMyThreads.Implementation ~/repos/OhMyThreads.jl/src/implementation.jl:97
 [3] #tmapreduce#21
   @ ~/repos/OhMyThreads.jl/src/implementation.jl:67 [inlined]
 [4] tmapreduce
   @ ~/repos/OhMyThreads.jl/src/implementation.jl:55 [inlined]
 [5] tforeach(f::Function, A::ChunkSplitters.Enumerate{ChunkSplitters.Chunk{…}}; kwargs::@Kwargs{chunking::Bool})
   @ OhMyThreads.Implementation ~/repos/OhMyThreads.jl/src/implementation.jl:293
 [6] macro expansion
   @ ~/repos/OhMyThreads.jl/src/macro_impl.jl:107 [inlined]
 [7] top-level scope
   @ REPL[4]:1
Some type information was truncated. Use `show(err)` to see complete types.
carstenbauer commented 1 month ago

@MasonProtter please merge if adequate.