File: radix_sort.h

package info (click to toggle)
hisat2 2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,756 kB
  • sloc: cpp: 86,309; python: 12,230; sh: 2,171; perl: 936; makefile: 375
file content (297 lines) | stat: -rw-r--r-- 10,756 bytes parent folder | download | duplicates (2)
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
#ifndef RADIX_SORT_H_
#define RADIX_SORT_H_

#include <time.h>

// in place radix sort using a single thread, should not be called directly
// used for leaves of both in and out of place radix sorts
template <typename T, typename CMP, typename index_t>
static void _radix_sort(T* begin, T* end, index_t (*hash)(T&), int log_size) {
    const int SHIFT = 8;
    const int BLOCKS = (1 << (SHIFT + 1));
    const int BLOCKS_MASK = BLOCKS - 1;

    // compute maximum of log_size - 7 and 0
    int right_shift = (log_size - SHIFT) * (log_size > SHIFT);
    // count number in each bin
    index_t count[BLOCKS] = {0};
    for(T* curr = begin; curr != end; curr++) {
        count[(hash(*curr) >> right_shift) & BLOCKS_MASK]++;
    }
    // sum numbers to create an index
    T* index[BLOCKS + 1];
    T* place[BLOCKS];
    index[0] = place[0] = begin;
    for(int i = 1; i < BLOCKS; i++) {
        index[i] = place[i] = index[i - 1] + count[i - 1];
    }
    index[BLOCKS] = end;
    //put objects in proper place
    for(int bin = 0; bin < BLOCKS; bin++) {
        while(place[bin] != index[bin + 1]) {
            T curr = *place[bin];
            int x = (hash(curr) >> right_shift) & BLOCKS_MASK;
            while(x != bin) {
                T temp = *place[x];
                *place[x]++ = curr;
                curr = temp;
                x = (hash(curr) >> right_shift) & BLOCKS_MASK;
            }
            *place[bin]++ = curr;
        }
    }
    //sort partitions
    for(int bin = 0; bin < BLOCKS; bin++) {
        if(index[bin + 1] - index[bin] > 64 && right_shift) {
            _radix_sort<T, CMP, index_t>(index[bin], index[bin + 1], hash, right_shift);
        } else if (index[bin + 1] - index[bin] > 1) {
            sort(index[bin], index[bin + 1], CMP());
        }
    }
}

template <typename T, typename index_t>
struct RecurseParams {
    index_t     (*hash)(T&);
    T**         begin;
    int         log_size;
    int         num;
};

//basically used to wrap together calls to bin_sort
template <typename T, typename CMP, typename index_t>
static void _radix_sort_worker(void* vp) {
    RecurseParams<T, index_t>* params = (RecurseParams<T, index_t>*)vp;
    index_t (*hash)(T&) = params->hash;
    T**     begin       = params->begin;
    int     log_size    = params->log_size;
    int     num         = params->num;
    for(int i = 0; i < num; i++) {
        if(begin[i + 1] - begin[i] > 1)
            _radix_sort<T, CMP, index_t>(begin[i], begin[i + 1], hash, log_size);
    }
}

