Use pinned host memory for GPU state-to-NumPy conversion (#2797)#4290
Open
mitchdz wants to merge 6 commits intoNVIDIA:mainfrom
Open
Use pinned host memory for GPU state-to-NumPy conversion (#2797)#4290mitchdz wants to merge 6 commits intoNVIDIA:mainfrom
mitchdz wants to merge 6 commits intoNVIDIA:mainfrom
Conversation
- Fix performance bottleneck in `np.array(cudaq.get_state(kernel))` for GPU-backed states by using pinned (`cudaMallocHost`) host memory instead of pageable (`new[]`) for the device-to-host transfer in the pybind11 buffer protocol handler - Add `SimulationState::toHostBuffer()` virtual method so GPU backends can provide optimized host allocation, with a pageable fallback for non-GPU backends and for systems that cannot pin the requested amount of memory - Fix `to_cupy()` passing a hardcoded 1024-byte size to `UnownedMemory` instead of the actual buffer size Signed-off-by: mdzurick <mitch_dz@hotmail.com>
Collaborator
Author
|
Re-opening as accidentally closed last commit (my bad). Also, I am not merging this in on purpose in order to do some more proper testing for larger qubit counts. My concern is this new alloc/dealloc may actually cause more overhead. |
|
CUDA Quantum Docs Bot: A preview of the documentation can be found here. |
|
CUDA Quantum Docs Bot: A preview of the documentation can be found here. |
Collaborator
Author
|
I am blocking this for now. Testing locally on an L4 I do see a regression at higher qubit counts. I did a comparison between main and the new pinned memory logic. |
Signed-off-by: mdzurick <mitch_dz@hotmail.com>
|
CUDA Quantum Docs Bot: A preview of the documentation can be found here. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
np.array(cudaq.get_state(kernel))for GPU-backedstates by using pinned (
cudaMallocHost) host memory instead of pageable (new[])for the device-to-host transfer in the pybind11 buffer protocol handler
SimulationState::toHostBuffer()virtual method so GPU backends can provideoptimized host allocation, with a pageable fallback for non-GPU backends and for
systems that cannot pin the requested amount of memory
to_cupy()passing a hardcoded 1024-byte size toUnownedMemoryinstead ofthe actual buffer size
Motivation
Closes #2797. When
np.array()is called on a GPU-resident state, the bufferprotocol handler allocates pageable host memory and copies via
cudaMemcpy. Withpageable destinations, CUDA must stage through an internal pinned buffer in chunks,
severely limiting effective bandwidth. On an L4 GPU with 25 qubits (537 MB state):
For the original reporter's 32-qubit case on GH200, the ~5.4s overhead should drop
to under 1s.