Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty input #104

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
OhMyThreads.jl Changelog
=========================

Version 0.5.2
-------------
- ![Enhancement][badge-enhancement] For empty input (e.g. `Float64[]` or `11:10`) behavior is now aligned with the serial functions in `Base`.

Version 0.5.1
-------------
- ![Feature][badge-feature] Within a parallel `@tasks` block one can now mark a region with `@one_by_one`. This region will be run by one task at a time ("critical region").
Expand Down
15 changes: 4 additions & 11 deletions src/implementation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,9 @@ function tmapreduce(f, op, Arrs...;
kwargs...)
mapreduce_kwargs = isgiven(init) ? (; init) : (;)
_scheduler = _scheduler_from_userinput(scheduler; kwargs...)
if isempty(first(Arrs))
isempty(mapreduce_kwargs) && reduce_empty_err()
return mapreduce_kwargs.init
end

# @show _scheduler
if _scheduler isa SerialScheduler
if _scheduler isa SerialScheduler || isempty(first(Arrs))
# empty input collection → align with Base.mapreduce behavior
mapreduce(f, op, Arrs...; mapreduce_kwargs...)
else
@noinline _tmapreduce(f, op, Arrs, outputtype, _scheduler, mapreduce_kwargs)
Expand All @@ -78,10 +74,6 @@ end
throw(ArgumentError("Providing an explicit scheduler as well as direct keyword arguments (e.g. $(kwargstr)) is currently not supported."))
end

@noinline function reduce_empty_err()
throw(ArgumentError("reducing over an empty collection is not allowed; consider supplying `init`"))
end

treducemap(op, f, A...; kwargs...) = tmapreduce(f, op, A...; kwargs...)

# DynamicScheduler: AbstractArray/Generic
Expand Down Expand Up @@ -357,7 +349,8 @@ function tmap(f,
end

Arrs = (A, _Arrs...)
if _scheduler isa SerialScheduler
if _scheduler isa SerialScheduler || isempty(A)
# empty input collection → align with Base.map behavior
map(f, Arrs...; kwargs...)
else
check_all_have_same_indices(Arrs)
Expand Down
18 changes: 17 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,30 @@ end;
end;

@testset "empty collections" begin
@static if VERSION < v"1.11.0-"
err = MethodError
else
err = ArgumentError
end
for empty_coll in (11:9, Float64[])
for f in (sin, x -> im * x, identity)
for op in (+, *, min)
@test_throws ArgumentError tmapreduce(f, op, empty_coll)
# mapreduce
for init in (0.0, 0, 0.0 * im, 0.0f0)
@test tmapreduce(f, op, empty_coll; init) == init
end
# foreach
@test tforeach(f, empty_coll) |> isnothing
# reduce
if op != min
@test treduce(op, empty_coll) == reduce(op, empty_coll)
else
@test_throws err treduce(op, empty_coll)
end
# map
@test tmap(f, empty_coll) == map(f, empty_coll)
# collect
@test tcollect(empty_coll) == collect(empty_coll)
end
end
end
Expand Down
Loading