template <typename T, typename CMP, typename index_t>
void radix_sort_in_place(T* begin, T* end, index_t (*hash)(T&), index_t maxv, int nthreads = 1) {
    const int SHIFT = 8;
    const int BLOCKS = (1 << (SHIFT + 1));

    int log_size = sizeof(maxv) * 8;
    while(!((1 << log_size) & maxv)) log_size--;
    int right_shift = log_size - SHIFT;

    // {(maxv >> right_shift) + 1 <= BLOCKS},
    int occupied = (maxv >> right_shift) + 1;
    time_t start = time(0);
    // count number in each bin
    index_t count[BLOCKS] = {0};
    for(T* curr = begin; curr != end; curr++) {
        count[hash(*curr) >> right_shift]++;
    }

    // sum numbers to create an index
    T* index[BLOCKS + 1];
    T* place[BLOCKS];
    index[0] = place[0] = begin;
    for(int i = 1; i < occupied; i++) {
        index[i] = place[i] = index[i - 1] + count[i - 1];
    }
    index[occupied] = end;
    if(nthreads != 1) cerr << "COUNT NUMBER IN EACH BIN: " << time(0) - start << endl;
    start = time(0);
    //put objects in proper place
    for(int bin = 0; bin < occupied; bin++) {
        while(place[bin] != index[bin + 1]) {
            T curr = *place[bin];
            int x = hash(curr) >> right_shift;
            while(x != bin) { // switched inner loop here, removed branch statement
                T temp = *place[x];
                *place[x]++ = curr;
                curr = temp;
                x = hash(curr) >> right_shift;
            }
            *place[bin]++ = curr;
        }
    }
    if(nthreads != 1) cerr << "PLACE IN CORRECT BIN: " << time(0) - start << endl;
    start = time(0);
    //sort partitions
    if(nthreads == 1) {
        for(int bin = 0; bin < occupied; bin++) {
            if(index[bin + 1] - index[bin] > 1) _radix_sort<T, CMP, index_t>(index[bin], index[bin + 1], hash, right_shift);
        }
    } else {
        AutoArray<tthread::thread*> threads(nthreads);
        EList<RecurseParams<T, index_t> > params; params.resizeExact(nthreads);
        int st = 0;
        for(int i = 0; i < nthreads; i++) {
            params[i].hash = hash;
            params[i].begin = index + st;
            params[i].log_size = right_shift;
            params[i].num = occupied / nthreads;
            threads[i] = new tthread::thread(&_radix_sort_worker<T, CMP, index_t>, (void*)&params[i]);
            st += params[i].num;
        }
        //do any remaining bins using main thread
        for(int bin = st; bin < occupied; bin++) {
            if(index[bin + 1] - index[bin] > 1) _radix_sort<T, CMP, index_t>(index[bin], index[bin + 1], hash, right_shift);
        }
        for(int i = 0; i < nthreads; i++) {
            threads[i]->join();
        }
    }
    if(nthreads != 1) cerr << "FINISHED RECURSIVE SORTS: " << time(0) - start << endl;
}

template <typename T, typename index_t>
struct CountParams {
    T* begin;
    T* end;
    T* o;
    index_t* count;
    index_t (*hash)(T&);
    int occupied;
    int right_shift;
};

template <typename T, typename index_t>
static void _count_worker(void* vp) {
    CountParams<T, index_t>* params = (CountParams<T, index_t>*)vp;
    T* begin            = params->begin;
    T* end              = params->end;
    index_t (*hash)(T&) = params->hash;
    int occupied        = params->occupied;
    int right_shift     = params->right_shift;

    params->count = new index_t[occupied + 1]();
    for(T* curr = begin; curr != end; curr++) {
        params->count[hash(*curr) >> right_shift]++;
    }
}
template <typename T, typename index_t>
static void _write_worker(void* vp) {
    CountParams<T, index_t>* params = (CountParams<T, index_t>*)vp;
    T* begin            = params->begin;
    T* end              = params->end;
    T* o                = params->o;
    index_t* count      = params->count;
    index_t (*hash)(T&) = params->hash;
    int right_shift     = params->right_shift;

    for(T* curr = begin; curr != end; curr++) {
        o[count[hash(*curr) >> right_shift]++] = *curr;
    }
}

