2019-03-04 01:15:30 +08:00
|
|
|
/* Parallel for loops
|
|
|
|
|
2024-01-12 23:30:44 +08:00
|
|
|
Copyright (C) 2019-2024 Free Software Foundation, Inc.
|
2019-03-04 01:15:30 +08:00
|
|
|
|
|
|
|
This file is part of GDB.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#ifndef GDBSUPPORT_PARALLEL_FOR_H
|
|
|
|
#define GDBSUPPORT_PARALLEL_FOR_H
|
|
|
|
|
|
|
|
#include <algorithm>
|
2021-06-14 02:46:28 +08:00
|
|
|
#include <type_traits>
|
2019-03-04 01:15:30 +08:00
|
|
|
#include "gdbsupport/thread-pool.h"
|
2022-08-05 22:12:56 +08:00
|
|
|
#include "gdbsupport/function-view.h"
|
2019-03-04 01:15:30 +08:00
|
|
|
|
|
|
|
namespace gdb
|
|
|
|
{
|
|
|
|
|
|
|
|
/* A very simple "parallel for". This splits the range of iterators
|
|
|
|
into subranges, and then passes each subrange to the callback. The
|
|
|
|
work may or may not be done in separate threads.
|
|
|
|
|
|
|
|
This approach was chosen over having the callback work on single
|
|
|
|
items because it makes it simple for the caller to do
|
2021-05-23 23:04:27 +08:00
|
|
|
once-per-subrange initialization and destruction.
|
|
|
|
|
|
|
|
The parameter N says how batching ought to be done -- there will be
|
|
|
|
at least N elements processed per thread. Setting N to 0 is not
|
2023-01-02 03:31:24 +08:00
|
|
|
allowed. */
|
2019-03-04 01:15:30 +08:00
|
|
|
|
|
|
|
template<class RandomIt, class RangeFunction>
|
2023-01-02 03:31:24 +08:00
|
|
|
void
|
2021-05-23 23:04:27 +08:00
|
|
|
parallel_for_each (unsigned n, RandomIt first, RandomIt last,
|
2023-01-02 03:31:24 +08:00
|
|
|
RangeFunction callback)
|
2019-03-04 01:15:30 +08:00
|
|
|
{
|
2022-07-18 11:34:01 +08:00
|
|
|
/* If enabled, print debug info about how the work is distributed across
|
|
|
|
the threads. */
|
2022-07-21 19:34:14 +08:00
|
|
|
const bool parallel_for_each_debug = false;
|
2022-07-18 11:34:01 +08:00
|
|
|
|
|
|
|
size_t n_worker_threads = thread_pool::g_thread_pool->thread_count ();
|
|
|
|
size_t n_threads = n_worker_threads;
|
2019-03-04 01:15:30 +08:00
|
|
|
size_t n_elements = last - first;
|
2021-06-14 02:46:28 +08:00
|
|
|
size_t elts_per_thread = 0;
|
2022-07-18 14:34:06 +08:00
|
|
|
size_t elts_left_over = 0;
|
|
|
|
|
2019-03-04 01:15:30 +08:00
|
|
|
if (n_threads > 1)
|
|
|
|
{
|
2023-01-02 03:31:24 +08:00
|
|
|
/* Require that there should be at least N elements in a
|
|
|
|
thread. */
|
|
|
|
gdb_assert (n > 0);
|
|
|
|
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. */
|
2019-03-04 01:15:30 +08:00
|
|
|
}
|
|
|
|
|
2021-06-14 02:46:28 +08:00
|
|
|
size_t count = n_threads == 0 ? 0 : n_threads - 1;
|
2023-01-02 03:31:24 +08:00
|
|
|
std::vector<gdb::future<void>> results;
|
2019-03-04 01:15:30 +08:00
|
|
|
|
2022-07-18 11:34:01 +08:00
|
|
|
if (parallel_for_each_debug)
|
|
|
|
{
|
|
|
|
debug_printf (_("Parallel for: n_elements: %zu\n"), n_elements);
|
2023-01-02 03:31:24 +08:00
|
|
|
debug_printf (_("Parallel for: minimum elements per thread: %u\n"), n);
|
|
|
|
debug_printf (_("Parallel for: elts_per_thread: %zu\n"), elts_per_thread);
|
2022-07-18 11:34:01 +08:00
|
|
|
}
|
|
|
|
|
2021-06-14 02:46:28 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
2022-08-05 22:12:56 +08:00
|
|
|
RandomIt end;
|
2023-01-02 03:31:24 +08:00
|
|
|
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++;
|
2022-12-14 03:03:34 +08:00
|
|
|
|
|
|
|
/* This case means we don't have enough elements to really
|
|
|
|
distribute them. Rather than ever submit a task that does
|
|
|
|
nothing, we short-circuit here. */
|
|
|
|
if (first == end)
|
|
|
|
end = last;
|
|
|
|
|
|
|
|
if (end == last)
|
|
|
|
{
|
|
|
|
/* We're about to dispatch the last batch of elements, which
|
|
|
|
we normally process in the main thread. So just truncate
|
|
|
|
the result list here. This avoids submitting empty tasks
|
|
|
|
to the thread pool. */
|
|
|
|
count = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-07-18 11:34:01 +08:00
|
|
|
if (parallel_for_each_debug)
|
2022-08-05 22:12:56 +08:00
|
|
|
{
|
|
|
|
debug_printf (_("Parallel for: elements on worker thread %i\t: %zu"),
|
|
|
|
i, (size_t)(end - first));
|
|
|
|
debug_printf (_("\n"));
|
|
|
|
}
|
2023-01-02 03:31:24 +08:00
|
|
|
results.push_back (gdb::thread_pool::g_thread_pool->post_task ([=] ()
|
|
|
|
{
|
|
|
|
return callback (first, end);
|
|
|
|
}));
|
2021-06-14 02:46:28 +08:00
|
|
|
first = end;
|
|
|
|
}
|
|
|
|
|
2022-07-18 11:34:01 +08:00
|
|
|
for (int i = count; i < n_worker_threads; ++i)
|
|
|
|
if (parallel_for_each_debug)
|
2022-08-05 22:12:56 +08:00
|
|
|
{
|
|
|
|
debug_printf (_("Parallel for: elements on worker thread %i\t: 0"), i);
|
|
|
|
debug_printf (_("\n"));
|
|
|
|
}
|
2022-07-18 11:34:01 +08:00
|
|
|
|
2021-06-14 02:46:28 +08:00
|
|
|
/* Process all the remaining elements in the main thread. */
|
2022-07-18 11:34:01 +08:00
|
|
|
if (parallel_for_each_debug)
|
2022-08-05 22:12:56 +08:00
|
|
|
{
|
|
|
|
debug_printf (_("Parallel for: elements on main thread\t\t: %zu"),
|
|
|
|
(size_t)(last - first));
|
|
|
|
debug_printf (_("\n"));
|
|
|
|
}
|
2023-01-02 03:31:24 +08:00
|
|
|
callback (first, last);
|
|
|
|
|
|
|
|
for (auto &fut : results)
|
|
|
|
fut.get ();
|
2019-03-04 01:15:30 +08:00
|
|
|
}
|
|
|
|
|
2022-07-14 23:01:52 +08:00
|
|
|
/* A sequential drop-in replacement of parallel_for_each. This can be useful
|
2024-11-23 19:20:34 +08:00
|
|
|
when debugging multi-threading behavior, and you want to limit
|
2022-07-14 23:01:52 +08:00
|
|
|
multi-threading in a fine-grained way. */
|
|
|
|
|
|
|
|
template<class RandomIt, class RangeFunction>
|
2023-01-02 03:31:24 +08:00
|
|
|
void
|
2022-07-14 23:01:52 +08:00
|
|
|
sequential_for_each (unsigned n, RandomIt first, RandomIt last,
|
2023-01-02 03:31:24 +08:00
|
|
|
RangeFunction callback)
|
2022-07-14 23:01:52 +08:00
|
|
|
{
|
2023-01-02 03:31:24 +08:00
|
|
|
callback (first, last);
|
2022-07-14 23:01:52 +08:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:15:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* GDBSUPPORT_PARALLEL_FOR_H */
|