Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

FMEThread.h
Go to the documentation of this file.
1 
32 #pragma once
33 
35 
36 #include <cstdint>
37 
38 namespace ogdf {
39 class Barrier;
40 
41 namespace fast_multipole_embedder {
42 
43 class FMEThreadPool;
44 
49 class FMETask {
50 public:
51  virtual ~FMETask() { }
52 
53  virtual void doWork() = 0;
54 };
55 
59 template<typename FuncInvokerType>
60 class FMEFuncInvokerTask : public FMETask {
61 public:
63  FMEFuncInvokerTask(FuncInvokerType f) : funcInvoker(f) { }
64 
66  void doWork() override { funcInvoker(); }
67 
68 private:
70  FuncInvokerType funcInvoker;
71 };
72 
76 class FMEThread /*: public Thread*/
77 {
78 public:
80  FMEThread(FMEThreadPool* pThreadPool, uint32_t threadNr);
81 
83  inline uint32_t threadNr() const { return m_threadNr; }
84 
86  inline uint32_t numThreads() const { return m_numThreads; }
87 
89  inline bool isMainThread() const { return m_threadNr == 0; }
90 
92  inline FMEThreadPool* threadPool() const { return m_pThreadPool; }
93 
95  void sync();
96 
97 #ifdef OGDF_HAS_LINUX_CPU_MACROS
98  void unixSetAffinity();
99 #else
100  void unixSetAffinity() { }
101 #endif
102 
104  void operator()() {
105  unixSetAffinity();
106  m_pTask->doWork();
107  delete m_pTask;
108  m_pTask = nullptr;
109  }
110 
112  void setTask(FMETask* pTask) { m_pTask = pTask; }
113 
114 private:
115  uint32_t m_threadNr;
116 
117  uint32_t m_numThreads;
118 
120 
122 
123  FMEThread(const FMEThread&); // = delete
124  FMEThread& operator=(const FMEThread&); // = delete
125 };
126 
128 public:
129  explicit FMEThreadPool(uint32_t numThreads);
130 
131  ~FMEThreadPool();
132 
134  inline uint32_t numThreads() const { return m_numThreads; }
135 
137  inline FMEThread* thread(uint32_t threadNr) const { return m_pThreads[threadNr]; }
138 
140  inline Barrier* syncBarrier() const { return m_pSyncBarrier; }
141 
143  void runThreads();
144 
145  template<typename KernelType, typename ArgType1>
146  void runKernel(ArgType1 arg1) {
147  for (uint32_t i = 0; i < numThreads(); i++) {
148  KernelType kernel(thread(i));
149  FuncInvoker<KernelType, ArgType1> invoker(kernel, arg1);
151  }
152  runThreads();
153  }
154 
155 private:
156  void allocate();
157 
158  void deallocate();
159 
160  uint32_t m_numThreads;
161 
163 
165 };
166 
167 }
168 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::fast_multipole_embedder::FMEThread::setTask
void setTask(FMETask *pTask)
sets the actual task
Definition: FMEThread.h:112
ogdf::fast_multipole_embedder::FMEThread
The fast multipole embedder work thread class.
Definition: FMEThread.h:76
ogdf::fast_multipole_embedder::FMEFuncInvokerTask::FMEFuncInvokerTask
FMEFuncInvokerTask(FuncInvokerType f)
constructor with an invoker
Definition: FMEThread.h:63
ogdf::fast_multipole_embedder::FMEThreadPool::allocate
void allocate()
ogdf::fast_multipole_embedder::FMEThreadPool::runKernel
void runKernel(ArgType1 arg1)
Definition: FMEThread.h:146
ogdf::fast_multipole_embedder::FMEThread::isMainThread
bool isMainThread() const
returns true if this is the main thread ( the main thread is always the first thread )
Definition: FMEThread.h:89
ogdf::fast_multipole_embedder::FMEThreadPool::runThreads
void runThreads()
runs one iteration. This call blocks the main thread
ogdf::fast_multipole_embedder::FMEThreadPool::FMEThreadPool
FMEThreadPool(uint32_t numThreads)
ogdf::fast_multipole_embedder::FMEThreadPool::~FMEThreadPool
~FMEThreadPool()
ogdf::fast_multipole_embedder::FMEThread::m_pThreadPool
FMEThreadPool * m_pThreadPool
Definition: FMEThread.h:119
ogdf::fast_multipole_embedder::FMEThread::threadPool
FMEThreadPool * threadPool() const
returns the ThreadPool this thread belongs to
Definition: FMEThread.h:92
ogdf::fast_multipole_embedder::FuncInvoker
Definition: FastUtils.h:344
ogdf::fast_multipole_embedder::FMEThreadPool
Definition: FMEThread.h:127
FastUtils.h
Definition of utility functions for FME layout.
ogdf::fast_multipole_embedder::FMEThreadPool::thread
FMEThread * thread(uint32_t threadNr) const
returns the threadNr-th thread
Definition: FMEThread.h:137
ogdf::fast_multipole_embedder::FMEFuncInvokerTask::funcInvoker
FuncInvokerType funcInvoker
the invoker
Definition: FMEThread.h:70
ogdf::fast_multipole_embedder::FMEFuncInvokerTask
Class used to invoke a functor or function inside a thread.
Definition: FMEThread.h:60
ogdf::fast_multipole_embedder::FMEThread::operator()
void operator()()
the main work function
Definition: FMEThread.h:104
ogdf::fast_multipole_embedder::FMEThread::numThreads
uint32_t numThreads() const
returns the total number of threads in the pool
Definition: FMEThread.h:86
ogdf::fast_multipole_embedder::FMETask::~FMETask
virtual ~FMETask()
Definition: FMEThread.h:51
ogdf::fast_multipole_embedder::FMEThread::unixSetAffinity
void unixSetAffinity()
ogdf::fast_multipole_embedder::FMETask::doWork
virtual void doWork()=0
ogdf::fast_multipole_embedder::FMEThreadPool::m_numThreads
uint32_t m_numThreads
Definition: FMEThread.h:160
ogdf::fast_multipole_embedder::FMEThreadPool::deallocate
void deallocate()
ogdf::fast_multipole_embedder::FMEThreadPool::m_pSyncBarrier
Barrier * m_pSyncBarrier
Definition: FMEThread.h:164
ogdf::fast_multipole_embedder::FMEThreadPool::numThreads
uint32_t numThreads() const
returns the number of threads in this pool
Definition: FMEThread.h:134
ogdf::Barrier
Representation of a barrier.
Definition: Barrier.h:48
ogdf::fast_multipole_embedder::FMETask
The thread task class used only as an interface.
Definition: FMEThread.h:49
ogdf::fast_multipole_embedder::FMEThread::operator=
FMEThread & operator=(const FMEThread &)
ogdf::fast_multipole_embedder::FMEThread::sync
void sync()
thread sync call
ogdf::fast_multipole_embedder::FMEThread::m_pTask
FMETask * m_pTask
Definition: FMEThread.h:121
ogdf::fast_multipole_embedder::FMEFuncInvokerTask::doWork
void doWork() override
overrides the task doWork() method and invokes the function or functor
Definition: FMEThread.h:66
ogdf::fast_multipole_embedder::FMEThreadPool::syncBarrier
Barrier * syncBarrier() const
returns the barrier instance used to sync the threads during execution
Definition: FMEThread.h:140
ogdf::fast_multipole_embedder::FMEThreadPool::m_pThreads
FMEThread ** m_pThreads
Definition: FMEThread.h:162
ogdf::fast_multipole_embedder::FMEThread::m_threadNr
uint32_t m_threadNr
Definition: FMEThread.h:115
ogdf::fast_multipole_embedder::FMEThread::FMEThread
FMEThread(FMEThreadPool *pThreadPool, uint32_t threadNr)
construtor
ogdf::fast_multipole_embedder::FMEThread::threadNr
uint32_t threadNr() const
returns the index of the thread ( 0.. numThreads()-1 )
Definition: FMEThread.h:83
ogdf::fast_multipole_embedder::FMEThread::m_numThreads
uint32_t m_numThreads
Definition: FMEThread.h:117