Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MinLoc and MaxLoc #2885

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions Src/Base/AMReX_GpuReduce.H
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <AMReX_GpuAtomic.H>
#include <AMReX_GpuUtility.H>
#include <AMReX_Functional.H>
#include <AMReX_TypeTraits.H>

#if !defined(AMREX_USE_CUB) && defined(AMREX_USE_CUDA) && defined(__CUDACC__) && (__CUDACC_VER_MAJOR__ >= 11)
#define AMREX_USE_CUB 1
Expand Down Expand Up @@ -249,15 +250,54 @@ void deviceReduceLogicalOr (int * dest, int source, Gpu::Handler const& h) noexc

#elif defined(AMREX_USE_CUDA) || defined(AMREX_USE_HIP)

namespace detail {

template <typename T>
AMREX_GPU_DEVICE AMREX_FORCE_INLINE
T shuffle_down (T x, int offset) noexcept
{
return AMREX_HIP_OR_CUDA(__shfl_down(x, offset),
__shfl_down_sync(0xffffffff, x, offset));
}

// If other sizeof is needed, we can implement it later.
template <class T, std::enable_if_t<sizeof(T)%sizeof(unsigned int) == 0, int> = 0>
AMREX_GPU_DEVICE AMREX_FORCE_INLINE
T multi_shuffle_down (T x, int offset) noexcept
{
constexpr int nwords = (sizeof(T) + sizeof(unsigned int) - 1) / sizeof(unsigned int);
T y;
auto py = reinterpret_cast<unsigned int*>(&y);
auto px = reinterpret_cast<unsigned int*>(&x);
for (int i = 0; i < nwords; ++i) {
py[i] = shuffle_down(px[i],offset);
}
return y;
}

}

template <int warpSize, typename T, typename F>
struct warpReduce
{
// Not all arithmetic types can be taken by shuffle_down, but it's good enough.
template <class U=T, std::enable_if_t<std::is_arithmetic<U>::value,int> = 0>
AMREX_GPU_DEVICE AMREX_FORCE_INLINE
T operator() (T x) const noexcept
{
for (int offset = warpSize/2; offset > 0; offset /= 2) {
T y = detail::shuffle_down(x, offset);
x = F()(x,y);
}
return x;
}

template <class U=T, std::enable_if_t<!std::is_arithmetic<U>::value,int> = 0>
AMREX_GPU_DEVICE AMREX_FORCE_INLINE
T operator() (T x) const noexcept
{
for (int offset = warpSize/2; offset > 0; offset /= 2) {
AMREX_HIP_OR_CUDA(T y = __shfl_down(x, offset);,
T y = __shfl_down_sync(0xffffffff, x, offset); )
T y = detail::multi_shuffle_down(x, offset);
x = F()(x,y);
}
return x;
Expand Down
40 changes: 38 additions & 2 deletions Src/Base/AMReX_Reduce.H
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,35 @@

#include <algorithm>
#include <functional>
#include <limits>

namespace amrex {

template <typename TV, typename TI>
struct ValLocPair
{
TV value;
TI index;

static constexpr ValLocPair<TV,TI> max () {
return ValLocPair<TV,TI>{std::numeric_limits<TV>::max(), TI()};
}

static constexpr ValLocPair<TV,TI> lowest () {
return ValLocPair<TV,TI>{std::numeric_limits<TV>::lowest(), TI()};
}

friend constexpr bool operator< (ValLocPair<TV,TI> const& a, ValLocPair<TV,TI> const& b)
{
return a.value < b.value;
}

friend constexpr bool operator> (ValLocPair<TV,TI> const& a, ValLocPair<TV,TI> const& b)
{
return a.value > b.value;
}
};

namespace Reduce { namespace detail {

#ifdef AMREX_USE_GPU
Expand Down Expand Up @@ -133,7 +159,12 @@ struct ReduceOpMin
void local_update (T& d, T const& s) const noexcept { d = amrex::min(d,s); }

template <typename T>
constexpr void init (T& t) const noexcept { t = std::numeric_limits<T>::max(); }
constexpr std::enable_if_t<std::numeric_limits<T>::is_specialized>
init (T& t) const noexcept { t = std::numeric_limits<T>::max(); }

template <typename T>
constexpr std::enable_if_t<!std::numeric_limits<T>::is_specialized>
init (T& t) const noexcept { t = T::max(); }
};

struct ReduceOpMax
Expand Down Expand Up @@ -161,7 +192,12 @@ struct ReduceOpMax
void local_update (T& d, T const& s) const noexcept { d = amrex::max(d,s); }

template <typename T>
constexpr void init (T& t) const noexcept { t = std::numeric_limits<T>::lowest(); }
constexpr std::enable_if_t<std::numeric_limits<T>::is_specialized>
init (T& t) const noexcept { t = std::numeric_limits<T>::lowest(); }

template <typename T>
constexpr std::enable_if_t<!std::numeric_limits<T>::is_specialized>
init (T& t) const noexcept { t = T::lowest(); }
};

struct ReduceOpLogicalAnd
Expand Down