Kokkos Core Kernels Package Version of the Day
Loading...
Searching...
No Matches
Kokkos_MinMaxClamp.hpp
1//@HEADER
2// ************************************************************************
3//
4// Kokkos v. 4.0
5// Copyright (2022) National Technology & Engineering
6// Solutions of Sandia, LLC (NTESS).
7//
8// Under the terms of Contract DE-NA0003525 with NTESS,
9// the U.S. Government retains certain rights in this software.
10//
11// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12// See https://kokkos.org/LICENSE for license information.
13// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14//
15//@HEADER
16
17#ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
18#include <Kokkos_Macros.hpp>
19static_assert(false,
20 "Including non-public Kokkos header files is not allowed.");
21#endif
22#ifndef KOKKOS_MIN_MAX_CLAMP_HPP
23#define KOKKOS_MIN_MAX_CLAMP_HPP
24
25#include <Kokkos_Macros.hpp>
26#include <Kokkos_Pair.hpp>
27
28#include <initializer_list>
29
30namespace Kokkos {
31
32// clamp
33template <class T>
34constexpr KOKKOS_INLINE_FUNCTION const T& clamp(const T& value, const T& lo,
35 const T& hi) {
36 KOKKOS_EXPECTS(!(hi < lo));
37 return (value < lo) ? lo : (hi < value) ? hi : value;
38}
39
40template <class T, class ComparatorType>
41constexpr KOKKOS_INLINE_FUNCTION const T& clamp(const T& value, const T& lo,
42 const T& hi,
43 ComparatorType comp) {
44 KOKKOS_EXPECTS(!comp(hi, lo));
45 return comp(value, lo) ? lo : comp(hi, value) ? hi : value;
46}
47
48// max
49template <class T>
50constexpr KOKKOS_INLINE_FUNCTION const T& max(const T& a, const T& b) {
51 return (a < b) ? b : a;
52}
53
54template <class T, class ComparatorType>
55constexpr KOKKOS_INLINE_FUNCTION const T& max(const T& a, const T& b,
56 ComparatorType comp) {
57 return comp(a, b) ? b : a;
58}
59
60template <class T>
61KOKKOS_INLINE_FUNCTION constexpr T max(std::initializer_list<T> ilist) {
62 auto first = ilist.begin();
63 auto const last = ilist.end();
64 auto result = *first;
65 if (first == last) return result;
66 while (++first != last) {
67 if (result < *first) result = *first;
68 }
69 return result;
70}
71
72template <class T, class Compare>
73KOKKOS_INLINE_FUNCTION constexpr T max(std::initializer_list<T> ilist,
74 Compare comp) {
75 auto first = ilist.begin();
76 auto const last = ilist.end();
77 auto result = *first;
78 if (first == last) return result;
79 while (++first != last) {
80 if (comp(result, *first)) result = *first;
81 }
82 return result;
83}
84
85// min
86template <class T>
87constexpr KOKKOS_INLINE_FUNCTION const T& min(const T& a, const T& b) {
88 return (b < a) ? b : a;
89}
90
91template <class T, class ComparatorType>
92constexpr KOKKOS_INLINE_FUNCTION const T& min(const T& a, const T& b,
93 ComparatorType comp) {
94 return comp(b, a) ? b : a;
95}
96
97template <class T>
98KOKKOS_INLINE_FUNCTION constexpr T min(std::initializer_list<T> ilist) {
99 auto first = ilist.begin();
100 auto const last = ilist.end();
101 auto result = *first;
102 if (first == last) return result;
103 while (++first != last) {
104 if (*first < result) result = *first;
105 }
106 return result;
107}
108
109template <class T, class Compare>
110KOKKOS_INLINE_FUNCTION constexpr T min(std::initializer_list<T> ilist,
111 Compare comp) {
112 auto first = ilist.begin();
113 auto const last = ilist.end();
114 auto result = *first;
115 if (first == last) return result;
116 while (++first != last) {
117 if (comp(*first, result)) result = *first;
118 }
119 return result;
120}
121
122// minmax
123template <class T>
124constexpr KOKKOS_INLINE_FUNCTION auto minmax(const T& a, const T& b) {
125 using return_t = ::Kokkos::pair<const T&, const T&>;
126 return (b < a) ? return_t{b, a} : return_t{a, b};
127}
128
129template <class T, class ComparatorType>
130constexpr KOKKOS_INLINE_FUNCTION auto minmax(const T& a, const T& b,
131 ComparatorType comp) {
132 using return_t = ::Kokkos::pair<const T&, const T&>;
133 return comp(b, a) ? return_t{b, a} : return_t{a, b};
134}
135
136template <class T>
137KOKKOS_INLINE_FUNCTION constexpr Kokkos::pair<T, T> minmax(
138 std::initializer_list<T> ilist) {
139 auto first = ilist.begin();
140 auto const last = ilist.end();
141 auto next = first;
142 Kokkos::pair<T, T> result{*first, *first};
143 if (first == last || ++next == last) return result;
144 if (*next < *first)
145 result.first = *next;
146 else
147 result.second = *next;
148 first = next;
149 while (++first != last) {
150 if (++next == last) {
151 if (*first < result.first)
152 result.first = *first;
153 else if (!(*first < result.second))
154 result.second = *first;
155 break;
156 }
157 if (*next < *first) {
158 if (*next < result.first) result.first = *next;
159 if (!(*first < result.second)) result.second = *first;
160 } else {
161 if (*first < result.first) result.first = *first;
162 if (!(*next < result.second)) result.second = *next;
163 }
164 first = next;
165 }
166 return result;
167}
168
169template <class T, class Compare>
170KOKKOS_INLINE_FUNCTION constexpr Kokkos::pair<T, T> minmax(
171 std::initializer_list<T> ilist, Compare comp) {
172 auto first = ilist.begin();
173 auto const last = ilist.end();
174 auto next = first;
175 Kokkos::pair<T, T> result{*first, *first};
176 if (first == last || ++next == last) return result;
177 if (comp(*next, *first))
178 result.first = *next;
179 else
180 result.second = *next;
181 first = next;
182 while (++first != last) {
183 if (++next == last) {
184 if (comp(*first, result.first))
185 result.first = *first;
186 else if (!comp(*first, result.second))
187 result.second = *first;
188 break;
189 }
190 if (comp(*next, *first)) {
191 if (comp(*next, result.first)) result.first = *next;
192 if (!comp(*first, result.second)) result.second = *first;
193 } else {
194 if (comp(*first, result.first)) result.first = *first;
195 if (!comp(*next, result.second)) result.second = *next;
196 }
197 first = next;
198 }
199 return result;
200}
201
202#ifdef KOKKOS_ENABLE_DEPRECATED_CODE_3
203namespace Experimental {
204using ::Kokkos::clamp;
205using ::Kokkos::max;
206using ::Kokkos::min;
207using ::Kokkos::minmax;
208} // namespace Experimental
209#endif
210
211} // namespace Kokkos
212
213#endif
Declaration and definition of Kokkos::pair.
Replacement for std::pair that works on CUDA devices.
Definition: Kokkos_Pair.hpp:43
first_type first
The first element of the pair.
Definition: Kokkos_Pair.hpp:50