From 28908e4dc21aaa5f120a09d771a41153f133b22f Mon Sep 17 00:00:00 2001 From: Lawrence Mitchell Date: Mon, 18 Nov 2024 16:21:21 +0000 Subject: [PATCH 1/3] Fix integer overflow in compiled binaryop For large columns, the computed stride might end up overflowing size_type. To fix this, use the grid_1d helper. See also #10368. - Closes #17353 --- cpp/src/binaryop/compiled/binary_ops.cuh | 18 +++++++----------- cpp/tests/binaryop/binop-compiled-test.cpp | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/cpp/src/binaryop/compiled/binary_ops.cuh b/cpp/src/binaryop/compiled/binary_ops.cuh index c6af0c3c58a..5c2e4665581 100644 --- a/cpp/src/binaryop/compiled/binary_ops.cuh +++ b/cpp/src/binaryop/compiled/binary_ops.cuh @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -253,16 +254,11 @@ struct binary_op_double_device_dispatcher { template CUDF_KERNEL void for_each_kernel(cudf::size_type size, Functor f) { - int tid = threadIdx.x; - int blkid = blockIdx.x; - int blksz = blockDim.x; - int gridsz = gridDim.x; - - int start = tid + blkid * blksz; - int step = blksz * gridsz; + auto start = cudf::detail::grid_1d::global_thread_id(); + auto const stride = cudf::detail::grid_1d::grid_stride(); #pragma unroll - for (cudf::size_type i = start; i < size; i += step) { + for (auto i = start; i < size; i += stride) { f(i); } } @@ -282,9 +278,9 @@ void for_each(rmm::cuda_stream_view stream, cudf::size_type size, Functor f) int min_grid_size; CUDF_CUDA_TRY( cudaOccupancyMaxPotentialBlockSize(&min_grid_size, &block_size, for_each_kernel)); - // 2 elements per thread. - int const grid_size = util::div_rounding_up_safe(size, 2 * block_size); - for_each_kernel<<>>(size, std::forward(f)); + auto grid = cudf::detail::grid_1d(size, block_size, 2 /* elements_per_thread */); + for_each_kernel<<>>( + size, std::forward(f)); } template diff --git a/cpp/tests/binaryop/binop-compiled-test.cpp b/cpp/tests/binaryop/binop-compiled-test.cpp index 3bd67001c16..87e339a8fdd 100644 --- a/cpp/tests/binaryop/binop-compiled-test.cpp +++ b/cpp/tests/binaryop/binop-compiled-test.cpp @@ -23,9 +23,11 @@ #include #include +#include #include #include #include +#include #include #include @@ -820,4 +822,24 @@ TEST_F(BinaryOperationCompiledTest_NullOpsString, NullMin_Vector_Vector) CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, result->view()); } +TEST(BinaryOperationCompiledTest, LargeColumnNoOverflow) +{ + cudf::size_type num_rows{1799989091}; + auto big = cudf::make_column_from_scalar( + cudf::numeric_scalar>{10, true}, num_rows); + auto small = cudf::make_column_from_scalar( + cudf::numeric_scalar>{1, true}, num_rows); + + auto mask = cudf::binary_operation(big->view(), + small->view(), + cudf::binary_operator::GREATER, + cudf::data_type{cudf::type_id::BOOL8}); + + auto agg = cudf::make_sum_aggregation(); + auto result = + cudf::reduce(mask->view(), *agg, cudf::data_type{cudf::type_to_id()}); + auto got = static_cast*>(result.get())->value(); + EXPECT_EQ(num_rows, got); +} + CUDF_TEST_PROGRAM_MAIN() From 6a33cfd5edcb37093261ed9e445bd13740d9a2a0 Mon Sep 17 00:00:00 2001 From: Lawrence Mitchell Date: Mon, 18 Nov 2024 16:51:12 +0000 Subject: [PATCH 2/3] No need for integer_utils include any more --- cpp/src/binaryop/compiled/binary_ops.cuh | 1 - 1 file changed, 1 deletion(-) diff --git a/cpp/src/binaryop/compiled/binary_ops.cuh b/cpp/src/binaryop/compiled/binary_ops.cuh index 5c2e4665581..06987139188 100644 --- a/cpp/src/binaryop/compiled/binary_ops.cuh +++ b/cpp/src/binaryop/compiled/binary_ops.cuh @@ -22,7 +22,6 @@ #include #include #include -#include #include #include From c6587542a1a45afbbdf89d2faee9a701f3fd1b91 Mon Sep 17 00:00:00 2001 From: Lawrence Mitchell Date: Mon, 18 Nov 2024 18:21:18 +0000 Subject: [PATCH 3/3] Digit separation Co-authored-by: Nghia Truong <7416935+ttnghia@users.noreply.github.com> --- cpp/tests/binaryop/binop-compiled-test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/tests/binaryop/binop-compiled-test.cpp b/cpp/tests/binaryop/binop-compiled-test.cpp index 87e339a8fdd..7cdce1ff735 100644 --- a/cpp/tests/binaryop/binop-compiled-test.cpp +++ b/cpp/tests/binaryop/binop-compiled-test.cpp @@ -824,7 +824,7 @@ TEST_F(BinaryOperationCompiledTest_NullOpsString, NullMin_Vector_Vector) TEST(BinaryOperationCompiledTest, LargeColumnNoOverflow) { - cudf::size_type num_rows{1799989091}; + cudf::size_type num_rows{1'799'989'091}; auto big = cudf::make_column_from_scalar( cudf::numeric_scalar>{10, true}, num_rows); auto small = cudf::make_column_from_scalar(