-
Notifications
You must be signed in to change notification settings - Fork 915
/
round.cu
333 lines (284 loc) · 11.5 KB
/
round.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright (c) 2020, 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 <cudf/column/column_factories.hpp>
#include <cudf/copying.hpp>
#include <cudf/detail/null_mask.hpp>
#include <cudf/detail/nvtx/ranges.hpp>
#include <cudf/detail/round.hpp>
#include <cudf/detail/unary.hpp>
#include <cudf/fixed_point/fixed_point.hpp>
#include <cudf/fixed_point/temporary.hpp>
#include <cudf/round.hpp>
#include <cudf/scalar/scalar.hpp>
#include <cudf/scalar/scalar_factories.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/error.hpp>
#include <cudf/utilities/type_dispatcher.hpp>
#include <rmm/cuda_stream_view.hpp>
#include <rmm/exec_policy.hpp>
#include <type_traits>
namespace cudf {
namespace detail {
namespace { // anonymous
inline float __device__ generic_round(float f) { return roundf(f); }
inline double __device__ generic_round(double d) { return ::round(d); }
inline float __device__ generic_round_half_even(float f) { return rintf(f); }
inline double __device__ generic_round_half_even(double d) { return rint(d); }
inline float __device__ generic_modf(float a, float* b) { return modff(a, b); }
inline double __device__ generic_modf(double a, double* b) { return modf(a, b); }
template <typename T, typename std::enable_if_t<cuda::std::is_signed<T>::value>* = nullptr>
T __device__ generic_abs(T value)
{
return numeric::detail::abs(value);
}
template <typename T, typename std::enable_if_t<not cuda::std::is_signed<T>::value>* = nullptr>
T __device__ generic_abs(T value)
{
return value;
}
template <typename T, typename std::enable_if_t<cuda::std::is_signed<T>::value>* = nullptr>
int16_t __device__ generic_sign(T value)
{
return value < 0 ? -1 : 1;
}
// this is needed to suppress warning: pointless comparison of unsigned integer with zero
template <typename T, typename std::enable_if_t<not cuda::std::is_signed<T>::value>* = nullptr>
int16_t __device__ generic_sign(T)
{
return 1;
}
template <typename T>
constexpr inline auto is_supported_round_type()
{
return (cudf::is_numeric<T>() && not std::is_same_v<T, bool>) || cudf::is_fixed_point<T>();
}
template <typename T>
struct half_up_zero {
T n; // unused in the decimal_places = 0 case
template <typename U = T, typename std::enable_if_t<cudf::is_floating_point<U>()>* = nullptr>
__device__ U operator()(U e)
{
return generic_round(e);
}
template <typename U = T, typename std::enable_if_t<cuda::std::is_integral<U>::value>* = nullptr>
__device__ U operator()(U)
{
assert(false); // Should never get here. Just for compilation
return U{};
}
};
template <typename T>
struct half_up_positive {
T n;
template <typename U = T, typename std::enable_if_t<cudf::is_floating_point<U>()>* = nullptr>
__device__ U operator()(U e)
{
T integer_part;
T const fractional_part = generic_modf(e, &integer_part);
return integer_part + generic_round(fractional_part * n) / n;
}
template <typename U = T, typename std::enable_if_t<cuda::std::is_integral<U>::value>* = nullptr>
__device__ U operator()(U)
{
assert(false); // Should never get here. Just for compilation
return U{};
}
};
template <typename T>
struct half_up_negative {
T n;
template <typename U = T, typename std::enable_if_t<cudf::is_floating_point<U>()>* = nullptr>
__device__ U operator()(U e)
{
return generic_round(e / n) * n;
}
template <typename U = T, typename std::enable_if_t<cuda::std::is_integral<U>::value>* = nullptr>
__device__ U operator()(U e)
{
auto const down = (e / n) * n; // result from rounding down
return down + generic_sign(e) * (generic_abs(e - down) >= n / 2 ? n : 0);
}
};
template <typename T>
struct half_even_zero {
T n; // unused in the decimal_places = 0 case
template <typename U = T, typename std::enable_if_t<cudf::is_floating_point<U>()>* = nullptr>
__device__ U operator()(U e)
{
return generic_round_half_even(e);
}
template <typename U = T, typename std::enable_if_t<cuda::std::is_integral<U>::value>* = nullptr>
__device__ U operator()(U)
{
assert(false); // Should never get here. Just for compilation
return U{};
}
};
template <typename T>
struct half_even_positive {
T n;
template <typename U = T, typename std::enable_if_t<cudf::is_floating_point<U>()>* = nullptr>
__device__ U operator()(U e)
{
T integer_part;
T const fractional_part = generic_modf(e, &integer_part);
return integer_part + generic_round_half_even(fractional_part * n) / n;
}
template <typename U = T, typename std::enable_if_t<cuda::std::is_integral<U>::value>* = nullptr>
__device__ U operator()(U)
{
assert(false); // Should never get here. Just for compilation
return U{};
}
};
template <typename T>
struct half_even_negative {
T n;
template <typename U = T, typename std::enable_if_t<cudf::is_floating_point<U>()>* = nullptr>
__device__ U operator()(U e)
{
return generic_round_half_even(e / n) * n;
}
template <typename U = T, typename std::enable_if_t<cuda::std::is_integral<U>::value>* = nullptr>
__device__ U operator()(U e)
{
auto const down_over_n = e / n; // use this to determine HALF_EVEN case
auto const down = down_over_n * n; // result from rounding down
auto const diff = generic_abs(e - down);
auto const adjustment =
(diff > n / 2) or (diff == n / 2 && generic_abs(down_over_n) % 2 == 1) ? n : 0;
return down + generic_sign(e) * adjustment;
}
};
template <typename T>
struct half_up_fixed_point {
T n;
__device__ T operator()(T e) { return half_up_negative<T>{n}(e) / n; }
};
template <typename T>
struct half_even_fixed_point {
T n;
__device__ T operator()(T e) { return half_even_negative<T>{n}(e) / n; }
};
template <typename T,
template <typename>
typename RoundFunctor,
typename std::enable_if_t<not cudf::is_fixed_point<T>()>* = nullptr>
std::unique_ptr<column> round_with(column_view const& input,
int32_t decimal_places,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
using Functor = RoundFunctor<T>;
if (decimal_places >= 0 && std::is_integral<T>::value)
return std::make_unique<cudf::column>(input, stream, mr);
auto result = cudf::make_fixed_width_column(
input.type(), input.size(), copy_bitmask(input, stream, mr), input.null_count(), stream, mr);
auto out_view = result->mutable_view();
T const n = std::pow(10, std::abs(decimal_places));
thrust::transform(
rmm::exec_policy(stream), input.begin<T>(), input.end<T>(), out_view.begin<T>(), Functor{n});
return result;
}
template <typename T,
template <typename>
typename RoundFunctor,
typename std::enable_if_t<cudf::is_fixed_point<T>()>* = nullptr>
std::unique_ptr<column> round_with(column_view const& input,
int32_t decimal_places,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
using namespace numeric;
using Type = device_storage_type_t<T>;
using FixedPointRoundFunctor = RoundFunctor<Type>;
if (input.type().scale() == -decimal_places)
return std::make_unique<cudf::column>(input, stream, mr);
auto const result_type = data_type{input.type().id(), scale_type{-decimal_places}};
// if rounding to more precision than fixed_point is capable of, just need to rescale
// note: decimal_places has the opposite sign of numeric::scale_type (therefore have to negate)
if (input.type().scale() > -decimal_places) return cudf::detail::cast(input, result_type);
auto result = cudf::make_fixed_width_column(
result_type, input.size(), copy_bitmask(input, stream, mr), input.null_count(), stream, mr);
auto out_view = result->mutable_view();
Type const n = std::pow(10, std::abs(decimal_places + input.type().scale()));
thrust::transform(rmm::exec_policy(stream),
input.begin<Type>(),
input.end<Type>(),
out_view.begin<Type>(),
FixedPointRoundFunctor{n});
return result;
}
struct round_type_dispatcher {
template <typename T, typename... Args>
std::enable_if_t<not is_supported_round_type<T>(), std::unique_ptr<column>> operator()(Args&&...)
{
CUDF_FAIL("Type not support for cudf::round");
}
template <typename T>
std::enable_if_t<is_supported_round_type<T>(), std::unique_ptr<column>> operator()(
column_view const& input,
int32_t decimal_places,
cudf::rounding_method method,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
// clang-format off
switch (method) {
case cudf::rounding_method::HALF_UP:
if (is_fixed_point<T>()) return round_with<T, half_up_fixed_point>(input, decimal_places, stream, mr);
else if (decimal_places == 0) return round_with<T, half_up_zero >(input, decimal_places, stream, mr);
else if (decimal_places > 0) return round_with<T, half_up_positive >(input, decimal_places, stream, mr);
else return round_with<T, half_up_negative >(input, decimal_places, stream, mr);
case cudf::rounding_method::HALF_EVEN:
if (is_fixed_point<T>()) return round_with<T, half_even_fixed_point>(input, decimal_places, stream, mr);
else if (decimal_places == 0) return round_with<T, half_even_zero >(input, decimal_places, stream, mr);
else if (decimal_places > 0) return round_with<T, half_even_positive >(input, decimal_places, stream, mr);
else return round_with<T, half_even_negative >(input, decimal_places, stream, mr);
default: CUDF_FAIL("Undefined rounding method");
}
// clang-format on
}
};
} // anonymous namespace
std::unique_ptr<column> round(column_view const& input,
int32_t decimal_places,
cudf::rounding_method method,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_EXPECTS(cudf::is_numeric(input.type()) || cudf::is_fixed_point(input.type()),
"Only integral/floating point/fixed point currently supported.");
if (input.is_empty()) {
if (is_fixed_point(input.type())) {
auto const type = data_type{input.type().id(), numeric::scale_type{-decimal_places}};
return std::make_unique<cudf::column>(type, 0, rmm::device_buffer{});
}
return empty_like(input);
}
return type_dispatcher(
input.type(), round_type_dispatcher{}, input, decimal_places, method, stream, mr);
}
} // namespace detail
std::unique_ptr<column> round(column_view const& input,
int32_t decimal_places,
rounding_method method,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return cudf::detail::round(input, decimal_places, method, rmm::cuda_stream_default, mr);
}
} // namespace cudf