Skip to content

Commit

Permalink
Merge to mainstream Thrust TOT
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhoberock committed Dec 2, 2010
2 parents c76efa1 + 8c818db commit 5788a4b
Show file tree
Hide file tree
Showing 3 changed files with 376 additions and 4 deletions.
129 changes: 129 additions & 0 deletions testing/binary_search_descending.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#include <unittest/unittest.h>
#include <thrust/binary_search.h>
#include <thrust/functional.h>

#include <thrust/sequence.h>
#include <thrust/sort.h>

//////////////////////
// Scalar Functions //
//////////////////////

template <class Vector>
void TestScalarLowerBoundDescendingSimple(void)
{
typedef typename Vector::value_type T;

Vector vec(5);

vec[0] = 8;
vec[1] = 7;
vec[2] = 5;
vec[3] = 2;
vec[4] = 0;

ASSERT_EQUAL_QUIET(vec.begin() + 4, thrust::lower_bound(vec.begin(), vec.end(), 0, thrust::greater<T>()));
ASSERT_EQUAL_QUIET(vec.begin() + 4, thrust::lower_bound(vec.begin(), vec.end(), 1, thrust::greater<T>()));
ASSERT_EQUAL_QUIET(vec.begin() + 3, thrust::lower_bound(vec.begin(), vec.end(), 2, thrust::greater<T>()));
ASSERT_EQUAL_QUIET(vec.begin() + 3, thrust::lower_bound(vec.begin(), vec.end(), 3, thrust::greater<T>()));
ASSERT_EQUAL_QUIET(vec.begin() + 3, thrust::lower_bound(vec.begin(), vec.end(), 4, thrust::greater<T>()));
ASSERT_EQUAL_QUIET(vec.begin() + 2, thrust::lower_bound(vec.begin(), vec.end(), 5, thrust::greater<T>()));
ASSERT_EQUAL_QUIET(vec.begin() + 2, thrust::lower_bound(vec.begin(), vec.end(), 6, thrust::greater<T>()));
ASSERT_EQUAL_QUIET(vec.begin() + 1, thrust::lower_bound(vec.begin(), vec.end(), 7, thrust::greater<T>()));
ASSERT_EQUAL_QUIET(vec.begin() + 0, thrust::lower_bound(vec.begin(), vec.end(), 8, thrust::greater<T>()));
ASSERT_EQUAL_QUIET(vec.begin() + 0, thrust::lower_bound(vec.begin(), vec.end(), 9, thrust::greater<T>()));
}
DECLARE_VECTOR_UNITTEST(TestScalarLowerBoundDescendingSimple);


template <class Vector>
void TestScalarUpperBoundDescendingSimple(void)
{
typedef typename Vector::value_type T;

Vector vec(5);

vec[0] = 8;
vec[1] = 7;
vec[2] = 5;
vec[3] = 2;
vec[4] = 0;

ASSERT_EQUAL_QUIET(vec.begin() + 5, thrust::upper_bound(vec.begin(), vec.end(), 0, thrust::greater<int>()));
ASSERT_EQUAL_QUIET(vec.begin() + 4, thrust::upper_bound(vec.begin(), vec.end(), 1, thrust::greater<int>()));
ASSERT_EQUAL_QUIET(vec.begin() + 4, thrust::upper_bound(vec.begin(), vec.end(), 2, thrust::greater<int>()));
ASSERT_EQUAL_QUIET(vec.begin() + 3, thrust::upper_bound(vec.begin(), vec.end(), 3, thrust::greater<int>()));
ASSERT_EQUAL_QUIET(vec.begin() + 3, thrust::upper_bound(vec.begin(), vec.end(), 4, thrust::greater<int>()));
ASSERT_EQUAL_QUIET(vec.begin() + 3, thrust::upper_bound(vec.begin(), vec.end(), 5, thrust::greater<int>()));
ASSERT_EQUAL_QUIET(vec.begin() + 2, thrust::upper_bound(vec.begin(), vec.end(), 6, thrust::greater<int>()));
ASSERT_EQUAL_QUIET(vec.begin() + 2, thrust::upper_bound(vec.begin(), vec.end(), 7, thrust::greater<int>()));
ASSERT_EQUAL_QUIET(vec.begin() + 1, thrust::upper_bound(vec.begin(), vec.end(), 8, thrust::greater<int>()));
ASSERT_EQUAL_QUIET(vec.begin() + 0, thrust::upper_bound(vec.begin(), vec.end(), 9, thrust::greater<int>()));
}
DECLARE_VECTOR_UNITTEST(TestScalarUpperBoundDescendingSimple);


