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

Add a new factory function : make_fixed_width_column() #3461

Merged
merged 5 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
- PR #3425 Strings column copy_if_else implementation
- PR #3422 Move utilities to legacy
- PR #3201 Define and implement new datetime_ops APIs
- PR #3461 Add a new overload to allocate_like() that takes explicit type and size params.

## Bug Fixes

Expand Down
58 changes: 58 additions & 0 deletions cpp/include/cudf/column/column_factories.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,64 @@ std::unique_ptr<column> make_timestamp_column(
std::forward<B>(null_mask), null_count);
}

/**
* @brief Construct column with sufficient uninitialized storage
* to hold `size` elements of the specified fixed width `data_type` with an optional
* null mask.
*
* @note `null_count()` is determined by the requested null mask `state`
*
* @throws std::bad_alloc if device memory allocation fails
* @throws cudf::logic_error if `type` is not a fixed width type
*
* @param[in] type The desired fixed width type
* @param[in] size The number of elements in the column
* @param[in] state Optional, controls allocation/initialization of the
* column's null mask. By default, no null mask is allocated.
* @param[in] stream Optional stream on which to issue all memory allocation and device
* kernels
* @param[in] mr Optional resource to use for device memory
* allocation of the column's `data` and `null_mask`.
*/
std::unique_ptr<column> make_fixed_width_column(
data_type type, size_type size, mask_state state = UNALLOCATED,
cudaStream_t stream = 0,
rmm::mr::device_memory_resource* mr = rmm::mr::get_default_resource());

/**
* @brief Construct column with sufficient uninitialized storage
* to hold `size` elements of the specified fixed width `data_type` with a
* null mask.
*
* @note null_count is optional and will be computed if not provided.
*
* @throws std::bad_alloc if device memory allocation fails
* @throws cudf::logic_error if `type` is not a fixed width type
*
* @param[in] type The desired fixed width element type
* @param[in] size The number of elements in the column
* @param[in] null_mask Null mask to use for this column.
* @param[in] null_count Optional number of nulls in the null_mask.
* @param[in] stream Optional stream on which to issue all memory allocation and device
* kernels
* @param[in] mr Optional resource to use for device memory
* allocation of the column's `data` and `null_mask`.
*/
template <typename B>
std::unique_ptr<column> make_fixed_width_column(
data_type type, size_type size,
B&& null_mask,
size_type null_count = cudf::UNKNOWN_NULL_COUNT, cudaStream_t stream = 0,
rmm::mr::device_memory_resource* mr = rmm::mr::get_default_resource())
{
CUDF_EXPECTS(is_fixed_width(type), "Invalid, non-fixed-width type.");
if(is_timestamp(type)){
return make_timestamp_column(type, size, std::forward<B>(null_mask), null_count, stream, mr);
}
return make_numeric_column(type, size, std::forward<B>(null_mask), null_count, stream, mr);
}


