diff --git a/include/xgboost/host_device_vector.h b/include/xgboost/host_device_vector.h index 75595187ef86..5bcceddcd9ca 100644 --- a/include/xgboost/host_device_vector.h +++ b/include/xgboost/host_device_vector.h @@ -95,6 +95,7 @@ class HostDeviceVector { HostDeviceVector& operator=(const HostDeviceVector&) = delete; HostDeviceVector& operator=(HostDeviceVector&&); + bool Empty() const { return Size() == 0; } size_t Size() const; int DeviceIdx() const; common::Span DeviceSpan(); diff --git a/src/common/host_device_vector.cu b/src/common/host_device_vector.cu index 3ffcf757e846..6afdadf39322 100644 --- a/src/common/host_device_vector.cu +++ b/src/common/host_device_vector.cu @@ -59,7 +59,9 @@ class HostDeviceVectorImpl { } } - size_t Size() const { return HostCanRead() ? data_h_.size() : data_d_->size(); } + size_t Size() const { + return HostCanRead() ? data_h_.size() : data_d_ ? data_d_->size() : 0; + } int DeviceIdx() const { return device_; } diff --git a/tests/cpp/common/test_host_device_vector.cu b/tests/cpp/common/test_host_device_vector.cu index e5ac339e794f..f380385852d5 100644 --- a/tests/cpp/common/test_host_device_vector.cu +++ b/tests/cpp/common/test_host_device_vector.cu @@ -176,6 +176,13 @@ TEST(HostDeviceVector, Span) { ASSERT_TRUE(vec.HostCanWrite()); } +TEST(HostDeviceVector, Empty) { + HostDeviceVector vec {1.0f, 2.0f, 3.0f, 4.0f}; + HostDeviceVector another { std::move(vec) }; + ASSERT_FALSE(another.Empty()); + ASSERT_TRUE(vec.Empty()); +} + TEST(HostDeviceVector, MGPU_Basic) { // NOLINT if (AllVisibleGPUs() < 2) { LOG(WARNING) << "Not testing in multi-gpu environment.";