Skip to content

Implement an option for tree-based reductions #8

@MasonProtter

Description

@MasonProtter

Currently, the way tmapreduce(f, op, A) works is that it does

    tasks = map(chunks(A; n=nchunks, split)) do inds
        @spawn mapreduce(f, op, @view(A[inds]); kwargs...)
    end
    mapreduce(fetch, op, tasks)

This means that after all the parallel tasks finish, we still do nchunks applications of op sequentially. This is normally fine, but for cases where op is very slow, it can be a killer (such as map where we use append!!).

Another strategy would be to do the final reduction as a parallel tree, so something like
image

where each step is done on separate tasks which properly parallelizes the application of op.

This could be written as

function tree_mapreduce(f, op, v::AbstractVector)
    v = @view v[begin:end]
    if length(v) > 2
        mid = (lastindex(v) - firstindex(v)) ÷ 2
        l, r = @views (v[begin:mid], v[mid+1:end])
        
        task_r = @spawn tree_mapreduce(f, op, r)
        result_l = tree_mapreduce(f, op, l)
        
        op(result_l, fetch(task_r))
    elseif length(v) == 2
        op(f(v[begin]), f(v[begin+1]))
    else
        f(only(v))
    end
end

but the compiler doesn't like the recursion here and gives up inferring it unfortunately.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions