19 #include <cudf/detail/utilities/host_vector.hpp>
20 #include <cudf/utilities/export.hpp>
26 #include <thrust/detail/raw_pointer_cast.h>
27 #include <thrust/device_vector.h>
28 #include <thrust/host_vector.h>
29 #include <thrust/memory.h>
33 #include <type_traits>
36 namespace CUDF_EXPORT
cudf {
45 constexpr std::size_t
dynamic_extent = std::numeric_limits<std::size_t>::max();
54 template <
typename T, std::
size_t Extent,
typename Derived>
56 static_assert(Extent ==
dynamic_extent,
"Only dynamic extent is supported");
70 static constexpr std::size_t extent = Extent;
134 [[nodiscard]] constexpr
iterator end() const noexcept {
return _data + _size; }
140 [[nodiscard]] constexpr
pointer data() const noexcept {
return _data; }
159 [[nodiscard]] constexpr
bool empty() const noexcept {
return _size == 0; }
169 return Derived(_data, count);
180 return Derived(_data + _size - count, count);
192 return Derived(_data + offset, count);
196 pointer _data{
nullptr};
211 template <
typename T>
214 template <
typename T,
typename Alloc>
216 std::vector<T, Alloc>> : std::true_type {};
218 template <
typename T,
typename Alloc>
220 thrust::host_vector<T, Alloc>> : std::true_type {};
222 template <
typename T,
typename Alloc>
224 std::basic_string<T, std::char_traits<T>, Alloc>> : std::true_type {};
230 template <
typename T, std::
size_t Extent = cudf::dynamic_extent>
242 std::enable_if_t<is_host_span_supported_container<C>::value &&
243 std::is_convertible_v<std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
244 std::declval<C&>().data()))> (*)[],
245 T (*)[]>>* =
nullptr>
246 constexpr
host_span(C& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
255 std::enable_if_t<is_host_span_supported_container<C>::value &&
256 std::is_convertible_v<std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
257 std::declval<C&>().data()))> (*)[],
258 T (*)[]>>* =
nullptr>
259 constexpr
host_span(C
const& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
265 template <
typename OtherT,
267 std::enable_if_t<std::is_convertible_v<OtherT (*)[], T (*)[]>>* =
nullptr>
268 constexpr
host_span(cudf::detail::host_vector<OtherT>& in)
269 :
base(in.data(), in.size()), _is_device_accessible{in.get_allocator().is_device_accessible()}
275 template <
typename OtherT,
277 std::enable_if_t<std::is_convertible_v<OtherT (*)[], T (*)[]>>* =
nullptr>
278 constexpr
host_span(cudf::detail::host_vector<OtherT>
const& in)
279 :
base(in.data(), in.size()), _is_device_accessible{in.get_allocator().is_device_accessible()}
285 template <
typename OtherT,
286 std::size_t OtherExtent,
287 std::enable_if_t<(Extent == OtherExtent || Extent ==
dynamic_extent) &&
288 std::is_convertible_v<OtherT (*)[], T (*)[]>,
291 :
base(other.data(), other.size())
303 bool _is_device_accessible{
false};
308 template <
typename T>
311 template <
typename T,
typename Alloc>
315 template <
typename T>
319 template <
typename T>
321 rmm::device_uvector<T>> : std::true_type {};
327 template <
typename T, std::
size_t Extent = cudf::dynamic_extent>
339 std::enable_if_t<is_device_span_supported_container<C>::value &&
340 std::is_convertible_v<std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
341 std::declval<C&>().data()))> (*)[],
342 T (*)[]>>* =
nullptr>
352 std::enable_if_t<is_device_span_supported_container<C>::value &&
353 std::is_convertible_v<std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
354 std::declval<C&>().data()))> (*)[],
355 T (*)[]>>* =
nullptr>
356 constexpr
device_span(C
const& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
362 template <
typename OtherT,
363 std::size_t OtherExtent,
364 std::enable_if_t<(Extent == OtherExtent || Extent ==
dynamic_extent) &&
365 std::is_convertible_v<OtherT (*)[], T (*)[]>,
368 :
base(other.data(), other.size())
381 template <
typename T,
template <
typename, std::
size_t>
typename RowType>
385 std::pair<size_t, size_t>;
395 constexpr
base_2dspan(T* data,
size_t rows,
size_t columns) noexcept
396 : _data{data}, _size{rows, columns}
412 constexpr
auto data() const noexcept {
return _data; }
418 constexpr
auto size() const noexcept {
return _size; }
424 constexpr
auto count() const noexcept {
return size().first * size().second; }
430 [[nodiscard]] constexpr
bool is_empty() const noexcept {
return count() == 0; }
442 return row * size.second +
column;
454 constexpr RowType<T, dynamic_extent>
operator[](
size_t row)
const
456 return {this->data() + flatten_index(row, 0, this->size()), this->size().second};
466 [[nodiscard]] constexpr RowType<T, dynamic_extent>
front()
const {
return (*
this)[0]; }
474 [[nodiscard]] constexpr RowType<T, dynamic_extent>
back()
const
476 return (*
this)[size().first - 1];
490 _data + flatten_index(first_row, 0, this->size()), num_rows, this->size().second);
500 return {this->data(), this->size().first * this->size().second};
510 template <
typename OtherT,
511 template <
typename,
size_t>
512 typename OtherRowType,
513 std::enable_if_t<std::is_convertible_v<OtherRowType<OtherT, dynamic_extent>,
514 RowType<T, dynamic_extent>>,
517 : _data{other.data()}, _size{other.size()}
A container of nullable device data as a column of elements.
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 auto count() const noexcept
Returns the number of elements in the span.
constexpr RowType< T, dynamic_extent > back() const
Returns a reference to the last element in the span.
constexpr bool is_empty() const noexcept
Checks if the span is empty.
constexpr auto data() const noexcept
Returns a pointer to the beginning of the sequence.
constexpr base_2dspan subspan(size_t first_row, size_t num_rows) const noexcept
Obtains a 2D span that is a view over the num_rows rows of this span starting at first_row
static constexpr size_t flatten_index(size_t row, size_t column, size_type size) noexcept
Returns flattened index of the element at the specified 2D position.
constexpr RowType< T, dynamic_extent > operator[](size_t row) const
Returns a reference to the row-th element of the sequence.
constexpr auto size() const noexcept
Returns the size in the span as pair.
constexpr RowType< T, dynamic_extent > front() const
Returns a reference to the first element in the span.
base_2dspan(T *data, size_type size) noexcept
Constructor a 2D span.
constexpr base_2dspan(base_2dspan< OtherT, OtherRowType > const &other) noexcept
Construct a 2D span from another 2D span of convertible type.
constexpr RowType< T, dynamic_extent > flat_view()
Returns a flattened span of the 2D span.
C++20 std::span with reduced feature set.
constexpr iterator end() const noexcept
Returns an iterator to the element following the last element of the span.
std::size_t size_type
The type used for the size of the span.
constexpr bool empty() const noexcept
Checks if the span is empty.
constexpr reference operator[](size_type idx) const
Returns a reference to the idx-th element of the sequence.
constexpr reference back() const
Returns a reference to the last element in the span.
constexpr Derived first(size_type count) const noexcept
Obtains a subspan consisting of the first N elements of the sequence.
T * iterator
The type of the iterator returned by begin()
constexpr size_type size_bytes() const noexcept
Returns the size of the sequence in bytes.
constexpr span_base(pointer data, size_type size)
Constructs a span from a pointer and a size.
constexpr pointer data() const noexcept
Returns a pointer to the beginning of the sequence.
constexpr span_base(span_base const &) noexcept=default
Copy constructor.
constexpr Derived last(size_type count) const noexcept
Obtains a subspan consisting of the last N elements of the sequence.
std::remove_cv< T > value_type
Stored value type.
std::ptrdiff_t difference_type
std::ptrdiff_t
T * pointer
The type of the pointer returned by data()
T const * const_pointer
The type of the pointer returned by data() const.
constexpr Derived 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.
T & reference
The type of the reference returned by operator[](size_type)
constexpr iterator begin() const noexcept
Returns an iterator to the first element of the span.
constexpr span_base & operator=(span_base const &) noexcept=default
Copy assignment operator.
constexpr reference front() const
Returns a reference to the first element in the span.
constexpr size_type size() const noexcept
Returns the number of elements in the span.
T const & const_reference
The type of the reference returned by operator[](size_type) const.
T element_type
The type of the elements in the span.
thrust::device_vector< T, rmm::mr::thrust_allocator< T > > device_vector
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.
Device version of C++20 std::span with reduced feature set.
constexpr device_span(device_span< OtherT, OtherExtent > const &other) noexcept
constexpr device_span(C &in)
constexpr device_span(C const &in)
C++20 std::span with reduced feature set.
constexpr host_span(cudf::detail::host_vector< OtherT > &in)
constexpr host_span(C const &in)
constexpr host_span(cudf::detail::host_vector< OtherT > const &in)
bool is_device_accessible() const
Returns whether the data is device accessible (e.g. pinned memory)
constexpr host_span(host_span< OtherT, OtherExtent > const &other) noexcept
constexpr host_span(C &in)