9 #include <cudf/utilities/export.hpp>
15 #include <cuda/std/span>
16 #include <thrust/detail/raw_pointer_cast.h>
17 #include <thrust/device_vector.h>
18 #include <thrust/host_vector.h>
19 #include <thrust/memory.h>
23 #include <type_traits>
31 namespace CUDF_EXPORT
cudf {
45 template <
typename T,
typename Alloc>
47 std::vector<T, Alloc>> : std::true_type {};
49 template <
typename T,
typename Alloc>
51 thrust::host_vector<T, Alloc>> : std::true_type {};
53 template <
typename T,
typename Alloc>
55 std::basic_string<T, std::char_traits<T>, Alloc>> : std::true_type {};
64 template <
typename T, std::
size_t Extent = cudf::dynamic_extent>
67 using span_type = cuda::std::span<T, Extent>;
74 using pointer =
typename span_type::pointer;
80 static constexpr std::size_t extent = span_type::extent;
104 : _span{data, size}, _is_device_accessible{is_device_accessible}
110 template <
typename C,
112 std::enable_if_t<is_host_span_supported_container<C>::value &&
113 std::is_convertible_v<
114 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
115 std::declval<C&>().data()))> (*)[],
116 T (*)[]>>* =
nullptr>
117 constexpr
host_span(C& in) : _span{thrust::raw_pointer_cast(in.data()), in.size()}
123 template <
typename C,
125 std::enable_if_t<is_host_span_supported_container<C>::value &&
126 std::is_convertible_v<
127 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
128 std::declval<C&>().data()))> (*)[],
129 T (*)[]>>* =
nullptr>
130 constexpr
host_span(C
const& in) : _span{thrust::raw_pointer_cast(in.data()), in.size()}
136 template <
typename OtherT,
137 std::size_t OtherExtent,
138 std::enable_if_t<(Extent == OtherExtent || Extent ==
dynamic_extent) &&
139 std::is_convertible_v<OtherT (*)[], T (*)[]>,
142 : _span{other.data(), other.size()}, _is_device_accessible{other.is_device_accessible()}
158 static_assert(
sizeof(idx) >=
sizeof(
size_t),
"index type must not be smaller than size_t");
198 return _span.data() + _span.size();
220 return _span.size_bytes();
277 [[nodiscard]] constexpr
operator std::span<T>() const noexcept
279 return std::span<T>(_span.data(), _span.size());
286 bool _is_device_accessible{
false};
295 template <
typename T, std::
size_t Extent = cuda::std::dynamic_extent>
306 template <
typename T,
template <
typename, std::
size_t>
typename RowType>
310 std::pair<size_t, size_t>;
320 : _flat{flat_view}, _size{columns == 0 ? 0 : flat_view.
size() / columns, columns}
322 #ifndef __CUDA_ARCH__
323 CUDF_EXPECTS(_size.first * _size.second == flat_view.size(),
"Invalid 2D span size");
366 return _flat.subspan(row * _size.second, _size.second);
386 template <
typename OtherT,
387 template <
typename,
size_t>
typename OtherRowType,
388 std::enable_if_t<std::is_convertible_v<OtherRowType<OtherT, dynamic_extent>,
389 RowType<T, dynamic_extent>>,
392 : _flat{other.flat_view()}, _size{other.size()}
Generic class for row-major 2D spans. Not compliant with STL container semantics/syntax.
std::pair< size_t, size_t > size_type
Type used to represent the dimension of the span.
constexpr CUDF_HOST_DEVICE bool is_empty() const noexcept
Checks if the span is empty.
constexpr CUDF_HOST_DEVICE auto size() const noexcept
Returns the size in the span as pair.
constexpr CUDF_HOST_DEVICE auto count() const noexcept
Returns the number of elements in the span.
constexpr CUDF_HOST_DEVICE RowType< T, dynamic_extent > operator[](std::size_t row) const
Returns a reference to the row-th element of the sequence.
constexpr CUDF_HOST_DEVICE auto data() const noexcept
Returns a pointer to the beginning of the sequence.
constexpr CUDF_HOST_DEVICE RowType< T, dynamic_extent > flat_view() const
Returns a flattened span of the 2D span.
RowType< T, dynamic_extent > _flat
flattened 2D span
constexpr base_2dspan(base_2dspan< OtherT, OtherRowType > const &other) noexcept
Construct a 2D span from another 2D span of convertible type.
std::unique_ptr< column > count(strings_column_view const &input, string_scalar const &target, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Returns the number of times the given target string matches in each string.
#define CUDF_EXPECTS(...)
Macro for checking (pre-)conditions that throws an exception when a condition is violated.
cuda::std::span< T, Extent > device_span
Device span is an alias of cuda::std::span.
constexpr std::size_t dynamic_extent
A constant used to differentiate std::span of static and dynamic extent.
int32_t size_type
Row index type for columns and tables.
Host span, a non-owning view over a contiguous sequence of host-accessible elements.
typename span_type::reference reference
Reference returned by operator[].
constexpr CUDF_HOST_DEVICE host_span(T *data, std::size_t size)
Constructs a span from a pointer and a size.
constexpr host_span last(size_type count) const noexcept
Obtains a subspan consisting of the last count elements of the sequence.
typename span_type::const_reference const_reference
Const reference to an element.
constexpr reference back() const
Returns a reference to the last element in the span.
constexpr CUDF_HOST_DEVICE size_type size() const noexcept
Returns the number of elements in the span.
constexpr host_span(C const &in)
typename span_type::size_type size_type
Size type.
typename span_type::pointer pointer
Pointer returned by data()
typename span_type::difference_type difference_type
std::ptrdiff_t
constexpr reference operator[](size_type idx) const
Returns a reference to the idx-th element of the sequence.
bool is_device_accessible() const
Returns whether the data is device accessible (e.g. pinned memory)
typename span_type::const_pointer const_pointer
Pointer returned by data() const.
constexpr CUDF_HOST_DEVICE host_span subspan(size_type offset, size_type count) const noexcept
Obtains a span that is a view over the count elements of this span starting at offset.
constexpr CUDF_HOST_DEVICE iterator end() const noexcept
Returns an iterator to the element following the last element of the span.
constexpr host_span(host_span< OtherT, OtherExtent > const &other) noexcept
constexpr CUDF_HOST_DEVICE size_type size_bytes() const noexcept
Returns the size of the sequence in bytes.
constexpr reference front() const
Returns a reference to the first element in the span.
constexpr CUDF_HOST_DEVICE host_span(T *data, std::size_t size, bool is_device_accessible)
Constructor from pointer, size and device-accessibility flag.
constexpr host_span first(size_type count) const noexcept
Obtains a subspan consisting of the first count elements of the sequence.
pointer iterator
The type of the iterator returned by begin()
constexpr CUDF_HOST_DEVICE pointer data() const noexcept
Returns a pointer to the beginning of the sequence.
typename span_type::element_type element_type
Element type.
constexpr CUDF_HOST_DEVICE iterator begin() const noexcept
Returns an iterator to the first element of the span.
constexpr CUDF_HOST_DEVICE bool empty() const noexcept
Checks if the span is empty.
typename span_type::value_type value_type
Stored value type.
constexpr host_span(C &in)
Type declarations for libcudf.
#define CUDF_HOST_DEVICE
Indicates that the function or method is usable on host and device.