Skip to content

Commit

Permalink
improve tests to address review
Browse files Browse the repository at this point in the history
  • Loading branch information
fnrizzi committed Sep 11, 2023
1 parent e3a608b commit 3bbbe2b
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 107 deletions.
23 changes: 23 additions & 0 deletions algorithms/unit_tests/TestStdAlgorithmsCommon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,29 @@ auto create_deep_copyable_compatible_clone(ViewType view) {
// others
//

template <class TeamHandleType, class ValueType1, class ValueType2>
KOKKOS_FUNCTION bool team_members_have_matching_result(
const TeamHandleType& teamHandle, const ValueType1 memberValueIn,
const ValueType2 targetIn) {
using T = std::common_type_t<ValueType1, ValueType2>;
const T memberValue = memberValueIn;
const T target = targetIn;

// set accum to 1 if a mismach is found
const bool mismatch = memberValue != target;
int accum = static_cast<int>(mismatch);
// FIXME_OPENMPTARGET: team API does not meet the TeamHandle concept and
// ignores the reducer passed
#if defined KOKKOS_ENABLE_OPENMPTARGET
Kokkos::Sum<int> dummyReducer(accum);
const auto result = teamHandle.team_reduce(accum, dummyReducer);
return (result == 0);
#else
teamHandle.team_reduce(Kokkos::Sum<int>(accum));
return (accum == 0);
#endif
}

template <class ValueType1, class ValueType2>
auto make_bounds(const ValueType1& lower, const ValueType2 upper) {
return Kokkos::pair<ValueType1, ValueType2>{lower, upper};
Expand Down
42 changes: 30 additions & 12 deletions algorithms/unit_tests/TestStdAlgorithmsTeamCopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@ namespace TeamCopy {

namespace KE = Kokkos::Experimental;

template <class SourceViewType, class DestViewType, class DistancesViewType>
template <class SourceViewType, class DestViewType, class DistancesViewType,
class IntraTeamSentinelView>
struct TestFunctorA {
SourceViewType m_sourceView;
DestViewType m_destView;
DistancesViewType m_distancesView;
IntraTeamSentinelView m_intraTeamSentinelView;
int m_apiPick;

TestFunctorA(const SourceViewType sourceView, const DestViewType destView,
const DistancesViewType distancesView, int apiPick)
const DistancesViewType distancesView,
const IntraTeamSentinelView intraTeamSentinelView, int apiPick)
: m_sourceView(sourceView),
m_destView(destView),
m_distancesView(distancesView),
m_intraTeamSentinelView(intraTeamSentinelView),
m_apiPick(apiPick) {}

template <class MemberType>
Expand All @@ -42,22 +46,31 @@ struct TestFunctorA {
auto myRowViewFrom =
Kokkos::subview(m_sourceView, myRowIndex, Kokkos::ALL());
auto myRowViewDest = Kokkos::subview(m_destView, myRowIndex, Kokkos::ALL());
ptrdiff_t resultDist = 0;

if (m_apiPick == 0) {
auto it = KE::copy(member, KE::begin(myRowViewFrom),
auto it = KE::copy(member, KE::begin(myRowViewFrom),
KE::end(myRowViewFrom), KE::begin(myRowViewDest));

resultDist = KE::distance(KE::begin(myRowViewDest), it);
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_distancesView(myRowIndex) =
KE::distance(KE::begin(myRowViewDest), it);
m_distancesView(myRowIndex) = resultDist;
});
} else if (m_apiPick == 1) {
auto it = KE::copy(member, myRowViewFrom, myRowViewDest);
auto it = KE::copy(member, myRowViewFrom, myRowViewDest);
resultDist = KE::distance(KE::begin(myRowViewDest), it);
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_distancesView(myRowIndex) =
KE::distance(KE::begin(myRowViewDest), it);
m_distancesView(myRowIndex) = resultDist;
});
}

// store result of checking if all members have their local
// values matching the one stored in m_distancesView
member.team_barrier();
const bool intraTeamCheck = team_members_have_matching_result(
member, resultDist, m_distancesView(myRowIndex));
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_intraTeamSentinelView(myRowIndex) = intraTeamCheck;
});
}
};

