/* * Copyright 2008-2013 NVIDIA Corporation * Modifications Copyright© 2019-2025 Advanced Micro Devices, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #include #include "test_param_fixtures.hpp" #include "test_utils.hpp" using VectorTestsParams = ::testing::Types< Params>, Params>, Params>, Params>, Params>>, Params>, Params>, Params>, Params>, Params>>, Params>, Params>>; using IntegralTypes = ::testing::Types< Params, Params, Params, Params, Params, Params, Params, Params, Params, Params, Params>; TESTS_DEFINE(InnerProductTests, FullTestsParams); TESTS_DEFINE(PrimitiveInnerProductTests, NumericalTestsParams); TESTS_DEFINE(VectorUnitInnerProductTests, VectorTestsParams); TESTS_DEFINE(VariableUnitInnerProductTests, IntegralTypes); TEST(InnerProductTests, UsingHip) { SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); ASSERT_EQ(THRUST_DEVICE_SYSTEM, THRUST_DEVICE_SYSTEM_HIP); } TYPED_TEST(VectorUnitInnerProductTests, TestInnerProductSimple) { using Vector = typename TestFixture::input_type; using T = typename Vector::value_type; SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); Vector v1{1, -2, 3}; Vector v2{-4, 5, 6}; T init = 3; T result = thrust::inner_product(v1.begin(), v1.end(), v2.begin(), init); ASSERT_EQ(result, 7); } template int inner_product(my_system& system, InputIterator1, InputIterator1, InputIterator2, OutputType) { system.validate_dispatch(); return 13; } TEST(InnerProductTests, TestInnerProductDispatchExplicit) { SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); thrust::device_vector vec; my_system sys(0); thrust::inner_product(sys, vec.begin(), vec.end(), vec.begin(), 0); ASSERT_EQ(true, sys.is_valid()); } template int inner_product(my_tag, InputIterator1, InputIterator1, InputIterator2, OutputType) { return 13; } TEST(InnerProductTests, TestInnerProductDispatchImplicit) { SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); thrust::device_vector vec; int result = thrust::inner_product( thrust::retag(vec.begin()), thrust::retag(vec.end()), thrust::retag(vec.begin()), 0); ASSERT_EQ(13, result); } TYPED_TEST(VectorUnitInnerProductTests, TestInnerProductWithOperator) { using Vector = typename TestFixture::input_type; using T = typename Vector::value_type; SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); Vector v1{1, -2, 3}; Vector v2{-1, 3, 6}; // compute (v1 - v2) and perform a multiplies reduction T init = 3; T result = thrust::inner_product(v1.begin(), v1.end(), v2.begin(), init, thrust::multiplies(), thrust::minus()); ASSERT_EQ(result, 90); } TYPED_TEST(VariableUnitInnerProductTests, TestInnerProduct) { using T = typename TestFixture::input_type; SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); for (auto size : get_sizes()) { SCOPED_TRACE(testing::Message() << "with size= " << size); thrust::host_vector h_v1 = random_integers(size); thrust::host_vector h_v2 = random_integers(size); thrust::device_vector d_v1 = h_v1; thrust::device_vector d_v2 = h_v2; T init = 13; T expected = thrust::inner_product(h_v1.begin(), h_v1.end(), h_v2.begin(), init); T result = thrust::inner_product(d_v1.begin(), d_v1.end(), d_v2.begin(), init); ASSERT_EQ(expected, result); } } struct only_set_when_both_expected { long long expected; bool* flag; THRUST_HOST_DEVICE long long operator()(long long x, long long y) { if (x == expected && y == expected) { *flag = true; } return x == y; } }; void TestInnerProductWithBigIndexesHelper(int magnitude) { thrust::counting_iterator begin(1); thrust::counting_iterator end = begin + (1ll << magnitude); ASSERT_EQ(thrust::distance(begin, end), 1ll << magnitude); thrust::device_ptr has_executed = thrust::device_malloc(1); *has_executed = false; only_set_when_both_expected fn = {(1ll << magnitude) - 1, thrust::raw_pointer_cast(has_executed)}; ASSERT_EQ(thrust::inner_product(thrust::device, begin, end, begin, 0ll, thrust::plus(), fn), (1ll << magnitude)); bool has_executed_h = *has_executed; thrust::device_free(has_executed); ASSERT_EQ(has_executed_h, true); } TEST(InnerProductTests, TestInnerProductWithBigIndexes) { SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); TestInnerProductWithBigIndexesHelper(30); #ifndef THRUST_FORCE_32_BIT_OFFSET_TYPE TestInnerProductWithBigIndexesHelper(31); TestInnerProductWithBigIndexesHelper(32); TestInnerProductWithBigIndexesHelper(33); #endif } TEST(InnerProductTests, TestInnerProductPlaceholders) { // Regression test for NVIDIA/thrust#1178 using namespace thrust::placeholders; SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); thrust::device_vector v1(100, 1.f); thrust::device_vector v2(100, 1.f); auto result = thrust::inner_product(v1.begin(), v1.end(), v2.begin(), 0.0f, thrust::plus{}, _1 * _2 + 1.0f); auto error_margin = 1e-4 * (::std::abs(result) + 200.f) * 1e-4; ASSERT_NEAR(static_cast(result), static_cast(200.f), static_cast(error_margin)); } template T clip_infinity(T val) { T min = std::numeric_limits::min(); T max = std::numeric_limits::max(); if (val > max) { return max; } if (val < min) { return min; } return val; } TYPED_TEST(PrimitiveInnerProductTests, InnerProductWithRandomData) { using T = typename TestFixture::input_type; SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); for (auto size : get_sizes()) { SCOPED_TRACE(testing::Message() << "with size= " << size); T error_margin = static_cast(0.01) * size; T min = saturate_cast(-10); T max = saturate_cast(10); for (auto seed : get_seeds()) { SCOPED_TRACE(testing::Message() << "with seed= " << seed); thrust::host_vector h_v1 = get_random_data(size, min, max, seed); thrust::host_vector h_v2 = get_random_data(size, min, max, seed + seed_value_addition); thrust::device_vector d_v1 = h_v1; thrust::device_vector d_v2 = h_v2; T init = 13; T expected = thrust::inner_product(h_v1.begin(), h_v1.end(), h_v2.begin(), init); T result = thrust::inner_product(d_v1.begin(), d_v1.end(), d_v2.begin(), init); ASSERT_NEAR(clip_infinity(expected), clip_infinity(result), error_margin); } } };