| xgboost | 
span 类实现,基于 ISO++20 span<T>。接口应该相同。 更多...
#include <span.h>

| 公共类型 | |
| 使用 | element_type = T | 
| 使用 | value_type = typename std::remove_cv< T >::type | 
| 使用 | index_type = std::size_t | 
| 使用 | difference_type = detail::ptrdiff_t | 
| 使用 | pointer = T * | 
| 使用 | reference = T & | 
| 使用 | iterator = detail::SpanIterator< Span< T, Extent >, false > | 
| 使用 | const_iterator = const detail::SpanIterator< Span< T, Extent >, true > | 
| 使用 | reverse_iterator = std::reverse_iterator< iterator > | 
| 使用 | const_reverse_iterator = const std::reverse_iterator< const_iterator > | 
| 公共成员函数 | |
| constexpr | Span ()=default | 
| XGBOOST_DEVICE | Span (pointer _ptr, index_type _count) | 
| XGBOOST_DEVICE | Span (pointer _first, pointer _last) | 
| template<std::size_t N> | |
| constexpr XGBOOST_DEVICE | Span (element_type(&arr)[N]) __span_noexcept | 
| template<class Container > | |
| Span (Container &_cont) | |
| template<class Container > | |
| Span (const Container &_cont) | |
| template<class U , std::size_t OtherExtent, class = typename std::enable_if_t< detail::IsAllowedElementTypeConversion<U, T>::value && detail::IsAllowedExtentConversion<OtherExtent, Extent>::value>> | |
| constexpr XGBOOST_DEVICE | Span (const Span< U, OtherExtent > &_other) __span_noexcept | 
| constexpr | Span (Span const &_other) noexcept(true)=default | 
| constexpr Span & | operator= (Span const &_other) noexcept(true)=default | 
| constexpr | Span (Span &&_other) noexcept(true)=default | 
| constexpr Span & | operator= (Span &&_other) noexcept(true)=default | 
| ~Span () noexcept(true)=default | |
| constexpr XGBOOST_DEVICE iterator | begin () const __span_noexcept | 
| constexpr XGBOOST_DEVICE iterator | end () const __span_noexcept | 
| constexpr XGBOOST_DEVICE const_iterator | cbegin () const __span_noexcept | 
| constexpr XGBOOST_DEVICE const_iterator | cend () const __span_noexcept | 
| constexpr reverse_iterator | rbegin () const __span_noexcept | 
| constexpr reverse_iterator | rend () const __span_noexcept | 
| constexpr XGBOOST_DEVICE const_reverse_iterator | crbegin () const __span_noexcept | 
| constexpr XGBOOST_DEVICE const_reverse_iterator | crend () const __span_noexcept | 
| XGBOOST_DEVICE reference | front () const | 
| XGBOOST_DEVICE reference | back () const | 
| XGBOOST_DEVICE reference | operator[] (index_type _idx) const | 
| XGBOOST_DEVICE reference | operator() (index_type _idx) const | 
| constexpr XGBOOST_DEVICE pointer | data () const __span_noexcept | 
| constexpr XGBOOST_DEVICE index_type | size () const __span_noexcept | 
| constexpr XGBOOST_DEVICE index_type | size_bytes () const __span_noexcept | 
| constexpr XGBOOST_DEVICE bool | empty () const __span_noexcept | 
| template<std::size_t Count> | |
| XGBOOST_DEVICE Span< element_type, Count > | first () const | 
| XGBOOST_DEVICE Span< element_type, dynamic_extent > | first (std::size_t _count) const | 
| template<std::size_t Count> | |
| XGBOOST_DEVICE Span< element_type, Count > | last () const | 
| XGBOOST_DEVICE Span< element_type, dynamic_extent > | last (std::size_t _count) const | 
| template<std::size_t Offset, std::size_t Count = dynamic_extent> | |
| XGBOOST_DEVICE auto | subspan () const -> Span< element_type, detail::ExtentValue< Extent, Offset, Count >::value > | 
| XGBOOST_DEVICE Span< element_type, dynamic_extent > | subspan (index_type _offset, index_type _count=dynamic_extent) const | 
span类实现,基于ISO++20 span<T>。接口应相同。
与 Guidelines Support Library (GSL) 中的 span<T> 有何不同
接口可能略有不同,我们遵循 ISO 标准。
GSL 使用 C++14/17 功能,此处不可用。GSL 广泛使用 constexpr,这在 C++11 的限制下是不可能实现的。GSL 不考虑 CUDA。
GSL 经过更彻底的实现和测试。GSL 经过更多优化,特别是针对静态范围。
GSL 在出错时使用 __buildin_unreachable(),Span<T> 使用 dmlc LOG 和自定义的 CUDA 日志记录。
与 ISO++20 (ISO) 中的 span<T> 有何不同
ISO 使用标准库中的函数/结构,这在 CUDA 中可能不可用。不支持从 std::array 初始化。
ISO 广泛使用 constexpr,这在 C++11 的限制下是不可能实现的。ISO 使用 C++14/17 功能,此处不可用。ISO 不考虑 CUDA。
ISO 使用 std::terminate(),Span<T> 使用 dmlc LOG 和自定义的 CUDA 日志记录。
限制:使用 Thrust:不建议直接使用 host_vector 初始化 Span,因为 host_vector::data() 是一个主机函数。不可能直接使用 device_vector 初始化 Span,因为 device_vector::data() 返回一个包装指针。不清楚哪种 Thrust 算法可以在没有内存错误的情况下使用。请参阅测试用例 "GPUSpan.WithTrust"
将迭代器传递给内核:不可能。请改用子范围。
SpanIterator 中的底层 Span 是一个指针,但 CUDA 通过值传递内核参数。如果我们要持有 Span 值而不是指针,以下代码片段将会崩溃,违反 Span 的安全目的
虽然持有指针或引用可以避免这个问题,但这是一种折衷。由于我们有子范围,所以不支持传递迭代器是可以接受的。
| 使用 xgboost::common::Span< T, Extent >::const_iterator = const detail::SpanIterator<Span<T, Extent>, true> | 
| 使用 xgboost::common::Span< T, Extent >::const_reverse_iterator = const std::reverse_iterator<const_iterator> | 
| 使用 xgboost::common::Span< T, Extent >::difference_type = detail::ptrdiff_t | 
| 使用 xgboost::common::Span< T, Extent >::element_type = T | 
| 使用 xgboost::common::Span< T, Extent >::index_type = std::size_t | 
| 使用 xgboost::common::Span< T, Extent >::iterator = detail::SpanIterator<Span<T, Extent>, false> | 
| 使用 xgboost::common::Span< T, Extent >::pointer = T* | 
| 使用 xgboost::common::Span< T, Extent >::reference = T& | 
| 使用 xgboost::common::Span< T, Extent >::reverse_iterator = std::reverse_iterator<iterator> | 
| 使用 xgboost::common::Span< T, Extent >::value_type = typename std::remove_cv<T>::type | 
| 
 | constexprdefault | 
| 
 | inline | 
| 
 | inline | 
| 
 | inlineconstexpr | 
| 
 | inline | 
| 
 | inline | 
| 
 | inlineconstexpr | 
| 
 | constexprdefaultnoexcept | 
| 
 | constexprdefaultnoexcept | 
| 
 | defaultnoexcept | 
| 
 | inline | 
| 
 | inlineconstexpr | 
| 
 | inlineconstexpr | 
| 
 | inlineconstexpr | 
| 
 | inlineconstexpr | 
| 
 | inlineconstexpr | 
| 
 | inlineconstexpr | 
| 
 | inlineconstexpr | 
| 
 | inlineconstexpr | 
| 
 | inline | 
| 
 | inline | 
| 
 | inline | 
| 
 | inline | 
| 
 | inline | 
| 
 | inline | 
| 
 | constexprdefaultnoexcept | 
| 
 | constexprdefaultnoexcept | 
| 
 | inline | 
| 
 | inlineconstexpr | 
| 
 | inlineconstexpr | 
| 
 | inlineconstexpr | 
| 
 | inlineconstexpr | 
| 
 | inline | 
如果 Count 为 std::dynamic_extent,则 r.size() == this->size() - Offset;否则 r.size() == Count。
| 
 | inline |