20 #ifndef GALOIS_SUBSTRATE_THREADPOOL_H
21 #define GALOIS_SUBSTRATE_THREADPOOL_H
25 #include <condition_variable>
34 namespace galois::substrate::internal {
36 template <
typename tpl,
int s,
int r>
37 struct ExecuteTupleImpl {
38 static inline void execute(tpl& cmds) {
44 template <
typename tpl,
int s>
45 struct ExecuteTupleImpl<tpl, s, 0> {
46 static inline void execute(tpl&) {}
51 namespace galois::substrate {
62 std::function<void(void)>
fn;
67 std::condition_variable
cv;
79 std::lock_guard<std::mutex> lg(
m);
88 while (!
fastRelease.load(std::memory_order_relaxed)) {
93 std::unique_lock<std::mutex> lg(
m);
94 cv.wait(lg, [=] {
return !
done; });
108 std::function<void(void)>
work;
141 template <
typename... Args>
142 void run(
unsigned num, Args&&... args) {
143 struct ExecuteTuple {
145 std::tuple<Args...> cmds;
148 internal::ExecuteTupleImpl<
149 std::tuple<Args...>, 0,
150 std::tuple_size<std::tuple<Args...>>::value>
::execute(this->cmds);
152 ExecuteTuple(Args&&... args) : cmds(std::forward<Args>(args)...) {}
156 ExecuteTuple lwork(std::forward<Args>(args)...);
157 work = std::ref(lwork);
191 return signals[tid]->topo.socketLeader == tid;
195 return signals[tid]->topo.socketLeader;
198 return signals[tid]->topo.cumulativeMaxSocket;
201 return signals[tid]->topo.numaNode;
221 namespace galois::substrate::internal {
223 void setThreadPool(ThreadPool* tp);
void beKind()
Definition: ThreadPool.cpp:83
unsigned maxThreads
Definition: HWTopo.h:41
ThreadPool & getThreadPool(void)
return a reference to system thread pool
Definition: ThreadPool.cpp:259
void threadLoop(unsigned tid)
main thread loop
Definition: ThreadPool.cpp:132
void run(unsigned num, Args &&...args)
execute work on all threads a simple wrapper for run
Definition: ThreadPool.h:142
unsigned wend
Definition: ThreadPool.h:69
std::function< void(void)> fn
Definition: ThreadPool.h:62
std::function< void(void)> work
Definition: ThreadPool.h:108
static unsigned getLeader()
Definition: ThreadPool.h:206
unsigned tid
Definition: HWTopo.h:31
void wait(bool fastmode)
Definition: ThreadPool.h:86
std::atomic< int > done
Definition: ThreadPool.h:70
unsigned getMaxThreads() const
return the number of threads supported by the thread pool on the current machine
Definition: ThreadPool.h:178
unsigned getMaxCores() const
Definition: ThreadPool.h:179
type for setting fastmode
Definition: ThreadPool.h:61
unsigned maxSockets
Definition: HWTopo.h:43
std::condition_variable cv
Definition: ThreadPool.h:67
std::atomic< int > fastRelease
Definition: ThreadPool.h:71
void decascade()
spin down after run
Definition: ThreadPool.cpp:160
unsigned reserved
Definition: ThreadPool.h:105
unsigned maxCores
Definition: HWTopo.h:42
unsigned getNumaNode(unsigned tid) const
Definition: ThreadPool.h:200
static unsigned getCumulativeMaxSocket()
Definition: ThreadPool.h:208
bool running
Definition: ThreadPool.h:107
unsigned socketLeader
Definition: HWTopo.h:32
Definition: substrate/SharedMem.h:32
std::mutex m
Definition: ThreadPool.h:68
unsigned numaNode
Definition: HWTopo.h:34
unsigned getCumulativeMaxSocket(unsigned tid) const
Definition: ThreadPool.h:197
void runInternal(unsigned num)
execute work on num threads
Definition: ThreadPool.cpp:203
unsigned wbegin
Definition: ThreadPool.h:69
void runDedicated(std::function< void(void)> &f)
run function in a dedicated thread until the threadpool exits
Definition: ThreadPool.cpp:231
static unsigned getTID()
Definition: ThreadPool.h:204
std::vector< per_signal * > signals
Definition: ThreadPool.h:103
bool isLeader(unsigned tid) const
Definition: ThreadPool.h:190
void initThread(unsigned tid)
Initialize a thread.
Definition: ThreadPool.cpp:118
static unsigned getSocket()
Definition: ThreadPool.h:207
unsigned getMaxNumaNodes() const
Definition: ThreadPool.h:181
unsigned getLeader(unsigned tid) const
Definition: ThreadPool.h:194
void burnPower(unsigned num)
Definition: ThreadPool.cpp:70
void operator()(void)
Definition: Executor_ParaMeter.h:417
bool mode
Definition: ThreadPool.h:59
static bool isLeader()
Definition: ThreadPool.h:205
ThreadTopoInfo topo
Definition: ThreadPool.h:72
std::vector< std::thread > threads
Definition: ThreadPool.h:104
Definition: ThreadPool.h:53
Definition: ThreadPool.h:57
void cascade(bool fastmode)
spin up for run
Definition: ThreadPool.cpp:179
unsigned masterFastmode
Definition: ThreadPool.h:106
unsigned getMaxSockets() const
Definition: ThreadPool.h:180
static thread_local per_signal my_box
Definition: ThreadPool.h:100
ThreadPool & operator=(const ThreadPool &)=delete
unsigned getLeaderForSocket(unsigned pid) const
Definition: ThreadPool.h:183
void wakeup(bool fastmode)
Definition: ThreadPool.h:74
MachineTopoInfo mi
Definition: ThreadPool.h:102
type for shutting down thread
Definition: ThreadPool.h:58
type to switch to dedicated mode
Definition: ThreadPool.h:66
unsigned cumulativeMaxSocket
Definition: HWTopo.h:35
unsigned getMaxUsableThreads() const
return the number of non-reserved threads in the pool
Definition: ThreadPool.h:175
unsigned maxNumaNodes
Definition: HWTopo.h:44
ThreadPool()
Definition: ThreadPool.cpp:40
bool isRunning() const
Definition: ThreadPool.h:172
unsigned getSocket(unsigned tid) const
Definition: ThreadPool.h:193
static unsigned getNumaNode()
Definition: ThreadPool.h:211
void destroyCommon()
destroy all threads
Definition: ThreadPool.cpp:65
~ThreadPool()
Definition: ThreadPool.cpp:58
class galois::runtime::ParaMeter::ParaMeterExecutor execute(const R &range)
Definition: Executor_ParaMeter.h:360
unsigned socket
Definition: HWTopo.h:33