-
Notifications
You must be signed in to change notification settings - Fork 915
/
simple.cuh
426 lines (387 loc) · 17.1 KB
/
simple.cuh
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* Copyright (c) 2019-2021, 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/detail/copy.hpp>
#include <cudf/detail/reduction.cuh>
#include <cudf/detail/utilities/cuda.cuh>
#include <cudf/dictionary/detail/iterator.cuh>
#include <cudf/dictionary/dictionary_column_view.hpp>
#include <cudf/scalar/scalar_device_view.cuh>
#include <cudf/scalar/scalar_factories.hpp>
#include <cudf/structs/struct_view.hpp>
#include <cudf/utilities/traits.hpp>
#include <cudf/utilities/type_dispatcher.hpp>
#include <rmm/cuda_stream_view.hpp>
namespace cudf {
namespace reduction {
namespace simple {
/**
* @brief Reduction for 'sum', 'product', 'min', 'max', 'sum of squares'
* which directly compute the reduction by a single step reduction call
*
* @tparam ElementType the input column data-type
* @tparam ResultType the output data-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 ElementType, typename ResultType, typename Op>
std::unique_ptr<scalar> simple_reduction(column_view const& col,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
// 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<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<ResultType>();
auto it = thrust::make_transform_iterator(dcol->begin<ElementType>(), f);
return detail::reduce(it, col.size(), simple_op, stream, mr);
}
}();
// set scalar is valid
result->set_valid_async(col.null_count() < col.size(), stream);
return result;
}
/**
* @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<DecimalXX>;
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<Type>();
auto it = thrust::make_transform_iterator(dcol->pair_begin<Type, true>(), f);
return detail::reduce(it, col.size(), simple_op, stream, mr);
} else {
auto f = simple_op.template get_element_transformer<Type>();
auto it = thrust::make_transform_iterator(dcol->begin<Type>(), f);
return detail::reduce(it, col.size(), simple_op, stream, mr);
}
}();
auto const scale = [&] {
if (std::is_same_v<Op, cudf::reduction::op::product>) {
auto const valid_count = static_cast<int32_t>(col.size() - col.null_count());
return numeric::scale_type{col.type().scale() * valid_count};
} else if (std::is_same_v<Op, cudf::reduction::op::sum_of_squares>) {
return numeric::scale_type{col.type().scale() * 2};
}
return numeric::scale_type{col.type().scale()};
}();
auto const val = static_cast<cudf::scalar_type_t<Type>*>(result.get());
return cudf::make_fixed_point_scalar<DecimalXX>(val->value(), scale);
}
/**
* @brief Reduction for 'sum', 'product', 'sum of squares' for dictionary columns.
*
* @tparam ElementType The key type of the input dictionary column.
* @tparam ResultType The output data-type for the resulting scalar
* @tparam Op The operator of cudf::reduction::op::
* @param col Input dictionary 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 ElementType, typename ResultType, typename Op>
std::unique_ptr<scalar> dictionary_reduction(column_view const& col,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
auto dcol = cudf::column_device_view::create(col, stream);
auto simple_op = Op{};
auto result = [&] {
auto f = simple_op.template get_null_replacing_element_transformer<ResultType>();
auto p =
cudf::dictionary::detail::make_dictionary_pair_iterator<ElementType>(*dcol, col.has_nulls());
auto it = thrust::make_transform_iterator(p, f);
return detail::reduce(it, col.size(), simple_op, stream, mr);
}();
// set scalar is valid
result->set_valid_async(col.null_count() < col.size(), stream);
return result;
}
/**
* @brief Convert a numeric scalar to another numeric scalar.
*
* The input value and validity are cast to the output scalar.
*
* @tparam InputType The type of the input scalar to copy from
* @tparam OutputType The output scalar type to copy to
*/
template <typename InputType, typename OutputType>
struct assign_scalar_fn {
__device__ void operator()()
{
d_output.set_value(static_cast<OutputType>(d_input.value()));
d_output.set_valid(d_input.is_valid());
}
cudf::numeric_scalar_device_view<InputType> d_input;
cudf::numeric_scalar_device_view<OutputType> d_output;
};
/**
* @brief A type-dispatcher functor for converting a numeric scalar.
*
* The InputType is known and the dispatch is on the ResultType
* which is the output numeric scalar type.
*
* @tparam InputType The scalar type to convert from
*/
template <typename InputType>
struct cast_numeric_scalar_fn {
private:
template <typename ResultType>
static constexpr bool is_supported()
{
return cudf::is_convertible<InputType, ResultType>::value && cudf::is_numeric<ResultType>();
}
public:
template <typename ResultType, std::enable_if_t<is_supported<ResultType>()>* = nullptr>
std::unique_ptr<scalar> operator()(numeric_scalar<InputType>* input,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
auto d_input = cudf::get_scalar_device_view(*input);
auto result = std::make_unique<numeric_scalar<ResultType>>(ResultType{}, true, stream, mr);
auto d_output = cudf::get_scalar_device_view(*result);
cudf::detail::device_single_thread(assign_scalar_fn<InputType, ResultType>{d_input, d_output},
stream);
return result;
}
template <typename ResultType, std::enable_if_t<not is_supported<ResultType>()>* = nullptr>
std::unique_ptr<scalar> operator()(numeric_scalar<InputType>*,
rmm::cuda_stream_view,
rmm::mr::device_memory_resource*)
{
CUDF_FAIL("input data type is not convertible to output data type");
}
};
/**
* @brief Call reduce and return a scalar of type bool.
*
* This is used by operations `any()` and `all()`.
*
* @tparam Op The reduce operation to execute on the column.
*/
template <typename Op>
struct bool_result_element_dispatcher {
template <typename ElementType,
std::enable_if_t<std::is_arithmetic<ElementType>::value>* = nullptr>
std::unique_ptr<scalar> operator()(column_view const& col,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
return simple_reduction<ElementType, bool, Op>(col, stream, mr);
}
template <typename ElementType,
std::enable_if_t<not std::is_arithmetic<ElementType>::value>* = nullptr>
std::unique_ptr<scalar> operator()(column_view const&,
rmm::cuda_stream_view,
rmm::mr::device_memory_resource*)
{
CUDF_FAIL("Reduction operator not supported for this type");
}
};
/**
* @brief Call reduce and return a scalar of type matching the input column.
*
* This is used by operations `min()` and `max()`.
*
* @tparam Op The reduce operation to execute on the column.
*/
template <typename Op>
struct same_element_type_dispatcher {
private:
template <typename ElementType>
static constexpr bool is_supported()
{
return !(cudf::is_dictionary<ElementType>() || std::is_same_v<ElementType, cudf::list_view> ||
std::is_same_v<ElementType, cudf::struct_view>);
}
template <typename IndexType,
typename std::enable_if_t<cudf::is_index_type<IndexType>()>* = nullptr>
std::unique_ptr<scalar> resolve_key(column_view const& keys,
scalar const& keys_index,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
auto& index = static_cast<numeric_scalar<IndexType> const&>(keys_index);
return cudf::detail::get_element(keys, index.value(stream), stream, mr);
}
template <typename IndexType,
typename std::enable_if_t<!cudf::is_index_type<IndexType>()>* = nullptr>
std::unique_ptr<scalar> resolve_key(column_view const&,
scalar const&,
rmm::cuda_stream_view,
rmm::mr::device_memory_resource*)
{
CUDF_FAIL("index type expected for dictionary column");
}
public:
template <typename ElementType,
std::enable_if_t<is_supported<ElementType>() &&
not cudf::is_fixed_point<ElementType>()>* = nullptr>
std::unique_ptr<scalar> operator()(column_view const& col,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
if (!cudf::is_dictionary(col.type())) {
return simple::simple_reduction<ElementType, ElementType, Op>(col, stream, mr);
}
auto index = simple::simple_reduction<ElementType, ElementType, Op>(
dictionary_column_view(col).get_indices_annotated(),
stream,
rmm::mr::get_current_device_resource());
return resolve_key<ElementType>(dictionary_column_view(col).keys(), *index, stream, mr);
}
template <typename ElementType, std::enable_if_t<cudf::is_fixed_point<ElementType>()>* = nullptr>
std::unique_ptr<scalar> operator()(column_view const& col,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
return fixed_point_reduction<ElementType, Op>(col, stream, mr);
}
template <typename ElementType, std::enable_if_t<not is_supported<ElementType>()>* = nullptr>
std::unique_ptr<scalar> operator()(column_view const&,
rmm::cuda_stream_view,
rmm::mr::device_memory_resource*)
{
CUDF_FAIL("Reduction operator not supported for this type");
}
};
/**
* @brief Call reduce and return a scalar of the type specified.
*
* This is used by operations sum(), product(), and sum_of_squares().
* It only supports numeric types. If the output type is not the
* same as the input type, an extra cast operation may incur.
*
* @tparam Op The reduce operation to execute on the column.
*/
template <typename Op>
struct element_type_dispatcher {
/**
* @brief Specialization for reducing floating-point column types to any output type.
*/
template <typename ElementType,
typename std::enable_if_t<std::is_floating_point<ElementType>::value>* = nullptr>
std::unique_ptr<scalar> reduce_numeric(column_view const& col,
data_type const output_type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
auto result = !cudf::is_dictionary(col.type())
? simple_reduction<ElementType, double, Op>(col, stream, mr)
: dictionary_reduction<ElementType, double, Op>(col, stream, mr);
if (output_type == result->type()) return result;
// this will cast the result to the output_type
return cudf::type_dispatcher(output_type,
cast_numeric_scalar_fn<double>{},
static_cast<numeric_scalar<double>*>(result.get()),
stream,
mr);
}
/**
* @brief Specialization for reducing integer column types to any output type.
*/
template <typename ElementType,
typename std::enable_if_t<std::is_integral<ElementType>::value>* = nullptr>
std::unique_ptr<scalar> reduce_numeric(column_view const& col,
data_type const output_type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
auto result = !cudf::is_dictionary(col.type())
? simple_reduction<ElementType, int64_t, Op>(col, stream, mr)
: dictionary_reduction<ElementType, int64_t, Op>(col, stream, mr);
if (output_type == result->type()) return result;
// this will cast the result to the output_type
return cudf::type_dispatcher(output_type,
cast_numeric_scalar_fn<int64_t>{},
static_cast<numeric_scalar<int64_t>*>(result.get()),
stream,
mr);
}
/**
* @brief Called by the type-dispatcher to reduce the input column `col` using
* the `Op` operation.
*
* @tparam ElementType The input column type or key type.
* @param col Input column (must be numeric)
* @param output_type Requested type of the scalar result
* @param mr Device memory resource used to allocate the returned scalar's device memory
* @param stream CUDA stream used for device memory operations and kernel launches.
*/
template <typename ElementType,
typename std::enable_if_t<cudf::is_numeric<ElementType>()>* = nullptr>
std::unique_ptr<scalar> operator()(column_view const& col,
data_type const output_type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
if (output_type.id() == cudf::type_to_id<ElementType>())
return !cudf::is_dictionary(col.type())
? simple_reduction<ElementType, ElementType, Op>(col, stream, mr)
: dictionary_reduction<ElementType, ElementType, Op>(col, stream, mr);
// reduce and map to output type
return reduce_numeric<ElementType>(col, output_type, stream, mr);
}
/**
* @brief Specialization for reducing integer column types to any output type.
*/
template <typename ElementType,
typename std::enable_if_t<cudf::is_fixed_point<ElementType>()>* = nullptr>
std::unique_ptr<scalar> operator()(column_view const& col,
data_type const output_type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_EXPECTS(output_type == col.type(), "Output type must be same as input column type.");
return fixed_point_reduction<ElementType, Op>(col, stream, mr);
}
template <typename ElementType,
typename std::enable_if_t<not cudf::is_numeric<ElementType>() and
not cudf::is_fixed_point<ElementType>()>* = nullptr>
std::unique_ptr<scalar> operator()(column_view const&,
data_type const,
rmm::cuda_stream_view,
rmm::mr::device_memory_resource*)
{
CUDF_FAIL("Reduction operator not supported for this type");
}
};
} // namespace simple
} // namespace reduction
} // namespace cudf