Skip to content

Commit

Permalink
Reduce span check overhead. (#5464)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis committed Apr 1, 2020
1 parent 15f40e5 commit babcb99
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 43 deletions.
39 changes: 23 additions & 16 deletions include/xgboost/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
#ifndef XGBOOST_SPAN_H_
#define XGBOOST_SPAN_H_

#include <xgboost/logging.h> // CHECK
#include <xgboost/base.h>

#include <cinttypes> // size_t
#include <numeric> // numeric_limits
#include <limits> // numeric_limits
#include <iterator>
#include <type_traits>
#include <cstdio>

/*!
* The version number 1910 is picked up from GSL.
Expand Down Expand Up @@ -69,26 +71,31 @@ namespace xgboost {
namespace common {

// Usual logging facility is not available inside device code.
// TODO(trivialfis): Make dmlc check more generic.
// assert is not supported in mac as of CUDA 10.0
#define KERNEL_CHECK(cond) \
do { \
if (!(cond)) { \
printf("\nKernel error:\n" \
"In: %s: %d\n" \
"\t%s\n\tExpecting: %s\n" \
"\tBlock: [%d, %d, %d], Thread: [%d, %d, %d]\n\n", \
__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, \
blockIdx.x, blockIdx.y, blockIdx.z, \
threadIdx.x, threadIdx.y, threadIdx.z); \
asm("trap;"); \
} \
#define KERNEL_CHECK(cond) \
do { \
if (!(cond)) { \
printf("\nKernel error:\n" \
"In: %s: %d\n" \
"\t%s\n\tExpecting: %s\n" \
"\tBlock: [%d, %d, %d], Thread: [%d, %d, %d]\n\n", \
__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, blockIdx.x, \
blockIdx.y, blockIdx.z, threadIdx.x, threadIdx.y, threadIdx.z); \
asm("trap;"); \
} \
} while (0);

#ifdef __CUDA_ARCH__
#define SPAN_CHECK KERNEL_CHECK
#else
#define SPAN_CHECK CHECK // check from dmlc
#define SPAN_CHECK(cond) \
do { \
if (XGBOOST_EXPECT(!(cond), false)) { \
fprintf(stderr, "[xgboost] Condition %s failed.\n", #cond); \
fflush(stderr); /* It seems stderr on Windows is beffered? */ \
std::terminate(); \
} \
} while (0);
#endif // __CUDA_ARCH__

