Skip to content

Commit

Permalink
MinLoc and MaxLoc Support (AMReX-Codes#2885)
Browse files Browse the repository at this point in the history
Add struct ValLocPair that can be used by ReduceOps/ReduceData and ParReduce
to find the location of the min/max value.

Add warp shuffle down function for more general types.  This is needed for
MinLoc/MaxLoc with CUDA < 11, because we don't use CUB for earlier versions
of CUDA.

The Intel GPU support is not done yet.  We need to allocate enough shared
local memory when the size of ValLocPair is larger than the size of unsigned
long long.
  • Loading branch information
WeiqunZhang committed Jul 26, 2022
1 parent 4b7e200 commit 7cf77dc
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
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

0 comments on commit 7cf77dc

Please sign in to comment.