template <class Vector>
void TestScalarBinarySearchDescendingSimple(void)
{
typedef typename Vector::value_type T;

Vector vec(5);

vec[0] = 8;
vec[1] = 7;
vec[2] = 5;
vec[3] = 2;
vec[4] = 0;

ASSERT_EQUAL(true, thrust::binary_search(vec.begin(), vec.end(), 0, thrust::greater<int>()));
ASSERT_EQUAL(false, thrust::binary_search(vec.begin(), vec.end(), 1, thrust::greater<int>()));
ASSERT_EQUAL(true, thrust::binary_search(vec.begin(), vec.end(), 2, thrust::greater<int>()));
ASSERT_EQUAL(false, thrust::binary_search(vec.begin(), vec.end(), 3, thrust::greater<int>()));
ASSERT_EQUAL(false, thrust::binary_search(vec.begin(), vec.end(), 4, thrust::greater<int>()));
ASSERT_EQUAL(true, thrust::binary_search(vec.begin(), vec.end(), 5, thrust::greater<int>()));
ASSERT_EQUAL(false, thrust::binary_search(vec.begin(), vec.end(), 6, thrust::greater<int>()));
ASSERT_EQUAL(true, thrust::binary_search(vec.begin(), vec.end(), 7, thrust::greater<int>()));
ASSERT_EQUAL(true, thrust::binary_search(vec.begin(), vec.end(), 8, thrust::greater<int>()));
ASSERT_EQUAL(false, thrust::binary_search(vec.begin(), vec.end(), 9, thrust::greater<int>()));
}
DECLARE_VECTOR_UNITTEST(TestScalarBinarySearchDescendingSimple);


