19 #include <cudf/detail/utilities/host_vector.hpp>
21 #include <cudf/utilities/export.hpp>
27 #include <thrust/detail/raw_pointer_cast.h>
28 #include <thrust/device_vector.h>
29 #include <thrust/host_vector.h>
30 #include <thrust/memory.h>
34 #include <type_traits>
37 namespace CUDF_EXPORT
cudf {
46 constexpr std::size_t
dynamic_extent = std::numeric_limits<std::size_t>::max();
55 template <
typename T, std::
size_t Extent,
typename Derived>
57 static_assert(Extent ==
dynamic_extent,
"Only dynamic extent is supported");
71 static constexpr std::size_t extent = Extent;
126 return sizeof(T) * _size;
144 return Derived(_data, count);
155 return Derived(_data + _size - count, count);
174 template <
typename T>
177 template <
typename T,
typename Alloc>
179 std::vector<T, Alloc>> : std::true_type {};
181 template <
typename T,
typename Alloc>
183 thrust::host_vector<T, Alloc>> : std::true_type {};
185 template <
typename T,
typename Alloc>
187 std::basic_string<T, std::char_traits<T>, Alloc>> : std::true_type {};
193 template <
typename T, std::
size_t Extent = cudf::dynamic_extent>
210 :
base(data, size), _is_device_accessible{is_device_accessible}
216 template <
typename C,
218 std::enable_if_t<is_host_span_supported_container<C>::value &&
219 std::is_convertible_v<
220 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
221 std::declval<C&>().data()))> (*)[],
222 T (*)[]>>* =
nullptr>
223 constexpr
host_span(C& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
229 template <
typename C,
231 std::enable_if_t<is_host_span_supported_container<C>::value &&
232 std::is_convertible_v<
233 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
234 std::declval<C&>().data()))> (*)[],
235 T (*)[]>>* =
nullptr>
236 constexpr
host_span(C
const& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
242 template <
typename OtherT,
244 std::enable_if_t<std::is_convertible_v<OtherT (*)[], T (*)[]>>* =
nullptr>
245 constexpr
host_span(cudf::detail::host_vector<OtherT>& in)
246 :
base(in.data(), in.size()), _is_device_accessible{in.get_allocator().is_device_accessible()}
252 template <
typename OtherT,
254 std::enable_if_t<std::is_convertible_v<OtherT (*)[], T (*)[]>>* =
nullptr>
255 constexpr
host_span(cudf::detail::host_vector<OtherT>
const& in)
256 :
base(in.data(), in.size()), _is_device_accessible{in.get_allocator().is_device_accessible()}
262 template <
typename OtherT,
263 std::size_t OtherExtent,
264 std::enable_if_t<(Extent == OtherExtent || Extent ==
dynamic_extent) &&
265 std::is_convertible_v<OtherT (*)[], T (*)[]>,
268 :
base(other.data(), other.size()), _is_device_accessible{other.is_device_accessible()}
283 static_assert(
sizeof(idx) >=
sizeof(
size_t),
"index type must not be smaller than size_t");
284 return this->_data[idx];
306 return this->_data[this->_size - 1];
326 return host_span{this->data() + offset, count, _is_device_accessible};
330 bool _is_device_accessible{
false};
335 template <
typename T>
338 template <
typename T,
typename Alloc>
342 template <
typename T>
346 template <
typename T>
348 rmm::device_uvector<T>> : std::true_type {};
354 template <
typename T, std::
size_t Extent = cudf::dynamic_extent>
363 template <
typename C,
365 std::enable_if_t<is_device_span_supported_container<C>::value &&
366 std::is_convertible_v<
367 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
368 std::declval<C&>().data()))> (*)[],
369 T (*)[]>>* =
nullptr>
376 template <
typename C,
378 std::enable_if_t<is_device_span_supported_container<C>::value &&
379 std::is_convertible_v<
380 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
381 std::declval<C&>().data()))> (*)[],
382 T (*)[]>>* =
nullptr>
383 constexpr
device_span(C
const& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
389 template <
typename OtherT,
390 std::size_t OtherExtent,
391 std::enable_if_t<(Extent == OtherExtent || Extent ==
dynamic_extent) &&
392 std::is_convertible_v<OtherT (*)[], T (*)[]>,
395 :
base(other.data(), other.size())
411 static_assert(
sizeof(idx) >=
sizeof(
size_t),
"index type must not be smaller than size_t");
412 return this->_data[idx];
425 return this->_data[0];
437 return this->_data[this->_size - 1];
462 template <
typename T,
template <
typename, std::
size_t>
typename RowType>
466 std::pair<size_t, size_t>;
476 : _flat{flat_view}, _size{columns == 0 ? 0 : flat_view.
size() / columns, columns}
478 #ifndef __CUDA_ARCH__
479 CUDF_EXPECTS(_size.first * _size.second == flat_view.size(),
"Invalid 2D span size");
522 return _flat.subspan(row * _size.second, _size.second);
542 template <
typename OtherT,
543 template <
typename,
size_t>
544 typename OtherRowType,
545 std::enable_if_t<std::is_convertible_v<OtherRowType<OtherT, dynamic_extent>,
546 RowType<T, dynamic_extent>>,
549 : _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.
C++20 std::span with reduced feature set.
std::size_t size_type
The type used for the size of the span.
constexpr CUDF_HOST_DEVICE iterator end() const noexcept
Returns an iterator to the element following the last element of 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 CUDF_HOST_DEVICE pointer data() const noexcept
Returns a pointer to the beginning of the sequence.
constexpr CUDF_HOST_DEVICE span_base(span_base const &) noexcept=default
Copy constructor.
constexpr CUDF_HOST_DEVICE size_type size() const noexcept
Returns the number of elements in the span.
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 CUDF_HOST_DEVICE span_base & operator=(span_base const &) noexcept=default
Copy assignment operator.
constexpr CUDF_HOST_DEVICE span_base(pointer data, size_type size)
Constructs a span from a pointer and a size.
T & reference
The type of the reference returned by operator[](size_type)
constexpr CUDF_HOST_DEVICE bool empty() const noexcept
Checks if the span is empty.
constexpr CUDF_HOST_DEVICE iterator begin() const noexcept
Returns an iterator to the first element of the span.
constexpr CUDF_HOST_DEVICE size_type size_bytes() const noexcept
Returns the size of the sequence in bytes.
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.
Device version of C++20 std::span with reduced feature set.
constexpr base::reference back() const
Returns a reference to the last element in the span.
constexpr CUDF_HOST_DEVICE device_span(device_span< OtherT, OtherExtent > const &other) noexcept
constexpr base::reference front() const
Returns a reference to the first element in the span.
constexpr CUDF_HOST_DEVICE 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 &in)
constexpr device_span(C const &in)
constexpr base::reference operator[](typename base::size_type idx) const
Returns a reference to the idx-th element of the sequence.
C++20 std::span with reduced feature set.
constexpr base::reference front() const
Returns a reference to the first element in the span.
constexpr host_span(cudf::detail::host_vector< OtherT > &in)
constexpr base::reference operator[](typename base::size_type idx) const
Returns a reference to the idx-th element of the sequence.
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 CUDF_HOST_DEVICE host_span(T *data, std::size_t size, bool is_device_accessible)
Constructor from pointer and size.
constexpr base::reference back() const
Returns a reference to the last element in the span.
constexpr host_span(C &in)
constexpr CUDF_HOST_DEVICE 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.
Type declarations for libcudf.
#define CUDF_HOST_DEVICE
Indicates that the function or method is usable on host and device.