/* * 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 "test_param_fixtures.hpp" #include "test_real_assertions.hpp" #include "test_utils.hpp" TESTS_DEFINE(ForEachTests, SignedIntegerTestsParams) TESTS_DEFINE(ForEachVectorTests, FullTestsParams) TESTS_DEFINE(ForEachPrimitiveTests, NumericalTestsParams); template struct mark_processed_functor { T* ptr; __host__ __device__ void operator()(size_t x) { ptr[x] = 1; } }; TYPED_TEST(ForEachTests, HostPathSimpleTest) { thrust::device_system_tag tag; using T = typename TestFixture::input_type; SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); const std::vector sizes = get_sizes(); for (auto size : sizes) { SCOPED_TRACE(testing::Message() << "with size = " << size); auto ptr = thrust::malloc(tag, sizeof(T) * size); auto raw_ptr = thrust::raw_pointer_cast(ptr); if (size > 0) { ASSERT_NE(raw_ptr, nullptr); } // Zero input memory if (size > 0) { HIP_CHECK(hipMemset(raw_ptr, 0, sizeof(T) * size)); } // Create unary function mark_processed_functor func; func.ptr = raw_ptr; // Run for_each in [0; end] range auto end = size < 2 ? size : size / 2; auto result = thrust::for_each(thrust::make_counting_iterator(0), thrust::make_counting_iterator(end), func); ASSERT_EQ(result, thrust::make_counting_iterator(end)); std::vector output(size); HIP_CHECK(hipMemcpy(output.data(), raw_ptr, size * sizeof(T), hipMemcpyDeviceToHost)); for (size_t i = 0; i < size; i++) { if (i < end) { ASSERT_EQ(output[i], T(1)) << "where index = " << i; } else { ASSERT_EQ(output[i], T(0)) << "where index = " << i; } } // Free thrust::free(tag, ptr); } } template __global__ THRUST_HIP_LAUNCH_BOUNDS_DEFAULT void simple_test_kernel(F func, int size) { // (void) func; (void) size; thrust::for_each( thrust::hip::par, thrust::make_counting_iterator(0), thrust::make_counting_iterator(size), func); } TYPED_TEST(ForEachTests, DevicePathSimpleTest) { SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); thrust::device_system_tag tag; using T = typename TestFixture::input_type; const size_t size = 1024; auto ptr = thrust::malloc(tag, sizeof(T) * size); auto raw_ptr = thrust::raw_pointer_cast(ptr); ASSERT_NE(raw_ptr, nullptr); // Zero input memory HIP_CHECK(hipMemset(raw_ptr, 0, sizeof(T) * size)); // Create unary function mark_processed_functor func; func.ptr = raw_ptr; // Run for_each in [0; end] range size_t end = 375; hipLaunchKernelGGL( HIP_KERNEL_NAME(simple_test_kernel>), dim3(1), dim3(1), 0, 0, func, static_cast(end)); HIP_CHECK(hipGetLastError()); std::vector output(size); HIP_CHECK(hipMemcpy(output.data(), raw_ptr, size * sizeof(T), hipMemcpyDeviceToHost)); for (size_t i = 0; i < size; i++) { if (i < end) { ASSERT_EQ(output[i], T(1)) << "where index = " << i; } else { ASSERT_EQ(output[i], T(0)) << "where index = " << i; } } // Free thrust::free(tag, ptr); } THRUST_DIAG_PUSH THRUST_DIAG_SUPPRESS_MSVC(4244 4267) // possible loss of data template class mark_present_for_each { public: T* ptr; THRUST_HOST_DEVICE void operator()(T x) { ptr[(int) x] = 1; } }; TYPED_TEST(ForEachVectorTests, TestForEachSimple) { 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 input{3, 2, 3, 4, 6}; Vector output(7, (T) 0); mark_present_for_each f; f.ptr = thrust::raw_pointer_cast(output.data()); typename Vector::iterator result = thrust::for_each(input.begin(), input.end(), f); Vector ref{0, 0, 1, 1, 1, 0, 1}; ASSERT_EQ(output, ref); ASSERT_EQ_QUIET(result, input.end()); } template InputIterator for_each(my_system& system, InputIterator first, InputIterator, Function) { system.validate_dispatch(); return first; } TEST(ForEachVectorTests, TestForEachDispatchExplicit) { SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); thrust::device_vector vec(1); my_system sys(0); thrust::for_each(sys, vec.begin(), vec.end(), 0); ASSERT_EQ(true, sys.is_valid()); } template InputIterator for_each(my_tag, InputIterator first, InputIterator, Function) { *first = 13; return first; } TEST(ForEachVectorTests, TestForEachDispatchImplicit) { SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); thrust::device_vector vec(1); thrust::for_each(thrust::retag(vec.begin()), thrust::retag(vec.end()), 0); ASSERT_EQ(13, vec.front()); } TYPED_TEST(ForEachVectorTests, TestForEachNSimple) { 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 input{3, 2, 3, 4, 6}; Vector output(7, (T) 0); mark_present_for_each f; f.ptr = thrust::raw_pointer_cast(output.data()); typename Vector::iterator result = thrust::for_each_n(input.begin(), input.size(), f); Vector ref{0, 0, 1, 1, 1, 0, 1}; ASSERT_EQ(output, ref); ASSERT_EQ_QUIET(result, input.end()); } template InputIterator for_each_n(my_system& system, InputIterator first, Size, Function) { system.validate_dispatch(); return first; } TEST(ForEachVectorTests, TestForEachNDispatchExplicit) { SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); thrust::device_vector vec(1); my_system sys(0); thrust::for_each_n(sys, vec.begin(), vec.size(), 0); ASSERT_EQ(true, sys.is_valid()); } template InputIterator for_each_n(my_tag, InputIterator first, Size, Function) { *first = 13; return first; } TEST(ForEachVectorTests, TestForEachNDispatchImplicit) { SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); thrust::device_vector vec(1); thrust::for_each_n(thrust::retag(vec.begin()), vec.size(), 0); ASSERT_EQ(13, vec.front()); } TEST(ForEachVectorTests, TestForEachSimpleAnySystem) { SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); thrust::device_vector output(7, 0); mark_present_for_each f; f.ptr = thrust::raw_pointer_cast(output.data()); thrust::counting_iterator result = thrust::for_each(thrust::make_counting_iterator(0), thrust::make_counting_iterator(5), f); thrust::device_vector ref{1, 1, 1, 1, 1, 0, 0}; ASSERT_EQ(output, ref); ASSERT_EQ_QUIET(result, thrust::make_counting_iterator(5)); } TEST(ForEachVectorTests, TestForEachNSimpleAnySystem) { SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); thrust::device_vector output(7, 0); mark_present_for_each f; f.ptr = thrust::raw_pointer_cast(output.data()); thrust::counting_iterator result = thrust::for_each_n(thrust::make_counting_iterator(0), 5, f); thrust::device_vector ref{1, 1, 1, 1, 1, 0, 0}; ASSERT_EQ(output, ref); ASSERT_EQ_QUIET(result, thrust::make_counting_iterator(5)); } TYPED_TEST(ForEachPrimitiveTests, TestForEach) { 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); const size_t output_size = std::min((size_t) 10, 2 * size); for (auto seed : get_seeds()) { SCOPED_TRACE(testing::Message() << "with seed= " << seed); thrust::host_vector h_input = get_random_data(size, get_default_limits::min(), get_default_limits::max(), seed); for (size_t i = 0; i < size; i++) { h_input[i] = ((size_t) h_input[i]) % output_size; } thrust::device_vector d_input = h_input; thrust::host_vector h_output(output_size, (T) 0); thrust::device_vector d_output(output_size, (T) 0); mark_present_for_each h_f; mark_present_for_each d_f; h_f.ptr = &h_output[0]; d_f.ptr = (&d_output[0]).get(); typename thrust::host_vector::iterator h_result = thrust::for_each(h_input.begin(), h_input.end(), h_f); typename thrust::device_vector::iterator d_result = thrust::for_each(d_input.begin(), d_input.end(), d_f); ASSERT_EQ(h_output, d_output); ASSERT_EQ_QUIET(h_result, h_input.end()); ASSERT_EQ_QUIET(d_result, d_input.end()); } } } TYPED_TEST(ForEachPrimitiveTests, TestForEachN) { 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); const size_t output_size = std::min((size_t) 10, 2 * size); for (auto seed : get_seeds()) { SCOPED_TRACE(testing::Message() << "with seed= " << seed); thrust::host_vector h_input = get_random_data(size, get_default_limits::min(), get_default_limits::max(), seed); for (size_t i = 0; i < size; i++) { h_input[i] = ((size_t) h_input[i]) % output_size; } thrust::device_vector d_input = h_input; thrust::host_vector h_output(output_size, (T) 0); thrust::device_vector d_output(output_size, (T) 0); mark_present_for_each h_f; mark_present_for_each d_f; h_f.ptr = &h_output[0]; d_f.ptr = (&d_output[0]).get(); typename thrust::host_vector::iterator h_result = thrust::for_each_n(h_input.begin(), h_input.size(), h_f); typename thrust::device_vector::iterator d_result = thrust::for_each_n(d_input.begin(), d_input.size(), d_f); ASSERT_EQ(h_output, d_output); ASSERT_EQ_QUIET(h_result, h_input.end()); ASSERT_EQ_QUIET(d_result, d_input.end()); } } } template struct SetFixedVectorToConstant { FixedVector exemplar; SetFixedVectorToConstant(T scalar) : exemplar(scalar) {} THRUST_HOST_DEVICE void operator()(FixedVector& t) { t = exemplar; } }; template void _TestForEachWithLargeTypes() { size_t n = (64 * 1024) / sizeof(FixedVector); thrust::host_vector> h_data(n); for (size_t i = 0; i < h_data.size(); i++) { h_data[i] = FixedVector(i); } thrust::device_vector> d_data = h_data; SetFixedVectorToConstant func(123); thrust::for_each(h_data.begin(), h_data.end(), func); thrust::for_each(d_data.begin(), d_data.end(), func); ASSERT_EQ_QUIET(h_data, d_data); } TEST(ForEachVectorTests, TestForEachWithLargeTypes) { SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); // fails on Linux 32 w/ gcc 4.1 _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); // XXX parallel_for doesn't support large types _TestForEachWithLargeTypes(); // fails on Vista 64 w/ VS2008 } template void _TestForEachNWithLargeTypes() { size_t n = (64 * 1024) / sizeof(FixedVector); thrust::host_vector> h_data(n); for (size_t i = 0; i < h_data.size(); i++) { h_data[i] = FixedVector(i); } thrust::device_vector> d_data = h_data; SetFixedVectorToConstant func(123); thrust::for_each_n(h_data.begin(), h_data.size(), func); thrust::for_each_n(d_data.begin(), d_data.size(), func); ASSERT_EQ_QUIET(h_data, d_data); } TEST(ForEachVectorTests, TestForEachNWithLargeTypes) { SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); // fails on Linux 32 w/ gcc 4.1 _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); // XXX parallel_for doesn't support large types _TestForEachNWithLargeTypes(); // fails on Vista 64 w/ VS2008 } THRUST_DIAG_POP struct only_set_when_expected { unsigned long long expected; bool* flag; THRUST_DEVICE void operator()(unsigned long long x) { if (x == expected) { *flag = true; } } }; void TestForEachWithBigIndexesHelper(int magnitude) { thrust::counting_iterator begin(0); thrust::counting_iterator end = begin + (1ull << magnitude); ASSERT_EQ(thrust::distance(begin, end), 1ll << magnitude); thrust::device_ptr has_executed = thrust::device_malloc(1); *has_executed = false; only_set_when_expected fn = {(1ull << magnitude) - 1, thrust::raw_pointer_cast(has_executed)}; thrust::for_each(thrust::device, begin, end, fn); bool has_executed_h = *has_executed; thrust::device_free(has_executed); ASSERT_EQ(has_executed_h, true); } TEST(ForEachVectorTests, TestForEachWithBigIndexes) { SCOPED_TRACE(testing::Message() << "with device_id= " << test::set_device_from_ctest()); TestForEachWithBigIndexesHelper(30); TestForEachWithBigIndexesHelper(31); TestForEachWithBigIndexesHelper(32); TestForEachWithBigIndexesHelper(33); }