Skip to content
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

Make transforms work on CUDA arrays #48

Merged
merged 7 commits into from
Jun 21, 2022
Merged

Make transforms work on CUDA arrays #48

merged 7 commits into from
Jun 21, 2022

Conversation

jipolanco
Copy link
Owner

@jipolanco jipolanco commented Jun 20, 2022

This PR allows performing FFTs on CuArrays.

Concretely, it's now possible to create and apply PencilFFTPlans on CUDA arrays.

Here is a short example:

using PencilFFTs
using PencilArrays
using CUDA
using MPI
using FFTW
using Random
 
MPI.Init()
comm = MPI.COMM_WORLD
dims = (16, 32, 12)
 
pen = Pencil(CuArray, dims, comm)
u = PencilArray{Float32}(undef, pen)
@assert parent(u) isa CuArray
 
plan = PencilFFTPlan(u, Transforms.RFFT())
randn!(u)

uhat = plan * u  # forwards transform
@assert parent(uhat) isa CuArray

v = plan \ uhat  # backwards transform
@assert parent(v) isa CuArray

# Compare with serial FFTW transforms on the CPU
U = gather(u)
Uhat = gather(uhat)
V = gather(v)
if U !== nothing
    @assert U isa Array
    uhat_serial = rfft(U)
    v_serial = irfft(uhat_serial, size(U, 1))
    @show uhat_serial  Uhat
    @show v_serial  V
end

For now this has only been tested on a few CPU processes and single GPU using CUDA. I'm not yet sure if this actually gives correct results, but it's a start. For correct results, this also needs jipolanco/PencilArrays.jl#65, which will be included in PencilArrays v0.17.5.

It would be great if this could be tested on multi-GPU configurations.

Note that an explicit dependency on CUDA.jl is not needed, since functions like plan_rfft(::CuArray, args...) automatically dispatch to CuFFT functions. The only minor issue is that those functions don't support keyword arguments (such as flags = FFTW.MEASURE, which may be passed to FFTW plans), which is one of the reasons why creating plans on CuArrays used to fail before this PR.

This should hopefully close #3.

In particular, `plan_rfft(::CuArray, args...; kws...)` fails when `kws`
is not empty.
Therefore, for GPU arrays, we avoid passing any keyword arguments to
planner functions.
@codecov
Copy link

codecov bot commented Jun 20, 2022

Codecov Report

Merging #48 (439dca3) into master (cbe89dc) will decrease coverage by 0.19%.
The diff coverage is 95.23%.

@@            Coverage Diff             @@
##           master      #48      +/-   ##
==========================================
- Coverage   98.32%   98.13%   -0.20%     
==========================================
  Files           9        9              
  Lines         419      428       +9     
==========================================
+ Hits          412      420       +8     
- Misses          7        8       +1     
Impacted Files Coverage Δ
src/Transforms/Transforms.jl 94.44% <ø> (ø)
src/plans.jl 96.59% <91.66%> (-0.51%) ⬇️
src/Transforms/c2c.jl 100.00% <100.00%> (ø)
src/Transforms/r2c.jl 92.85% <100.00%> (ø)
src/Transforms/r2r.jl 100.00% <100.00%> (ø)
src/operations.jl 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cbe89dc...439dca3. Read the comment docs.

@jipolanco jipolanco changed the title Improve support for CuArray Make transforms work on CUDA arrays Jun 21, 2022
@jipolanco jipolanco merged commit cc8c381 into master Jun 21, 2022
@jipolanco jipolanco deleted the GPUArrays branch June 21, 2022 12:12
@jipolanco jipolanco mentioned this pull request Jun 21, 2022
@Lightup1
Copy link

Lightup1 commented Jun 23, 2022

Hi @jipolanco , I wonder whether it needs CUDA-aware MPI or just normal MPI?

@jipolanco
Copy link
Owner Author

Hi, you definitely need CUDA-aware MPI if you're using CuArrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PencilFFTs on GPUs?
2 participants