JuliaSIMD / JuliaSIMD/LoopVectorization.jl
Weird behavior
- Dominant language
- Julia
- Stars
- 789
- Forks
- 73
- PR merge metrics
- No merged PRs in 30d
Description
Function `f()`, which goes over the data once, is more than ten times slower than function `f2()`, which goes over the data twice. Is this some kind of bug?
```julia
using BenchmarkTools, LoopVectorization, Random
function f(g::AbstractArray{T}, q::AbstractArray{T}, f::AbstractArray{T}) where T
r = zero(T)
I, J = size(g)
@turbo for j in 1:J
for i in 1:I
qf_local = zero(T)
for k in 1:K
qf_local += q[k, i] * f[k, j]
end
r += g[i, j] * log(qf_local) + (2one(T) - g[i, j]) * log(one(T) - qf_local)
end
end
end
function f2(g::AbstractArray{T}, q::AbstractArray{T}, f::AbstractArray{T}) where T
r = zero(T)
I, J = size(g)
@turbo for j in 1:J
for i in 1:I
qf_local = zero(T)
for k in 1:K
qf_local += q[k, i] * f[k, j]
end
r += g[i, j] * log(qf_local)
end
end
@turbo for j in 1:J
for i in 1:I
qf_local = zero(T)
for k in 1:K
qf_local += q[k, i] * f[k, j]
end
r += (2one(T) - g[i, j]) * log(one(T) - qf_local)
end
end
end
g = rand(100000, 1000)
qq = rand(3, 100000)
ff = rand(3, 1000)
```
```julia
@btime f(g, qq, ff)
```
1.961 s (22 allocations: 560 bytes)
```julia
@btime f2(g, qq, ff)
```
172.651 ms (53 allocations: 1.77 KiB)
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.