diff --git a/cpp/include/rapidsmpf/memory/buffer_resource.hpp b/cpp/include/rapidsmpf/memory/buffer_resource.hpp index 5da70c216..d49b1f5f2 100644 --- a/cpp/include/rapidsmpf/memory/buffer_resource.hpp +++ b/cpp/include/rapidsmpf/memory/buffer_resource.hpp @@ -516,6 +516,10 @@ class BufferResource : public std::enable_shared_from_this { * - @p stream is synchronized with the device buffer's current stream, and * - the device buffer's current stream is updated to @p stream. * + * If @p data is empty and has no stream (e.g. a default-constructed + * `rmm::device_buffer`), no synchronization is performed and its stream is set to + * @p stream. + * * @param data Unique pointer to the device or pinned host buffer. * @param stream CUDA stream associated with the new Buffer. Use or synchronize with * this stream when operating on the Buffer. diff --git a/cpp/src/memory/buffer_resource.cpp b/cpp/src/memory/buffer_resource.cpp index bc474fbba..42bc75a08 100644 --- a/cpp/src/memory/buffer_resource.cpp +++ b/cpp/src/memory/buffer_resource.cpp @@ -335,7 +335,12 @@ std::unique_ptr BufferResource::move( ); cuda::stream_ref upstream = data->stream(); if (upstream.get() != stream.get()) { - cuda_stream_join(stream, upstream); + // Default-constructed RMM buffers have no stream or work to join. + bool const empty_without_stream = + data->capacity() == 0 && upstream == cuda::invalid_stream; + if (!empty_without_stream) { + cuda_stream_join(stream, upstream); + } data->set_stream(stream); } diff --git a/cpp/tests/test_buffer_resource.cpp b/cpp/tests/test_buffer_resource.cpp index 7f6b8a43f..dca11f142 100644 --- a/cpp/tests/test_buffer_resource.cpp +++ b/cpp/tests/test_buffer_resource.cpp @@ -67,6 +67,18 @@ std::int64_t reserved_bytes(BufferResource const& br, MemoryType mem_type) { return br.memory_available(mem_type) - br.memory_available_for_reservation(mem_type); } +TEST(BufferResource, MoveDefaultConstructedDeviceBuffer) { + StreamPool stream_pool{1}; + auto stream = stream_pool.get_stream(); + auto br = BufferResource::create(rmm::mr::get_current_device_resource_ref()); + + auto buffer = br->move(std::make_unique(), stream); + + EXPECT_EQ(buffer->size, 0U); + EXPECT_EQ(buffer->stream().get(), stream.get()); + EXPECT_NO_THROW(buffer->latest_write_event().host_wait()); +} + TEST(BufferResource, ReservationOverbooking) { // Create a buffer resource that always reports 10 KiB of available device memory. auto br = BufferResource::create(