Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REVIEW] Cross join feature. #5327

Merged
merged 15 commits into from
Jun 2, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## New Features

- PR #5222 Adding clip feature support to DataFrame and Series
- PR #5327 Add `cudf::cross_join` feature

## Improvements
- PR #5245 Add column reduction benchmark
Expand Down
1 change: 1 addition & 0 deletions conda/recipes/libcudf/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ test:
- test -f $PREFIX/include/cudf/detail/reduction_functions.hpp
- test -f $PREFIX/include/cudf/detail/repeat.hpp
- test -f $PREFIX/include/cudf/detail/replace.hpp
- test -f $PREFIX/include/cudf/detail/reshape.hpp
- test -f $PREFIX/include/cudf/detail/scatter.hpp
- test -f $PREFIX/include/cudf/detail/search.hpp
- test -f $PREFIX/include/cudf/detail/sequence.hpp
Expand Down
1 change: 1 addition & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ add_library(cudf
src/merge/merge.cu
src/partitioning/round_robin.cu
src/join/join.cu
src/join/cross_join.cu
src/join/semi_join.cu
src/sort/is_sorted.cu
src/binaryop/binaryop.cpp
Expand Down
35 changes: 35 additions & 0 deletions cpp/include/cudf/detail/reshape.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 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.
*/

#pragma once

#include <cudf/types.hpp>

#include <memory>

namespace cudf {
namespace detail {
/**
* @copydoc cudf::tile
*
* @param stream CUDA stream used for device memory operations and kernel launches.
*/
std::unique_ptr<table> tile(table_view const& input,
size_type count,
rmm::mr::device_memory_resource* mr = rmm::mr::get_default_resource(),
cudaStream_t stream = 0);
} // namespace detail
} // namespace cudf
21 changes: 21 additions & 0 deletions cpp/include/cudf/join.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,26 @@ std::unique_ptr<cudf::table> left_anti_join(
std::vector<cudf::size_type> const& return_columns,
rmm::mr::device_memory_resource* mr = rmm::mr::get_default_resource());

/**
* @brief Performs a cross join on two tables (left, right)
*
* The cross join returns the cartesian product of rows from each table.
bdice marked this conversation as resolved.
Show resolved Hide resolved
*
jrhemstad marked this conversation as resolved.
Show resolved Hide resolved
* The approach is to repeat the left table by the number of rows in the right table
bdice marked this conversation as resolved.
Show resolved Hide resolved
* and tile the right table by the number of rows in the left table.
*
* @throws cudf::logic_error if number of columns in either `left` or `right` table is 0
bdice marked this conversation as resolved.
Show resolved Hide resolved
*
* @param[in] left The left table
* @param[in] right The right table
* @param[in] mr Device memory resource to use for device memory allocation
bdice marked this conversation as resolved.
Show resolved Hide resolved
*
* @returns Result of cross joining `left` and `right` tables
bdice marked this conversation as resolved.
Show resolved Hide resolved
*/
std::unique_ptr<cudf::table> cross_join(
cudf::table_view const& left,
cudf::table_view const& right,
bdice marked this conversation as resolved.
Show resolved Hide resolved
rmm::mr::device_memory_resource* mr = rmm::mr::get_default_resource());

/** @} */ // end of group
} // namespace cudf
94 changes: 94 additions & 0 deletions cpp/src/join/cross_join.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 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/column/column.hpp>
#include <cudf/copying.hpp>
#include <cudf/detail/nvtx/ranges.hpp>
#include <cudf/detail/repeat.hpp>
#include <cudf/detail/reshape.hpp>
#include <cudf/filling.hpp>
#include <cudf/join.hpp>
#include <cudf/reshape.hpp>
#include <cudf/table/table.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/error.hpp>

