Add tests for hip::merge
ADD
test/test_merge.cpp
+// MIT License
+//
+// Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+
+// Google Test
+#include <gtest/gtest.h>
+#include "test_utils.hpp"
+
+// Thrust
+#include <thrust/device_vector.h>
+#include <thrust/host_vector.h>
+#include <thrust/merge.h>
+#include <thrust/functional.h>
+#include <thrust/sort.h>
+#include <thrust/unique.h>
+#include <thrust/extrema.h>
+#include <thrust/iterator/discard_iterator.h>
+#include <thrust/iterator/retag.h>
+
+// HIP API
+#if THRUST_DEVICE_COMPILER == THRUST_DEVICE_COMPILER_HCC
+#include <hip/hip_runtime_api.h>
+#include <hip/hip_runtime.h>
+
+#define HIP_CHECK(condition) ASSERT_EQ(condition, hipSuccess)
+#endif // THRUST_DEVICE_COMPILER == THRUST_DEVICE_COMPILER_HCC
+
+template< class InputType >
+struct Params
+{
+ using input_type = InputType;
+};
+
+template<class Params>
+class MergeTests : public ::testing::Test
+{
+public:
+ using input_type = typename Params::input_type;
+};
+
+template<class Params>
+class PrimitiveMergeTests : public ::testing::Test
+{
+public:
+ using input_type = typename Params::input_type;
+};
+
+typedef ::testing::Types<
+ Params<thrust::host_vector<short>>,
+ Params<thrust::host_vector<int>>,
+ Params<thrust::host_vector<long long>>,
+ Params<thrust::host_vector<unsigned short>>,
+ Params<thrust::host_vector<unsigned int>>,
+ Params<thrust::host_vector<unsigned long long>>,
+ Params<thrust::host_vector<float>>,
+ Params<thrust::host_vector<double>>,
+ Params<thrust::device_vector<short>>,
+ Params<thrust::device_vector<int>>,
+ Params<thrust::device_vector<long long>>,
+ Params<thrust::device_vector<unsigned short>>,
+ Params<thrust::device_vector<unsigned int>>,
+ Params<thrust::device_vector<unsigned long long>>,
+ Params<thrust::device_vector<float>>,
+ Params<thrust::device_vector<double>>
+> MergeTestsParams;
+
+typedef ::testing::Types<
+ Params<short>,
+ Params<int>,
+ Params<long long>,
+ Params<unsigned short>,
+ Params<unsigned int>,
+ Params<unsigned long long>,
+ Params<float>,
+ Params<double>
+> MergeTestsPrimitiveParams;
+
+TYPED_TEST_CASE(MergeTests, MergeTestsParams);
+TYPED_TEST_CASE(PrimitiveMergeTests, MergeTestsPrimitiveParams);
+
+#if THRUST_DEVICE_COMPILER == THRUST_DEVICE_COMPILER_HCC
+
+TEST(MergeTests, UsingHip)
+{
+ ASSERT_EQ(THRUST_DEVICE_SYSTEM, THRUST_DEVICE_SYSTEM_HIP);
+}
+
+TYPED_TEST(MergeTests, MergeSimple)
+{
+ using Vector = typename TestFixture::input_type;
+ using Iterator = typename Vector::iterator;
+
+ Vector a(3), b(4);
+
+ a[0] = 0; a[1] = 2; a[2] = 4;
+ b[0] = 0; b[1] = 3; b[2] = 3; b[3] = 4;
+
+ Vector ref(7);
+ ref[0] = 0;
+ ref[1] = 0;
+ ref[2] = 2;
+ ref[3] = 3;
+ ref[4] = 3;
+ ref[5] = 4;
+ ref[6] = 4;
+
+ Vector result(7);
+
+ Iterator end = thrust::merge(a.begin(), a.end(),
+ b.begin(), b.end(),
+ result.begin());
+
+ EXPECT_EQ(result.end(), end);
+ ASSERT_EQ(ref, result);
+}
+
+template<typename InputIterator1,
+ typename InputIterator2,
+ typename OutputIterator>
+OutputIterator merge(my_system &system,
+ InputIterator1,
+ InputIterator1,
+ InputIterator2,
+ InputIterator2,
+ OutputIterator result)
+{
+ system.validate_dispatch();
+ return result;
+}
+
+TEST(MergeTests, MergeDispatchExplicit)
+{
+ thrust::device_vector<int> vec(1);
+
+ my_system sys(0);
+ thrust::merge(sys,
+ vec.begin(),
+ vec.begin(),
+ vec.begin(),
+ vec.begin(),
+ vec.begin());
+
+ ASSERT_EQ(true, sys.is_valid());
+}
+
+template<typename InputIterator1,
+ typename InputIterator2,
+ typename OutputIterator>
+OutputIterator merge(my_tag,
+ InputIterator1,
+ InputIterator1,
+ InputIterator2,
+ InputIterator2,
+ OutputIterator result)
+{
+ *result = 13;
+ return result;
+}
+
+TEST(MergeTests, MergeDispatchImplicit)
+{
+ thrust::device_vector<int> vec(1);
+
+ thrust::merge(thrust::retag<my_tag>(vec.begin()),
+ thrust::retag<my_tag>(vec.begin()),
+ thrust::retag<my_tag>(vec.begin()),
+ thrust::retag<my_tag>(vec.begin()),
+ thrust::retag<my_tag>(vec.begin()));
+
+ ASSERT_EQ(13, vec.front());
+}
+
+TYPED_TEST(PrimitiveMergeTests, MergeWithRandomData)
+{
+ using T = typename TestFixture::input_type;
+
+ const std::vector<size_t> sizes = get_sizes();
+ for(auto size : sizes)
+ {
+ size_t expanded_sizes[] = {0, 1, size / 2, size, size + 1, 2 * size};
+ size_t num_expanded_sizes = sizeof(expanded_sizes) / sizeof(size_t);
+
+ thrust::host_vector<T> random = get_random_data<unsigned short int>(size + *thrust::max_element(expanded_sizes, expanded_sizes + num_expanded_sizes), 0, 255);
+ thrust::host_vector<T> h_a(random.begin(), random.begin() + size);
+ thrust::host_vector<T> h_b(random.begin() + size, random.end());
+
+ thrust::stable_sort(h_a.begin(), h_a.end());
+ thrust::stable_sort(h_b.begin(), h_b.end());
+
+ thrust::device_vector<T> d_a = h_a;
+ thrust::device_vector<T> d_b = h_b;
+
+ for (size_t i = 0; i < num_expanded_sizes; i++)
+ {
+ size_t expanded_size = expanded_sizes[i];
+
+ thrust::host_vector<T> h_result(size + expanded_size);
+ thrust::device_vector<T> d_result(size + expanded_size);
+
+ typename thrust::host_vector<T>::iterator h_end;
+ typename thrust::device_vector<T>::iterator d_end;
+
+ h_end = thrust::merge(h_a.begin(), h_a.end(),
+ h_b.begin(), h_b.begin() + expanded_size,
+ h_result.begin());
+ h_result.resize(h_end - h_result.begin());
+
+ d_end = thrust::merge(d_a.begin(), d_a.end(),
+ d_b.begin(), d_b.begin() + expanded_size,
+ d_result.begin());
+ d_result.resize(d_end - d_result.begin());
+
+ thrust::host_vector<T> d_result_h = d_result;
+ ASSERT_EQ(h_result, d_result_h);
+ }
+ }
+}
+
+TYPED_TEST(PrimitiveMergeTests, MergeToDiscardIterator)
+{
+ using T = typename TestFixture::input_type;
+
+ const std::vector<size_t> sizes = get_sizes();
+ for(auto size : sizes)
+ {
+ thrust::host_vector<T> h_a = get_random_data<T>(size,
+ std::numeric_limits<T>::min(),
+ std::numeric_limits<T>::max());
+ thrust::host_vector<T> h_b = get_random_data<T>(size,
+ std::numeric_limits<T>::min(),
+ std::numeric_limits<T>::max());
+
+ thrust::stable_sort(h_a.begin(), h_a.end());
+ thrust::stable_sort(h_b.begin(), h_b.end());
+
+ thrust::device_vector<T> d_a = h_a;
+ thrust::device_vector<T> d_b = h_b;
+
+ thrust::discard_iterator<> h_result =
+ thrust::merge(h_a.begin(), h_a.end(),
+ h_b.begin(), h_b.end(),
+ thrust::make_discard_iterator());
+
+ thrust::discard_iterator<> d_result =
+ thrust::merge(d_a.begin(), d_a.end(),
+ d_b.begin(), d_b.end(),
+ thrust::make_discard_iterator());
+
+ thrust::discard_iterator<> reference(2 * size);
+
+ ASSERT_EQ(reference, h_result);
+ ASSERT_EQ(reference, d_result);
+ }
+}
+
+TYPED_TEST(PrimitiveMergeTests, MergeDescending)
+{
+ using T = typename TestFixture::input_type;
+
+ const std::vector<size_t> sizes = get_sizes();
+ for(auto size : sizes)
+ {
+ thrust::host_vector<T> h_a = get_random_data<T>(size,
+ std::numeric_limits<T>::min(),
+ std::numeric_limits<T>::max());
+ thrust::host_vector<T> h_b = get_random_data<T>(size,
+ std::numeric_limits<T>::min(),
+ std::numeric_limits<T>::max());
+
+ thrust::stable_sort(h_a.begin(), h_a.end(), thrust::greater<T>());
+ thrust::stable_sort(h_b.begin(), h_b.end(), thrust::greater<T>());
+
+ thrust::device_vector<T> d_a = h_a;
+ thrust::device_vector<T> d_b = h_b;
+
+ thrust::host_vector<T> h_result(h_a.size() + h_b.size());
+ thrust::device_vector<T> d_result(d_a.size() + d_b.size());
+
+ typename thrust::host_vector<T>::iterator h_end;
+ typename thrust::device_vector<T>::iterator d_end;
+
+ h_end = thrust::merge(h_a.begin(), h_a.end(),
+ h_b.begin(), h_b.end(),
+ h_result.begin(),
+ thrust::greater<T>());
+
+ d_end = thrust::merge(d_a.begin(), d_a.end(),
+ d_b.begin(), d_b.end(),
+ d_result.begin(),
+ thrust::greater<T>());
+
+ ASSERT_EQ(h_result, d_result);
+ }
+}
+
+#endif // THRUST_DEVICE_COMPILER == THRUST_DEVICE_COMPILER_HCC

Add tests for hip::merge