From 25383b7ed6575ce49cbf87f7b1665c0a97239773 Mon Sep 17 00:00:00 2001 From: fis Date: Mon, 30 Mar 2020 23:23:48 +0800 Subject: [PATCH] Split up test helpers header. --- tests/cpp/data/test_ellpack_page.cu | 1 + tests/cpp/helpers.h | 53 +---------------------------- tests/cpp/histogram_helpers.h | 53 +++++++++++++++++++++++++++++ tests/cpp/tree/test_gpu_hist.cu | 3 +- 4 files changed, 57 insertions(+), 53 deletions(-) create mode 100644 tests/cpp/histogram_helpers.h diff --git a/tests/cpp/data/test_ellpack_page.cu b/tests/cpp/data/test_ellpack_page.cu index 93942cb883db..a713979e3b87 100644 --- a/tests/cpp/data/test_ellpack_page.cu +++ b/tests/cpp/data/test_ellpack_page.cu @@ -6,6 +6,7 @@ #include #include "../helpers.h" +#include "../histogram_helpers.h" #include "gtest/gtest.h" #include "../../../src/common/hist_util.h" diff --git a/tests/cpp/helpers.h b/tests/cpp/helpers.h index 42bc8dde91ce..d8bcec58703f 100644 --- a/tests/cpp/helpers.h +++ b/tests/cpp/helpers.h @@ -18,13 +18,9 @@ #include #include #include -#include #include "../../src/common/common.h" #include "../../src/gbm/gbtree_model.h" -#if defined(__CUDACC__) -#include "../../src/data/ellpack_page.cuh" -#endif #if defined(__CUDACC__) #define DeclareUnifiedTest(name) GPU ## name @@ -42,6 +38,7 @@ namespace xgboost { class ObjFunction; class Metric; struct LearnerModelParam; +class GradientBooster; } bool FileExists(const std::string& filename); @@ -271,53 +268,5 @@ inline HostDeviceVector GenerateRandomGradients(const size_t n_row HostDeviceVector gpair(h_gpair); return gpair; } - -#if defined(__CUDACC__) -namespace { -class HistogramCutsWrapper : public common::HistogramCuts { - public: - using SuperT = common::HistogramCuts; - void SetValues(std::vector cuts) { - SuperT::cut_values_.HostVector() = std::move(cuts); - } - void SetPtrs(std::vector ptrs) { - SuperT::cut_ptrs_.HostVector() = std::move(ptrs); - } - void SetMins(std::vector mins) { - SuperT::min_vals_.HostVector() = std::move(mins); - } -}; -} // anonymous namespace - -inline std::unique_ptr BuildEllpackPage( - int n_rows, int n_cols, bst_float sparsity= 0) { - auto dmat = RandomDataGenerator(n_rows, n_cols, sparsity).Seed(3).GenerateDMatix(); - const SparsePage& batch = *dmat->GetBatches().begin(); - - HistogramCutsWrapper cmat; - cmat.SetPtrs({0, 3, 6, 9, 12, 15, 18, 21, 24}); - // 24 cut fields, 3 cut fields for each feature (column). - cmat.SetValues({0.30f, 0.67f, 1.64f, - 0.32f, 0.77f, 1.95f, - 0.29f, 0.70f, 1.80f, - 0.32f, 0.75f, 1.85f, - 0.18f, 0.59f, 1.69f, - 0.25f, 0.74f, 2.00f, - 0.26f, 0.74f, 1.98f, - 0.26f, 0.71f, 1.83f}); - cmat.SetMins({0.1f, 0.2f, 0.3f, 0.1f, 0.2f, 0.3f, 0.2f, 0.2f}); - - bst_row_t row_stride = 0; - const auto &offset_vec = batch.offset.ConstHostVector(); - for (size_t i = 1; i < offset_vec.size(); ++i) { - row_stride = std::max(row_stride, offset_vec[i] - offset_vec[i-1]); - } - - auto page = std::unique_ptr( - new EllpackPageImpl(0, cmat, batch, dmat->IsDense(), row_stride)); - - return page; -} -#endif } // namespace xgboost #endif diff --git a/tests/cpp/histogram_helpers.h b/tests/cpp/histogram_helpers.h new file mode 100644 index 000000000000..86a46c6da583 --- /dev/null +++ b/tests/cpp/histogram_helpers.h @@ -0,0 +1,53 @@ +#if defined(__CUDACC__) +#include "../../src/data/ellpack_page.cuh" +#endif + +namespace xgboost { +#if defined(__CUDACC__) +namespace { +class HistogramCutsWrapper : public common::HistogramCuts { + public: + using SuperT = common::HistogramCuts; + void SetValues(std::vector cuts) { + SuperT::cut_values_.HostVector() = std::move(cuts); + } + void SetPtrs(std::vector ptrs) { + SuperT::cut_ptrs_.HostVector() = std::move(ptrs); + } + void SetMins(std::vector mins) { + SuperT::min_vals_.HostVector() = std::move(mins); + } +}; +} // anonymous namespace + +inline std::unique_ptr BuildEllpackPage( + int n_rows, int n_cols, bst_float sparsity= 0) { + auto dmat = RandomDataGenerator(n_rows, n_cols, sparsity).Seed(3).GenerateDMatix(); + const SparsePage& batch = *dmat->GetBatches().begin(); + + HistogramCutsWrapper cmat; + cmat.SetPtrs({0, 3, 6, 9, 12, 15, 18, 21, 24}); + // 24 cut fields, 3 cut fields for each feature (column). + cmat.SetValues({0.30f, 0.67f, 1.64f, + 0.32f, 0.77f, 1.95f, + 0.29f, 0.70f, 1.80f, + 0.32f, 0.75f, 1.85f, + 0.18f, 0.59f, 1.69f, + 0.25f, 0.74f, 2.00f, + 0.26f, 0.74f, 1.98f, + 0.26f, 0.71f, 1.83f}); + cmat.SetMins({0.1f, 0.2f, 0.3f, 0.1f, 0.2f, 0.3f, 0.2f, 0.2f}); + + bst_row_t row_stride = 0; + const auto &offset_vec = batch.offset.ConstHostVector(); + for (size_t i = 1; i < offset_vec.size(); ++i) { + row_stride = std::max(row_stride, offset_vec[i] - offset_vec[i-1]); + } + + auto page = std::unique_ptr( + new EllpackPageImpl(0, cmat, batch, dmat->IsDense(), row_stride)); + + return page; +} +#endif +} // namespace xgboost diff --git a/tests/cpp/tree/test_gpu_hist.cu b/tests/cpp/tree/test_gpu_hist.cu index a8816dece9ca..9eca3e78d6fd 100644 --- a/tests/cpp/tree/test_gpu_hist.cu +++ b/tests/cpp/tree/test_gpu_hist.cu @@ -1,6 +1,7 @@ /*! * Copyright 2017-2020 XGBoost contributors */ +#include #include #include #include @@ -9,7 +10,7 @@ #include #include "../helpers.h" -#include "gtest/gtest.h" +#include "../histogram_helpers.h" #include "xgboost/json.h" #include "../../../src/data/sparse_page_source.h"