Skip to content

Commit

Permalink
Cleanup hostdevice_vector and add more APIs (#15252)
Browse files Browse the repository at this point in the history
This work includes:
 * Fix a bug in `hostdevice_vector` when the host buffer does not change its size when appending new elements. Instead, the new elements are written directly into raw memory (which is out of bounds). Previously, this did not trigger any issue since the host buffer has reserved plenty of memory upon its construction, until I attempted to access the `front()` and `back()` elements of it.
 * Add `front()` and `back()` accessors which return the first and last elements in the host buffer.

Authors:
  - Nghia Truong (https://github.com/ttnghia)

Approvers:
  - https://github.com/nvdbaranec
  - Vukasin Milovanovic (https://github.com/vuule)

URL: #15252
  • Loading branch information
ttnghia committed Mar 11, 2024
1 parent e2fcf12 commit 8ed3e20
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions cpp/src/io/utilities/hostdevice_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@
#include <cudf/utilities/span.hpp>

#include <rmm/cuda_stream_view.hpp>
#include <rmm/device_buffer.hpp>
#include <rmm/device_uvector.hpp>
#include <rmm/mr/host/host_memory_resource.hpp>

#include <thrust/host_vector.h>

#include <variant>

namespace cudf::detail {

/**
Expand All @@ -57,26 +53,23 @@ class hostdevice_vector {
}

explicit hostdevice_vector(size_t initial_size, size_t max_size, rmm::cuda_stream_view stream)
: h_data({cudf::io::get_host_memory_resource(), stream}), d_data(0, stream)
: h_data({cudf::io::get_host_memory_resource(), stream}), d_data(max_size, stream)
{
CUDF_EXPECTS(initial_size <= max_size, "initial_size cannot be larger than max_size");

h_data.reserve(max_size);
h_data.resize(initial_size);

current_size = initial_size;
d_data.resize(max_size, stream);
}

void push_back(T const& data)
{
CUDF_EXPECTS(size() < capacity(),
"Cannot insert data into hostdevice_vector because capacity has been exceeded.");
h_data[current_size++] = data;
h_data.push_back(data);
}

[[nodiscard]] size_t capacity() const noexcept { return d_data.size(); }
[[nodiscard]] size_t size() const noexcept { return current_size; }
[[nodiscard]] size_t size() const noexcept { return h_data.size(); }
[[nodiscard]] size_t size_bytes() const noexcept { return sizeof(T) * size(); }
[[nodiscard]] bool empty() const noexcept { return size() == 0; }

Expand All @@ -92,6 +85,12 @@ class hostdevice_vector {
[[nodiscard]] T* end() { return host_ptr(size()); }
[[nodiscard]] T const* end() const { return host_ptr(size()); }

[[nodiscard]] T& front() { return h_data.front(); }
[[nodiscard]] T const& front() const { return front(); }

[[nodiscard]] T& back() { return h_data.back(); }
[[nodiscard]] T const& back() const { return back(); }

[[nodiscard]] T* device_ptr(size_t offset = 0) { return d_data.data() + offset; }
[[nodiscard]] T const* device_ptr(size_t offset = 0) const { return d_data.data() + offset; }

Expand Down Expand Up @@ -175,7 +174,6 @@ class hostdevice_vector {

private:
cudf::detail::rmm_host_vector<T> h_data;
size_t current_size = 0;
rmm::device_uvector<T> d_data;
};

Expand Down

0 comments on commit 8ed3e20

Please sign in to comment.