-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsort.hpp
192 lines (182 loc) · 5.27 KB
/
sort.hpp
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
/**
* @file sort.hpp
* @author Wuqiong Zhao ([email protected]), et al.
* @brief Sorting for FLAMES
* @version 0.1.0
* @date 2023-07-15
*
* @copyright Copyright (c) 2023 Wuqiong Zhao
*
*/
#ifndef _FLAMES_SORT_HPP_
#define _FLAMES_SORT_HPP_
#ifndef _FLAMES_CORE_HPP_
# include "core.hpp"
#endif
namespace flames {
template <typename V>
static void mergeSort(V& vec) {
constexpr size_t size = V::size();
typename V::value_type temp[size];
MERGE_SORT_STAGE:
for (int width = 1; width < size; width *= 2) {
int f1 = 0;
int f2 = width;
int i2 = width;
int i3 = 2 * width;
if (i2 >= size) i2 = size;
if (i3 >= size) i3 = size;
MERGE_ARRAYS:
for (int i = 0; i < size; ++i) {
FLAMES_PRAGMA(PIPELINE II = 1)
typename V::value_type t1 = vec[f1];
typename V::value_type t2 = (f2 == i3) ? static_cast<typename V::value_type>(0) : vec[f2];
if (f2 == i3 || (f1 < i2 && t1 <= t2)) {
temp[i] = t1;
++f1;
} else {
assert(f2 < i3);
temp[i] = t2;
++f2;
}
if (f1 == i2 && f2 == i3) {
f1 = i3;
i2 += 2 * width;
i3 += 2 * width;
if (i2 >= size) i2 = size;
if (i3 >= size) i3 = size;
f2 = i2;
}
}
MERGE_SORT_COPY:
for (int i = 0; i < size; ++i) {
FLAMES_PRAGMA(UNROLL factor = FLAMES_MAT_COPY_UNROLL_FACTOR)
vec[i] = temp[i];
}
}
}
template <typename V1, typename V2>
static void mergeSort(const V1& in, V2& out) {
constexpr size_t size = V1::size();
static_assert(size == V2::size(), "Sort in and out vectors/matrices should be of same size.");
typename V1::value_type temp[size];
MERGE_SORT_STAGE:
for (int width = 1; width < size; width *= 2) {
int f1 = 0;
int f2 = width;
int i2 = width;
int i3 = 2 * width;
if (i2 >= size) i2 = size;
if (i3 >= size) i3 = size;
MERGE_ARRAYS:
for (int i = 0; i < size; ++i) {
FLAMES_PRAGMA(PIPELINE II = 1)
typename V1::value_type t1 = in[f1];
// TODO: here minimum is zero
typename V1::value_type t2 = (f2 == i3) ? static_cast<typename V1::value_type>(0) : in[f2];
if (f2 == i3 || (f1 < i2 && t1 <= t2)) {
out[i] = t1;
++f1;
} else {
assert(f2 < i3);
out[i] = t2;
++f2;
}
if (f1 == i2 && f2 == i3) {
f1 = i3;
i2 += 2 * width;
i3 += 2 * width;
if (i2 >= size) i2 = size;
if (i3 >= size) i3 = size;
f2 = i2;
}
}
}
}
template <typename V>
static inline void sort(V& vec) {
return mergeSort(vec);
}
template <typename V1, typename V2>
static inline void sort(const V1& in, V2& out) {
return mergeSort(in, out);
}
template <typename T, typename I>
static inline void argmax_4_2(T in1, T in2, T in3, T in4, I i_in1, I i_in2, I i_in3, I i_in4, T& out1, T& out2,
I& i_out1, I& i_out2, bool sorted = false) {
FLAMES_PRAGMA(INLINE)
if (sorted) {
assert(in1 >= in2 && in3 >= in4 && "Should be sorted in flames::argmax_4_2 with sorted=true.");
if (in1 < in4) { // 3, 4
out1 = in3;
out2 = in4;
i_out1 = i_in3;
i_out2 = i_in4;
} else if (in3 < in2) { // 1, 2
out1 = in1;
out2 = in2;
i_out1 = i_in1;
i_out2 = i_in2;
} else if (in1 < in3) { // 3, 1
out1 = in3;
out2 = in1;
i_out1 = i_in3;
i_out2 = i_in1;
} else { // 1, 3
out1 = in1;
out2 = in3;
i_out1 = i_in1;
i_out2 = i_in3;
}
} else {
T _in1, _in2, _in3, _in4;
I _i_in1, _i_in2, _i_in3, _i_in4;
// pre sort 1 and 2
if (in1 > in2) {
_in1 = in1;
_in2 = in2;
_i_in1 = i_in1;
_i_in2 = i_in2;
} else {
_in1 = in2;
_in2 = in1;
_i_in1 = i_in2;
_i_in2 = i_in1;
}
if (in3 > in4) {
_in3 = in3;
_in4 = in4;
_i_in3 = i_in3;
_i_in4 = i_in4;
} else {
_in3 = in4;
_in4 = in3;
_i_in3 = i_in4;
_i_in4 = i_in3;
}
// now same as sorted=true
if (_in1 < _in4) { // 3, 4
out1 = _in3;
out2 = _in4;
i_out1 = _i_in3;
i_out2 = _i_in4;
} else if (_in3 < _in2) { // 1, 2
out1 = _in1;
out2 = _in2;
i_out1 = _i_in1;
i_out2 = _i_in2;
} else if (_in1 < _in3) { // 3, 1
out1 = _in3;
out2 = _in1;
i_out1 = _i_in3;
i_out2 = _i_in1;
} else { // 1, 3
out1 = _in1;
out2 = _in3;
i_out1 = _i_in1;
i_out2 = _i_in3;
}
}
}
} // namespace flames
#endif