FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
string-sorting/external/parallel_string_radix_sort.h at master · rantala/string-sorting · GitHub
rantala
/
string-sorting
Public
Notifications
You must be signed in to change notification settings
Fork
15
Star
58
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
string-sorting
/
external
/
parallel_string_radix_sort.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
415 lines (358 loc) · 11.6 KB
Breadcrumbs
string-sorting
/
external
/
parallel_string_radix_sort.h
Copy path
File metadata and controls
415 lines (358 loc) · 11.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//
Copyright 2011, Takuya Akiba
//
All rights reserved.
//
//
Redistribution and use in source and binary forms, with or without
//
modification, are permitted provided that the following conditions are
//
met:
//
//
* Redistributions of source code must retain the above copyright
//
notice, this list of conditions and the following disclaimer.
//
* Redistributions in binary form must reproduce the above
//
copyright notice, this list of conditions and the following disclaimer
//
in the documentation and/or other materials provided with the
//
distribution.
//
* Neither the name of Takuya Akiba nor the names of its
//
contributors may be used to endorse or promote products derived from
//
this software without specific prior written permission.
//
//
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
//
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
//
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
//
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
//
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
//
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
//
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
ifndef
PARALLEL_STRING_RADIX_SORT_H_
#
define
PARALLEL_STRING_RADIX_SORT_H_
//
#ifdef _OPENMP
//
#include <omp.h>
//
#endif
#
include
<
stdint.h
>
#
include
<
algorithm
>
#
include
<
cassert
>
#
include
<
cstdio
>
#
include
<
cstdlib
>
#
include
<
cstring
>
#
include
<
string
>
#
define
PSRS_CHECK
(
expr
) \
if
(expr) { \
}
else
{ \
fprintf
(stderr,
"
CHECK Failed(%s:%d): %s
\n
"
, \
__FILE__, __LINE__, #expr); \
exit
(
EXIT_FAILURE
); \
}
namespace
parallel_string_radix_sort
{
namespace
internal
{
/*
*
* Comparison function class used in |ParallelStringRadixSort|.
*/
template
<
typename
StringType>
class
Compare
;
template
<>
class
Compare
<
const
char
*> {
public:
explicit
Compare
(
int
depth) : depth_(depth) {}
inline
bool
operator
()(
const
char
*
const
a,
const
char
*
const
b) {
return
strcmp
(a + depth_, b + depth_) <
0
;
}
private:
int
depth_;
};
template
<>
class
Compare
<std::string> {
public:
explicit
Compare
(
int
depth) : depth_(depth) {}
inline
bool
operator
()(
const
std::string &a,
const
std::string &b) {
return
a.
compare
(depth_, a.
length
() - depth_,
b, depth_, b.
length
() - depth_) <
0
;
}
private:
int
depth_;
};
}
//
namespace internal
/*
*
* The class to perform string sorting.
*
* @param StringType The type of the strings to be sorted.
* This should be either of |const char*| or |std::string|.
*/
template
<
typename
StringType>
class
ParallelStringRadixSort
{
public:
ParallelStringRadixSort
();
~ParallelStringRadixSort
();
/*
*
* Initialize the class.
*
* @param max_elems the maximum number of the elements to be sorted
*/
void
Init
(
size_t
max_elems);
/*
*
* Sort an array of strings.
*
* @param strings the array to be sorted
* @param num_elems the number of the elements in the given array
*/
void
Sort
(StringType *strings,
size_t
num_elems);
private:
//
/ The theshold of size of ranges to call |std::sort|
static
const
size_t
kThreshold
=
30
;
//
/ The limit of depth to call |std::sort|
static
const
size_t
kDepthLimit
=
100
;
size_t
max_elems_;
StringType *data_, *temp_;
uint8_t
*letters8_;
uint16_t
*letters16_;
/*
*
* Release all the allocated resources.
*/
void
DeleteAll
();
/*
*
* Sort elements in range [bgn, end),
* which have common prefix with |depth| characters.
*
* @param flip When false, read from |data_|, write to |temp_| and recurse.
* When true, read from |temp_|, write to |data_| and recurse.
*/
void
Sort8
(
size_t
bgn,
size_t
end,
size_t
depth,
bool
flip);
/*
*
* Sort elements in range [bgn, end),
* which have common prefix with |depth| characters.
*
* Treat pairs of characters as single super characters.
*
* @param flip When false, read from |data_|, write to |temp_| and recurse.
* When true, read from |temp_|, write to |data_| and recurse.
*/
void
Sort16
(
size_t
bgn,
size_t
end,
size_t
depth,
bool
flip);
/*
*
* Sort elements in range [bgn, end),
* which have common prefix with |depth| characters.
*
* Treat pairs of characters as single super characters.
*
* Builds threads and recurse in parallel.
*
* @param flip When false, read from |data_|, write to |temp_| and recurse.
* When true, read from |temp_|, write to |data_| and recurse.
*/
void
Sort16Parallel
(
size_t
bgn,
size_t
end,
size_t
depth,
bool
flip);
/*
*
* Sort elements in range [bgn, end),
* which have common prefix with |depth| characters.
*
* The size of the range determines the action of this function.
*
* If the size is sufficiently small (smaller than or equal to |kThreshold|),
* we call |std::sort| complete the sorting.
* In doing so, regardless of |flip|, write the result to |data_|.
*
* If the size is smaller than |1 << 16| then we call |Sort8|,
* otherwise we call |Sort16|.
*
* If the |depth| is larget than or equal to |kDepthLimit|,
* we also use |std::sort|. This is to avoid stack overflow.
*/
inline
void
Recurse
(
size_t
bgn,
size_t
end,
size_t
depth,
bool
flip) {
size_t
n = end - bgn;
if
(depth >=
kDepthLimit
|| n <=
kThreshold
) {
if
(flip) {
for
(
size_t
i = bgn; i < end; ++i) {
std::swap
(data_[i], temp_[i]);
}
}
if
(n >
1
) {
std::sort
(data_ + bgn, data_ + end, internal::Compare<StringType>(depth));
}
}
else
if
(n < (
1
<<
16
)) {
Sort8
(bgn, end, depth, flip);
}
else
{
Sort16
(bgn, end, depth, flip);
}
}
};
template
<
typename
StringType>
ParallelStringRadixSort<StringType>::ParallelStringRadixSort()
: max_elems_(
0
), temp_(
NULL
), letters8_(
NULL
), letters16_(
NULL
) {}
template
<
typename
StringType>
ParallelStringRadixSort<StringType>::
~ParallelStringRadixSort
() {
DeleteAll
();
}
template
<
typename
StringType>
void
ParallelStringRadixSort<StringType>::Init(
size_t
max_elems) {
DeleteAll
();
max_elems_ = max_elems;
temp_ =
new
StringType[max_elems];
letters8_ =
new
uint8_t
[max_elems];
letters16_ =
new
uint16_t
[max_elems];
PSRS_CHECK
(temp_ !=
NULL
&& letters8_ !=
NULL
&& letters16_ !=
NULL
);
}
template
<
typename
StringType>
void
ParallelStringRadixSort<StringType>::DeleteAll() {
delete []
temp_;
delete []
letters8_;
delete []
letters16_;
max_elems_ =
0
;
temp_ =
NULL
;
letters8_ =
NULL
;
letters16_ =
NULL
;
}
template
<
typename
StringType>
void
ParallelStringRadixSort<StringType>
::Sort
(StringType *strings,
size_t
num_elems) {
assert
(num_elems <= max_elems_);
data_ = strings;
Sort16Parallel
(
0
, num_elems,
0
,
false
);
}
template
<
typename
StringType>
void
ParallelStringRadixSort<StringType>
::Sort8
(
size_t
bgn,
size_t
end,
size_t
depth,
bool
flip) {
//
Here we do not use |new| or |malloc|. This is because:
//
1. these may be heavy bottle-necks under multi-thread, and
//
2. the size is sufficiently small for preventing stack overflow.
size_t
cnt[
1
<<
8
] = {};
StringType *src = (flip ? temp_ : data_) + bgn;
StringType *dst = (flip ? data_ : temp_) + bgn;
uint8_t
*let = letters8_ + bgn;
size_t
n = end - bgn;
for
(
size_t
i =
0
; i < n; ++i) {
let[i] = src[i][depth];
}
for
(
size_t
i =
0
; i < n; ++i) {
++cnt[let[i]];
}
size_t
s =
0
;
for
(
int
i =
0
; i <
1
<<
8
; ++i) {
std::swap
(cnt[i], s);
s += cnt[i];
}
for
(
size_t
i =
0
; i < n; ++i) {
std::swap
(dst[cnt[let[i]]++], src[i]);
}
if
(flip ==
false
) {
size_t
b =
0
, e = cnt[
0
];
for
(
size_t
j = b; j < e; ++j) {
std::swap
(src[j], dst[j]);
}
}
for
(
size_t
i =
1
; i <
1
<<
8
; ++i) {
if
(cnt[i] - cnt[i -
1
] >=
1
) {
Recurse
(bgn + cnt[i -
1
], bgn + cnt[i], depth +
1
, !flip);
}
}
}
template
<
typename
StringType>
void
ParallelStringRadixSort<StringType>
::Sort16
(
size_t
bgn,
size_t
end,
size_t
depth,
bool
flip) {
size_t
*cnt =
new
size_t
[
1
<<
16
]();
PSRS_CHECK
(cnt !=
NULL
);
StringType *src = (flip ? temp_ : data_) + bgn;
StringType *dst = (flip ? data_ : temp_) + bgn;
uint16_t
*let = letters16_ + bgn;
size_t
n = end - bgn;
for
(
size_t
i =
0
; i < n; ++i) {
uint16_t
x = src[i][depth];
let[i] = x ==
0
?
0
: ((x <<
8
) | src[i][depth +
1
]);
}
for
(
size_t
i =
0
; i < n; ++i) {
++cnt[let[i]];
}
size_t
s =
0
;
for
(
int
i =
0
; i <
1
<<
16
; ++i) {
std::swap
(cnt[i], s);
s += cnt[i];
}
for
(
size_t
i =
0
; i < n; ++i) {
std::swap
(dst[cnt[let[i]]++], src[i]);
}
if
(flip ==
false
) {
for
(
int
i =
0
; i <
1
<<
8
; ++i) {
size_t
b = i ==
0
?
0
: cnt[(i <<
8
) -
1
];
size_t
e = cnt[i <<
8
];
for
(
size_t
j = b; j < e; ++j) {
std::swap
(src[j], dst[j]);
}
}
}
for
(
size_t
i =
1
; i <
1
<<
16
; ++i) {
if
((i &
0xFF
) !=
0
&& cnt[i] - cnt[i -
1
] >=
1
) {
Recurse
(bgn + cnt[i -
1
], bgn + cnt[i], depth +
2
, !flip);
}
}
delete []
cnt;
}
template
<
typename
StringType>
void
ParallelStringRadixSort<StringType>
::Sort16Parallel
(
size_t
bgn,
size_t
end,
size_t
depth,
bool
flip) {
size_t
cnt[
1
<<
16
] = {};
StringType *src = (flip ? temp_ : data_) + bgn;
StringType *dst = (flip ? data_ : temp_) + bgn;
uint16_t
*let = letters16_ + bgn;
size_t
n = end - bgn;
#
ifdef
_OPENMP
#
pragma
omp parallel for schedule(static)
#
endif
for
(
size_t
i =
0
; i < n; ++i) {
uint16_t
x = src[i][depth];
let[i] = x ==
0
?
0
: ((x <<
8
) | src[i][depth +
1
]);
}
for
(
size_t
i =
0
; i < n; ++i) {
++cnt[let[i]];
}
{
size_t
s =
0
;
for
(
int
i =
0
; i <
1
<<
16
; ++i) {
std::swap
(cnt[i], s);
s += cnt[i];
}
}
for
(
size_t
i =
0
; i < n; ++i) {
std::swap
(dst[cnt[let[i]]++], src[i]);
}
if
(flip ==
false
) {
#
ifdef
_OPENMP
#
pragma
omp parallel for schedule(static)
#
endif
for
(
int
i =
0
; i <
1
<<
8
; ++i) {
size_t
b = i ==
0
?
0
: cnt[(i <<
8
) -
1
];
size_t
e = cnt[i <<
8
];
for
(
size_t
j = b; j < e; ++j) {
src[j] = dst[j];
}
}
}
#
ifdef
_OPENMP
#
pragma
omp parallel for schedule(dynamic)
#
endif
for
(
size_t
i =
1
; i <
1
<<
16
; ++i) {
if
((i &
0xFF
) !=
0
&& cnt[i] - cnt[i -
1
] >=
1
) {
Recurse
(bgn + cnt[i -
1
], bgn + cnt[i], depth +
2
, !flip);
}
}
}
/*
*
* Sort an array of strings.
*
* This function automatically initialize an instance of
* |ParallelStringRadixSort| and perform sorting.
*
* If you perform sorting more than once, you can avoid the cost of
* initialization using |ParallelStringRadixSort| class by yourself.
*
* @param strings the array to be sorted
* @param num_elems the number of the elements in the given array
*/
template
<
typename
StringType>
void
Sort
(StringType *strings,
size_t
num_elems) {
ParallelStringRadixSort<StringType> psrs;
psrs.
Init
(num_elems);
psrs.
Sort
(strings, num_elems);
}
template
<
typename
StringType,
size_t
kNumElems
>
void
Sort
(StringType (&strings)[kNumElems]) {
Sort
(strings,
kNumElems
);
}
}
//
namespace parallel_string_radix_sort
#
undef
PSRS_CHECK
#
endif
//
PARALLEL_STRING_RADIX_SORT_H_
Back
|
FazBrowse Home
|
New Git URL