2017-05-11 08:03:52 +08:00
|
|
|
//===- llvm/Support/Parallel.cpp - Parallel algorithms --------------------===//
|
2017-05-06 05:09:26 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-05-06 05:09:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-05-11 08:03:52 +08:00
|
|
|
#include "llvm/Support/Parallel.h"
|
2017-05-06 05:14:55 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2019-03-17 03:36:29 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2017-10-05 04:27:01 +08:00
|
|
|
#include "llvm/Support/Threading.h"
|
2017-05-06 05:09:26 +08:00
|
|
|
|
|
|
|
#include <atomic>
|
2019-03-17 03:36:29 +08:00
|
|
|
#include <future>
|
2017-05-06 05:09:26 +08:00
|
|
|
#include <stack>
|
2017-05-06 05:27:30 +08:00
|
|
|
#include <thread>
|
2019-03-17 03:36:29 +08:00
|
|
|
#include <vector>
|
2017-05-06 05:09:26 +08:00
|
|
|
|
[lld][COFF][ELF][WebAssembly] Replace --[no-]threads /threads[:no] with --threads={1,2,...} /threads:{1,2,...}
--no-threads is a name copied from gold.
gold has --no-thread, --thread-count and several other --thread-count-*.
There are needs to customize the number of threads (running several lld
processes concurrently or customizing the number of LTO threads).
Having a single --threads=N is a straightforward replacement of gold's
--no-threads + --thread-count.
--no-threads is used rarely. So just delete --no-threads instead of
keeping it for compatibility for a while.
If --threads= is specified (ELF,wasm; COFF /threads: is similar),
--thinlto-jobs= defaults to --threads=,
otherwise all available hardware threads are used.
There is currently no way to override a --threads={1,2,...}. It is still
a debate whether we should use --threads=all.
Reviewed By: rnk, aganea
Differential Revision: https://reviews.llvm.org/D76885
2020-03-18 03:40:19 +08:00
|
|
|
llvm::ThreadPoolStrategy llvm::parallel::strategy;
|
|
|
|
|
2020-07-09 01:50:53 +08:00
|
|
|
#if LLVM_ENABLE_THREADS
|
|
|
|
|
2019-04-25 19:33:30 +08:00
|
|
|
namespace llvm {
|
|
|
|
namespace parallel {
|
|
|
|
namespace detail {
|
2017-05-06 05:09:26 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// An abstract class that takes closures and runs them asynchronously.
|
2017-05-06 05:09:26 +08:00
|
|
|
class Executor {
|
|
|
|
public:
|
|
|
|
virtual ~Executor() = default;
|
|
|
|
virtual void add(std::function<void()> func) = 0;
|
|
|
|
|
|
|
|
static Executor *getDefaultExecutor();
|
|
|
|
};
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// An implementation of an Executor that runs closures on a thread pool
|
2017-05-06 05:09:26 +08:00
|
|
|
/// in filo order.
|
|
|
|
class ThreadPoolExecutor : public Executor {
|
|
|
|
public:
|
[Support] On Windows, ensure hardware_concurrency() extends to all CPU sockets and all NUMA groups
The goal of this patch is to maximize CPU utilization on multi-socket or high core count systems, so that parallel computations such as LLD/ThinLTO can use all hardware threads in the system. Before this patch, on Windows, a maximum of 64 hardware threads could be used at most, in some cases dispatched only on one CPU socket.
== Background ==
Windows doesn't have a flat cpu_set_t like Linux. Instead, it projects hardware CPUs (or NUMA nodes) to applications through a concept of "processor groups". A "processor" is the smallest unit of execution on a CPU, that is, an hyper-thread if SMT is active; a core otherwise. There's a limit of 32-bit processors on older 32-bit versions of Windows, which later was raised to 64-processors with 64-bit versions of Windows. This limit comes from the affinity mask, which historically is represented by the sizeof(void*). Consequently, the concept of "processor groups" was introduced for dealing with systems with more than 64 hyper-threads.
By default, the Windows OS assigns only one "processor group" to each starting application, in a round-robin manner. If the application wants to use more processors, it needs to programmatically enable it, by assigning threads to other "processor groups". This also means that affinity cannot cross "processor group" boundaries; one can only specify a "preferred" group on start-up, but the application is free to allocate more groups if it wants to.
This creates a peculiar situation, where newer CPUs like the AMD EPYC 7702P (64-cores, 128-hyperthreads) are projected by the OS as two (2) "processor groups". This means that by default, an application can only use half of the cores. This situation could only get worse in the years to come, as dies with more cores will appear on the market.
== The problem ==
The heavyweight_hardware_concurrency() API was introduced so that only *one hardware thread per core* was used. Once that API returns, that original intention is lost, only the number of threads is retained. Consider a situation, on Windows, where the system has 2 CPU sockets, 18 cores each, each core having 2 hyper-threads, for a total of 72 hyper-threads. Both heavyweight_hardware_concurrency() and hardware_concurrency() currently return 36, because on Windows they are simply wrappers over std::thread::hardware_concurrency() -- which can only return processors from the current "processor group".
== The changes in this patch ==
To solve this situation, we capture (and retain) the initial intention until the point of usage, through a new ThreadPoolStrategy class. The number of threads to use is deferred as late as possible, until the moment where the std::threads are created (ThreadPool in the case of ThinLTO).
When using hardware_concurrency(), setting ThreadCount to 0 now means to use all the possible hardware CPU (SMT) threads. Providing a ThreadCount above to the maximum number of threads will have no effect, the maximum will be used instead.
The heavyweight_hardware_concurrency() is similar to hardware_concurrency(), except that only one thread per hardware *core* will be used.
When LLVM_ENABLE_THREADS is OFF, the threading APIs will always return 1, to ensure any caller loops will be exercised at least once.
Differential Revision: https://reviews.llvm.org/D71775
2020-02-14 11:49:57 +08:00
|
|
|
explicit ThreadPoolExecutor(ThreadPoolStrategy S = hardware_concurrency()) {
|
|
|
|
unsigned ThreadCount = S.compute_thread_count();
|
2017-05-06 05:09:26 +08:00
|
|
|
// Spawn all but one of the threads in another thread as spawning threads
|
|
|
|
// can take a while.
|
2019-03-17 03:36:29 +08:00
|
|
|
Threads.reserve(ThreadCount);
|
|
|
|
Threads.resize(1);
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
[Support] On Windows, ensure hardware_concurrency() extends to all CPU sockets and all NUMA groups
The goal of this patch is to maximize CPU utilization on multi-socket or high core count systems, so that parallel computations such as LLD/ThinLTO can use all hardware threads in the system. Before this patch, on Windows, a maximum of 64 hardware threads could be used at most, in some cases dispatched only on one CPU socket.
== Background ==
Windows doesn't have a flat cpu_set_t like Linux. Instead, it projects hardware CPUs (or NUMA nodes) to applications through a concept of "processor groups". A "processor" is the smallest unit of execution on a CPU, that is, an hyper-thread if SMT is active; a core otherwise. There's a limit of 32-bit processors on older 32-bit versions of Windows, which later was raised to 64-processors with 64-bit versions of Windows. This limit comes from the affinity mask, which historically is represented by the sizeof(void*). Consequently, the concept of "processor groups" was introduced for dealing with systems with more than 64 hyper-threads.
By default, the Windows OS assigns only one "processor group" to each starting application, in a round-robin manner. If the application wants to use more processors, it needs to programmatically enable it, by assigning threads to other "processor groups". This also means that affinity cannot cross "processor group" boundaries; one can only specify a "preferred" group on start-up, but the application is free to allocate more groups if it wants to.
This creates a peculiar situation, where newer CPUs like the AMD EPYC 7702P (64-cores, 128-hyperthreads) are projected by the OS as two (2) "processor groups". This means that by default, an application can only use half of the cores. This situation could only get worse in the years to come, as dies with more cores will appear on the market.
== The problem ==
The heavyweight_hardware_concurrency() API was introduced so that only *one hardware thread per core* was used. Once that API returns, that original intention is lost, only the number of threads is retained. Consider a situation, on Windows, where the system has 2 CPU sockets, 18 cores each, each core having 2 hyper-threads, for a total of 72 hyper-threads. Both heavyweight_hardware_concurrency() and hardware_concurrency() currently return 36, because on Windows they are simply wrappers over std::thread::hardware_concurrency() -- which can only return processors from the current "processor group".
== The changes in this patch ==
To solve this situation, we capture (and retain) the initial intention until the point of usage, through a new ThreadPoolStrategy class. The number of threads to use is deferred as late as possible, until the moment where the std::threads are created (ThreadPool in the case of ThinLTO).
When using hardware_concurrency(), setting ThreadCount to 0 now means to use all the possible hardware CPU (SMT) threads. Providing a ThreadCount above to the maximum number of threads will have no effect, the maximum will be used instead.
The heavyweight_hardware_concurrency() is similar to hardware_concurrency(), except that only one thread per hardware *core* will be used.
When LLVM_ENABLE_THREADS is OFF, the threading APIs will always return 1, to ensure any caller loops will be exercised at least once.
Differential Revision: https://reviews.llvm.org/D71775
2020-02-14 11:49:57 +08:00
|
|
|
Threads[0] = std::thread([this, ThreadCount, S] {
|
|
|
|
for (unsigned I = 1; I < ThreadCount; ++I) {
|
|
|
|
Threads.emplace_back([=] { work(S, I); });
|
2019-03-17 03:36:29 +08:00
|
|
|
if (Stop)
|
|
|
|
break;
|
2017-05-06 05:09:26 +08:00
|
|
|
}
|
2019-03-17 03:36:29 +08:00
|
|
|
ThreadsCreated.set_value();
|
[Support] On Windows, ensure hardware_concurrency() extends to all CPU sockets and all NUMA groups
The goal of this patch is to maximize CPU utilization on multi-socket or high core count systems, so that parallel computations such as LLD/ThinLTO can use all hardware threads in the system. Before this patch, on Windows, a maximum of 64 hardware threads could be used at most, in some cases dispatched only on one CPU socket.
== Background ==
Windows doesn't have a flat cpu_set_t like Linux. Instead, it projects hardware CPUs (or NUMA nodes) to applications through a concept of "processor groups". A "processor" is the smallest unit of execution on a CPU, that is, an hyper-thread if SMT is active; a core otherwise. There's a limit of 32-bit processors on older 32-bit versions of Windows, which later was raised to 64-processors with 64-bit versions of Windows. This limit comes from the affinity mask, which historically is represented by the sizeof(void*). Consequently, the concept of "processor groups" was introduced for dealing with systems with more than 64 hyper-threads.
By default, the Windows OS assigns only one "processor group" to each starting application, in a round-robin manner. If the application wants to use more processors, it needs to programmatically enable it, by assigning threads to other "processor groups". This also means that affinity cannot cross "processor group" boundaries; one can only specify a "preferred" group on start-up, but the application is free to allocate more groups if it wants to.
This creates a peculiar situation, where newer CPUs like the AMD EPYC 7702P (64-cores, 128-hyperthreads) are projected by the OS as two (2) "processor groups". This means that by default, an application can only use half of the cores. This situation could only get worse in the years to come, as dies with more cores will appear on the market.
== The problem ==
The heavyweight_hardware_concurrency() API was introduced so that only *one hardware thread per core* was used. Once that API returns, that original intention is lost, only the number of threads is retained. Consider a situation, on Windows, where the system has 2 CPU sockets, 18 cores each, each core having 2 hyper-threads, for a total of 72 hyper-threads. Both heavyweight_hardware_concurrency() and hardware_concurrency() currently return 36, because on Windows they are simply wrappers over std::thread::hardware_concurrency() -- which can only return processors from the current "processor group".
== The changes in this patch ==
To solve this situation, we capture (and retain) the initial intention until the point of usage, through a new ThreadPoolStrategy class. The number of threads to use is deferred as late as possible, until the moment where the std::threads are created (ThreadPool in the case of ThinLTO).
When using hardware_concurrency(), setting ThreadCount to 0 now means to use all the possible hardware CPU (SMT) threads. Providing a ThreadCount above to the maximum number of threads will have no effect, the maximum will be used instead.
The heavyweight_hardware_concurrency() is similar to hardware_concurrency(), except that only one thread per hardware *core* will be used.
When LLVM_ENABLE_THREADS is OFF, the threading APIs will always return 1, to ensure any caller loops will be exercised at least once.
Differential Revision: https://reviews.llvm.org/D71775
2020-02-14 11:49:57 +08:00
|
|
|
work(S, 0);
|
2019-03-17 03:36:29 +08:00
|
|
|
});
|
2017-05-06 05:09:26 +08:00
|
|
|
}
|
|
|
|
|
2019-03-17 03:36:29 +08:00
|
|
|
void stop() {
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
if (Stop)
|
|
|
|
return;
|
|
|
|
Stop = true;
|
|
|
|
}
|
2017-05-06 05:09:26 +08:00
|
|
|
Cond.notify_all();
|
2019-03-17 03:36:29 +08:00
|
|
|
ThreadsCreated.get_future().wait();
|
2017-05-06 05:09:26 +08:00
|
|
|
}
|
|
|
|
|
2019-03-17 03:36:29 +08:00
|
|
|
~ThreadPoolExecutor() override {
|
|
|
|
stop();
|
|
|
|
std::thread::id CurrentThreadId = std::this_thread::get_id();
|
|
|
|
for (std::thread &T : Threads)
|
|
|
|
if (T.get_id() == CurrentThreadId)
|
|
|
|
T.detach();
|
|
|
|
else
|
|
|
|
T.join();
|
|
|
|
}
|
|
|
|
|
[lld][COFF][ELF][WebAssembly] Replace --[no-]threads /threads[:no] with --threads={1,2,...} /threads:{1,2,...}
--no-threads is a name copied from gold.
gold has --no-thread, --thread-count and several other --thread-count-*.
There are needs to customize the number of threads (running several lld
processes concurrently or customizing the number of LTO threads).
Having a single --threads=N is a straightforward replacement of gold's
--no-threads + --thread-count.
--no-threads is used rarely. So just delete --no-threads instead of
keeping it for compatibility for a while.
If --threads= is specified (ELF,wasm; COFF /threads: is similar),
--thinlto-jobs= defaults to --threads=,
otherwise all available hardware threads are used.
There is currently no way to override a --threads={1,2,...}. It is still
a debate whether we should use --threads=all.
Reviewed By: rnk, aganea
Differential Revision: https://reviews.llvm.org/D76885
2020-03-18 03:40:19 +08:00
|
|
|
struct Creator {
|
|
|
|
static void *call() { return new ThreadPoolExecutor(strategy); }
|
|
|
|
};
|
2019-03-17 03:36:29 +08:00
|
|
|
struct Deleter {
|
|
|
|
static void call(void *Ptr) { ((ThreadPoolExecutor *)Ptr)->stop(); }
|
|
|
|
};
|
|
|
|
|
2017-05-06 05:09:26 +08:00
|
|
|
void add(std::function<void()> F) override {
|
2019-03-17 03:36:29 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
WorkStack.push(F);
|
|
|
|
}
|
2017-05-06 05:09:26 +08:00
|
|
|
Cond.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
[Support] On Windows, ensure hardware_concurrency() extends to all CPU sockets and all NUMA groups
The goal of this patch is to maximize CPU utilization on multi-socket or high core count systems, so that parallel computations such as LLD/ThinLTO can use all hardware threads in the system. Before this patch, on Windows, a maximum of 64 hardware threads could be used at most, in some cases dispatched only on one CPU socket.
== Background ==
Windows doesn't have a flat cpu_set_t like Linux. Instead, it projects hardware CPUs (or NUMA nodes) to applications through a concept of "processor groups". A "processor" is the smallest unit of execution on a CPU, that is, an hyper-thread if SMT is active; a core otherwise. There's a limit of 32-bit processors on older 32-bit versions of Windows, which later was raised to 64-processors with 64-bit versions of Windows. This limit comes from the affinity mask, which historically is represented by the sizeof(void*). Consequently, the concept of "processor groups" was introduced for dealing with systems with more than 64 hyper-threads.
By default, the Windows OS assigns only one "processor group" to each starting application, in a round-robin manner. If the application wants to use more processors, it needs to programmatically enable it, by assigning threads to other "processor groups". This also means that affinity cannot cross "processor group" boundaries; one can only specify a "preferred" group on start-up, but the application is free to allocate more groups if it wants to.
This creates a peculiar situation, where newer CPUs like the AMD EPYC 7702P (64-cores, 128-hyperthreads) are projected by the OS as two (2) "processor groups". This means that by default, an application can only use half of the cores. This situation could only get worse in the years to come, as dies with more cores will appear on the market.
== The problem ==
The heavyweight_hardware_concurrency() API was introduced so that only *one hardware thread per core* was used. Once that API returns, that original intention is lost, only the number of threads is retained. Consider a situation, on Windows, where the system has 2 CPU sockets, 18 cores each, each core having 2 hyper-threads, for a total of 72 hyper-threads. Both heavyweight_hardware_concurrency() and hardware_concurrency() currently return 36, because on Windows they are simply wrappers over std::thread::hardware_concurrency() -- which can only return processors from the current "processor group".
== The changes in this patch ==
To solve this situation, we capture (and retain) the initial intention until the point of usage, through a new ThreadPoolStrategy class. The number of threads to use is deferred as late as possible, until the moment where the std::threads are created (ThreadPool in the case of ThinLTO).
When using hardware_concurrency(), setting ThreadCount to 0 now means to use all the possible hardware CPU (SMT) threads. Providing a ThreadCount above to the maximum number of threads will have no effect, the maximum will be used instead.
The heavyweight_hardware_concurrency() is similar to hardware_concurrency(), except that only one thread per hardware *core* will be used.
When LLVM_ENABLE_THREADS is OFF, the threading APIs will always return 1, to ensure any caller loops will be exercised at least once.
Differential Revision: https://reviews.llvm.org/D71775
2020-02-14 11:49:57 +08:00
|
|
|
void work(ThreadPoolStrategy S, unsigned ThreadID) {
|
|
|
|
S.apply_thread_strategy(ThreadID);
|
2017-05-06 05:09:26 +08:00
|
|
|
while (true) {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
Cond.wait(Lock, [&] { return Stop || !WorkStack.empty(); });
|
|
|
|
if (Stop)
|
|
|
|
break;
|
|
|
|
auto Task = WorkStack.top();
|
|
|
|
WorkStack.pop();
|
|
|
|
Lock.unlock();
|
|
|
|
Task();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::atomic<bool> Stop{false};
|
|
|
|
std::stack<std::function<void()>> WorkStack;
|
|
|
|
std::mutex Mutex;
|
|
|
|
std::condition_variable Cond;
|
2019-03-17 03:36:29 +08:00
|
|
|
std::promise<void> ThreadsCreated;
|
|
|
|
std::vector<std::thread> Threads;
|
2017-05-06 05:09:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Executor *Executor::getDefaultExecutor() {
|
2019-03-17 03:36:29 +08:00
|
|
|
// The ManagedStatic enables the ThreadPoolExecutor to be stopped via
|
|
|
|
// llvm_shutdown() which allows a "clean" fast exit, e.g. via _exit(). This
|
|
|
|
// stops the thread pool and waits for any worker thread creation to complete
|
|
|
|
// but does not wait for the threads to finish. The wait for worker thread
|
|
|
|
// creation to complete is important as it prevents intermittent crashes on
|
|
|
|
// Windows due to a race condition between thread creation and process exit.
|
|
|
|
//
|
|
|
|
// The ThreadPoolExecutor will only be destroyed when the static unique_ptr to
|
|
|
|
// it is destroyed, i.e. in a normal full exit. The ThreadPoolExecutor
|
|
|
|
// destructor ensures it has been stopped and waits for worker threads to
|
|
|
|
// finish. The wait is important as it prevents intermittent crashes on
|
|
|
|
// Windows when the process is doing a full exit.
|
|
|
|
//
|
|
|
|
// The Windows crashes appear to only occur with the MSVC static runtimes and
|
|
|
|
// are more frequent with the debug static runtime.
|
|
|
|
//
|
|
|
|
// This also prevents intermittent deadlocks on exit with the MinGW runtime.
|
[lld][COFF][ELF][WebAssembly] Replace --[no-]threads /threads[:no] with --threads={1,2,...} /threads:{1,2,...}
--no-threads is a name copied from gold.
gold has --no-thread, --thread-count and several other --thread-count-*.
There are needs to customize the number of threads (running several lld
processes concurrently or customizing the number of LTO threads).
Having a single --threads=N is a straightforward replacement of gold's
--no-threads + --thread-count.
--no-threads is used rarely. So just delete --no-threads instead of
keeping it for compatibility for a while.
If --threads= is specified (ELF,wasm; COFF /threads: is similar),
--thinlto-jobs= defaults to --threads=,
otherwise all available hardware threads are used.
There is currently no way to override a --threads={1,2,...}. It is still
a debate whether we should use --threads=all.
Reviewed By: rnk, aganea
Differential Revision: https://reviews.llvm.org/D76885
2020-03-18 03:40:19 +08:00
|
|
|
|
|
|
|
static ManagedStatic<ThreadPoolExecutor, ThreadPoolExecutor::Creator,
|
2019-03-17 03:36:29 +08:00
|
|
|
ThreadPoolExecutor::Deleter>
|
|
|
|
ManagedExec;
|
|
|
|
static std::unique_ptr<ThreadPoolExecutor> Exec(&(*ManagedExec));
|
|
|
|
return Exec.get();
|
2017-05-06 05:09:26 +08:00
|
|
|
}
|
2019-10-11 02:57:23 +08:00
|
|
|
} // namespace
|
2017-05-06 05:09:26 +08:00
|
|
|
|
2019-04-25 19:33:30 +08:00
|
|
|
static std::atomic<int> TaskGroupInstances;
|
|
|
|
|
|
|
|
// Latch::sync() called by the dtor may cause one thread to block. If is a dead
|
|
|
|
// lock if all threads in the default executor are blocked. To prevent the dead
|
|
|
|
// lock, only allow the first TaskGroup to run tasks parallelly. In the scenario
|
|
|
|
// of nested parallel_for_each(), only the outermost one runs parallelly.
|
|
|
|
TaskGroup::TaskGroup() : Parallel(TaskGroupInstances++ == 0) {}
|
|
|
|
TaskGroup::~TaskGroup() { --TaskGroupInstances; }
|
|
|
|
|
|
|
|
void TaskGroup::spawn(std::function<void()> F) {
|
|
|
|
if (Parallel) {
|
|
|
|
L.inc();
|
|
|
|
Executor::getDefaultExecutor()->add([&, F] {
|
|
|
|
F();
|
|
|
|
L.dec();
|
|
|
|
});
|
|
|
|
} else {
|
2017-05-11 01:39:18 +08:00
|
|
|
F();
|
2019-04-25 19:33:30 +08:00
|
|
|
}
|
2017-05-06 05:09:26 +08:00
|
|
|
}
|
2019-04-25 19:33:30 +08:00
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace parallel
|
|
|
|
} // namespace llvm
|
2018-05-11 23:25:38 +08:00
|
|
|
#endif // LLVM_ENABLE_THREADS
|