Skip to content

Commit

Permalink
simd: make mask and condition unit test to check with all data types (k…
Browse files Browse the repository at this point in the history
…okkos#6360)

* Improved on unit tests for mask ops

* Fixed invalid calls in the device version of test

* Converted to check for all data types for condition unit test
  • Loading branch information
ldh4 committed Aug 23, 2023
1 parent bb0633e commit 7e45f17
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 20 deletions.
62 changes: 48 additions & 14 deletions simd/unit_tests/include/TestSIMD_Condition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,69 @@
#include <Kokkos_SIMD.hpp>
#include <SIMDTesting_Utilities.hpp>

template <typename Abi>
template <typename Abi, typename DataType>
inline void host_check_condition() {
auto a = Kokkos::Experimental::condition(
Kokkos::Experimental::simd<std::int32_t, Abi>(1) > 0,
Kokkos::Experimental::simd<std::uint64_t, Abi>(16),
Kokkos::Experimental::simd<std::uint64_t, Abi>(20));
EXPECT_TRUE(all_of(a == decltype(a)(16)));
using simd_type = typename Kokkos::Experimental::simd<DataType, Abi>;
using mask_type = typename simd_type::mask_type;

auto condition_op = [](mask_type const& mask, simd_type const& a,
simd_type const& b) {
return Kokkos::Experimental::condition(mask, a, b);
};

simd_type value_a(16);
simd_type value_b(20);

auto condition_result = condition_op(mask_type(false), value_a, value_b);
EXPECT_TRUE(all_of(condition_result == value_b));
condition_result = condition_op(mask_type(true), value_a, value_b);
EXPECT_TRUE(all_of(condition_result == value_a));
}

template <typename Abi, typename... DataTypes>
inline void host_check_condition_all_types(
Kokkos::Experimental::Impl::data_types<DataTypes...>) {
(host_check_condition<Abi, DataTypes>(), ...);
}

template <typename... Abis>
inline void host_check_condition_all_abis(
Kokkos::Experimental::Impl::abi_set<Abis...>) {
(host_check_condition<Abis>(), ...);
using DataTypes = Kokkos::Experimental::Impl::data_type_set;
(host_check_condition_all_types<Abis>(DataTypes()), ...);
}

template <typename Abi>
template <typename Abi, typename DataType>
KOKKOS_INLINE_FUNCTION void device_check_condition() {
using simd_type = typename Kokkos::Experimental::simd<DataType, Abi>;
using mask_type = typename simd_type::mask_type;
kokkos_checker checker;
auto a = Kokkos::Experimental::condition(
Kokkos::Experimental::simd<std::int32_t, Abi>(1) > 0,
Kokkos::Experimental::simd<std::uint64_t, Abi>(16),
Kokkos::Experimental::simd<std::uint64_t, Abi>(20));
checker.truth(all_of(a == decltype(a)(16)));

auto condition_op = [](mask_type const& mask, simd_type const& a,
simd_type const& b) {
return Kokkos::Experimental::condition(mask, a, b);
};

simd_type value_a(16);
simd_type value_b(20);

auto condition_result = condition_op(mask_type(false), value_a, value_b);
checker.truth(all_of(condition_result == value_b));
condition_result = condition_op(mask_type(true), value_a, value_b);
checker.truth(all_of(condition_result == value_a));
}

template <typename Abi, typename... DataTypes>
KOKKOS_INLINE_FUNCTION void device_check_condition_all_types(
Kokkos::Experimental::Impl::data_types<DataTypes...>) {
(device_check_condition<Abi, DataTypes>(), ...);
}

template <typename... Abis>
KOKKOS_INLINE_FUNCTION void device_check_condition_all_abis(
Kokkos::Experimental::Impl::abi_set<Abis...>) {
(device_check_condition<Abis>(), ...);
using DataTypes = Kokkos::Experimental::Impl::data_type_set;
(device_check_condition_all_types<Abis>(DataTypes()), ...);
}

class simd_device_condition_functor {
Expand Down
57 changes: 51 additions & 6 deletions simd/unit_tests/include/TestSIMD_MaskOps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,80 @@
#include <Kokkos_SIMD.hpp>
#include <SIMDTesting_Utilities.hpp>

template <typename Abi>
template <typename Abi, typename DataType>
inline void host_check_mask_ops() {
using mask_type = Kokkos::Experimental::simd_mask<double, Abi>;
using mask_type = Kokkos::Experimental::simd_mask<DataType, Abi>;

EXPECT_FALSE(none_of(mask_type(true)));
EXPECT_TRUE(none_of(mask_type(false)));
EXPECT_TRUE(all_of(mask_type(true)));
EXPECT_FALSE(all_of(mask_type(false)));
EXPECT_TRUE(any_of(mask_type(true)));
EXPECT_FALSE(any_of(mask_type(false)));

for (std::size_t i = 0; i < mask_type::size(); ++i) {
mask_type test_mask([&](std::size_t j) { return i == j; });

EXPECT_TRUE(any_of(test_mask));
EXPECT_FALSE(none_of(test_mask));

if constexpr (mask_type::size() > 1) {
EXPECT_FALSE(all_of(test_mask));
} else {
EXPECT_TRUE(all_of(test_mask));
}
}
}

template <typename Abi, typename... DataTypes>
inline void host_check_mask_ops_all_types(
Kokkos::Experimental::Impl::data_types<DataTypes...>) {
(host_check_mask_ops<Abi, DataTypes>(), ...);
}

template <typename... Abis>
inline void host_check_mask_ops_all_abis(
Kokkos::Experimental::Impl::abi_set<Abis...>) {
(host_check_mask_ops<Abis>(), ...);
using DataTypes = Kokkos::Experimental::Impl::data_type_set;
(host_check_mask_ops_all_types<Abis>(DataTypes()), ...);
}

template <typename Abi>
template <typename Abi, typename DataType>
KOKKOS_INLINE_FUNCTION void device_check_mask_ops() {
using mask_type = Kokkos::Experimental::simd_mask<double, Abi>;
using mask_type = Kokkos::Experimental::simd_mask<DataType, Abi>;
kokkos_checker checker;
checker.truth(!none_of(mask_type(true)));
checker.truth(none_of(mask_type(false)));
checker.truth(all_of(mask_type(true)));
checker.truth(!all_of(mask_type(false)));
checker.truth(any_of(mask_type(true)));
checker.truth(!any_of(mask_type(false)));

for (std::size_t i = 0; i < mask_type::size(); ++i) {
mask_type test_mask([&](std::size_t j) { return i == j; });

checker.truth(any_of(test_mask));
checker.truth(!none_of(test_mask));

if constexpr (mask_type::size() > 1) {
checker.truth(!all_of(test_mask));
} else {
checker.truth(all_of(test_mask));
}
}
}

template <typename Abi, typename... DataTypes>
KOKKOS_INLINE_FUNCTION void device_check_mask_ops_all_types(
Kokkos::Experimental::Impl::data_types<DataTypes...>) {
(device_check_mask_ops<Abi, DataTypes>(), ...);
}

template <typename... Abis>
KOKKOS_INLINE_FUNCTION void device_check_mask_ops_all_abis(
Kokkos::Experimental::Impl::abi_set<Abis...>) {
(device_check_mask_ops<Abis>(), ...);
using DataTypes = Kokkos::Experimental::Impl::data_type_set;
(device_check_mask_ops_all_types<Abis>(DataTypes()), ...);
}

class simd_device_mask_ops_functor {
Expand Down

0 comments on commit 7e45f17

Please sign in to comment.