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;
125 return sizeof(T) * _size;
143 return Derived(_data, count);
154 return Derived(_data + _size - count, count);
173 template <
typename T>
176 template <
typename T,
typename Alloc>
178 std::vector<T, Alloc>> : std::true_type {};
180 template <
typename T,
typename Alloc>
182 thrust::host_vector<T, Alloc>> : std::true_type {};
184 template <
typename T,
typename Alloc>
186 std::basic_string<T, std::char_traits<T>, Alloc>> : std::true_type {};
192 template <
typename T, std::
size_t Extent = cudf::dynamic_extent>
209 :
base(data, size), _is_device_accessible{is_device_accessible}
215 template <
typename C,
217 std::enable_if_t<is_host_span_supported_container<C>::value &&
218 std::is_convertible_v<
219 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
220 std::declval<C&>().data()))> (*)[],
221 T (*)[]>>* =
nullptr>
222 constexpr
host_span(C& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
228 template <
typename C,
230 std::enable_if_t<is_host_span_supported_container<C>::value &&
231 std::is_convertible_v<
232 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
233 std::declval<C&>().data()))> (*)[],
234 T (*)[]>>* =
nullptr>
235 constexpr
host_span(C
const& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
241 template <
typename OtherT,
242 std::size_t OtherExtent,
243 std::enable_if_t<(Extent == OtherExtent || Extent ==
dynamic_extent) &&
244 std::is_convertible_v<OtherT (*)[], T (*)[]>,
247 :
base(other.data(), other.size()), _is_device_accessible{other.is_device_accessible()}
262 static_assert(
sizeof(idx) >=
sizeof(
size_t),
"index type must not be smaller than size_t");
263 return this->_data[idx];
285 return this->_data[this->_size - 1];
305 return host_span{this->data() + offset, count, _is_device_accessible};
309 bool _is_device_accessible{
false};
314 template <
typename T>
317 template <
typename T,
typename Alloc>
321 template <
typename T>
325 template <
typename T>
327 rmm::device_uvector<T>> : std::true_type {};
333 template <
typename T, std::
size_t Extent = cudf::dynamic_extent>
342 template <
typename C,
344 std::enable_if_t<is_device_span_supported_container<C>::value &&
345 std::is_convertible_v<
346 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
347 std::declval<C&>().data()))> (*)[],
348 T (*)[]>>* =
nullptr>
355 template <
typename C,
357 std::enable_if_t<is_device_span_supported_container<C>::value &&
358 std::is_convertible_v<
359 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
360 std::declval<C&>().data()))> (*)[],
361 T (*)[]>>* =
nullptr>
362 constexpr
device_span(C
const& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
368 template <
typename OtherT,
369 std::size_t OtherExtent,
370 std::enable_if_t<(Extent == OtherExtent || Extent ==
dynamic_extent) &&
371 std::is_convertible_v<OtherT (*)[], T (*)[]>,
374 :
base(other.data(), other.size())
390 static_assert(
sizeof(idx) >=
sizeof(
size_t),
"index type must not be smaller than size_t");
391 return this->_data[idx];
404 return this->_data[0];
416 return this->_data[this->_size - 1];
441 template <
typename T,
template <
typename, std::
size_t>
typename RowType>
445 std::pair<size_t, size_t>;
455 : _flat{flat_view}, _size{columns == 0 ? 0 : flat_view.
size() / columns, columns}
457 #ifndef __CUDA_ARCH__
458 CUDF_EXPECTS(_size.first * _size.second == flat_view.size(),
"Invalid 2D span size");
501 return _flat.subspan(row * _size.second, _size.second);
521 template <
typename OtherT,
522 template <
typename,
size_t>
typename OtherRowType,
523 std::enable_if_t<std::is_convertible_v<OtherRowType<OtherT, dynamic_extent>,
524 RowType<T, dynamic_extent>>,
527 : _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 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)
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.