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>
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 (*)[],
void>>* =
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(),
"无效的二维跨度大小");
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()}
行主序二维跨度的通用类。不兼容 STL 容器语义/语法。
std::pair< size_t, size_t > size_type
用于表示跨度尺寸的类型。
constexpr CUDF_HOST_DEVICE bool is_empty() const noexcept
检查跨度是否为空。
constexpr CUDF_HOST_DEVICE auto size() const noexcept
返回跨度的尺寸作为一对值。
constexpr CUDF_HOST_DEVICE auto count() const noexcept
返回跨度中的元素数量。
constexpr CUDF_HOST_DEVICE RowType< T, dynamic_extent > operator[](std::size_t row) const
返回序列中第 `row` 行的引用。
constexpr CUDF_HOST_DEVICE auto data() const noexcept
返回指向序列开头的指针。
constexpr CUDF_HOST_DEVICE RowType< T, dynamic_extent > flat_view() const
返回该二维跨度的展平跨度。
RowType< T, dynamic_extent > _flat
展平的二维跨度
constexpr base_2dspan(base_2dspan< OtherT, OtherRowType > const &other) noexcept
从另一个可转换类型的二维跨度构造一个二维跨度。
具有精简功能集的 C++20 std::span。
std::size_t size_type
用于表示跨度大小的类型。
constexpr CUDF_HOST_DEVICE iterator end() const noexcept
返回指向跨度中最后一个元素之后元素的迭代器。
constexpr Derived first(size_type count) const noexcept
获取由序列前 N 个元素组成的子跨度。
T * iterator
begin() 返回的迭代器类型
constexpr CUDF_HOST_DEVICE pointer data() const noexcept
返回指向序列开头的指针。
constexpr CUDF_HOST_DEVICE span_base & operator=(span_base const &) noexcept=default
复制构造函数。
constexpr CUDF_HOST_DEVICE size_type size() const noexcept
返回跨度中的元素数量。
constexpr Derived last(size_type count) const noexcept
获取由序列后 N 个元素组成的子跨度。
std::remove_cv< T > value_type
存储的值类型。
std::ptrdiff_t difference_type
std::ptrdiff_t
T * pointer
data() 返回的指针类型
T const * const_pointer
data() const 返回的指针类型。
constexpr CUDF_HOST_DEVICE span_base & operator=(span_base const &) noexcept=default
拷贝赋值运算符。
constexpr CUDF_HOST_DEVICE span_base(pointer data, size_type size)
从指针和大小构造一个跨度。
T & reference
operator[](size_type) 返回的引用类型
constexpr CUDF_HOST_DEVICE bool empty() const noexcept
检查跨度是否为空。
constexpr CUDF_HOST_DEVICE iterator begin() const noexcept
返回指向跨度第一个元素的迭代器。
constexpr CUDF_HOST_DEVICE size_type size_bytes() const noexcept
返回序列的大小(以字节为单位)。
T const & const_reference
operator[](size_type) const 返回的引用类型。
thrust::device_vector< T, rmm::mr::thrust_allocator< T > > device_vector
#define CUDF_EXPECTS(...)
用于检查(前置)条件的宏,当条件不满足时抛出异常。
constexpr std::size_t dynamic_extent
用于区分静态和动态范围的 std::span 的常量。
具有精简功能集的 C++20 std::span 的设备版本。
constexpr base::reference back() const
返回跨度中最后一个元素的引用。
constexpr CUDF_HOST_DEVICE device_span(device_span< OtherT, OtherExtent > const &other) noexcept
constexpr base::reference front() const
返回跨度中第一个元素的引用。
constexpr CUDF_HOST_DEVICE device_span subspan(typename base::size_type offset, typename base::size_type count) const noexcept
获取一个子跨度,它是从此跨度中从 offset 开始的 count 个元素的视图。
constexpr device_span(C &in)
constexpr device_span(C const &in)
constexpr base::reference operator[](typename base::size_type idx) const
返回序列中第 `idx` 个元素的引用。
具有精简功能集的 C++20 std::span。
constexpr base::reference front() const
返回跨度中第一个元素的引用。
constexpr host_span(cudf::detail::host_vector< OtherT > &in)
constexpr base::reference operator[](typename base::size_type idx) const
返回序列中第 `idx` 个元素的引用。
constexpr host_span(C const &in)
constexpr host_span(cudf::detail::host_vector< OtherT > const &in)
bool is_device_accessible() const
返回数据是否可由设备访问(例如,固定内存)
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)
从指针和大小构造。
constexpr base::reference back() const
返回跨度中最后一个元素的引用。
constexpr host_span(C &in)
constexpr CUDF_HOST_DEVICE host_span subspan(typename base::size_type offset, typename base::size_type count) const noexcept
获取一个子跨度,它是从此跨度中从 offset 开始的 count 个元素的视图。
#define CUDF_HOST_DEVICE
表示该函数或方法可在主机和设备上使用。