Expand Down Expand Up @@ -95,17 +108,22 @@ void test_A(std::size_t numTeams, std::size_t numCols, int apiId) {
// beginning of the interval that team operates on and then we check
// that these distances match the expectation
Kokkos::View<std::size_t*> distancesView("distancesView", numTeams);
// sentinel to check if all members of the team compute the same result
Kokkos::View<bool*> intraTeamSentinelView("intraTeamSameResult", numTeams);

// use CTAD for functor
TestFunctorA fnc(sourceView, destView, distancesView, apiId);
TestFunctorA fnc(sourceView, destView, distancesView, intraTeamSentinelView,
apiId);
Kokkos::parallel_for(policy, fnc);

// -----------------------------------------------
// check
// -----------------------------------------------
auto distancesView_h = create_host_space_copy(distancesView);
auto destViewAfterOp_h = create_host_space_copy(destView);
auto distancesView_h = create_host_space_copy(distancesView);
auto intraTeamSentinelView_h = create_host_space_copy(intraTeamSentinelView);
auto destViewAfterOp_h = create_host_space_copy(destView);
for (std::size_t i = 0; i < destViewBeforeOp_h.extent(0); ++i) {
ASSERT_TRUE(intraTeamSentinelView_h(i));
for (std::size_t j = 0; j < destViewBeforeOp_h.extent(1); ++j) {
ASSERT_EQ(destViewBeforeOp_h(i, j), ValueType(0));
EXPECT_TRUE(destViewAfterOp_h(i, j) != destViewBeforeOp_h(i, j));
Expand Down
41 changes: 30 additions & 11 deletions algorithms/unit_tests/TestStdAlgorithmsTeamCopyBackward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@ namespace TeamCopybackward {

namespace KE = Kokkos::Experimental;

template <class SourceViewType, class DestViewType, class DistancesViewType>
template <class SourceViewType, class DestViewType, class DistancesViewType,
class IntraTeamSentinelView>
struct TestFunctorA {
SourceViewType m_sourceView;
DestViewType m_destView;
DistancesViewType m_distancesView;
IntraTeamSentinelView m_intraTeamSentinelView;
int m_apiPick;

TestFunctorA(const SourceViewType sourceView, const DestViewType destView,
const DistancesViewType distancesView, int apiPick)
const DistancesViewType distancesView,
const IntraTeamSentinelView intraTeamSentinelView, int apiPick)
: m_sourceView(sourceView),
m_destView(destView),
m_distancesView(distancesView),
m_intraTeamSentinelView(intraTeamSentinelView),
m_apiPick(apiPick) {}

template <class MemberType>
Expand All @@ -42,23 +46,32 @@ struct TestFunctorA {
auto myRowViewFrom =
Kokkos::subview(m_sourceView, myRowIndex, Kokkos::ALL());
auto myRowViewDest = Kokkos::subview(m_destView, myRowIndex, Kokkos::ALL());
ptrdiff_t resultDist = 0;

if (m_apiPick == 0) {
auto it =
KE::copy_backward(member, KE::cbegin(myRowViewFrom),
KE::cend(myRowViewFrom), KE::end(myRowViewDest));

resultDist = KE::distance(KE::begin(myRowViewDest), it);
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_distancesView(myRowIndex) =
KE::distance(KE::begin(myRowViewDest), it);
m_distancesView(myRowIndex) = resultDist;
});
} else if (m_apiPick == 1) {
auto it = KE::copy_backward(member, myRowViewFrom, myRowViewDest);
auto it = KE::copy_backward(member, myRowViewFrom, myRowViewDest);
resultDist = KE::distance(KE::begin(myRowViewDest), it);
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_distancesView(myRowIndex) =
KE::distance(KE::begin(myRowViewDest), it);
m_distancesView(myRowIndex) = resultDist;
});
}

// store result of checking if all members have their local
// values matching the one stored in m_distancesView
member.team_barrier();
const bool intraTeamCheck = team_members_have_matching_result(
member, resultDist, m_distancesView(myRowIndex));
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_intraTeamSentinelView(myRowIndex) = intraTeamCheck;
});
}
};

