/*! * Copyright 2018-2022 by XGBoost Contributors */ #include #include #include #include #include #include "../../../src/common/transform.h" #include "../helpers.h" #if defined(__CUDACC__) #define TRANSFORM_GPU 0 #else #define TRANSFORM_GPU -1 #endif namespace xgboost { namespace common { template struct TestTransformRange { void XGBOOST_DEVICE operator()(size_t _idx, Span _out, Span _in) { _out[_idx] = _in[_idx]; } }; TEST(Transform, DeclareUnifiedTest(Basic)) { const size_t size {256}; std::vector h_in(size); std::vector h_out(size); std::iota(h_in.begin(), h_in.end(), 0); std::vector h_sol(size); std::iota(h_sol.begin(), h_sol.end(), 0); const HostDeviceVector in_vec{h_in, TRANSFORM_GPU}; HostDeviceVector out_vec{h_out, TRANSFORM_GPU}; out_vec.Fill(0); Transform<>::Init(TestTransformRange{}, Range{0, static_cast(size)}, common::OmpGetNumThreads(0), TRANSFORM_GPU) .Eval(&out_vec, &in_vec); std::vector res = out_vec.HostVector(); ASSERT_TRUE(std::equal(h_sol.begin(), h_sol.end(), res.begin())); } #if !defined(__CUDACC__) TEST(TransformDeathTest, Exception) { size_t const kSize {16}; std::vector h_in(kSize); const HostDeviceVector in_vec{h_in, -1}; EXPECT_DEATH( { Transform<>::Init([](size_t idx, common::Span _in) { _in[idx + 1]; }, Range(0, static_cast(kSize)), common::OmpGetNumThreads(0), -1) .Eval(&in_vec); }, ""); } #endif } // namespace common } // namespace xgboost