|
| 1 | +#ifndef MAM4XX_BFBHASH_HPP |
| 2 | +#define MAM4xx_BFBHASH_HPP |
| 3 | + |
| 4 | +// This was copied from Andrew Bradley's original implementation at |
| 5 | +// E3SM/components/eamxx/src/share/util/eamxx_bfbhash.hpp. |
| 6 | + |
| 7 | +#include <cstdint> |
| 8 | + |
| 9 | +#include <ekat_comm.hpp> |
| 10 | +#include <ekat_kokkos_types.hpp> |
| 11 | + |
| 12 | +namespace mam4 { |
| 13 | +namespace bfbhash { |
| 14 | + |
| 15 | +typedef std::uint64_t HashType; |
| 16 | + |
| 17 | +KOKKOS_INLINE_FUNCTION void hash(const HashType v, HashType &accum) { |
| 18 | + constexpr auto first_bit = 1ULL << 63; |
| 19 | + accum += ~first_bit & v; // no overflow |
| 20 | + accum ^= first_bit & v; // handle most significant bit |
| 21 | +} |
| 22 | + |
| 23 | +KOKKOS_INLINE_FUNCTION void hash(const double v_, HashType &accum) { |
| 24 | + static_assert(sizeof(double) == sizeof(HashType), |
| 25 | + "HashType must have size sizeof(double)."); |
| 26 | + HashType v; |
| 27 | + std::memcpy(&v, &v_, sizeof(HashType)); |
| 28 | + hash(v, accum); |
| 29 | +} |
| 30 | + |
| 31 | +KOKKOS_INLINE_FUNCTION void hash(const float v, HashType &accum) { |
| 32 | + hash(double(v), accum); |
| 33 | +} |
| 34 | + |
| 35 | +// For Kokkos::parallel_reduce. |
| 36 | +template <typename ExecSpace = Kokkos::HostSpace> struct HashReducer { |
| 37 | + typedef HashReducer reducer; |
| 38 | + typedef HashType value_type; |
| 39 | + typedef Kokkos::View<value_type *, ExecSpace, Kokkos::MemoryUnmanaged> |
| 40 | + result_view_type; |
| 41 | + |
| 42 | + KOKKOS_INLINE_FUNCTION HashReducer(value_type &value_) : value(value_) {} |
| 43 | + KOKKOS_INLINE_FUNCTION void join(value_type &dest, |
| 44 | + const value_type &src) const { |
| 45 | + hash(src, dest); |
| 46 | + } |
| 47 | + KOKKOS_INLINE_FUNCTION void init(value_type &val) const { val = 0; } |
| 48 | + KOKKOS_INLINE_FUNCTION value_type &reference() const { return value; } |
| 49 | + KOKKOS_INLINE_FUNCTION bool references_scalar() const { return true; } |
| 50 | + KOKKOS_INLINE_FUNCTION result_view_type view() const { |
| 51 | + return result_view_type(&value, 1); |
| 52 | + } |
| 53 | + |
| 54 | +private: |
| 55 | + value_type &value; |
| 56 | +}; |
| 57 | + |
| 58 | +} // namespace bfbhash |
| 59 | +} // namespace mam4 |
| 60 | + |
| 61 | +#endif |
0 commit comments