Skip to content

Commit

Permalink
improve tests (kokkos#6432)
Browse files Browse the repository at this point in the history
  • Loading branch information
fnrizzi committed Sep 20, 2023
1 parent 773e346 commit 1f0183b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 33 deletions.
37 changes: 28 additions & 9 deletions algorithms/unit_tests/TestStdAlgorithmsTeamFill_n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ namespace TeamFill_n {

namespace KE = Kokkos::Experimental;

template <class ViewType, class DistancesViewType>
template <class ViewType, class DistancesViewType, class IntraTeamSentinelView>
struct TestFunctorA {
ViewType m_view;
DistancesViewType m_distancesView;
IntraTeamSentinelView m_intraTeamSentinelView;
std::size_t m_fillCount;
int m_apiPick;

TestFunctorA(const ViewType view, const DistancesViewType distancesView,
const IntraTeamSentinelView intraTeamSentinelView,
std::size_t fillCount, int apiPick)
: m_view(view),
m_distancesView(distancesView),
m_intraTeamSentinelView(intraTeamSentinelView),
m_fillCount(fillCount),
m_apiPick(apiPick) {}

Expand All @@ -41,21 +44,31 @@ struct TestFunctorA {
const auto leagueRank = member.league_rank();
const auto myRowIndex = leagueRank;
auto myRowView = Kokkos::subview(m_view, myRowIndex, Kokkos::ALL());
ptrdiff_t resultDist = 0;

if (m_apiPick == 0) {
auto it =
KE::fill_n(member, KE::begin(myRowView), m_fillCount, leagueRank);

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

auto it = KE::fill_n(member, myRowView, m_fillCount, leagueRank);
resultDist = KE::distance(KE::begin(myRowView), it);
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_distancesView(myRowIndex) = KE::distance(KE::begin(myRowView), 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 @@ -91,17 +104,23 @@ void test_A(std::size_t numTeams, std::size_t numCols, std::size_t fillCount,
// beginning of the interval that team operates on and then we check
// that these distances match the expected value
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(dataView, distancesView, fillCount, apiId);
TestFunctorA fnc(dataView, distancesView, intraTeamSentinelView, fillCount,
apiId);
Kokkos::parallel_for(policy, fnc);

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

// check that values match what we expect
for (std::size_t j = 0; j < fillCount; ++j) {
ASSERT_EQ(dataViewAfterOp_h(i, j), ValueType(i));
Expand Down
40 changes: 28 additions & 12 deletions algorithms/unit_tests/TestStdAlgorithmsTeamReplaceCopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,24 @@ struct UnifDist<int> {
};

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_targetValue;
ValueType m_newValue;
int m_apiPick;

TestFunctorA(const SourceViewType sourceView, const DestViewType destView,
const DistancesViewType distancesView, ValueType targetVal,
ValueType newVal, int apiPick)
const DistancesViewType distancesView,
const IntraTeamSentinelView intraTeamSentinelView,
ValueType targetVal, ValueType newVal, int apiPick)
: m_sourceView(sourceView),
m_destView(destView),
m_distancesView(distancesView),
m_intraTeamSentinelView(intraTeamSentinelView),
m_targetValue(targetVal),
m_newValue(newVal),
m_apiPick(apiPick) {}
Expand All @@ -63,24 +66,33 @@ 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::replace_copy(
member, KE::begin(myRowViewFrom), KE::end(myRowViewFrom),
KE::begin(myRowViewDest), m_targetValue, m_newValue);

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::replace_copy(member, myRowViewFrom, myRowViewDest,
auto it = KE::replace_copy(member, myRowViewFrom, myRowViewDest,
m_targetValue, m_newValue);
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 @@ -141,16 +153,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, targetVal, newVal,
apiId);
TestFunctorA fnc(sourceView, destView, distancesView, intraTeamSentinelView,
targetVal, newVal, 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);
for (std::size_t i = 0; i < sourceView_dc_h.extent(0); ++i) {
Expand All @@ -160,6 +175,7 @@ void test_A(std::size_t numTeams, std::size_t numCols, int apiId) {
KE::begin(rowDest), targetVal, newVal);
const std::size_t stdDistance = KE::distance(KE::begin(rowDest), it);
ASSERT_EQ(stdDistance, distancesView_h(i));
ASSERT_TRUE(intraTeamSentinelView_h(i));
}

auto dataViewAfterOp_h = create_host_space_copy(destView);
Expand Down
40 changes: 28 additions & 12 deletions algorithms/unit_tests/TestStdAlgorithmsTeamReplaceCopyIf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,24 @@ 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;
ValueType m_newValue;
int m_apiPick;

TestFunctorA(const SourceViewType sourceView, const DestViewType destView,
const DistancesViewType distancesView, ValueType threshold,
ValueType newVal, int apiPick)
const DistancesViewType distancesView,
const IntraTeamSentinelView intraTeamSentinelView,
ValueType threshold, ValueType newVal, int apiPick)
: m_sourceView(sourceView),
m_destView(destView),
m_distancesView(distancesView),
m_intraTeamSentinelView(intraTeamSentinelView),
m_threshold(threshold),
m_newValue(newVal),
m_apiPick(apiPick) {}
Expand All @@ -59,26 +62,35 @@ struct TestFunctorA {
auto myRowViewFrom =
Kokkos::subview(m_sourceView, myRowIndex, Kokkos::ALL());
auto myRowViewDest = Kokkos::subview(m_destView, myRowIndex, Kokkos::ALL());
ptrdiff_t resultDist = 0;

GreaterThanValueFunctor predicate(m_threshold);

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

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::replace_copy_if(member, myRowViewFrom, myRowViewDest,
auto it = KE::replace_copy_if(member, myRowViewFrom, myRowViewDest,
predicate, m_newValue);
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 @@ -118,16 +130,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, newVal,
apiId);
TestFunctorA fnc(sourceView, destView, distancesView, intraTeamSentinelView,
threshold, newVal, 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 All @@ -139,6 +154,7 @@ void test_A(std::size_t numTeams, std::size_t numCols, int apiId) {
KE::begin(rowDest), predicate, newVal);
const std::size_t stdDistance = KE::distance(KE::begin(rowDest), it);
ASSERT_EQ(stdDistance, distancesView_h(i));
ASSERT_TRUE(intraTeamSentinelView_h(i));
}

auto dataViewAfterOp_h = create_host_space_copy(destView);
Expand Down

0 comments on commit 1f0183b

Please sign in to comment.