ThreadsX.jl icon indicating copy to clipboard operation
ThreadsX.jl copied to clipboard

Wrong results with `reduce`

Open xiaodaigh opened this issue 3 years ago • 1 comments

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

xiaodaigh avatar Aug 29 '20 14:08 xiaodaigh

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.

jishnub avatar Jul 12 '21 09:07 jishnub