aboutsummaryrefslogtreecommitdiff
path: root/gdbsupport
diff options
context:
space:
mode:
authorTom de Vries <tdevries@suse.de>2022-07-18 08:34:06 +0200
committerTom de Vries <tdevries@suse.de>2022-07-18 08:34:06 +0200
commit4319180c8139e579a6337eb020309c95fa1e00b3 (patch)
treea9db3c8f9ce5a6cb8c43c777165c1b9387012ee2 /gdbsupport
parentc3d3b64b34bff289f178e2267e6363f71b0c4234 (diff)
downloadgdb-4319180c8139e579a6337eb020309c95fa1e00b3.zip
gdb-4319180c8139e579a6337eb020309c95fa1e00b3.tar.gz
gdb-4319180c8139e579a6337eb020309c95fa1e00b3.tar.bz2
[gdbsupport] Improve thread scheduling in parallel_for_each
When running a task using parallel_for_each, we get the following distribution: ... Parallel for: n_elements: 7271 Parallel for: minimum elements per thread: 10 Parallel for: elts_per_thread: 1817 Parallel for: elements on worker thread 0 : 1817 Parallel for: elements on worker thread 1 : 1817 Parallel for: elements on worker thread 2 : 1817 Parallel for: elements on worker thread 3 : 0 Parallel for: elements on main thread : 1820 ... Note that there are 4 active threads, and scheduling elts_per_thread on each of those handles 4 * 1817 == 7268, leaving 3 "left over" elements. These leftovers are currently handled in the main thread. That doesn't seem to matter much for this example, but for say 10 threads and 99 elements, you'd have 9 threads handling 9 elements and 1 thread handling 18 elements. Instead, distribute the left over elements over the worker threads, such that we have: ... Parallel for: elements on worker thread 0 : 1818 Parallel for: elements on worker thread 1 : 1818 Parallel for: elements on worker thread 2 : 1818 Parallel for: elements on worker thread 3 : 0 Parallel for: elements on main thread : 1817 ... Tested on x86_64-linux.
Diffstat (limited to 'gdbsupport')
-rw-r--r--gdbsupport/parallel-for.h8
1 files changed, 8 insertions, 0 deletions
diff --git a/gdbsupport/parallel-for.h b/gdbsupport/parallel-for.h
index cfe8a6e..bf40f12 100644
--- a/gdbsupport/parallel-for.h
+++ b/gdbsupport/parallel-for.h
@@ -147,6 +147,8 @@ parallel_for_each (unsigned n, RandomIt first, RandomIt last,
size_t n_threads = n_worker_threads;
size_t n_elements = last - first;
size_t elts_per_thread = 0;
+ size_t elts_left_over = 0;
+
if (n_threads > 1)
{
/* Require that there should be at least N elements in a
@@ -155,6 +157,8 @@ parallel_for_each (unsigned n, RandomIt first, RandomIt last,
if (n_elements / n_threads < n)
n_threads = std::max (n_elements / n, (size_t) 1);
elts_per_thread = n_elements / n_threads;
+ elts_left_over = n_elements % n_threads;
+ /* n_elements == n_threads * elts_per_thread + elts_left_over. */
}
size_t count = n_threads == 0 ? 0 : n_threads - 1;
@@ -170,6 +174,10 @@ parallel_for_each (unsigned n, RandomIt first, RandomIt last,
for (int i = 0; i < count; ++i)
{
RandomIt end = first + elts_per_thread;
+ if (i < elts_left_over)
+ /* Distribute the leftovers over the worker threads, to avoid having
+ to handle all of them in a single thread. */
+ end++;
if (parallel_for_each_debug)
debug_printf (_("Parallel for: elements on worker thread %i\t: %zu\n"),
i, (size_t)(end - first));