Skip to content

Commit

Permalink
improve tests to check intra-team result (kokkos#6431)
Browse files Browse the repository at this point in the history
  • Loading branch information
fnrizzi committed Sep 21, 2023
1 parent 9b37781 commit e542e98
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 67 deletions.
33 changes: 26 additions & 7 deletions algorithms/unit_tests/TestStdAlgorithmsTeamAllOf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,22 @@ struct GreaterThanValueFunctor {
bool operator()(ValueType val) const { return (val > m_val); }
};

template <class DataViewType, class AlllOfResultsViewType, class UnaryPredType>
template <class DataViewType, class AlllOfResultsViewType,
class IntraTeamSentinelView, class UnaryPredType>
struct TestFunctorA {
DataViewType m_dataView;
AlllOfResultsViewType m_allOfResultsView;
IntraTeamSentinelView m_intraTeamSentinelView;
int m_apiPick;
UnaryPredType m_unaryPred;

TestFunctorA(const DataViewType dataView,
const AlllOfResultsViewType allOfResultsView, int apiPick,
const AlllOfResultsViewType allOfResultsView,
const IntraTeamSentinelView intraTeamSentinelView, int apiPick,
UnaryPredType unaryPred)
: m_dataView(dataView),
m_allOfResultsView(allOfResultsView),
m_intraTeamSentinelView(intraTeamSentinelView),
m_apiPick(apiPick),
m_unaryPred(unaryPred) {}

Expand All @@ -53,25 +57,35 @@ struct TestFunctorA {
const auto myRowIndex = member.league_rank();

auto myRowViewFrom = Kokkos::subview(m_dataView, myRowIndex, Kokkos::ALL());
bool result = false;

switch (m_apiPick) {
case 0: {
const bool result = KE::all_of(member, KE::cbegin(myRowViewFrom),
KE::cend(myRowViewFrom), m_unaryPred);
result = KE::all_of(member, KE::cbegin(myRowViewFrom),
KE::cend(myRowViewFrom), m_unaryPred);
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_allOfResultsView(myRowIndex) = result;
});
break;
}

case 1: {
const bool result = KE::all_of(member, myRowViewFrom, m_unaryPred);
result = KE::all_of(member, myRowViewFrom, m_unaryPred);
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_allOfResultsView(myRowIndex) = result;
});
break;
}
}

// 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, result, m_allOfResultsView(myRowIndex));
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_intraTeamSentinelView(myRowIndex) = intraTeamCheck;
});
}
};

Expand Down Expand Up @@ -104,23 +118,28 @@ void test_A(std::size_t numTeams, std::size_t numCols, int apiId) {
// to verify that things work, each team stores the result of its all_of call,
// and then we check that these match what we expect
Kokkos::View<bool*> allOfResultsView("allOfResultsView", numTeams);
// sentinel to check if all members of the team compute the same result
Kokkos::View<bool*> intraTeamSentinelView("intraTeamSameResult", numTeams);

GreaterThanValueFunctor unaryPred{lowerBound - 1};

// use CTAD for functor
TestFunctorA fnc(dataView, allOfResultsView, apiId, unaryPred);
TestFunctorA fnc(dataView, allOfResultsView, intraTeamSentinelView, apiId,
unaryPred);
Kokkos::parallel_for(policy, fnc);

// -----------------------------------------------
// run cpp-std kernel and check
// -----------------------------------------------
auto allOfResultsView_h = create_host_space_copy(allOfResultsView);
auto allOfResultsView_h = create_host_space_copy(allOfResultsView);
auto intraTeamSentinelView_h = create_host_space_copy(intraTeamSentinelView);

for (std::size_t i = 0; i < dataView.extent(0); ++i) {
auto rowFrom = Kokkos::subview(dataViewBeforeOp_h, i, Kokkos::ALL());
const bool result =
std::all_of(KE::cbegin(rowFrom), KE::cend(rowFrom), unaryPred);
ASSERT_EQ(result, allOfResultsView_h(i));
ASSERT_TRUE(intraTeamSentinelView_h(i));
}
}

Expand Down
33 changes: 26 additions & 7 deletions algorithms/unit_tests/TestStdAlgorithmsTeamAnyOf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,22 @@ struct GreaterThanValueFunctor {
bool operator()(ValueType val) const { return (val > m_val); }
};

template <class DataViewType, class AnyOfResultsViewType, class UnaryPredType>
template <class DataViewType, class AnyOfResultsViewType,
class IntraTeamSentinelView, class UnaryPredType>
struct TestFunctorA {
DataViewType m_dataView;
AnyOfResultsViewType m_anyOfResultsView;
IntraTeamSentinelView m_intraTeamSentinelView;
int m_apiPick;
UnaryPredType m_unaryPred;

TestFunctorA(const DataViewType dataView,
const AnyOfResultsViewType anyOfResultsView, int apiPick,
const AnyOfResultsViewType anyOfResultsView,
const IntraTeamSentinelView intraTeamSentinelView, int apiPick,
UnaryPredType unaryPred)
: m_dataView(dataView),
m_anyOfResultsView(anyOfResultsView),
m_intraTeamSentinelView(intraTeamSentinelView),
m_apiPick(apiPick),
m_unaryPred(unaryPred) {}

Expand All @@ -53,25 +57,35 @@ struct TestFunctorA {
const auto myRowIndex = member.league_rank();

auto myRowViewFrom = Kokkos::subview(m_dataView, myRowIndex, Kokkos::ALL());
bool result = false;

switch (m_apiPick) {
case 0: {
const bool result = KE::any_of(member, KE::cbegin(myRowViewFrom),
KE::cend(myRowViewFrom), m_unaryPred);
result = KE::any_of(member, KE::cbegin(myRowViewFrom),
KE::cend(myRowViewFrom), m_unaryPred);
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_anyOfResultsView(myRowIndex) = result;
});
break;
}

case 1: {
const bool result = KE::any_of(member, myRowViewFrom, m_unaryPred);
result = KE::any_of(member, myRowViewFrom, m_unaryPred);
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_anyOfResultsView(myRowIndex) = result;
});
break;
}
}

