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

Use offsetalator in cudf::strings::detail::merge #15294

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ add_library(
src/strings/filling/fill.cu
src/strings/filter_chars.cu
src/strings/like.cu
src/strings/merge/merge.cu
src/strings/padding.cu
src/strings/regex/regcomp.cpp
src/strings/regex/regexec.cpp
Expand Down
4 changes: 3 additions & 1 deletion cpp/benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ ConfigureNVBench(HASHING_NVBENCH hashing/hash.cpp)
# ##################################################################################################
# * merge benchmark -------------------------------------------------------------------------------
ConfigureBench(MERGE_BENCH merge/merge.cpp)
ConfigureNVBench(MERGE_NVBENCH merge/merge_structs.cpp merge/merge_lists.cpp)
ConfigureNVBench(
MERGE_NVBENCH merge/merge_lists.cpp merge/merge_structs.cpp merge/merge_strings.cpp
)

# ##################################################################################################
# * null_mask benchmark ---------------------------------------------------------------------------
Expand Down
64 changes: 64 additions & 0 deletions cpp/benchmarks/merge/merge_strings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2024, 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 <benchmarks/common/generate_input.hpp>

#include <cudf/merge.hpp>
#include <cudf/sorting.hpp>
#include <cudf/strings/strings_column_view.hpp>
#include <cudf/utilities/default_stream.hpp>

#include <nvbench/nvbench.cuh>

void nvbench_merge_strings(nvbench::state& state)
{
auto stream = cudf::get_default_stream();

auto const num_rows = static_cast<cudf::size_type>(state.get_int64("num_rows"));
auto const row_width = static_cast<cudf::size_type>(state.get_int64("row_width"));
if (static_cast<std::size_t>(2 * num_rows) * static_cast<std::size_t>(row_width) >=
static_cast<std::size_t>(std::numeric_limits<cudf::size_type>::max())) {
state.skip("Skip benchmarks greater than size_type limit");
}

data_profile const table_profile =
data_profile_builder()
.distribution(cudf::type_id::STRING, distribution_id::NORMAL, 0, row_width)
.no_validity();
auto const source_tables = create_random_table(
{cudf::type_id::STRING, cudf::type_id::STRING}, row_count{num_rows}, table_profile);

auto const sorted_lhs = cudf::sort(cudf::table_view({source_tables->view().column(0)}));
auto const sorted_rhs = cudf::sort(cudf::table_view({source_tables->view().column(1)}));
auto const lhs = sorted_lhs->view().column(0);
auto const rhs = sorted_rhs->view().column(0);

state.set_cuda_stream(nvbench::make_cuda_stream_view(stream.value()));
auto chars_size = cudf::strings_column_view(lhs).chars_size(stream) +
cudf::strings_column_view(rhs).chars_size(stream);
state.add_global_memory_reads<nvbench::int8_t>(chars_size); // all bytes are read
state.add_global_memory_writes<nvbench::int8_t>(chars_size); // all bytes are written

state.exec(nvbench::exec_tag::sync, [&](nvbench::launch& launch) {
[[maybe_unused]] auto result = cudf::merge(
{cudf::table_view({lhs}), cudf::table_view({rhs})}, {0}, {cudf::order::ASCENDING});
});
}

NVBENCH_BENCH(nvbench_merge_strings)
.set_name("merge_strings")
.add_int64_axis("row_width", {32, 64, 128, 256, 512, 1024, 2048, 4096})
.add_int64_axis("num_rows", {4096, 32768, 262144, 2097152, 16777216});
8 changes: 6 additions & 2 deletions cpp/examples/strings/custom_optimized.cu
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,12 @@ std::unique_ptr<cudf::column> redact_strings(cudf::column_view const& names,
redact_kernel<<<blocks, block_size, 0, stream.value()>>>(
*d_names, *d_visibilities, offsets.data(), chars.data());

// create column from offsets and chars vectors (no copy is performed)
auto result = cudf::make_strings_column(names.size(), std::move(offsets), chars.release(), {}, 0);
// create column from offsets vector (move only)
auto offsets_column = std::make_unique<cudf::column>(std::move(offsets), rmm::device_buffer{}, 0);

// create column for chars vector (no copy is performed)
auto result = cudf::make_strings_column(
names.size(), std::move(offsets_column), chars.release(), 0, rmm::device_buffer{});

// wait for all of the above to finish
stream.synchronize();
Expand Down
110 changes: 0 additions & 110 deletions cpp/include/cudf/strings/detail/merge.cuh

This file was deleted.

47 changes: 47 additions & 0 deletions cpp/include/cudf/strings/detail/merge.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2024, 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/column/column.hpp>
#include <cudf/detail/merge.hpp>
#include <cudf/strings/strings_column_view.hpp>

#include <rmm/cuda_stream_view.hpp>

namespace cudf {
namespace strings {
namespace detail {
/**
* @brief Merges two strings columns
*
* Caller must set the validity mask in the output column.
*
* @param lhs First column
* @param rhs Second column
* @param row_order Indexes for each column
* @param stream CUDA stream used for device memory operations and kernel launches
* @param mr Device memory resource used to allocate the returned column's device memory
* @return New strings column
*/
std::unique_ptr<column> merge(strings_column_view const& lhs,
strings_column_view const& rhs,
cudf::detail::index_vector const& row_order,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr);

} // namespace detail
} // namespace strings
} // namespace cudf
17 changes: 12 additions & 5 deletions cpp/include/cudf/strings/detail/strings_children.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,29 @@ std::pair<std::unique_ptr<column>, int64_t> make_offsets_child_column(
});
auto input_itr = cudf::detail::make_counting_transform_iterator(0, map_fn);
// Use the sizes-to-offsets iterator to compute the total number of elements
auto const total_elements =
auto const total_bytes =
cudf::detail::sizes_to_offsets(input_itr, input_itr + strings_count + 1, d_offsets, stream);

// TODO: replace exception with if-statement when enabling creating INT64 offsets
CUDF_EXPECTS(total_elements <= size_type_max,
CUDF_EXPECTS(total_bytes <= size_type_max,
"Size of output exceeds the character size limit",
std::overflow_error);
// if (total_elements >= get_offset64_threshold()) {
// auto const threshold = get_offset64_threshold();
// if (!is_large_strings_enabled()) { implemented in PR 15195
// CUDF_EXPECTS(
// total_bytes < threshold, "Size of output exceeds the column size limit",
// std::overflow_error);
// }
// if (total_bytes >= get_offset64_threshold()) {
// // recompute as int64 offsets when above the threshold
// offsets_column = make_numeric_column(
// data_type{type_id::INT64}, strings_count + 1, mask_state::UNALLOCATED, stream, mr);
// auto d_offsets64 = offsets_column->mutable_view().template data<int64_t>();
// sizes_to_offsets(input_itr, input_itr + strings_count + 1, d_offsets64, stream);
// cudf::detail::sizes_to_offsets(input_itr, input_itr + strings_count + 1, d_offsets64,
// stream);
// }

return std::pair(std::move(offsets_column), total_elements);
return std::pair(std::move(offsets_column), total_bytes);
}

} // namespace detail
Expand Down
10 changes: 3 additions & 7 deletions cpp/src/merge/merge.cu
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <cudf/dictionary/detail/update_keys.hpp>
#include <cudf/lists/detail/concatenate.hpp>
#include <cudf/lists/lists_column_view.hpp>
#include <cudf/strings/detail/merge.cuh>
#include <cudf/strings/detail/merge.hpp>
#include <cudf/structs/structs_column_view.hpp>
#include <cudf/table/experimental/row_operators.cuh>
#include <cudf/table/table.hpp>
Expand Down Expand Up @@ -433,12 +433,8 @@ std::unique_ptr<column> column_merger::operator()<cudf::string_view>(
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr) const
{
auto column = strings::detail::merge<index_type>(strings_column_view(lcol),
strings_column_view(rcol),
row_order_.begin(),
row_order_.end(),
stream,
mr);
auto column = strings::detail::merge(
strings_column_view(lcol), strings_column_view(rcol), row_order_, stream, mr);
if (lcol.has_nulls() || rcol.has_nulls()) {
auto merged_view = column->mutable_view();
materialize_bitmask(
Expand Down
74 changes: 74 additions & 0 deletions cpp/src/strings/merge/merge.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2019-2024, 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/column/column_device_view.cuh>
#include <cudf/strings/detail/merge.hpp>
#include <cudf/strings/detail/strings_column_factories.cuh>
#include <cudf/strings/string_view.cuh>
#include <cudf/strings/strings_column_view.hpp>

#include <rmm/cuda_stream_view.hpp>
#include <rmm/exec_policy.hpp>

#include <cuda/functional>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/transform.h>
#include <thrust/tuple.h>

namespace cudf {
namespace strings {
namespace detail {
std::unique_ptr<column> merge(strings_column_view const& lhs,
strings_column_view const& rhs,
cudf::detail::index_vector const& row_order,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
using cudf::detail::side;
if (row_order.is_empty()) { return make_empty_column(type_id::STRING); }
auto const strings_count = static_cast<cudf::size_type>(row_order.size());

auto const lhs_column = column_device_view::create(lhs.parent(), stream);
auto const d_lhs = *lhs_column;
auto const rhs_column = column_device_view::create(rhs.parent(), stream);
auto const d_rhs = *rhs_column;

auto const begin = row_order.begin();

// build vector of strings
rmm::device_uvector<string_index_pair> indices(strings_count, stream);
thrust::transform(rmm::exec_policy_nosync(stream),
thrust::make_counting_iterator<size_type>(0),
thrust::make_counting_iterator<size_type>(strings_count),
indices.begin(),
[d_lhs, d_rhs, begin] __device__(size_type idx) {
auto const [side, index] = begin[idx];
if (side == side::LEFT ? d_lhs.is_null(index) : d_rhs.is_null(index)) {
return string_index_pair{nullptr, 0};
}
auto d_str = side == side::LEFT ? d_lhs.element<string_view>(index)
: d_rhs.element<string_view>(index);
return string_index_pair{d_str.data(), d_str.size_bytes()};
});

// convert vector into strings column
return make_strings_column(indices.begin(), indices.end(), stream, mr);
}

} // namespace detail
} // namespace strings
} // namespace cudf
Loading
Loading