X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fsorter%2Farray.c;h=dd7659e70f04a2ccc18da447f6a9bdb269070eab;hb=a5ff98a53789157a6c96e58b2385bb898d688a22;hp=26b4a1b53048e1f11de89a653c36bcbdb513b292;hpb=7cf300f543b5023ad46a909ed6fde0feba5d1acd;p=libucw.git diff --git a/lib/sorter/array.c b/lib/sorter/array.c index 26b4a1b5..dd7659e7 100644 --- a/lib/sorter/array.c +++ b/lib/sorter/array.c @@ -48,7 +48,7 @@ asort_radix(struct asort_context *ctx, void *array, void *buffer, uns num_elts, for (uns i=0; ielt_size < sorter_radix_threshold || shift < ASORT_MIN_SHIFT) { ctx->quicksort(buffer, n); if (!swapped_output) @@ -182,7 +182,7 @@ threaded_quicksort(struct asort_context *ctx) struct rs_work { struct work w; struct asort_context *ctx; - void *in, *out; + void *array, *buffer; // Like asort_radix(). uns num_elts; uns shift; uns swap_output; @@ -195,7 +195,7 @@ rs_count(struct worker_thread *thr UNUSED, struct work *ww) struct rs_work *w = (struct rs_work *) ww; DBG("Thread %d: Counting %d items, shift=%d", thr->id, w->num_elts, w->shift); - w->ctx->radix_count(w->in, w->num_elts, w->cnt, w->shift); + w->ctx->radix_count(w->array, w->num_elts, w->cnt, w->shift); DBG("Thread %d: Counting done", thr->id); } @@ -205,7 +205,7 @@ rs_split(struct worker_thread *thr UNUSED, struct work *ww) struct rs_work *w = (struct rs_work *) ww; DBG("Thread %d: Splitting %d items, shift=%d", thr->id, w->num_elts, w->shift); - w->ctx->radix_split(w->in, w->out, w->num_elts, w->cnt, w->shift); + w->ctx->radix_split(w->array, w->buffer, w->num_elts, w->cnt, w->shift); DBG("Thread %d: Splitting done", thr->id); } @@ -214,16 +214,30 @@ rs_finish(struct worker_thread *thr UNUSED, struct work *ww) { struct rs_work *w = (struct rs_work *) ww; - DBG("Thread %d: Finishing %d items, shift=%d", thr->id, w->num_elts, w->shift); - if (w->shift < ASORT_MIN_SHIFT || w->num_elts < sorter_radix_threshold) + if (thr) + DBG("Thread %d: Finishing %d items, shift=%d", thr->id, w->num_elts, w->shift); + if (w->shift < ASORT_MIN_SHIFT || w->num_elts * w->ctx->elt_size < sorter_radix_threshold) { - w->ctx->quicksort(w->in, w->num_elts); + w->ctx->quicksort(w->array, w->num_elts); if (w->swap_output) - memcpy(w->out, w->in, w->num_elts * w->ctx->elt_size); + memcpy(w->buffer, w->array, w->num_elts * w->ctx->elt_size); } else - asort_radix(w->ctx, w->in, w->out, w->num_elts, w->shift, w->swap_output); - DBG("Thread %d: Finishing done", thr->id); + asort_radix(w->ctx, w->array, w->buffer, w->num_elts, w->shift, w->swap_output); + if (thr) + DBG("Thread %d: Finishing done", thr->id); +} + +static void +rs_wait_small(struct asort_context *ctx) +{ + struct rs_work *w; + + while (w = (struct rs_work *) work_wait(ctx->rs_work_queue)) + { + DBG("Reaping small chunk of %d items", w->num_elts); + ep_free(ctx->eltpool, w); + } } static void @@ -235,6 +249,9 @@ rs_radix(struct asort_context *ctx, void *array, void *buffer, uns num_elts, uns uns blksize = num_elts / sorter_threads; DBG(">>> n=%d h=%d s=%d blk=%d sw=%d", num_elts, hash_bits, shift, blksize, swapped_output); + // If there are any small chunks in progress, wait for them to finish + rs_wait_small(ctx); + // Start parallel counting void *iptr = array; for (uns i=0; iw.priority = 0; w->w.go = rs_count; w->ctx = ctx; - w->in = iptr; - w->out = buffer; + w->array = iptr; + w->buffer = buffer; w->num_elts = blksize; if (i == sorter_threads-1) w->num_elts += num_elts % sorter_threads; @@ -298,19 +315,30 @@ rs_radix(struct asort_context *ctx, void *array, void *buffer, uns num_elts, uns for (uns i=0; ielt_size < sorter_thread_threshold) { struct rs_work *w = ep_alloc(ctx->eltpool); w->w.priority = 0; w->w.go = rs_finish; w->ctx = ctx; - w->in = buffer; - w->out = array; + w->array = buffer; + w->buffer = array; w->num_elts = n; w->shift = shift; w->swap_output = !swapped_output; - clist_add_tail(&ctx->rs_bits, &w->w.n); - DBG("Scheduling block %d+%d", pos, n); + if (n * ctx->elt_size < sorter_thread_chunk) + { + DBG("Sorting block %d+%d inline", pos, n); + rs_finish(NULL, &w->w); + ep_free(ctx->eltpool, w); + } + else + { + DBG("Scheduling block %d+%d", pos, n); + work_submit(ctx->rs_work_queue, &w->w); + } } else rs_radix(ctx, buffer, array, n, shift, !swapped_output); @@ -321,7 +349,7 @@ rs_radix(struct asort_context *ctx, void *array, void *buffer, uns num_elts, uns } static void -threaded_radixsort(struct asort_context *ctx) +threaded_radixsort(struct asort_context *ctx, uns swap) { struct work_queue q; @@ -335,23 +363,18 @@ threaded_radixsort(struct asort_context *ctx) for (uns i=0; irs_works[i] = big_alloc(sizeof(struct rs_work) + sizeof(uns) * (1 << ctx->radix_bits)); - // Prepare work structures for all remaining small bits which will be sorted later. - clist_init(&ctx->rs_bits); + // Prepare a pool for all remaining small bits which will be sorted on background. ctx->eltpool = ep_new(sizeof(struct rs_work), 1000); // Do the big splitting - // FIXME: Set the swap bit carefully. - rs_radix(ctx, ctx->array, ctx->buffer, ctx->num_elts, ctx->hash_bits, 0); + rs_radix(ctx, ctx->array, ctx->buffer, ctx->num_elts, ctx->hash_bits, swap); for (uns i=0; irs_works[i], sizeof(struct rs_work) + sizeof(uns) * (1 << ctx->radix_bits)); // Finish the small blocks - struct rs_work *w, *tmp; - CLIST_WALK_DELSAFE(w, ctx->rs_bits, tmp) - work_submit(&q, &w->w); - while (work_wait(&q)) - ; + rs_wait_small(ctx); + ASSERT(!ctx->eltpool->num_allocated); ep_delete(ctx->eltpool); work_queue_cleanup(&q); asort_stop_threads(); @@ -364,6 +387,23 @@ void asort_stop_threads(void) { } #endif +static uns +predict_swap(struct asort_context *ctx) +{ + uns bits = ctx->radix_bits; + uns elts = ctx->num_elts; + uns swap = 0; + + while (elts * ctx->elt_size >= sorter_radix_threshold && bits >= ASORT_MIN_SHIFT) + { + DBG("Predicting pass: %d elts, %d bits", elts, bits); + swap = !swap; + elts >>= ctx->radix_bits; + bits = MAX(bits, ctx->radix_bits) - ctx->radix_bits; + } + return swap; +} + void asort_run(struct asort_context *ctx) { @@ -372,7 +412,7 @@ asort_run(struct asort_context *ctx) ctx->num_elts * ctx->elt_size >= sorter_thread_threshold && !(sorter_debug & SORT_DEBUG_ASORT_NO_THREADS)); - if (ctx->num_elts < sorter_radix_threshold || + if (ctx->num_elts * ctx->elt_size < sorter_radix_threshold || ctx->hash_bits <= ASORT_MIN_SHIFT || !ctx->radix_split || (sorter_debug & SORT_DEBUG_ASORT_NO_RADIX)) @@ -390,17 +430,19 @@ asort_run(struct asort_context *ctx) } else { + uns swap = predict_swap(ctx); #ifdef CONFIG_UCW_THREADS if (allow_threads) { - SORT_XTRACE(12, "Decided to use parallel radix-sort"); - threaded_radixsort(ctx); + SORT_XTRACE(12, "Decided to use parallel radix-sort (swap=%d)", swap); + threaded_radixsort(ctx, swap); return; } #endif - SORT_XTRACE(12, "Decided to use sequential radix-sort"); - // FIXME: select dest buffer - asort_radix(ctx, ctx->array, ctx->buffer, ctx->num_elts, ctx->hash_bits, 0); + SORT_XTRACE(12, "Decided to use sequential radix-sort (swap=%d)", swap); + asort_radix(ctx, ctx->array, ctx->buffer, ctx->num_elts, ctx->hash_bits, swap); + if (swap) + ctx->array = ctx->buffer; } SORT_XTRACE(11, "Array-sort finished");