JuliaSIMD / JuliaSIMD/LoopVectorization.jl
Plain for loop faster than @turbo
- Ngôn ngữ chính
- Julia
- Star
- 789
- Fork
- 73
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
Consider the following:
```julia
using LoopVectorization, StrideArraysCore, BenchmarkTools
randn_stridearray(size...) = StrideArray(randn(Float32, size...), static.(size))
y = randn_stridearray(64, 32, 256);
x = randn_stridearray(64, 32, 256);
b = randn_stridearray(32);
function foo!(y, x, b)
@turbo for i in axes(y, 1), c in axes(y, 2), n in axes(y, 3)
y[i, c, n] = x[i, c, n] + b[c]
end
return nothing
end
function foo2!(y, x, b)
for n in axes(y, 3)
@turbo for i in axes(y, 1), c in axes(y, 2)
y[i, c, n] = x[i, c, n] + b[c]
end
end
return nothing
end
```
I would expect `foo2` to perform slightly worse or the same as `foo`.
However, the result is
```
julia> @btime foo!($y, $x, $b)
153.958 μs (0 allocations: 0 bytes)
julia> @btime foo2!($y, $x, $b)
51.583 μs (0 allocations: 0 bytes)
```
What is happening here? Am I doing something wrong?
If it matters: I am running this on an Apple M1.
Edit:
A plain for loop is about as fast as `foo2!`:
```
julia> function foo3!(y, x, b)
for n in axes(y, 3)
for c in axes(y, 2)
for i in axes(y, 1)
y[i, c, n] = x[i, c, n] + b[c]
end
end
end
return nothing
end
foo3! (generic function with 1 method)
julia> @btime foo3!($y, $x, $b)
52.375 μs (0 allocations: 0 bytes)
```
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Hướng nghiên cứu
Start by reproducing the reported timings for foo!, foo2!, and foo3! from the issue using the shown Julia, LoopVectorization, StrideArraysCore, and BenchmarkTools setup. Compare the generated behavior or benchmark results for the three loop forms; the investigation is complete when the performance difference is explained and any needed project change is identified.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- julia
- Lĩnh vực
- performance
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 35/100