Skip to content

Commit

Permalink
Convert cudf::concatenate APIs to use spans and device_uvector (#7621)
Browse files Browse the repository at this point in the history
Contributes to #7287

This PR replaces `std::vector` with `host_span` in public and detail `cudf::contatenate` functions, and replaces `rmm::device_vector` with `rmm::device_uvector` in the concatenate implementations.

It also strengthens the SFINAE restrictions on `cudf::host_span` and `cudf::device_span` so that they cannot be constructed from containers unless the container's value_type is the same as the span's value_type.

This PR also
 - [x] Updates cython.
 - [x] benchmarks before and after

Authors:
  - Mark Harris (@harrism)

Approvers:
  - Jake Hemstad (@jrhemstad)
  - Vukasin Milovanovic (@vuule)
  - Ashwin Srinath (@shwina)

URL: #7621
  • Loading branch information
harrism authored Mar 25, 2021
1 parent 3136124 commit b854598
Show file tree
Hide file tree
Showing 25 changed files with 282 additions and 200 deletions.
1 change: 0 additions & 1 deletion cpp/include/cudf/column/column_factories.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <cudf/utilities/traits.hpp>

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

namespace cudf {
/**
Expand Down
18 changes: 8 additions & 10 deletions cpp/include/cudf/concatenate.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
* Copyright (c) 2020-2021, 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 @@ -17,9 +17,9 @@

#include <cudf/column/column_view.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/utilities/span.hpp>

#include <memory>
#include <vector>

namespace cudf {
/**
Expand All @@ -36,13 +36,13 @@ namespace cudf {
*
* Returns empty `device_buffer` if the column is not nullable
*
* @param views Vector of column views whose bitmask will to be concatenated
* @param views host_span of column views whose bitmask will to be concatenated
* @param mr Device memory resource used for allocating the new device_buffer
* @return rmm::device_buffer A `device_buffer` containing the bitmasks of all
* the column views in the views vector
*/
rmm::device_buffer concatenate_masks(
std::vector<column_view> const& views,
host_span<column_view const> views,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
Expand All @@ -51,14 +51,13 @@ rmm::device_buffer concatenate_masks(
* @throws cudf::logic_error
* If types of the input columns mismatch
*
* @param columns_to_concat The column views to be concatenated into a single
* column
* @param columns_to_concat host_span of column views to be concatenated into a single column
* @param mr Device memory resource used to allocate the returned column's device memory.
* @return Unique pointer to a single table having all the rows from the
* elements of `columns_to_concat` respectively in the same order.
*/
std::unique_ptr<column> concatenate(
std::vector<column_view> const& columns_to_concat,
host_span<column_view const> columns_to_concat,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
Expand All @@ -82,14 +81,13 @@ std::unique_ptr<column> concatenate(
* @throws cudf::logic_error
* If number of columns mismatch
*
* @param tables_to_concat The table views to be concatenated into a single
* table
* @param tables_to_concat host_span of table views to be concatenated into a single table
* @param mr Device memory resource used to allocate the returned table's device memory.
* @return Unique pointer to a single table having all the rows from the
* elements of `tables_to_concat` respectively in the same order.
*/
std::unique_ptr<table> concatenate(
std::vector<table_view> const& tables_to_concat,
host_span<table_view const> tables_to_concat,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/** @} */ // end of group
Expand Down
9 changes: 5 additions & 4 deletions cpp/include/cudf/detail/concatenate.cuh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
* Copyright (c) 2020-2021, 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 @@ -20,6 +20,7 @@
#include <cudf/concatenate.hpp>
#include <cudf/detail/concatenate.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/utilities/span.hpp>

#include <rmm/cuda_stream_view.hpp>

Expand All @@ -34,8 +35,8 @@ namespace detail {
*
* @param stream CUDA stream used for device memory operations and kernel launches.
*/
void concatenate_masks(rmm::device_vector<column_device_view> const& d_views,
rmm::device_vector<size_t> const& d_offsets,
void concatenate_masks(device_span<column_device_view const> d_views,
device_span<size_t const> d_offsets,
bitmask_type* dest_mask,
size_type output_size,
rmm::cuda_stream_view stream);
Expand All @@ -45,7 +46,7 @@ void concatenate_masks(rmm::device_vector<column_device_view> const& d_views,
*
* @param stream CUDA stream used for device memory operations and kernel launches.
*/
void concatenate_masks(std::vector<column_view> const& views,
void concatenate_masks(host_span<column_view const> views,
bitmask_type* dest_mask,
rmm::cuda_stream_view stream);

Expand Down
11 changes: 6 additions & 5 deletions cpp/include/cudf/detail/concatenate.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
* Copyright (c) 2020-2021, 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 @@ -18,6 +18,7 @@
#include <cudf/column/column_view.hpp>
#include <cudf/concatenate.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/utilities/span.hpp>

#include <rmm/cuda_stream_view.hpp>

Expand All @@ -27,22 +28,22 @@ namespace cudf {
//! Inner interfaces and implementations
namespace detail {
/**
* @copydoc cudf::concatenate(std::vector<column_view> const&,rmm::mr::device_memory_resource*)
* @copydoc cudf::concatenate(host_span<column_view const>,rmm::mr::device_memory_resource*)
*
* @param stream CUDA stream used for device memory operations and kernel launches.
*/
std::unique_ptr<column> concatenate(
std::vector<column_view> const& columns_to_concat,
host_span<column_view const> columns_to_concat,
rmm::cuda_stream_view stream = rmm::cuda_stream_default,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @copydoc cudf::concatenate(std::vector<table_view> const&,rmm::mr::device_memory_resource*)
* @copydoc cudf::concatenate(host_span<table_view const>,rmm::mr::device_memory_resource*)
*
* @param stream CUDA stream used for device memory operations and kernel launches.
*/
std::unique_ptr<table> concatenate(
std::vector<table_view> const& tables_to_concat,
host_span<table_view const> tables_to_concat,
rmm::cuda_stream_view stream = rmm::cuda_stream_default,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

Expand Down
5 changes: 3 additions & 2 deletions cpp/include/cudf/dictionary/detail/concatenate.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
* Copyright (c) 2020-2021, 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 @@ -17,6 +17,7 @@

#include <cudf/column/column.hpp>
#include <cudf/dictionary/dictionary_column_view.hpp>
#include <cudf/utilities/span.hpp>

#include <rmm/cuda_stream_view.hpp>

Expand All @@ -36,7 +37,7 @@ namespace detail {
* @return New column with concatenated results.
*/
std::unique_ptr<column> concatenate(
std::vector<column_view> const& columns,
host_span<column_view const> columns,
rmm::cuda_stream_view stream = rmm::cuda_stream_default,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

Expand Down
5 changes: 3 additions & 2 deletions cpp/include/cudf/lists/detail/concatenate.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
* Copyright (c) 2020-2021, 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 @@ -18,6 +18,7 @@
#include <cudf/column/column.hpp>
#include <cudf/lists/lists_column_view.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/utilities/span.hpp>

#include <rmm/cuda_stream_view.hpp>

Expand All @@ -42,7 +43,7 @@ namespace detail {
* @return New column with concatenated results.
*/
std::unique_ptr<column> concatenate(
std::vector<column_view> const& columns,
host_span<column_view const> columns,
rmm::cuda_stream_view stream = rmm::cuda_stream_default,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

Expand Down
5 changes: 3 additions & 2 deletions cpp/include/cudf/strings/detail/concatenate.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2020, NVIDIA CORPORATION.
* Copyright (c) 2019-2021, 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 @@ -18,6 +18,7 @@
#include <cudf/column/column.hpp>
#include <cudf/strings/strings_column_view.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/utilities/span.hpp>

#include <rmm/cuda_stream_view.hpp>

Expand All @@ -41,7 +42,7 @@ namespace detail {
* @return New column with concatenated results.
*/
std::unique_ptr<column> concatenate(
std::vector<column_view> const& columns,
host_span<column_view const> columns,
rmm::cuda_stream_view stream = rmm::cuda_stream_default,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

Expand Down
5 changes: 3 additions & 2 deletions cpp/include/cudf/structs/detail/concatenate.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
* Copyright (c) 2020-2021, 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 @@ -18,6 +18,7 @@
#include <cudf/column/column.hpp>
#include <cudf/structs/structs_column_view.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/utilities/span.hpp>

namespace cudf {
namespace structs {
Expand Down Expand Up @@ -48,7 +49,7 @@ namespace detail {
* @return New column with concatenated results.
*/
std::unique_ptr<column> concatenate(
std::vector<column_view> const& columns,
host_span<column_view const> columns,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

Expand Down
35 changes: 31 additions & 4 deletions cpp/include/cudf/utilities/span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,31 @@ struct host_span : public cudf::detail::span_base<T, Extent, host_span<T, Extent

constexpr host_span() noexcept : base() {} // required to compile on centos

template <typename C, std::enable_if_t<is_host_span_supported_container<C>::value>* = nullptr>
// Constructor from container
template <
typename C,
// Only supported containers of types convertible to T
std::enable_if_t<is_host_span_supported_container<C>::value &&
std::is_convertible<std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
std::declval<C&>().data()))> (*)[],
T (*)[]>::value>* = nullptr>
constexpr host_span(C& in) : base(in.data(), in.size())
{
}

template <typename C, std::enable_if_t<is_host_span_supported_container<C>::value>* = nullptr>
// Constructor from const container
template <
typename C,
// Only supported containers of types convertible to T
std::enable_if_t<is_host_span_supported_container<C>::value &&
std::is_convertible<std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
std::declval<C&>().data()))> (*)[],
T (*)[]>::value>* = nullptr>
constexpr host_span(C const& in) : base(in.data(), in.size())
{
}

// Copy construction to support const conversion
template <typename OtherT,
std::size_t OtherExtent,
typename std::enable_if<(Extent == OtherExtent || Extent == dynamic_extent) &&
Expand Down Expand Up @@ -175,12 +190,24 @@ struct device_span : public cudf::detail::span_base<T, Extent, device_span<T, Ex

constexpr device_span() noexcept : base() {} // required to compile on centos

template <typename C, std::enable_if_t<is_device_span_supported_container<C>::value>* = nullptr>
template <
typename C,
// Only supported containers of types convertible to T
std::enable_if_t<is_device_span_supported_container<C>::value &&
std::is_convertible<std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
std::declval<C&>().data()))> (*)[],
T (*)[]>::value>* = nullptr>
constexpr device_span(C& in) : base(thrust::raw_pointer_cast(in.data()), in.size())
{
}

template <typename C, std::enable_if_t<is_device_span_supported_container<C>::value>* = nullptr>
template <
typename C,
// Only supported containers of types convertible to T
std::enable_if_t<is_device_span_supported_container<C>::value &&
std::is_convertible<std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
std::declval<C&>().data()))> (*)[],
T (*)[]>::value>* = nullptr>
constexpr device_span(C const& in) : base(thrust::raw_pointer_cast(in.data()), in.size())
{
}
Expand Down
Loading

0 comments on commit b854598

Please sign in to comment.