Expand Down Expand Up @@ -99,17 +112,23 @@ void test_A(std::size_t numTeams, std::size_t numCols, int apiId) {
// beginning of the interval that team operates on and then we check
// that these distances match the expectation
Kokkos::View<std::size_t*> distancesView("distancesView", numTeams);
// sentinel to check if all members of the team compute the same result
Kokkos::View<bool*> intraTeamSentinelView("intraTeamSameResult", numTeams);

// use CTAD for functor
TestFunctorA fnc(sourceView, destView, distancesView, apiId);
TestFunctorA fnc(sourceView, destView, distancesView, intraTeamSentinelView,
apiId);
Kokkos::parallel_for(policy, fnc);

// -----------------------------------------------
// check
// -----------------------------------------------
auto distancesView_h = create_host_space_copy(distancesView);
auto destViewAfterOp_h = create_host_space_copy(destView);
auto distancesView_h = create_host_space_copy(distancesView);
auto intraTeamSentinelView_h = create_host_space_copy(intraTeamSentinelView);
auto destViewAfterOp_h = create_host_space_copy(destView);
for (std::size_t i = 0; i < destViewAfterOp_h.extent(0); ++i) {
ASSERT_TRUE(intraTeamSentinelView_h(i));

// first extra num of columns should be unchanged
for (std::size_t j = 0; j < extra; ++j) {
EXPECT_TRUE(destViewAfterOp_h(i, j) == destViewBeforeOp_h(i, j));
Expand Down
39 changes: 27 additions & 12 deletions algorithms/unit_tests/TestStdAlgorithmsTeamCopyIf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,23 @@ struct GreaterThanValueFunctor {
};

template <class SourceViewType, class DestViewType, class DistancesViewType,
class ValueType>
class IntraTeamSentinelView, class ValueType>
struct TestFunctorA {
SourceViewType m_sourceView;
DestViewType m_destView;
DistancesViewType m_distancesView;
IntraTeamSentinelView m_intraTeamSentinelView;
ValueType m_threshold;
int m_apiPick;

TestFunctorA(const SourceViewType sourceView, const DestViewType destView,
const DistancesViewType distancesView, ValueType threshold,
int apiPick)
const DistancesViewType distancesView,
const IntraTeamSentinelView intraTeamSentinelView,
ValueType threshold, int apiPick)
: m_sourceView(sourceView),
m_destView(destView),
m_distancesView(distancesView),
m_intraTeamSentinelView(intraTeamSentinelView),
m_threshold(threshold),
m_apiPick(apiPick) {}

Expand All @@ -59,24 +62,32 @@ struct TestFunctorA {
auto myRowViewDest = Kokkos::subview(m_destView, myRowIndex, Kokkos::ALL());

GreaterThanValueFunctor predicate(m_threshold);
ptrdiff_t resultDist = 0;

if (m_apiPick == 0) {
auto it =
KE::copy_if(member, KE::begin(myRowViewFrom), KE::end(myRowViewFrom),
KE::begin(myRowViewDest), predicate);

resultDist = KE::distance(KE::begin(myRowViewDest), it);
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_distancesView(myRowIndex) =
KE::distance(KE::begin(myRowViewDest), it);
m_distancesView(myRowIndex) = resultDist;
});
} else if (m_apiPick == 1) {
auto it = KE::copy_if(member, myRowViewFrom, myRowViewDest, predicate);

auto it = KE::copy_if(member, myRowViewFrom, myRowViewDest, predicate);
resultDist = KE::distance(KE::begin(myRowViewDest), it);
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_distancesView(myRowIndex) =
KE::distance(KE::begin(myRowViewDest), it);
m_distancesView(myRowIndex) = resultDist;
});
}

// store result of checking if all members have their local
// values matching the one stored in m_distancesView
member.team_barrier();
const bool intraTeamCheck = team_members_have_matching_result(
member, resultDist, m_distancesView(myRowIndex));
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_intraTeamSentinelView(myRowIndex) = intraTeamCheck;
});
}
};

