Skip to content

Commit

Permalink
team-level stdalgos: improve tests, check intra-team result matching …
Browse files Browse the repository at this point in the history
…(part 6/7) (kokkos#6436)

* improve tests

* fix failing CI

* add space
  • Loading branch information
fnrizzi committed Sep 19, 2023
1 parent 5ed274c commit e13f67c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 30 deletions.
40 changes: 29 additions & 11 deletions algorithms/unit_tests/TestStdAlgorithmsTeamReverseCopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@ namespace TeamReverseCopy {

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::reverse_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::reverse_copy(member, myRowViewFrom, myRowViewDest);
auto it = KE::reverse_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 @@ -93,23 +106,28 @@ 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 destViewAfterOp_h = create_host_space_copy(destView);
auto intraTeamSentinelView_h = create_host_space_copy(intraTeamSentinelView);
for (std::size_t i = 0; i < destViewAfterOp_h.extent(0); ++i) {
for (std::size_t j = 0; j < destViewAfterOp_h.extent(1); ++j) {
EXPECT_TRUE(destViewAfterOp_h(i, j) ==
cloneOfSourceViewBeforeOp_h(i, numCols - j - 1));
}
// each team should return an iterator past the last column
EXPECT_TRUE(distancesView_h(i) == numCols);
ASSERT_TRUE(intraTeamSentinelView_h(i));
}
}

Expand Down
34 changes: 26 additions & 8 deletions algorithms/unit_tests/TestStdAlgorithmsTeamRotate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,53 @@ namespace TeamRotate {

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_pivotShift;
int m_apiPick;

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

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

if (m_apiPick == 0) {
auto pivot = KE::begin(myRowView) + m_pivotShift;
auto it =
KE::rotate(member, KE::begin(myRowView), pivot, KE::end(myRowView));

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::rotate(member, myRowView, m_pivotShift);

auto it = KE::rotate(member, myRowView, m_pivotShift);
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 @@ -89,24 +102,29 @@ void test_A(std::size_t numTeams, std::size_t numCols, std::size_t pivotShift,
// 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(dataView, distancesView, pivotShift, apiId);
TestFunctorA fnc(dataView, distancesView, intraTeamSentinelView, pivotShift,
apiId);
Kokkos::parallel_for(policy, fnc);

// -----------------------------------------------
// run std algo and check
// -----------------------------------------------
// here I can use cloneOfDataViewBeforeOp_h to run std algo on
// since that contains a valid copy of the data
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 < cloneOfDataViewBeforeOp_h.extent(0); ++i) {
auto myRow = Kokkos::subview(cloneOfDataViewBeforeOp_h, i, Kokkos::ALL());
auto pivot = KE::begin(myRow) + pivotShift;

auto it = std::rotate(KE::begin(myRow), pivot, KE::end(myRow));
const std::size_t stdDistance = KE::distance(KE::begin(myRow), it);
ASSERT_EQ(stdDistance, distancesView_h(i));
ASSERT_TRUE(intraTeamSentinelView_h(i));
}

auto dataViewAfterOp_h = create_host_space_copy(dataView);
Expand Down
40 changes: 29 additions & 11 deletions algorithms/unit_tests/TestStdAlgorithmsTeamRotateCopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ namespace TeamRotateCopy {

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_pivotShift;
int m_apiPick;

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

Expand All @@ -46,26 +50,34 @@ 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 pivot = KE::cbegin(myRowViewFrom) + m_pivotShift;
auto it =
KE::rotate_copy(member, KE::cbegin(myRowViewFrom), pivot,
KE::cend(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::rotate_copy(member, myRowViewFrom, m_pivotShift, 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 @@ -101,17 +113,22 @@ void test_A(std::size_t numTeams, std::size_t numCols, std::size_t pivotShift,
// 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, pivotShift, apiId);
TestFunctorA fnc(sourceView, destView, distancesView, intraTeamSentinelView,
pivotShift, apiId);
Kokkos::parallel_for(policy, fnc);

// -----------------------------------------------
// run std algo and check
// -----------------------------------------------
Kokkos::View<ValueType**, Kokkos::HostSpace> stdDestView("stdDestView",
numTeams, numCols);
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 < cloneOfSourceViewBeforeOp_h.extent(0); ++i) {
auto myRowFrom =
Kokkos::subview(cloneOfSourceViewBeforeOp_h, i, Kokkos::ALL());
Expand All @@ -122,6 +139,7 @@ void test_A(std::size_t numTeams, std::size_t numCols, std::size_t pivotShift,
KE::cend(myRowFrom), KE::begin(myRowDest));
const std::size_t stdDistance = KE::distance(KE::begin(myRowDest), it);
ASSERT_EQ(stdDistance, distancesView_h(i));
ASSERT_TRUE(intraTeamSentinelView_h(i));
}

auto destViewAfterOp_h = create_host_space_copy(destView);
Expand Down

0 comments on commit e13f67c

Please sign in to comment.