1#ifndef AMREX_ALGORITHM_H_
2#define AMREX_ALGORITHM_H_
3#include <AMReX_Config.H>
40 template <
class T,
class ... Ts>
62 template <
class T,
class ... Ts>
79 template <
class T,
class ... Ts>
81 T
elemwiseMin (
const T& a,
const T& b,
const Ts& ... c)
noexcept
96 template <
class T,
class ... Ts>
98 T
elemwiseMax (
const T& a,
const T& b,
const Ts& ... c)
noexcept
108 void Swap (T& t1, T& t2)
noexcept
110 T temp = std::move(t1);
112 t2 = std::move(temp);
120 template <
typename T>
122 constexpr const T&
Clamp (
const T& v,
const T& lo,
const T& hi)
136 template <std::
floating_po
int T>
144 return std::abs(
x-
y) <= std::numeric_limits<T>::epsilon() * std::abs(
x+
y) * ulp
146 || std::abs(
x-
y) < std::numeric_limits<T>::min();
167 template <std::
floating_po
int T,
class F>
169 T
bisect (T lo, T hi,
F f, T tol=1e-12,
int max_iter=100)
172 "Error - calling bisect but lo and hi don't describe a reasonable interval.");
177 if (flo == T(0)) {
return lo; }
178 if (fhi == T(0)) {
return hi; }
181 "Error - calling bisect but lo and hi don't bracket a root.");
183 T mi = (lo + hi) / T(2);
186 while (n <= max_iter)
188 if (hi - lo < tol ||
almostEqual(lo,hi)) {
break; }
189 mi = (lo + hi) / T(2);
191 if (fmi == T(0)) {
break; }
192 fmi*flo < T(0) ? hi = mi : lo = mi;
199 "Error - maximum number of iterations reached in bisect.");
226 template <
typename T, std::
integral I>
228 I
bisect (T
const* d, I lo, I hi, T
const& v)
230 while (hi - lo > 1) {
231 I mid = lo + (hi - lo) / 2;
256 template<
typename ItType,
typename ValType>
258 ItType
upper_bound (ItType first, ItType last,
const ValType& val)
261 std::ptrdiff_t count = last-first;
264 const auto step = count/2;
277 return std::upper_bound(first, last, val);
296 template<
typename ItType,
typename ValType>
298 ItType
lower_bound (ItType first, ItType last,
const ValType& val)
301 std::ptrdiff_t count = last-first;
305 const auto step = count/2;
319 return std::lower_bound(first, last, val);
341 template<
typename ItType, std::
floating_po
int ValType>
342 requires (std::floating_point<typename std::iterator_traits<ItType>::value_type>)
344 void linspace (ItType first,
const ItType& last,
const ValType& start,
const ValType& stop)
346 const std::ptrdiff_t count = last-first;
348 const auto delta = (stop - start)/(count - 1);
349 for (std::ptrdiff_t i = 0; i < count-1; ++i){
350 *(first++) = start + i*delta;
375 template<
typename ItType, std::
floating_po
int ValType>
376 requires (std::floating_point<typename std::iterator_traits<ItType>::value_type>)
379 const ValType& start,
const ValType& stop,
const ValType& base)
381 const std::ptrdiff_t count = last-first;
383 const auto delta = (stop - start)/(count - 1);
384 for (std::ptrdiff_t i = 0; i < count-1; ++i){
385 *(first++) = std::pow(base, start + i*delta);
387 *first = std::pow(base, stop);
Assertion macros used across AMReX for runtime consistency checks.
#define AMREX_ASSERT_WITH_MESSAGE(EX, MSG)
Definition AMReX_BLassert.H:37
Simple structs for 3-component integer and real-valued tuples.
Compiler- and backend-specific extension macros (e.g., restrict, SIMD, inline).
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:124
#define AMREX_IF_ON_DEVICE(CODE)
Definition AMReX_GpuQualifiers.H:56
#define AMREX_IF_ON_HOST(CODE)
Definition AMReX_GpuQualifiers.H:58
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
__host__ __device__ ItType upper_bound(ItType first, ItType last, const ValType &val)
Return an iterator to the first element greater than a given value.
Definition AMReX_Algorithm.H:258
__host__ __device__ void logspace(ItType first, const ItType &last, const ValType &start, const ValType &stop, const ValType &base)
Fill a range with logarithmically spaced values over a closed interval.
Definition AMReX_Algorithm.H:378
__host__ __device__ void Swap(T &t1, T &t2) noexcept
Definition AMReX_Algorithm.H:108
__host__ __device__ T bisect(T lo, T hi, F f, T tol=1e-12, int max_iter=100)
Find a root of a scalar function on a bracketing interval using bisection.
Definition AMReX_Algorithm.H:169
__host__ __device__ constexpr T elemwiseMax(T const &a, T const &b) noexcept
Definition AMReX_Algorithm.H:90
__host__ __device__ constexpr const T & Clamp(const T &v, const T &lo, const T &hi)
Definition AMReX_Algorithm.H:122
__host__ __device__ constexpr const T & min(const T &a, const T &b) noexcept
Definition AMReX_Algorithm.H:31
__host__ __device__ ItType lower_bound(ItType first, ItType last, const ValType &val)
Return an iterator to the first element not less than a given value.
Definition AMReX_Algorithm.H:298
__host__ __device__ void linspace(ItType first, const ItType &last, const ValType &start, const ValType &stop)
Fill a range with linearly spaced values over a closed interval.
Definition AMReX_Algorithm.H:344
__host__ __device__ constexpr const T & max(const T &a, const T &b) noexcept
Definition AMReX_Algorithm.H:53
__host__ __device__ bool almostEqual(T x, T y, int ulp=2)
Definition AMReX_Algorithm.H:139
__host__ __device__ constexpr T elemwiseMin(T const &a, T const &b) noexcept
Definition AMReX_Algorithm.H:73
Definition AMReX_Amr.cpp:50