template <class Vector>
void TestScalarEqualRangeDescendingSimple(void)
{
typedef typename Vector::value_type T;

Vector vec(5);

vec[0] = 8;
vec[1] = 7;
vec[2] = 5;
vec[3] = 2;
vec[4] = 0;

ASSERT_EQUAL_QUIET(vec.begin() + 4, thrust::equal_range(vec.begin(), vec.end(), 0, thrust::greater<int>()).first);
ASSERT_EQUAL_QUIET(vec.begin() + 4, thrust::equal_range(vec.begin(), vec.end(), 1, thrust::greater<int>()).first);
ASSERT_EQUAL_QUIET(vec.begin() + 3, thrust::equal_range(vec.begin(), vec.end(), 2, thrust::greater<int>()).first);
ASSERT_EQUAL_QUIET(vec.begin() + 3, thrust::equal_range(vec.begin(), vec.end(), 3, thrust::greater<int>()).first);
ASSERT_EQUAL_QUIET(vec.begin() + 3, thrust::equal_range(vec.begin(), vec.end(), 4, thrust::greater<int>()).first);
ASSERT_EQUAL_QUIET(vec.begin() + 2, thrust::equal_range(vec.begin(), vec.end(), 5, thrust::greater<int>()).first);
ASSERT_EQUAL_QUIET(vec.begin() + 2, thrust::equal_range(vec.begin(), vec.end(), 6, thrust::greater<int>()).first);
ASSERT_EQUAL_QUIET(vec.begin() + 1, thrust::equal_range(vec.begin(), vec.end(), 7, thrust::greater<int>()).first);
ASSERT_EQUAL_QUIET(vec.begin() + 0, thrust::equal_range(vec.begin(), vec.end(), 8, thrust::greater<int>()).first);
ASSERT_EQUAL_QUIET(vec.begin() + 0, thrust::equal_range(vec.begin(), vec.end(), 9, thrust::greater<int>()).first);

ASSERT_EQUAL_QUIET(vec.begin() + 5, thrust::equal_range(vec.begin(), vec.end(), 0, thrust::greater<int>()).second);
ASSERT_EQUAL_QUIET(vec.begin() + 4, thrust::equal_range(vec.begin(), vec.end(), 1, thrust::greater<int>()).second);
ASSERT_EQUAL_QUIET(vec.begin() + 4, thrust::equal_range(vec.begin(), vec.end(), 2, thrust::greater<int>()).second);
ASSERT_EQUAL_QUIET(vec.begin() + 3, thrust::equal_range(vec.begin(), vec.end(), 3, thrust::greater<int>()).second);
ASSERT_EQUAL_QUIET(vec.begin() + 3, thrust::equal_range(vec.begin(), vec.end(), 4, thrust::greater<int>()).second);
ASSERT_EQUAL_QUIET(vec.begin() + 3, thrust::equal_range(vec.begin(), vec.end(), 5, thrust::greater<int>()).second);
ASSERT_EQUAL_QUIET(vec.begin() + 2, thrust::equal_range(vec.begin(), vec.end(), 6, thrust::greater<int>()).second);
ASSERT_EQUAL_QUIET(vec.begin() + 2, thrust::equal_range(vec.begin(), vec.end(), 7, thrust::greater<int>()).second);
ASSERT_EQUAL_QUIET(vec.begin() + 1, thrust::equal_range(vec.begin(), vec.end(), 8, thrust::greater<int>()).second);
ASSERT_EQUAL_QUIET(vec.begin() + 0, thrust::equal_range(vec.begin(), vec.end(), 9, thrust::greater<int>()).second);
}
DECLARE_VECTOR_UNITTEST(TestScalarEqualRangeDescendingSimple);

237 changes: 237 additions & 0 deletions testing/binary_search_vector_descending.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
#include <unittest/unittest.h>
#include <thrust/binary_search.h>
#include <thrust/functional.h>

#include <thrust/sequence.h>
#include <thrust/sort.h>

//////////////////////
// Vector Functions //
//////////////////////

// convert xxx_vector<T1> to xxx_vector<T2>
template <class ExampleVector, typename NewType>
struct vector_like
{
typedef typename ExampleVector::allocator_type alloc;
typedef typename alloc::template rebind<NewType>::other new_alloc;
typedef thrust::detail::vector_base<NewType, new_alloc> type;
};

template <class Vector>
void TestVectorLowerBoundDescendingSimple(void)
{
typedef typename Vector::value_type T;

Vector vec(5);

vec[0] = 8;
vec[1] = 7;
vec[2] = 5;
vec[3] = 2;
vec[4] = 0;

Vector input(10);
thrust::sequence(input.begin(), input.end());

typedef typename vector_like<Vector, int>::type IntVector;

// test with integral output type
IntVector integral_output(10);
typename IntVector::iterator output_end = thrust::lower_bound(vec.begin(), vec.end(), input.begin(), input.end(), integral_output.begin(), thrust::greater<T>());

ASSERT_EQUAL_QUIET(integral_output.end(), output_end);

ASSERT_EQUAL(4, integral_output[0]);
ASSERT_EQUAL(4, integral_output[1]);
ASSERT_EQUAL(3, integral_output[2]);
ASSERT_EQUAL(3, integral_output[3]);
ASSERT_EQUAL(3, integral_output[4]);
ASSERT_EQUAL(2, integral_output[5]);
ASSERT_EQUAL(2, integral_output[6]);
ASSERT_EQUAL(1, integral_output[7]);
ASSERT_EQUAL(0, integral_output[8]);
ASSERT_EQUAL(0, integral_output[9]);
}
DECLARE_VECTOR_UNITTEST(TestVectorLowerBoundDescendingSimple);


