X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fsorter%2Farray.c;h=e1805245ff4b1a609903fecca20120f7a8014416;hb=e19f28ac6765d6f014c43e724065232d69c08f0d;hp=68ada9015968a8dc7c495a93c47cbe388c4595fb;hpb=c5fbc7b75705d1f7a322ad73e6055284a3b94e73;p=libucw.git diff --git a/lib/sorter/array.c b/lib/sorter/array.c index 68ada901..e1805245 100644 --- a/lib/sorter/array.c +++ b/lib/sorter/array.c @@ -75,6 +75,15 @@ static uns asort_threads_use_count; static uns asort_threads_ready; static struct worker_pool asort_thread_pool; +static uns +rs_estimate_stack(void) +{ + // Stack space needed by the recursive radix-sorter + uns ctrsize = sizeof(uns) * (1 << CONFIG_UCW_RADIX_SORTER_BITS); + uns maxdepth = (64 / CONFIG_UCW_RADIX_SORTER_BITS) + 1; + return ctrsize * maxdepth; +} + void asort_start_threads(uns run) { @@ -82,8 +91,11 @@ asort_start_threads(uns run) asort_threads_use_count++; if (run && !asort_threads_ready) { - ASORT_TRACE("Initializing thread pool (%d threads)", sorter_threads); + // XXX: If somebody overrides the radix-sorter parameters to insane values, + // he also should override the stack size to insane values. + asort_thread_pool.stack_size = default_thread_stack_size + rs_estimate_stack(); asort_thread_pool.num_threads = sorter_threads; + ASORT_TRACE("Initializing thread pool (%d threads, %dK stack)", sorter_threads, asort_thread_pool.stack_size >> 10); worker_pool_init(&asort_thread_pool); asort_threads_ready = 1; } @@ -320,7 +332,7 @@ rs_radix(struct asort_context *ctx, void *array, void *buffer, uns num_elts, uns uns n = cnt[i] - pos; if (!n) continue; - if (n * ctx->elt_size < sorter_thread_threshold) + if (n * ctx->elt_size < sorter_thread_threshold || shift < ASORT_MIN_SHIFT) { struct rs_work *w = ep_alloc(ctx->eltpool); w->w.priority = 0; @@ -425,11 +437,13 @@ asort_run(struct asort_context *ctx) { ASORT_XTRACE(2, "Decided to use parallel quicksort"); threaded_quicksort(ctx); - return; } + else #endif - ASORT_XTRACE(2, "Decided to use sequential quicksort"); - ctx->quicksort(ctx->array, ctx->num_elts); + { + ASORT_XTRACE(2, "Decided to use sequential quicksort"); + ctx->quicksort(ctx->array, ctx->num_elts); + } } else { @@ -441,9 +455,12 @@ asort_run(struct asort_context *ctx) threaded_radixsort(ctx, swap); return; } + else #endif - ASORT_XTRACE(2, "Decided to use sequential radix-sort (swap=%d)", swap); - asort_radix(ctx, ctx->array, ctx->buffer, ctx->num_elts, ctx->hash_bits, swap); + { + ASORT_XTRACE(2, "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; }