// @HEADER // ***************************************************************************** // Sacado Package // // Copyright 2006 NTESS and the Sacado contributors. // SPDX-License-Identifier: LGPL-2.1-or-later // ***************************************************************************** // @HEADER #include "Teuchos_TestingHelpers.hpp" #include "Sacado.hpp" template struct is_dfad { static const bool value = false; }; template struct is_dfad< Sacado::Fad::Exp::DFad > { static const bool value = true; }; template bool checkFads(const FadType1& x, const FadType2& x2, Teuchos::FancyOStream& out, double tol = 1.0e-15) { bool success = true; // Check sizes match TEUCHOS_TEST_EQUALITY(x.size(), x2.size(), out, success); // Check values match TEUCHOS_TEST_FLOATING_EQUALITY(x.val(), x2.val(), tol, out, success); // Check derivatives match for (int i=0; i inline fadtype generate_fad( const ordinal num_rows, const ordinal num_cols, const ordinal fad_size, const ordinal row, const ordinal col ) { typedef typename fadtype::value_type scalar; fadtype x(fad_size, scalar(0.0)); const scalar x_row = 100.0 + scalar(num_rows) / scalar(row+1); const scalar x_col = 10.0 + scalar(num_cols) / scalar(col+1); x.val() = x_row + x_col; for (ordinal i=0; i static auto apply(const T1& a, const T2& b) -> decltype(a+b) { return a+b; } }; struct SubTag { static double init() { return 0.0; } template static auto apply(const T1& a, const T2& b) -> decltype(a-b) { return a-b; } }; struct MulTag { static double init() { return 1.0; } template static auto apply(const T1& a, const T2& b) -> decltype(a*b) { return a*b; } }; struct DivTag { static double init() { return 1.0; } template static auto apply(const T1& a, const T2& b) -> decltype(a/b) { return a/b; } }; struct MaxTag { static double init() { return 1.0; } template static auto apply(const T1& a, const T2& b) -> decltype(max(a,b)) { return max(a,b); } }; struct MinTag { static double init() { return 1.0; } template static auto apply(const T1& a, const T2& b) -> decltype(min(a,b)) { return min(a,b); } }; // Kernel to test atomic_add template struct AtomicKernel { typedef typename ViewType::execution_space execution_space; typedef typename ViewType::size_type size_type; typedef typename Kokkos::TeamPolicy< execution_space>::member_type team_handle; typedef typename Kokkos::ThreadLocalScalarType::type local_scalar_type; static const size_type stride = Kokkos::ViewScalarStride::stride; const ViewType m_v; const ScalarViewType m_s; AtomicKernel(const ViewType& v, const ScalarViewType& s) : m_v(v), m_s(s) {}; KOKKOS_INLINE_FUNCTION void operator() (AddTag tag, const size_type i) const { local_scalar_type x = m_v(i); if (OperFetch) Kokkos::atomic_add_fetch(&(m_s()), x); else Kokkos::atomic_fetch_add(&(m_s()), x); } KOKKOS_INLINE_FUNCTION void operator() (SubTag tag, const size_type i) const { local_scalar_type x = m_v(i); if (OperFetch) Kokkos::atomic_sub_fetch(&(m_s()), x); else Kokkos::atomic_fetch_sub(&(m_s()), x); } KOKKOS_INLINE_FUNCTION void operator() (MulTag tag, const size_type i) const { local_scalar_type x = m_v(i); if (OperFetch) Kokkos::atomic_mul_fetch(&(m_s()), x); else Kokkos::atomic_fetch_mul(&(m_s()), x); } KOKKOS_INLINE_FUNCTION void operator() (DivTag tag, const size_type i) const { local_scalar_type x = m_v(i); if (OperFetch) Kokkos::atomic_div_fetch(&(m_s()), x); else Kokkos::atomic_fetch_div(&(m_s()), x); } KOKKOS_INLINE_FUNCTION void operator() (MaxTag tag, const size_type i) const { local_scalar_type x = m_v(i); if (OperFetch) Kokkos::atomic_max_fetch(&(m_s()), x); else Kokkos::atomic_fetch_max(&(m_s()), x); } KOKKOS_INLINE_FUNCTION void operator() (MinTag tag, const size_type i) const { local_scalar_type x = m_v(i); if (OperFetch) Kokkos::atomic_min_fetch(&(m_s()), x); else Kokkos::atomic_fetch_min(&(m_s()), x); } template KOKKOS_INLINE_FUNCTION void operator()( Tag tag, const team_handle& team ) const { const size_type i = team.league_rank()*team.team_size() + team.team_rank(); if (i < m_v.extent(0)) (*this)(tag, i); } // Kernel launch template static void apply(Tag tag, const ViewType& v, const ScalarViewType& s) { const size_type nrow = v.extent(0); #if defined (KOKKOS_ENABLE_CUDA) && defined (SACADO_VIEW_CUDA_HIERARCHICAL) const bool use_team = std::is_same::value && Kokkos::is_view_fad_contiguous::value && ( stride > 1 ); #elif defined (KOKKOS_ENABLE_CUDA) && defined (SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) const bool use_team = std::is_same::value && Kokkos::is_view_fad_contiguous::value && is_dfad::value; #elif defined (KOKKOS_ENABLE_HIP) && defined (SACADO_VIEW_CUDA_HIERARCHICAL) const bool use_team = std::is_same::value && Kokkos::is_view_fad_contiguous::value && ( stride > 1 ); #elif defined (KOKKOS_ENABLE_HIP) && defined (SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) const bool use_team = std::is_same::value && Kokkos::is_view_fad_contiguous::value && is_dfad::value; #else const bool use_team = false; #endif if (use_team) { const size_type team_size = 256 / stride; Kokkos::TeamPolicy policy( (nrow+team_size-1)/team_size, team_size, stride ); Kokkos::parallel_for( policy, AtomicKernel(v,s) ); } else { Kokkos::RangePolicy policy( 0, nrow ); Kokkos::parallel_for( policy, AtomicKernel(v,s) ); } } }; template bool testAtomic(const TagType& tag, Teuchos::FancyOStream& out) { typedef Kokkos::View ViewType; typedef Kokkos::View ScalarViewType; typedef typename ViewType::size_type size_type; typedef typename ViewType::HostMirror host_view_type; typedef typename ScalarViewType::HostMirror host_scalar_view_type; const size_type num_rows = global_num_rows; const size_type fad_size = global_fad_size; // Create and fill view ViewType v; ScalarViewType s0; #if defined (SACADO_DISABLE_FAD_VIEW_SPEC) v = ViewType ("view", num_rows); s0 = ScalarViewType (""); #else v = ViewType ("view", num_rows, fad_size+1); s0 = ScalarViewType ("", fad_size+1); #endif host_view_type h_v = Kokkos::create_mirror_view(v); for (size_type i=0; i(num_rows, size_type(1), fad_size, i, size_type(0)); Kokkos::deep_copy(v, h_v); Kokkos::deep_copy(s0, tag.init()); // Create scalar view ScalarViewType s; #if defined (SACADO_DISABLE_FAD_VIEW_SPEC) s = ScalarViewType ("scalar view"); #else s = ScalarViewType ("scalar view", fad_size+1); #endif Kokkos::deep_copy( s, tag.init() ); // Call atomic_add kernel, which adds up entries in v AtomicKernel::apply( tag, v, s ); // Copy to host host_scalar_view_type hs = Kokkos::create_mirror_view(s); Kokkos::deep_copy(hs, s); // Compute correct result auto b = Kokkos::create_mirror_view(s0); Kokkos::deep_copy(b, s0); for (size_type i=0; i(AddTag(), out); } TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL( Kokkos_View_Fad, AtomicSubFetch, FadType, Layout, Device ) { success = testAtomic(SubTag(), out); } TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL( Kokkos_View_Fad, AtomicMulFetch, FadType, Layout, Device ) { success = testAtomic(MulTag(), out); } TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL( Kokkos_View_Fad, AtomicDivFetch, FadType, Layout, Device ) { success = testAtomic(DivTag(), out); } TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL( Kokkos_View_Fad, AtomicMaxFetch, FadType, Layout, Device ) { success = testAtomic(MaxTag(), out); } TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL( Kokkos_View_Fad, AtomicMinFetch, FadType, Layout, Device ) { success = testAtomic(MinTag(), out); } // Test atomic_fetch_oper form TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL( Kokkos_View_Fad, AtomicFetchAdd, FadType, Layout, Device ) { success = testAtomic(AddTag(), out); } TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL( Kokkos_View_Fad, AtomicFetchSub, FadType, Layout, Device ) { success = testAtomic(SubTag(), out); } TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL( Kokkos_View_Fad, AtomicFetchMul, FadType, Layout, Device ) { success = testAtomic(MulTag(), out); } TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL( Kokkos_View_Fad, AtomicFetchDiv, FadType, Layout, Device ) { success = testAtomic(DivTag(), out); } TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL( Kokkos_View_Fad, AtomicFetchMax, FadType, Layout, Device ) { success = testAtomic(MaxTag(), out); } TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL( Kokkos_View_Fad, AtomicFetchMin, FadType, Layout, Device ) { success = testAtomic(MinTag(), out); } #define VIEW_FAD_TESTS_FLD( F, L, D ) \ TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( Kokkos_View_Fad, AtomicAddFetch, F, L, D ) \ TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( Kokkos_View_Fad, AtomicSubFetch, F, L, D ) \ TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( Kokkos_View_Fad, AtomicMulFetch, F, L, D ) \ TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( Kokkos_View_Fad, AtomicDivFetch, F, L, D ) \ TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( Kokkos_View_Fad, AtomicMaxFetch, F, L, D ) \ TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( Kokkos_View_Fad, AtomicMinFetch, F, L, D ) \ TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( Kokkos_View_Fad, AtomicFetchAdd, F, L, D ) \ TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( Kokkos_View_Fad, AtomicFetchSub, F, L, D ) \ TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( Kokkos_View_Fad, AtomicFetchMul, F, L, D ) \ TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( Kokkos_View_Fad, AtomicFetchDiv, F, L, D ) \ TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( Kokkos_View_Fad, AtomicFetchMax, F, L, D ) \ TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( Kokkos_View_Fad, AtomicFetchMin, F, L, D ) using Kokkos::LayoutLeft; using Kokkos::LayoutRight; typedef Kokkos::LayoutContiguous LeftContiguous; typedef Kokkos::LayoutContiguous RightContiguous; #define VIEW_FAD_TESTS_FD( F, D ) \ VIEW_FAD_TESTS_FLD( F, LayoutLeft, D ) \ VIEW_FAD_TESTS_FLD( F, LayoutRight, D ) \ VIEW_FAD_TESTS_FLD( F, LeftContiguous, D ) \ VIEW_FAD_TESTS_FLD( F, RightContiguous, D ) // Full set of atomics only implemented for new design #if SACADO_ENABLE_NEW_DESIGN typedef Sacado::Fad::Exp::DFad DFadType; typedef Sacado::Fad::Exp::SLFad SLFadType; typedef Sacado::Fad::Exp::SFad SFadType; #if SACADO_TEST_DFAD #define VIEW_FAD_TESTS_D( D ) \ VIEW_FAD_TESTS_FD( SFadType, D ) \ VIEW_FAD_TESTS_FD( SLFadType, D ) \ VIEW_FAD_TESTS_FD( DFadType, D ) #else #define VIEW_FAD_TESTS_D( D ) \ VIEW_FAD_TESTS_FD( SFadType, D ) \ VIEW_FAD_TESTS_FD( SLFadType, D ) #endif #else #define VIEW_FAD_TESTS_D( D ) /* */ #endif