-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSortParallel.h
More file actions
95 lines (82 loc) · 3.58 KB
/
SortParallel.h
File metadata and controls
95 lines (82 loc) · 3.58 KB
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
// TODO: Benchmark how long memory allocation takes
// TODO: Benchmark how much better algorithm does where dst/working buffer is provided, versus one that is provided and paged in
#pragma once
#include "Configuration.h"
#include <iostream>
#include <algorithm>
#include <chrono>
#include <random>
#include <ratio>
#include <vector>
#include <thread>
#include <execution>
#include "ParallelMergeSort.h"
namespace ParallelAlgorithms
{
// Sort the entire array of any data type with comparable elements
// Adaptive algorithm: if enough memory to allocate a temporary working buffer, then faster not-in-place parallel merge sort is used.
// if not enough memory, then the standard C++ in-place parallel sort is used, which is slower.
template< class _Type >
inline void sort_par(_Type* src, size_t src_size)
{
ParallelAlgorithms::sort_par(src, 0, src_size);
}
template< class _Type >
inline void sort_par(std::vector<_Type>& src)
{
ParallelAlgorithms::sort_par(src, 0, src.size());
}
// Array bounds includes l/left, but does not include r/right
template< class _Type >
inline void sort_par(_Type* src, size_t l, size_t r)
{
size_t src_size = r;
_Type* sorted = new(std::nothrow) _Type[src_size];
if (!sorted)
sort(std::execution::par_unseq, src + l, src + r);
else
{
ParallelAlgorithms::parallel_merge_sort_hybrid_rh_1(src, l, r - 1, sorted, false); // r - 1 because this algorithm wants inclusive bounds
delete[] sorted;
}
}
// Array bounds includes l/left, but does not include r/right
template< class _Type >
inline void sort_par(std::vector<_Type>& src, size_t l, size_t r)
{
try
{
size_t src_size = r;
std::vector<_Type> sorted(src_size);
ParallelAlgorithms::parallel_merge_sort_hybrid_rh_1(src.data(), l, r - 1, sorted.data(), false); // r - 1 because this algorithm wants inclusive bounds
}
catch (std::bad_alloc& ba)
{
sort(std::execution::par_unseq, src.begin() + l, src.begin() + r);
}
}
// Array bounds includes l/left, but does not include r/right
// dst buffer must be large enough to provide elements dst[0 to r-1], as the result is placed in dst[l to r-1]
// Two use cases:
// - in-place interface, where the dst buffer is a temporary work buffer
// - not-in-place interface, where the dst buffer is the destination memory buffer
template< class _Type >
inline void sort_par(_Type* src, size_t l, size_t r, _Type* dst, size_t dst_size, bool srcToDst = false)
{
if (!dst)
throw std::invalid_argument("dst is null, which is not supported");
size_t src_size = r;
if (dst_size < src_size)
throw std::invalid_argument("dst_size must be larger or equal to r, to be able to return dst[l to r-1]");
ParallelAlgorithms::parallel_merge_sort_hybrid_rh_2(src, l, r - 1, dst, srcToDst); // r - 1 because this algorithm wants inclusive bounds
}
// dst buffer must be the same or larger in size than the src
// Two use cases:
// - in-place interface, where the dst buffer is a temporary work buffer
// - not-in-place interface, where the dst buffer is the destination memory buffer
template< class _Type >
inline void sort_par(_Type* src, size_t src_size, _Type* dst, size_t dst_size, bool srcToDst = false)
{
ParallelAlgorithms::sort_par(src, (size_t)0, src_size - 1, dst, dst_size, srcToDst);
}
}