Skip to content

Commit

Permalink
Update hash_partition to use experimental::row::row_hasher (#12761)
Browse files Browse the repository at this point in the history
This PR is a part of #11844.

Authors:
  - Divye Gala (https://github.com/divyegala)

Approvers:
  - Nghia Truong (https://github.com/ttnghia)
  - Yunsong Wang (https://github.com/PointKernel)

URL: #12761
  • Loading branch information
divyegala committed Feb 17, 2023
1 parent 4e32bfe commit 79a924a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
14 changes: 7 additions & 7 deletions cpp/src/partitioning/partitioning.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2022, NVIDIA CORPORATION.
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,7 @@
#include <cudf/detail/utilities/hash_functions.cuh>
#include <cudf/detail/utilities/vector_factories.hpp>
#include <cudf/partitioning.hpp>
#include <cudf/table/row_operators.cuh>
#include <cudf/table/experimental/row_operators.cuh>
#include <cudf/table/table_device_view.cuh>
#include <cudf/utilities/default_stream.hpp>

Expand Down Expand Up @@ -489,9 +489,9 @@ std::pair<std::unique_ptr<table>, std::vector<size_type>> hash_partition_table(
auto row_partition_offset =
cudf::detail::make_zeroed_device_uvector_async<size_type>(num_rows, stream);

auto const device_input = table_device_view::create(table_to_hash, stream);
auto const hasher = row_hasher<hash_function, nullate::DYNAMIC>(
nullate::DYNAMIC{hash_has_nulls}, *device_input, seed);
auto const row_hasher = experimental::row::hash::row_hasher(table_to_hash, stream);
auto const hasher =
row_hasher.device_hasher<hash_function>(nullate::DYNAMIC{hash_has_nulls}, seed);

// If the number of partitions is a power of two, we can compute the partition
// number of each row more efficiently with bitwise operations
Expand Down Expand Up @@ -578,7 +578,7 @@ std::pair<std::unique_ptr<table>, std::vector<size_type>> hash_partition_table(
mr);
});

if (has_nulls(input)) {
if (has_nested_nulls(input)) {
// Use copy_block_partitions to compute a gather map
auto gather_map = compute_gather_map(num_rows,
num_partitions,
Expand Down Expand Up @@ -730,7 +730,7 @@ std::pair<std::unique_ptr<table>, std::vector<size_type>> hash_partition(
return std::pair(empty_like(input), std::vector<size_type>(num_partitions, 0));
}

if (has_nulls(table_to_hash)) {
if (has_nested_nulls(table_to_hash)) {
return hash_partition_table<hash_function, true>(
input, table_to_hash, num_partitions, seed, stream, mr);
} else {
Expand Down
79 changes: 78 additions & 1 deletion cpp/tests/partitioning/hash_partition_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>

Expand All @@ -29,6 +30,10 @@

using cudf::test::fixed_width_column_wrapper;
using cudf::test::strings_column_wrapper;
using structs_col = cudf::test::structs_column_wrapper;

using cudf::test::iterators::null_at;
using cudf::test::iterators::nulls_at;

// Transform vector of column wrappers to vector of column views
template <typename T>
Expand Down Expand Up @@ -361,4 +366,76 @@ TEST_F(HashPartition, FixedPointColumnsToHash)
CUDF_TEST_EXPECT_COLUMNS_EQUAL(first_result->get_column(1).view(), first_input.column(1));
}

TEST_F(HashPartition, ListWithNulls)
{
using lcw = cudf::test::lists_column_wrapper<int32_t>;

lcw to_hash{{{1}, {2, 2}, {0}, {1}, {2, 2}, {0}}, nulls_at({2, 5})};
fixed_width_column_wrapper<int32_t> first_col({7, 8, 9, 10, 11, 12});
fixed_width_column_wrapper<int32_t> second_col({13, 14, 15, 16, 17, 18});
auto const first_input = cudf::table_view({to_hash, first_col});
auto const second_input = cudf::table_view({to_hash, second_col});

auto const columns_to_hash = std::vector<cudf::size_type>({0});

cudf::size_type const num_partitions = 3;
auto const [first_result, first_offsets] =
cudf::hash_partition(first_input, columns_to_hash, num_partitions);
auto const [second_result, second_offsets] =
cudf::hash_partition(second_input, columns_to_hash, num_partitions);

// Expect offsets to be equal and num_partitions in length
EXPECT_EQ(static_cast<size_t>(num_partitions), first_offsets.size());
EXPECT_TRUE(std::equal(
first_offsets.begin(), first_offsets.end(), second_offsets.begin(), second_offsets.end()));

// Expect same result for the hashed columns
CUDF_TEST_EXPECT_COLUMNS_EQUAL(first_result->get_column(0).view(),
second_result->get_column(0).view());
}

TEST_F(HashPartition, StructofStructWithNulls)
{
// +-----------------+
// | s1{s2{a,b}, c} |
// +-----------------+
// 0 | { {1, 1}, 5} |
// 1 | { {1, 1}, 5} |
// 2 | { {1, 2}, 4} |
// 3 | Null |
// 4 | { Null, 4} |
// 5 | { Null, 4} |

auto const to_hash = [&] {
auto a = fixed_width_column_wrapper<int32_t>{1, 1, 1, 0, 0, 0};
auto b = fixed_width_column_wrapper<int32_t>{1, 1, 2, 0, 0, 0};
auto s2 = structs_col{{a, b}, nulls_at({4, 5})};

auto c = fixed_width_column_wrapper<int32_t>{5, 5, 4, 0, 4, 4};
return structs_col({s2, c}, null_at(3));
}();

fixed_width_column_wrapper<int32_t> first_col({7, 8, 9, 10, 11, 12});
fixed_width_column_wrapper<int32_t> second_col({13, 14, 15, 16, 17, 18});
auto const first_input = cudf::table_view({to_hash, first_col});
auto const second_input = cudf::table_view({to_hash, second_col});

auto const columns_to_hash = std::vector<cudf::size_type>({0});

cudf::size_type const num_partitions = 3;
auto const [first_result, first_offsets] =
cudf::hash_partition(first_input, columns_to_hash, num_partitions);
auto const [second_result, second_offsets] =
cudf::hash_partition(second_input, columns_to_hash, num_partitions);

// Expect offsets to be equal and num_partitions in length
EXPECT_EQ(static_cast<size_t>(num_partitions), first_offsets.size());
EXPECT_TRUE(std::equal(
first_offsets.begin(), first_offsets.end(), second_offsets.begin(), second_offsets.end()));

// Expect same result for the hashed columns
CUDF_TEST_EXPECT_COLUMNS_EQUAL(first_result->get_column(0).view(),
second_result->get_column(0).view());
}

CUDF_TEST_PROGRAM_MAIN()

0 comments on commit 79a924a

Please sign in to comment.