/**
* @brief Construct STRING type column given a vector of pointer/size pairs.
* The total number of char bytes must not exceed the maximum size of size_type.
Expand Down
1 change: 1 addition & 0 deletions cpp/include/cudf/detail/copy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ std::unique_ptr<column> allocate_like(column_view const& input, size_type size,
rmm::mr::get_default_resource(),
cudaStream_t stream = 0);


/**
* @brief Creates a table of empty columns with the same types as the `input_table`
*
Expand Down
12 changes: 12 additions & 0 deletions cpp/src/column/column_factories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,16 @@ std::unique_ptr<column> make_timestamp_column(
std::vector<std::unique_ptr<column>>{});
}

// Allocate storage for a specified number of fixed width elements
std::unique_ptr<column> make_fixed_width_column(
data_type type, size_type size, mask_state state, cudaStream_t stream,
rmm::mr::device_memory_resource* mr) {
CUDF_EXPECTS(is_fixed_width(type), "Invalid, non-fixed-width type.");

if(is_timestamp(type)){
return make_timestamp_column(type, size, state, stream, mr);
}
return make_numeric_column(type, size, state, stream, mr);
}

} // namespace cudf
142 changes: 141 additions & 1 deletion cpp/tests/column/factories_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,144 @@ TEST_P(NonNumericFactoryTest, NonNumericThrow) {
}

INSTANTIATE_TEST_CASE_P(NonNumeric, NonNumericFactoryTest,
testing::ValuesIn(cudf::test::non_numeric_type_ids));
testing::ValuesIn(cudf::test::non_numeric_type_ids));

template <typename T>
class FixedWidthFactoryTest : public ColumnFactoryTest {};

TYPED_TEST_CASE(FixedWidthFactoryTest, cudf::test::FixedWidthTypes);

TYPED_TEST(FixedWidthFactoryTest, EmptyNoMask) {
auto column = cudf::make_fixed_width_column(
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()}, 0,
cudf::mask_state::UNALLOCATED, this->stream(), this->mr());
EXPECT_EQ(column->type(),
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()});
EXPECT_EQ(column->size(), 0);
EXPECT_EQ(0, column->null_count());
EXPECT_FALSE(column->nullable());
EXPECT_FALSE(column->has_nulls());
EXPECT_EQ(0, column->num_children());
}

TYPED_TEST(FixedWidthFactoryTest, EmptyAllValidMask) {
auto column = cudf::make_fixed_width_column(
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()}, 0,
cudf::mask_state::ALL_VALID, this->stream(), this->mr());
EXPECT_EQ(column->type(),
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()});
EXPECT_EQ(column->size(), 0);
EXPECT_EQ(0, column->null_count());
EXPECT_FALSE(column->nullable());
EXPECT_FALSE(column->has_nulls());
EXPECT_EQ(0, column->num_children());
}

TYPED_TEST(FixedWidthFactoryTest, EmptyAllNullMask) {
auto column = cudf::make_fixed_width_column(
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()}, 0,
cudf::mask_state::ALL_NULL, this->stream(), this->mr());
EXPECT_EQ(column->type(),
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()});
EXPECT_EQ(column->size(), 0);
EXPECT_EQ(0, column->null_count());
EXPECT_FALSE(column->nullable());
EXPECT_FALSE(column->has_nulls());
EXPECT_EQ(0, column->num_children());
}

TYPED_TEST(FixedWidthFactoryTest, NoMask) {
auto column = cudf::make_fixed_width_column(
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()}, this->size(),
cudf::mask_state::UNALLOCATED, this->stream(), this->mr());
EXPECT_EQ(column->type(),
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()});
EXPECT_EQ(column->size(), this->size());
EXPECT_EQ(0, column->null_count());
EXPECT_FALSE(column->nullable());
EXPECT_FALSE(column->has_nulls());
EXPECT_EQ(0, column->num_children());
}

TYPED_TEST(FixedWidthFactoryTest, UnitializedMask) {
auto column = cudf::make_fixed_width_column(
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()}, this->size(),
cudf::mask_state::UNINITIALIZED, this->stream(), this->mr());
EXPECT_EQ(column->type(),
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()});
EXPECT_EQ(column->size(), this->size());
EXPECT_TRUE(column->nullable());
EXPECT_EQ(0, column->num_children());
}

TYPED_TEST(FixedWidthFactoryTest, AllValidMask) {
auto column = cudf::make_fixed_width_column(
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()}, this->size(),
cudf::mask_state::ALL_VALID, this->stream(), this->mr());
EXPECT_EQ(column->type(),
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()});
EXPECT_EQ(column->size(), this->size());
EXPECT_EQ(0, column->null_count());
EXPECT_TRUE(column->nullable());
EXPECT_FALSE(column->has_nulls());
EXPECT_EQ(0, column->num_children());
}

TYPED_TEST(FixedWidthFactoryTest, AllNullMask) {
auto column = cudf::make_fixed_width_column(
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()}, this->size(),
cudf::mask_state::ALL_NULL, this->stream(), this->mr());
EXPECT_EQ(column->type(),
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()});
EXPECT_EQ(column->size(), this->size());
EXPECT_EQ(this->size(), column->null_count());
EXPECT_TRUE(column->nullable());
EXPECT_TRUE(column->has_nulls());
EXPECT_EQ(0, column->num_children());
}

TYPED_TEST(FixedWidthFactoryTest, NullMaskAsParm) {
rmm::device_buffer null_mask{
create_null_mask(this->size(), cudf::mask_state::ALL_NULL)};
auto column = cudf::make_fixed_width_column(
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()}, this->size(),
null_mask, this->size(), this->stream(), this->mr());
EXPECT_EQ(column->type(),
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()});
EXPECT_EQ(column->size(), this->size());
EXPECT_EQ(this->size(), column->null_count());
EXPECT_TRUE(column->nullable());
EXPECT_TRUE(column->has_nulls());
EXPECT_EQ(0, column->num_children());
}

TYPED_TEST(FixedWidthFactoryTest, NullMaskAsEmptyParm) {
rmm::device_buffer null_mask{};
auto column = cudf::make_fixed_width_column(
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()}, this->size(),
null_mask, 0, this->stream(), this->mr());
EXPECT_EQ(column->type(),
cudf::data_type{cudf::experimental::type_to_id<TypeParam>()});
EXPECT_EQ(column->size(), this->size());
EXPECT_EQ(0, column->null_count());
EXPECT_FALSE(column->nullable());
EXPECT_FALSE(column->has_nulls());
EXPECT_EQ(0, column->num_children());
}

class NonFixedWidthFactoryTest
: public ColumnFactoryTest,
public testing::WithParamInterface<cudf::type_id> {};

// All non-fixed types should throw
TEST_P(NonFixedWidthFactoryTest, NonFixedWidthThrow) {
auto construct = [this]() {
auto column = cudf::make_fixed_width_column(
cudf::data_type{GetParam()}, this->size(),
cudf::mask_state::UNALLOCATED, this->stream(), this->mr());
};
EXPECT_THROW(construct(), cudf::logic_error);
}

INSTANTIATE_TEST_CASE_P(NonFixedWidth, NonFixedWidthFactoryTest,
testing::ValuesIn(cudf::test::non_fixed_width_type_ids));
1 change: 0 additions & 1 deletion cpp/tests/copying/utility_tests.cu
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,3 @@ TYPED_TEST(AllocateLikeTest, ColumnNumericTestSpecifiedSize) {
auto got = cudf::experimental::allocate_like(input->view(), specified_size);
cudf::test::expect_column_properties_equal(*expected, *got);
}

13 changes: 12 additions & 1 deletion cpp/tests/utilities/type_lists.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ static constexpr std::array<cudf::type_id, 5> timestamp_type_ids{
* This can be used for iterating over `type_id`s for custom testing, or used in
* GTest value-parameterized tests.
*---------------------------------------------------------------------------**/
static constexpr std::array<cudf::type_id, 9> non_numeric_type_ids{
static constexpr std::array<cudf::type_id, 8> non_numeric_type_ids{
cudf::EMPTY,
cudf::TIMESTAMP_DAYS,
cudf::TIMESTAMP_SECONDS,
Expand All @@ -180,5 +180,16 @@ static constexpr std::array<cudf::type_id, 9> non_numeric_type_ids{
cudf::CATEGORY,
cudf::STRING};

/**---------------------------------------------------------------------------*
* @brief `std::array` of all non-fixed-width `cudf::type_id`s
*
* This can be used for iterating over `type_id`s for custom testing, or used in
* GTest value-parameterized tests.
*---------------------------------------------------------------------------**/
static constexpr std::array<cudf::type_id, 3> non_fixed_width_type_ids{
cudf::EMPTY,
cudf::CATEGORY,
cudf::STRING};

} // namespace test
} // namespace cudf