template <typename T, typename CMP, typename index_t>
void radix_sort_copy(T* begin, T* end, T* o, index_t (*hash)(T&), index_t maxv, int nthreads = 1) {
    //set parameters
    const int SHIFT = 8;
    const int BLOCKS = (1 << (SHIFT + 1));
    int log_size = sizeof(maxv) * 8;
    while(!((1 << log_size) & maxv)) log_size--;
    int right_shift = log_size - SHIFT;
    int occupied = (maxv >> right_shift) + 1;
    //count nodes
    time_t start = time(0);
    EList<CountParams<T, index_t> > cparams; cparams.resizeExact(nthreads);
    AutoArray<tthread::thread*> threads1(nthreads);
    T* st = begin;
    T* en = st + (end - begin) / nthreads;
    for(int i = 0; i < nthreads; i++) {
        cparams[i].begin = st;
        cparams[i].end = en;
        cparams[i].hash = hash;
        cparams[i].o = o;
        cparams[i].occupied = occupied;
        cparams[i].right_shift= right_shift;
        if(nthreads == 1) {
            _count_worker<T, index_t>((void*)&cparams[i]);
        } else {
            threads1[i] = new tthread::thread(&_count_worker<T, index_t>, (void*)&cparams[i]);
        }
        st = en;
        if(i + 2 == nthreads) {
            en = end;
        } else {
            en = st + (end - begin) / nthreads;
        }
    }
    if(nthreads > 1) {
        for(int i = 0; i < nthreads; i++) {
            threads1[i]->join();
            delete threads1[i];
        }
    }
    if(nthreads != 1) cerr << "COUNT NUMBER IN EACH BIN: " << time(0) - start << endl;
    start = time(0);
    //transform counts into index
    index_t tot = cparams[0].count[0];
    cparams[0].count[0] = 0;
    for(int i = 1; i < nthreads; i++) {
        tot += cparams[i].count[0];
        cparams[i].count[0] = tot - cparams[i].count[0];
    }
    for(int j = 1; j < occupied + 1; j++) {
        for(int i = 0; i < nthreads; i++) {
            tot += cparams[i].count[j];
            cparams[i].count[j] = tot - cparams[i].count[j];
        }
    }
    T* index[BLOCKS + 1];
    for(int i = 0; i < occupied + 1; i++) {
        index[i] = o + cparams[0].count[i];
    }
    //write T's to correct bin
    if(nthreads == 1) {
        _write_worker<T, index_t>((void*)&cparams[0]);
    } else {
        for(int i = 0; i < nthreads; i++)
            threads1[i] = new tthread::thread(&_write_worker<T, index_t>, (void*)&cparams[i]);
        for(int i = 0; i < nthreads; i++) {
            threads1[i]->join();
        }
    }
    for(int i = 0; i < nthreads; i++) {
        delete[] cparams[i].count;
        delete threads1[i];
    }
    if(nthreads != 1) cerr << "FINISHED FIRST ROUND: " << time(0) - start << endl;
    start = time(0);
    //sort partitions
    if(nthreads == 1) {
        for(int bin = 0; bin < occupied; bin++)
            if(index[bin + 1] - index[bin] > 1)
                _radix_sort<T, CMP, index_t>(index[bin], index[bin + 1], hash, right_shift);
    } else {
        AutoArray<tthread::thread*> threads(nthreads);
        EList<RecurseParams<T, index_t> > params; params.resizeExact(nthreads);
        int st = 0;
        for(int i = 0; i < nthreads; i++) {
            params[i].hash = hash;
            params[i].begin = index + st;
            params[i].log_size = right_shift;
            params[i].num = 0;
            index_t remaining_elements = (index_t)(index[occupied] - index[st]);
            while(params[i].num + st < occupied
                        && (index_t)(index[params[i].num + st] - index[st]) < remaining_elements / (nthreads - i))
                params[i].num++;
            cerr << params[i].num << " " << (index_t)(index[params[i].num + st] - index[st]) << endl;
            threads[i] = new tthread::thread(&_radix_sort_worker<T, CMP, index_t>, (void*)&params[i]);
            st += params[i].num;
        }
        //do any remaining bins using main thread
        for(int bin = st; bin < occupied; bin++) {
            if(index[bin + 1] - index[bin] > 1)
                _radix_sort<T, CMP, index_t>(index[bin], index[bin + 1], hash, right_shift);
        }
        for(int i = 0; i < nthreads; i++) {
            threads[i]->join();
            delete threads[i];
        }
    }
    if(nthreads != 1) cerr << "FINISHED RECURSIVE SORTS: " << time(0) - start << endl;
}

#endif //RADIX_SORT_H_