-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates for better OffsetArray support. Check the start values of ite…
…rables, and handle different starting offsets of OffsetArrays based on linear vs cartesian indexing.
- Loading branch information
1 parent
b720d11
commit 73e2ed5
Showing
10 changed files
with
144 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
struct LoopValue end | ||
@inline VectorizationBase.stridedpointer(::LoopValue) = LoopValue() | ||
@inline VectorizationBase.vload(::LoopValue, i::Tuple{_MM{W}}) where {W} = _MM{W}(@inbounds(i[1].i) + 1) | ||
# @inline VectorizationBase.vload(::LoopValue, i::Tuple{_MM{W}}, ::Unsigned) where {W} = _MM{W}(@inbounds(i[1].i) + 1) | ||
@inline VectorizationBase.vload(::LoopValue, i::Tuple{_MM{W}}, ::Mask) where {W} = _MM{W}(@inbounds(i[1].i) + 1) | ||
@inline VectorizationBase.vload(::LoopValue, i::Integer) = i + one(i) | ||
@inline VectorizationBase.vload(::LoopValue, i::Tuple{I}) where {I<:Integer} = @inbounds(i[1]) + one(I) | ||
@inline Base.eltype(::LoopValue) = Int8 | ||
|
||
import OffsetArrays | ||
|
||
# If ndim(::OffsetArray) == 1, we can convert to a regular strided pointer and offset. | ||
@inline VectorizationBase.stridedpointer(a::OffsetArrays.OffsetArray{<:Any,1}) = gesp(stridedpointer(parent(a)), (-@inbounds(a.offsets[1]),)) | ||
|
||
struct OffsetStridedPointer{T, N, P <: VectorizationBase.AbstractStridedPointer{T}} <: VectorizationBase.AbstractStridedPointer{T} | ||
ptr::P | ||
offsets::NTuple{N,Int} | ||
end | ||
# if ndim(A::OffsetArray) ≥ 2, then eachindex(A) isa Base.OneTo, index starting at 1. | ||
# but multiple indexing is calculated using offsets, so we need a special type to express this. | ||
@inline function VectorizationBase.stridedpointer(A::OffsetArrays.OffsetArray) | ||
OffsetStridedPointer(stridedpointer(parent(A)), A.offsets) | ||
end | ||
# Tuple of length == 1, use ind directly. | ||
# @inline VectorizationBase.offset(ptr::OffsetStridedPointer, ind::Tuple{I}) where {I} = VectorizationBase.offset(ptr.ptr, ind) | ||
# Tuple of length > 1, subtract offsets. | ||
# @inline VectorizationBase.offset(ptr::OffsetStridedPointer{<:Any,N}, ind::Tuple) where {N} = VectorizationBase.offset(ptr.ptr, ntuple(n -> ind[n] + ptr.offsets[n], Val{N}())) | ||
@inline VectorizationBase.offset(ptr::OffsetStridedPointer, ind::Tuple{I}) where {I} = ind | ||
# Tuple of length > 1, subtract offsets. | ||
@inline VectorizationBase.offset(ptr::OffsetStridedPointer{<:Any,N}, ind::Tuple) where {N} = ntuple(n -> ind[n] - ptr.offsets[n], Val{N}()) | ||
@inline Base.similar(p::OffsetStridedPointer, ptr::Ptr) = OffsetStridedPointer(similar(p.ptr, ptr), p.offsets) | ||
|
||
# If an OffsetArray is getting indexed by a (loop-)constant value, then this particular vptr object cannot also be eachindexed, so we can safely return a stridedpointer | ||
@inline function VectorizationBase.subsetview(ptr::OffsetStridedPointer{<:Any,N}, ::Val{I}, i) where {I,N} | ||
subsetview(gesp(ptr.ptr, ntuple(n -> 0 - @inbounds(ptr.offsets[n]), Val{N}())), Val{I}(), i) | ||
end | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using LoopVectorization, OffsetArrays | ||
using Test | ||
|
||
@testset "OffsetArrays" begin | ||
|
||
function old2d!(out::AbstractMatrix, A::AbstractMatrix, kern, R=CartesianIndices(out), z=zero(eltype(out))) | ||
rng1k, rng2k = axes(kern) | ||
rng1, rng2 = R.indices | ||
for j in rng2, i in rng1 | ||
tmp = z | ||
@inbounds for jk in rng2k, ik in rng1k | ||
tmp += oftype(tmp, A[i+ik,j+jk])*kern[ik,jk] | ||
end | ||
@inbounds out[i,j] = tmp | ||
end | ||
out | ||
end | ||
function avx2d!(out::AbstractMatrix, A::AbstractMatrix, kern::OffsetArray, R=CartesianIndices(out), z=zero(eltype(out))) | ||
rng1k, rng2k = axes(kern) | ||
rng1, rng2 = R.indices | ||
# Manually unpack the OffsetArray | ||
kernA = parent(kern) | ||
o1, o2 = kern.offsets | ||
for j in rng2, i in rng1 | ||
tmp = z | ||
@avx for jk in rng2k, ik in rng1k | ||
tmp += A[i+ik,j+jk]*kernA[ik-o1,jk-o2] | ||
end | ||
out[i,j] = tmp | ||
end | ||
out | ||
end | ||
function avx2douter!(out::AbstractMatrix, A::AbstractMatrix, kern::OffsetArray, R=CartesianIndices(out), z=zero(eltype(out))) | ||
rng1k, rng2k = axes(kern) | ||
rng1, rng2 = R.indices | ||
# Manually unpack the OffsetArray | ||
kernA = parent(kern) | ||
o1, o2 = kern.offsets | ||
@avx for j in rng2, i in rng1 | ||
tmp = z | ||
for jk in rng2k, ik in rng1k | ||
tmp += A[i+ik,j+jk]*kernA[ik-o1,jk-o2] | ||
1 | ||
end | ||
out[i,j] = tmp | ||
end | ||
out | ||
end | ||
|
||
for T ∈ (Float32, Float64) | ||
@show T, @__LINE__ | ||
A = rand(T, 100, 100); | ||
kern = OffsetArray(rand(T, 3, 3), -1:1, -1:1); | ||
out1 = OffsetArray(similar(A, size(A).-2), 1, 1); # stay away from the edges of A | ||
out2 = similar(out1); out3 = similar(out1); | ||
|
||
old2d!(out1, A, kern); | ||
avx2d!(out2, A, kern); | ||
@test out1 ≈ out2 | ||
avx2douter!(out3, A, kern); | ||
@test out1 ≈ out3 | ||
end | ||
|
||
|
||
end | ||
|
This file contains 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