-
Notifications
You must be signed in to change notification settings - Fork 916
/
rolling.cu
1596 lines (1427 loc) · 69.1 KB
/
rolling.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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2019-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/aggregation.hpp>
#include <cudf/column/column_device_view.cuh>
#include <cudf/column/column_factories.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/copying.hpp>
#include <cudf/detail/aggregation/aggregation.cuh>
#include <cudf/detail/copy.hpp>
#include <cudf/detail/gather.hpp>
#include <cudf/detail/groupby/sort_helper.hpp>
#include <cudf/detail/nvtx/ranges.hpp>
#include <cudf/detail/utilities/cuda.cuh>
#include <cudf/rolling.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/bit.hpp>
#include <rolling/rolling_detail.hpp>
#include <rolling/rolling_jit_detail.hpp>
#include <jit/launcher.h>
#include <jit/parser.h>
#include <jit/type.h>
#include <rolling/jit/code/code.h>
#include <bit.hpp.jit>
#include <rolling_jit_detail.hpp.jit>
#include <types.hpp.jit>
#include <thrust/binary_search.h>
#include <rmm/device_scalar.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/detail/utilities/device_operators.cuh>
#include <cudf/utilities/error.hpp>
#include <cudf/utilities/traits.hpp>
#include <memory>
namespace cudf {
namespace detail {
namespace { // anonymous
/**
* @brief Only count operation is executed and count is updated
* depending on `min_periods` and returns true if it was
* valid, else false.
*/
template <typename InputType,
typename OutputType,
typename agg_op,
aggregation::Kind op,
bool has_nulls>
std::enable_if_t<op == aggregation::COUNT_VALID || op == aggregation::COUNT_ALL, bool> __device__
process_rolling_window(column_device_view input,
column_device_view ignored_default_outputs,
mutable_column_device_view output,
size_type start_index,
size_type end_index,
size_type current_index,
size_type min_periods)
{
// declare this as volatile to avoid some compiler optimizations that lead to incorrect results
// for CUDA 10.0 and below (fixed in CUDA 10.1)
volatile cudf::size_type count = 0;
for (size_type j = start_index; j < end_index; j++) {
if (op == aggregation::COUNT_ALL || !has_nulls || input.is_valid(j)) { count++; }
}
bool output_is_valid = (count >= min_periods);
output.element<OutputType>(current_index) = count;
return output_is_valid;
}
/**
* @brief Calculates row-number within [start_index, end_index).
* Count is updated depending on `min_periods`
* Returns true if it was valid, else false.
*/
template <typename InputType,
typename OutputType,
typename agg_op,
aggregation::Kind op,
bool has_nulls>
std::enable_if_t<op == aggregation::ROW_NUMBER, bool> __device__
process_rolling_window(column_device_view input,
column_device_view ignored_default_outputs,
mutable_column_device_view output,
size_type start_index,
size_type end_index,
size_type current_index,
size_type min_periods)
{
bool output_is_valid = ((end_index - start_index) >= min_periods);
output.element<OutputType>(current_index) = ((current_index - start_index) + 1);
return output_is_valid;
}
/**
* @brief LEAD(N): Returns the row from the input column, at the specified offset past the
* current row.
* If the offset crosses the grouping boundary or column boundary for
* a given row, a "default" value is returned. The "default" value is null, by default.
*
* E.g. Consider an input column with the following values and grouping:
* [10, 11, 12, 13, 20, 21, 22, 23]
* <------G1-----> <------G2------>
*
* LEAD(input_col, 1) yields:
* [11, 12, 13, null, 21, 22, 23, null]
*
* LEAD(input_col, 1, 99) (where 99 indicates the default) yields:
* [11, 12, 13, 99, 21, 22, 23, 99]
*/
template <typename InputType,
typename OutputType,
typename agg_op,
aggregation::Kind op,
bool has_nulls>
std::enable_if_t<(op == aggregation::LEAD) && (cudf::is_fixed_width<InputType>()), bool> __device__
process_rolling_window(column_device_view input,
column_device_view default_outputs,
mutable_column_device_view output,
size_type start_index,
size_type end_index,
size_type current_index,
size_type min_periods,
agg_op device_agg_op)
{
// Offsets have already been normalized.
auto row_offset = device_agg_op.row_offset;
// Check if row is invalid.
if (row_offset > (end_index - current_index - 1)) {
// Invalid row marked. Use default value, if available.
if (default_outputs.size() == 0 || default_outputs.is_null(current_index)) { return false; }
output.element<OutputType>(current_index) = default_outputs.element<OutputType>(current_index);
return true;
}
// Not an invalid row.
auto index = current_index + row_offset;
auto is_null = input.is_null(index);
if (!is_null) { output.element<OutputType>(current_index) = input.element<InputType>(index); }
return !is_null;
}
/**
* @brief LAG(N): returns the row from the input column at the specified offset preceding
* the current row.
* If the offset crosses the grouping boundary or column boundary for
* a given row, a "default" value is returned. The "default" value is null, by default.
*
* E.g. Consider an input column with the following values and grouping:
* [10, 11, 12, 13, 20, 21, 22, 23]
* <------G1-----> <------G2------>
*
* LAG(input_col, 2) yields:
* [null, null, 10, 11, null, null, 20, 21]
* LAG(input_col, 2, 99) yields:
* [99, 99, 10, 11, 99, 99, 20, 21]
*/
template <typename InputType,
typename OutputType,
typename agg_op,
aggregation::Kind op,
bool has_nulls>
std::enable_if_t<(op == aggregation::LAG) && (cudf::is_fixed_width<InputType>()), bool> __device__
process_rolling_window(column_device_view input,
column_device_view default_outputs,
mutable_column_device_view output,
size_type start_index,
size_type end_index,
size_type current_index,
size_type min_periods,
agg_op device_agg_op)
{
// Offsets have already been normalized.
auto row_offset = device_agg_op.row_offset;
// Check if row is invalid.
if (row_offset > (current_index - start_index)) {
// Invalid row marked. Use default value, if available.
if (default_outputs.size() == 0 || default_outputs.is_null(current_index)) { return false; }
output.element<OutputType>(current_index) = default_outputs.element<OutputType>(current_index);
return true;
}
// Not an invalid row.
auto index = current_index - row_offset;
auto is_null = input.is_null(index);
if (!is_null) { output.element<OutputType>(current_index) = input.element<InputType>(index); }
return !is_null;
}
/**
* @brief Only used for `string_view` type to get ARGMIN and ARGMAX, which
* will be used to gather MIN and MAX. And returns true if the
* operation was valid, else false.
*/
template <typename InputType,
typename OutputType,
typename agg_op,
aggregation::Kind op,
bool has_nulls>
std::enable_if_t<(op == aggregation::ARGMIN or op == aggregation::ARGMAX) and
std::is_same<InputType, cudf::string_view>::value,
bool>
__device__ process_rolling_window(column_device_view input,
column_device_view ignored_default_outputs,
mutable_column_device_view output,
size_type start_index,
size_type end_index,
size_type current_index,
size_type min_periods)
{
// declare this as volatile to avoid some compiler optimizations that lead to incorrect results
// for CUDA 10.0 and below (fixed in CUDA 10.1)
volatile cudf::size_type count = 0;
InputType val = agg_op::template identity<InputType>();
OutputType val_index = (op == aggregation::ARGMIN) ? ARGMIN_SENTINEL : ARGMAX_SENTINEL;
for (size_type j = start_index; j < end_index; j++) {
if (!has_nulls || input.is_valid(j)) {
InputType element = input.element<InputType>(j);
val = agg_op{}(element, val);
if (val == element) { val_index = j; }
count++;
}
}
bool output_is_valid = (count >= min_periods);
// -1 will help identify null elements while gathering for Min and Max
// In case of count, this would be null, so doesn't matter.
output.element<OutputType>(current_index) = (output_is_valid) ? val_index : -1;
// The gather mask shouldn't contain null values, so
// always return zero
return true;
}
/**
* @brief Operates on only fixed-width types and returns true if the
* operation was valid, else false.
*/
template <typename InputType,
typename OutputType,
typename agg_op,
aggregation::Kind op,
bool has_nulls>
std::enable_if_t<!std::is_same<InputType, cudf::string_view>::value and
!(op == aggregation::COUNT_VALID || op == aggregation::COUNT_ALL ||
op == aggregation::ROW_NUMBER || op == aggregation::LEAD ||
op == aggregation::LAG),
bool>
__device__ process_rolling_window(column_device_view input,
column_device_view ignored_default_outputs,
mutable_column_device_view output,
size_type start_index,
size_type end_index,
size_type current_index,
size_type min_periods)
{
// declare this as volatile to avoid some compiler optimizations that lead to incorrect results
// for CUDA 10.0 and below (fixed in CUDA 10.1)
volatile cudf::size_type count = 0;
OutputType val = agg_op::template identity<OutputType>();
for (size_type j = start_index; j < end_index; j++) {
if (!has_nulls || input.is_valid(j)) {
OutputType element = input.element<InputType>(j);
val = agg_op{}(element, val);
count++;
}
}
bool output_is_valid = (count >= min_periods);
// store the output value, one per thread
cudf::detail::rolling_store_output_functor<OutputType, op == aggregation::MEAN>{}(
output.element<OutputType>(current_index), val, count);
return output_is_valid;
}
/**
* @brief Computes the rolling window function
*
* @tparam InputType Datatype of `input`
* @tparam OutputType Datatype of `output`
* @tparam agg_op A functor that defines the aggregation operation
* @tparam op The aggregation operator (enum value)
* @tparam block_size CUDA block size for the kernel
* @tparam has_nulls true if the input column has nulls
* @tparam PrecedingWindowIterator iterator type (inferred)
* @tparam FollowingWindowIterator iterator type (inferred)
* @param input Input column device view
* @param output Output column device view
* @param preceding_window_begin[in] Rolling window size iterator, accumulates from
* in_col[i-preceding_window] to in_col[i] inclusive
* @param following_window_begin[in] Rolling window size iterator in the forward
* direction, accumulates from in_col[i] to
* in_col[i+following_window] inclusive
* @param min_periods[in] Minimum number of observations in window required to
* have a value, otherwise 0 is stored in the valid bit mask
*/
template <typename InputType,
typename OutputType,
typename agg_op,
aggregation::Kind op,
int block_size,
bool has_nulls,
typename PrecedingWindowIterator,
typename FollowingWindowIterator>
__launch_bounds__(block_size) __global__
void gpu_rolling(column_device_view input,
column_device_view default_outputs,
mutable_column_device_view output,
size_type* __restrict__ output_valid_count,
PrecedingWindowIterator preceding_window_begin,
FollowingWindowIterator following_window_begin,
size_type min_periods)
{
size_type i = blockIdx.x * block_size + threadIdx.x;
size_type stride = block_size * gridDim.x;
size_type warp_valid_count{0};
auto active_threads = __ballot_sync(0xffffffff, i < input.size());
while (i < input.size()) {
size_type preceding_window = preceding_window_begin[i];
size_type following_window = following_window_begin[i];
// compute bounds
size_type start = min(input.size(), max(0, i - preceding_window + 1));
size_type end = min(input.size(), max(0, i + following_window + 1));
size_type start_index = min(start, end);
size_type end_index = max(start, end);
// aggregate
// TODO: We should explore using shared memory to avoid redundant loads.
// This might require separating the kernel into a special version
// for dynamic and static sizes.
volatile bool output_is_valid = false;
output_is_valid = process_rolling_window<InputType, OutputType, agg_op, op, has_nulls>(
input, default_outputs, output, start_index, end_index, i, min_periods);
// set the mask
cudf::bitmask_type result_mask{__ballot_sync(active_threads, output_is_valid)};
// only one thread writes the mask
if (0 == threadIdx.x % cudf::detail::warp_size) {
output.set_mask_word(cudf::word_index(i), result_mask);
warp_valid_count += __popc(result_mask);
}
// process next element
i += stride;
active_threads = __ballot_sync(active_threads, i < input.size());
}
// sum the valid counts across the whole block
size_type block_valid_count =
cudf::detail::single_lane_block_sum_reduce<block_size, 0>(warp_valid_count);
if (threadIdx.x == 0) { atomicAdd(output_valid_count, block_valid_count); }
}
template <typename InputType,
typename OutputType,
typename agg_op,
aggregation::Kind op,
int block_size,
bool has_nulls,
typename PrecedingWindowIterator,
typename FollowingWindowIterator>
__launch_bounds__(block_size) __global__
void gpu_rolling(column_device_view input,
column_device_view default_outputs,
mutable_column_device_view output,
size_type* __restrict__ output_valid_count,
PrecedingWindowIterator preceding_window_begin,
FollowingWindowIterator following_window_begin,
size_type min_periods,
agg_op device_agg_op)
{
size_type i = blockIdx.x * block_size + threadIdx.x;
size_type stride = block_size * gridDim.x;
size_type warp_valid_count{0};
auto active_threads = __ballot_sync(0xffffffff, i < input.size());
while (i < input.size()) {
size_type preceding_window = preceding_window_begin[i];
size_type following_window = following_window_begin[i];
// compute bounds
size_type start = min(input.size(), max(0, i - preceding_window + 1));
size_type end = min(input.size(), max(0, i + following_window + 1));
size_type start_index = min(start, end);
size_type end_index = max(start, end);
// aggregate
// TODO: We should explore using shared memory to avoid redundant loads.
// This might require separating the kernel into a special version
// for dynamic and static sizes.
volatile bool output_is_valid = false;
output_is_valid = process_rolling_window<InputType, OutputType, agg_op, op, has_nulls>(
input, default_outputs, output, start_index, end_index, i, min_periods, device_agg_op);
// set the mask
cudf::bitmask_type result_mask{__ballot_sync(active_threads, output_is_valid)};
// only one thread writes the mask
if (0 == threadIdx.x % cudf::detail::warp_size) {
output.set_mask_word(cudf::word_index(i), result_mask);
warp_valid_count += __popc(result_mask);
}
// process next element
i += stride;
active_threads = __ballot_sync(active_threads, i < input.size());
}
// sum the valid counts across the whole block
size_type block_valid_count =
cudf::detail::single_lane_block_sum_reduce<block_size, 0>(warp_valid_count);
if (threadIdx.x == 0) { atomicAdd(output_valid_count, block_valid_count); }
}
template <typename InputType>
struct rolling_window_launcher {
template <typename T,
typename agg_op,
aggregation::Kind op,
typename PrecedingWindowIterator,
typename FollowingWindowIterator>
size_type kernel_launcher(column_view const& input,
column_view const& default_outputs,
mutable_column_view& output,
PrecedingWindowIterator preceding_window_begin,
FollowingWindowIterator following_window_begin,
size_type min_periods,
std::unique_ptr<aggregation> const& agg,
cudaStream_t stream)
{
constexpr cudf::size_type block_size = 256;
cudf::detail::grid_1d grid(input.size(), block_size);
auto input_device_view = column_device_view::create(input, stream);
auto output_device_view = mutable_column_device_view::create(output, stream);
auto default_outputs_device_view = column_device_view::create(default_outputs, stream);
rmm::device_scalar<size_type> device_valid_count{0, stream};
if (input.has_nulls()) {
gpu_rolling<T, target_type_t<InputType, op>, agg_op, op, block_size, true>
<<<grid.num_blocks, block_size, 0, stream>>>(*input_device_view,
*default_outputs_device_view,
*output_device_view,
device_valid_count.data(),
preceding_window_begin,
following_window_begin,
min_periods);
} else {
gpu_rolling<T, target_type_t<InputType, op>, agg_op, op, block_size, false>
<<<grid.num_blocks, block_size, 0, stream>>>(*input_device_view,
*default_outputs_device_view,
*output_device_view,
device_valid_count.data(),
preceding_window_begin,
following_window_begin,
min_periods);
}
size_type valid_count = device_valid_count.value(stream);
// check the stream for debugging
CHECK_CUDA(stream);
return valid_count;
}
template <typename T,
typename agg_op,
aggregation::Kind op,
typename PrecedingWindowIterator,
typename FollowingWindowIterator>
size_type kernel_launcher(column_view const& input,
column_view const& default_outputs,
mutable_column_view& output,
PrecedingWindowIterator preceding_window_begin,
FollowingWindowIterator following_window_begin,
size_type min_periods,
std::unique_ptr<aggregation> const& agg,
agg_op const& device_agg_op,
cudaStream_t stream)
{
constexpr cudf::size_type block_size = 256;
cudf::detail::grid_1d grid(input.size(), block_size);
auto input_device_view = column_device_view::create(input, stream);
auto output_device_view = mutable_column_device_view::create(output, stream);
auto default_outputs_device_view = column_device_view::create(default_outputs, stream);
rmm::device_scalar<size_type> device_valid_count{0, stream};
if (input.has_nulls()) {
gpu_rolling<T, target_type_t<InputType, op>, agg_op, op, block_size, true>
<<<grid.num_blocks, block_size, 0, stream>>>(*input_device_view,
*default_outputs_device_view,
*output_device_view,
device_valid_count.data(),
preceding_window_begin,
following_window_begin,
min_periods,
device_agg_op);
} else {
gpu_rolling<T, target_type_t<InputType, op>, agg_op, op, block_size, false>
<<<grid.num_blocks, block_size, 0, stream>>>(*input_device_view,
*default_outputs_device_view,
*output_device_view,
device_valid_count.data(),
preceding_window_begin,
following_window_begin,
min_periods,
device_agg_op);
}
size_type valid_count = device_valid_count.value(stream);
// check the stream for debugging
CHECK_CUDA(stream);
return valid_count;
}
// This launch is only for fixed width columns with valid aggregation option
// numeric: All
// timestamp: MIN, MAX, COUNT_VALID, COUNT_ALL, ROW_NUMBER
// string, dictionary, list : COUNT_VALID, COUNT_ALL, ROW_NUMBER
template <typename T,
typename agg_op,
aggregation::Kind op,
typename PrecedingWindowIterator,
typename FollowingWindowIterator>
std::enable_if_t<cudf::detail::is_rolling_supported<T, agg_op, op>() and
!cudf::detail::is_rolling_string_specialization<T, agg_op, op>(),
std::unique_ptr<column>>
launch(column_view const& input,
column_view const& default_outputs,
PrecedingWindowIterator preceding_window_begin,
FollowingWindowIterator following_window_begin,
size_type min_periods,
std::unique_ptr<aggregation> const& agg,
rmm::mr::device_memory_resource* mr,
cudaStream_t stream)
{
if (input.is_empty()) return empty_like(input);
auto output = make_fixed_width_column(
target_type(input.type(), op), input.size(), mask_state::UNINITIALIZED, stream, mr);
cudf::mutable_column_view output_view = output->mutable_view();
auto valid_count =
kernel_launcher<T, agg_op, op, PrecedingWindowIterator, FollowingWindowIterator>(
input,
default_outputs,
output_view,
preceding_window_begin,
following_window_begin,
min_periods,
agg,
stream);
output->set_null_count(output->size() - valid_count);
return output;
}
// This launch is only for string specializations
// string: MIN, MAX
template <typename T,
typename agg_op,
aggregation::Kind op,
typename PrecedingWindowIterator,
typename FollowingWindowIterator>
std::enable_if_t<cudf::detail::is_rolling_string_specialization<T, agg_op, op>(),
std::unique_ptr<column>>
launch(column_view const& input,
column_view const& default_outputs,
PrecedingWindowIterator preceding_window_begin,
FollowingWindowIterator following_window_begin,
size_type min_periods,
std::unique_ptr<aggregation> const& agg,
rmm::mr::device_memory_resource* mr,
cudaStream_t stream)
{
if (input.is_empty()) return empty_like(input);
auto output = make_numeric_column(cudf::data_type{cudf::type_to_id<size_type>()},
input.size(),
cudf::mask_state::UNINITIALIZED,
stream,
mr);
cudf::mutable_column_view output_view = output->mutable_view();
// Passing the agg_op and aggregation::Kind as constant to group them in pair, else it
// evolves to error when try to use agg_op as compiler tries different combinations
if (op == aggregation::MIN) {
kernel_launcher<T,
DeviceMin,
aggregation::ARGMIN,
PrecedingWindowIterator,
FollowingWindowIterator>(input,
default_outputs,
output_view,
preceding_window_begin,
following_window_begin,
min_periods,
agg,
stream);
} else if (op == aggregation::MAX) {
kernel_launcher<T,
DeviceMax,
aggregation::ARGMAX,
PrecedingWindowIterator,
FollowingWindowIterator>(input,
default_outputs,
output_view,
preceding_window_begin,
following_window_begin,
min_periods,
agg,
stream);
} else {
CUDF_FAIL("MIN and MAX are the only supported aggregation types for string columns");
}
// The rows that represent null elements will be having negative values in gather map,
// and that's why nullify_out_of_bounds/ignore_out_of_bounds is true.
auto output_table = detail::gather(table_view{{input}},
output->view(),
detail::out_of_bounds_policy::IGNORE,
detail::negative_index_policy::NOT_ALLOWED,
mr,
stream);
return std::make_unique<cudf::column>(std::move(output_table->get_column(0)));
}
// Deals with invalid column and/or aggregation options
template <typename T,
typename agg_op,
aggregation::Kind op,
typename PrecedingWindowIterator,
typename FollowingWindowIterator>
std::enable_if_t<!cudf::detail::is_rolling_supported<T, agg_op, op>() and
!cudf::detail::is_rolling_string_specialization<T, agg_op, op>(),
std::unique_ptr<column>>
launch(column_view const& input,
column_view const& default_outputs,
PrecedingWindowIterator preceding_window_begin,
FollowingWindowIterator following_window_begin,
size_type min_periods,
std::unique_ptr<aggregation> const& agg,
rmm::mr::device_memory_resource* mr,
cudaStream_t stream)
{
CUDF_FAIL("Aggregation operator and/or input type combination is invalid");
}
template <typename T,
typename agg_op,
aggregation::Kind op,
typename PrecedingWindowIterator,
typename FollowingWindowIterator>
std::enable_if_t<cudf::is_fixed_width<T>() and
(op == aggregation::LEAD || op == aggregation::LAG),
std::unique_ptr<column>>
launch(column_view const& input,
column_view const& default_outputs,
PrecedingWindowIterator preceding_window_begin,
FollowingWindowIterator following_window_begin,
size_type min_periods,
std::unique_ptr<aggregation> const& agg,
agg_op const& device_agg_op,
rmm::mr::device_memory_resource* mr,
cudaStream_t stream)
{
if (input.is_empty()) return empty_like(input);
CUDF_EXPECTS(default_outputs.type().id() == input.type().id(),
"Defaults column type must match input column."); // Because LEAD/LAG.
// For LEAD(0)/LAG(0), no computation need be performed.
// Return copy of input.
if (0 == static_cast<cudf::detail::lead_lag_aggregation*>(agg.get())->row_offset) {
return std::make_unique<column>(input, stream, mr);
}
auto output = make_fixed_width_column(
target_type(input.type(), op), input.size(), mask_state::UNINITIALIZED, stream, mr);
cudf::mutable_column_view output_view = output->mutable_view();
auto valid_count =
kernel_launcher<T, agg_op, op, PrecedingWindowIterator, FollowingWindowIterator>(
input,
default_outputs,
output_view,
preceding_window_begin,
following_window_begin,
min_periods,
agg,
device_agg_op,
stream);
output->set_null_count(output->size() - valid_count);
return output;
}
// Deals with invalid column and/or aggregation options
template <typename T,
typename agg_op,
aggregation::Kind op,
typename PrecedingWindowIterator,
typename FollowingWindowIterator>
std::enable_if_t<!(op == aggregation::LEAD || op == aggregation::LAG) ||
!cudf::is_fixed_width<T>(),
std::unique_ptr<column>>
launch(column_view const& input,
column_view const& default_outputs,
PrecedingWindowIterator preceding_window_begin,
FollowingWindowIterator following_window_begin,
size_type min_periods,
std::unique_ptr<aggregation> const& agg,
agg_op device_agg_op,
rmm::mr::device_memory_resource* mr,
cudaStream_t stream)
{
CUDF_FAIL(
"Aggregation operator and/or input type combination is invalid: "
"LEAD/LAG supported only on fixed-width types");
}
template <aggregation::Kind op,
typename PrecedingWindowIterator,
typename FollowingWindowIterator>
std::enable_if_t<!(op == aggregation::MEAN || op == aggregation::LEAD || op == aggregation::LAG),
std::unique_ptr<column>>
operator()(column_view const& input,
column_view const& default_outputs,
PrecedingWindowIterator preceding_window_begin,
FollowingWindowIterator following_window_begin,
size_type min_periods,
std::unique_ptr<aggregation> const& agg,
rmm::mr::device_memory_resource* mr,
cudaStream_t stream)
{
CUDF_EXPECTS(default_outputs.is_empty(),
"Only LEAD/LAG window functions support default values.");
return launch<InputType,
typename corresponding_operator<op>::type,
op,
PrecedingWindowIterator,
FollowingWindowIterator>(input,
default_outputs,
preceding_window_begin,
following_window_begin,
min_periods,
agg,
mr,
stream);
}
// This variant is just to handle mean
template <aggregation::Kind op,
typename PrecedingWindowIterator,
typename FollowingWindowIterator>
std::enable_if_t<(op == aggregation::MEAN), std::unique_ptr<column>> operator()(
column_view const& input,
column_view const& default_outputs,
PrecedingWindowIterator preceding_window_begin,
FollowingWindowIterator following_window_begin,
size_type min_periods,
std::unique_ptr<aggregation> const& agg,
rmm::mr::device_memory_resource* mr,
cudaStream_t stream)
{
return launch<InputType, cudf::DeviceSum, op, PrecedingWindowIterator, FollowingWindowIterator>(
input,
default_outputs,
preceding_window_begin,
following_window_begin,
min_periods,
agg,
mr,
stream);
}
template <aggregation::Kind op,
typename PrecedingWindowIterator,
typename FollowingWindowIterator>
std::enable_if_t<(op == aggregation::LEAD || op == aggregation::LAG), std::unique_ptr<column>>
operator()(column_view const& input,
column_view const& default_outputs,
PrecedingWindowIterator preceding_window_begin,
FollowingWindowIterator following_window_begin,
size_type min_periods,
std::unique_ptr<aggregation> const& agg,
rmm::mr::device_memory_resource* mr,
cudaStream_t stream)
{
return launch<InputType,
cudf::DeviceLeadLag,
op,
PrecedingWindowIterator,
FollowingWindowIterator>(
input,
default_outputs,
preceding_window_begin,
following_window_begin,
min_periods,
agg,
cudf::DeviceLeadLag{static_cast<cudf::detail::lead_lag_aggregation*>(agg.get())->row_offset},
mr,
stream);
}
};
struct dispatch_rolling {
template <typename T, typename PrecedingWindowIterator, typename FollowingWindowIterator>
std::unique_ptr<column> operator()(column_view const& input,
column_view const& default_outputs,
PrecedingWindowIterator preceding_window_begin,
FollowingWindowIterator following_window_begin,
size_type min_periods,
std::unique_ptr<aggregation> const& agg,
rmm::mr::device_memory_resource* mr,
cudaStream_t stream)
{
return aggregation_dispatcher(agg->kind,
rolling_window_launcher<T>{},
input,
default_outputs,
preceding_window_begin,
following_window_begin,
min_periods,
agg,
mr,
stream);
}
};
} // namespace
// Applies a user-defined rolling window function to the values in a column.
template <typename PrecedingWindowIterator, typename FollowingWindowIterator>
std::unique_ptr<column> rolling_window_udf(column_view const& input,
PrecedingWindowIterator preceding_window,
std::string const& preceding_window_str,
FollowingWindowIterator following_window,
std::string const& following_window_str,
size_type min_periods,
std::unique_ptr<aggregation> const& agg,
rmm::mr::device_memory_resource* mr,
cudaStream_t stream = 0)
{
static_assert(warp_size == cudf::detail::size_in_bits<cudf::bitmask_type>(),
"bitmask_type size does not match CUDA warp size");
if (input.has_nulls())
CUDF_FAIL("Currently the UDF version of rolling window does NOT support inputs with nulls.");
min_periods = std::max(min_periods, 0);
auto udf_agg = static_cast<udf_aggregation*>(agg.get());
std::string hash = "prog_rolling." + std::to_string(std::hash<std::string>{}(udf_agg->_source));
std::string cuda_source;
switch (udf_agg->kind) {
case aggregation::Kind::PTX:
cuda_source = cudf::rolling::jit::code::kernel_headers;
cuda_source +=
cudf::jit::parse_single_function_ptx(udf_agg->_source,
udf_agg->_function_name,
cudf::jit::get_type_name(udf_agg->_output_type),
{0, 5}); // args 0 and 5 are pointers.
cuda_source += cudf::rolling::jit::code::kernel;
break;
case aggregation::Kind::CUDA:
cuda_source = cudf::rolling::jit::code::kernel_headers;
cuda_source +=
cudf::jit::parse_single_function_cuda(udf_agg->_source, udf_agg->_function_name);
cuda_source += cudf::rolling::jit::code::kernel;
break;
default: CUDF_FAIL("Unsupported UDF type.");
}
std::unique_ptr<column> output = make_numeric_column(
udf_agg->_output_type, input.size(), cudf::mask_state::UNINITIALIZED, stream, mr);
auto output_view = output->mutable_view();
rmm::device_scalar<size_type> device_valid_count{0, stream};
const std::vector<std::string> compiler_flags{"-std=c++14",
// Have jitify prune unused global variables
"-remove-unused-globals",
// suppress all NVRTC warnings
"-w"};
// Launch the jitify kernel
cudf::jit::launcher(hash,
cuda_source,
{cudf_types_hpp,
cudf_utilities_bit_hpp,
cudf::rolling::jit::code::operation_h,
___src_rolling_rolling_jit_detail_hpp},
compiler_flags,
nullptr,
stream)
.set_kernel_inst("gpu_rolling_new", // name of the kernel we are launching
{cudf::jit::get_type_name(input.type()), // list of template arguments
cudf::jit::get_type_name(output->type()),
udf_agg->_operator_name,
preceding_window_str.c_str(),
following_window_str.c_str()})
.launch(input.size(),
cudf::jit::get_data_ptr(input),
input.null_mask(),
cudf::jit::get_data_ptr(output_view),
output_view.null_mask(),
device_valid_count.data(),
preceding_window,
following_window,
min_periods);
output->set_null_count(output->size() - device_valid_count.value(stream));
// check the stream for debugging
CHECK_CUDA(stream);
return output;
}
/**
* @copydoc cudf::rolling_window(column_view const& input,
* PrecedingWindowIterator preceding_window_begin,
* FollowingWindowIterator following_window_begin,
* size_type min_periods,
* std::unique_ptr<aggregation> const& agg,
* rmm::mr::device_memory_resource* mr)
*
* @param stream CUDA stream used for device memory operations and kernel launches.
*/
template <typename PrecedingWindowIterator, typename FollowingWindowIterator>
std::unique_ptr<column> rolling_window(column_view const& input,
column_view const& default_outputs,
PrecedingWindowIterator preceding_window_begin,
FollowingWindowIterator following_window_begin,
size_type min_periods,
std::unique_ptr<aggregation> const& agg,
rmm::mr::device_memory_resource* mr,
cudaStream_t stream = 0)
{
static_assert(warp_size == cudf::detail::size_in_bits<cudf::bitmask_type>(),
"bitmask_type size does not match CUDA warp size");
min_periods = std::max(min_periods, 0);
return cudf::type_dispatcher(input.type(),
dispatch_rolling{},
input,
default_outputs,
preceding_window_begin,
following_window_begin,
min_periods,