Skip to content

Commit

Permalink
Simplify 3-arg conv, remove unnecessary inbounds (#610)
Browse files Browse the repository at this point in the history
* clean 2d conv

CartesianIndices(A)

* remove inbounds from `unsafe_conv_kern_os!`

slightly faster on v1.10, about the same on v1.6

* vt for clarity

* Apply suggestion to avoid broadcasting

Co-authored-by: Martin Holters <[email protected]>

* don't plan fft

Co-authored-by: Martin Holters <[email protected]>

---------

Co-authored-by: Martin Holters <[email protected]>
  • Loading branch information
wheeheee and martinholters authored Dec 13, 2024
1 parent 51c17d6 commit 8607be8
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/dspbase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ function unsafe_conv_kern_os_edge!(
# on an edge.
#
# First make all entries equal to the center blocks:
@inbounds copyto!(perimeter_range, 1, center_block_ranges, 1, N)
copyto!(perimeter_range, 1, center_block_ranges, 1, N)

# For the dimensions chosen to be on an edge (edge_dims), get the
# ranges of the blocks that would need to be padded (lie on an edge)
Expand All @@ -421,15 +421,15 @@ function unsafe_conv_kern_os_edge!(
# The center region for non-edge dimensions has been specified above,
# so finish specifying the region of the perimeter for this edge
# block
@inbounds for (i, dim) in enumerate(edge_dims)
for (i, dim) in enumerate(edge_dims)
perimeter_range[dim] = perimeter_edge_ranges[i]
end

# Region of block indices, not data indices!
block_region = CartesianIndices(
NTuple{N, UnitRange{Int}}(perimeter_range)
)
@inbounds for block_pos in block_region
for block_pos in block_region
# Figure out which portion of the input data should be transformed

block_idx = convert(NTuple{N, Int}, block_pos)
Expand Down Expand Up @@ -571,7 +571,7 @@ function unsafe_conv_kern_os!(out,
# Portion of buffer with valid result of convolution
valid_buff_region = CartesianIndices(UnitRange.(sv, nffts))
# Iterate over block indices (not data indices) that do not need to be padded
@inbounds for block_pos in CartesianIndices(center_block_ranges)
for block_pos in CartesianIndices(center_block_ranges)
# Calculate portion of data to transform

block_idx = convert(NTuple{N, Int}, block_pos)
Expand Down Expand Up @@ -794,13 +794,13 @@ function conv(u::AbstractVector{T}, v::Transpose{T,<:AbstractVector}, A::Abstrac
if any(conv_axis_with_offset, (axes(u)..., axes(v)..., axes(A)...))
throw(ArgumentError("offset axes not supported"))
end
m = length(u)+size(A,1)-1
n = length(v)+size(A,2)-1
m = length(u) + size(A, 1) - 1
n = length(v) + size(A, 2) - 1
B = zeros(T, m, n)
B[1:size(A,1),1:size(A,2)] = A
u = fft([u;zeros(T,m-length(u))])
v = fft([v transpose(zeros(T,n-length(v)))])
C = ifft(fft(B) .* (u * v))
B[CartesianIndices(A)] = A
u = fft(_zeropad(u, m))
vt = fft(_zeropad(transpose(v), n))
C = ifft(fft(B) .*= u .* transpose(vt))
if T <: Real
return real(C)
end
Expand Down

0 comments on commit 8607be8

Please sign in to comment.