|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include <cudf/column/column_view.hpp> |
| 7 | +#include <cudf/detail/algorithms/copy_if.cuh> |
| 8 | +#include <cudf/detail/nvtx/ranges.hpp> |
| 9 | +#include <cudf/join/direct_join.hpp> |
| 10 | +#include <cudf/join/join.hpp> |
| 11 | +#include <cudf/types.hpp> |
| 12 | +#include <cudf/utilities/error.hpp> |
| 13 | +#include <cudf/utilities/memory_resource.hpp> |
| 14 | + |
| 15 | +#include <rmm/cuda_stream_view.hpp> |
| 16 | +#include <rmm/device_uvector.hpp> |
| 17 | + |
| 18 | +#include <cub/device/device_for.cuh> |
| 19 | +#include <cub/device/device_transform.cuh> |
| 20 | +#include <cuda/iterator> |
| 21 | +#include <cuda/std/iterator> |
| 22 | + |
| 23 | +#include <cstdint> |
| 24 | +#include <memory> |
| 25 | +#include <utility> |
| 26 | + |
| 27 | +namespace cudf { |
| 28 | +namespace detail { |
| 29 | +namespace { |
| 30 | + |
| 31 | +// Scatters each right row index to the lookup slot addressed by its key value |
| 32 | +struct scatter_right_index { |
| 33 | + size_type* lookup; |
| 34 | + std::uint32_t const* right_keys; |
| 35 | + |
| 36 | + __device__ void operator()(size_type right_idx) const |
| 37 | + { |
| 38 | + lookup[right_keys[right_idx]] = right_idx; |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +// Writes the (left, right) index pair of the `out_idx`-th match, given a matched left row index |
| 43 | +struct emit_match_pair { |
| 44 | + size_type* left_out; |
| 45 | + size_type* right_out; |
| 46 | + size_type const* lookup; |
| 47 | + std::uint32_t const* left_keys; |
| 48 | + |
| 49 | + __device__ void operator()(size_type out_idx, size_type left_idx) const |
| 50 | + { |
| 51 | + left_out[out_idx] = left_idx; |
| 52 | + right_out[out_idx] = lookup[left_keys[left_idx]]; |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +// Returns true if the left row's key hits a right row in the lookup table |
| 57 | +struct is_match { |
| 58 | + size_type const* lookup; |
| 59 | + std::uint32_t const* left_keys; |
| 60 | + |
| 61 | + __device__ bool operator()(size_type left_idx) const |
| 62 | + { |
| 63 | + return lookup[left_keys[left_idx]] != JoinNoMatch; |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +} // namespace |
| 68 | + |
| 69 | +std::pair<std::unique_ptr<rmm::device_uvector<size_type>>, |
| 70 | + std::unique_ptr<rmm::device_uvector<size_type>>> |
| 71 | +direct_inner_join(column_view const& left_keys, |
| 72 | + column_view const& right_keys, |
| 73 | + std::size_t capacity, |
| 74 | + rmm::cuda_stream_view stream, |
| 75 | + rmm::device_async_resource_ref mr) |
| 76 | +{ |
| 77 | + CUDF_EXPECTS( |
| 78 | + left_keys.type().id() == type_id::UINT32 and right_keys.type().id() == type_id::UINT32, |
| 79 | + "direct_inner_join keys must be of type UINT32", |
| 80 | + cudf::data_type_error); |
| 81 | + CUDF_EXPECTS(not left_keys.has_nulls() and not right_keys.has_nulls(), |
| 82 | + "direct_inner_join keys must not contain nulls", |
| 83 | + std::invalid_argument); |
| 84 | + CUDF_EXPECTS(static_cast<std::size_t>(right_keys.size()) <= capacity, |
| 85 | + "capacity must be at least the number of right keys", |
| 86 | + std::invalid_argument); |
| 87 | + |
| 88 | + if (left_keys.is_empty() or right_keys.is_empty()) { |
| 89 | + return std::pair(std::make_unique<rmm::device_uvector<size_type>>(0, stream, mr), |
| 90 | + std::make_unique<rmm::device_uvector<size_type>>(0, stream, mr)); |
| 91 | + } |
| 92 | + |
| 93 | + // Build: scatter each right row index to the slot addressed by its key value |
| 94 | + auto lookup = |
| 95 | + rmm::device_uvector<size_type>(capacity, stream, cudf::get_current_device_resource_ref()); |
| 96 | + CUDF_CUDA_TRY( |
| 97 | + cub::DeviceTransform::Fill(lookup.begin(), lookup.size(), JoinNoMatch, stream.value())); |
| 98 | + CUDF_CUDA_TRY( |
| 99 | + cub::DeviceFor::Bulk(right_keys.size(), |
| 100 | + scatter_right_index{lookup.data(), right_keys.begin<std::uint32_t>()}, |
| 101 | + stream.value())); |
| 102 | + |
| 103 | + // Probe: a single pass emitting the (left index, matched right index) pairs |
| 104 | + auto left_indices = |
| 105 | + std::make_unique<rmm::device_uvector<size_type>>(left_keys.size(), stream, mr); |
| 106 | + auto right_indices = |
| 107 | + std::make_unique<rmm::device_uvector<size_type>>(left_keys.size(), stream, mr); |
| 108 | + |
| 109 | + auto const d_left_keys = left_keys.begin<std::uint32_t>(); |
| 110 | + auto const out_iter = cuda::tabulate_output_iterator{ |
| 111 | + emit_match_pair{left_indices->data(), right_indices->data(), lookup.data(), d_left_keys}}; |
| 112 | + |
| 113 | + auto const out_end = cudf::detail::copy_if(cuda::counting_iterator<size_type>{0}, |
| 114 | + cuda::counting_iterator<size_type>{left_keys.size()}, |
| 115 | + out_iter, |
| 116 | + is_match{lookup.data(), d_left_keys}, |
| 117 | + stream); |
| 118 | + |
| 119 | + auto const num_matches = cuda::std::distance(out_iter, out_end); |
| 120 | + left_indices->resize(num_matches, stream); |
| 121 | + right_indices->resize(num_matches, stream); |
| 122 | + |
| 123 | + return std::pair(std::move(left_indices), std::move(right_indices)); |
| 124 | +} |
| 125 | + |
| 126 | +} // namespace detail |
| 127 | + |
| 128 | +std::pair<std::unique_ptr<rmm::device_uvector<size_type>>, |
| 129 | + std::unique_ptr<rmm::device_uvector<size_type>>> |
| 130 | +direct_inner_join(column_view const& left_keys, |
| 131 | + column_view const& right_keys, |
| 132 | + std::size_t capacity, |
| 133 | + rmm::cuda_stream_view stream, |
| 134 | + rmm::device_async_resource_ref mr) |
| 135 | +{ |
| 136 | + CUDF_FUNC_RANGE(); |
| 137 | + return detail::direct_inner_join(left_keys, right_keys, capacity, stream, mr); |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace cudf |
0 commit comments