From 8de7f1928e4815843fbf8773a5ac7ecbc37b2e15 Mon Sep 17 00:00:00 2001 From: Philip Hyunsu Cho Date: Wed, 29 Apr 2020 21:56:34 -0700 Subject: [PATCH] Fix build on big endian CPUs (#5617) * Fix build on big endian CPUs * Clang-tidy --- include/xgboost/data.h | 19 ++++++++++++++++++- src/data/data.cc | 12 ++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/include/xgboost/data.h b/include/xgboost/data.h index 7a09439c5..e6754b9e2 100644 --- a/include/xgboost/data.h +++ b/include/xgboost/data.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -554,5 +555,21 @@ inline BatchSet DMatrix::GetBatches(const BatchParam& param) { namespace dmlc { DMLC_DECLARE_TRAITS(is_pod, xgboost::Entry, true); -} + +namespace serializer { + +template <> +struct Handler { + inline static void Write(Stream* strm, const xgboost::Entry& data) { + strm->Write(data.index); + strm->Write(data.fvalue); + } + + inline static bool Read(Stream* strm, xgboost::Entry* data) { + return strm->Read(&data->index) && strm->Read(&data->fvalue); + } +}; + +} // namespace serializer +} // namespace dmlc #endif // XGBOOST_DATA_H_ diff --git a/src/data/data.cc b/src/data/data.cc index 2e7b710a9..2a620b7d4 100644 --- a/src/data/data.cc +++ b/src/data/data.cc @@ -37,7 +37,7 @@ template void SaveScalarField(dmlc::Stream *strm, const std::string &name, xgboost::DataType type, const T &field) { strm->Write(name); - strm->Write(type); + strm->Write(static_cast(type)); strm->Write(true); // is_scalar=True strm->Write(field); } @@ -47,7 +47,7 @@ void SaveVectorField(dmlc::Stream *strm, const std::string &name, xgboost::DataType type, std::pair shape, const std::vector& field) { strm->Write(name); - strm->Write(type); + strm->Write(static_cast(type)); strm->Write(false); // is_scalar=False strm->Write(shape.first); strm->Write(shape.second); @@ -71,7 +71,9 @@ void LoadScalarField(dmlc::Stream* strm, const std::string& expected_name, CHECK(strm->Read(&name)) << invalid; CHECK_EQ(name, expected_name) << invalid << " Expected field: " << expected_name << ", got: " << name; - CHECK(strm->Read(&type)) << invalid; + uint8_t type_val; + CHECK(strm->Read(&type_val)) << invalid; + type = static_cast(type_val); CHECK(type == expected_type) << invalid << "Expected field of type: " << static_cast(expected_type) << ", " << "got field type: " << static_cast(type); @@ -91,7 +93,9 @@ void LoadVectorField(dmlc::Stream* strm, const std::string& expected_name, CHECK(strm->Read(&name)) << invalid; CHECK_EQ(name, expected_name) << invalid << " Expected field: " << expected_name << ", got: " << name; - CHECK(strm->Read(&type)) << invalid; + uint8_t type_val; + CHECK(strm->Read(&type_val)) << invalid; + type = static_cast(type_val); CHECK(type == expected_type) << invalid << "Expected field of type: " << static_cast(expected_type) << ", " << "got field type: " << static_cast(type);