Skip to content

Flush kernel output when synchronizing a stream that polling finds done - #3363

Closed
maleadt wants to merge 1 commit into
mainfrom
tb/printf-flush
Closed

maleadt wants to merge 1 commit into
mainfrom
tb/printf-flush

Conversation

@maleadt

@maleadt maleadt commented Oct 8, 2026

Copy link
Copy Markdown
Member

Since #3315, synchronize() returns without synchronizing when a query finds the stream done. The driver only flushes the device's printf buffer when synchronizing, not when querying, so the output of short kernels only shows up after a later kernel launch or blocking synchronization:

using CUDA
kernel() = (@cuprintln("hello"); nothing)
@cuda kernel(); synchronize()
out = Pipe()
redirect_stdout(out) do
    @cuda kernel(); synchronize()
end
close(out.in); read(out, String)   # v6.4.1: "hello\n", v6.4.2 and main: ""

This broke cuTile.jl's printing tests, which capture the output of @cuda ...; synchronize(). CUDA.jl's own tests didn't catch it because @grab_output synchronizes the context, which always flushes.

Synchronizing the stream after a successful query would flush, but would also wait for work submitted since, which #3315 avoided (684 ms vs. 0.03 ms when another kernel was queued after the query). Instead, synchronize an event that was never recorded, as check_exceptions already did: that flushes without waiting for any work. Events keep synchronizing themselves after a successful query (#3346), which also flushes.

The event isn't cached, because CUDA 11 drivers can't detect a destroyed context whose handle is reused. On an RTX 5080, synchronize() of an idle stream goes from 0.07 to 0.29 µs, and a launch followed by synchronize() from 4.97 to 5.27 µs. A cached event would save 0.1 µs of that.

Not covered: synchronize(::CuArray) still doesn't flush when the array's last use is known to have completed (in v6.4.1 it synchronized the stream).

The driver only flushes the device's printf buffer when synchronizing,
not when querying for completion. Since #3315, synchronizing a stream that
a query finds done returns without synchronizing, so the output of short
kernels only appeared after a later kernel launch or blocking
synchronization. Synchronizing the stream anyway would wait for work
submitted since, which #3315 avoided, so instead synchronize an event that
was never recorded: that flushes the buffer without waiting for any work.
Exception reporting already did so, and now shares the helper.
@maleadt maleadt added the bugfix label Oct 8, 2026
@maleadt

maleadt commented Oct 8, 2026

Copy link
Copy Markdown
Member Author

Eh, not sure about this.

@maleadt maleadt closed this Oct 8, 2026
@codecov

codecov Bot commented Oct 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.34%. Comparing base (2f34355) to head (e768d39).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3363      +/-   ##
==========================================
- Coverage   87.31%   85.34%   -1.97%     
==========================================
  Files         195      194       -1     
  Lines       20066    19984      -82     
==========================================
- Hits        17521    17056     -465     
- Misses       2545     2928     +383     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

CUDA.jl Benchmarks

