tkf / ThreadsX.jl

Parallelized Base functions
MIT License
321 stars 10 forks source link

Error with `reduce` #151

Open xiaodaigh opened 4 years ago

xiaodaigh commented 4 years ago

I implemented a function with reduce and ThreadsX.reduce. The threads version failed.

using ThreadsX

feature = rand(Int, 1_000_000)
target = rand(Bool, 1_000_000)

function reduce_groupreduce(feature, target)
    zft = zip(feature, target)

    f_fold, t_fold = reduce((u, x) -> begin
        last_f = last(u[1])
        target_sum = last(u[2])

        f = x[1]
        t = x[2]

        if isequal(f, last_f)
            u[2][end] += t
        else
            push!(u[1], f)
            push!(u[2], t)
        end
        u
    end, zft;
    init = (feature[1:1], Int[0]))
end

@time reduce_groupreduce(feature, target) # this works

function reduce_groupreduce(feature, target)
    zft = zip(feature, target)

    f_fold, t_fold = ThreadsX.reduce((u, x) -> begin
        last_f = last(u[1])
        target_sum = last(u[2])

        f = x[1]
        t = x[2]

        if isequal(f, last_f)
            u[2][end] += t
        else
            push!(u[1], f)
            push!(u[2], t)
        end
        u
    end, zft;
    init = (feature[1:1], Int[0]))
end

@time reduce_groupreduce(feature, target) # this doesn't