Spaces:
Runtime error
Runtime error
File size: 10,762 Bytes
be11144 |
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
#include <unittest/unittest.h>
#include <thrust/sequence.h>
#include <thrust/find.h>
#include <thrust/iterator/retag.h>
template <typename T>
struct equal_to_value_pred
{
T value;
equal_to_value_pred(T value) : value(value) {}
__host__ __device__
bool operator()(T v) const { return v == value; }
};
template <typename T>
struct not_equal_to_value_pred
{
T value;
not_equal_to_value_pred(T value) : value(value) {}
__host__ __device__
bool operator()(T v) const { return v != value; }
};
template<typename T>
struct less_than_value_pred
{
T value;
less_than_value_pred(T value) : value(value) {}
__host__ __device__
bool operator()(T v) const { return v < value; }
};
template <class Vector>
void TestFindSimple(void)
{
Vector vec(5);
vec[0] = 1;
vec[1] = 2;
vec[2] = 3;
vec[3] = 3;
vec[4] = 5;
ASSERT_EQUAL(thrust::find(vec.begin(), vec.end(), 0) - vec.begin(), 5);
ASSERT_EQUAL(thrust::find(vec.begin(), vec.end(), 1) - vec.begin(), 0);
ASSERT_EQUAL(thrust::find(vec.begin(), vec.end(), 2) - vec.begin(), 1);
ASSERT_EQUAL(thrust::find(vec.begin(), vec.end(), 3) - vec.begin(), 2);
ASSERT_EQUAL(thrust::find(vec.begin(), vec.end(), 4) - vec.begin(), 5);
ASSERT_EQUAL(thrust::find(vec.begin(), vec.end(), 5) - vec.begin(), 4);
}
DECLARE_VECTOR_UNITTEST(TestFindSimple);
template<typename InputIterator, typename T>
InputIterator find(my_system &system, InputIterator first, InputIterator, const T&)
{
system.validate_dispatch();
return first;
}
void TestFindDispatchExplicit()
{
thrust::device_vector<int> vec(1);
my_system sys(0);
thrust::find(sys,
vec.begin(),
vec.end(),
0);
ASSERT_EQUAL(true, sys.is_valid());
}
DECLARE_UNITTEST(TestFindDispatchExplicit);
template<typename InputIterator, typename T>
InputIterator find(my_tag, InputIterator first, InputIterator, const T&)
{
*first = 13;
return first;
}
void TestFindDispatchImplicit()
{
thrust::device_vector<int> vec(1);
thrust::find(thrust::retag<my_tag>(vec.begin()),
thrust::retag<my_tag>(vec.end()),
0);
ASSERT_EQUAL(13, vec.front());
}
DECLARE_UNITTEST(TestFindDispatchImplicit);
template <class Vector>
void TestFindIfSimple(void)
{
typedef typename Vector::value_type T;
Vector vec(5);
vec[0] = 1;
vec[1] = 2;
vec[2] = 3;
vec[3] = 3;
vec[4] = 5;
ASSERT_EQUAL(thrust::find_if(vec.begin(), vec.end(), equal_to_value_pred<T>(0)) - vec.begin(), 5);
ASSERT_EQUAL(thrust::find_if(vec.begin(), vec.end(), equal_to_value_pred<T>(1)) - vec.begin(), 0);
ASSERT_EQUAL(thrust::find_if(vec.begin(), vec.end(), equal_to_value_pred<T>(2)) - vec.begin(), 1);
ASSERT_EQUAL(thrust::find_if(vec.begin(), vec.end(), equal_to_value_pred<T>(3)) - vec.begin(), 2);
ASSERT_EQUAL(thrust::find_if(vec.begin(), vec.end(), equal_to_value_pred<T>(4)) - vec.begin(), 5);
ASSERT_EQUAL(thrust::find_if(vec.begin(), vec.end(), equal_to_value_pred<T>(5)) - vec.begin(), 4);
}
DECLARE_VECTOR_UNITTEST(TestFindIfSimple);
template<typename InputIterator, typename Predicate>
InputIterator find_if(my_system &system, InputIterator first, InputIterator, Predicate)
{
system.validate_dispatch();
return first;
}
void TestFindIfDispatchExplicit()
{
thrust::device_vector<int> vec(1);
my_system sys(0);
thrust::find_if(sys,
vec.begin(),
vec.end(),
thrust::identity<int>());
ASSERT_EQUAL(true, sys.is_valid());
}
DECLARE_UNITTEST(TestFindIfDispatchExplicit);
template<typename InputIterator, typename Predicate>
InputIterator find_if(my_tag, InputIterator first, InputIterator, Predicate)
{
*first = 13;
return first;
}
void TestFindIfDispatchImplicit()
{
thrust::device_vector<int> vec(1);
thrust::find_if(thrust::retag<my_tag>(vec.begin()),
thrust::retag<my_tag>(vec.end()),
thrust::identity<int>());
ASSERT_EQUAL(13, vec.front());
}
DECLARE_UNITTEST(TestFindIfDispatchImplicit);
template <class Vector>
void TestFindIfNotSimple(void)
{
typedef typename Vector::value_type T;
Vector vec(5);
vec[0] = 0;
vec[1] = 1;
vec[2] = 2;
vec[3] = 3;
vec[4] = 4;
ASSERT_EQUAL(0, thrust::find_if_not(vec.begin(), vec.end(), less_than_value_pred<T>(0)) - vec.begin());
ASSERT_EQUAL(1, thrust::find_if_not(vec.begin(), vec.end(), less_than_value_pred<T>(1)) - vec.begin());
ASSERT_EQUAL(2, thrust::find_if_not(vec.begin(), vec.end(), less_than_value_pred<T>(2)) - vec.begin());
ASSERT_EQUAL(3, thrust::find_if_not(vec.begin(), vec.end(), less_than_value_pred<T>(3)) - vec.begin());
ASSERT_EQUAL(4, thrust::find_if_not(vec.begin(), vec.end(), less_than_value_pred<T>(4)) - vec.begin());
ASSERT_EQUAL(5, thrust::find_if_not(vec.begin(), vec.end(), less_than_value_pred<T>(5)) - vec.begin());
}
DECLARE_VECTOR_UNITTEST(TestFindIfNotSimple);
template<typename InputIterator, typename Predicate>
InputIterator find_if_not(my_system &system, InputIterator first, InputIterator, Predicate)
{
system.validate_dispatch();
return first;
}
void TestFindIfNotDispatchExplicit()
{
thrust::device_vector<int> vec(1);
my_system sys(0);
thrust::find_if_not(sys,
vec.begin(),
vec.end(),
thrust::identity<int>());
ASSERT_EQUAL(true, sys.is_valid());
}
DECLARE_UNITTEST(TestFindIfNotDispatchExplicit);
template<typename InputIterator, typename Predicate>
InputIterator find_if_not(my_tag, InputIterator first, InputIterator, Predicate)
{
*first = 13;
return first;
}
void TestFindIfNotDispatchImplicit()
{
thrust::device_vector<int> vec(1);
thrust::find_if_not(thrust::retag<my_tag>(vec.begin()),
thrust::retag<my_tag>(vec.end()),
thrust::identity<int>());
ASSERT_EQUAL(13, vec.front());
}
DECLARE_UNITTEST(TestFindIfNotDispatchImplicit);
template <typename T>
struct TestFind
{
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;
typename thrust::host_vector<T>::iterator h_iter;
typename thrust::device_vector<T>::iterator d_iter;
h_iter = thrust::find(h_data.begin(), h_data.end(), T(0));
d_iter = thrust::find(d_data.begin(), d_data.end(), T(0));
ASSERT_EQUAL(h_iter - h_data.begin(), d_iter - d_data.begin());
for (size_t i = 1; i < n; i *= 2)
{
T sample = h_data[i];
h_iter = thrust::find(h_data.begin(), h_data.end(), sample);
d_iter = thrust::find(d_data.begin(), d_data.end(), sample);
ASSERT_EQUAL(h_iter - h_data.begin(), d_iter - d_data.begin());
}
}
};
VariableUnitTest<TestFind, SignedIntegralTypes> TestFindInstance;
template <typename T>
struct TestFindIf
{
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;
typename thrust::host_vector<T>::iterator h_iter;
typename thrust::device_vector<T>::iterator d_iter;
h_iter = thrust::find_if(h_data.begin(), h_data.end(), equal_to_value_pred<T>(0));
d_iter = thrust::find_if(d_data.begin(), d_data.end(), equal_to_value_pred<T>(0));
ASSERT_EQUAL(h_iter - h_data.begin(), d_iter - d_data.begin());
for (size_t i = 1; i < n; i *= 2)
{
T sample = h_data[i];
h_iter = thrust::find_if(h_data.begin(), h_data.end(), equal_to_value_pred<T>(sample));
d_iter = thrust::find_if(d_data.begin(), d_data.end(), equal_to_value_pred<T>(sample));
ASSERT_EQUAL(h_iter - h_data.begin(), d_iter - d_data.begin());
}
}
};
VariableUnitTest<TestFindIf, SignedIntegralTypes> TestFindIfInstance;
template <typename T>
struct TestFindIfNot
{
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;
typename thrust::host_vector<T>::iterator h_iter;
typename thrust::device_vector<T>::iterator d_iter;
h_iter = thrust::find_if_not(h_data.begin(), h_data.end(), not_equal_to_value_pred<T>(0));
d_iter = thrust::find_if_not(d_data.begin(), d_data.end(), not_equal_to_value_pred<T>(0));
ASSERT_EQUAL(h_iter - h_data.begin(), d_iter - d_data.begin());
for (size_t i = 1; i < n; i *= 2)
{
T sample = h_data[i];
h_iter = thrust::find_if_not(h_data.begin(), h_data.end(), not_equal_to_value_pred<T>(sample));
d_iter = thrust::find_if_not(d_data.begin(), d_data.end(), not_equal_to_value_pred<T>(sample));
ASSERT_EQUAL(h_iter - h_data.begin(), d_iter - d_data.begin());
}
}
};
VariableUnitTest<TestFindIfNot, SignedIntegralTypes> TestFindIfNotInstance;
void TestFindWithBigIndexesHelper(int magnitude)
{
thrust::counting_iterator<long long> begin(1);
thrust::counting_iterator<long long> end = begin + (1ll << magnitude);
ASSERT_EQUAL(thrust::distance(begin, end), 1ll << magnitude);
thrust::detail::intmax_t distance_low_value = thrust::distance(
begin,
thrust::find(
thrust::device,
begin,
end,
17));
thrust::detail::intmax_t distance_high_value = thrust::distance(
begin,
thrust::find(
thrust::device,
begin,
end,
(1ll << magnitude) - 17));
ASSERT_EQUAL(distance_low_value, 16);
ASSERT_EQUAL(distance_high_value, (1ll << magnitude) - 18);
}
void TestFindWithBigIndexes()
{
TestFindWithBigIndexesHelper(30);
TestFindWithBigIndexesHelper(31);
TestFindWithBigIndexesHelper(32);
TestFindWithBigIndexesHelper(33);
}
DECLARE_UNITTEST(TestFindWithBigIndexes);
namespace
{
class Weird
{
int value;
public:
__host__ __device__ Weird(int val, int)
: value(val)
{}
friend __host__ __device__
bool operator==(int x, Weird y)
{
return x == y.value;
}
};
} // end anon namespace
void TestFindAsymmetricEquality()
{ // Regression test for thrust/thrust#1229
thrust::host_vector<int> v(1000);
thrust::sequence(v.begin(), v.end());
thrust::device_vector<int> dv(v);
auto result = thrust::find(dv.begin(), dv.end(), Weird(333, 0));
ASSERT_EQUAL(*result, 333);
ASSERT_EQUAL(result - dv.begin(), 333);
}
DECLARE_UNITTEST(TestFindAsymmetricEquality);
|