RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
sort.hpp
Go to the documentation of this file.
1 
11 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
12 // Copyright (c) Lawrence Livermore National Security, LLC and other
13 // RAJA Project Developers. See top-level LICENSE and COPYRIGHT
14 // files for dates and other details. No copyright assignment is required
15 // to contribute to RAJA.
16 //
17 // SPDX-License-Identifier: (BSD-3-Clause)
18 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
19 
20 #ifndef RAJA_sort_sequential_HPP
21 #define RAJA_sort_sequential_HPP
22 
23 #include "RAJA/config.hpp"
24 
25 #include <algorithm>
26 #include <functional>
27 #include <iterator>
28 
29 #include "RAJA/util/macros.hpp"
30 
31 #include "RAJA/util/concepts.hpp"
32 
33 #include "RAJA/util/zip.hpp"
34 
35 #include "RAJA/util/sort.hpp"
36 
38 
39 namespace RAJA
40 {
41 namespace impl
42 {
43 namespace sort
44 {
45 
46 namespace detail
47 {
48 
54 {
55  template<typename... Args>
56  RAJA_INLINE void operator()(Args&&... args) const
57  {
58  RAJA::detail::intro_sort(std::forward<Args>(args)...);
59  }
60 };
61 
67 {
68  template<typename... Args>
69  RAJA_INLINE void operator()(Args&&... args) const
70  {
71  RAJA::detail::merge_sort(std::forward<Args>(args)...);
72  }
73 };
74 
75 } // namespace detail
76 
80 template<typename ExecPolicy, typename Iter, typename Compare>
81 concepts::enable_if_t<resources::EventProxy<resources::Host>,
83 unstable(resources::Host host_res,
84  const ExecPolicy&,
85  Iter begin,
86  Iter end,
87  Compare comp)
88 {
89  detail::UnstableSorter {}(begin, end, comp);
90 
91  return resources::EventProxy<resources::Host>(host_res);
92 }
93 
97 template<typename ExecPolicy, typename Iter, typename Compare>
98 concepts::enable_if_t<resources::EventProxy<resources::Host>,
100 stable(resources::Host host_res,
101  const ExecPolicy&,
102  Iter begin,
103  Iter end,
104  Compare comp)
105 {
106  detail::StableSorter {}(begin, end, comp);
107 
108  return resources::EventProxy<resources::Host>(host_res);
109 }
110 
114 template<typename ExecPolicy,
115  typename KeyIter,
116  typename ValIter,
117  typename Compare>
118 concepts::enable_if_t<resources::EventProxy<resources::Host>,
120 unstable_pairs(resources::Host host_res,
121  const ExecPolicy&,
122  KeyIter keys_begin,
123  KeyIter keys_end,
124  ValIter vals_begin,
125  Compare comp)
126 {
127  auto begin = RAJA::zip(keys_begin, vals_begin);
128  auto end = RAJA::zip(keys_end, vals_begin + (keys_end - keys_begin));
129  using zip_ref = RAJA::detail::IterRef<camp::decay<decltype(begin)>>;
130  detail::UnstableSorter {}(begin, end, RAJA::compare_first<zip_ref>(comp));
131 
132  return resources::EventProxy<resources::Host>(host_res);
133 }
134 
139 template<typename ExecPolicy,
140  typename KeyIter,
141  typename ValIter,
142  typename Compare>
143 concepts::enable_if_t<resources::EventProxy<resources::Host>,
145 stable_pairs(resources::Host host_res,
146  const ExecPolicy&,
147  KeyIter keys_begin,
148  KeyIter keys_end,
149  ValIter vals_begin,
150  Compare comp)
151 {
152  auto begin = RAJA::zip(keys_begin, vals_begin);
153  auto end = RAJA::zip(keys_end, vals_begin + (keys_end - keys_begin));
154  using zip_ref = RAJA::detail::IterRef<camp::decay<decltype(begin)>>;
155  detail::StableSorter {}(begin, end, RAJA::compare_first<zip_ref>(comp));
156 
157  return resources::EventProxy<resources::Host>(host_res);
158 }
159 
160 } // namespace sort
161 
162 } // namespace impl
163 
164 } // namespace RAJA
165 
166 #endif
Header file for RAJA concept definitions.
Header file for common RAJA internal macro definitions.
Args args
Definition: WorkRunner.hpp:212
typename ::std::iterator_traits< Iter >::reference IterRef
Definition: algorithm.hpp:41
RAJA_INLINE void merge_sort(Iter begin, Iter end, Compare comp)
stable merge sort given range inplace using comparison function and using O(N*lg(N)) comparisons and ...
Definition: sort.hpp:579
RAJA_HOST_DEVICE void intro_sort(Iter begin, Iter end, Compare comp)
unstable intro sort given range inplace using comparison function and using O(N*lg(N)) comparisons an...
Definition: sort.hpp:417
concepts::enable_if_t< resources::EventProxy< resources::Host >, type_traits::is_openmp_policy< ExecPolicy > > stable_pairs(resources::Host host_res, const ExecPolicy &, KeyIter keys_begin, KeyIter keys_end, ValIter vals_begin, Compare comp)
Definition: sort.hpp:276
concepts::enable_if_t< resources::EventProxy< resources::Host >, type_traits::is_openmp_policy< ExecPolicy > > stable(resources::Host host_res, const ExecPolicy &, Iter begin, Iter end, Compare comp)
stable sort given range using comparison function
Definition: sort.hpp:230
concepts::enable_if_t< resources::EventProxy< resources::Host >, type_traits::is_openmp_policy< ExecPolicy > > unstable(resources::Host host_res, const ExecPolicy &, Iter begin, Iter end, Compare comp)
sort given range using comparison function
Definition: sort.hpp:213
concepts::enable_if_t< resources::EventProxy< resources::Host >, type_traits::is_openmp_policy< ExecPolicy > > unstable_pairs(resources::Host host_res, const ExecPolicy &, KeyIter keys_begin, KeyIter keys_end, ValIter vals_begin, Compare comp)
sort given range of pairs using comparison function on keys
Definition: sort.hpp:250
concepts::enable_if_t< resources::EventProxy< Res >, type_traits::is_execution_policy< ExecPolicy >, type_traits::is_resource< Res >, std::is_constructible< camp::resources::Resource, Res >, type_traits::is_range< Container > > sort(ExecPolicy &&p, Res r, Container &&c, Compare comp=Compare {})
sort execution pattern
Definition: sort.hpp:61
Definition: AlignedRangeIndexSetBuilders.cpp:35
RAJA_HOST_DEVICE constexpr RAJA_INLINE auto zip(Args &&... args) -> ZipIterator< camp::decay< Args >... >
Zip multiple iterators together to iterate them simultaneously with a single ZipIterator object.
Definition: zip.hpp:242
Header file containing RAJA sequential policy definitions.
Functional that performs a stable sort with the given arguments, calls RAJA::merge_sort.
Definition: sort.hpp:67
RAJA_INLINE void operator()(Args &&... args) const
Definition: sort.hpp:69
Functional that performs an unstable sort with the given arguments, uses RAJA::intro_sort.
Definition: sort.hpp:54
RAJA_INLINE void operator()(Args &&... args) const
Definition: sort.hpp:56
Definition: IndexSet.hpp:70
Definition: PolicyBase.hpp:207
Tuple used by ZipIterator for storing multiple references and values. Acts like a reference to its me...
Definition: zip_tuple.hpp:247
Header file providing RAJA sort templates.
Header file for multi-iterator Zip Views.