|
1 | 1 | #include <unittest/unittest.h> |
2 | 2 | #include <thrust/inner_product.h> |
3 | 3 | #include <thrust/iterator/retag.h> |
| 4 | +#include <thrust/device_malloc.h> |
| 5 | +#include <thrust/device_free.h> |
4 | 6 |
|
5 | 7 | template <class Vector> |
6 | 8 | void TestInnerProductSimple(void) |
@@ -100,4 +102,54 @@ struct TestInnerProduct |
100 | 102 | }; |
101 | 103 | VariableUnitTest<TestInnerProduct, IntegralTypes> TestInnerProductInstance; |
102 | 104 |
|
| 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); |
103 | 127 |
|
| 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