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

fixed_point_value double-shifts in fixed_point construction #6950

Merged
merged 3 commits into from
Dec 9, 2020
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
3 changes: 2 additions & 1 deletion cpp/include/cudf/scalar/scalar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ class fixed_point_scalar : public scalar {
*/
T fixed_point_value(rmm::cuda_stream_view stream = rmm::cuda_stream_default) const
{
return T{_data.value(stream), numeric::scale_type{type().scale()}};
using namespace numeric;
return T{scaled_integer<rep_type>{_data.value(stream), scale_type{type().scale()}}};
}

/**
Expand Down
28 changes: 18 additions & 10 deletions cpp/src/reductions/simple.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,18 @@ std::unique_ptr<scalar> simple_reduction(column_view const& col,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
using Type = device_storage_type_t<ElementType>;
using ResType = device_storage_type_t<ResultType>;

// reduction by iterator
auto dcol = cudf::column_device_view::create(col, stream);
auto simple_op = Op{};

auto result = [&] {
if (col.has_nulls()) {
auto f = simple_op.template get_null_replacing_element_transformer<ResType>();
auto it = thrust::make_transform_iterator(dcol->pair_begin<Type, true>(), f);
auto f = simple_op.template get_null_replacing_element_transformer<ResultType>();
auto it = thrust::make_transform_iterator(dcol->pair_begin<ElementType, true>(), f);
return detail::reduce(it, col.size(), simple_op, stream, mr);
} else {
auto f = simple_op.template get_element_transformer<ResType>();
auto it = thrust::make_transform_iterator(dcol->begin<Type>(), f);
auto f = simple_op.template get_element_transformer<ResultType>();
auto it = thrust::make_transform_iterator(dcol->begin<ElementType>(), f);
return detail::reduce(it, col.size(), simple_op, stream, mr);
}
}();
Expand All @@ -74,12 +71,23 @@ std::unique_ptr<scalar> simple_reduction(column_view const& col,
return result;
}

template <typename ElementType, typename Op>
/**
* @brief Reduction for `sum`, `product`, `min` and `max` for decimal types
*
* @tparam DecimalXX The `decimal32` or `decimal64` type
* @tparam Op The operator of cudf::reduction::op::
* @param col Input column of data to reduce

* @param mr Device memory resource used to allocate the returned scalar's device memory
* @param stream Used for device memory operations and kernel launches.
* @return Output scalar in device memory
*/
template <typename DecimalXX, typename Op>
std::unique_ptr<scalar> fixed_point_reduction(column_view const& col,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
using Type = device_storage_type_t<ElementType>;
using Type = device_storage_type_t<DecimalXX>;

auto dcol = cudf::column_device_view::create(col, stream);
auto simple_op = Op{};
Expand All @@ -100,7 +108,7 @@ std::unique_ptr<scalar> fixed_point_reduction(column_view const& col,
? numeric::scale_type{col.type().scale() * (col.size() - col.null_count())}
: numeric::scale_type{col.type().scale()};
auto const val = static_cast<cudf::scalar_type_t<Type>*>(result.get());
return cudf::make_fixed_point_scalar<ElementType>(val->value(), scale);
return cudf::make_fixed_point_scalar<DecimalXX>(val->value(), scale);
}

/**
Expand Down
23 changes: 7 additions & 16 deletions cpp/tests/reductions/reduction_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ TYPED_TEST(FixedPointTestBothReps, FixedPointReductionProduct)
auto const scale = scale_type{i};
auto const column = fp_wrapper{{1, 2, 3, 1, 2, 3}, scale};
auto const out_type = static_cast<cudf::column_view>(column).type();
auto const expected = decimalXX{36, scale};
auto const expected = decimalXX{scaled_integer<RepType>{36, scale_type{i * 6}}};

auto const result = cudf::reduce(column, cudf::make_product_aggregation(), out_type);
auto const result_scalar = static_cast<cudf::scalar_type_t<decimalXX> *>(result.get());
Expand All @@ -1091,23 +1091,14 @@ TYPED_TEST(FixedPointTestBothReps, FixedPointReductionSum)
for (int i = -3; i <= 0; ++i) {
auto const scale = scale_type{i};

auto const ZERO = decimalXX{0, scale};
auto const ONE = decimalXX{1, scale};
auto const TWO = decimalXX{2, scale};
auto const THREE = decimalXX{3, scale};
auto const FOUR = decimalXX{4, scale};
auto const TEN = decimalXX{10, scale};

auto const in = std::vector<decimalXX>{ONE, TWO, THREE, FOUR};
auto const column = fp_wrapper{{1, 2, 3, 4}, scale};
auto const expected = std::accumulate(in.cbegin(), in.cend(), ZERO, std::plus<decimalXX>());
auto const expected = decimalXX{scaled_integer<RepType>{10, scale}};
auto const out_type = static_cast<cudf::column_view>(column).type();

auto const result = cudf::reduce(column, cudf::make_sum_aggregation(), out_type);
auto const result_scalar = static_cast<cudf::scalar_type_t<decimalXX> *>(result.get());

EXPECT_EQ(result_scalar->fixed_point_value(), expected);
EXPECT_EQ(result_scalar->fixed_point_value(), TEN);
}
}

Expand Down Expand Up @@ -1146,7 +1137,7 @@ TYPED_TEST(FixedPointTestBothReps, FixedPointReductionSumFractional)
auto const scale = scale_type{i};
auto const column = fp_wrapper{{111, 222, 333}, scale};
auto const out_type = static_cast<cudf::column_view>(column).type();
auto const expected = decimalXX{666, scale};
auto const expected = decimalXX{scaled_integer<RepType>{666, scale}};

auto const result = cudf::reduce(column, cudf::make_sum_aggregation(), out_type);
auto const result_scalar = static_cast<cudf::scalar_type_t<decimalXX> *>(result.get());
Expand All @@ -1169,7 +1160,7 @@ TYPED_TEST(FixedPointTestBothReps, FixedPointReductionSumLarge)
auto const column = fp_wrapper{values.cbegin(), values.cend(), scale};
auto const out_type = static_cast<cudf::column_view>(column).type();
auto const expected_value = std::accumulate(values.cbegin(), values.cend(), RepType{0});
auto const expected = decimalXX{expected_value, scale};
auto const expected = decimalXX{scaled_integer<RepType>{expected_value, scale}};

auto const result = cudf::reduce(column, cudf::make_sum_aggregation(), out_type);
auto const result_scalar = static_cast<cudf::scalar_type_t<decimalXX> *>(result.get());
Expand All @@ -1187,7 +1178,7 @@ TYPED_TEST(FixedPointTestBothReps, FixedPointReductionMin)

for (int i = -3; i <= 0; ++i) {
auto const scale = scale_type{i};
auto const ONE = decimalXX{1, scale};
auto const ONE = decimalXX{scaled_integer<RepType>{1, scale}};
auto const column = fp_wrapper{{1, 2, 3, 4}, scale};
auto const out_type = static_cast<cudf::column_view>(column).type();

Expand Down Expand Up @@ -1228,7 +1219,7 @@ TYPED_TEST(FixedPointTestBothReps, FixedPointReductionMax)

for (int i = -3; i <= 0; ++i) {
auto const scale = scale_type{i};
auto const FOUR = decimalXX{4, scale};
auto const FOUR = decimalXX{scaled_integer<RepType>{4, scale}};
auto const column = fp_wrapper{{1, 2, 3, 4}, scale};
auto const out_type = static_cast<cudf::column_view>(column).type();

Expand All @@ -1251,7 +1242,7 @@ TYPED_TEST(FixedPointTestBothReps, FixedPointReductionMaxLarge)
auto f = cudf::test::make_counting_transform_iterator(0, [](auto e) { return e % 43; });
auto const column = fp_wrapper{f, f + 5000, scale};
auto const out_type = static_cast<cudf::column_view>(column).type();
auto const expected = decimalXX{42, scale};
auto const expected = decimalXX{scaled_integer<RepType>{42, scale}};

auto const result = cudf::reduce(column, cudf::make_max_aggregation(), out_type);
auto const result_scalar = static_cast<cudf::scalar_type_t<decimalXX> *>(result.get());
Expand Down