Kokkos Core Kernels Package  Version of the Day
Kokkos_Pair.hpp
Go to the documentation of this file.
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 
22 
23 #ifndef KOKKOS_PAIR_HPP
24 #define KOKKOS_PAIR_HPP
25 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
26 #define KOKKOS_IMPL_PUBLIC_INCLUDE
27 #define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_PAIR
28 #endif
29 
30 #include <Kokkos_Macros.hpp>
31 #include <utility>
32 
33 namespace Kokkos {
42 template <class T1, class T2>
43 struct pair {
45  using first_type = T1;
47  using second_type = T2;
48 
53 
59  KOKKOS_DEFAULTED_FUNCTION constexpr pair() = default;
60 
65 #ifdef KOKKOS_COMPILER_NVHPC // FIXME_NVHPC bug in NVHPC regarding constexpr
66  // constructors used in device code
67  KOKKOS_FORCEINLINE_FUNCTION
68 #else
69  KOKKOS_FORCEINLINE_FUNCTION constexpr
70 #endif
71  pair(first_type const& f, second_type const& s) : first(f), second(s) {}
72 
77  template <class U, class V>
78 #ifdef KOKKOS_COMPILER_NVHPC // FIXME_NVHPC bug in NVHPC regarding constexpr
79  // constructors used in device code
80  KOKKOS_FORCEINLINE_FUNCTION
81 #else
82  KOKKOS_FORCEINLINE_FUNCTION constexpr
83 #endif
84  pair(const pair<U, V>& p)
85  : first(p.first), second(p.second) {
86  }
87 
88 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
89  template <class U, class V>
94  KOKKOS_DEPRECATED KOKKOS_FORCEINLINE_FUNCTION constexpr pair(
95  const volatile pair<U, V>& p)
96  : first(p.first), second(p.second) {}
97 #endif
98 
103  template <class U, class V>
104  KOKKOS_FORCEINLINE_FUNCTION pair<T1, T2>& operator=(const pair<U, V>& p) {
105  first = p.first;
106  second = p.second;
107  return *this;
108  }
109 
110 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
111  template <class U, class V>
123  KOKKOS_DEPRECATED KOKKOS_FORCEINLINE_FUNCTION void operator=(
124  const volatile pair<U, V>& p) volatile {
125  first = p.first;
126  second = p.second;
127  // We deliberately do not return anything here. See explanation
128  // in public documentation above.
129  }
130 #endif
131 
132  // from std::pair<U,V>
133  template <class U, class V>
134  pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
135 
145  std::pair<T1, T2> to_std_pair() const {
146  return std::make_pair(first, second);
147  }
148 };
149 
150 template <class T1, class T2>
151 struct pair<T1&, T2&> {
153  using first_type = T1&;
155  using second_type = T2&;
156 
158  first_type first;
160  second_type second;
161 
166  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type f, second_type s)
167  : first(f), second(s) {}
168 
173  template <class U, class V>
174  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
175  : first(p.first), second(p.second) {}
176 
177  // from std::pair<U,V>
178  template <class U, class V>
179  pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
180 
185  template <class U, class V>
186  KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
187  const pair<U, V>& p) {
188  first = p.first;
189  second = p.second;
190  return *this;
191  }
192 
202  std::pair<T1, T2> to_std_pair() const {
203  return std::make_pair(first, second);
204  }
205 };
206 
207 template <class T1, class T2>
208 struct pair<T1, T2&> {
210  using first_type = T1;
212  using second_type = T2&;
213 
215  first_type first;
217  second_type second;
218 
223  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type const& f, second_type s)
224  : first(f), second(s) {}
225 
230  template <class U, class V>
231  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
232  : first(p.first), second(p.second) {}
233 
234  // from std::pair<U,V>
235  template <class U, class V>
236  pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
237 
242  template <class U, class V>
243  KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
244  const pair<U, V>& p) {
245  first = p.first;
246  second = p.second;
247  return *this;
248  }
249 
259  std::pair<T1, T2> to_std_pair() const {
260  return std::make_pair(first, second);
261  }
262 };
263 
264 template <class T1, class T2>
265 struct pair<T1&, T2> {
267  using first_type = T1&;
269  using second_type = T2;
270 
272  first_type first;
274  second_type second;
275 
280  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type f, second_type const& s)
281  : first(f), second(s) {}
282 
287  template <class U, class V>
288  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
289  : first(p.first), second(p.second) {}
290 
291  // from std::pair<U,V>
292  template <class U, class V>
293  pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
294 
299  template <class U, class V>
300  KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
301  const pair<U, V>& p) {
302  first = p.first;
303  second = p.second;
304  return *this;
305  }
306 
316  std::pair<T1, T2> to_std_pair() const {
317  return std::make_pair(first, second);
318  }
319 };
320 
322 template <class T1, class T2>
323 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator==(const pair<T1, T2>& lhs,
324  const pair<T1, T2>& rhs) {
325  return lhs.first == rhs.first && lhs.second == rhs.second;
326 }
327 
329 template <class T1, class T2>
330 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator!=(const pair<T1, T2>& lhs,
331  const pair<T1, T2>& rhs) {
332  return !(lhs == rhs);
333 }
334 
336 template <class T1, class T2>
337 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(const pair<T1, T2>& lhs,
338  const pair<T1, T2>& rhs) {
339  return lhs.first < rhs.first ||
340  (!(rhs.first < lhs.first) && lhs.second < rhs.second);
341 }
342 
344 template <class T1, class T2>
345 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<=(const pair<T1, T2>& lhs,
346  const pair<T1, T2>& rhs) {
347  return !(rhs < lhs);
348 }
349 
351 template <class T1, class T2>
352 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>(const pair<T1, T2>& lhs,
353  const pair<T1, T2>& rhs) {
354  return rhs < lhs;
355 }
356 
358 template <class T1, class T2>
359 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(const pair<T1, T2>& lhs,
360  const pair<T1, T2>& rhs) {
361  return !(lhs < rhs);
362 }
363 
368 template <class T1, class T2>
369 KOKKOS_FORCEINLINE_FUNCTION constexpr pair<T1, T2> make_pair(T1 x, T2 y) {
370  return (pair<T1, T2>(x, y));
371 }
372 
412 template <class T1, class T2>
413 KOKKOS_FORCEINLINE_FUNCTION pair<T1&, T2&> tie(T1& x, T2& y) {
414  return (pair<T1&, T2&>(x, y));
415 }
416 
417 //
418 // Specialization of Kokkos::pair for a \c void second argument. This
419 // is not actually a "pair"; it only contains one element, the first.
420 //
421 template <class T1>
422 struct pair<T1, void> {
423  using first_type = T1;
424  using second_type = void;
425 
427  enum { second = 0 };
428 
429  KOKKOS_DEFAULTED_FUNCTION constexpr pair() = default;
430 
431  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const first_type& f) : first(f) {}
432 
433  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const first_type& f, int)
434  : first(f) {}
435 
436  template <class U>
437  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, void>& p)
438  : first(p.first) {}
439 
440  template <class U>
441  KOKKOS_FORCEINLINE_FUNCTION pair<T1, void>& operator=(
442  const pair<U, void>& p) {
443  first = p.first;
444  return *this;
445  }
446 };
447 
448 //
449 // Specialization of relational operators for Kokkos::pair<T1,void>.
450 //
451 
452 template <class T1>
453 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator==(
454  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
455  return lhs.first == rhs.first;
456 }
457 
458 template <class T1>
459 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator!=(
460  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
461  return !(lhs == rhs);
462 }
463 
464 template <class T1>
465 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(
466  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
467  return lhs.first < rhs.first;
468 }
469 
470 template <class T1>
471 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<=(
472  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
473  return !(rhs < lhs);
474 }
475 
476 template <class T1>
477 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>(
478  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
479  return rhs < lhs;
480 }
481 
482 template <class T1>
483 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(
484  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
485  return !(lhs < rhs);
486 }
487 
488 namespace Impl {
489 
490 template <class T>
491 struct is_pair_like : std::false_type {};
492 template <class T, class U>
493 struct is_pair_like<Kokkos::pair<T, U>> : std::true_type {};
494 template <class T, class U>
495 struct is_pair_like<std::pair<T, U>> : std::true_type {};
496 
497 } // end namespace Impl
498 
499 } // namespace Kokkos
500 
501 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_PAIR
502 #undef KOKKOS_IMPL_PUBLIC_INCLUDE
503 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_PAIR
504 #endif
505 #endif // KOKKOS_PAIR_HPP
KOKKOS_FORCEINLINE_FUNCTION constexpr pair< T1, T2 > make_pair(T1 x, T2 y)
Return a new pair.
Replacement for std::pair that works on CUDA devices.
Definition: Kokkos_Pair.hpp:43
std::pair< T1, T2 > to_std_pair() const
Return the std::pair version of this object.
T2 second_type
The second template parameter of this class.
Definition: Kokkos_Pair.hpp:47
KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(const pair< T1, T2 > &lhs, const pair< T1, T2 > &rhs)
Less-than operator for Kokkos::pair.
first_type first
The first element of the pair.
Definition: Kokkos_Pair.hpp:50
KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type const &f, second_type const &s)
Constructor that takes both elements of the pair.
Definition: Kokkos_Pair.hpp:71
KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<=(const pair< T1, T2 > &lhs, const pair< T1, T2 > &rhs)
Less-than-or-equal-to operator for Kokkos::pair.
KOKKOS_FORCEINLINE_FUNCTION pair< T1 &, T2 & > tie(T1 &x, T2 &y)
Return a pair of references to the input arguments.
T1 first_type
The first template parameter of this class.
Definition: Kokkos_Pair.hpp:45
KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(const pair< T1, T2 > &lhs, const pair< T1, T2 > &rhs)
Greater-than-or-equal-to operator for Kokkos::pair.
KOKKOS_DEFAULTED_FUNCTION constexpr pair()=default
Default constructor.
KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>(const pair< T1, T2 > &lhs, const pair< T1, T2 > &rhs)
Greater-than operator for Kokkos::pair.
KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair< U, V > &p)
Copy constructor.
Definition: Kokkos_Pair.hpp:84
Definition: dummy.cpp:17
second_type second
The second element of the pair.
Definition: Kokkos_Pair.hpp:52
KOKKOS_FORCEINLINE_FUNCTION pair< T1, T2 > & operator=(const pair< U, V > &p)
Assignment operator.