2 * UCW Library -- Optimized Array Sorter
4 * (c) 2003--2007 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 #include "lib/sorter/common.h"
18 #define ASORT_MIN_RADIX 5000 // FIXME: var?
19 #define ASORT_MIN_SHIFT 2
22 asort_radix(struct asort_context *ctx, void *array, void *buffer, uns num_elts, uns hash_bits, uns swapped_output)
24 // swap_output == 0 if result should be returned in `array', otherwise in `buffer'
25 uns buckets = (1 << ctx->radix_bits);
26 uns shift = (hash_bits > ctx->radix_bits) ? (hash_bits - ctx->radix_bits) : 0;
30 static int reported[64];
31 if (!reported[hash_bits]++)
33 DBG(">>> n=%d h=%d s=%d sw=%d", num_elts, hash_bits, shift, swapped_output);
35 bzero(cnt, sizeof(cnt));
36 ctx->radix_count(array, num_elts, cnt, shift);
39 for (uns i=0; i<buckets; i++)
45 ASSERT(pos == num_elts);
47 ctx->radix_split(array, buffer, num_elts, cnt, shift);
49 for (uns i=0; i<buckets; i++)
52 if (n < ASORT_MIN_RADIX || shift < ASORT_MIN_SHIFT)
54 ctx->quicksort(buffer, n);
56 memcpy(array, buffer, n * ctx->elt_size);
59 asort_radix(ctx, buffer, array, n, shift, !swapped_output);
60 array += n * ctx->elt_size;
61 buffer += n * ctx->elt_size;
66 #ifdef CONFIG_UCW_THREADS
68 #include "lib/threads.h"
69 #include "lib/workqueue.h"
70 #include "lib/eltpool.h"
72 static uns asort_threads_use_count;
73 static uns asort_threads_ready;
74 static struct worker_pool asort_thread_pool;
77 asort_start_threads(uns run)
80 asort_threads_use_count++;
81 if (run && !asort_threads_ready)
83 SORT_XTRACE(2, "Initializing thread pool (%d threads)", sorter_threads);
84 asort_thread_pool.num_threads = sorter_threads;
85 worker_pool_init(&asort_thread_pool);
86 asort_threads_ready = 1;
92 asort_stop_threads(void)
95 if (!--asort_threads_use_count && asort_threads_ready)
97 SORT_XTRACE(2, "Shutting down thread pool");
98 worker_pool_cleanup(&asort_thread_pool);
99 asort_threads_ready = 0;
106 struct asort_context *ctx;
110 #define LR_UNDEF -100
114 qs_handle_work(struct worker_thread *thr UNUSED, struct work *ww)
116 struct qs_work *w = (struct qs_work *) ww;
118 DBG("Thread %d: got %d elts", thr->id, w->num_elts);
119 if (w->num_elts * w->ctx->elt_size < sorter_thread_threshold)
121 w->ctx->quicksort(w->array, w->num_elts);
122 w->left = w->right = LR_UNDEF;
125 w->ctx->quicksplit(w->array, w->num_elts, &w->left, &w->right);
126 DBG("Thread %d: returning l=%d r=%d", thr->id, w->left, w->right);
129 static struct qs_work *
130 qs_alloc_work(struct asort_context *ctx)
132 struct qs_work *w = ep_alloc(ctx->eltpool);
134 w->w.go = qs_handle_work;
140 threaded_quicksort(struct asort_context *ctx)
143 struct qs_work *v, *w;
145 asort_start_threads(1);
146 work_queue_init(&asort_thread_pool, &q);
147 ctx->eltpool = ep_new(sizeof(struct qs_work), 1000);
149 w = qs_alloc_work(ctx);
150 w->array = ctx->array;
151 w->num_elts = ctx->num_elts;
152 work_submit(&q, &w->w);
154 while (v = (struct qs_work *) work_wait(&q))
156 if (v->left != LR_UNDEF)
160 w = qs_alloc_work(ctx);
162 w->num_elts = v->right + 1;
163 w->w.priority = v->w.priority + 1;
164 work_submit(&q, &w->w);
166 if (v->left < (int)v->num_elts - 1)
168 w = qs_alloc_work(ctx);
169 w->array = v->array + v->left * ctx->elt_size;
170 w->num_elts = v->num_elts - v->left;
171 w->w.priority = v->w.priority + 1;
172 work_submit(&q, &w->w);
175 ep_free(ctx->eltpool, v);
178 ep_delete(ctx->eltpool);
179 work_queue_cleanup(&q);
180 asort_stop_threads();
185 struct asort_context *ctx;
194 rs_count(struct worker_thread *thr UNUSED, struct work *ww)
196 struct rs_work *w = (struct rs_work *) ww;
198 DBG("Thread %d: Counting %d items, shift=%d", thr->id, w->num_elts, w->shift);
199 w->ctx->radix_count(w->in, w->num_elts, w->cnt, w->shift);
200 DBG("Thread %d: Counting done", thr->id);
204 rs_split(struct worker_thread *thr UNUSED, struct work *ww)
206 struct rs_work *w = (struct rs_work *) ww;
208 DBG("Thread %d: Splitting %d items, shift=%d", thr->id, w->num_elts, w->shift);
209 w->ctx->radix_split(w->in, w->out, w->num_elts, w->cnt, w->shift);
210 DBG("Thread %d: Splitting done", thr->id);
214 rs_finish(struct worker_thread *thr UNUSED, struct work *ww)
216 struct rs_work *w = (struct rs_work *) ww;
218 DBG("Thread %d: Finishing %d items, shift=%d", thr->id, w->num_elts, w->shift);
219 if (w->shift < ASORT_MIN_SHIFT || w->num_elts < ASORT_MIN_RADIX)
221 w->ctx->quicksort(w->in, w->num_elts);
223 memcpy(w->out, w->in, w->num_elts * w->ctx->elt_size);
226 asort_radix(w->ctx, w->in, w->out, w->num_elts, w->shift, w->swap_output);
227 DBG("Thread %d: Finishing done", thr->id);
231 rs_radix(struct asort_context *ctx, void *array, void *buffer, uns num_elts, uns hash_bits, uns swapped_output)
233 uns buckets = (1 << ctx->radix_bits);
234 uns shift = (hash_bits > ctx->radix_bits) ? (hash_bits - ctx->radix_bits) : 0;
236 uns blksize = num_elts / sorter_threads;
237 DBG(">>> n=%d h=%d s=%d blk=%d sw=%d", num_elts, hash_bits, shift, blksize, swapped_output);
239 // Start parallel counting
241 for (uns i=0; i<sorter_threads; i++)
243 struct rs_work *w = ctx->rs_works[i];
249 w->num_elts = blksize;
250 if (i == sorter_threads-1)
251 w->num_elts += num_elts % sorter_threads;
253 iptr += w->num_elts * ctx->elt_size;
254 bzero(w->cnt, sizeof(uns) * buckets);
255 work_submit(ctx->rs_work_queue, &w->w);
258 // Get bucket sizes from the counts
259 bzero(cnt, sizeof(cnt));
260 for (uns i=0; i<sorter_threads; i++)
262 struct rs_work *w = (struct rs_work *) work_wait(ctx->rs_work_queue);
264 for (uns j=0; j<buckets; j++)
268 // Calculate bucket starts
270 for (uns i=0; i<buckets; i++)
276 ASSERT(pos == num_elts);
278 // Start parallel splitting
279 for (uns i=0; i<sorter_threads; i++)
281 struct rs_work *w = ctx->rs_works[i];
283 for (uns j=0; j<buckets; j++)
289 work_submit(ctx->rs_work_queue, &w->w);
291 ASSERT(cnt[buckets-1] == num_elts);
293 // Wait for splits to finish
294 while (work_wait(ctx->rs_work_queue))
297 // Recurse on buckets
299 for (uns i=0; i<buckets; i++)
301 uns n = cnt[i] - pos;
302 if (n * ctx->elt_size < sorter_thread_threshold)
304 struct rs_work *w = ep_alloc(ctx->eltpool);
312 w->swap_output = !swapped_output;
313 clist_add_tail(&ctx->rs_bits, &w->w.n);
314 DBG("Scheduling block %d+%d", pos, n);
317 rs_radix(ctx, buffer, array, n, shift, !swapped_output);
319 array += n * ctx->elt_size;
320 buffer += n * ctx->elt_size;
325 threaded_radixsort(struct asort_context *ctx)
329 asort_start_threads(1);
330 work_queue_init(&asort_thread_pool, &q);
332 // Prepare work structures for counting and splitting.
333 // We use big_alloc(), because we want to avoid cacheline aliasing between threads.
334 ctx->rs_work_queue = &q;
335 ctx->rs_works = alloca(sizeof(struct rs_work *) * sorter_threads);
336 for (uns i=0; i<sorter_threads; i++)
337 ctx->rs_works[i] = big_alloc(sizeof(struct rs_work) + sizeof(uns) * (1 << ctx->radix_bits));
339 // Prepare work structures for all remaining small bits which will be sorted later.
340 clist_init(&ctx->rs_bits);
341 ctx->eltpool = ep_new(sizeof(struct rs_work), 1000);
343 // Do the big splitting
344 // FIXME: Set the swap bit carefully.
345 rs_radix(ctx, ctx->array, ctx->buffer, ctx->num_elts, ctx->hash_bits, 0);
346 for (uns i=0; i<sorter_threads; i++)
347 big_free(ctx->rs_works[i], sizeof(struct rs_work) + sizeof(uns) * (1 << ctx->radix_bits));
349 // Finish the small blocks
350 struct rs_work *w, *tmp;
351 CLIST_WALK_DELSAFE(w, ctx->rs_bits, tmp)
352 work_submit(&q, &w->w);
353 while (work_wait(&q))
356 ep_delete(ctx->eltpool);
357 work_queue_cleanup(&q);
358 asort_stop_threads();
363 void asort_start_threads(uns run UNUSED) { }
364 void asort_stop_threads(void) { }
369 asort_run(struct asort_context *ctx)
371 SORT_XTRACE(10, "Array-sorting %d items per %d bytes, hash_bits=%d", ctx->num_elts, ctx->elt_size, ctx->hash_bits);
372 uns allow_threads UNUSED = (sorter_threads > 1 &&
373 ctx->num_elts * ctx->elt_size >= sorter_thread_threshold &&
374 !(sorter_debug & SORT_DEBUG_ASORT_NO_THREADS));
376 if (ctx->num_elts < ASORT_MIN_RADIX ||
377 ctx->hash_bits <= ASORT_MIN_SHIFT ||
379 (sorter_debug & SORT_DEBUG_ASORT_NO_RADIX))
381 #ifdef CONFIG_UCW_THREADS
384 SORT_XTRACE(12, "Decided to use parallel quicksort");
385 threaded_quicksort(ctx);
389 SORT_XTRACE(12, "Decided to use sequential quicksort");
390 ctx->quicksort(ctx->array, ctx->num_elts);
394 #ifdef CONFIG_UCW_THREADS
397 SORT_XTRACE(12, "Decided to use parallel radix-sort");
398 threaded_radixsort(ctx);
402 SORT_XTRACE(12, "Decided to use sequential radix-sort");
403 // FIXME: select dest buffer
404 asort_radix(ctx, ctx->array, ctx->buffer, ctx->num_elts, ctx->hash_bits, 0);
407 SORT_XTRACE(11, "Array-sort finished");