Repository navigation
[MINOR] Reject invalid CUDA streams in buffer factories #1263
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
Changes from all commits
7f20e80
b9fcf58
f499a45
2f5a09a
86798db
aacb463
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,12 +15,14 @@ | |
|
|
||
| #include <cuda/stream> | ||
|
|
||
| #include <rmm/cuda_stream.hpp> | ||
| #include <rmm/device_buffer.hpp> | ||
| #include <rmm/mr/per_device_resource.hpp> | ||
|
|
||
| #include <rapidsmpf/config.hpp> | ||
| #include <rapidsmpf/config_defaults.hpp> | ||
| #include <rapidsmpf/cuda_stream.hpp> | ||
| #include <rapidsmpf/error.hpp> | ||
| #include <rapidsmpf/memory/buffer_resource.hpp> | ||
| #include <rapidsmpf/memory/pinned_memory_resource.hpp> | ||
| #include <rapidsmpf/system_info.hpp> | ||
|
|
@@ -269,6 +271,79 @@ TEST(PinnedResource, transient_mr) { | |
| EXPECT_NO_THROW(test_buffer(std::move(buffer), source_data)); | ||
| } | ||
|
|
||
| TEST(PinnedResource, from_rmm_device_buffer_stream_ordering) { | ||
| if (!rapidsmpf::is_pinned_memory_resources_supported()) { | ||
| GTEST_SKIP() << "PinnedMemoryResource is not supported"; | ||
| } | ||
| auto br = rapidsmpf::BufferResource::create( | ||
| rmm::mr::get_current_device_resource_ref(), rapidsmpf::PinnedPoolProperties{} | ||
| ); | ||
| auto mr = br->try_pinned_mr(); | ||
| rmm::cuda_stream stream_a; | ||
| rmm::cuda_stream stream_b; | ||
| rmm::cuda_stream stream_c; | ||
| constexpr std::size_t size = 1 << 20; | ||
|
|
||
| // Fill the pinned buffer asynchronously on stream A, without synchronizing. | ||
| auto pinned_host_buffer = std::make_unique<rmm::device_buffer>(size, stream_a, *mr); | ||
| RAPIDSMPF_CUDA_TRY( | ||
| cudaMemsetAsync(pinned_host_buffer->data(), 0xAB, size, stream_a.value()) | ||
| ); | ||
|
|
||
| // The new stream B must be ordered after the fill on A. | ||
| auto buffer = rapidsmpf::HostBuffer::from_rmm_device_buffer( | ||
| std::move(pinned_host_buffer), stream_b | ||
| ); | ||
| EXPECT_EQ(buffer.stream().get(), stream_b.value()); | ||
| EXPECT_EQ(buffer.copy_to_uint8_vector(), std::vector<std::uint8_t>(size, 0xAB)); | ||
|
|
||
| buffer.set_stream(stream_c); // Switch to stream C; the memory must be freed on C. | ||
| EXPECT_EQ(buffer.stream().get(), stream_c.value()); | ||
| buffer.deallocate_async(); | ||
| EXPECT_TRUE(buffer.empty()); | ||
| stream_c.synchronize(); | ||
| } | ||
|
|
||
| TEST(HostBuffer, invalid_stream_throws) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add an invalid-stream
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the checks. Two things from the earlier comment are still open:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think 1 is already there. I will add a test to 2. That is a good point. It was a bug uncovered during this invalid stream changes |
||
| cuda::stream_ref const valid{cudaStreamLegacy}; | ||
| cuda::stream_ref const invalid{cuda::invalid_stream}; | ||
| std::vector<std::uint8_t> const data(8); | ||
|
|
||
| EXPECT_THROW( | ||
| std::ignore = rapidsmpf::HostBuffer::from_owned_vector( | ||
| std::vector<std::uint8_t>(data), invalid | ||
| ), | ||
| std::invalid_argument | ||
| ); | ||
| auto br = rapidsmpf::BufferResource::create( | ||
| rmm::mr::get_current_device_resource_ref(), | ||
| rapidsmpf::is_pinned_memory_resources_supported() | ||
| ? std::make_optional<rapidsmpf::PinnedPoolProperties>() | ||
| : std::nullopt | ||
| ); | ||
| EXPECT_THROW( | ||
| std::ignore = | ||
| rapidsmpf::HostBuffer::from_uint8_vector(data, invalid, br->host_mr()), | ||
| std::invalid_argument | ||
| ); | ||
| EXPECT_THROW( | ||
| rapidsmpf::HostBuffer(data.size(), invalid, br->host_mr()), std::invalid_argument | ||
| ); | ||
|
|
||
| auto buffer = | ||
| rapidsmpf::HostBuffer::from_owned_vector(std::vector<std::uint8_t>(data), valid); | ||
| EXPECT_THROW(buffer.set_stream(invalid), std::invalid_argument); | ||
|
|
||
| if (auto pinned_mr = br->try_pinned_mr()) { | ||
| EXPECT_THROW( | ||
| std::ignore = rapidsmpf::HostBuffer::from_rmm_device_buffer( | ||
| std::make_unique<rmm::device_buffer>(8, valid, *pinned_mr), invalid | ||
| ), | ||
| std::invalid_argument | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| namespace { | ||
|
|
||
| /// Discover the actual pool size the driver creates when a small max is requested. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the
from_rmm_device_bufferdeallocator now frees memory on theHostBuffer's current stream,HostBuffer::set_stream(cuda::invalid_stream)leads to freeing on an invalid stream inside thenoexceptdestructor. I thinkHostBuffer::set_streamshould get the samestd::invalid_argument.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done