Viskores  1.0
ParallelRadixSortInterface.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 
19 #ifndef viskores_cont_internal_ParallelRadixSortInterface_h
20 #define viskores_cont_internal_ParallelRadixSortInterface_h
21 
24 
25 #include <functional>
26 #include <type_traits>
27 
28 namespace viskores
29 {
30 namespace cont
31 {
32 namespace internal
33 {
34 namespace radix
35 {
36 
37 const size_t MIN_BYTES_FOR_PARALLEL = 400000;
38 const size_t BYTES_FOR_MAX_PARALLELISM = 4000000;
39 
40 struct RadixSortTag
41 {
42 };
43 
44 struct PSortTag
45 {
46 };
47 
48 // Detect supported functors for radix sort:
49 template <typename T>
50 struct is_valid_compare_type : std::integral_constant<bool, false>
51 {
52 };
53 template <typename T>
54 struct is_valid_compare_type<std::less<T>> : std::integral_constant<bool, true>
55 {
56 };
57 template <typename T>
58 struct is_valid_compare_type<std::greater<T>> : std::integral_constant<bool, true>
59 {
60 };
61 template <>
62 struct is_valid_compare_type<viskores::SortLess> : std::integral_constant<bool, true>
63 {
64 };
65 template <>
66 struct is_valid_compare_type<viskores::SortGreater> : std::integral_constant<bool, true>
67 {
68 };
69 
70 // Convert viskores::Sort[Less|Greater] to the std:: equivalents:
71 template <typename BComp, typename T>
72 BComp&& get_std_compare(BComp&& b, T&&)
73 {
74  return std::forward<BComp>(b);
75 }
76 template <typename T>
77 std::less<T> get_std_compare(viskores::SortLess, T&&)
78 {
79  return std::less<T>{};
80 }
81 template <typename T>
82 std::greater<T> get_std_compare(viskores::SortGreater, T&&)
83 {
84  return std::greater<T>{};
85 }
86 
87 // Determine if radix sort can be used for a given ValueType, StorageType, and
88 // comparison functor.
89 template <typename T, typename StorageTag, typename BinaryCompare>
90 struct sort_tag_type
91 {
92  using type = PSortTag;
93 };
94 template <typename T, typename BinaryCompare>
95 struct sort_tag_type<T, viskores::cont::StorageTagBasic, BinaryCompare>
96 {
97  using PrimT = std::is_arithmetic<T>;
98  using LongDT = std::is_same<T, long double>;
99  using BComp = is_valid_compare_type<BinaryCompare>;
100  using type = typename std::
101  conditional<PrimT::value && BComp::value && !LongDT::value, RadixSortTag, PSortTag>::type;
102 };
103 
104 template <typename KeyType,
105  typename ValueType,
106  typename KeyStorageTagType,
107  typename ValueStorageTagType,
108  class BinaryCompare>
109 struct sortbykey_tag_type
110 {
111  using type = PSortTag;
112 };
113 template <typename KeyType, typename ValueType, class BinaryCompare>
114 struct sortbykey_tag_type<KeyType,
115  ValueType,
116  viskores::cont::StorageTagBasic,
118  BinaryCompare>
119 {
120  using PrimKey = std::is_arithmetic<KeyType>;
121  using PrimValue = std::is_arithmetic<ValueType>;
122  using LongDKey = std::is_same<KeyType, long double>;
123  using BComp = is_valid_compare_type<BinaryCompare>;
124  using type = typename std::conditional<PrimKey::value && PrimValue::value && BComp::value &&
125  !LongDKey::value,
126  RadixSortTag,
127  PSortTag>::type;
128 };
129 
130 #define VISKORES_INTERNAL_RADIX_SORT_DECLARE(key_type) \
131  VISKORES_CONT_EXPORT void parallel_radix_sort( \
132  key_type* data, size_t num_elems, const std::greater<key_type>& comp); \
133  VISKORES_CONT_EXPORT void parallel_radix_sort( \
134  key_type* data, size_t num_elems, const std::less<key_type>& comp); \
135  VISKORES_CONT_EXPORT void parallel_radix_sort_key_values( \
136  key_type* keys, viskores::Id* vals, size_t num_elems, const std::greater<key_type>& comp); \
137  VISKORES_CONT_EXPORT void parallel_radix_sort_key_values( \
138  key_type* keys, viskores::Id* vals, size_t num_elems, const std::less<key_type>& comp);
139 
140 // Generate radix sort interfaces for key and key value sorts.
141 #define VISKORES_DECLARE_RADIX_SORT() \
142  VISKORES_INTERNAL_RADIX_SORT_DECLARE(short int) \
143  VISKORES_INTERNAL_RADIX_SORT_DECLARE(unsigned short int) \
144  VISKORES_INTERNAL_RADIX_SORT_DECLARE(int) \
145  VISKORES_INTERNAL_RADIX_SORT_DECLARE(unsigned int) \
146  VISKORES_INTERNAL_RADIX_SORT_DECLARE(long int) \
147  VISKORES_INTERNAL_RADIX_SORT_DECLARE(unsigned long int) \
148  VISKORES_INTERNAL_RADIX_SORT_DECLARE(long long int) \
149  VISKORES_INTERNAL_RADIX_SORT_DECLARE(unsigned long long int) \
150  VISKORES_INTERNAL_RADIX_SORT_DECLARE(unsigned char) \
151  VISKORES_INTERNAL_RADIX_SORT_DECLARE(signed char) \
152  VISKORES_INTERNAL_RADIX_SORT_DECLARE(char) \
153  VISKORES_INTERNAL_RADIX_SORT_DECLARE(char16_t) \
154  VISKORES_INTERNAL_RADIX_SORT_DECLARE(char32_t) \
155  VISKORES_INTERNAL_RADIX_SORT_DECLARE(wchar_t) \
156  VISKORES_INTERNAL_RADIX_SORT_DECLARE(float) \
157  VISKORES_INTERNAL_RADIX_SORT_DECLARE(double)
158 }
159 }
160 }
161 } // end viskores::cont::internal::radix
162 
163 #endif // viskores_cont_internal_ParallelRadixSortInterface_h
ArrayHandle.h
viskores::SortLess
Binary Predicate that takes two arguments argument x, and y and returns True if and only if x is less...
Definition: BinaryPredicates.h:53
viskores::cont::StorageTagBasic
A tag for the basic implementation of a Storage object.
Definition: ArrayHandle.h:53
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
BinaryPredicates.h
viskores::SortGreater
Binary Predicate that takes two arguments argument x, and y and returns True if and only if x is grea...
Definition: BinaryPredicates.h:66