Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

FMEThread.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Thread.h>
35 #include <ogdf/basic/Barrier.h>
36 
40 
41 namespace ogdf {
42 namespace fast_multipole_embedder {
43 
44 class FMEThreadPool;
45 
50 class FMETask
51 {
52 public:
53  virtual ~FMETask() { }
54 
55  virtual void doWork() = 0;
56 };
57 
58 
62 template<typename FuncInvokerType>
64 {
65 public:
67  FMEFuncInvokerTask(FuncInvokerType f) : funcInvoker(f) { }
68 
70  void doWork() override { funcInvoker(); }
71 private:
73  FuncInvokerType funcInvoker;
74 };
75 
76 
80 class FMEThread /*: public Thread*/
81 {
82 public:
84  FMEThread(FMEThreadPool* pThreadPool, uint32_t threadNr);
85 
87  inline uint32_t threadNr() const { return m_threadNr; }
88 
90  inline uint32_t numThreads() const { return m_numThreads; }
91 
93  inline bool isMainThread() const { return m_threadNr == 0; }
94 
96  inline FMEThreadPool* threadPool() const { return m_pThreadPool; }
97 
99  void sync();
100 
101 #ifdef OGDF_HAS_LINUX_CPU_MACROS
102  void unixSetAffinity();
103 #else
104  void unixSetAffinity() { }
105 #endif
106 
108  void operator()() {
109  unixSetAffinity();
110  m_pTask->doWork();
111  delete m_pTask;
112  m_pTask = nullptr;
113  }
114 
116  void setTask(FMETask* pTask) { m_pTask = pTask; }
117 
118 private:
119  uint32_t m_threadNr;
120 
121  uint32_t m_numThreads;
122 
124 
126 
127  FMEThread(const FMEThread &); // = delete
128  FMEThread &operator=(const FMEThread &); // = delete
129 };
130 
131 
133 {
134 public:
135  explicit FMEThreadPool(uint32_t numThreads);
136 
137  ~FMEThreadPool();
138 
140  inline uint32_t numThreads() const { return m_numThreads; }
141 
143  inline FMEThread* thread(uint32_t threadNr) const { return m_pThreads[threadNr]; }
144 
146  inline Barrier *syncBarrier() const { return m_pSyncBarrier; }
147 
149  void runThreads();
150 
151  template<typename KernelType, typename ArgType1>
152  void runKernel(ArgType1 arg1)
153  {
154  for (uint32_t i=0; i < numThreads(); i++)
155  {
156  KernelType kernel(thread(i));
157  FuncInvoker<KernelType, ArgType1> invoker(kernel, arg1);
158  thread(i)->setTask(new FMEFuncInvokerTask< FuncInvoker< KernelType, ArgType1 > >(invoker));
159  }
160  runThreads();
161  }
162 
163 private:
164  void allocate();
165 
166  void deallocate();
167 
168  uint32_t m_numThreads;
169 
171 
173 };
174 
175 }
176 }
uint32_t threadNr() const
returns the index of the thread ( 0.. numThreads()-1 )
Definition: FMEThread.h:87
Definition of utility functions for FME layout.
Declaration of class ArrayGraph.
uint32_t numThreads() const
returns the total number of threads in the pool
Definition: FMEThread.h:90
void doWork() override
overrides the task doWork() method and invokes the function or functor
Definition: FMEThread.h:70
Representation of a barrier.
Definition: Barrier.h:50
Class used to invoke a functor or function inside a thread.
Definition: FMEThread.h:63
FMEThreadPool * threadPool() const
returns the ThreadPool this thread belongs to
Definition: FMEThread.h:96
bool isMainThread() const
returns true if this is the main thread ( the main thread is always the first thread ) ...
Definition: FMEThread.h:93
FMEFuncInvokerTask(FuncInvokerType f)
constructor with an invoker
Definition: FMEThread.h:67
Implementation of a thread barrier.
FuncInvokerType funcInvoker
the invoker
Definition: FMEThread.h:73
Declaration of Thread class representing threads.
The fast multipole embedder work thread class.
Definition: FMEThread.h:80
Declaration of class LinearQuadtree.
Barrier * syncBarrier() const
returns the barrier instance used to sync the threads during execution
Definition: FMEThread.h:146
uint32_t numThreads() const
returns the number of threads in this pool
Definition: FMEThread.h:140
FMEThread * thread(uint32_t threadNr) const
returns the threadNr-th thread
Definition: FMEThread.h:143
void setTask(FMETask *pTask)
sets the actual task
Definition: FMEThread.h:116
The namespace for all OGDF objects.
void operator()()
the main work function
Definition: FMEThread.h:108
The thread task class used only as an interface.
Definition: FMEThread.h:50