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>
204 constexpr
host_span(T* data, std::size_t size,
bool is_device_accessible)
205 :
base(data, size), _is_device_accessible{is_device_accessible}
211 template <
typename C,
213 std::enable_if_t<is_host_span_supported_container<C>::value &&
214 std::is_convertible_v<
215 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
216 std::declval<C&>().data()))> (*)[],
217 T (*)[]>>* =
nullptr>
218 constexpr
host_span(C& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
224 template <
typename C,
226 std::enable_if_t<is_host_span_supported_container<C>::value &&
227 std::is_convertible_v<
228 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
229 std::declval<C&>().data()))> (*)[],
230 T (*)[]>>* =
nullptr>
231 constexpr
host_span(C
const& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
237 template <
typename OtherT,
239 std::enable_if_t<std::is_convertible_v<OtherT (*)[], T (*)[]>>* =
nullptr>
240 constexpr
host_span(cudf::detail::host_vector<OtherT>& in)
241 :
base(in.data(), in.size()), _is_device_accessible{in.get_allocator().is_device_accessible()}
247 template <
typename OtherT,
249 std::enable_if_t<std::is_convertible_v<OtherT (*)[], T (*)[]>>* =
nullptr>
250 constexpr
host_span(cudf::detail::host_vector<OtherT>
const& in)
251 :
base(in.data(), in.size()), _is_device_accessible{in.get_allocator().is_device_accessible()}
257 template <
typename OtherT,
258 std::size_t OtherExtent,
259 std::enable_if_t<(Extent == OtherExtent || Extent ==
dynamic_extent) &&
260 std::is_convertible_v<OtherT (*)[], T (*)[]>,
263 :
base(other.data(), other.size()), _is_device_accessible{other.is_device_accessible()}
297 return this->_data[this->_size - 1];
317 return host_span{this->data() + offset, count, _is_device_accessible};
321 bool _is_device_accessible{
false};
326 template <
typename T>
329 template <
typename T,
typename Alloc>
333 template <
typename T>
337 template <
typename T>
339 rmm::device_uvector<T>> : std::true_type {};
345 template <
typename T, std::
size_t Extent = cudf::dynamic_extent>
354 template <
typename C,
356 std::enable_if_t<is_device_span_supported_container<C>::value &&
357 std::is_convertible_v<
358 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
359 std::declval<C&>().data()))> (*)[],
360 T (*)[]>>* =
nullptr>
367 template <
typename C,
369 std::enable_if_t<is_device_span_supported_container<C>::value &&
370 std::is_convertible_v<
371 std::remove_pointer_t<decltype(thrust::raw_pointer_cast(
372 std::declval<C&>().data()))> (*)[],
373 T (*)[]>>* =
nullptr>
374 constexpr
device_span(C
const& in) :
base(thrust::raw_pointer_cast(in.data()), in.size())
380 template <
typename OtherT,
381 std::size_t OtherExtent,
382 std::enable_if_t<(Extent == OtherExtent || Extent ==
dynamic_extent) &&
383 std::is_convertible_v<OtherT (*)[], T (*)[]>,
386 :
base(other.data(), other.size())
402 return this->_data[idx];
415 return this->_data[0];
427 return this->_data[this->_size - 1];
452 template <
typename T,
template <
typename, std::
size_t>
typename RowType>
456 std::pair<size_t, size_t>;
466 : _flat{flat_view}, _size{columns == 0 ? 0 : flat_view.
size() / columns, columns}
468 #ifndef __CUDA_ARCH__
469 CUDF_EXPECTS(_size.first * _size.second == flat_view.size(),
"Invalid 2D span size");
478 [[nodiscard]] constexpr
auto data() const noexcept {
return _flat.data(); }
485 [[nodiscard]] constexpr
auto size() const noexcept {
return _size; }
492 [[nodiscard]] constexpr
auto count() const noexcept {
return _flat.size(); }
499 [[nodiscard]] constexpr
bool is_empty() const noexcept {
return count() == 0; }
510 constexpr RowType<T, dynamic_extent>
operator[](
size_t row)
const
512 return _flat.subspan(row * _size.second, _size.second);
520 [[nodiscard]] constexpr RowType<T, dynamic_extent>
flat_view()
const {
return _flat; }
529 template <
typename OtherT,
530 template <
typename,
size_t>
531 typename OtherRowType,
532 std::enable_if_t<std::is_convertible_v<OtherRowType<OtherT, dynamic_extent>,
533 RowType<T, dynamic_extent>>,
536 : _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.
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 operator[](size_type idx) const
Returns a reference to the idx-th element of the sequence.
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 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 base::reference front() const
Returns a reference to the first element in the span.
constexpr host_span(cudf::detail::host_vector< OtherT > &in)
constexpr host_span(C const &in)
constexpr base::reference operator[](size_type idx) const
Returns a reference to the idx-th element of the sequence.
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 base::reference back() const
Returns a reference to the last element in the span.
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)
Type declarations for libcudf.
#define CUDF_HOST_DEVICE
Indicates that the function or method is usable on host and device.