-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Inline the whole broadcast expression to avoid allocation #30986
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`map` has an inline limit of 16. To make sure that the whole broadcast tree gets inlined properly, I added the `_inlined_map` function. I am not sure if it is a good idea, but worth trying. This PR solves the issue which I have mentioned in JuliaLang@2693778#issuecomment-461248258 ```julia julia> @allocated foo(tmp, uprev, k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k13, k14, k15, k16, k17, k18, k19, k20, k21, k22, k23, k24, k25, k26, k27, k28, k29, k30, k31, k32, k33, k34) 0 ```
using Test
u, uprev, k1, k2, k3, k4, k5, k6, k7, k8 = (ones(1000) for i in 1:10)
function foo(u, uprev, k1, k2, k3, k4, k5, k6) # vectorized
@. u = uprev + 0.1*(0.1*k1 + 0.2*k2 + 0.3*k3 + 0.4*k4 + 0.5*k5 + 0.6*k6)
nothing
end
function goo(u, uprev, k1, k2, k3, k4, k5, k6, k7) # vectorized
@. u = uprev + 0.1*(0.1*k1 + 0.2*k2 + 0.3*k3 + 0.4*k4 + 0.5*k5 + 0.6*k6 + 0.7*k7)
nothing
end
function hoo(u, uprev, k1, k2, k3, k4, k5, k6, k7, k8) # not vectorized
@. u = uprev + 0.1*(0.1*k1 + 0.2*k2 + 0.3*k3 + 0.4*k4 + 0.5*k5 + 0.6*k6 + 0.7*k7 + 0.8*k8)
nothing
end
function ioo(u, uprev, k1, k2, k3, k4, k5, k6, k7, k8) # not vectorized
@. u = uprev + k1 + k2 + k3 + k4 + k5 + k6 + k7 + k8
nothing
end
@allocated foo(u, uprev, k1, k2, k3, k4, k5, k6)
@allocated goo(u, uprev, k1, k2, k3, k4, k5, k6, k7)
@allocated hoo(u, uprev, k1, k2, k3, k4, k5, k6, k7, k8)
@allocated ioo(u, uprev, k1, k2, k3, k4, k5, k6, k7, k8)
@test @allocated(foo(u, uprev, k1, k2, k3, k4, k5, k6)) == 0
@test @allocated(goo(u, uprev, k1, k2, k3, k4, k5, k6, k7)) == 0
@test @allocated(hoo(u, uprev, k1, k2, k3, k4, k5, k6, k7, k8)) == 0
@test @allocated(ioo(u, uprev, k1, k2, k3, k4, k5, k6, k7, k8)) == 0
@test occursin("vector.body", sprint(code_llvm, foo, NTuple{8, Vector{Float32}}))
@test occursin("vector.body", sprint(code_llvm, goo, NTuple{9, Vector{Float32}}))
@test occursin("vector.body", sprint(code_llvm, hoo, NTuple{10, Vector{Float32}}))
@test occursin("vector.body", sprint(code_llvm, ioo, NTuple{10, Vector{Float32}})) julia> ioo(u, uprev, k1, k2, k3, k4, k5, k6, k7, k8)
remark: simdloop.jl:71:0: loop not vectorized: cannot prove it is safe to reorder memory operations
remark: simdloop.jl:71:0: loop not vectorized
remark: simdloop.jl:71:0: loop not vectorized: cannot identify array bounds
remark: simdloop.jl:71:0: loop not vectorized
remark: simdloop.jl:71:0: loop not vectorized: cannot identify array bounds
remark: simdloop.jl:71:0: loop not vectorized
remark: simdloop.jl:71:0: loop not vectorized: cannot identify array bounds
remark: simdloop.jl:71:0: loop not vectorized
remark: simdloop.jl:71:0: loop not vectorized: cannot identify array bounds
remark: simdloop.jl:71:0: loop not vectorized Looks like the new vectorization cut off is 10 arrays. |
https://github.com/JuliaLang/julia/pull/30986/files#diff-fdaf2dae46d7b04f9c9d9e3940188cb9R802 wouldn't pass because Julia enables bounds check during testing. |
Nice work — thanks! |
mbauman
pushed a commit
that referenced
this pull request
Mar 14, 2019
* Inline the whole broadcast expression to avoid allocation `map` has an inline limit of 16. To make sure that the whole broadcast tree gets inlined properly, I added the `_inlined_map` function. I am not sure if it is a good idea, but worth trying. This PR solves the issue which I have mentioned in 2693778#issuecomment-461248258 ```julia julia> @allocated foo(tmp, uprev, k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, k11, k12, k13, k14, k15, k16, k17, k18, k19, k20, k21, k22, k23, k24, k25, k26, k27, k28, k29, k30, k31, k32, k33, k34) 0 ``` * Fix CI failure * Stricter test (vectorization & no allocation for a 9-array bc) * rm `_inlined_map`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
map
has an inline limit of 16. To make sure that the whole broadcast tree gets inlined properly, I added the_inlined_map
function. I am not sure if it is a good idea, but worth trying.This PR solves the issue which I have mentioned in#30973
As LLVM vectorization cut-off is 10 arrays, it doesn't make sense to try to make
map
inline for more than 16 args. I have removed_inlined_map
, but preserved other@inline
s to ensure a 9-array broadcast has no allocation.