-
Notifications
You must be signed in to change notification settings - Fork 915
/
binaryop.cpp
802 lines (721 loc) · 36.4 KB
/
binaryop.cpp
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
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
*
* Copyright 2018-2019 BlazingDB, Inc.
* Copyright 2018 Christian Noboa Mardini <[email protected]>
*
* 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 "compiled/binary_ops.hpp"
#include "jit/code/code.h"
#include "jit/util.hpp"
#include <jit/launcher.h>
#include <jit/parser.h>
#include <jit/type.h>
#include <jit/bit.hpp.jit>
#include <jit/common_headers.hpp>
#include <jit/durations.hpp.jit>
#include <jit/fixed_point.hpp.jit>
#include <jit/timestamps.hpp.jit>
#include <jit/types.hpp.jit>
#include <cudf/binaryop.hpp>
#include <cudf/column/column_factories.hpp>
#include <cudf/detail/binaryop.hpp>
#include <cudf/detail/null_mask.hpp>
#include <cudf/detail/nvtx/ranges.hpp>
#include <cudf/fixed_point/fixed_point.hpp>
#include <cudf/scalar/scalar.hpp>
#include <cudf/scalar/scalar_factories.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
#include <cudf/unary.hpp>
#include <cudf/utilities/error.hpp>
#include <cudf/utilities/traits.hpp>
#include <rmm/cuda_stream_view.hpp>
#include <string>
#include <thrust/optional.h>
namespace cudf {
namespace binops {
namespace detail {
/**
* @brief Computes output valid mask for op between a column and a scalar
*/
rmm::device_buffer scalar_col_valid_mask_and(column_view const& col,
scalar const& s,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
if (col.is_empty()) return rmm::device_buffer{0, stream, mr};
if (not s.is_valid()) {
return cudf::detail::create_null_mask(col.size(), mask_state::ALL_NULL, stream, mr);
} else if (s.is_valid() and col.nullable()) {
return cudf::detail::copy_bitmask(col, stream, mr);
} else {
return rmm::device_buffer{0, stream, mr};
}
}
} // namespace detail
namespace jit {
const std::string hash = "prog_binop";
const std::vector<std::string> header_names{"operation.h",
"traits.h",
cudf_types_hpp,
cudf_utilities_bit_hpp,
cudf_wrappers_timestamps_hpp,
cudf_wrappers_durations_hpp,
cudf_fixed_point_fixed_point_hpp};
std::istream* headers_code(std::string filename, std::iostream& stream)
{
if (filename == "operation.h") {
stream << code::operation;
return &stream;
}
if (filename == "traits.h") {
stream << code::traits;
return &stream;
}
auto it = cudf::jit::stringified_headers.find(filename);
if (it != cudf::jit::stringified_headers.end()) {
return cudf::jit::send_stringified_header(stream, it->second);
}
return nullptr;
}
void binary_operation(mutable_column_view& out,
scalar const& lhs,
column_view const& rhs,
binary_operator op,
rmm::cuda_stream_view stream)
{
if (is_null_dependent(op)) {
cudf::jit::launcher(
hash, code::kernel, header_names, cudf::jit::compiler_flags, headers_code, stream)
.set_kernel_inst("kernel_v_s_with_validity", // name of the kernel we are
// launching
{cudf::jit::get_type_name(out.type()), // list of template arguments
cudf::jit::get_type_name(rhs.type()),
cudf::jit::get_type_name(lhs.type()),
get_operator_name(op, OperatorType::Reverse)})
.launch(out.size(),
cudf::jit::get_data_ptr(out),
cudf::jit::get_data_ptr(rhs),
cudf::jit::get_data_ptr(lhs),
out.null_mask(),
rhs.null_mask(),
rhs.offset(),
lhs.is_valid());
} else {
cudf::jit::launcher(
hash, code::kernel, header_names, cudf::jit::compiler_flags, headers_code, stream)
.set_kernel_inst("kernel_v_s", // name of the kernel we are
// launching
{cudf::jit::get_type_name(out.type()), // list of template arguments
cudf::jit::get_type_name(rhs.type()),
cudf::jit::get_type_name(lhs.type()),
get_operator_name(op, OperatorType::Reverse)})
.launch(out.size(),
cudf::jit::get_data_ptr(out),
cudf::jit::get_data_ptr(rhs),
cudf::jit::get_data_ptr(lhs));
}
}
void binary_operation(mutable_column_view& out,
column_view const& lhs,
scalar const& rhs,
binary_operator op,
rmm::cuda_stream_view stream)
{
if (is_null_dependent(op)) {
cudf::jit::launcher(
hash, code::kernel, header_names, cudf::jit::compiler_flags, headers_code, stream)
.set_kernel_inst("kernel_v_s_with_validity", // name of the kernel we are
// launching
{cudf::jit::get_type_name(out.type()), // list of template arguments
cudf::jit::get_type_name(lhs.type()),
cudf::jit::get_type_name(rhs.type()),
get_operator_name(op, OperatorType::Direct)})
.launch(out.size(),
cudf::jit::get_data_ptr(out),
cudf::jit::get_data_ptr(lhs),
cudf::jit::get_data_ptr(rhs),
out.null_mask(),
lhs.null_mask(),
lhs.offset(),
rhs.is_valid());
} else {
cudf::jit::launcher(
hash, code::kernel, header_names, cudf::jit::compiler_flags, headers_code, stream)
.set_kernel_inst("kernel_v_s", // name of the kernel we are
// launching
{cudf::jit::get_type_name(out.type()), // list of template arguments
cudf::jit::get_type_name(lhs.type()),
cudf::jit::get_type_name(rhs.type()),
get_operator_name(op, OperatorType::Direct)})
.launch(out.size(),
cudf::jit::get_data_ptr(out),
cudf::jit::get_data_ptr(lhs),
cudf::jit::get_data_ptr(rhs));
}
}
void binary_operation(mutable_column_view& out,
column_view const& lhs,
column_view const& rhs,
binary_operator op,
rmm::cuda_stream_view stream)
{
if (is_null_dependent(op)) {
cudf::jit::launcher(
hash, code::kernel, header_names, cudf::jit::compiler_flags, headers_code, stream)
.set_kernel_inst("kernel_v_v_with_validity", // name of the kernel we are
// launching
{cudf::jit::get_type_name(out.type()), // list of template arguments
cudf::jit::get_type_name(lhs.type()),
cudf::jit::get_type_name(rhs.type()),
get_operator_name(op, OperatorType::Direct)})
.launch(out.size(),
cudf::jit::get_data_ptr(out),
cudf::jit::get_data_ptr(lhs),
cudf::jit::get_data_ptr(rhs),
out.null_mask(),
lhs.null_mask(),
rhs.offset(),
rhs.null_mask(),
rhs.offset());
} else {
cudf::jit::launcher(
hash, code::kernel, header_names, cudf::jit::compiler_flags, headers_code, stream)
.set_kernel_inst("kernel_v_v", // name of the kernel we are
// launching
{cudf::jit::get_type_name(out.type()), // list of template arguments
cudf::jit::get_type_name(lhs.type()),
cudf::jit::get_type_name(rhs.type()),
get_operator_name(op, OperatorType::Direct)})
.launch(out.size(),
cudf::jit::get_data_ptr(out),
cudf::jit::get_data_ptr(lhs),
cudf::jit::get_data_ptr(rhs));
}
}
void binary_operation(mutable_column_view& out,
column_view const& lhs,
column_view const& rhs,
const std::string& ptx,
rmm::cuda_stream_view stream)
{
std::string const output_type_name = cudf::jit::get_type_name(out.type());
std::string ptx_hash =
hash + "." + std::to_string(std::hash<std::string>{}(ptx + output_type_name));
std::string cuda_source =
"\n#include <cudf/types.hpp>\n" +
cudf::jit::parse_single_function_ptx(ptx, "GENERIC_BINARY_OP", output_type_name) + code::kernel;
cudf::jit::launcher(
ptx_hash, cuda_source, header_names, cudf::jit::compiler_flags, headers_code, stream)
.set_kernel_inst("kernel_v_v", // name of the kernel
// we are launching
{output_type_name, // list of template arguments
cudf::jit::get_type_name(lhs.type()),
cudf::jit::get_type_name(rhs.type()),
get_operator_name(binary_operator::GENERIC_BINARY, OperatorType::Direct)})
.launch(out.size(),
cudf::jit::get_data_ptr(out),
cudf::jit::get_data_ptr(lhs),
cudf::jit::get_data_ptr(rhs));
}
} // namespace jit
} // namespace binops
namespace detail {
// There are 3 overloads of each of the following functions:
// - `make_fixed_width_column_for_output`
// - `fixed_point_binary_operation`
// - `binary_operation`
// The overloads are overloaded on the first two parameters of each function:
// - scalar const& lhs, column_view const& rhs,
// - column_view const& lhs, scalar const& rhs
// - column_view const& lhs, column_view const& rhs,
/**
* @brief Helper function for making output column for binary operation
*
* @param lhs Left-hand side `scalar` used in the binary operation
* @param rhs Right-hand side `column_view` used in the binary operation
* @param op `binary_operator` to be used to combine `lhs` and `rhs`
* @param output_type `data_type` of the output column
* @param mr Device memory resource to use for device memory allocation
* @param stream CUDA stream used for device memory operations
* @return std::unique_ptr<column> Output column used for binary operation
*/
std::unique_ptr<column> make_fixed_width_column_for_output(scalar const& lhs,
column_view const& rhs,
binary_operator op,
data_type output_type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
if (binops::is_null_dependent(op)) {
return make_fixed_width_column(output_type, rhs.size(), mask_state::ALL_VALID, stream, mr);
} else {
auto new_mask = binops::detail::scalar_col_valid_mask_and(rhs, lhs, stream, mr);
return make_fixed_width_column(
output_type, rhs.size(), std::move(new_mask), cudf::UNKNOWN_NULL_COUNT, stream, mr);
}
};
/**
* @brief Helper function for making output column for binary operation
*
* @param lhs Left-hand side `column_view` used in the binary operation
* @param rhs Right-hand side `scalar` used in the binary operation
* @param op `binary_operator` to be used to combine `lhs` and `rhs`
* @param output_type `data_type` of the output column
* @param mr Device memory resource to use for device memory allocation
* @param stream CUDA stream used for device memory operations
* @return std::unique_ptr<column> Output column used for binary operation
*/
std::unique_ptr<column> make_fixed_width_column_for_output(column_view const& lhs,
scalar const& rhs,
binary_operator op,
data_type output_type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
if (binops::is_null_dependent(op)) {
return make_fixed_width_column(output_type, lhs.size(), mask_state::ALL_VALID, stream, mr);
} else {
auto new_mask = binops::detail::scalar_col_valid_mask_and(lhs, rhs, stream, mr);
return make_fixed_width_column(
output_type, lhs.size(), std::move(new_mask), cudf::UNKNOWN_NULL_COUNT, stream, mr);
}
};
/**
* @brief Helper function for making output column for binary operation
*
* @param lhs Left-hand side `column_view` used in the binary operation
* @param rhs Right-hand side `column_view` used in the binary operation
* @param op `binary_operator` to be used to combine `lhs` and `rhs`
* @param output_type `data_type` of the output column
* @param mr Device memory resource to use for device memory allocation
* @param stream CUDA stream used for device memory operations
* @return std::unique_ptr<column> Output column used for binary operation
*/
std::unique_ptr<column> make_fixed_width_column_for_output(column_view const& lhs,
column_view const& rhs,
binary_operator op,
data_type output_type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
if (binops::is_null_dependent(op)) {
return make_fixed_width_column(output_type, rhs.size(), mask_state::ALL_VALID, stream, mr);
} else {
auto new_mask = cudf::detail::bitmask_and(table_view({lhs, rhs}), stream, mr);
return make_fixed_width_column(
output_type, lhs.size(), std::move(new_mask), cudf::UNKNOWN_NULL_COUNT, stream, mr);
}
};
/**
* @brief Returns `true` if `binary_operator` `op` is a basic arithmetic binary operation
*/
bool is_basic_arithmetic_binop(binary_operator op)
{
return op == binary_operator::ADD or // operator +
op == binary_operator::SUB or // operator -
op == binary_operator::MUL or // operator *
op == binary_operator::DIV or // operator / using common type of lhs and rhs
op == binary_operator::NULL_MIN or // 2 null = null, 1 null = value, else min
op == binary_operator::NULL_MAX; // 2 null = null, 1 null = value, else max
}
/**
* @brief Returns `true` if `binary_operator` `op` is a comparison binary operation
*/
bool is_comparison_binop(binary_operator op)
{
return op == binary_operator::EQUAL or // operator ==
op == binary_operator::NOT_EQUAL or // operator !=
op == binary_operator::LESS or // operator <
op == binary_operator::GREATER or // operator >
op == binary_operator::LESS_EQUAL or // operator <=
op == binary_operator::GREATER_EQUAL or // operator >=
op == binary_operator::NULL_EQUALS; // 2 null = true; 1 null = false; else ==
}
/**
* @brief Returns `true` if `binary_operator` `op` is supported by `fixed_point`
*/
bool is_supported_fixed_point_binop(binary_operator op)
{
return is_basic_arithmetic_binop(op) or is_comparison_binop(op);
}
/**
* @brief Helper predicate function that identifies if `op` requires scales to be the same
*
* @param op `binary_operator`
* @return true `op` requires scales of lhs and rhs to be the same
* @return false `op` does not require scales of lhs and rhs to be the same
*/
bool is_same_scale_necessary(binary_operator op)
{
return op != binary_operator::MUL && op != binary_operator::DIV;
}
template <typename Lhs, typename Rhs>
void fixed_point_binary_operation_validation(binary_operator op,
Lhs lhs,
Rhs rhs,
thrust::optional<cudf::data_type> output_type = {})
{
CUDF_EXPECTS(is_fixed_point(lhs), "Input must have fixed_point data_type.");
CUDF_EXPECTS(is_fixed_point(rhs), "Input must have fixed_point data_type.");
CUDF_EXPECTS(is_supported_fixed_point_binop(op), "Unsupported fixed_point binary operation");
CUDF_EXPECTS(lhs.id() == rhs.id(), "Data type mismatch");
if (output_type.has_value()) {
if (is_comparison_binop(op))
CUDF_EXPECTS(output_type == cudf::data_type{type_id::BOOL8},
"Comparison operations require boolean output type.");
else
CUDF_EXPECTS(is_fixed_point(output_type.value()),
"fixed_point binary operations require fixed_point output type.");
}
}
/**
* @brief Function to compute binary operation of one `column_view` and one `scalar`
*
* @param lhs Left-hand side `scalar` used in the binary operation
* @param rhs Right-hand side `column_view` used in the binary operation
* @param op `binary_operator` to be used to combine `lhs` and `rhs`
* @param mr Device memory resource to use for device memory allocation
* @param stream CUDA stream used for device memory operations
* @return std::unique_ptr<column> Resulting output column from the binary operation
*/
std::unique_ptr<column> fixed_point_binary_operation(scalar const& lhs,
column_view const& rhs,
binary_operator op,
cudf::data_type output_type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
using namespace numeric;
fixed_point_binary_operation_validation(op, lhs.type(), rhs.type(), output_type);
if (rhs.is_empty())
return make_fixed_width_column_for_output(lhs, rhs, op, output_type, stream, mr);
auto const scale = binary_operation_fixed_point_scale(op, lhs.type().scale(), rhs.type().scale());
auto const type =
is_comparison_binop(op) ? data_type{type_id::BOOL8} : cudf::data_type{rhs.type().id(), scale};
auto out = make_fixed_width_column_for_output(lhs, rhs, op, type, stream, mr);
auto out_view = out->mutable_view();
if (lhs.type().scale() != rhs.type().scale() && is_same_scale_necessary(op)) {
// Adjust scalar/column so they have they same scale
if (rhs.type().scale() < lhs.type().scale()) {
auto const diff = lhs.type().scale() - rhs.type().scale();
if (lhs.type().id() == type_id::DECIMAL32) {
auto const factor = numeric::detail::ipow<int32_t, Radix::BASE_10>(diff);
auto const val = static_cast<fixed_point_scalar<decimal32> const&>(lhs).value();
auto const scale = scale_type{rhs.type().scale()};
auto const scalar = make_fixed_point_scalar<decimal32>(val * factor, scale);
binops::jit::binary_operation(out_view, *scalar, rhs, op, stream);
} else {
CUDF_EXPECTS(lhs.type().id() == type_id::DECIMAL64, "Unexpected DTYPE");
auto const factor = numeric::detail::ipow<int64_t, Radix::BASE_10>(diff);
auto const val = static_cast<fixed_point_scalar<decimal64> const&>(lhs).value();
auto const scale = scale_type{rhs.type().scale()};
auto const scalar = make_fixed_point_scalar<decimal64>(val * factor, scale);
binops::jit::binary_operation(out_view, *scalar, rhs, op, stream);
}
} else {
auto const diff = rhs.type().scale() - lhs.type().scale();
auto const result = [&] {
if (lhs.type().id() == type_id::DECIMAL32) {
auto const factor = numeric::detail::ipow<int32_t, Radix::BASE_10>(diff);
auto const scalar = make_fixed_point_scalar<decimal32>(factor, scale_type{-diff});
return binary_operation(*scalar, rhs, binary_operator::MUL, lhs.type(), stream, mr);
} else {
CUDF_EXPECTS(lhs.type().id() == type_id::DECIMAL64, "Unexpected DTYPE");
auto const factor = numeric::detail::ipow<int64_t, Radix::BASE_10>(diff);
auto const scalar = make_fixed_point_scalar<decimal64>(factor, scale_type{-diff});
return binary_operation(*scalar, rhs, binary_operator::MUL, lhs.type(), stream, mr);
}
}();
binops::jit::binary_operation(out_view, lhs, result->view(), op, stream);
}
} else {
binops::jit::binary_operation(out_view, lhs, rhs, op, stream);
}
return output_type.scale() != scale ? cudf::cast(out_view, output_type) : std::move(out);
}
/**
* @brief Function to compute binary operation of one `column_view` and one `scalar`
*
* @param lhs Left-hand side `column_view` used in the binary operation
* @param rhs Right-hand side `scalar` used in the binary operation
* @param op `binary_operator` to be used to combine `lhs` and `rhs`
* @param mr Device memory resource to use for device memory allocation
* @param stream CUDA stream used for device memory operations
* @return std::unique_ptr<column> Resulting output column from the binary operation
*/
std::unique_ptr<column> fixed_point_binary_operation(column_view const& lhs,
scalar const& rhs,
binary_operator op,
cudf::data_type output_type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
using namespace numeric;
fixed_point_binary_operation_validation(op, lhs.type(), rhs.type(), output_type);
if (lhs.is_empty())
return make_fixed_width_column_for_output(lhs, rhs, op, output_type, stream, mr);
auto const scale = binary_operation_fixed_point_scale(op, lhs.type().scale(), rhs.type().scale());
auto const type =
is_comparison_binop(op) ? data_type{type_id::BOOL8} : cudf::data_type{lhs.type().id(), scale};
auto out = make_fixed_width_column_for_output(lhs, rhs, op, type, stream, mr);
auto out_view = out->mutable_view();
if (lhs.type().scale() != rhs.type().scale() && is_same_scale_necessary(op)) {
// Adjust scalar/column so they have they same scale
if (rhs.type().scale() > lhs.type().scale()) {
auto const diff = rhs.type().scale() - lhs.type().scale();
if (rhs.type().id() == type_id::DECIMAL32) {
auto const factor = numeric::detail::ipow<int32_t, Radix::BASE_10>(diff);
auto const val = static_cast<fixed_point_scalar<decimal32> const&>(rhs).value();
auto const scale = scale_type{lhs.type().scale()};
auto const scalar = make_fixed_point_scalar<decimal32>(val * factor, scale);
binops::jit::binary_operation(out_view, lhs, *scalar, op, stream);
} else {
CUDF_EXPECTS(rhs.type().id() == type_id::DECIMAL64, "Unexpected DTYPE");
auto const factor = numeric::detail::ipow<int64_t, Radix::BASE_10>(diff);
auto const val = static_cast<fixed_point_scalar<decimal64> const&>(rhs).value();
auto const scale = scale_type{rhs.type().scale()};
auto const scalar = make_fixed_point_scalar<decimal64>(val * factor, scale);
binops::jit::binary_operation(out_view, lhs, *scalar, op, stream);
}
} else {
auto const diff = lhs.type().scale() - rhs.type().scale();
auto const result = [&] {
if (rhs.type().id() == type_id::DECIMAL32) {
auto const factor = numeric::detail::ipow<int32_t, Radix::BASE_10>(diff);
auto const scalar = make_fixed_point_scalar<decimal32>(factor, scale_type{-diff});
return binary_operation(*scalar, lhs, binary_operator::MUL, rhs.type(), stream, mr);
} else {
CUDF_EXPECTS(rhs.type().id() == type_id::DECIMAL64, "Unexpected DTYPE");
auto const factor = numeric::detail::ipow<int64_t, Radix::BASE_10>(diff);
auto const scalar = make_fixed_point_scalar<decimal64>(factor, scale_type{-diff});
return binary_operation(*scalar, lhs, binary_operator::MUL, rhs.type(), stream, mr);
}
}();
binops::jit::binary_operation(out_view, result->view(), rhs, op, stream);
}
} else {
binops::jit::binary_operation(out_view, lhs, rhs, op, stream);
}
return output_type.scale() != scale ? cudf::cast(out_view, output_type) : std::move(out);
}
/**
* @brief Function to compute binary operation of two `column_view`s
*
* @param lhs Left-hand side `column_view` used in the binary operation
* @param rhs Right-hand side `column_view` used in the binary operation
* @param op `binary_operator` to be used to combine `lhs` and `rhs`
* @param mr Device memory resource to use for device memory allocation
* @param stream CUDA stream used for device memory operations
* @return std::unique_ptr<column> Resulting output column from the binary operation
*/
std::unique_ptr<column> fixed_point_binary_operation(column_view const& lhs,
column_view const& rhs,
binary_operator op,
cudf::data_type output_type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
using namespace numeric;
fixed_point_binary_operation_validation(op, lhs.type(), rhs.type(), output_type);
if (lhs.is_empty() or rhs.is_empty())
return make_fixed_width_column_for_output(lhs, rhs, op, output_type, stream, mr);
auto const scale = binary_operation_fixed_point_scale(op, lhs.type().scale(), rhs.type().scale());
auto const type =
is_comparison_binop(op) ? data_type{type_id::BOOL8} : cudf::data_type{lhs.type().id(), scale};
auto out = make_fixed_width_column_for_output(lhs, rhs, op, type, stream, mr);
auto out_view = out->mutable_view();
if (lhs.type().scale() != rhs.type().scale() && is_same_scale_necessary(op)) {
if (rhs.type().scale() < lhs.type().scale()) {
auto const diff = lhs.type().scale() - rhs.type().scale();
auto const result = [&] {
if (lhs.type().id() == type_id::DECIMAL32) {
auto const factor = numeric::detail::ipow<int32_t, Radix::BASE_10>(diff);
auto const scalar = make_fixed_point_scalar<decimal32>(factor, scale_type{-diff});
return binary_operation(*scalar, lhs, binary_operator::MUL, rhs.type(), stream, mr);
} else {
CUDF_EXPECTS(lhs.type().id() == type_id::DECIMAL64, "Unexpected DTYPE");
auto const factor = numeric::detail::ipow<int64_t, Radix::BASE_10>(diff);
auto const scalar = make_fixed_point_scalar<decimal64>(factor, scale_type{-diff});
return binary_operation(*scalar, lhs, binary_operator::MUL, rhs.type(), stream, mr);
}
}();
binops::jit::binary_operation(out_view, result->view(), rhs, op, stream);
} else {
auto const diff = rhs.type().scale() - lhs.type().scale();
auto const result = [&] {
if (lhs.type().id() == type_id::DECIMAL32) {
auto const factor = numeric::detail::ipow<int32_t, Radix::BASE_10>(diff);
auto const scalar = make_fixed_point_scalar<decimal32>(factor, scale_type{-diff});
return binary_operation(*scalar, rhs, binary_operator::MUL, lhs.type(), stream, mr);
} else {
CUDF_EXPECTS(lhs.type().id() == type_id::DECIMAL64, "Unexpected DTYPE");
auto const factor = numeric::detail::ipow<int64_t, Radix::BASE_10>(diff);
auto const scalar = make_fixed_point_scalar<decimal64>(factor, scale_type{-diff});
return binary_operation(*scalar, rhs, binary_operator::MUL, lhs.type(), stream, mr);
}
}();
binops::jit::binary_operation(out_view, lhs, result->view(), op, stream);
}
} else {
binops::jit::binary_operation(out_view, lhs, rhs, op, stream);
}
return output_type.scale() != scale ? cudf::cast(out_view, output_type) : std::move(out);
}
std::unique_ptr<column> binary_operation(scalar const& lhs,
column_view const& rhs,
binary_operator op,
data_type output_type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
if (lhs.type().id() == type_id::STRING and rhs.type().id() == type_id::STRING)
return binops::compiled::binary_operation(lhs, rhs, op, output_type, stream, mr);
if (is_fixed_point(lhs.type()) or is_fixed_point(rhs.type()))
return fixed_point_binary_operation(lhs, rhs, op, output_type, stream, mr);
// Check for datatype
CUDF_EXPECTS(is_fixed_width(output_type), "Invalid/Unsupported output datatype");
CUDF_EXPECTS(is_fixed_width(lhs.type()), "Invalid/Unsupported lhs datatype");
CUDF_EXPECTS(is_fixed_width(rhs.type()), "Invalid/Unsupported rhs datatype");
auto out = make_fixed_width_column_for_output(lhs, rhs, op, output_type, stream, mr);
if (rhs.is_empty()) return out;
auto out_view = out->mutable_view();
binops::jit::binary_operation(out_view, lhs, rhs, op, stream);
return out;
}
std::unique_ptr<column> binary_operation(column_view const& lhs,
scalar const& rhs,
binary_operator op,
data_type output_type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
if (lhs.type().id() == type_id::STRING and rhs.type().id() == type_id::STRING)
return binops::compiled::binary_operation(lhs, rhs, op, output_type, stream, mr);
if (is_fixed_point(lhs.type()) or is_fixed_point(rhs.type()))
return fixed_point_binary_operation(lhs, rhs, op, output_type, stream, mr);
// Check for datatype
CUDF_EXPECTS(is_fixed_width(output_type), "Invalid/Unsupported output datatype");
CUDF_EXPECTS(is_fixed_width(lhs.type()), "Invalid/Unsupported lhs datatype");
CUDF_EXPECTS(is_fixed_width(rhs.type()), "Invalid/Unsupported rhs datatype");
auto out = make_fixed_width_column_for_output(lhs, rhs, op, output_type, stream, mr);
if (lhs.is_empty()) return out;
auto out_view = out->mutable_view();
binops::jit::binary_operation(out_view, lhs, rhs, op, stream);
return out;
}
std::unique_ptr<column> binary_operation(column_view const& lhs,
column_view const& rhs,
binary_operator op,
data_type output_type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_EXPECTS(lhs.size() == rhs.size(), "Column sizes don't match");
if (lhs.type().id() == type_id::STRING and rhs.type().id() == type_id::STRING)
return binops::compiled::binary_operation(lhs, rhs, op, output_type, stream, mr);
if (is_fixed_point(lhs.type()) or is_fixed_point(rhs.type()))
return fixed_point_binary_operation(lhs, rhs, op, output_type, stream, mr);
// Check for datatype
CUDF_EXPECTS(is_fixed_width(output_type), "Invalid/Unsupported output datatype");
CUDF_EXPECTS(is_fixed_width(lhs.type()), "Invalid/Unsupported lhs datatype");
CUDF_EXPECTS(is_fixed_width(rhs.type()), "Invalid/Unsupported rhs datatype");
auto out = make_fixed_width_column_for_output(lhs, rhs, op, output_type, stream, mr);
if (lhs.is_empty() or rhs.is_empty()) return out;
auto out_view = out->mutable_view();
binops::jit::binary_operation(out_view, lhs, rhs, op, stream);
return out;
}
std::unique_ptr<column> binary_operation(column_view const& lhs,
column_view const& rhs,
std::string const& ptx,
data_type output_type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
// Check for datatype
auto is_type_supported_ptx = [](data_type type) -> bool {
return is_fixed_width(type) and not is_fixed_point(type) and
type.id() != type_id::INT8; // Numba PTX doesn't support int8
};
CUDF_EXPECTS(is_type_supported_ptx(lhs.type()), "Invalid/Unsupported lhs datatype");
CUDF_EXPECTS(is_type_supported_ptx(rhs.type()), "Invalid/Unsupported rhs datatype");
CUDF_EXPECTS(is_type_supported_ptx(output_type), "Invalid/Unsupported output datatype");
CUDF_EXPECTS((lhs.size() == rhs.size()), "Column sizes don't match");
auto new_mask = bitmask_and(table_view({lhs, rhs}), stream, mr);
auto out = make_fixed_width_column(
output_type, lhs.size(), std::move(new_mask), cudf::UNKNOWN_NULL_COUNT, stream, mr);
// Check for 0 sized data
if (lhs.is_empty() or rhs.is_empty()) return out;
auto out_view = out->mutable_view();
binops::jit::binary_operation(out_view, lhs, rhs, ptx, stream);
return out;
}
} // namespace detail
int32_t binary_operation_fixed_point_scale(binary_operator op,
int32_t left_scale,
int32_t right_scale)
{
CUDF_EXPECTS(cudf::detail::is_supported_fixed_point_binop(op),
"Unsupported fixed_point binary operation.");
if (op == binary_operator::MUL) return left_scale + right_scale;
if (op == binary_operator::DIV) return left_scale - right_scale;
return std::min(left_scale, right_scale);
}
cudf::data_type binary_operation_fixed_point_output_type(binary_operator op,
cudf::data_type const& lhs,
cudf::data_type const& rhs)
{
cudf::detail::fixed_point_binary_operation_validation(op, lhs, rhs);
auto const scale = binary_operation_fixed_point_scale(op, lhs.scale(), rhs.scale());
return cudf::data_type{lhs.id(), scale};
}
std::unique_ptr<column> binary_operation(scalar const& lhs,
column_view const& rhs,
binary_operator op,
data_type output_type,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::binary_operation(lhs, rhs, op, output_type, rmm::cuda_stream_default, mr);
}
std::unique_ptr<column> binary_operation(column_view const& lhs,
scalar const& rhs,
binary_operator op,
data_type output_type,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::binary_operation(lhs, rhs, op, output_type, rmm::cuda_stream_default, mr);
}
std::unique_ptr<column> binary_operation(column_view const& lhs,
column_view const& rhs,
binary_operator op,
data_type output_type,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::binary_operation(lhs, rhs, op, output_type, rmm::cuda_stream_default, mr);
}
std::unique_ptr<column> binary_operation(column_view const& lhs,
column_view const& rhs,
std::string const& ptx,
data_type output_type,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::binary_operation(lhs, rhs, ptx, output_type, rmm::cuda_stream_default, mr);
}
} // namespace cudf