template <class Vector>
void TestVectorUpperBoundDescendingSimple(void)
{
typedef typename Vector::value_type T;

Vector vec(5);

vec[0] = 8;
vec[1] = 7;
vec[2] = 5;
vec[3] = 2;
vec[4] = 0;

Vector input(10);
thrust::sequence(input.begin(), input.end());

typedef typename vector_like<Vector, int>::type IntVector;

// test with integral output type
IntVector integral_output(10);
typename IntVector::iterator output_end = thrust::upper_bound(vec.begin(), vec.end(), input.begin(), input.end(), integral_output.begin(), thrust::greater<int>());

ASSERT_EQUAL_QUIET(output_end, integral_output.end());

ASSERT_EQUAL(5, integral_output[0]);
ASSERT_EQUAL(4, integral_output[1]);
ASSERT_EQUAL(4, integral_output[2]);
ASSERT_EQUAL(3, integral_output[3]);
ASSERT_EQUAL(3, integral_output[4]);
ASSERT_EQUAL(3, integral_output[5]);
ASSERT_EQUAL(2, integral_output[6]);
ASSERT_EQUAL(2, integral_output[7]);
ASSERT_EQUAL(1, integral_output[8]);
ASSERT_EQUAL(0, integral_output[9]);
}
DECLARE_VECTOR_UNITTEST(TestVectorUpperBoundDescendingSimple);


template <class Vector>
void TestVectorBinarySearchDescendingSimple(void)
{
typedef typename Vector::value_type T;

Vector vec(5);

vec[0] = 8;
vec[1] = 7;
vec[2] = 5;
vec[3] = 2;
vec[4] = 0;

Vector input(10);
thrust::sequence(input.begin(), input.end());

typedef typename vector_like<Vector, bool>::type BoolVector;
typedef typename vector_like<Vector, int>::type IntVector;

// test with boolean output type
BoolVector bool_output(10);
typename BoolVector::iterator bool_output_end = thrust::binary_search(vec.begin(), vec.end(), input.begin(), input.end(), bool_output.begin(), thrust::greater<int>());

ASSERT_EQUAL_QUIET(bool_output_end, bool_output.end());

ASSERT_EQUAL(true, bool_output[0]);
ASSERT_EQUAL(false, bool_output[1]);
ASSERT_EQUAL(true, bool_output[2]);
ASSERT_EQUAL(false, bool_output[3]);
ASSERT_EQUAL(false, bool_output[4]);
ASSERT_EQUAL(true, bool_output[5]);
ASSERT_EQUAL(false, bool_output[6]);
ASSERT_EQUAL(true, bool_output[7]);
ASSERT_EQUAL(true, bool_output[8]);
ASSERT_EQUAL(false, bool_output[9]);

// test with integral output type
IntVector integral_output(10, 2);
typename IntVector::iterator int_output_end = thrust::binary_search(vec.begin(), vec.end(), input.begin(), input.end(), integral_output.begin(), thrust::greater<int>());

ASSERT_EQUAL_QUIET(int_output_end, integral_output.end());

ASSERT_EQUAL(1, integral_output[0]);
ASSERT_EQUAL(0, integral_output[1]);
ASSERT_EQUAL(1, integral_output[2]);
ASSERT_EQUAL(0, integral_output[3]);
ASSERT_EQUAL(0, integral_output[4]);
ASSERT_EQUAL(1, integral_output[5]);
ASSERT_EQUAL(0, integral_output[6]);
ASSERT_EQUAL(1, integral_output[7]);
ASSERT_EQUAL(1, integral_output[8]);
ASSERT_EQUAL(0, integral_output[9]);
}
DECLARE_VECTOR_UNITTEST(TestVectorBinarySearchDescendingSimple);


