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);
184 pointer _data{
nullptr};
199 template <
typename T>
202 template <
typename T,
typename Alloc>
204 std::vector<T, Alloc>> : std::true_type {};
206 template <
typename T,
typename Alloc>
208 thrust::host_vector<T, Alloc>> : std::true_type {};
210 template <
typename T,
typename Alloc>
212 std::basic_string<T, std::char_traits<T>, Alloc>> : std::true_type {};
218 template <
typename T, std::
size_t Extent = cudf::dynamic_extent>
229 constexpr
host_span(T* data, std::size_t size,
bool is_device_accessible)
230 :
base(data, size), _is_device_accessible{is_device_accessible}
236 template <
typename C,
238 std::enable_if_t<is_host_span_supported_container<C>::value &&
239 std::is_convertible_v<
240 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
241 std::declval<C&>().data()))> (*)[],
242 T (*)[]>>* =
nullptr>
243 constexpr
host_span(C& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
249 template <
typename C,
251 std::enable_if_t<is_host_span_supported_container<C>::value &&
252 std::is_convertible_v<
253 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
254 std::declval<C&>().data()))> (*)[],
255 T (*)[]>>* =
nullptr>
256 constexpr
host_span(C
const& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
262 template <
typename OtherT,
264 std::enable_if_t<std::is_convertible_v<OtherT (*)[], T (*)[]>>* =
nullptr>
265 constexpr
host_span(cudf::detail::host_vector<OtherT>& in)
266 :
base(in.data(), in.size()), _is_device_accessible{in.get_allocator().is_device_accessible()}
272 template <
typename OtherT,
274 std::enable_if_t<std::is_convertible_v<OtherT (*)[], T (*)[]>>* =
nullptr>
275 constexpr
host_span(cudf::detail::host_vector<OtherT>
const& in)
276 :
base(in.data(), in.size()), _is_device_accessible{in.get_allocator().is_device_accessible()}
282 template <
typename OtherT,
283 std::size_t OtherExtent,
284 std::enable_if_t<(Extent == OtherExtent || Extent ==
dynamic_extent) &&
285 std::is_convertible_v<OtherT (*)[], T (*)[]>,
288 :
base(other.data(), other.size()), _is_device_accessible{other.is_device_accessible()}
309 return host_span{this->data() + offset, count, _is_device_accessible};
313 bool _is_device_accessible{
false};
318 template <
typename T>
321 template <
typename T,
typename Alloc>
325 template <
typename T>
329 template <
typename T>
331 rmm::device_uvector<T>> : std::true_type {};
337 template <
typename T, std::
size_t Extent = cudf::dynamic_extent>
346 template <
typename C,
348 std::enable_if_t<is_device_span_supported_container<C>::value &&
349 std::is_convertible_v<
350 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
351 std::declval<C&>().data()))> (*)[],
352 T (*)[]>>* =
nullptr>
359 template <
typename C,
361 std::enable_if_t<is_device_span_supported_container<C>::value &&
362 std::is_convertible_v<
363 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
364 std::declval<C&>().data()))> (*)[],
365 T (*)[]>>* =
nullptr>
366 constexpr
device_span(C
const& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
372 template <
typename OtherT,
373 std::size_t OtherExtent,
374 std::enable_if_t<(Extent == OtherExtent || Extent ==
dynamic_extent) &&
375 std::is_convertible_v<OtherT (*)[], T (*)[]>,
378 :
base(other.data(), other.size())
404 template <
typename T,
template <
typename, std::
size_t>
typename RowType>
408 std::pair<size_t, size_t>;
418 : _flat{flat_view}, _size{columns == 0 ? 0 : flat_view.
size() / columns, columns}
420 CUDF_EXPECTS(_size.first * _size.second == flat_view.size(),
"Invalid 2D span size");
428 [[nodiscard]] constexpr
auto data() const noexcept {
return _flat.data(); }
435 [[nodiscard]] constexpr
auto size() const noexcept {
return _size; }
442 [[nodiscard]] constexpr
auto count() const noexcept {
return _flat.size(); }
449 [[nodiscard]] constexpr
bool is_empty() const noexcept {
return count() == 0; }
460 constexpr RowType<T, dynamic_extent>
operator[](
size_t row)
const
462 return _flat.subspan(row * _size.second, _size.second);
470 [[nodiscard]] constexpr RowType<T, dynamic_extent>
flat_view()
const {
return _flat; }
479 template <
typename OtherT,
480 template <
typename,
size_t>
481 typename OtherRowType,
482 std::enable_if_t<std::is_convertible_v<OtherRowType<OtherT, dynamic_extent>,
483 RowType<T, dynamic_extent>>,
486 : _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 auto count() const noexcept
Returns the number of elements in the span.
constexpr RowType< T, dynamic_extent > flat_view() const
Returns a flattened span of the 2D 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 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.
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.
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.
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
#define CUDF_EXPECTS(...)
Macro for checking (pre-)conditions that throws an exception when a condition is violated.
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 subspan(typename base::size_type offset, typename base::size_type count) const noexcept
Obtains a span that is a view over the count elements of this span starting at offset.
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)
constexpr host_span subspan(typename base::size_type offset, typename base::size_type count) const noexcept
Obtains a span that is a view over the count elements of this span starting at offset.
constexpr host_span(T *data, std::size_t size, bool is_device_accessible)