Skip to content

Commit

Permalink
Add column constructor from device_uvector&& (#11356)
Browse files Browse the repository at this point in the history
Closes #11115 

This PR adds a `column` constructor to be constructible from a `device_uvector&&` using move semantics.

Authors:
  - Srikar Vanavasam (https://github.com/SrikarVanavasam)

Approvers:
  - Bradley Dice (https://github.com/bdice)
  - Nghia Truong (https://github.com/ttnghia)
  - Jake Hemstad (https://github.com/jrhemstad)

URL: #11356
  • Loading branch information
SrikarVanavasam authored Aug 3, 2022
1 parent 554fb10 commit d86bb39
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cpp/include/cudf/column/column.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <rmm/cuda_stream_view.hpp>
#include <rmm/device_buffer.hpp>
#include <rmm/device_uvector.hpp>
#include <rmm/mr/device/per_device_resource.hpp>

#include <memory>
Expand Down Expand Up @@ -75,6 +76,33 @@ class column {
*/
column(column&& other) noexcept;

/**
* @brief Construct a new column by taking ownership of the contents of a device_uvector.
*
* @param other The device_uvector whose contents will be moved into the new column.
* @param null_mask Optional, column's null value indicator bitmask. May
* be empty if `null_count` is 0 or `UNKNOWN_NULL_COUNT`.
* @param null_count Optional, the count of null elements. If unknown, specify
* `UNKNOWN_NULL_COUNT` to indicate that the null count should be computed on
* the first invocation of `null_count()`.
*/
template <typename T, CUDF_ENABLE_IF(cudf::is_numeric<T>() or cudf::is_chrono<T>())>
column(rmm::device_uvector<T>&& other,
rmm::device_buffer&& null_mask = {},
size_type null_count = UNKNOWN_NULL_COUNT)
: _type{cudf::data_type{cudf::type_to_id<T>()}},
_size{[&]() {
CUDF_EXPECTS(
other.size() <= static_cast<std::size_t>(std::numeric_limits<size_type>::max()),
"The device_uvector size exceeds the maximum size_type.");
return static_cast<size_type>(other.size());
}()},
_data{other.release()},
_null_mask{std::move(null_mask)},
_null_count{null_count}
{
}

/**
* @brief Construct a new column from existing device memory.
*
Expand Down
36 changes: 36 additions & 0 deletions cpp/tests/column/column_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,42 @@ TYPED_TEST(TypedColumnTest, MoveConstructorWithMask)
EXPECT_EQ(original_mask, moved_to_view.null_mask());
}

TYPED_TEST(TypedColumnTest, DeviceUvectorConstructorNoMask)
{
rmm::device_uvector<TypeParam> original{static_cast<std::size_t>(this->num_elements()),
cudf::default_stream_value};
thrust::copy(thrust::device,
static_cast<TypeParam*>(this->data.data()),
static_cast<TypeParam*>(this->data.data()) + this->num_elements(),
original.begin());
auto original_data = original.data();
cudf::column moved_to{std::move(original)};
verify_column_views(moved_to);

// Verify move
cudf::column_view moved_to_view = moved_to;
EXPECT_EQ(original_data, moved_to_view.head());
}

TYPED_TEST(TypedColumnTest, DeviceUvectorConstructorWithMask)
{
rmm::device_uvector<TypeParam> original{static_cast<std::size_t>(this->num_elements()),
cudf::default_stream_value};
thrust::copy(thrust::device,
static_cast<TypeParam*>(this->data.data()),
static_cast<TypeParam*>(this->data.data()) + this->num_elements(),
original.begin());
auto original_data = original.data();
auto original_mask = this->all_valid_mask.data();
cudf::column moved_to{std::move(original), std::move(this->all_valid_mask)};
verify_column_views(moved_to);

// Verify move
cudf::column_view moved_to_view = moved_to;
EXPECT_EQ(original_data, moved_to_view.head());
EXPECT_EQ(original_mask, moved_to_view.null_mask());
}

TYPED_TEST(TypedColumnTest, ConstructWithChildren)
{
std::vector<std::unique_ptr<cudf::column>> children;
Expand Down

0 comments on commit d86bb39

Please sign in to comment.