1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/*
* Copyright 2008-2013 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <thrust/detail/config.h>
// don't attempt to #include this file without omp support
#if (THRUST_DEVICE_COMPILER_IS_OMP_CAPABLE == THRUST_TRUE)
#include <omp.h>
#endif // omp support
#include <thrust/iterator/iterator_traits.h>
#include <thrust/system/omp/detail/default_decomposition.h>
#include <thrust/system/detail/generic/select_system.h>
#include <thrust/sort.h>
#include <thrust/merge.h>
#include <thrust/detail/seq.h>
#include <thrust/detail/temporary_array.h>
THRUST_NAMESPACE_BEGIN
namespace system
{
namespace omp
{
namespace detail
{
namespace sort_detail
{
template<typename DerivedPolicy,
typename RandomAccessIterator,
typename StrictWeakOrdering>
void inplace_merge(execution_policy<DerivedPolicy> &exec,
RandomAccessIterator first,
RandomAccessIterator middle,
RandomAccessIterator last,
StrictWeakOrdering comp)
{
typedef typename thrust::iterator_value<RandomAccessIterator>::type value_type;
thrust::detail::temporary_array<value_type,DerivedPolicy> a(exec, first, middle);
thrust::detail::temporary_array<value_type,DerivedPolicy> b(exec, middle, last);
thrust::merge(thrust::seq, a.begin(), a.end(), b.begin(), b.end(), first, comp);
}
template<typename DerivedPolicy,
typename RandomAccessIterator1,
typename RandomAccessIterator2,
typename StrictWeakOrdering>
void inplace_merge_by_key(execution_policy<DerivedPolicy> &exec,
RandomAccessIterator1 first1,
RandomAccessIterator1 middle1,
RandomAccessIterator1 last1,
RandomAccessIterator2 first2,
StrictWeakOrdering comp)
{
typedef typename thrust::iterator_value<RandomAccessIterator1>::type value_type1;
typedef typename thrust::iterator_value<RandomAccessIterator2>::type value_type2;
RandomAccessIterator2 middle2 = first2 + (middle1 - first1);
RandomAccessIterator2 last2 = first2 + (last1 - first1);
thrust::detail::temporary_array<value_type1,DerivedPolicy> lhs1(exec, first1, middle1);
thrust::detail::temporary_array<value_type1,DerivedPolicy> rhs1(exec, middle1, last1);
thrust::detail::temporary_array<value_type2,DerivedPolicy> lhs2(exec, first2, middle2);
thrust::detail::temporary_array<value_type2,DerivedPolicy> rhs2(exec, middle2, last2);
thrust::merge_by_key(thrust::seq,
lhs1.begin(), lhs1.end(),
rhs1.begin(), rhs1.end(),
lhs2.begin(), rhs2.begin(),
first1, first2,
comp);
}
} // end sort_detail
template<typename DerivedPolicy,
typename RandomAccessIterator,
typename StrictWeakOrdering>
void stable_sort(execution_policy<DerivedPolicy> &exec,
RandomAccessIterator first,
RandomAccessIterator last,
StrictWeakOrdering comp)
{
// we're attempting to launch an omp kernel, assert we're compiling with omp support
// ========================================================================
// X Note to the user: If you've found this line due to a compiler error, X
// X you need to enable OpenMP support in your compiler. X
// ========================================================================
THRUST_STATIC_ASSERT_MSG(
(thrust::detail::depend_on_instantiation<
RandomAccessIterator, (THRUST_DEVICE_COMPILER_IS_OMP_CAPABLE == THRUST_TRUE)
>::value)
, "OpenMP compiler support is not enabled"
);
// Avoid issues on compilers that don't provide `omp_get_num_threads()`.
#if (THRUST_DEVICE_COMPILER_IS_OMP_CAPABLE == THRUST_TRUE)
typedef typename thrust::iterator_difference<RandomAccessIterator>::type IndexType;
if(first == last)
return;
THRUST_PRAGMA_OMP(parallel)
{
thrust::system::detail::internal::uniform_decomposition<IndexType> decomp(last - first, 1, omp_get_num_threads());
// process id
IndexType p_i = omp_get_thread_num();
// every thread sorts its own tile
if(p_i < decomp.size())
{
thrust::stable_sort(thrust::seq,
first + decomp[p_i].begin(),
first + decomp[p_i].end(),
comp);
}
THRUST_PRAGMA_OMP(barrier)
// XXX For some reason, MSVC 2015 yields an error unless we include this meaningless semicolon here
;
IndexType nseg = decomp.size();
IndexType h = 2;
// keep track of which sub-range we're processing
IndexType a=p_i, b=p_i, c=p_i+1;
while(nseg>1)
{
if(c >= decomp.size())
c = decomp.size() - 1;
if((p_i % h) == 0 && c > b)
{
sort_detail::inplace_merge(exec,
first + decomp[a].begin(),
first + decomp[b].end(),
first + decomp[c].end(),
comp);
b = c;
c += h;
}
nseg = (nseg + 1) / 2;
h *= 2;
THRUST_PRAGMA_OMP(barrier)
}
}
#endif // THRUST_DEVICE_COMPILER_IS_OMP_CAPABLE
}
template<typename DerivedPolicy,
typename RandomAccessIterator1,
typename RandomAccessIterator2,
typename StrictWeakOrdering>
void stable_sort_by_key(execution_policy<DerivedPolicy> &exec,
RandomAccessIterator1 keys_first,
RandomAccessIterator1 keys_last,
RandomAccessIterator2 values_first,
StrictWeakOrdering comp)
{
// we're attempting to launch an omp kernel, assert we're compiling with omp support
// ========================================================================
// X Note to the user: If you've found this line due to a compiler error, X
// X you need to enable OpenMP support in your compiler. X
// ========================================================================
THRUST_STATIC_ASSERT_MSG(
(thrust::detail::depend_on_instantiation<
RandomAccessIterator1, (THRUST_DEVICE_COMPILER_IS_OMP_CAPABLE == THRUST_TRUE)
>::value)
, "OpenMP compiler support is not enabled"
);
// Avoid issues on compilers that don't provide `omp_get_num_threads()`.
#if (THRUST_DEVICE_COMPILER_IS_OMP_CAPABLE == THRUST_TRUE)
typedef typename thrust::iterator_difference<RandomAccessIterator1>::type IndexType;
if(keys_first == keys_last)
return;
THRUST_PRAGMA_OMP(parallel)
{
thrust::system::detail::internal::uniform_decomposition<IndexType> decomp(keys_last - keys_first, 1, omp_get_num_threads());
// process id
IndexType p_i = omp_get_thread_num();
// every thread sorts its own tile
if(p_i < decomp.size())
{
thrust::stable_sort_by_key(thrust::seq,
keys_first + decomp[p_i].begin(),
keys_first + decomp[p_i].end(),
values_first + decomp[p_i].begin(),
comp);
}
THRUST_PRAGMA_OMP(barrier)
// XXX For some reason, MSVC 2015 yields an error unless we include this meaningless semicolon here
;
IndexType nseg = decomp.size();
IndexType h = 2;
// keep track of which sub-range we're processing
IndexType a=p_i, b=p_i, c=p_i+1;
while(nseg>1)
{
if(c >= decomp.size())
c = decomp.size() - 1;
if((p_i % h) == 0 && c > b)
{
sort_detail::inplace_merge_by_key(exec,
keys_first + decomp[a].begin(),
keys_first + decomp[b].end(),
keys_first + decomp[c].end(),
values_first + decomp[a].begin(),
comp);
b = c;
c += h;
}
nseg = (nseg + 1) / 2;
h *= 2;
THRUST_PRAGMA_OMP(barrier)
}
}
#endif // THRUST_DEVICE_COMPILER_IS_OMP_CAPABLE
}
} // end namespace detail
} // end namespace omp
} // end namespace system
THRUST_NAMESPACE_END
|