forked from NVIDIA/thrust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduce.cu
232 lines (182 loc) · 5.85 KB
/
reduce.cu
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <unittest/unittest.h>
#include <thrust/reduce.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/retag.h>
#include <limits>
template<typename T>
struct plus_mod_10
{
__host__ __device__
T operator()(T lhs, T rhs) const
{
return ((lhs % 10) + (rhs % 10)) % 10;
}
};
template<typename T>
struct is_equal_div_10_reduce
{
__host__ __device__
bool operator()(const T x, const T& y) const { return ((int) x / 10) == ((int) y / 10); }
};
template <class Vector>
void TestReduceSimple(void)
{
typedef typename Vector::value_type T;
Vector v(3);
v[0] = 1; v[1] = -2; v[2] = 3;
// no initializer
ASSERT_EQUAL(thrust::reduce(v.begin(), v.end()), 2);
// with initializer
ASSERT_EQUAL(thrust::reduce(v.begin(), v.end(), (T) 10), 12);
}
DECLARE_VECTOR_UNITTEST(TestReduceSimple);
template<typename InputIterator>
int reduce(my_system &system, InputIterator, InputIterator)
{
system.validate_dispatch();
return 13;
}
void TestReduceDispatchExplicit()
{
thrust::device_vector<int> vec;
my_system sys(0);
thrust::reduce(sys, vec.begin(), vec.end());
ASSERT_EQUAL(true, sys.is_valid());
}
DECLARE_UNITTEST(TestReduceDispatchExplicit);
template<typename InputIterator>
int reduce(my_tag, InputIterator, InputIterator)
{
return 13;
}
void TestReduceDispatchImplicit()
{
thrust::device_vector<int> vec;
int result = thrust::reduce(thrust::retag<my_tag>(vec.begin()),
thrust::retag<my_tag>(vec.end()));
ASSERT_EQUAL(13, result);
}
DECLARE_UNITTEST(TestReduceDispatchImplicit);
template <typename T>
struct TestReduce
{
void operator()(const size_t n)
{
thrust::host_vector<T> h_data = unittest::random_integers<T>(n);
thrust::device_vector<T> d_data = h_data;
T init = 13;
T h_result = thrust::reduce(h_data.begin(), h_data.end(), init);
T d_result = thrust::reduce(d_data.begin(), d_data.end(), init);
ASSERT_EQUAL(h_result, d_result);
}
};
VariableUnitTest<TestReduce, IntegralTypes> TestReduceInstance;
template <class IntVector, class FloatVector>
void TestReduceMixedTypes(void)
{
// make sure we get types for default args and operators correct
IntVector int_input(4);
int_input[0] = 1;
int_input[1] = 2;
int_input[2] = 3;
int_input[3] = 4;
FloatVector float_input(4);
float_input[0] = 1.5;
float_input[1] = 2.5;
float_input[2] = 3.5;
float_input[3] = 4.5;
// float -> int should use using plus<int> operator by default
ASSERT_EQUAL(thrust::reduce(float_input.begin(), float_input.end(), (int) 0), 10);
// int -> float should use using plus<float> operator by default
ASSERT_EQUAL(thrust::reduce(int_input.begin(), int_input.end(), (float) 0.5), 10.5);
}
void TestReduceMixedTypesHost(void)
{
TestReduceMixedTypes< thrust::host_vector<int>, thrust::host_vector<float> >();
}
DECLARE_UNITTEST(TestReduceMixedTypesHost);
void TestReduceMixedTypesDevice(void)
{
TestReduceMixedTypes< thrust::device_vector<int>, thrust::device_vector<float> >();
}
DECLARE_UNITTEST(TestReduceMixedTypesDevice);
template <typename T>
struct TestReduceWithOperator
{
void operator()(const size_t n)
{
thrust::host_vector<T> h_data = unittest::random_integers<T>(n);
thrust::device_vector<T> d_data = h_data;
T init = 3;
T cpu_result = thrust::reduce(h_data.begin(), h_data.end(), init, plus_mod_10<T>());
T gpu_result = thrust::reduce(d_data.begin(), d_data.end(), init, plus_mod_10<T>());
ASSERT_EQUAL(cpu_result, gpu_result);
}
};
VariableUnitTest<TestReduceWithOperator, UnsignedIntegralTypes> TestReduceWithOperatorInstance;
template <typename T>
struct plus_mod3
{
T * table;
plus_mod3(T * table) : table(table) {}
__host__ __device__
T operator()(T a, T b)
{
return table[(int) (a + b)];
}
};
template <typename Vector>
void TestReduceWithIndirection(void)
{
// add numbers modulo 3 with external lookup table
typedef typename Vector::value_type T;
Vector data(7);
data[0] = 0;
data[1] = 1;
data[2] = 2;
data[3] = 1;
data[4] = 2;
data[5] = 0;
data[6] = 1;
Vector table(6);
table[0] = 0;
table[1] = 1;
table[2] = 2;
table[3] = 0;
table[4] = 1;
table[5] = 2;
T result = thrust::reduce(data.begin(), data.end(), T(0), plus_mod3<T>(thrust::raw_pointer_cast(&table[0])));
ASSERT_EQUAL(result, T(1));
}
DECLARE_INTEGRAL_VECTOR_UNITTEST(TestReduceWithIndirection);
template<typename T>
void TestReduceCountingIterator()
{
size_t const n = 15 * sizeof(T);
ASSERT_LEQUAL(T(n), unittest::truncate_to_max_representable<T>(n));
thrust::counting_iterator<T, thrust::host_system_tag> h_first = thrust::make_counting_iterator<T>(0);
thrust::counting_iterator<T, thrust::device_system_tag> d_first = thrust::make_counting_iterator<T>(0);
T init = unittest::random_integer<T>();
T h_result = thrust::reduce(h_first, h_first + n, init);
T d_result = thrust::reduce(d_first, d_first + n, init);
// we use ASSERT_ALMOST_EQUAL because we're testing floating point types
ASSERT_ALMOST_EQUAL(h_result, d_result);
}
DECLARE_GENERIC_UNITTEST(TestReduceCountingIterator);
void TestReduceWithBigIndexesHelper(int magnitude)
{
thrust::constant_iterator<long long> begin(1);
thrust::constant_iterator<long long> end = begin + (1ll << magnitude);
ASSERT_EQUAL(thrust::distance(begin, end), 1ll << magnitude);
long long result = thrust::reduce(thrust::device, begin, end);
ASSERT_EQUAL(result, 1ll << magnitude);
}
void TestReduceWithBigIndexes()
{
TestReduceWithBigIndexesHelper(30);
TestReduceWithBigIndexesHelper(31);
TestReduceWithBigIndexesHelper(32);
TestReduceWithBigIndexesHelper(33);
}
DECLARE_UNITTEST(TestReduceWithBigIndexes);