Reduce span check overhead. (#5464)

This commit is contained in:
Jiaming Yuan 2020-04-01 22:07:24 +08:00 committed by GitHub
parent 15f40e51e9
commit babcb996e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 43 deletions

View File

@ -29,11 +29,13 @@
#ifndef XGBOOST_SPAN_H_ #ifndef XGBOOST_SPAN_H_
#define XGBOOST_SPAN_H_ #define XGBOOST_SPAN_H_
#include <xgboost/logging.h> // CHECK #include <xgboost/base.h>
#include <cinttypes> // size_t #include <cinttypes> // size_t
#include <numeric> // numeric_limits #include <limits> // numeric_limits
#include <iterator>
#include <type_traits> #include <type_traits>
#include <cstdio>
/*! /*!
* The version number 1910 is picked up from GSL. * The version number 1910 is picked up from GSL.
@ -69,26 +71,31 @@ namespace xgboost {
namespace common { namespace common {
// Usual logging facility is not available inside device code. // 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 // assert is not supported in mac as of CUDA 10.0
#define KERNEL_CHECK(cond) \ #define KERNEL_CHECK(cond) \
do { \ do { \
if (!(cond)) { \ if (!(cond)) { \
printf("\nKernel error:\n" \ printf("\nKernel error:\n" \
"In: %s: %d\n" \ "In: %s: %d\n" \
"\t%s\n\tExpecting: %s\n" \ "\t%s\n\tExpecting: %s\n" \
"\tBlock: [%d, %d, %d], Thread: [%d, %d, %d]\n\n", \ "\tBlock: [%d, %d, %d], Thread: [%d, %d, %d]\n\n", \
__FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, \ __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, blockIdx.x, \
blockIdx.x, blockIdx.y, blockIdx.z, \ blockIdx.y, blockIdx.z, threadIdx.x, threadIdx.y, threadIdx.z); \
threadIdx.x, threadIdx.y, threadIdx.z); \ asm("trap;"); \
asm("trap;"); \ } \
} \
} while (0); } while (0);
#ifdef __CUDA_ARCH__ #ifdef __CUDA_ARCH__
#define SPAN_CHECK KERNEL_CHECK #define SPAN_CHECK KERNEL_CHECK
#else #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__ #endif // __CUDA_ARCH__
namespace detail { namespace detail {

View File

@ -99,7 +99,7 @@ TEST(Span, FromPtrLen) {
{ {
auto lazy = [=]() {Span<float const, 16> tmp (arr, 5);}; auto lazy = [=]() {Span<float const, 16> tmp (arr, 5);};
EXPECT_ANY_THROW(lazy()); EXPECT_DEATH(lazy(), "\\[xgboost\\] Condition .* failed.\n");
} }
// dynamic extent // dynamic extent
@ -286,11 +286,11 @@ TEST(Span, ElementAccess) {
++j; ++j;
} }
EXPECT_ANY_THROW(s[16]); EXPECT_DEATH(s[16], "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s[-1]); EXPECT_DEATH(s[-1], "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s(16)); EXPECT_DEATH(s(16), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s(-1)); EXPECT_DEATH(s(-1), "\\[xgboost\\] Condition .* failed.\n");
} }
TEST(Span, Obversers) { TEST(Span, Obversers) {
@ -315,13 +315,13 @@ TEST(Span, FrontBack) {
{ {
Span<float, 0> s; Span<float, 0> s;
EXPECT_ANY_THROW(s.front()); EXPECT_DEATH(s.front(), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s.back()); EXPECT_DEATH(s.back(), "\\[xgboost\\] Condition .* failed.\n");
} }
{ {
Span<float> s; Span<float> s;
EXPECT_ANY_THROW(s.front()); EXPECT_DEATH(s.front(), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s.back()); EXPECT_DEATH(s.back(), "\\[xgboost\\] Condition .* failed.\n");
} }
} }
@ -341,9 +341,9 @@ TEST(Span, FirstLast) {
ASSERT_EQ(first[i], arr[i]); ASSERT_EQ(first[i], arr[i]);
} }
auto constexpr kOne = static_cast<Span<float, 4>::index_type>(-1); auto constexpr kOne = static_cast<Span<float, 4>::index_type>(-1);
EXPECT_ANY_THROW(s.first<kOne>()); EXPECT_DEATH(s.first<kOne>(), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s.first<17>()); EXPECT_DEATH(s.first<17>(), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s.first<32>()); EXPECT_DEATH(s.first<32>(), "\\[xgboost\\] Condition .* failed.\n");
} }
{ {
@ -360,9 +360,9 @@ TEST(Span, FirstLast) {
ASSERT_EQ(last[i], arr[i+12]); ASSERT_EQ(last[i], arr[i+12]);
} }
auto constexpr kOne = static_cast<Span<float, 4>::index_type>(-1); auto constexpr kOne = static_cast<Span<float, 4>::index_type>(-1);
EXPECT_ANY_THROW(s.last<kOne>()); EXPECT_DEATH(s.last<kOne>(), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s.last<17>()); EXPECT_DEATH(s.last<17>(), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s.last<32>()); EXPECT_DEATH(s.last<32>(), "\\[xgboost\\] Condition .* failed.\n");
} }
// dynamic extent // dynamic extent
@ -379,9 +379,9 @@ TEST(Span, FirstLast) {
ASSERT_EQ(first[i], s[i]); ASSERT_EQ(first[i], s[i]);
} }
EXPECT_ANY_THROW(s.first(-1)); EXPECT_DEATH(s.first(-1), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s.first(17)); EXPECT_DEATH(s.first(17), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s.first(32)); EXPECT_DEATH(s.first(32), "\\[xgboost\\] Condition .* failed.\n");
delete [] arr; delete [] arr;
} }
@ -399,9 +399,9 @@ TEST(Span, FirstLast) {
ASSERT_EQ(s[12 + i], last[i]); ASSERT_EQ(s[12 + i], last[i]);
} }
EXPECT_ANY_THROW(s.last(-1)); EXPECT_DEATH(s.last(-1), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s.last(17)); EXPECT_DEATH(s.last(17), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s.last(32)); EXPECT_DEATH(s.last(32), "\\[xgboost\\] Condition .* failed.\n");
delete [] arr; delete [] arr;
} }
@ -421,12 +421,12 @@ TEST(Span, Subspan) {
ASSERT_EQ(s1.data() + 2, s4.data()); ASSERT_EQ(s1.data() + 2, s4.data());
ASSERT_EQ(s4.size(), s1.size() - 2); ASSERT_EQ(s4.size(), s1.size() - 2);
EXPECT_ANY_THROW(s1.subspan(-1, 0)); EXPECT_DEATH(s1.subspan(-1, 0), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s1.subspan(16, 0)); EXPECT_DEATH(s1.subspan(16, 0), "\\[xgboost\\] Condition .* failed.\n");
auto constexpr kOne = static_cast<Span<int, 4>::index_type>(-1); auto constexpr kOne = static_cast<Span<int, 4>::index_type>(-1);
EXPECT_ANY_THROW(s1.subspan<kOne>()); EXPECT_DEATH(s1.subspan<kOne>(), "\\[xgboost\\] Condition .* failed.\n");
EXPECT_ANY_THROW(s1.subspan<16>()); EXPECT_DEATH(s1.subspan<16>(), "\\[xgboost\\] Condition .* failed.\n");
} }
TEST(Span, Compare) { TEST(Span, Compare) {

View File

@ -63,11 +63,11 @@ TEST(Transform, Exception) {
size_t const kSize {16}; size_t const kSize {16};
std::vector<bst_float> h_in(kSize); std::vector<bst_float> h_in(kSize);
const HostDeviceVector<bst_float> in_vec{h_in, -1}; 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]; }, Transform<>::Init([](size_t idx, common::Span<float const> _in) { _in[idx + 1]; },
Range(0, static_cast<Range::DifferenceType>(kSize)), -1) Range(0, static_cast<Range::DifferenceType>(kSize)), -1)
.Eval(&in_vec); .Eval(&in_vec);
}); }, "");
} }
#endif #endif