Kokkos Core Kernels Package Version of the Day
Kokkos_Functional.hpp
1//@HEADER
2// ************************************************************************
3//
4// Kokkos v. 3.0
5// Copyright (2020) 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// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact Christian R. Trott (crtrott@sandia.gov)
39//
40// ************************************************************************
41//@HEADER
42
43#ifndef KOKKOS_FUNCTIONAL_HPP
44#define KOKKOS_FUNCTIONAL_HPP
45
46#include <Kokkos_Macros.hpp>
47#include <impl/Kokkos_Functional_impl.hpp>
48
49namespace Kokkos {
50
51// These should work for most types
52
53template <typename T>
54struct pod_hash {
55 using argument_type = T;
56 using first_argument_type = T;
57 using second_argument_type = uint32_t;
58 using result_type = uint32_t;
59
60 KOKKOS_FORCEINLINE_FUNCTION
61 uint32_t operator()(T const& t) const {
62 return Impl::MurmurHash3_x86_32(&t, sizeof(T), 0);
63 }
64
65 KOKKOS_FORCEINLINE_FUNCTION
66 uint32_t operator()(T const& t, uint32_t seed) const {
67 return Impl::MurmurHash3_x86_32(&t, sizeof(T), seed);
68 }
69};
70
71template <typename T>
72struct pod_equal_to {
73 using first_argument_type = T;
74 using second_argument_type = T;
75 using result_type = bool;
76
77 KOKKOS_FORCEINLINE_FUNCTION
78 bool operator()(T const& a, T const& b) const {
79 return Impl::bitwise_equal(&a, &b);
80 }
81};
82
83template <typename T>
84struct pod_not_equal_to {
85 using first_argument_type = T;
86 using second_argument_type = T;
87 using result_type = bool;
88
89 KOKKOS_FORCEINLINE_FUNCTION
90 bool operator()(T const& a, T const& b) const {
91 return !Impl::bitwise_equal(&a, &b);
92 }
93};
94
95template <typename T>
96struct equal_to {
97 using first_argument_type = T;
98 using second_argument_type = T;
99 using result_type = bool;
100
101 KOKKOS_FORCEINLINE_FUNCTION
102 bool operator()(T const& a, T const& b) const { return a == b; }
103};
104
105template <typename T>
106struct not_equal_to {
107 using first_argument_type = T;
108 using second_argument_type = T;
109 using result_type = bool;
110
111 KOKKOS_FORCEINLINE_FUNCTION
112 bool operator()(T const& a, T const& b) const { return a != b; }
113};
114
115template <typename T>
116struct greater {
117 using first_argument_type = T;
118 using second_argument_type = T;
119 using result_type = bool;
120
121 KOKKOS_FORCEINLINE_FUNCTION
122 bool operator()(T const& a, T const& b) const { return a > b; }
123};
124
125template <typename T>
126struct less {
127 using first_argument_type = T;
128 using second_argument_type = T;
129 using result_type = bool;
130
131 KOKKOS_FORCEINLINE_FUNCTION
132 bool operator()(T const& a, T const& b) const { return a < b; }
133};
134
135template <typename T>
136struct greater_equal {
137 using first_argument_type = T;
138 using second_argument_type = T;
139 using result_type = bool;
140
141 KOKKOS_FORCEINLINE_FUNCTION
142 bool operator()(T const& a, T const& b) const { return a >= b; }
143};
144
145template <typename T>
146struct less_equal {
147 using first_argument_type = T;
148 using second_argument_type = T;
149 using result_type = bool;
150
151 KOKKOS_FORCEINLINE_FUNCTION
152 bool operator()(T const& a, T const& b) const { return a <= b; }
153};
154
155} // namespace Kokkos
156
157#endif // KOKKOS_FUNCTIONAL_HPP