9 #include <cudf/utilities/export.hpp>
15 #include <thrust/detail/raw_pointer_cast.h>
16 #include <thrust/device_vector.h>
17 #include <thrust/host_vector.h>
18 #include <thrust/memory.h>
22 #include <type_traits>
25 namespace CUDF_EXPORT
cudf {
34 constexpr std::size_t
dynamic_extent = std::numeric_limits<std::size_t>::max();
43 template <
typename T, std::
size_t Extent,
typename Derived>
45 static_assert(Extent ==
dynamic_extent,
"Only dynamic extent is supported");
59 static constexpr std::size_t extent = Extent;
114 return sizeof(T) * _size;
132 return Derived(_data, count);
143 return Derived(_data + _size - count, count);
162 template <
typename T>
165 template <
typename T,
typename Alloc>
167 std::vector<T, Alloc>> : std::true_type {};
169 template <
typename T,
typename Alloc>
171 thrust::host_vector<T, Alloc>> : std::true_type {};
173 template <
typename T,
typename Alloc>
175 std::basic_string<T, std::char_traits<T>, Alloc>> : std::true_type {};
181 template <
typename T, std::
size_t Extent = cudf::dynamic_extent>
198 :
base(data, size), _is_device_accessible{is_device_accessible}
204 template <
typename C,
206 std::enable_if_t<is_host_span_supported_container<C>::value &&
207 std::is_convertible_v<
208 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
209 std::declval<C&>().data()))> (*)[],
210 T (*)[]>>* =
nullptr>
211 constexpr
host_span(C& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
217 template <
typename C,
219 std::enable_if_t<is_host_span_supported_container<C>::value &&
220 std::is_convertible_v<
221 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
222 std::declval<C&>().data()))> (*)[],
223 T (*)[]>>* =
nullptr>
224 constexpr
host_span(C
const& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
230 template <
typename OtherT,
231 std::size_t OtherExtent,
232 std::enable_if_t<(Extent == OtherExtent || Extent ==
dynamic_extent) &&
233 std::is_convertible_v<OtherT (*)[], T (*)[]>,
236 :
base(other.data(), other.size()), _is_device_accessible{other.is_device_accessible()}
251 static_assert(
sizeof(idx) >=
sizeof(
size_t),
"index type must not be smaller than size_t");
252 return this->_data[idx];
274 return this->_data[this->_size - 1];
294 return host_span{this->data() + offset, count, _is_device_accessible};
298 bool _is_device_accessible{
false};
303 template <
typename T>
306 template <
typename T,
typename Alloc>
310 template <
typename T>
314 template <
typename T>
316 rmm::device_uvector<T>> : std::true_type {};
322 template <
typename T, std::
size_t Extent = cudf::dynamic_extent>
331 template <
typename C,
333 std::enable_if_t<is_device_span_supported_container<C>::value &&
334 std::is_convertible_v<
335 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
336 std::declval<C&>().data()))> (*)[],
337 T (*)[]>>* =
nullptr>
344 template <
typename C,
346 std::enable_if_t<is_device_span_supported_container<C>::value &&
347 std::is_convertible_v<
348 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
349 std::declval<C&>().data()))> (*)[],
350 T (*)[]>>* =
nullptr>
351 constexpr
device_span(C
const& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
357 template <
typename OtherT,
358 std::size_t OtherExtent,
359 std::enable_if_t<(Extent == OtherExtent || Extent ==
dynamic_extent) &&
360 std::is_convertible_v<OtherT (*)[], T (*)[]>,
363 :
base(other.data(), other.size())
379 static_assert(
sizeof(idx) >=
sizeof(
size_t),
"index type must not be smaller than size_t");
380 return this->_data[idx];
393 return this->_data[0];
405 return this->_data[this->_size - 1];
430 template <
typename T,
template <
typename, std::
size_t>
typename RowType>
434 std::pair<size_t, size_t>;
444 : _flat{flat_view}, _size{columns == 0 ? 0 : flat_view.
size() / columns, columns}
446 #ifndef __CUDA_ARCH__
447 CUDF_EXPECTS(_size.first * _size.second == flat_view.size(),
"Invalid 2D span size");
490 return _flat.subspan(row * _size.second, _size.second);
510 template <
typename OtherT,
511 template <
typename,
size_t>
typename OtherRowType,
512 std::enable_if_t<std::is_convertible_v<OtherRowType<OtherT, dynamic_extent>,
513 RowType<T, dynamic_extent>>,
516 : _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.