-
Notifications
You must be signed in to change notification settings - Fork 912
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
Deduplicate decimal32/decimal64 to decimal128 conversion function #16236
Changes from all commits
7e6532f
d454a70
f665fd7
133be69
6c62b6a
6be8e12
cf962aa
08cbad7
98a336d
c83fd2c
9070c6e
a0d2d1e
4affd15
e705b8e
0693d79
38bb635
6b20e32
5cd053c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2024, NVIDIA CORPORATION. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "decimal_conversion_utilities.cuh" | ||
|
||
#include <cudf/detail/utilities/integer_utils.hpp> | ||
#include <cudf/detail/utilities/linked_column.hpp> | ||
#include <cudf/fixed_point/fixed_point.hpp> | ||
|
||
#include <rmm/exec_policy.hpp> | ||
|
||
#include <thrust/for_each.h> | ||
|
||
#include <type_traits> | ||
|
||
namespace cudf { | ||
namespace detail { | ||
|
||
template <typename DecimalType> | ||
std::unique_ptr<rmm::device_buffer> convert_decimals_to_decimal128( | ||
cudf::column_view const& column, rmm::cuda_stream_view stream, rmm::device_async_resource_ref mr) | ||
{ | ||
static_assert(std::is_same_v<DecimalType, int32_t> or std::is_same_v<DecimalType, int64_t>, | ||
mhaseeb123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Only int32 and int64 decimal types can be converted to decimal128."); | ||
|
||
constexpr size_type BIT_WIDTH_RATIO = sizeof(__int128_t) / sizeof(DecimalType); | ||
auto buf = std::make_unique<rmm::device_buffer>(column.size() * sizeof(__int128_t), stream, mr); | ||
|
||
thrust::for_each(rmm::exec_policy_nosync(stream, mr), | ||
thrust::make_counting_iterator(0), | ||
thrust::make_counting_iterator(column.size()), | ||
[in = column.begin<DecimalType>(), | ||
out = reinterpret_cast<DecimalType*>(buf->data()), | ||
BIT_WIDTH_RATIO] __device__(auto in_idx) { | ||
auto const out_idx = in_idx * BIT_WIDTH_RATIO; | ||
// the lowest order bits are the value, the remainder | ||
// simply matches the sign bit to satisfy the two's | ||
// complement integer representation of negative numbers. | ||
out[out_idx] = in[in_idx]; | ||
#pragma unroll BIT_WIDTH_RATIO - 1 | ||
for (auto i = 1; i < BIT_WIDTH_RATIO; ++i) { | ||
out[out_idx + i] = in[in_idx] < 0 ? -1 : 0; | ||
} | ||
}); | ||
|
||
return buf; | ||
mhaseeb123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Instantiate templates for int32_t and int64_t decimal types | ||
template std::unique_ptr<rmm::device_buffer> convert_decimals_to_decimal128<int32_t>( | ||
cudf::column_view const& column, rmm::cuda_stream_view stream, rmm::device_async_resource_ref mr); | ||
|
||
template std::unique_ptr<rmm::device_buffer> convert_decimals_to_decimal128<int64_t>( | ||
cudf::column_view const& column, rmm::cuda_stream_view stream, rmm::device_async_resource_ref mr); | ||
|
||
} // namespace detail | ||
} // namespace cudf |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2024, NVIDIA CORPORATION. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <cudf/column/column_view.hpp> | ||
#include <cudf/types.hpp> | ||
|
||
#include <rmm/cuda_stream_view.hpp> | ||
#include <rmm/resource_ref.hpp> | ||
|
||
#include <type_traits> | ||
|
||
namespace cudf::detail { | ||
|
||
/** | ||
* @brief Convert decimal32 and decimal64 numeric data to decimal128 and return the device vector | ||
* | ||
* @tparam DecimalType to convert from | ||
* | ||
* @param column A view of the input columns | ||
* @param stream CUDA stream used for device memory operations and kernel launches | ||
* @param mr Device memory resource to use for device memory allocation | ||
* | ||
* @return A device vector containing the converted decimal128 data | ||
*/ | ||
template <typename DecimalType> | ||
std::unique_ptr<rmm::device_buffer> convert_decimals_to_decimal128( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I'm not clear why this needs to be a smart pointer, instead of just the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Imo both are ok. Before we used to return |
||
cudf::column_view const& input, rmm::cuda_stream_view stream, rmm::device_async_resource_ref mr); | ||
|
||
} // namespace cudf::detail |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy pasted this from
to_arrow_host.cu
. Just renamed the function to be more generic