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 3/7) (kokkos#6425)

* improve tests to check for intrateam results

* fix for openmptarget
  • Loading branch information
fnrizzi committed Sep 20, 2023
1 parent e13f67c commit 773e346
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 58 deletions.
42 changes: 29 additions & 13 deletions algorithms/unit_tests/TestStdAlgorithmsTeamEqual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ struct EqualFunctor {
};

template <class DataViewType, class CompViewType, class ResultsViewType,
class BinaryPredType>
class IntraTeamSentinelView, class BinaryPredType>
struct TestFunctorA {
DataViewType m_dataView;
CompViewType m_compView;
ResultsViewType m_resultsView;
IntraTeamSentinelView m_intraTeamSentinelView;
int m_apiPick;
BinaryPredType m_binaryPred;

TestFunctorA(const DataViewType dataView, const CompViewType compView,
const ResultsViewType resultsView, int apiPick,
const ResultsViewType resultsView,
const IntraTeamSentinelView intraTeamSentinelView, int apiPick,
BinaryPredType binaryPred)
: m_dataView(dataView),
m_compView(compView),
m_resultsView(resultsView),
m_intraTeamSentinelView(intraTeamSentinelView),
m_apiPick(apiPick),
m_binaryPred(binaryPred) {}

Expand All @@ -60,53 +63,61 @@ struct TestFunctorA {
const auto compBegin = KE::cbegin(rowComp);
const auto compEnd = KE::cend(rowComp);

bool result = false;
switch (m_apiPick) {
case 0: {
const bool result = KE::equal(member, dataBegin, dataEnd, compBegin);
result = KE::equal(member, dataBegin, dataEnd, compBegin);
Kokkos::single(Kokkos::PerTeam(member),
[=, *this]() { m_resultsView(rowIndex) = result; });
break;
}

case 1: {
const bool result =
KE::equal(member, dataBegin, dataEnd, compBegin, m_binaryPred);
result = KE::equal(member, dataBegin, dataEnd, compBegin, m_binaryPred);
Kokkos::single(Kokkos::PerTeam(member),
[=, *this]() { m_resultsView(rowIndex) = result; });
break;
}

case 2: {
const bool result = KE::equal(member, rowData, rowComp);
result = KE::equal(member, rowData, rowComp);
Kokkos::single(Kokkos::PerTeam(member),
[=, *this]() { m_resultsView(rowIndex) = result; });
break;
}

case 3: {
const bool result = KE::equal(member, rowData, rowComp, m_binaryPred);
result = KE::equal(member, rowData, rowComp, m_binaryPred);
Kokkos::single(Kokkos::PerTeam(member),
[=, *this]() { m_resultsView(rowIndex) = result; });

break;
}

case 4: {
const bool result =
KE::equal(member, dataBegin, dataEnd, compBegin, compEnd);
result = KE::equal(member, dataBegin, dataEnd, compBegin, compEnd);
Kokkos::single(Kokkos::PerTeam(member),
[=, *this]() { m_resultsView(rowIndex) = result; });
break;
}

case 5: {
const bool result = KE::equal(member, dataBegin, dataEnd, compBegin,
compEnd, m_binaryPred);
result = KE::equal(member, dataBegin, dataEnd, compBegin, compEnd,
m_binaryPred);
Kokkos::single(Kokkos::PerTeam(member),
[=, *this]() { m_resultsView(rowIndex) = 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_resultsView(rowIndex));
Kokkos::single(Kokkos::PerTeam(member), [=, *this]() {
m_intraTeamSentinelView(rowIndex) = intraTeamCheck;
});
}
};

Expand Down Expand Up @@ -155,17 +166,21 @@ void test_A(const bool viewsAreEqual, std::size_t numTeams, std::size_t numCols,

// create the view to store results of equal()
Kokkos::View<bool*> resultsView("resultsView", numTeams);
// sentinel to check if all members of the team compute the same result
Kokkos::View<bool*> intraTeamSentinelView("intraTeamSameResult", numTeams);

EqualFunctor<ValueType> binaryPred{};

// use CTAD for functor
TestFunctorA fnc(dataView, compView, resultsView, apiId, binaryPred);
TestFunctorA fnc(dataView, compView, resultsView, intraTeamSentinelView,
apiId, binaryPred);
Kokkos::parallel_for(policy, fnc);

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

for (std::size_t i = 0; i < dataView.extent(0); ++i) {
auto rowData = Kokkos::subview(dataViewBeforeOp_h, i, Kokkos::ALL());
Expand All @@ -176,6 +191,7 @@ void test_A(const bool viewsAreEqual, std::size_t numTeams, std::size_t numCols,
const auto compBegin = KE::cbegin(rowComp);
const auto compEnd = KE::cend(rowComp);

ASSERT_TRUE(intraTeamSentinelView_h(i));
switch (apiId) {
case 0:
case 2: {
Expand Down
52 changes: 35 additions & 17 deletions algorithms/unit_tests/TestStdAlgorithmsTeamFindEnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,25 @@ struct EqualFunctor {
};

template <class DataViewType, class SearchedSequencesViewType,
class DistancesViewType, class BinaryPredType>
class DistancesViewType, class IntraTeamSentinelView,
class BinaryPredType>
struct TestFunctorA {
DataViewType m_dataView;
SearchedSequencesViewType m_searchedSequencesView;
DistancesViewType m_distancesView;
IntraTeamSentinelView m_intraTeamSentinelView;
BinaryPredType m_binaryPred;
int m_apiPick;

TestFunctorA(const DataViewType dataView,
const SearchedSequencesViewType searchedSequencesView,
const DistancesViewType distancesView, BinaryPredType binaryPred,
int apiPick)
const DistancesViewType distancesView,
const IntraTeamSentinelView intraTeamSentinelView,
BinaryPredType binaryPred, int apiPick)
: m_dataView(dataView),
m_searchedSequencesView(searchedSequencesView),
m_distancesView(distancesView),
m_intraTeamSentinelView(intraTeamSentinelView),
m_binaryPred(binaryPred),
m_apiPick(apiPick) {}

Expand All @@ -55,54 +59,64 @@ struct TestFunctorA {
auto myRowViewFrom = Kokkos::subview(m_dataView, myRowIndex, Kokkos::ALL());
auto myRowSearchedSeqView =
Kokkos::subview(m_searchedSequencesView, myRowIndex, Kokkos::ALL());
ptrdiff_t resultDist = 0;

switch (m_apiPick) {
case 0: {
auto it = KE::find_end(
member, KE::cbegin(myRowViewFrom), KE::cend(myRowViewFrom),
KE::cbegin(myRowSearchedSeqView), KE::cend(myRowSearchedSeqView));
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_end(member, myRowViewFrom, myRowSearchedSeqView);
auto it = KE::find_end(member, myRowViewFrom, myRowSearchedSeqView);
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;
}

case 2: {
auto it = KE::find_end(member, KE::cbegin(myRowViewFrom),
auto it = KE::find_end(member, KE::cbegin(myRowViewFrom),
KE::cend(myRowViewFrom),
KE::cbegin(myRowSearchedSeqView),
KE::cend(myRowSearchedSeqView), m_binaryPred);
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 3: {
auto it = KE::find_end(member, myRowViewFrom, myRowSearchedSeqView,
auto it = KE::find_end(member, myRowViewFrom, myRowSearchedSeqView,
m_binaryPred);
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 @@ -165,18 +179,21 @@ void test_A(const bool sequencesExist, 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);

EqualFunctor<ValueType> binaryPred;

// use CTAD for functor
TestFunctorA fnc(dataView, searchedSequencesView, distancesView, binaryPred,
apiId);
TestFunctorA fnc(dataView, searchedSequencesView, distancesView,
intraTeamSentinelView, binaryPred, 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 @@ -189,6 +206,7 @@ void test_A(const bool sequencesExist, std::size_t numTeams,
std::size_t stdDistance = std::numeric_limits<std::size_t>::max();
const std::size_t beginEndDistance = KE::distance(rowFromBegin, rowFromEnd);

ASSERT_TRUE(intraTeamSentinelView_h(i));
switch (apiId) {
case 0:
case 1: {
Expand Down
46 changes: 32 additions & 14 deletions algorithms/unit_tests/TestStdAlgorithmsTeamFindFirstOf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,25 @@ struct EqualFunctor {
};

template <class DataViewType, class SearchedSequencesViewType,
class DistancesViewType, class BinaryPredType>
class DistancesViewType, class IntraTeamSentinelView,
class BinaryPredType>
struct TestFunctorA {
DataViewType m_dataView;
SearchedSequencesViewType m_searchedSequencesView;
DistancesViewType m_distancesView;
IntraTeamSentinelView m_intraTeamSentinelView;
BinaryPredType m_binaryPred;
int m_apiPick;

TestFunctorA(const DataViewType dataView,
const SearchedSequencesViewType searchedSequencesView,
const DistancesViewType distancesView, BinaryPredType binaryPred,
int apiPick)
const DistancesViewType distancesView,
const IntraTeamSentinelView intraTeamSentinelView,
BinaryPredType binaryPred, int apiPick)
: m_dataView(dataView),
m_searchedSequencesView(searchedSequencesView),
m_distancesView(distancesView),
m_intraTeamSentinelView(intraTeamSentinelView),
m_binaryPred(binaryPred),
m_apiPick(apiPick) {}

Expand All @@ -55,15 +59,16 @@ struct TestFunctorA {
auto myRowViewFrom = Kokkos::subview(m_dataView, myRowIndex, Kokkos::ALL());
auto myRowSearchedSeqView =
Kokkos::subview(m_searchedSequencesView, myRowIndex, Kokkos::ALL());
ptrdiff_t resultDist = 0;

switch (m_apiPick) {
case 0: {
auto it = KE::find_first_of(
member, KE::cbegin(myRowViewFrom), KE::cend(myRowViewFrom),
KE::cbegin(myRowSearchedSeqView), KE::cend(myRowSearchedSeqView));
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;
Expand All @@ -72,9 +77,9 @@ struct TestFunctorA {
case 1: {
auto it =
KE::find_first_of(member, myRowViewFrom, myRowSearchedSeqView);
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;
Expand All @@ -85,9 +90,9 @@ struct TestFunctorA {
member, KE::cbegin(myRowViewFrom), KE::cend(myRowViewFrom),
KE::cbegin(myRowSearchedSeqView), KE::cend(myRowSearchedSeqView),
m_binaryPred);
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;
Expand All @@ -96,14 +101,23 @@ struct TestFunctorA {
case 3: {
auto it = KE::find_first_of(member, myRowViewFrom, myRowSearchedSeqView,
m_binaryPred);
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 @@ -167,18 +181,21 @@ void test_A(const bool sequencesExist, 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);

EqualFunctor<ValueType> binaryPred;

// use CTAD for functor
TestFunctorA fnc(dataView, searchedSequencesView, distancesView, binaryPred,
apiId);
TestFunctorA fnc(dataView, searchedSequencesView, distancesView,
intraTeamSentinelView, binaryPred, 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 @@ -192,6 +209,7 @@ void test_A(const bool sequencesExist, std::size_t numTeams,

const std::size_t beginEndDistance = KE::distance(rowFromBegin, rowFromEnd);

ASSERT_TRUE(intraTeamSentinelView_h(i));
switch (apiId) {
case 0:
case 1: {
Expand Down

0 comments on commit 773e346

Please sign in to comment.