]> mj.ucw.cz Git - libucw.git/blobdiff - lib/sorter/array.c
Use a different file access method for small inputs.
[libucw.git] / lib / sorter / array.c
index 5bc60276d5046fc8fca71dd29dbd6f40f4e21258..dd7659e70f04a2ccc18da447f6a9bdb269070eab 100644 (file)
@@ -48,7 +48,7 @@ asort_radix(struct asort_context *ctx, void *array, void *buffer, uns num_elts,
   for (uns i=0; i<buckets; i++)
     {
       uns n = cnt[i] - pos;
-      if (n * cts->elt_size < sorter_radix_threshold || shift < ASORT_MIN_SHIFT)
+      if (n * ctx->elt_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);
 }
 
@@ -216,14 +216,14 @@ rs_finish(struct worker_thread *thr UNUSED, struct work *ww)
 
   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 * ctx->elt_size < sorter_radix_threshold)
+  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);
+    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);
 }
@@ -260,8 +260,8 @@ rs_radix(struct asort_context *ctx, void *array, void *buffer, uns num_elts, uns
       w->w.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;
@@ -323,8 +323,8 @@ rs_radix(struct asort_context *ctx, void *array, void *buffer, uns num_elts, uns
          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;
@@ -349,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;
 
@@ -367,8 +367,7 @@ threaded_radixsort(struct asort_context *ctx)
   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; i<sorter_threads; i++)
     big_free(ctx->rs_works[i], sizeof(struct rs_work) + sizeof(uns) * (1 << ctx->radix_bits));
 
@@ -388,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)
 {
@@ -414,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");