Skip to content

Commit

Permalink
Fix test cases, some naming issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis committed Aug 4, 2018
1 parent be517fd commit 9ac5392
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 52 deletions.
66 changes: 36 additions & 30 deletions src/common/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace common {
#ifdef __CUDA_ARCH__
#define SPAN_CHECK KERNEL_CHECK
#else
#define SPAN_CHECK CHECK
#define SPAN_CHECK CHECK // check from dmlc
#endif

constexpr const std::ptrdiff_t dynamic_extent = -1;
Expand All @@ -60,15 +60,15 @@ class Span;

template <typename SpanType, bool IsConst>
class SpanIterator {
using element_type_ = typename SpanType::element_type;
using ElementType_ = typename SpanType::element_type;

public:
using iterator_category = std::random_access_iterator_tag;
using value_type = typename std::remove_cv<element_type_>::type;
using value_type = typename std::remove_cv<ElementType_>::type;
using difference_type = typename SpanType::index_type;

using reference = typename std::conditional<
IsConst, const element_type_, element_type_>::type&;
IsConst, const ElementType_, ElementType_>::type&;
using pointer = typename std::add_pointer<reference>::type&;

XGBOOST_DEVICE SpanIterator() {}
Expand Down Expand Up @@ -211,21 +211,21 @@ constexpr T narrow_cast(U&& u) noexcept {
return static_cast<T>(std::forward<U>(u));
}

template<typename T>
template <typename T>
struct Less {
XGBOOST_DEVICE constexpr bool operator()(const T& _x, const T& _y) const {
return _x < _y;
}
};

template<typename T>
template <typename T>
struct Greater {
XGBOOST_DEVICE constexpr bool operator()(const T& _x, const T& _y) const {
return _x > _y;
}
};

template<class InputIt1, class InputIt2,
template <class InputIt1, class InputIt2,
class Compare =
detail::Less<decltype(std::declval<InputIt1>().operator*())>>
XGBOOST_DEVICE bool lexicographical_compare(InputIt1 first1, InputIt1 last1,
Expand Down Expand Up @@ -259,6 +259,9 @@ XGBOOST_DEVICE bool lexicographical_compare(InputIt1 first1, InputIt1 last1,
* GSL is more thoroughly implemented and tested.
* GSL is more optimized, especially for static extent.
*
* GSL uses __buildin_unreachable() when error, Span<T> uses dmlc LOG and
* customized CUDA logging.
*
* What's different from span<T> in ISO++20 (ISO)
* ISO uses functions/structs from std library, which might be not available
* in CUDA.
Expand All @@ -267,11 +270,14 @@ XGBOOST_DEVICE bool lexicographical_compare(InputIt1 first1, InputIt1 last1,
* ISO uses C++14/17 features, which is not available here.
* ISO doesn't concern about CUDA.
*
* ISO uses std::terminate(), Span<T> uses dmlc LOG and customized CUDA
* logging.
*
* Limitations:
* With thrust:
* It's not adviced to initialize Span with host_vector directly, since
* host_vector::data() is a host function.
* It's not possible to initialize Span with device_vector, since
* It's not possible to initialize Span with device_vector directly, since
* device_vector::data() returns a wrapped pointer.
* It's unclear that what kind of thrust algorithm can be used without
* memory error. See the test case "GPUSpan.WithTrust"
Expand Down Expand Up @@ -299,12 +305,12 @@ class Span {
XGBOOST_DEVICE constexpr Span() noexcept :
data_(nullptr), size_(0) {}

XGBOOST_DEVICE Span(pointer ptr, index_type count) {
SPAN_CHECK(count >= 0);
SPAN_CHECK(ptr || count == 0);
XGBOOST_DEVICE Span(pointer _ptr, index_type _count) {
SPAN_CHECK(_count >= 0);
SPAN_CHECK(_ptr || _count == 0);

data_ = ptr;
size_ = count;
data_ = _ptr;
size_ = _count;
}

XGBOOST_DEVICE Span(pointer _first, pointer _last) :
Expand Down Expand Up @@ -381,14 +387,13 @@ class Span {
return const_reverse_iterator{cbegin()};
}

XGBOOST_DEVICE reference operator[](index_type idx) const {
SPAN_CHECK(idx >= 0 && idx < size());
SPAN_CHECK(idx >= 0 && idx < size());
return data()[idx];
XGBOOST_DEVICE reference operator[](index_type _idx) const {
SPAN_CHECK(_idx >= 0 && _idx < size());
return data()[_idx];
}

XGBOOST_DEVICE constexpr reference operator()(index_type idx) const {
return this->operator[](idx);
XGBOOST_DEVICE constexpr reference operator()(index_type _idx) const {
return this->operator[](_idx);
}

XGBOOST_DEVICE constexpr pointer data() const noexcept {
Expand All @@ -408,7 +413,7 @@ class Span {
}

// Subviews
template< std::ptrdiff_t Count >
template <std::ptrdiff_t Count >
XGBOOST_DEVICE Span<element_type, Count> first() const {
SPAN_CHECK(Count >= 0 && Count <= size());
return {data(), Count};
Expand All @@ -420,7 +425,7 @@ class Span {
return {data(), _count};
}

template< std::ptrdiff_t Count >
template < std::ptrdiff_t Count >
XGBOOST_DEVICE Span<element_type, Count> last() const {
SPAN_CHECK(Count >=0 && size() - Count >= 0);
return {data() + size() - Count, Count};
Expand All @@ -436,7 +441,7 @@ class Span {
* If Count is std::dynamic_extent, r.size() == this->size() - Offset;
* Otherwise r.size() == Count.
*/
template< std::ptrdiff_t Offset,
template < std::ptrdiff_t Offset,
std::ptrdiff_t Count = dynamic_extent >
XGBOOST_DEVICE auto subspan() const ->
Span<element_type,
Expand All @@ -449,13 +454,14 @@ class Span {
}

XGBOOST_DEVICE Span<element_type, dynamic_extent>
subspan(std::ptrdiff_t Offset,
std::ptrdiff_t Count = dynamic_extent) const {
SPAN_CHECK(Offset >= 0 && Offset < size());
SPAN_CHECK(Count == dynamic_extent ||
Count >= 0 && Offset + Count <= size());
subspan(std::ptrdiff_t _offset,
std::ptrdiff_t _count = dynamic_extent) const {
SPAN_CHECK(_offset >= 0 && _offset < size());
SPAN_CHECK(_count == dynamic_extent ||
_count >= 0 && _offset + _count <= size());

return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
return {data() + _offset, _count ==
dynamic_extent ? size() - _offset : _count};
}

private:
Expand Down Expand Up @@ -506,13 +512,13 @@ XGBOOST_DEVICE constexpr bool operator>=(Span<T, X> l, Span<U, Y> r) {
return !(l < r);
}

template< class T, std::ptrdiff_t E>
template <class T, std::ptrdiff_t E>
XGBOOST_DEVICE auto as_bytes(Span<T, E> s) noexcept ->
Span<const byte, detail::calculate_extent_as_bytes<T>(E)> {
return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
}

template< class T, std::ptrdiff_t E>
template <class T, std::ptrdiff_t E>
XGBOOST_DEVICE auto as_writable_bytes(Span<T, E> s) noexcept ->
Span<byte, detail::calculate_extent_as_bytes<T>(E)> {
return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
Expand Down
20 changes: 10 additions & 10 deletions tests/cpp/common/test_span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ TEST(Span, Constructors) {
ASSERT_EQ(s.size(), 0);
ASSERT_EQ(s.data(), nullptr);

Span<int> cs;
Span<int const> cs;
ASSERT_EQ(cs.size(), 0);
ASSERT_EQ(cs.data(), nullptr);
}
Expand All @@ -34,7 +34,7 @@ TEST(Span, Constructors) {
ASSERT_EQ(s.size(), 0);
ASSERT_EQ(s.data(), nullptr);

Span<const int, 0> cs;
Span<int const, 0> cs;
ASSERT_EQ(cs.size(), 0);
ASSERT_EQ(cs.data(), nullptr);
}
Expand All @@ -45,7 +45,7 @@ TEST(Span, Constructors) {
ASSERT_EQ(s.size(), 0);
ASSERT_EQ(s.data(), nullptr);

Span<const int> cs {};
Span<int const> cs {};
ASSERT_EQ(cs.size(), 0);
ASSERT_EQ(cs.data(), nullptr);
}
Expand All @@ -58,7 +58,7 @@ TEST(Span, FromNullPtr) {
ASSERT_EQ(s.size(), 0);
ASSERT_EQ(s.data(), nullptr);

Span<float> cs {nullptr, static_cast<Span<float>::index_type>(0)};
Span<float const> cs {nullptr, static_cast<Span<float>::index_type>(0)};
ASSERT_EQ(cs.size(), 0);
ASSERT_EQ(cs.data(), nullptr);
}
Expand All @@ -68,7 +68,7 @@ TEST(Span, FromNullPtr) {
ASSERT_EQ(s.size(), 0);
ASSERT_EQ(s.data(), nullptr);

Span<float, 0> cs {nullptr, static_cast<Span<float>::index_type>(0)};
Span<float const, 0> cs {nullptr, static_cast<Span<float>::index_type>(0)};
ASSERT_EQ(cs.size(), 0);
ASSERT_EQ(cs.data(), nullptr);
}
Expand All @@ -84,15 +84,15 @@ TEST(Span, FromPtrLen) {
ASSERT_EQ (s.size(), 16);
ASSERT_EQ (s.data(), arr);

for (size_t i = 0; i < 16; ++i) {
for (Span<float>::index_type i = 0; i < 16; ++i) {
ASSERT_EQ (s[i], arr[i]);
}

Span<const float> cs (arr, 16);
Span<float const> cs (arr, 16);
ASSERT_EQ (cs.size(), 16);
ASSERT_EQ (cs.data(), arr);

for (Span<const float>::index_type i = 0; i < 16; ++i) {
for (Span<float const>::index_type i = 0; i < 16; ++i) {
ASSERT_EQ (cs[i], arr[i]);
}
}
Expand All @@ -111,11 +111,11 @@ TEST(Span, FromPtrLen) {
ASSERT_EQ (s[i], arr[i]);
}

Span<const float, 16> cs (arr, 16);
Span<float const, 16> cs (arr, 16);
ASSERT_EQ (cs.size(), 16);
ASSERT_EQ (cs.data(), arr);

for (Span<const float>::index_type i = 0; i < 16; ++i) {
for (Span<float const>::index_type i = 0; i < 16; ++i) {
ASSERT_EQ (cs[i], arr[i]);
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/cpp/common/test_span.cu
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*!
* Copyright 2018 XGBoost contributors
*/
#include <stdio.h>
#include <gtest/gtest.h>

#include <thrust/host_vector.h>
Expand Down
22 changes: 11 additions & 11 deletions tests/cpp/common/test_span.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct TestTestStatus {
XGBOOST_DEVICE void operator()() {
this->operator()(0);
}
XGBOOST_DEVICE void operator()(int idx) {
XGBOOST_DEVICE void operator()(int _idx) {
SPAN_ASSERT_TRUE(false, status_);
}
};
Expand All @@ -49,7 +49,7 @@ struct TestBeginEnd {
XGBOOST_DEVICE void operator()() {
this->operator()(0);
}
XGBOOST_DEVICE void operator()(int idx) {
XGBOOST_DEVICE void operator()(int _idx) {
float arr[16];
InitializeRange(arr, arr + 16);

Expand All @@ -71,7 +71,7 @@ struct TestRBeginREnd {
XGBOOST_DEVICE void operator()() {
this->operator()(0);
}
XGBOOST_DEVICE void operator()(int idx) {
XGBOOST_DEVICE void operator()(int _idx) {
float arr[16];
InitializeRange(arr, arr + 16);

Expand All @@ -93,7 +93,7 @@ struct TestObservers {
XGBOOST_DEVICE void operator()() {
this->operator()(0);
}
XGBOOST_DEVICE void operator()(int idx) {
XGBOOST_DEVICE void operator()(int _idx) {
// empty
{
float *arr = nullptr;
Expand All @@ -120,7 +120,7 @@ struct TestCompare {
XGBOOST_DEVICE void operator()() {
this->operator()(0);
}
XGBOOST_DEVICE void operator()(int idx) {
XGBOOST_DEVICE void operator()(int _idx) {
float lhs_arr[16], rhs_arr[16];
InitializeRange(lhs_arr, lhs_arr + 16);
InitializeRange(rhs_arr, rhs_arr + 16);
Expand Down Expand Up @@ -149,7 +149,7 @@ struct TestIterConstruct {
XGBOOST_DEVICE void operator()() {
this->operator()(0);
}
XGBOOST_DEVICE void operator()(int idx) {
XGBOOST_DEVICE void operator()(int _idx) {
Span<float>::iterator it1;
Span<float>::iterator it2;
SPAN_ASSERT_TRUE(it1 == it2, status_);
Expand All @@ -168,7 +168,7 @@ struct TestIterRef {
XGBOOST_DEVICE void operator()() {
this->operator()(0);
}
XGBOOST_DEVICE void operator()(int idx) {
XGBOOST_DEVICE void operator()(int _idx) {
float arr[16];
InitializeRange(arr, arr + 16);

Expand All @@ -186,7 +186,7 @@ struct TestIterCalculate {
XGBOOST_DEVICE void operator()() {
this->operator()(0);
}
XGBOOST_DEVICE void operator()(int idx) {
XGBOOST_DEVICE void operator()(int _idx) {
float arr[16];
InitializeRange(arr, arr + 16);

Expand Down Expand Up @@ -215,7 +215,7 @@ struct TestIterCompare {
XGBOOST_DEVICE void operator()() {
this->operator()(0);
}
XGBOOST_DEVICE void operator()(int idx) {
XGBOOST_DEVICE void operator()(int _idx) {
float arr[16];
InitializeRange(arr, arr + 16);
Span<float> s (arr);
Expand Down Expand Up @@ -245,7 +245,7 @@ struct TestAsBytes {
XGBOOST_DEVICE void operator()() {
this->operator()(0);
}
XGBOOST_DEVICE void operator()(int idx) {
XGBOOST_DEVICE void operator()(int _idx) {
float arr[16];
InitializeRange(arr, arr + 16);

Expand Down Expand Up @@ -280,7 +280,7 @@ struct TestAsWritableBytes {
XGBOOST_DEVICE void operator()() {
this->operator()(0);
}
XGBOOST_DEVICE void operator()(int idx) {
XGBOOST_DEVICE void operator()(int _idx) {
float arr[16];
InitializeRange(arr, arr + 16);

Expand Down

0 comments on commit 9ac5392

Please sign in to comment.