Kokkos Core Kernels Package Version of the Day
Loading...
Searching...
No Matches
Kokkos_ReductionIdentity.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_REDUCTION_IDENTITY_HPP
18#define KOKKOS_REDUCTION_IDENTITY_HPP
19#ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
20#define KOKKOS_IMPL_PUBLIC_INCLUDE
21#define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_REDUCTION_IDENTITY
22#endif
23
24#include <Kokkos_Macros.hpp>
25#include <cfloat>
26#include <climits>
27
28namespace Kokkos {
29
30template <class T>
31struct reduction_identity; /*{
32 KOKKOS_FORCEINLINE_FUNCTION constexpr static T sum() { return T(); } // 0
33 KOKKOS_FORCEINLINE_FUNCTION constexpr static T prod() // 1
34 { static_assert( false, "Missing specialization of
35Kokkos::reduction_identity for custom prod reduction type"); return T(); }
36 KOKKOS_FORCEINLINE_FUNCTION constexpr static T max() // minimum value
37 { static_assert( false, "Missing specialization of
38Kokkos::reduction_identity for custom max reduction type"); return T(); }
39 KOKKOS_FORCEINLINE_FUNCTION constexpr static T min() // maximum value
40 { static_assert( false, "Missing specialization of
41Kokkos::reduction_identity for custom min reduction type"); return T(); }
42 KOKKOS_FORCEINLINE_FUNCTION constexpr static T bor() // 0, only for integer
43type { static_assert( false, "Missing specialization of
44Kokkos::reduction_identity for custom bor reduction type"); return T(); }
45 KOKKOS_FORCEINLINE_FUNCTION constexpr static T band() // !0, only for integer
46type { static_assert( false, "Missing specialization of
47Kokkos::reduction_identity for custom band reduction type"); return T(); }
48 KOKKOS_FORCEINLINE_FUNCTION constexpr static T lor() // 0, only for integer
49type { static_assert( false, "Missing specialization of
50Kokkos::reduction_identity for custom lor reduction type"); return T(); }
51 KOKKOS_FORCEINLINE_FUNCTION constexpr static T land() // !0, only for integer
52type { static_assert( false, "Missing specialization of
53Kokkos::reduction_identity for custom land reduction type"); return T(); }
54};*/
55
56template <>
57struct reduction_identity<char> {
58 KOKKOS_FORCEINLINE_FUNCTION constexpr static char sum() {
59 return static_cast<char>(0);
60 }
61 KOKKOS_FORCEINLINE_FUNCTION constexpr static char prod() {
62 return static_cast<char>(1);
63 }
64 KOKKOS_FORCEINLINE_FUNCTION constexpr static char max() { return CHAR_MIN; }
65 KOKKOS_FORCEINLINE_FUNCTION constexpr static char min() { return CHAR_MAX; }
66 KOKKOS_FORCEINLINE_FUNCTION constexpr static char bor() {
67 return static_cast<char>(0x0);
68 }
69 KOKKOS_FORCEINLINE_FUNCTION constexpr static char band() {
70 return ~static_cast<char>(0x0);
71 }
72 KOKKOS_FORCEINLINE_FUNCTION constexpr static char lor() {
73 return static_cast<char>(0);
74 }
75 KOKKOS_FORCEINLINE_FUNCTION constexpr static char land() {
76 return static_cast<char>(1);
77 }
78};
79
80template <>
81struct reduction_identity<signed char> {
82 KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char sum() {
83 return static_cast<signed char>(0);
84 }
85 KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char prod() {
86 return static_cast<signed char>(1);
87 }
88 KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char max() {
89 return SCHAR_MIN;
90 }
91 KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char min() {
92 return SCHAR_MAX;
93 }
94 KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char bor() {
95 return static_cast<signed char>(0x0);
96 }
97 KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char band() {
98 return ~static_cast<signed char>(0x0);
99 }
100 KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char lor() {
101 return static_cast<signed char>(0);
102 }
103 KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char land() {
104 return static_cast<signed char>(1);
105 }
106};
107
108template <>
109struct reduction_identity<bool> {
110 KOKKOS_FORCEINLINE_FUNCTION constexpr static bool lor() {
111 return static_cast<bool>(false);
112 }
113 KOKKOS_FORCEINLINE_FUNCTION constexpr static bool land() {
114 return static_cast<bool>(true);
115 }
116};
117
118template <>
119struct reduction_identity<short> {
120 KOKKOS_FORCEINLINE_FUNCTION constexpr static short sum() {
121 return static_cast<short>(0);
122 }
123 KOKKOS_FORCEINLINE_FUNCTION constexpr static short prod() {
124 return static_cast<short>(1);
125 }
126 KOKKOS_FORCEINLINE_FUNCTION constexpr static short max() { return SHRT_MIN; }
127 KOKKOS_FORCEINLINE_FUNCTION constexpr static short min() { return SHRT_MAX; }
128 KOKKOS_FORCEINLINE_FUNCTION constexpr static short bor() {
129 return static_cast<short>(0x0);
130 }
131 KOKKOS_FORCEINLINE_FUNCTION constexpr static short band() {
132 return ~static_cast<short>(0x0);
133 }
134 KOKKOS_FORCEINLINE_FUNCTION constexpr static short lor() {
135 return static_cast<short>(0);
136 }
137 KOKKOS_FORCEINLINE_FUNCTION constexpr static short land() {
138 return static_cast<short>(1);
139 }
140};
141
142template <>
143struct reduction_identity<int> {
144 KOKKOS_FORCEINLINE_FUNCTION constexpr static int sum() {
145 return static_cast<int>(0);
146 }
147 KOKKOS_FORCEINLINE_FUNCTION constexpr static int prod() {
148 return static_cast<int>(1);
149 }
150 KOKKOS_FORCEINLINE_FUNCTION constexpr static int max() { return INT_MIN; }
151 KOKKOS_FORCEINLINE_FUNCTION constexpr static int min() { return INT_MAX; }
152 KOKKOS_FORCEINLINE_FUNCTION constexpr static int bor() {
153 return static_cast<int>(0x0);
154 }
155 KOKKOS_FORCEINLINE_FUNCTION constexpr static int band() {
156 return ~static_cast<int>(0x0);
157 }
158 KOKKOS_FORCEINLINE_FUNCTION constexpr static int lor() {
159 return static_cast<int>(0);
160 }
161 KOKKOS_FORCEINLINE_FUNCTION constexpr static int land() {
162 return static_cast<int>(1);
163 }
164};
165
166template <>
167struct reduction_identity<long> {
168 KOKKOS_FORCEINLINE_FUNCTION constexpr static long sum() {
169 return static_cast<long>(0);
170 }
171 KOKKOS_FORCEINLINE_FUNCTION constexpr static long prod() {
172 return static_cast<long>(1);
173 }
174 KOKKOS_FORCEINLINE_FUNCTION constexpr static long max() { return LONG_MIN; }
175 KOKKOS_FORCEINLINE_FUNCTION constexpr static long min() { return LONG_MAX; }
176 KOKKOS_FORCEINLINE_FUNCTION constexpr static long bor() {
177 return static_cast<long>(0x0);
178 }
179 KOKKOS_FORCEINLINE_FUNCTION constexpr static long band() {
180 return ~static_cast<long>(0x0);
181 }
182 KOKKOS_FORCEINLINE_FUNCTION constexpr static long lor() {
183 return static_cast<long>(0);
184 }
185 KOKKOS_FORCEINLINE_FUNCTION constexpr static long land() {
186 return static_cast<long>(1);
187 }
188};
189
190template <>
191struct reduction_identity<long long> {
192 KOKKOS_FORCEINLINE_FUNCTION constexpr static long long sum() {
193 return static_cast<long long>(0);
194 }
195 KOKKOS_FORCEINLINE_FUNCTION constexpr static long long prod() {
196 return static_cast<long long>(1);
197 }
198 KOKKOS_FORCEINLINE_FUNCTION constexpr static long long max() {
199 return LLONG_MIN;
200 }
201 KOKKOS_FORCEINLINE_FUNCTION constexpr static long long min() {
202 return LLONG_MAX;
203 }
204 KOKKOS_FORCEINLINE_FUNCTION constexpr static long long bor() {
205 return static_cast<long long>(0x0);
206 }
207 KOKKOS_FORCEINLINE_FUNCTION constexpr static long long band() {
208 return ~static_cast<long long>(0x0);
209 }
210 KOKKOS_FORCEINLINE_FUNCTION constexpr static long long lor() {
211 return static_cast<long long>(0);
212 }
213 KOKKOS_FORCEINLINE_FUNCTION constexpr static long long land() {
214 return static_cast<long long>(1);
215 }
216};
217
218template <>
219struct reduction_identity<unsigned char> {
220 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char sum() {
221 return static_cast<unsigned char>(0);
222 }
223 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char prod() {
224 return static_cast<unsigned char>(1);
225 }
226 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char max() {
227 return static_cast<unsigned char>(0);
228 }
229 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char min() {
230 return UCHAR_MAX;
231 }
232 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char bor() {
233 return static_cast<unsigned char>(0x0);
234 }
235 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char band() {
236 return ~static_cast<unsigned char>(0x0);
237 }
238 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char lor() {
239 return static_cast<unsigned char>(0);
240 }
241 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char land() {
242 return static_cast<unsigned char>(1);
243 }
244};
245
246template <>
247struct reduction_identity<unsigned short> {
248 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short sum() {
249 return static_cast<unsigned short>(0);
250 }
251 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short prod() {
252 return static_cast<unsigned short>(1);
253 }
254 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short max() {
255 return static_cast<unsigned short>(0);
256 }
257 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short min() {
258 return USHRT_MAX;
259 }
260 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short bor() {
261 return static_cast<unsigned short>(0x0);
262 }
263 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short band() {
264 return ~static_cast<unsigned short>(0x0);
265 }
266 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short lor() {
267 return static_cast<unsigned short>(0);
268 }
269 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short land() {
270 return static_cast<unsigned short>(1);
271 }
272};
273
274template <>
275struct reduction_identity<unsigned int> {
276 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int sum() {
277 return static_cast<unsigned int>(0);
278 }
279 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int prod() {
280 return static_cast<unsigned int>(1);
281 }
282 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int max() {
283 return static_cast<unsigned int>(0);
284 }
285 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int min() {
286 return UINT_MAX;
287 }
288 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int bor() {
289 return static_cast<unsigned int>(0x0);
290 }
291 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int band() {
292 return ~static_cast<unsigned int>(0x0);
293 }
294 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int lor() {
295 return static_cast<unsigned int>(0);
296 }
297 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int land() {
298 return static_cast<unsigned int>(1);
299 }
300};
301
302template <>
303struct reduction_identity<unsigned long> {
304 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long sum() {
305 return static_cast<unsigned long>(0);
306 }
307 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long prod() {
308 return static_cast<unsigned long>(1);
309 }
310 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long max() {
311 return static_cast<unsigned long>(0);
312 }
313 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long min() {
314 return ULONG_MAX;
315 }
316 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long bor() {
317 return static_cast<unsigned long>(0x0);
318 }
319 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long band() {
320 return ~static_cast<unsigned long>(0x0);
321 }
322 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long lor() {
323 return static_cast<unsigned long>(0);
324 }
325 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long land() {
326 return static_cast<unsigned long>(1);
327 }
328};
329
330template <>
331struct reduction_identity<unsigned long long> {
332 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long sum() {
333 return static_cast<unsigned long long>(0);
334 }
335 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long prod() {
336 return static_cast<unsigned long long>(1);
337 }
338 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long max() {
339 return static_cast<unsigned long long>(0);
340 }
341 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long min() {
342 return ULLONG_MAX;
343 }
344 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long bor() {
345 return static_cast<unsigned long long>(0x0);
346 }
347 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long band() {
348 return ~static_cast<unsigned long long>(0x0);
349 }
350 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long lor() {
351 return static_cast<unsigned long long>(0);
352 }
353 KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long land() {
354 return static_cast<unsigned long long>(1);
355 }
356};
357
358template <>
359struct reduction_identity<float> {
360 KOKKOS_FORCEINLINE_FUNCTION constexpr static float sum() {
361 return static_cast<float>(0.0f);
362 }
363 KOKKOS_FORCEINLINE_FUNCTION constexpr static float prod() {
364 return static_cast<float>(1.0f);
365 }
366 KOKKOS_FORCEINLINE_FUNCTION constexpr static float max() { return -FLT_MAX; }
367 KOKKOS_FORCEINLINE_FUNCTION constexpr static float min() { return FLT_MAX; }
368};
369
370template <>
371struct reduction_identity<double> {
372 KOKKOS_FORCEINLINE_FUNCTION constexpr static double sum() {
373 return static_cast<double>(0.0);
374 }
375 KOKKOS_FORCEINLINE_FUNCTION constexpr static double prod() {
376 return static_cast<double>(1.0);
377 }
378 KOKKOS_FORCEINLINE_FUNCTION constexpr static double max() { return -DBL_MAX; }
379 KOKKOS_FORCEINLINE_FUNCTION constexpr static double min() { return DBL_MAX; }
380};
381
382// No __host__ __device__ annotation because long double treated as double in
383// device code. May be revisited later if that is not true any more.
384template <>
385struct reduction_identity<long double> {
386 constexpr static long double sum() { return static_cast<long double>(0.0); }
387 constexpr static long double prod() { return static_cast<long double>(1.0); }
388 constexpr static long double max() { return -LDBL_MAX; }
389 constexpr static long double min() { return LDBL_MAX; }
390};
391
392} // namespace Kokkos
393
394#ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_REDUCTION_IDENTITY
395#undef KOKKOS_IMPL_PUBLIC_INCLUDE
396#undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_REDUCTION_IDENTITY
397#endif
398#endif