llvm.org GIT mirror llvm / a72e2a9
Bring r314809 back. But now include a check for CPU_COUNT so we still build on 10 year old versions of glibc. Original message: 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@314931 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 3 years ago
8 changed file(s) with 40 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
266266 check_symbol_exists(__GLIBC__ stdio.h LLVM_USING_GLIBC)
267267 if( LLVM_USING_GLIBC )
268268 add_definitions( -D_GNU_SOURCE )
269 list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
269270 endif()
270271 # This check requires _GNU_SOURCE
272 check_symbol_exists(sched_getaffinity sched.h HAVE_SCHED_GETAFFINITY)
273 check_symbol_exists(CPU_COUNT sched.h HAVE_CPU_COUNT)
271274 if(HAVE_LIBPTHREAD)
272275 check_library_exists(pthread pthread_getname_np "" HAVE_PTHREAD_GETNAME_NP)
273276 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
190 /* Define to 1 if you have the `CPU_COUNT' macro. */
191 #cmakedefine HAVE_CPU_COUNT ${HAVE_CPU_COUNT}
192
187193 /* Define to 1 if you have the `setrlimit' function. */
188194 #cmakedefine HAVE_SETRLIMIT ${HAVE_SETRLIMIT}
189195
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
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 #if defined(HAVE_SCHED_GETAFFINITY) && defined(HAVE_CPU_COUNT)
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;