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

Implement accumulate and friends for Tuple #34654

Merged
merged 4 commits into from
Feb 26, 2020

Conversation

tkf
Copy link
Member

@tkf tkf commented Feb 4, 2020

close #26690

Demo:

julia> demo() = cumprod((1, 2, 3))
demo (generic function with 1 method)

julia> @code_typed optimize=true demo()
CodeInfo(
1return (1, 2, 6)
) => Tuple{Int64,Int64,Int64}

function accumulate(op, xs::Tuple; init = _InitialValue())
rf = BottomRF(op)
ys, = foldl(xs; init = ((), init)) do (ys, acc), x
acc = rf(acc, x)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this uses the machinery I added in #33526

julia/base/reduce.jl

Lines 73 to 78 in 3720edf

struct BottomRF{T}
rf::T
end
@inline (op::BottomRF)(::_InitialValue, x) = reduce_first(op.rf, x)
@inline (op::BottomRF)(acc, x) = op.rf(acc, x)

(ys..., acc), acc
end
return ys
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this implementation lead to unrolled code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It const-folds, as shown in the OP. It is also unrolled when the tuple is not constant:

julia> @code_typed optimize=true cumsum(ntuple(identity, 10))
CodeInfo(
1 ── %1  = (getfield)(itr, 1)::Int64%2  = (getfield)(itr, 2)::Int64%3  = (getfield)(itr, 3)::Int64%4  = (getfield)(itr, 4)::Int64%5  = (getfield)(itr, 5)::Int64%6  = (getfield)(itr, 6)::Int64%7  = (getfield)(itr, 7)::Int64%8  = (getfield)(itr, 8)::Int64%9  = (getfield)(itr, 9)::Int64%10 = (getfield)(itr, 10)::Int64%11 = Base.add_int(%1, %2)::Int64%12 = Base.add_int(%11, %3)::Int64%13 = Base.add_int(%12, %4)::Int64%14 = Base.add_int(%13, %5)::Int64%15 = Base.add_int(%14, %6)::Int64%16 = Base.add_int(%15, %7)::Int64%17 = Base.add_int(%16, %8)::Int64%18 = Base.add_int(%17, %9)::Int64%19 = Base.add_int(%18, %10)::Int64%20 = Core.tuple(%1, %11, %12, %13, %14, %15, %16, %17, %18, %19)::NTuple{10,Int64}
└───       goto #3
2 ──       $(Expr(:meta, :nkw, 1))
3 ┄─       goto #4
4 ──       goto #6
5 ──       $(Expr(:meta, :nkw, 1))
6 ┄─       goto #7
7 ──       goto #9
8 ──       $(Expr(:meta, :nkw, 1))
9 ┄─       goto #10
10return %20
) => NTuple{10,Int64}

@tkf
Copy link
Member Author

tkf commented Feb 26, 2020

Bump. Can this be merged?

@StefanKarpinski StefanKarpinski merged commit fde1ec8 into JuliaLang:master Feb 26, 2020
@tkf tkf deleted the accumulate-tuple branch February 26, 2020 23:25
ravibitsgoa pushed a commit to ravibitsgoa/julia that referenced this pull request Apr 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cumsum and friends don't support tuples
4 participants