namespace cudf {
namespace detail {
/**
* @brief Performs a cross join on two tables (left, right)
*
* The cross join returns the cartesian product of rows from each table.
*
* The approach is to repeat the left table by the number of rows in the right table
* and tile the right table by the number of rows in the left table.
*
* @throws cudf::logic_error if number of columns in either `left` or `right` table is 0
bdice marked this conversation as resolved.
Show resolved Hide resolved
*
* @param[in] left The left table
* @param[in] right The right table
* @param[in] mr Device memory resource to use for device memory allocation
* @param[in] stream Cuda stream
*
* @returns Result of cross joining `left` and `right` tables
*/
std::unique_ptr<cudf::table> cross_join(
cudf::table_view const& left,
cudf::table_view const& right,
rmm::mr::device_memory_resource* mr = rmm::mr::get_default_resource(),
cudaStream_t stream = 0)
bdice marked this conversation as resolved.
Show resolved Hide resolved
{
CUDF_EXPECTS(0 != left.num_columns(), "Left table is empty");
CUDF_EXPECTS(0 != right.num_columns(), "Right table is empty");

// If left or right table has no rows, return an empty table with all columns
if ((0 == left.num_rows()) || (0 == right.num_rows())) {
auto empty_left_columns = empty_like(left)->release();
auto empty_right_columns = empty_like(right)->release();
std::move(empty_right_columns.begin(),
bdice marked this conversation as resolved.
Show resolved Hide resolved
empty_right_columns.end(),
std::back_inserter(empty_left_columns));
return std::make_unique<table>(std::move(empty_left_columns));
}

// Repeat left table
numeric_scalar<size_type> num_repeats = right.num_rows();
auto left_repeated = detail::repeat(left, num_repeats, mr, stream);

// Tile right table
auto right_tiled = detail::tile(right, left.num_rows(), mr, stream);

// Concatenate all repeated/tiled columns into one table
auto left_repeated_columns = left_repeated->release();
auto right_tiled_columns = right_tiled->release();
std::move(right_tiled_columns.begin(),
right_tiled_columns.end(),
std::back_inserter(left_repeated_columns));

return std::make_unique<table>(std::move(left_repeated_columns));
}
} // namespace detail

std::unique_ptr<cudf::table> cross_join(cudf::table_view const& left,
cudf::table_view const& right,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::cross_join(left, right, mr, 0);
}

} // namespace cudf
16 changes: 16 additions & 0 deletions cpp/src/join/semi_join.cu
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 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/column/column_factories.hpp>
#include <cudf/copying.hpp>
#include <cudf/detail/gather.hpp>
Expand Down
17 changes: 14 additions & 3 deletions cpp/src/reshape/tile.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <cudf/copying.hpp>
#include <cudf/detail/nvtx/ranges.hpp>
#include <cudf/detail/reshape.hpp>
#include <cudf/table/table.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/error.hpp>
Expand All @@ -35,11 +36,12 @@ struct tile_functor {

} // anonymous namespace

namespace detail {
std::unique_ptr<table> tile(const table_view &in,
size_type count,
rmm::mr::device_memory_resource *mr)
rmm::mr::device_memory_resource *mr,
cudaStream_t stream)
{
CUDF_FUNC_RANGE();
CUDF_EXPECTS(count >= 0, "Count cannot be negative");

auto in_num_rows = in.num_rows();
Expand All @@ -50,7 +52,16 @@ std::unique_ptr<table> tile(const table_view &in,
auto counting_it = thrust::make_counting_iterator<size_type>(0);
auto tiled_it = thrust::make_transform_iterator(counting_it, tile_functor{in_num_rows});

return detail::gather(in, tiled_it, tiled_it + out_num_rows, false, mr);
return detail::gather(in, tiled_it, tiled_it + out_num_rows, false, mr, stream);
}
} // namespace detail

std::unique_ptr<table> tile(const table_view &in,
size_type count,
rmm::mr::device_memory_resource *mr)
{
CUDF_FUNC_RANGE();
return detail::tile(in, count, mr, 0);
}

} // namespace cudf
1 change: 1 addition & 0 deletions cpp/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ ConfigureTest(GROUPBY_TEST "${GROUPBY_TEST_SRC}")

set(JOIN_TEST_SRC
"${CMAKE_CURRENT_SOURCE_DIR}/join/join_tests.cu"
"${CMAKE_CURRENT_SOURCE_DIR}/join/cross_join_tests.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/join/semi_join_tests.cpp")

ConfigureTest(JOIN_TEST "${JOIN_TEST_SRC}")
Expand Down
Loading