packed_data.hpp
1 
5 #pragma once
6 
7 #include <cstdint>
8 #include <memory>
9 #include <vector>
10 
11 #include <rmm/cuda_stream_view.hpp>
12 #include <rmm/device_buffer.hpp>
13 
14 #include <rapidsmpf/buffer/buffer.hpp>
15 #include <rapidsmpf/buffer/resource.hpp>
16 #include <rapidsmpf/error.hpp>
17 
18 namespace rapidsmpf {
19 
26 struct PackedData {
27  std::unique_ptr<std::vector<std::uint8_t>> metadata;
28  std::unique_ptr<Buffer> data;
29 
37  std::unique_ptr<std::vector<std::uint8_t>> metadata, std::unique_ptr<Buffer> data
38  )
39  : metadata{std::move(metadata)}, data{std::move(data)} {
40  RAPIDSMPF_EXPECTS(
41  this->metadata != nullptr, "the metadata pointer cannot be null"
42  );
43  RAPIDSMPF_EXPECTS(this->data != nullptr, "the GPU data pointer cannot be null");
44  RAPIDSMPF_EXPECTS(
45  (this->metadata->size() > 0 || this->data->size == 0),
46  "Empty Metadata and non-empty GPU data is not allowed"
47  );
48  }
49 
50  ~PackedData() = default;
51 
53  PackedData(PackedData&&) = default;
54 
61  PackedData(PackedData const&) = delete;
62  PackedData& operator=(PackedData const&) = delete;
63 
69  [[nodiscard]] bool empty() const {
70  return metadata->empty() && data->size == 0;
71  }
72 
78  [[nodiscard]] rmm::cuda_stream_view stream() const {
79  return data->stream();
80  }
81 
90  PackedData copy(MemoryReservation& reservation) const {
91  auto dst = reservation.br()->allocate(data->size, data->stream(), reservation);
92  buffer_copy(*dst, *data, data->size);
93  return PackedData{
94  std::make_unique<std::vector<std::uint8_t>>(*metadata), std::move(dst)
95  };
96  }
97 };
98 
99 } // namespace rapidsmpf
std::unique_ptr< Buffer > allocate(std::size_t size, rmm::cuda_stream_view stream, MemoryReservation &reservation)
Allocate a buffer of the specified memory type by the reservation.
Represents a reservation for future memory allocation.
Definition: resource.hpp:33
constexpr BufferResource * br() const noexcept
Get the buffer resource associated with this reservation.
Definition: resource.hpp:100
Bag of bytes with metadata suitable for sending over the wire.
Definition: packed_data.hpp:26
rmm::cuda_stream_view stream() const
Get the stream associated with the data buffer.
Definition: packed_data.hpp:78
std::unique_ptr< Buffer > data
The GPU data.
Definition: packed_data.hpp:28
PackedData(std::unique_ptr< std::vector< std::uint8_t >> metadata, std::unique_ptr< Buffer > data)
Construct from metadata and GPU data, taking ownership.
Definition: packed_data.hpp:36
std::unique_ptr< std::vector< std::uint8_t > > metadata
The metadata.
Definition: packed_data.hpp:27
bool empty() const
Check if the packed data is empty.
Definition: packed_data.hpp:69
PackedData copy(MemoryReservation &reservation) const
Create a deep copy of the packed data.
Definition: packed_data.hpp:90
PackedData & operator=(PackedData &&)=default
Move assignment.
PackedData(PackedData &&)=default
PackedData is moveable.