/*! Copyright 2019-2021 by XGBoost Contributors */ #include #include #include #include #include #include "test_array_interface.h" #include "../../../src/common/device_helpers.cuh" #include "test_metainfo.h" namespace xgboost { template std::string PrepareData(std::string typestr, thrust::device_vector* out, const size_t kRows=16) { out->resize(kRows); auto& d_data = *out; for (size_t i = 0; i < d_data.size(); ++i) { d_data[i] = i * 2.0; } Json column { Object() }; std::vector j_shape {Json(Integer(static_cast(kRows)))}; column["shape"] = Array(j_shape); column["strides"] = Array(std::vector{Json(Integer{static_cast(sizeof(T))})}); column["version"] = 3; column["typestr"] = String(typestr); auto p_d_data = d_data.data().get(); std::vector j_data{Json(Integer{reinterpret_cast(p_d_data)}), Json(Boolean(false))}; column["data"] = j_data; column["stream"] = nullptr; Json array(std::vector{column}); std::string str; Json::Dump(array, &str); return str; } TEST(MetaInfo, FromInterface) { cudaSetDevice(0); Context ctx; thrust::device_vector d_data; std::string str = PrepareData(" d_group_data; std::string group_str = PrepareData(" expected_group_ptr = {0, 4, 7, 9, 10}; EXPECT_EQ(info.group_ptr_, expected_group_ptr); } TEST(MetaInfo, GPUStridedData) { TestMetaInfoStridedData(0); } TEST(MetaInfo, Group) { cudaSetDevice(0); MetaInfo info; Context ctx; thrust::device_vector d_uint; std::string uint_str = PrepareData(" d_int64; std::string int_str = PrepareData(" d_float; std::string float_str = PrepareData(" qid(info.num_row_, 0); for (size_t i = 0; i < qid.size(); ++i) { qid[i] = i; } auto column = Generate2dArrayInterface(info.num_row_, 1, "{column}}; std::string array_str; Json::Dump(array, &array_str); info.SetInfo(ctx, "qid", array_str.c_str()); ASSERT_EQ(info.group_ptr_.size(), info.num_row_ + 1); ASSERT_EQ(info.group_ptr_.front(), 0); ASSERT_EQ(info.group_ptr_.back(), info.num_row_); for (size_t i = 0; i < info.num_row_ + 1; ++i) { ASSERT_EQ(info.group_ptr_[i], i); } } TEST(MetaInfo, DeviceExtend) { dh::safe_cuda(cudaSetDevice(0)); size_t const kRows = 100; MetaInfo lhs, rhs; Context ctx; thrust::device_vector d_data; std::string str = PrepareData("HostCanRead()); lhs.num_row_ = kRows; rhs.num_row_ = kRows; lhs.Extend(rhs, true, true); ASSERT_EQ(lhs.num_row_, kRows * 2); ASSERT_FALSE(lhs.labels.Data()->HostCanRead()); ASSERT_FALSE(lhs.labels.Data()->HostCanRead()); ASSERT_FALSE(rhs.labels.Data()->HostCanRead()); } } // namespace xgboost