|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +# ################################################################################ |
| 6 | +# |
| 7 | +# This example transfers a buffer from a producer stream to a consumer stream. |
| 8 | +# An event orders the consumer after the producer. The buffer then records the |
| 9 | +# consumer stream for its eventual deallocation. |
| 10 | +# |
| 11 | +# ################################################################################ |
| 12 | + |
| 13 | +# /// script |
| 14 | +# dependencies = ["cuda_bindings", "cuda_core"] |
| 15 | +# /// |
| 16 | + |
| 17 | +import ctypes |
| 18 | + |
| 19 | +from cuda.core import Device, LegacyPinnedMemoryResource |
| 20 | + |
| 21 | + |
| 22 | +def produce_data(device, stream, size, value): |
| 23 | + """Allocate and fill a buffer on the producer stream.""" |
| 24 | + buffer = device.allocate(size, stream=stream) |
| 25 | + buffer.fill(value, stream=stream) |
| 26 | + ready = stream.record() |
| 27 | + return buffer, ready |
| 28 | + |
| 29 | + |
| 30 | +def consume_data(buffer, ready, output, stream): |
| 31 | + """Submit consumer work and transfer the deallocation stream.""" |
| 32 | + stream.wait(ready) |
| 33 | + buffer.set_deallocation_stream(stream) |
| 34 | + buffer.copy_to(output, stream=stream) |
| 35 | + |
| 36 | + |
| 37 | +def main(): |
| 38 | + device = Device() |
| 39 | + device.set_current() |
| 40 | + producer_stream = device.create_stream() |
| 41 | + consumer_stream = device.create_stream() |
| 42 | + pinned_mr = LegacyPinnedMemoryResource() |
| 43 | + |
| 44 | + size = 4096 |
| 45 | + value = 42 |
| 46 | + buffer = None |
| 47 | + ready = None |
| 48 | + output = None |
| 49 | + |
| 50 | + try: |
| 51 | + output = pinned_mr.allocate(size) |
| 52 | + buffer, ready = produce_data(device, producer_stream, size, value) |
| 53 | + consume_data(buffer, ready, output, consumer_stream) |
| 54 | + |
| 55 | + # No stream argument is needed. The buffer now records consumer_stream. |
| 56 | + # The free operation runs after the copy on that stream. |
| 57 | + buffer.close() |
| 58 | + buffer = None |
| 59 | + consumer_stream.sync() |
| 60 | + |
| 61 | + result = ctypes.string_at(int(output.handle), output.size) |
| 62 | + assert result == bytes([value]) * size |
| 63 | + print("Buffer deallocation stream transfer completed.") |
| 64 | + finally: |
| 65 | + if buffer is not None: |
| 66 | + buffer.close() |
| 67 | + if output is not None: |
| 68 | + output.close() |
| 69 | + if ready is not None: |
| 70 | + ready.close() |
| 71 | + consumer_stream.close() |
| 72 | + producer_stream.close() |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + main() |
0 commit comments