Details
Benchmark suite Current: e768d39 Previous: 2f34355 Ratio
array/accumulate/Float32/1d 105912 ns 105820 ns 1.00
array/accumulate/Float32/dims=1 78020 ns 77794 ns 1.00
array/accumulate/Float32/dims=1L 1684559 ns 1675264 ns 1.01
array/accumulate/Float32/dims=2 148817 ns 148368 ns 1.00
array/accumulate/Float32/dims=2L 685784 ns 684464 ns 1.00
array/accumulate/Int64/1d 123990 ns 123400 ns 1.00
array/accumulate/Int64/dims=1 82375 ns 81768 ns 1.01
array/accumulate/Int64/dims=1L 1805420 ns 1798675 ns 1.00
array/accumulate/Int64/dims=2 163670 ns 163560 ns 1.00
array/accumulate/Int64/dims=2L 988832 ns 988728 ns 1.00
array/broadcast 17743 ns 17596 ns 1.01
array/broadcast launch 8368.666666666666 ns 8338.666666666666 ns 1.00
array/construct 906.9459459459459 ns 967.36 ns 0.94
array/copy 17825 ns 17621 ns 1.01
array/copyto!/cpu_to_gpu 208022 ns 207798 ns 1.00
array/copyto!/gpu_to_cpu 241069 ns 239887 ns 1.00
array/copyto!/gpu_to_gpu 9612 ns 9569 ns 1.00
array/iteration/findall/bool 141942 ns 140093 ns 1.01
array/iteration/findall/int 152875 ns 151472 ns 1.01
array/iteration/findfirst/bool 76429 ns 74672 ns 1.02
array/iteration/findfirst/int 78721 ns 77410 ns 1.02
array/iteration/findmin/1d 64188 ns 67569 ns 0.95
array/iteration/findmin/2d 104353 ns 104838 ns 1.00
array/iteration/logical 203373 ns 198787 ns 1.02
array/iteration/scalar 65099 ns 55807 ns 1.17
array/permutedims/2d 50497 ns 50509 ns 1.00
array/permutedims/3d 51977 ns 51420 ns 1.01
array/permutedims/4d 52895 ns 52481 ns 1.01
array/random/rand/Float32 10935 ns 10776 ns 1.01
array/random/rand/Int64 19173 ns 19059 ns 1.01
array/random/rand!/Float32 8217.666666666666 ns 8164 ns 1.01
array/random/rand!/Int64 16686 ns 16660 ns 1.00
array/random/randn/Float32 34773 ns 34551 ns 1.01
array/random/randn!/Float32 24891 ns 24847 ns 1.00
array/reductions/mapreduce/Float32/1d 35406 ns 33391 ns 1.06
array/reductions/mapreduce/Float32/dims=1 41146 ns 40679 ns 1.01
array/reductions/mapreduce/Float32/dims=1L 54404 ns 54218 ns 1.00
array/reductions/mapreduce/Float32/dims=2 60129 ns 59491 ns 1.01
array/reductions/mapreduce/Float32/dims=2L 72131 ns 72017 ns 1.00
array/reductions/mapreduce/Int64/1d 41624 ns 39675 ns 1.05
array/reductions/mapreduce/Int64/dims=1 43520 ns 43359 ns 1.00
array/reductions/mapreduce/Int64/dims=1L 90243 ns 89909 ns 1.00
array/reductions/mapreduce/Int64/dims=2 61195 ns 60860 ns 1.01
array/reductions/mapreduce/Int64/dims=2L 88556 ns 87707 ns 1.01
array/reductions/reduce/Float32/1d 35374 ns 33503 ns 1.06
array/reductions/reduce/Float32/dims=1 40803 ns 40509 ns 1.01
array/reductions/reduce/Float32/dims=1L 53815 ns 53371 ns 1.01
array/reductions/reduce/Float32/dims=2 59896 ns 59505 ns 1.01
array/reductions/reduce/Float32/dims=2L 72328 ns 72117 ns 1.00
array/reductions/reduce/Int64/1d 41637 ns 39620 ns 1.05
array/reductions/reduce/Int64/dims=1 43984 ns 43689 ns 1.01
array/reductions/reduce/Int64/dims=1L 90279 ns 89931 ns 1.00
array/reductions/reduce/Int64/dims=2 61105 ns 60828 ns 1.00
array/reductions/reduce/Int64/dims=2L 88219 ns 87372 ns 1.01
array/reverse/1d 17221 ns 16830 ns 1.02
array/reverse/1dL 71009 ns 70757 ns 1.00
array/reverse/1dL_inplace 68318 ns 68190 ns 1.00
array/reverse/1d_inplace 10098 ns 9767 ns 1.03
array/reverse/2d 21528 ns 21534 ns 1.00
array/reverse/2dL 75746 ns 75376 ns 1.00
array/reverse/2dL_inplace 68468 ns 68399 ns 1.00
array/reverse/2d_inplace 11436 ns 11264 ns 1.02
array/sorting/1d 2845601 ns 2842268 ns 1.00
array/sorting/2d 1090112 ns 1088961 ns 1.00
array/sorting/by 3396672 ns 3394714 ns 1.00
cuda/graph/capture 48735 ns 46949 ns 1.04
cuda/graph/eager 47261 ns 46953 ns 1.01
cuda/graph/launch 15002 ns 14823 ns 1.01
cuda/synchronization/context/auto 8124.75 ns 7993.75 ns 1.02
cuda/synchronization/context/blocking 893.7234042553191 ns 909.9767441860465 ns 0.98
cuda/synchronization/context/nonblocking 8158 ns 7999 ns 1.02
cuda/synchronization/stream/auto 1206.3 ns 348.4562211981567 ns 3.46
cuda/synchronization/stream/blocking 760.0084033613446 ns 767.9469026548672 ns 0.99
cuda/synchronization/stream/nonblocking 7748.666666666667 ns 7882.333333333333 ns 0.98
integration/byval/reference 148894 ns 148584 ns 1.00
integration/byval/slices=1 149782 ns 149648 ns 1.00
integration/byval/slices=2 291999 ns 291810 ns 1.00
integration/byval/slices=3 434910 ns 434504 ns 1.00
integration/cudadevrt 106334 ns 106021 ns 1.00
integration/volumerhs 9207468 ns 9207188 ns 1.00
kernel/indexing 12393 ns 12133 ns 1.02
kernel/indexing_checked 12878 ns 12620 ns 1.02
kernel/launch 2498.5555555555557 ns 2497.1111111111113 ns 1.00
kernel/occupancy 963.3157894736842 ns 939.4166666666666 ns 1.03
kernel/rand 15601 ns 15434 ns 1.01
latency/import 4334723655 ns 4338023747 ns 1.00
latency/precompile 5118293576 ns 5130684551 ns 1.00
latency/ttfp 4845303819 ns 4803836711 ns 1.01

This comment was automatically generated by workflow using github-action-benchmark.

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