template <typename T>
struct TestVectorLowerBoundDescending
{
void operator()(const size_t n)
{
// XXX an MSVC bug causes problems inside std::stable_sort's implementation:
// std::lower_bound/upper_bound is confused with thrust::lower_bound/upper_bound
#if (THRUST_HOST_COMPILER == THRUST_HOST_COMPILER_MSVC) && (THRUST_DEVICE_BACKEND == THRUST_DEVICE_BACKEND_OMP)
KNOWN_FAILURE;
#else
thrust::host_vector<T> h_vec = unittest::random_integers<T>(n); thrust::sort(h_vec.begin(), h_vec.end(), thrust::greater<T>());
thrust::device_vector<T> d_vec = h_vec;

thrust::host_vector<T> h_input = unittest::random_integers<T>(2*n);
thrust::device_vector<T> d_input = h_input;

thrust::host_vector<int> h_output(2*n);
thrust::device_vector<int> d_output(2*n);

thrust::lower_bound(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), h_output.begin(), thrust::greater<int>());
thrust::lower_bound(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), d_output.begin(), thrust::greater<int>());

ASSERT_EQUAL(h_output, d_output);
#endif
}
};
VariableUnitTest<TestVectorLowerBoundDescending, SignedIntegralTypes> TestVectorLowerBoundDescendingInstance;


template <typename T>
struct TestVectorUpperBoundDescending
{
void operator()(const size_t n)
{
// XXX an MSVC bug causes problems inside std::stable_sort's implementation:
// std::lower_bound/upper_bound is confused with thrust::lower_bound/upper_bound
#if (THRUST_HOST_COMPILER == THRUST_HOST_COMPILER_MSVC) && (THRUST_DEVICE_BACKEND == THRUST_DEVICE_BACKEND_OMP)
KNOWN_FAILURE;
#else
thrust::host_vector<T> h_vec = unittest::random_integers<T>(n); thrust::sort(h_vec.begin(), h_vec.end(), thrust::greater<int>());
thrust::device_vector<T> d_vec = h_vec;

thrust::host_vector<T> h_input = unittest::random_integers<T>(2*n);
thrust::device_vector<T> d_input = h_input;

thrust::host_vector<int> h_output(2*n);
thrust::device_vector<int> d_output(2*n);

thrust::upper_bound(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), h_output.begin(), thrust::greater<int>());
thrust::upper_bound(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), d_output.begin(), thrust::greater<int>());

ASSERT_EQUAL(h_output, d_output);
#endif
}
};
VariableUnitTest<TestVectorUpperBoundDescending, SignedIntegralTypes> TestVectorUpperBoundDescendingInstance;

template <typename T>
struct TestVectorBinarySearchDescending
{
void operator()(const size_t n)
{
// XXX an MSVC bug causes problems inside std::stable_sort's implementation:
// std::lower_bound/upper_bound is confused with thrust::lower_bound/upper_bound
#if (THRUST_HOST_COMPILER == THRUST_HOST_COMPILER_MSVC) && (THRUST_DEVICE_BACKEND == THRUST_DEVICE_BACKEND_OMP)
KNOWN_FAILURE;
#else
thrust::host_vector<T> h_vec = unittest::random_integers<T>(n); thrust::sort(h_vec.begin(), h_vec.end(), thrust::greater<int>());
thrust::device_vector<T> d_vec = h_vec;

thrust::host_vector<T> h_input = unittest::random_integers<T>(2*n);
thrust::device_vector<T> d_input = h_input;

thrust::host_vector<int> h_output(2*n);
thrust::device_vector<int> d_output(2*n);

thrust::binary_search(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), h_output.begin(), thrust::greater<int>());
thrust::binary_search(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), d_output.begin(), thrust::greater<int>());

ASSERT_EQUAL(h_output, d_output);
#endif
}
};
VariableUnitTest<TestVectorBinarySearchDescending, SignedIntegralTypes> TestVectorBinarySearchDescendingInstance;

Loading

0 comments on commit 5788a4b

Please sign in to comment.