ThreadsX.jl
ThreadsX.jl copied to clipboard
Wrong results with `reduce`
I am trying to count the number of unique in sorted array. The Base.reduce
works as expected but ThreadsX.reduce
gives the wrong result.
See
function unique_count_reduce_inner((cnt, last_a), new_a)
cnt += (last_a != new_a)
cnt, new_a
end
reduce_nunique(a) = reduce(unique_count_reduce_inner, a; init = (1, a[1]))[1]
using ThreadsX
treduce_nunique(a) = ThreadsX.reduce(unique_count_reduce_inner, a; init = (1, a[1]))[1]
a = rand(1_000_000)
sort!(a)
@time reduce_nunique(a) # 0.5
@time treduce_nunique(a) # 0.3
Looks like init
as a keyword argument is not supported.
julia> mapreduce(identity, +, 1:10, init = 100)
155
julia> ThreadsX.mapreduce(identity, +, 1:10, init = 100)
1055
init
is used on each thread instead of only once.