namespace detail {
Expand Down
50 changes: 25 additions & 25 deletions tests/cpp/common/test_span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ TEST(Span, FromPtrLen) {

{
auto lazy = [=]() {Span<float const, 16> tmp (arr, 5);};
EXPECT_ANY_THROW(lazy());
EXPECT_DEATH(lazy(), "\\[xgboost\\] Condition .* failed.\n");
}

// dynamic extent
Expand Down Expand Up @@ -286,11 +286,11 @@ TEST(Span, ElementAccess) {
++j;
}

EXPECT_ANY_THROW(s[16]);
EXPECT_ANY_THROW(s[-1]);
EXPECT_DEATH(s[16], "\\[xgboost\\] Condition .* failed.\n");
EXPECT_DEATH(s[-1], "\\[xgboost\\] Condition .* failed.\n");

EXPECT_ANY_THROW(s(16));
EXPECT_ANY_THROW(s(-1));
EXPECT_DEATH(s(16), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_DEATH(s(-1), "\\[xgboost\\] Condition .* failed.\n");
}

TEST(Span, Obversers) {
Expand All @@ -315,13 +315,13 @@ TEST(Span, FrontBack) {

{
Span<float, 0> s;
EXPECT_ANY_THROW(s.front());
EXPECT_ANY_THROW(s.back());
EXPECT_DEATH(s.front(), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_DEATH(s.back(), "\\[xgboost\\] Condition .* failed.\n");
}
{
Span<float> s;
EXPECT_ANY_THROW(s.front());
EXPECT_ANY_THROW(s.back());
EXPECT_DEATH(s.front(), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_DEATH(s.back(), "\\[xgboost\\] Condition .* failed.\n");
}
}

Expand All @@ -341,9 +341,9 @@ TEST(Span, FirstLast) {
ASSERT_EQ(first[i], arr[i]);
}
auto constexpr kOne = static_cast<Span<float, 4>::index_type>(-1);
EXPECT_ANY_THROW(s.first<kOne>());
EXPECT_ANY_THROW(s.first<17>());
EXPECT_ANY_THROW(s.first<32>());
EXPECT_DEATH(s.first<kOne>(), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_DEATH(s.first<17>(), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_DEATH(s.first<32>(), "\\[xgboost\\] Condition .* failed.\n");
}

{
Expand All @@ -360,9 +360,9 @@ TEST(Span, FirstLast) {
ASSERT_EQ(last[i], arr[i+12]);
}
auto constexpr kOne = static_cast<Span<float, 4>::index_type>(-1);
EXPECT_ANY_THROW(s.last<kOne>());
EXPECT_ANY_THROW(s.last<17>());
EXPECT_ANY_THROW(s.last<32>());
EXPECT_DEATH(s.last<kOne>(), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_DEATH(s.last<17>(), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_DEATH(s.last<32>(), "\\[xgboost\\] Condition .* failed.\n");
}

// dynamic extent
Expand All @@ -379,9 +379,9 @@ TEST(Span, FirstLast) {
ASSERT_EQ(first[i], s[i]);
}

EXPECT_ANY_THROW(s.first(-1));
EXPECT_ANY_THROW(s.first(17));
EXPECT_ANY_THROW(s.first(32));
EXPECT_DEATH(s.first(-1), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_DEATH(s.first(17), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_DEATH(s.first(32), "\\[xgboost\\] Condition .* failed.\n");

delete [] arr;
}
Expand All @@ -399,9 +399,9 @@ TEST(Span, FirstLast) {
ASSERT_EQ(s[12 + i], last[i]);
}

EXPECT_ANY_THROW(s.last(-1));
EXPECT_ANY_THROW(s.last(17));
EXPECT_ANY_THROW(s.last(32));
EXPECT_DEATH(s.last(-1), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_DEATH(s.last(17), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_DEATH(s.last(32), "\\[xgboost\\] Condition .* failed.\n");

delete [] arr;
}
Expand All @@ -421,12 +421,12 @@ TEST(Span, Subspan) {
ASSERT_EQ(s1.data() + 2, s4.data());
ASSERT_EQ(s4.size(), s1.size() - 2);

EXPECT_ANY_THROW(s1.subspan(-1, 0));
EXPECT_ANY_THROW(s1.subspan(16, 0));
EXPECT_DEATH(s1.subspan(-1, 0), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_DEATH(s1.subspan(16, 0), "\\[xgboost\\] Condition .* failed.\n");

auto constexpr kOne = static_cast<Span<int, 4>::index_type>(-1);
EXPECT_ANY_THROW(s1.subspan<kOne>());
EXPECT_ANY_THROW(s1.subspan<16>());
EXPECT_DEATH(s1.subspan<kOne>(), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_DEATH(s1.subspan<16>(), "\\[xgboost\\] Condition .* failed.\n");
}

TEST(Span, Compare) {
Expand Down
4 changes: 2 additions & 2 deletions tests/cpp/common/test_transform_range.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ TEST(Transform, Exception) {
size_t const kSize {16};
std::vector<bst_float> h_in(kSize);
const HostDeviceVector<bst_float> in_vec{h_in, -1};
EXPECT_ANY_THROW({
EXPECT_DEATH({
Transform<>::Init([](size_t idx, common::Span<float const> _in) { _in[idx + 1]; },
Range(0, static_cast<Range::DifferenceType>(kSize)), -1)
.Eval(&in_vec);
});
}, "");
}
#endif

Expand Down

0 comments on commit babcb99

Please sign in to comment.