Created
December 25, 2020 20:59
-
-
Save chriselrod/e91518c74429a0dd3450d3bd3d6df470 to your computer and use it in GitHub Desktop.
Sum over size range
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
N = iszero(length(ARGS)) ? 64 : parse(Int, last(ARGS)); | |
@show N | |
using LoopVectorization, Random, BenchmarkTools | |
x = rand(max(512,N)); y = Vector{Float64}(undef, N); | |
Ns = shuffle(Base.OneTo(N)); | |
@inline function mysumavx_inline(a) | |
s = zero(eltype(a)) | |
@avx for i ∈ eachindex(a) | |
s += a[i] | |
end | |
s | |
end | |
@inline function mysum_inline(a) | |
s = zero(eltype(a)) | |
@fastmath @inbounds @simd for i ∈ eachindex(a) | |
s += a[i] | |
end | |
s | |
end | |
@inline function myselfdotavx_inline(a) | |
s = zero(eltype(a)) | |
@avx for i ∈ eachindex(a) | |
s += a[i]*a[i] | |
end | |
s | |
end | |
@inline function myselfdot_inline(a) | |
s = zero(eltype(a)) | |
@fastmath @inbounds @simd for i ∈ eachindex(a) | |
s += a[i]*a[i] | |
end | |
s | |
end | |
@noinline mysumavx_noinline(a) = mysumavx_inline(a) | |
@noinline mysum_noinline(a) = mysum_inline(a) | |
@noinline myselfdotavx_noinline(a) = myselfdotavx_inline(a) | |
@noinline myselfdot_noinline(a) = myselfdot_inline(a) | |
mapviews!(f::F, y, x, Ns) where {F} = map!(@inline(n -> f(view(x, Base.OneTo(n)))), y, Ns) | |
println("Checking that results match.") | |
@assert mapviews!(mysum_inline, y, x, Ns) ≈ mapviews!(mysumavx_inline, similar(y), x, Ns) | |
@assert mapviews!(myselfdot_inline, y, x, Ns) ≈ mapviews!(myselfdotavx_inline, similar(y), x, Ns) | |
@assert mapviews!(mysum_noinline, y, x, Ns) ≈ mapviews!(mysumavx_noinline, similar(y), x, Ns) | |
@assert mapviews!(myselfdot_noinline, y, x, Ns) ≈ mapviews!(myselfdotavx_noinline, similar(y), x, Ns) | |
println("Results match.") | |
println("@fastmath @inbounds @simd sum - inline:") | |
display(@benchmark mapviews!(mysum_inline, $y, $x, $Ns)); println() | |
println("@avx sum - inline:") | |
display(@benchmark mapviews!(mysumavx_inline, $y, $x, $Ns)); println() | |
println("@fastmath @inbounds @simd self-dot - inline:") | |
display(@benchmark mapviews!(myselfdot_inline, $y, $x, $Ns)); println() | |
println("@avx self-dot - inline:") | |
display(@benchmark mapviews!(myselfdotavx_inline, $y, $x, $Ns)); println() | |
println("@fastmath @inbounds @simd sum - noinline:") | |
display(@benchmark mapviews!(mysum_noinline, $y, $x, $Ns)); println() | |
println("@avx sum - noinline:") | |
display(@benchmark mapviews!(mysumavx_noinline, $y, $x, $Ns)); println() | |
println("@fastmath @inbounds @simd self-dot - noinline:") | |
display(@benchmark mapviews!(myselfdot_noinline, $y, $x, $Ns)); println() | |
println("@avx self-dot - noinline:") | |
display(@benchmark mapviews!(myselfdotavx_noinline, $y, $x, $Ns)); println() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment