Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanKKrueger committed Nov 20, 2024
1 parent f2a1d74 commit 5bf92e8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 70 deletions.
46 changes: 24 additions & 22 deletions ports-of-call/portability.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ void portableCopyToHost(T *const to, T const *const from, size_t const size_byte
}

template <typename Function>
void portableFor([[maybe_unused]] const char *name, int start, int stop, Function function) {
void portableFor([[maybe_unused]] const char *name, int start, int stop,
Function function) {
#ifdef PORTABILITY_STRATEGY_KOKKOS
using policy = Kokkos::RangePolicy<>;
Kokkos::parallel_for(name, policy(start, stop), function);
Expand All @@ -165,8 +166,8 @@ void portableFor([[maybe_unused]] const char *name, int start, int stop, Functio
}

template <typename Function>
void portableFor([[maybe_unused]] const char *name, int starty, int stopy, int startx, int stopx,
Function function) {
void portableFor([[maybe_unused]] const char *name, int starty, int stopy, int startx,
int stopx, Function function) {
#ifdef PORTABILITY_STRATEGY_KOKKOS
using Policy2D = Kokkos::MDRangePolicy<Kokkos::Rank<2>>;
Kokkos::parallel_for(name, Policy2D({starty, startx}, {stopy, stopx}), function);
Expand All @@ -180,8 +181,8 @@ void portableFor([[maybe_unused]] const char *name, int starty, int stopy, int s
}

template <typename Function>
void portableFor([[maybe_unused]] const char *name, int startz, int stopz, int starty, int stopy,
int startx, int stopx, Function function) {
void portableFor([[maybe_unused]] const char *name, int startz, int stopz, int starty,
int stopy, int startx, int stopx, Function function) {
#ifdef PORTABILITY_STRATEGY_KOKKOS
using Policy3D = Kokkos::MDRangePolicy<Kokkos::Rank<3>>;
Kokkos::parallel_for(name, Policy3D({startz, starty, startx}, {stopz, stopy, stopx}),
Expand All @@ -198,8 +199,9 @@ void portableFor([[maybe_unused]] const char *name, int startz, int stopz, int s
}

template <typename Function>
void portableFor([[maybe_unused]] const char *name, int starta, int stopa, int startz, int stopz,
int starty, int stopy, int startx, int stopx, Function function) {
void portableFor([[maybe_unused]] const char *name, int starta, int stopa, int startz,
int stopz, int starty, int stopy, int startx, int stopx,
Function function) {
#ifdef PORTABILITY_STRATEGY_KOKKOS
using Policy4D = Kokkos::MDRangePolicy<Kokkos::Rank<4>>;
Kokkos::parallel_for(
Expand All @@ -219,9 +221,9 @@ void portableFor([[maybe_unused]] const char *name, int starta, int stopa, int s
}

template <typename Function>
void portableFor([[maybe_unused]] const char *name, int startb, int stopb, int starta, int stopa,
int startz, int stopz, int starty, int stopy, int startx, int stopx,
Function function) {
void portableFor([[maybe_unused]] const char *name, int startb, int stopb, int starta,
int stopa, int startz, int stopz, int starty, int stopy, int startx,
int stopx, Function function) {
#ifdef PORTABILITY_STRATEGY_KOKKOS
using Policy5D = Kokkos::MDRangePolicy<Kokkos::Rank<5>>;
Kokkos::parallel_for(name,
Expand All @@ -244,8 +246,8 @@ void portableFor([[maybe_unused]] const char *name, int startb, int stopb, int s
}

template <typename Function, typename T>
void portableReduce([[maybe_unused]] const char *name, int start, int stop, Function function,
T &reduced) {
void portableReduce([[maybe_unused]] const char *name, int start, int stop,
Function function, T &reduced) {
#ifdef PORTABILITY_STRATEGY_KOKKOS
using Policy = Kokkos::RangePolicy<>;
Kokkos::parallel_reduce(name, Policy(start, stop), function, reduced);
Expand All @@ -257,8 +259,8 @@ void portableReduce([[maybe_unused]] const char *name, int start, int stop, Func
}

template <typename Function, typename T>
void portableReduce([[maybe_unused]] const char *name, int starty, int stopy, int startx, int stopx,
Function function, T &reduced) {
void portableReduce([[maybe_unused]] const char *name, int starty, int stopy, int startx,
int stopx, Function function, T &reduced) {
#ifdef PORTABILITY_STRATEGY_KOKKOS
using Policy2D = Kokkos::MDRangePolicy<Kokkos::Rank<2>>;
Kokkos::parallel_reduce(name, Policy2D({starty, startx}, {stopy, stopx}), function,
Expand All @@ -273,8 +275,8 @@ void portableReduce([[maybe_unused]] const char *name, int starty, int stopy, in
}

template <typename Function, typename T>
void portableReduce([[maybe_unused]] const char *name, int startz, int stopz, int starty, int stopy,
int startx, int stopx, Function function, T &reduced) {
void portableReduce([[maybe_unused]] const char *name, int startz, int stopz, int starty,
int stopy, int startx, int stopx, Function function, T &reduced) {
#ifdef PORTABILITY_STRATEGY_KOKKOS
using Policy3D = Kokkos::MDRangePolicy<Kokkos::Rank<3>>;
Kokkos::parallel_reduce(name, Policy3D({startz, starty, startx}, {stopz, stopy, stopx}),
Expand All @@ -291,9 +293,9 @@ void portableReduce([[maybe_unused]] const char *name, int startz, int stopz, in
}

template <typename Function, typename T>
void portableReduce([[maybe_unused]] const char *name, int starta, int stopa, int startz, int stopz,
int starty, int stopy, int startx, int stopx, Function function,
T &reduced) {
void portableReduce([[maybe_unused]] const char *name, int starta, int stopa, int startz,
int stopz, int starty, int stopy, int startx, int stopx,
Function function, T &reduced) {
#ifdef PORTABILITY_STRATEGY_KOKKOS
using Policy4D = Kokkos::MDRangePolicy<Kokkos::Rank<4>>;
Kokkos::parallel_reduce(
Expand All @@ -313,9 +315,9 @@ void portableReduce([[maybe_unused]] const char *name, int starta, int stopa, in
}

template <typename Function, typename T>
void portableReduce([[maybe_unused]] const char *name, int startb, int stopb, int starta, int stopa,
int startz, int stopz, int starty, int stopy, int startx, int stopx,
Function function, T &reduced) {
void portableReduce([[maybe_unused]] const char *name, int startb, int stopb, int starta,
int stopa, int startz, int stopz, int starty, int stopy, int startx,
int stopx, Function function, T &reduced) {
#ifdef PORTABILITY_STRATEGY_KOKKOS
using Policy5D = Kokkos::MDRangePolicy<Kokkos::Rank<5>>;
Kokkos::parallel_reduce(name,
Expand Down
21 changes: 9 additions & 12 deletions ports-of-call/span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,13 @@ using std::data;
using std::size;

// type-safe check against 0 (prevents warnings about comparing an unsigned against 0)
template <typename T,
std::enable_if_t<std::is_unsigned<T>::value, bool> = true>
PORTABLE_FUNCTION constexpr bool check_nonnegative(const T)
{
template <typename T, std::enable_if_t<std::is_unsigned<T>::value, bool> = true>
PORTABLE_FUNCTION constexpr bool check_nonnegative(const T) {
return true;
}
template <typename T,
std::enable_if_t<!std::is_unsigned<T>::value, bool> = true>
PORTABLE_FUNCTION constexpr bool check_nonnegative(const T t)
{
return t >= 0;
template <typename T, std::enable_if_t<!std::is_unsigned<T>::value, bool> = true>
PORTABLE_FUNCTION constexpr bool check_nonnegative(const T t) {
return t >= 0;
}

// object to handle storage of span
Expand Down Expand Up @@ -210,7 +206,7 @@ class span {
// NB: iterator concepts to further restrict overload resolution
constexpr span(pointer ptr, size_type count) : storage_(ptr, count) {
span_EXPECTS((ptr == nullptr && count == 0) ||
(ptr != nullptr && detail::check_nonnegative(count)));
(ptr != nullptr && detail::check_nonnegative(count)));
}

// constructs a span that is a view over the range [first, last)
Expand Down Expand Up @@ -321,7 +317,8 @@ class span {
template <std::size_t Offset, std::size_t Count = dynamic_extent>
constexpr subspan_return_t<Offset, Count> subspan() const {
span_EXPECTS((detail::check_nonnegative(Offset) && Offset <= size()) &&
(Count == dynamic_extent || (detail::check_nonnegative(Count) && Count + Offset <= size())));
(Count == dynamic_extent ||
(detail::check_nonnegative(Count) && Count + Offset <= size())));
return {data() + Offset, Count != dynamic_extent ? Count : size() - Offset};
}

Expand Down Expand Up @@ -396,7 +393,7 @@ span(const std::array<T, N> &) -> span<const T, N>;

template <class Container>
span(Container &) -> span<typename std::remove_reference<
decltype(*detail::data(std::declval<Container &>()))>::type>;
decltype(*detail::data(std::declval<Container &>()))>::type>;

template <class Container>
span(const Container &) -> span<const typename Container::value_type>;
Expand Down
2 changes: 1 addition & 1 deletion test/test_portability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ TEST_CASE("portableCopy works with all portability strategies", "[portableCopy]"
int sum{0};
portableReduce(
"check portableCopy", 0, N, 0, 0, 0, 0,
PORTABLE_LAMBDA(const int &i, const int &/*j*/, const int &/*k*/, int &isum) {
PORTABLE_LAMBDA(const int &i, const int & /*j*/, const int & /*k*/, int &isum) {
if (a[i] != index_func(i)) {
isum += 1;
}
Expand Down
3 changes: 1 addition & 2 deletions test/test_span.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ constexpr void generate(ForwardIt first, ForwardIt last, Generator g) {
}

template <class T, std::size_t N>
[[nodiscard]]
constexpr auto slide(span<T, N> s, std::size_t offset, std::size_t width) {
[[nodiscard]] constexpr auto slide(span<T, N> s, std::size_t offset, std::size_t width) {
return s.subspan(offset, offset + width <= s.size() ? width : 0U);
}

Expand Down
54 changes: 22 additions & 32 deletions test/test_static_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,25 @@ constexpr static bool really_const = std::is_const<std::remove_reference_t<T>>::
// This doesn't do anything interesting. It just needs to be non-trivial to
// test that static_vector works for non-trivial types.
struct NonTrivialType {
int n;
int * p;
PORTABLE_FUNCTION NonTrivialType(int nn)
: n{nn}
, p{&n}
{}
PORTABLE_FUNCTION NonTrivialType(const NonTrivialType & other)
: n{other.n}
, p{&n}
{}
PORTABLE_FUNCTION NonTrivialType(NonTrivialType && other)
: n{other.n}
, p{&n}
{}
PORTABLE_FUNCTION NonTrivialType& operator=(const NonTrivialType & other) {
n = other.n;
p = &n;
return *this;
}
PORTABLE_FUNCTION NonTrivialType& operator=(NonTrivialType && other) {
n = other.n;
p = &n;
return *this;
}
PORTABLE_FUNCTION ~NonTrivialType() {}
int n;
int *p;
PORTABLE_FUNCTION NonTrivialType(int nn) : n{nn}, p{&n} {}
PORTABLE_FUNCTION NonTrivialType(const NonTrivialType &other) : n{other.n}, p{&n} {}
PORTABLE_FUNCTION NonTrivialType(NonTrivialType &&other) : n{other.n}, p{&n} {}
PORTABLE_FUNCTION NonTrivialType &operator=(const NonTrivialType &other) {
n = other.n;
p = &n;
return *this;
}
PORTABLE_FUNCTION NonTrivialType &operator=(NonTrivialType &&other) {
n = other.n;
p = &n;
return *this;
}
PORTABLE_FUNCTION ~NonTrivialType() {}
};

}
} // namespace static_vector_test

static_assert(!std::is_trivial<static_vector_test::NonTrivialType>::value);

Expand All @@ -54,8 +45,8 @@ TEST_CASE("static_vector", "[util][static_vector]") {
using std::cend;
using std::end;

using static_vector_test::really_const;
using static_vector_test::NonTrivialType;
using static_vector_test::really_const;

SECTION("begin/end iteration") {
SECTION("with non-zero size") {
Expand Down Expand Up @@ -226,13 +217,12 @@ TEST_CASE("static_vector", "[util][static_vector]") {

SECTION("with non-trivial type") {
using test_t = PortsOfCall::static_vector<NonTrivialType, 5>;
test_t data = {NonTrivialType(1), NonTrivialType(2),
NonTrivialType(3), NonTrivialType(4),
NonTrivialType(5)};
test_t data = {NonTrivialType(1), NonTrivialType(2), NonTrivialType(3),
NonTrivialType(4), NonTrivialType(5)};

data.pop_back();
test_t const expected = {NonTrivialType(1), NonTrivialType(2),
NonTrivialType(3), NonTrivialType(4)};
test_t const expected = {NonTrivialType(1), NonTrivialType(2), NonTrivialType(3),
NonTrivialType(4)};

auto size_check =
std::equal(begin(data), end(data), begin(expected), end(expected),
Expand Down
2 changes: 1 addition & 1 deletion test/test_static_vector_kokkos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@ TEST_CASE("static vector GPU", "[reaction_network]") {

// ================================================================================================

#endif //PORTABILITY_STRATEGY_KOKKOS
#endif // PORTABILITY_STRATEGY_KOKKOS

0 comments on commit 5bf92e8

Please sign in to comment.