-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
reducedim much slower for user-defined functions #3426
Comments
Failure to inline user defined functions is one issue that leads to suboptimal performance of Issue #2325 shows that |
Here is what I am considering to address the inlining problem -- use typed functor instead Julia functions. abstract BinaryOp
type Add <: BinaryOp end
evaluate(op::Add, x, y) = x + y
function fast_reduce{R<:Number}(op::BinaryOp, initvalue::R, a::AbstractArray)
v::R = initvalue
for i in 1 : length(a)
v = evaluate(op, v, a[i])
end
v
end
plus(x, y) = x + y
a = rand(10^6)
# warming
sum(a)
fast_reduce(Add(), 0., a)
reduce(plus, 0., a)
# benchmark
@time for i in 1 : 500 sum(a) end # 0.3955 sec
@time for i in 1 : 500 fast_reduce(Add(), 0., a) end # 0.3938 sec
@time for i in 1 : 500 reduce(plus, 0., a) end # 21.62 sec Note here that I believe this addresses the performance issue without losing generality. |
Yes, both inlining and cache-unfriendliness are big issues here. Even along the first dimension, where cache isn't a problem, things are 1 OOM slower than they could be. Along the second dimension it's more like 2 OOM. I think the best solution to the inlining problem is to specialize code generation on function arguments, which I think has been mentioned in the past, although I can't find the issue for it. As a stopgap, I'd be happy with a type-based approach like yours above. If the order of iteration were fixed in |
Yes, specialized code generation depending on function arguments is the ideal way, which, however, probably takes much longer as it may need some changes in the core. At this moment, I am going to work on an improved version of reduction based on typed functors. |
Special case of #210. |
gives:
This seems to be due to lack of inlining. I don't see an easy solution that can be implemented in
reducedim()
itself, although maybe others do. Alternatively, it would be nice to movegen_reducedim_func
outside of thelet
block inarray.jl
so that other code can use it.See also my comment at https://groups.google.com/forum/?fromgroups=#!topic/julia-dev/ipbrvflVY1k
The text was updated successfully, but these errors were encountered: