Skip to content

FFT: Add GPU support and tests #633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/array/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,26 @@ function copyto_view!(Bpart, Brange, Apart, Arange)
copyto!(view(Bpart, Brange), view(Apart, Arange))
return
end

function Base.copyto!(B::DArray{T,N}, A::Array{T,N}) where {T,N}
if size(B) != size(A)
# Fallback to the default implementation
return Base.invoke(copyto!, Tuple{AbstractArray, AbstractArray}, B, A)
end

A_view = view(A, B.partitioning)
copyto!(B, A_view)

return B
end
function Base.copyto!(B::Array{T,N}, A::DArray{T,N}) where {T,N}
if size(B) != size(A)
# Fallback to the default implementation
return Base.invoke(copyto!, Tuple{AbstractArray, AbstractArray}, B, A)
end

B_view = view(B, A.partitioning)
copyto!(B_view, A)

return B
end
8 changes: 5 additions & 3 deletions src/memory-spaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,13 @@ function _memory_spans(a::StridedAliasing{T,N,S}, spans, ptr, dim) where {T,N,S}

return spans
end
memory_space(x::SubArray) = memory_space(parent(x))
function aliasing(x::SubArray{T,N,A}) where {T,N,A<:Array}
if isbitstype(T)
S = CPURAMMemorySpace
return StridedAliasing{T,ndims(x),S}(RemotePtr{Cvoid}(pointer(parent(x))),
RemotePtr{Cvoid}(pointer(x)),
space = memory_space(x)
S = typeof(space)
return StridedAliasing{T,ndims(x),S}(RemotePtr{Cvoid}(pointer(parent(x)), space),
RemotePtr{Cvoid}(pointer(x), space),
parentindices(x),
size(x), strides(parent(x)))
else
Expand Down
275 changes: 145 additions & 130 deletions test/array/fft.jl
Original file line number Diff line number Diff line change
@@ -1,141 +1,156 @@
using FFTW

@testset "FFT" begin
@testset for T in (ComplexF64, ComplexF32)
@testset "1D" begin
# Out-of-place
A = rand(T, 100)
DA = DArray(A)
B = fft(A)
DB = fft(DA)
@test DB isa DVector{T}
@test B ≈ collect(DB)

# In-place
A = rand(T, 100)
DA = DArray(A)
fft!(A)
fft!(DA)
@test A ≈ collect(DA)
end

@testset "2D" begin
# Out-of-place
A = rand(T, 100, 100)
DA = DArray(A)
B = fft(A)
DB = fft(DA)
@test DB isa DMatrix{T}
@test B ≈ collect(DB)

# In-place
A = rand(T, 100, 100)
DA = DArray(A)
fft!(A)
fft!(DA)
@test A ≈ collect(DA)
function test_fft()
@testset "FFT" begin
@testset for T in (ComplexF64, ComplexF32)
@testset "1D" begin
# Out-of-place
A = rand(T, 100)
DA = DArray(A)
B = fft(A)
DB = fft(DA)
@test DB isa DVector{T}
@test B ≈ collect(DB)

# In-place
A = rand(T, 100)
DA = DArray(A)
fft!(A)
fft!(DA)
@test A ≈ collect(DA)
end

@testset "2D" begin
# Out-of-place
A = rand(T, 100, 100)
DA = DArray(A)
B = fft(A)
DB = fft(DA)
@test DB isa DMatrix{T}
@test B ≈ collect(DB)

# In-place
A = rand(T, 100, 100)
DA = DArray(A)
fft!(A)
fft!(DA)
@test A ≈ collect(DA)
end

@testset "3D" begin
# Out-of-place (Pencil)
A = rand(T, 100, 100, 100)
DA = DArray(A)
B = fft(A)
DB = fft(DA; decomp=:pencil)
@test DB isa DArray{T, 3}
@test B ≈ collect(DB)

# Out-of-place (Slab)
A = rand(T, 100, 100, 100)
DA = DArray(A)
B = fft(A)
DB = fft(DA; decomp=:slab)
@test DB isa DArray{T, 3}
@test B ≈ collect(DB)

# In-place (Pencil)
A = rand(T, 100, 100, 100)
DA = DArray(A)
fft!(A)
fft!(DA; decomp=:pencil)
@test A ≈ collect(DA)

# In-place (Slab)
A = rand(T, 100, 100, 100)
DA = DArray(A)
fft!(A)
fft!(DA; decomp=:slab)
@test A ≈ collect(DA)
end
end
end

@testset "3D" begin
# Out-of-place (Pencil)
A = rand(T, 100, 100, 100)
DA = DArray(A)
B = fft(A)
DB = fft(DA; decomp=:pencil)
@test DB isa DArray{T, 3}
@test B ≈ collect(DB)

# Out-of-place (Slab)
A = rand(T, 100, 100, 100)
DA = DArray(A)
B = fft(A)
DB = fft(DA; decomp=:slab)
@test DB isa DArray{T, 3}
@test B ≈ collect(DB)

# In-place (Pencil)
A = rand(T, 100, 100, 100)
DA = DArray(A)
fft!(A)
fft!(DA; decomp=:pencil)
@test A ≈ collect(DA)

# In-place (Slab)
A = rand(T, 100, 100, 100)
DA = DArray(A)
fft!(A)
fft!(DA; decomp=:slab)
@test A ≈ collect(DA)
@testset "IFFT" begin
for T in (ComplexF64, ComplexF32)
@testset "1D" begin
# Out-of-place
A = rand(T, 100)
DA = DArray(A)
B = ifft(A)
DB = ifft(DA)
@test DB isa DVector{T}
@test B ≈ collect(DB)

# In-place
A = rand(T, 100)
DA = DArray(A)
ifft!(A)
ifft!(DA)
@test A ≈ collect(DA)
end

@testset "2D" begin
# Out-of-place
A = rand(T, 100, 100)
DA = DArray(A)
B = ifft(A)
DB = ifft(DA)
@test DB isa DMatrix{T}
@test B ≈ collect(DB)

# In-place
A = rand(T, 100, 100)
DA = DArray(A)
ifft!(A)
ifft!(DA)
@test A ≈ collect(DA)
end

@testset "3D" begin
# Out-of-place (Pencil)
A = rand(T, 100, 100, 100)
DA = DArray(A)
B = ifft(A)
DB = ifft(DA; decomp=:pencil)
@test DB isa DArray{T, 3}
@test B ≈ collect(DB)

# Out-of-place (Slab)
A = rand(T, 100, 100, 100)
DA = DArray(A)
B = ifft(A)
DB = ifft(DA; decomp=:slab)
@test DB isa DArray{T, 3}
@test B ≈ collect(DB)

# In-place (Pencil)
A = rand(T, 100, 100, 100)
DA = DArray(A)
ifft!(A)
ifft!(DA; decomp=:pencil)
@test A ≈ collect(DA)

# In-place (Slab)
A = rand(T, 100, 100, 100)
DA = DArray(A)
ifft!(A)
ifft!(DA; decomp=:slab)
@test A ≈ collect(DA)
end
end
end
end

@testset "IFFT" begin
for T in (ComplexF64, ComplexF32)
@testset "1D" begin
# Out-of-place
A = rand(T, 100)
DA = DArray(A)
B = ifft(A)
DB = ifft(DA)
@test DB isa DVector{T}
@test B ≈ collect(DB)

# In-place
A = rand(T, 100)
DA = DArray(A)
ifft!(A)
ifft!(DA)
@test A ≈ collect(DA)
end

@testset "2D" begin
# Out-of-place
A = rand(T, 100, 100)
DA = DArray(A)
B = ifft(A)
DB = ifft(DA)
@test DB isa DMatrix{T}
@test B ≈ collect(DB)

# In-place
A = rand(T, 100, 100)
DA = DArray(A)
ifft!(A)
ifft!(DA)
@test A ≈ collect(DA)
end
@testset "CPU" begin
test_fft()
end

@testset "3D" begin
# Out-of-place (Pencil)
A = rand(T, 100, 100, 100)
DA = DArray(A)
B = ifft(A)
DB = ifft(DA; decomp=:pencil)
@test DB isa DArray{T, 3}
@test B ≈ collect(DB)

# Out-of-place (Slab)
A = rand(T, 100, 100, 100)
DA = DArray(A)
B = ifft(A)
DB = ifft(DA; decomp=:slab)
@test DB isa DArray{T, 3}
@test B ≈ collect(DB)

# In-place (Pencil)
A = rand(T, 100, 100, 100)
DA = DArray(A)
ifft!(A)
ifft!(DA; decomp=:pencil)
@test A ≈ collect(DA)

# In-place (Slab)
A = rand(T, 100, 100, 100)
DA = DArray(A)
ifft!(A)
ifft!(DA; decomp=:slab)
@test A ≈ collect(DA)
for (kind, scope) in GPU_SCOPES
kind == :CUDA || kind == :ROCm || continue
@testset "$kind" begin
Dagger.with_options(;scope) do
test_fft()
end
end
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ if USE_GPU
tests = [
("GPU", "gpu.jl"),
("Array - Stencils", "array/stencil.jl"),
("Array - FFT", "array/fft.jl"),
]
end
all_test_names = map(test -> replace(last(test), ".jl"=>""), tests)
Expand Down