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

add parallelism to reducible example #490

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions examples/reducibles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,37 @@ end
Base.length(vov::VecOfVec) = sum(length, vov.vectors)

collect(vov)

# # Adding parallelism
#
# Up to now our vector of vector is not parallelizable. For instance if we try `tcollect(vov)` we get an error.
# To add support, we need to overload two methods from [SplittablesBase.jl](https://github.com/JuliaFolds/SplittablesBase.jl):

import Transducers: SplittablesBase # hide
import SplittablesBase
function SplittablesBase.amount(vov::VecOfVec)
# return the rough length of a collection
sum(length, vov.vectors)
end
function SplittablesBase.halve(vov::VecOfVec)
# split the collection in two halves of roughy same size
if length(vov.vectors) == 1
l, r = SplittablesBase.halve(first(vov.vectors))
left = VecOfVec([collect(l)])
right = VecOfVec([collect(r)])
else
isplit = round(Int, length(vov.vectors)/2)
left_inds = 1:isplit
right_inds = (isplit+1):length(vov.vectors)
left = VecOfVec(vov.vectors[ left_inds])
right = VecOfVec(vov.vectors[right_inds])
end
(left, right)
end

# Now it works:

tcollect(vov)

@assert collect(vov) == vcat(vov.vectors...) # hide
@assert tcollect(vov) == collect(vov) # hide