Expand Down Expand Up @@ -113,15 +124,19 @@ void test_A(std::size_t numTeams, std::size_t numCols, int apiId) {
// beginning of the interval that team operates on and then we check
// that these distances match the std result
Kokkos::View<std::size_t*> distancesView("distancesView", numTeams);
// sentinel to check if all members of the team compute the same result
Kokkos::View<bool*> intraTeamSentinelView("intraTeamSameResult", numTeams);

// use CTAD for functor
TestFunctorA fnc(sourceView, destView, distancesView, threshold, apiId);
TestFunctorA fnc(sourceView, destView, distancesView, intraTeamSentinelView,
threshold, apiId);
Kokkos::parallel_for(policy, fnc);

// -----------------------------------------------
// run cpp-std kernel and check
// -----------------------------------------------
auto distancesView_h = create_host_space_copy(distancesView);
auto distancesView_h = create_host_space_copy(distancesView);
auto intraTeamSentinelView_h = create_host_space_copy(intraTeamSentinelView);
Kokkos::View<ValueType**, Kokkos::HostSpace> stdDestView("stdDestView",
numTeams, numCols);
GreaterThanValueFunctor predicate(threshold);
Expand Down
43 changes: 30 additions & 13 deletions algorithms/unit_tests/TestStdAlgorithmsTeamCopy_n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,24 @@ namespace TeamCopy_n {

namespace KE = Kokkos::Experimental;

template <class SourceViewType, class DestViewType, class DistancesViewType>
template <class SourceViewType, class DestViewType, class DistancesViewType,
class IntraTeamSentinelView>
struct TestFunctorA {
SourceViewType m_sourceView;
DestViewType m_destView;
DistancesViewType m_distancesView;
IntraTeamSentinelView m_intraTeamSentinelView;
std::size_t m_copyCount;
int m_apiPick;

TestFunctorA(const SourceViewType fromView, const DestViewType destView,
const DistancesViewType distancesView, std::size_t copyCount,
int apiPick)
const DistancesViewType distancesView,
const IntraTeamSentinelView intraTeamSentinelView,
std::size_t copyCount, int apiPick)
: m_sourceView(fromView),
m_destView(destView),
m_distancesView(distancesView),
m_intraTeamSentinelView(intraTeamSentinelView),
m_copyCount(copyCount),
m_apiPick(apiPick) {}

Expand All @@ -45,23 +49,31 @@ struct TestFunctorA {
auto myRowViewFrom =
Kokkos::subview(m_sourceView, myRowIndex, Kokkos::ALL());
auto myRowViewDest = Kokkos::subview(m_destView, myRowIndex, Kokkos::ALL());
ptrdiff_t resultDist = 0;

if (m_apiPick == 0) {
auto it = KE::copy_n(member, KE::begin(myRowViewFrom), m_copyCount,
auto it = KE::copy_n(member, KE::begin(myRowViewFrom), m_copyCount,
KE::begin(myRowViewDest));

resultDist = KE::distance(KE::begin(myRowViewDest), it);
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_distancesView(myRowIndex) =
KE::distance(KE::begin(myRowViewDest), it);
m_distancesView(myRowIndex) = resultDist;
});
} else if (m_apiPick == 1) {
auto it = KE::copy_n(member, myRowViewFrom, m_copyCount, myRowViewDest);

resultDist = KE::distance(KE::begin(myRowViewDest), it);
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_distancesView(myRowIndex) =
KE::distance(KE::begin(myRowViewDest), it);
m_distancesView(myRowIndex) = resultDist;
});
}

// store result of checking if all members have their local
// values matching the one stored in m_distancesView
member.team_barrier();
const bool intraTeamCheck = team_members_have_matching_result(
member, resultDist, m_distancesView(myRowIndex));
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_intraTeamSentinelView(myRowIndex) = intraTeamCheck;
});
}
};

Expand Down Expand Up @@ -100,17 +112,22 @@ void test_A(std::size_t numTeams, std::size_t numCols, std::size_t copyCount,
// beginning of the interval that team operates on and then we check
// that these distances match the expectation
Kokkos::View<std::size_t*> distancesView("distancesView", numTeams);
// sentinel to check if all members of the team compute the same result
Kokkos::View<bool*> intraTeamSentinelView("intraTeamSameResult", numTeams);

// use CTAD for functor
TestFunctorA fnc(sourceView, destView, distancesView, copyCount, apiId);
TestFunctorA fnc(sourceView, destView, distancesView, intraTeamSentinelView,
copyCount, apiId);
Kokkos::parallel_for(policy, fnc);

// -----------------------------------------------
// check
// -----------------------------------------------
auto distancesView_h = create_host_space_copy(distancesView);
auto destViewAfterOp_h = create_host_space_copy(destView);
auto distancesView_h = create_host_space_copy(distancesView);
auto intraTeamSentinelView_h = create_host_space_copy(intraTeamSentinelView);
auto destViewAfterOp_h = create_host_space_copy(destView);
for (std::size_t i = 0; i < destViewBeforeOp_h.extent(0); ++i) {
ASSERT_TRUE(intraTeamSentinelView_h(i));
for (std::size_t j = 0; j < copyCount; ++j) {
ASSERT_EQ(destViewAfterOp_h(i, j), cloneOfSourceViewBeforeOp_h(i, j));
}
Expand Down

0 comments on commit 3bbbe2b

Please sign in to comment.