#include #include #include #include #include template auto call_merge(Args&&... args) -> decltype(thrust::merge(std::forward(args)...)) { if constexpr (::cuda::std::is_void::value) { return thrust::merge(std::forward(args)...); } else { // TODO(bgruber): remove next line in C++17 and pass CompareOp{} directly to stable_sort using C = ::cuda::std::conditional_t<::cuda::std::is_void::value, ::cuda::std::less, CompareOp>; return thrust::merge(std::forward(args)..., C{}); } _CCCL_UNREACHABLE(); } template void TestMergeKeyValue(size_t n) { using T = key_value; const auto h_keys_a = unittest::random_integers(n); const auto h_values_a = unittest::random_integers(n); const auto h_keys_b = unittest::random_integers(n); const auto h_values_b = unittest::random_integers(n); thrust::host_vector h_a(n), h_b(n); for (size_t i = 0; i < n; ++i) { h_a[i] = T(h_keys_a[i], h_values_a[i]); h_b[i] = T(h_keys_b[i], h_values_b[i]); } if constexpr (::cuda::std::is_void::value) { thrust::stable_sort(h_a.begin(), h_a.end()); thrust::stable_sort(h_b.begin(), h_b.end()); } else { // TODO(bgruber): remove next line in C++17 and pass CompareOp{} directly to stable_sort using C = ::cuda::std::conditional_t<::cuda::std::is_void::value, ::cuda::std::less, CompareOp>; thrust::stable_sort(h_a.begin(), h_a.end(), C{}); thrust::stable_sort(h_b.begin(), h_b.end(), C{}); } const thrust::device_vector d_a = h_a; const thrust::device_vector d_b = h_b; thrust::host_vector h_result(h_a.size() + h_b.size()); thrust::device_vector d_result(d_a.size() + d_b.size()); const auto h_end = call_merge(h_a.begin(), h_a.end(), h_b.begin(), h_b.end(), h_result.begin()); const auto d_end = call_merge(d_a.begin(), d_a.end(), d_b.begin(), d_b.end(), d_result.begin()); ASSERT_EQUAL_QUIET(h_result, d_result); ASSERT_EQUAL(true, h_end == h_result.end()); ASSERT_EQUAL(true, d_end == d_result.end()); } DECLARE_VARIABLE_UNITTEST(TestMergeKeyValue); template void TestMergeKeyValueDescending(size_t n) { TestMergeKeyValue>>(n); } DECLARE_VARIABLE_UNITTEST(TestMergeKeyValueDescending);