// 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, result, m_anyOfResultsView(myRowIndex));
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_intraTeamSentinelView(myRowIndex) = intraTeamCheck;
});
}
};

Expand Down Expand Up @@ -104,23 +118,28 @@ void test_A(std::size_t numTeams, std::size_t numCols, int apiId) {
// to verify that things work, each team stores the result of its any_of call,
// and then we check that these match what we expect
Kokkos::View<bool*> anyOfResultsView("anyOfResultsView", numTeams);
// sentinel to check if all members of the team compute the same result
Kokkos::View<bool*> intraTeamSentinelView("intraTeamSameResult", numTeams);

GreaterThanValueFunctor unaryPred{lowerBound};

// use CTAD for functor
TestFunctorA fnc(dataView, anyOfResultsView, apiId, unaryPred);
TestFunctorA fnc(dataView, anyOfResultsView, intraTeamSentinelView, apiId,
unaryPred);
Kokkos::parallel_for(policy, fnc);

// -----------------------------------------------
// run cpp-std kernel and check
// -----------------------------------------------
auto anyOfResultsView_h = create_host_space_copy(anyOfResultsView);
auto anyOfResultsView_h = create_host_space_copy(anyOfResultsView);
auto intraTeamSentinelView_h = create_host_space_copy(intraTeamSentinelView);

for (std::size_t i = 0; i < dataView.extent(0); ++i) {
auto rowFrom = Kokkos::subview(dataViewBeforeOp_h, i, Kokkos::ALL());
const bool result =
std::any_of(KE::cbegin(rowFrom), KE::cend(rowFrom), unaryPred);
ASSERT_EQ(result, anyOfResultsView_h(i));
ASSERT_TRUE(intraTeamSentinelView_h(i));
}
}

Expand Down
40 changes: 28 additions & 12 deletions algorithms/unit_tests/TestStdAlgorithmsTeamFind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,62 @@ namespace TeamFind {
namespace KE = Kokkos::Experimental;

template <class DataViewType, class SearchedValuesViewType,
class DistancesViewType>
class DistancesViewType, class IntraTeamSentinelView>
struct TestFunctorA {
DataViewType m_dataView;
SearchedValuesViewType m_searchedValuesView;
DistancesViewType m_distancesView;
IntraTeamSentinelView m_intraTeamSentinelView;
int m_apiPick;

TestFunctorA(const DataViewType dataView,
const SearchedValuesViewType searchedValuesView,
const DistancesViewType distancesView, int apiPick)
const DistancesViewType distancesView,
const IntraTeamSentinelView intraTeamSentinelView, int apiPick)
: m_dataView(dataView),
m_searchedValuesView(searchedValuesView),
m_distancesView(distancesView),
m_intraTeamSentinelView(intraTeamSentinelView),
m_apiPick(apiPick) {}

template <class MemberType>
KOKKOS_INLINE_FUNCTION void operator()(const MemberType& member) const {
const auto myRowIndex = member.league_rank();
auto myRowViewFrom = Kokkos::subview(m_dataView, myRowIndex, Kokkos::ALL());
const auto searchedValue = m_searchedValuesView(myRowIndex);
ptrdiff_t resultDist = 0;

switch (m_apiPick) {
case 0: {
auto it = KE::find(member, KE::cbegin(myRowViewFrom),
auto it = KE::find(member, KE::cbegin(myRowViewFrom),
KE::cend(myRowViewFrom), searchedValue);

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

break;
}

case 1: {
auto it = KE::find(member, myRowViewFrom, searchedValue);

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

break;
}
}

// 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 @@ -103,6 +114,8 @@ void test_A(const bool searchedValuesExist, std::size_t numTeams,
// 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);

// If searchedValuesExist == true we want to ensure that each value we're
// looking for exists in dataView. To do that, for each numTeams, a random j
Expand Down Expand Up @@ -135,13 +148,15 @@ void test_A(const bool searchedValuesExist, std::size_t numTeams,
Kokkos::deep_copy(searchedValuesView, searchedValuesView_h);

// use CTAD for functor
TestFunctorA fnc(dataView, searchedValuesView, distancesView, apiId);
TestFunctorA fnc(dataView, searchedValuesView, distancesView,
intraTeamSentinelView, 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);

for (std::size_t i = 0; i < dataView.extent(0); ++i) {
auto rowFrom = Kokkos::subview(dataViewBeforeOp_h, i, Kokkos::ALL());
Expand All @@ -160,6 +175,7 @@ void test_A(const bool searchedValuesExist, std::size_t numTeams,
}

ASSERT_EQ(stdDistance, distancesView_h(i));
ASSERT_TRUE(intraTeamSentinelView_h(i));
}
}

Expand Down

0 comments on commit e542e98

Please sign in to comment.