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

Remove eager synchronization with HtoD copies. #2625

Merged
merged 1 commit into from
Jan 17, 2025
Merged

Conversation

maleadt
Copy link
Member

@maleadt maleadt commented Jan 17, 2025

We assumed unpinned memory would always synchronize, but that does not seem to be the case. For some copy sizes (and potentially on some, e.g. coherent, memory architectures) the copy is fully asynchronous.

This optimization was made to make CuRef of a scalar fully async. I considered making the CuRef ctor call memset instead, which is always asynchronous by virtue of passing the memory by value, however that does not support 64-bits floats while memcpy of 64 bits is still executed fully asynchronously.

Demo script:

using CUDA, NVTX

function main()
    function doit()
        A = NVTX.@range "rand" CUDA.rand(4096, 4096)
        B = NVTX.@range "mul" A*A
        c = NVTX.@range "ref" CuRef{Float64}(1)
        synchronize()
    end

    NVTX.@range "run 1" doit()
    NVTX.@range "run 2" doit()
end

Before:

image

After:

image

We assumed unpinned memory would always synchronize, but that does
not seem to be the case. For some copy sizes (and potentially on
some, e.g. coherent, memory architectures) the copy is fully
asynchronous.

This optimization was made to make `CuRef` of a scalar fully async.
I considered making the `CuRef` ctor call `memset` instead, which
is always asynchronous by virtue of passing the memory by value,
however that does not support 64-bits floats while `memcpy` of
64 bits is still executed fully asynchronously.
Comment on lines +530 to +532
# the copy below may block in `libcuda`, so it'd be good to perform a nonblocking
# synchronization here, but the exact cases are hard to know and detect (e.g., unpinned
# memory normally blocks, but not for all sizes, and not on all memory architectures).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# the copy below may block in `libcuda`, so it'd be good to perform a nonblocking
# synchronization here, but the exact cases are hard to know and detect (e.g., unpinned
# memory normally blocks, but not for all sizes, and not on all memory architectures).
# the copy below may block in `libcuda`, so it'd be good to perform a nonblocking
# synchronization here, but the exact cases are hard to know and detect (e.g., unpinned
# memory normally blocks, but not for all sizes, and not on all memory architectures).

is_pinned(pointer(dest)) || synchronize()
end

# the copy below may block in `libcuda`; see the note above.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# the copy below may block in `libcuda`; see the note above.
# the copy below may block in `libcuda`; see the note above.

Copy link

codecov bot commented Jan 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.59%. Comparing base (d07a245) to head (9346e4f).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2625      +/-   ##
==========================================
- Coverage   73.60%   73.59%   -0.01%     
==========================================
  Files         157      157              
  Lines       15230    15226       -4     
==========================================
- Hits        11210    11206       -4     
  Misses       4020     4020              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@maleadt maleadt merged commit 3d45d85 into master Jan 17, 2025
3 checks passed
@maleadt maleadt deleted the tb/async_mempcy branch January 17, 2025 20:09
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.

1 participant