From 54bd64dfd9ab2ab297a23fa3b8247ffca5e14760 Mon Sep 17 00:00:00 2001 From: fis Date: Tue, 31 Mar 2020 21:36:07 +0800 Subject: [PATCH] Implement host span. --- include/xgboost/host_device_vector.h | 3 +++ tests/cpp/common/test_host_device_vector.cu | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/include/xgboost/host_device_vector.h b/include/xgboost/host_device_vector.h index b358c78899f9..b3ed36b99366 100644 --- a/include/xgboost/host_device_vector.h +++ b/include/xgboost/host_device_vector.h @@ -105,6 +105,9 @@ class HostDeviceVector { const T* DevicePointer() const { return ConstDevicePointer(); } T* HostPointer() { return HostVector().data(); } + common::Span HostSpan() { return common::Span{HostVector()}; } + common::Span HostSpan() const { return common::Span{HostVector()}; } + common::Span ConstHostSpan() const { return HostSpan(); } const T* ConstHostPointer() const { return ConstHostVector().data(); } const T* HostPointer() const { return ConstHostPointer(); } diff --git a/tests/cpp/common/test_host_device_vector.cu b/tests/cpp/common/test_host_device_vector.cu index 777f8f7c21de..b483edf3906e 100644 --- a/tests/cpp/common/test_host_device_vector.cu +++ b/tests/cpp/common/test_host_device_vector.cu @@ -165,6 +165,15 @@ TEST(HostDeviceVector, Span) { auto const_span = vec.ConstDeviceSpan(); ASSERT_EQ(vec.Size(), const_span.size()); ASSERT_EQ(vec.ConstDevicePointer(), const_span.data()); + + auto h_span = vec.ConstHostSpan(); + ASSERT_TRUE(vec.HostCanRead()); + ASSERT_FALSE(vec.HostCanWrite()); + ASSERT_EQ(h_span.size(), vec.Size()); + ASSERT_EQ(h_span.data(), vec.ConstHostPointer()); + + h_span = vec.HostSpan(); + ASSERT_TRUE(vec.HostCanWrite()); } TEST(HostDeviceVector, MGPU_Basic) {