Skip to content
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

Cleanup hostdevice_vector and add more APIs #15252

Merged
merged 17 commits into from
Mar 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading