Skip to content

Support multi-GPU cupy.cuda.cufft.Plan1d - #2644

Merged
asi1024 merged 41 commits into
cupy:masterfrom
leofang:cufft_mgpu
Feb 27, 2020
Merged

asi1024 merged 41 commits into
cupy:masterfrom
leofang:cufft_mgpu

Conversation

@leofang

@leofang leofang commented Nov 15, 2019 •

Copy link
Copy Markdown
Member

--- PR description updated, please read ---

This PR adds initial support for multi-GPU cuFFT. I only target Plan1d here because it's easier to test and to understand the corner cases. This PR makes the necessary preparation for future extension. For example, I will support PlanNd in the next PR.

In short, this PR allows multi-GPU cuFFT plans to be used with both high- and low-level API calls:

import cupy as cp


a = cp.random.random(1024).astype(cp.complex64)
a += 1j*cp.random.random(1024)

cp.fft.config.use_multi_gpus = True  # flag added in this PR
cp.fft.config.set_cufft_gpus(2)      # handle added in this PR
out = cp.fft.fft(a)                  # this just works
import cupy as cp


batch = 7
length = 512

# use 2 GPUs
a = cp.random.random((batch, length)) +1j*cp.random.random((batch, length))
a = a.astype(cp.complex64)
plan = cp.cuda.cufft.Plan1d(length, cp.cuda.cufft.CUFFT_C2C, batch, devices=2)
out = plan.get_output_array(a)
plan.fft(a, out, cp.cuda.cufft.CUFFT_FORWARD)

# use 1 GPU
out2 = cp.fft.fft(a)

# results should be identical 
assert cp.allclose(out, out2)

The second example is nice because Plan1d can actually be retrieved by cupyx.scipy.fftpack.get_fft_plan().

Multi-GPU cuFFT are beneficial for at least two reasons:

  1. Bypassing memory limitation: every GPU has limited memory, and even worse in order to run cupy.fft.fft(), for example, it would need additional buffer space. So it turns out one cannot even FFT a 8G array on a P100 with 16G memory. Having extra workhorses would alleviate this limitation.
  2. Reasonable performance: while it's not guaranteed to be faster using multiple GPUs, for users who have NVLink interconnects, the bandwidth is large enough that performance is acceptable for a huge array. It seems that NVLinks can support more simultaneous async transfers than allowed by the engine counts (which shows up in nvprof/nvvp as Memcpy PtoP --- on a DGX-2 I found overlapped memcpy among 16 GPUs is possible with this PR!)

Under the hood, the cupy.cuda.cufft module manages all nuisances. In particular, these two cuFFT APIs are not used: cufftXtMalloc() and cufftXtFree(). This decision is made because

  1. we don't want memory allocation outside of CuPy memory pool
  2. we want to reuse the buffers when the plan is pre-allocated (as shown above) and reused

So some efforts are invested to ensure this really works. This approach is encouraged by NVIDIA architect @maxpkatz, who is very kind to offer help ❤️ This PR would have been much more difficult without his support! Thank you (again), Max!

(Disclaimer: Max did not review nor approve this PR. He only provided technical discussion and consultation.)

Also note that when using Plan1d.fft() directly, passing NumPy arrays are allowed. I prefer to keep this feature undocumented and polish it later (likely in the next PR, during which I will need to re-evaluate this feature again for 2D/3D FFTs). Is it OK?
UPDATE: this functionality was suggested to be temporarily disabled in 3e2b7d0 and 705fe1a.
UPDATE: for performance benchmark see #2644 (comment).

TODO:

  • more local tests and polishes
  • avoid cufftXtMalloc and cufftXtFree
  • make it work with high-level API (cupy.fft.fft())
  • write test suites and docs
  • fix read the docs

TODO:
1. check the validity of input arguments
2. handle batch = 1

This passes:

