Skip to content

Commit 0d17b82

Browse files
committed
The great Thrust index type fix, part 5: verify equal, find, IP.
1 parent 926ea8d commit 0d17b82

3 files changed

Lines changed: 133 additions & 0 deletions

File tree

testing/equal.cu

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <thrust/equal.h>
33
#include <thrust/functional.h>
44
#include <thrust/iterator/retag.h>
5+
#include <thrust/device_malloc.h>
6+
#include <thrust/device_free.h>
57

68
template <class Vector>
79
void TestEqualSimple(void)
@@ -102,3 +104,48 @@ void TestEqualDispatchImplicit()
102104
}
103105
DECLARE_UNITTEST(TestEqualDispatchImplicit);
104106

107+
struct only_set_when_both_expected
108+
{
109+
long long expected;
110+
bool * flag;
111+
112+
__device__
113+
bool operator()(long long x, long long y)
114+
{
115+
if (x == expected && y == expected)
116+
{
117+
*flag = true;
118+
}
119+
120+
return x == y;
121+
}
122+
};
123+
124+
void TestEqualWithBigIndexesHelper(int magnitude)
125+
{
126+
thrust::counting_iterator<long long> begin(1);
127+
thrust::counting_iterator<long long> end = begin + (1ll << magnitude);
128+
ASSERT_EQUAL(thrust::distance(begin, end), 1ll << magnitude);
129+
130+
thrust::device_ptr<bool> has_executed = thrust::device_malloc<bool>(1);
131+
*has_executed = false;
132+
133+
only_set_when_both_expected fn = { (1ll << magnitude) - 1,
134+
thrust::raw_pointer_cast(has_executed) };
135+
136+
ASSERT_EQUAL(thrust::equal(thrust::device, begin, end, begin, fn), true);
137+
138+
bool has_executed_h = *has_executed;
139+
thrust::device_free(has_executed);
140+
141+
ASSERT_EQUAL(has_executed_h, true);
142+
}
143+
144+
void TestEqualWithBigIndexes()
145+
{
146+
TestEqualWithBigIndexesHelper(30);
147+
TestEqualWithBigIndexesHelper(31);
148+
TestEqualWithBigIndexesHelper(32);
149+
TestEqualWithBigIndexesHelper(33);
150+
}
151+
DECLARE_UNITTEST(TestEqualWithBigIndexes);

testing/find.cu

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,37 @@ struct TestFindIfNot
304304
};
305305
VariableUnitTest<TestFindIfNot, SignedIntegralTypes> TestFindIfNotInstance;
306306

307+
void TestFindWithBigIndexesHelper(int magnitude)
308+
{
309+
thrust::counting_iterator<long long> begin(1);
310+
thrust::counting_iterator<long long> end = begin + (1ll << magnitude);
311+
ASSERT_EQUAL(thrust::distance(begin, end), 1ll << magnitude);
312+
313+
thrust::detail::intmax_t distance_low_value = thrust::distance(
314+
begin,
315+
thrust::find(
316+
thrust::device,
317+
begin,
318+
end,
319+
17));
320+
321+
thrust::detail::intmax_t distance_high_value = thrust::distance(
322+
begin,
323+
thrust::find(
324+
thrust::device,
325+
begin,
326+
end,
327+
(1ll << magnitude) - 17));
328+
329+
ASSERT_EQUAL(distance_low_value, 16);
330+
ASSERT_EQUAL(distance_high_value, (1ll << magnitude) - 18);
331+
}
332+
333+
void TestFindWithBigIndexes()
334+
{
335+
TestFindWithBigIndexesHelper(30);
336+
TestFindWithBigIndexesHelper(31);
337+
TestFindWithBigIndexesHelper(32);
338+
TestFindWithBigIndexesHelper(33);
339+
}
340+
DECLARE_UNITTEST(TestFindWithBigIndexes);

testing/inner_product.cu

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <unittest/unittest.h>
22
#include <thrust/inner_product.h>
33
#include <thrust/iterator/retag.h>
4+
#include <thrust/device_malloc.h>
5+
#include <thrust/device_free.h>
46

57
template <class Vector>
68
void TestInnerProductSimple(void)
@@ -100,4 +102,54 @@ struct TestInnerProduct
100102
};
101103
VariableUnitTest<TestInnerProduct, IntegralTypes> TestInnerProductInstance;
102104

105+
struct only_set_when_both_expected
106+
{
107+
long long expected;
108+
bool * flag;
109+
110+
__device__
111+
long long operator()(long long x, long long y)
112+
{
113+
if (x == expected && y == expected)
114+
{
115+
*flag = true;
116+
}
117+
118+
return x == y;
119+
}
120+
};
121+
122+
void TestInnerProductWithBigIndexesHelper(int magnitude)
123+
{
124+
thrust::counting_iterator<long long> begin(1);
125+
thrust::counting_iterator<long long> end = begin + (1ll << magnitude);
126+
ASSERT_EQUAL(thrust::distance(begin, end), 1ll << magnitude);
103127

128+
thrust::device_ptr<bool> has_executed = thrust::device_malloc<bool>(1);
129+
*has_executed = false;
130+
131+
only_set_when_both_expected fn = { (1ll << magnitude) - 1,
132+
thrust::raw_pointer_cast(has_executed) };
133+
134+
ASSERT_EQUAL(thrust::inner_product(
135+
thrust::device,
136+
begin, end,
137+
begin,
138+
0ll,
139+
thrust::plus<long long>(),
140+
fn), (1ll << magnitude));
141+
142+
bool has_executed_h = *has_executed;
143+
thrust::device_free(has_executed);
144+
145+
ASSERT_EQUAL(has_executed_h, true);
146+
}
147+
148+
void TestInnerProductWithBigIndexes()
149+
{
150+
TestInnerProductWithBigIndexesHelper(30);
151+
TestInnerProductWithBigIndexesHelper(31);
152+
TestInnerProductWithBigIndexesHelper(32);
153+
TestInnerProductWithBigIndexesHelper(33);
154+
}
155+
DECLARE_UNITTEST(TestInnerProductWithBigIndexes);

0 commit comments

Comments
 (0)