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
|
#include <thrust/execution_policy.h>
#include <thrust/functional.h>
#include <thrust/sort.h>
#include <unittest/unittest.h>
template <typename T>
struct my_less
{
_CCCL_HOST_DEVICE bool operator()(const T& lhs, const T& rhs) const
{
return lhs < rhs;
}
};
#ifdef THRUST_TEST_DEVICE_SIDE
template <typename ExecutionPolicy, typename Iterator, typename Compare>
__global__ void sort_kernel(ExecutionPolicy exec, Iterator first, Iterator last, Compare comp)
{
thrust::sort(exec, first, last, comp);
}
template <typename T, typename ExecutionPolicy, typename Compare>
void TestComparisonSortDevice(ExecutionPolicy exec, const size_t n, Compare comp)
{
thrust::host_vector<T> h_data = unittest::random_integers<T>(n);
thrust::device_vector<T> d_data = h_data;
sort_kernel<<<1, 1>>>(exec, d_data.begin(), d_data.end(), comp);
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
thrust::sort(h_data.begin(), h_data.end(), comp);
ASSERT_EQUAL(h_data, d_data);
};
template <typename T>
struct TestComparisonSortDeviceSeq
{
void operator()(const size_t n)
{
TestComparisonSortDevice<T>(thrust::seq, n, my_less<T>());
}
};
VariableUnitTest<TestComparisonSortDeviceSeq, unittest::type_list<unittest::int8_t, unittest::int32_t>>
TestComparisonSortDeviceSeqInstance;
template <typename T>
struct TestComparisonSortDeviceDevice
{
void operator()(const size_t n)
{
TestComparisonSortDevice<T>(thrust::device, n, my_less<T>());
}
};
VariableUnitTest<TestComparisonSortDeviceDevice, unittest::type_list<unittest::int8_t, unittest::int32_t>>
TestComparisonSortDeviceDeviceDeviceInstance;
template <typename T, typename ExecutionPolicy>
void TestSortDevice(ExecutionPolicy exec, const size_t n)
{
TestComparisonSortDevice<T>(exec, n, thrust::less<T>());
};
template <typename T>
struct TestSortDeviceSeq
{
void operator()(const size_t n)
{
TestSortDevice<T>(thrust::seq, n);
}
};
VariableUnitTest<TestSortDeviceSeq, unittest::type_list<unittest::int8_t, unittest::int32_t>> TestSortDeviceSeqInstance;
template <typename T>
struct TestSortDeviceDevice
{
void operator()(const size_t n)
{
TestSortDevice<T>(thrust::device, n);
}
};
VariableUnitTest<TestSortDeviceDevice, unittest::type_list<unittest::int8_t, unittest::int32_t>>
TestSortDeviceDeviceInstance;
#endif
void TestSortCudaStreams()
{
thrust::device_vector<int> keys(10);
keys[0] = 9;
keys[1] = 3;
keys[2] = 2;
keys[3] = 0;
keys[4] = 4;
keys[5] = 7;
keys[6] = 8;
keys[7] = 1;
keys[8] = 5;
keys[9] = 6;
cudaStream_t s;
cudaStreamCreate(&s);
thrust::sort(thrust::cuda::par.on(s), keys.begin(), keys.end());
cudaStreamSynchronize(s);
ASSERT_EQUAL(true, thrust::is_sorted(keys.begin(), keys.end()));
cudaStreamDestroy(s);
}
DECLARE_UNITTEST(TestSortCudaStreams);
void TestComparisonSortCudaStreams()
{
thrust::device_vector<int> keys(10);
keys[0] = 9;
keys[1] = 3;
keys[2] = 2;
keys[3] = 0;
keys[4] = 4;
keys[5] = 7;
keys[6] = 8;
keys[7] = 1;
keys[8] = 5;
keys[9] = 6;
cudaStream_t s;
cudaStreamCreate(&s);
thrust::sort(thrust::cuda::par.on(s), keys.begin(), keys.end(), my_less<int>());
cudaStreamSynchronize(s);
ASSERT_EQUAL(true, thrust::is_sorted(keys.begin(), keys.end(), my_less<int>()));
cudaStreamDestroy(s);
}
DECLARE_UNITTEST(TestComparisonSortCudaStreams);
|