Skip to content

Commit

Permalink
tmap for enumerate(chunks(...))
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenbauer committed Sep 18, 2024
1 parent 2702ac4 commit 66bc292
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/implementation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ function tmap(f, ::Type{T}, A::AbstractArray, _Arrs::AbstractArray...; kwargs...
end

function tmap(f,
A::Union{AbstractArray, ChunkSplitters.Chunk},
A::Union{AbstractArray, ChunkSplitters.Chunk, ChunkSplitters.Enumerate},
_Arrs::AbstractArray...;
scheduler::MaybeScheduler = NotGiven(),
kwargs...)
Expand All @@ -333,7 +333,8 @@ function tmap(f,
_scheduler.split != :batch
error("Only `split == :batch` is supported because the parallel operation isn't commutative. (Scheduler: $_scheduler)")
end
if A isa ChunkSplitters.Chunk && chunking_enabled(_scheduler)
if (A isa ChunkSplitters.Chunk || A isa ChunkSplitters.Enumerate) &&
chunking_enabled(_scheduler)
auto_disable_chunking_warning()
if _scheduler isa DynamicScheduler
_scheduler = DynamicScheduler(;
Expand Down Expand Up @@ -377,7 +378,7 @@ end
# w/o chunking (DynamicScheduler{NoChunking}): ChunkSplitters.Chunk
function _tmap(scheduler::DynamicScheduler{NoChunking},
f,
A::ChunkSplitters.Chunk,
A::Union{ChunkSplitters.Chunk, ChunkSplitters.Enumerate},
_Arrs::AbstractArray...)
(; threadpool) = scheduler
tasks = map(A) do idcs
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ end;
@test tmapreduce(+, enumerate(chunks(data; size=5)); chunking=false) do (i, idcs)
[i, sum(@view(data[idcs]))]
end == [sum(1:20), sum(data)]
@test tmap(enumerate(chunks(data; n=5)); chunking=false) do (i, idcs)
[i, idcs]
end == [[1, 1:20], [2, 21:40], [3, 41:60], [4, 61:80], [5, 81:100]]
end;

@testset "macro API" begin
Expand Down Expand Up @@ -267,6 +270,11 @@ end;
@set reducer = +
[i, sum(@view(data[idcs]))]
end) == [sum(1:20), sum(data)]
@test @tasks(for (i, idcs) in enumerate(chunks(1:100; n=5))
@set chunking=false
@set collect=true
[i, idcs]
end) == [[1, 1:20], [2, 21:40], [3, 41:60], [4, 61:80], [5, 81:100]]
end;

@testset "WithTaskLocals" begin
Expand Down

0 comments on commit 66bc292

Please sign in to comment.