>>> import cupy as cp
>>>
>>> a = cp.random.random((2, 512)) +1j*cp.random.random((2, 512))
>>> a = a.astype(cp.complex64)
>>> plan = cp.cuda.cufft.Plan1d(512, cp.cuda.cufft.CUFFT_C2C, 2, use_multi_gpus=True, devices=2)
>>> out = plan.get_output_array(a)
>>> plan.fft(a, out, cp.cuda.cufft.CUFFT_FORWARD)
>>> out2 = cp.fft.fft(a)
>>> assert cp.allclose(out2, out)
>>> import cupy as cp
>>>
>>>
>>> a = cp.random.random(1024).astype(cp.complex64)
>>> a += 1j*cp.random.random(1024)
>>>
>>> cp.fft.config.use_multi_gpus = True
>>> cp.fft.config.set_cufft_gpus(2)
>>> out = cp.fft.fft(a)
>>> print(out.data.ptr)
>>> print(out)
>>>
>>> with cp.cuda.Device(0):  # must use this line to avoid cudaErrorIllegalAddress bug, why?
>>>     cp.fft.config.use_multi_gpus = False
>>>     cp.fft.config._devices = None
>>>     out2 = cp.fft.fft(a)
>>>     print(out2.data.ptr)
>>>     print(out2)
>>>
>>> print(cp.allclose(out, out2, rtol=5E-4))
Comment thread cupy/fft/fft.py Resolved
@leofang

leofang commented Nov 19, 2019 •

Copy link
Copy Markdown
Member Author

@asi1024 If the team is interested in this multi-GPU capability, I’ll start writing some tests. UPDATE: done. Docs could wait for another PR — this PR lays the groundwork for supporting multi-GPU PlanNd, which is my actual interest 🙂

@leofang

leofang commented Nov 21, 2019 •

Copy link
Copy Markdown
Member Author

@asi1024 I took some time on writing tests. The main PR description is also fully rewritten. PTAL. Thanks!

@leofang
leofang marked this pull request as ready for review November 21, 2019 04:01
@leofang

leofang commented Dec 13, 2019

Copy link
Copy Markdown
Member Author

@asi1024 Could you address my questions above? I am a bit losing track of your expectations here...Thanks.

Comment thread tests/cupy_tests/fft_tests/test_fft.py Resolved
@leofang

leofang commented Dec 16, 2019 •

Copy link
Copy Markdown
Member Author

@asi1024 I think this is relevant to the discussion in #2743: If you want to expose only the low-level Plan1d object for using multi GPUs, and you want to remove all the changes I made to cupy/fft/config.py here, then we've got one more reason to make cupyx.scipy.fftpack.get_fft_plan() a stable API so that it can be used to return a multi-GPU plan.

@leofang

leofang commented Jan 27, 2020 •

Copy link
Copy Markdown
Member Author

@asi1024 Any chance you have time to address #2644 (comment) and #2644 (comment)?

Another thought: If we are wiping out the high-level APIs for turning on multi-GPU as you suggested, is it then OK to add a multi_gpu=False argument to cupyx.scipy.fftpack.get_fft_plan() so that it can return a multi-GPU plan if needed? I still don't think it's a good idea to expose the low-level cupy.cuda.fft interface, which was considered private API afaik.

Thanks.

@asi1024 asi1024 added this to the v8.0.0b1 milestone Feb 12, 2020
1. mark cupy.fft.config.set_cufft_gpus experimental
2. remove use_multi_gpus from Plan1d's constructor
3. fix flake8
In this commit, the code is simply commented out instead of being removed.
@leofang

leofang commented Feb 17, 2020

Copy link
Copy Markdown
Member Author

I hope I addressed all of your comments, @asi1024. PTAL.

@asi1024

asi1024 commented Feb 27, 2020

Copy link
Copy Markdown
Member

Jenkins, test this please.

@pfn-ci-bot

Copy link
Copy Markdown
Collaborator

Successfully created a job for commit 7ac66b3:

@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 7ac66b3, target branch master) succeeded!

@asi1024

asi1024 commented Feb 27, 2020

Copy link
Copy Markdown
Member

LGTM.

@asi1024
asi1024 merged commit 9cfbed5 into cupy:master Feb 27, 2020
@leofang
leofang deleted the cufft_mgpu branch February 28, 2020 00:22
@leofang

leofang commented Feb 28, 2020

Copy link
Copy Markdown
Member Author

Thanks a lot, @asi1024! I think the scatter/gather part might have room for optimization. Will look into that and create a follow up PR.

@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 7ac66b3, target branch master) succeeded!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:feature New features/APIs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants