llvm.org GIT mirror llvm / 70deadb
Use sched_getaffinity instead of std::thread::hardware_concurrency. The issue with std::thread::hardware_concurrency is that it forwards to libc and some implementations (like glibc) don't take thread affinity into consideration. With this change a llvm program that can execute in only 2 cores will use 2 threads, even if the machine has 32 cores. This makes benchmarking a lot easier, but should also help if someone doesn't want to use all cores for compilation for example. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@314809 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 1 year, 11 months ago
9 changed file(s) with 36 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
268268 add_definitions( -D_GNU_SOURCE )
269269 endif()
270270 # This check requires _GNU_SOURCE
271 check_library_exists(c sched_getaffinity "" HAVE_SCHED_GETAFFINITY)
271272 if(HAVE_LIBPTHREAD)
272273 check_library_exists(pthread pthread_getname_np "" HAVE_PTHREAD_GETNAME_NP)
273274 check_library_exists(pthread pthread_setname_np "" HAVE_PTHREAD_SETNAME_NP)
184184 /* Define to 1 if you have the `setenv' function. */
185185 #cmakedefine HAVE_SETENV ${HAVE_SETENV}
186186
187 /* Define to 1 if you have the `sched_getaffinity' function. */
188 #cmakedefine HAVE_SCHED_GETAFFINITY ${HAVE_SCHED_GETAFFINITY}
189
187190 /* Define to 1 if you have the `setrlimit' function. */
188191 #cmakedefine HAVE_SETRLIMIT ${HAVE_SETRLIMIT}
189192
3737 using TaskTy = std::function;
3838 using PackagedTaskTy = std::packaged_task;
3939
40 /// Construct a pool with the number of core available on the system (or
41 /// whatever the value returned by std::thread::hardware_concurrency() is).
40 /// Construct a pool with the number of threads found by
41 /// hardware_concurrency().
4242 ThreadPool();
4343
4444 /// Construct a pool of \p ThreadCount threads
130130 /// Returns 1 when LLVM is configured with LLVM_ENABLE_THREADS=OFF
131131 unsigned heavyweight_hardware_concurrency();
132132
133 /// Get the number of threads that the current program can execute
134 /// concurrently. On some systems std::thread::hardware_concurrency() returns
135 /// the total number of cores, without taking affinity into consideration.
136 /// Returns 1 when LLVM is configured with LLVM_ENABLE_THREADS=OFF.
137 /// Fallback to std::thread::hardware_concurrency() if sched_getaffinity is
138 /// not available.
139 unsigned hardware_concurrency();
140
133141 /// \brief Return the current thread id, as used in various OS system calls.
134142 /// Note that not all platforms guarantee that the value returned will be
135143 /// unique across the entire system, so portable code should not assume
194194 Printf(FallbackFMT, PC);
195195 }
196196
197 unsigned NumberOfCpuCores() {
198 unsigned N = std::thread::hardware_concurrency();
199 if (!N) {
200 Printf("WARNING: std::thread::hardware_concurrency not well defined for "
201 "your platform. Assuming CPU count of 1.\n");
202 N = 1;
203 }
204 return N;
205 }
197 unsigned NumberOfCpuCores() { return hardware_concurrency(); }
206198
207199 size_t SimpleFastHash(const uint8_t *Data, size_t Size) {
208200 size_t Res = 0;
88
99 #include "llvm/Support/Parallel.h"
1010 #include "llvm/Config/llvm-config.h"
11 #include "llvm/Support/Threading.h"
1112
1213 #include
1314 #include
6970 /// in filo order.
7071 class ThreadPoolExecutor : public Executor {
7172 public:
72 explicit ThreadPoolExecutor(
73 unsigned ThreadCount = std::thread::hardware_concurrency())
73 explicit ThreadPoolExecutor(unsigned ThreadCount = hardware_concurrency())
7474 : Done(ThreadCount) {
7575 // Spawn all but one of the threads in another thread as spawning threads
7676 // can take a while.
1313 #include "llvm/Support/ThreadPool.h"
1414
1515 #include "llvm/Config/llvm-config.h"
16 #include "llvm/Support/Threading.h"
1617 #include "llvm/Support/raw_ostream.h"
1718
1819 using namespace llvm;
1920
2021 #if LLVM_ENABLE_THREADS
2122
22 // Default to std::thread::hardware_concurrency
23 ThreadPool::ThreadPool() : ThreadPool(std::thread::hardware_concurrency()) {}
23 // Default to hardware_concurrency
24 ThreadPool::ThreadPool() : ThreadPool(hardware_concurrency()) {}
2425
2526 ThreadPool::ThreadPool(unsigned ThreadCount)
2627 : ActiveThreads(0), EnableFlag(true) {
4646
4747 unsigned llvm::heavyweight_hardware_concurrency() { return 1; }
4848
49 unsigned llvm::hardware_concurrency() { return 1; }
50
4951 uint64_t llvm::get_threadid() { return 0; }
5052
5153 uint32_t llvm::get_max_thread_name_length() { return 0; }
7072 return NumPhysical;
7173 }
7274
75 unsigned llvm::hardware_concurrency() {
76 #ifdef HAVE_SCHED_GETAFFINITY
77 cpu_set_t Set;
78 if (sched_getaffinity(0, sizeof(Set), &Set))
79 return CPU_COUNT(&Set);
80 #endif
81 // Guard against std::thread::hardware_concurrency() returning 0.
82 if (unsigned Val = std::thread::hardware_concurrency())
83 return Val;
84 return 1;
85 }
86
7387 // Include the platform-specific parts of this class.
7488 #ifdef LLVM_ON_UNIX
7589 #include "Unix/Threading.inc"
210210
211211 // If NumThreads is not specified, auto-detect a good default.
212212 if (NumThreads == 0)
213 NumThreads = std::max(1U, std::min(std::thread::hardware_concurrency(),
214 unsigned(Inputs.size() / 2)));
213 NumThreads =
214 std::min(hardware_concurrency(), unsigned((Inputs.size() + 1) / 2));
215215
216216 // Initialize the writer contexts.
217217 SmallVector, 4> Contexts;