forked from dmlc/xgboost
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_transform_range.cc
More file actions
70 lines (61 loc) · 1.9 KB
/
test_transform_range.cc
File metadata and controls
70 lines (61 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Copyright 2018-2024, XGBoost Contributors
*/
#include <gtest/gtest.h>
#include <xgboost/base.h>
#include <xgboost/host_device_vector.h>
#include <xgboost/span.h>
#include <numeric> // for iota
#include <vector>
#include "../../../src/common/transform.h"
#include "../helpers.h"
namespace xgboost::common {
namespace {
constexpr DeviceOrd TransformDevice() {
#if defined(__CUDACC__)
return DeviceOrd::CUDA(0);
#else
return DeviceOrd::CPU();
#endif
}
} // namespace
template <typename T>
struct TestTransformRange {
void XGBOOST_DEVICE operator()(std::size_t _idx, Span<float> _out, Span<const float> _in) {
_out[_idx] = _in[_idx];
}
};
TEST(Transform, DeclareUnifiedTest(Basic)) {
const size_t size{256};
std::vector<float> h_in(size);
std::vector<float> h_out(size);
std::iota(h_in.begin(), h_in.end(), 0);
std::vector<float> h_sol(size);
std::iota(h_sol.begin(), h_sol.end(), 0);
auto device = TransformDevice();
HostDeviceVector<float> const in_vec{h_in, device};
HostDeviceVector<float> out_vec{h_out, device};
out_vec.Fill(0);
Transform<>::Init(TestTransformRange<float>{},
Range{0, static_cast<Range::DifferenceType>(size)}, AllThreadsForTest(),
TransformDevice())
.Eval(&out_vec, &in_vec);
std::vector<float> 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<float> h_in(kSize);
const HostDeviceVector<float> in_vec{h_in, DeviceOrd::CPU()};
EXPECT_DEATH(
{
Transform<>::Init([](size_t idx, common::Span<float const> _in) { _in[idx + 1]; },
Range(0, static_cast<Range::DifferenceType>(kSize)), AllThreadsForTest(),
DeviceOrd::CPU())
.Eval(&in_vec);
},
"");
}
#endif
} // namespace xgboost::common