Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
FMEThread.h
Go to the documentation of this file.
1
32#pragma once
33
35
36#include <cstdint>
37
38namespace ogdf {
39class Barrier;
40
41namespace fast_multipole_embedder {
42
43class FMEThreadPool;
44
49class FMETask {
50public:
51 virtual ~FMETask() { }
52
53 virtual void doWork() = 0;
54};
55
59template<typename FuncInvokerType>
61public:
63 FMEFuncInvokerTask(FuncInvokerType f) : funcInvoker(f) { }
64
66 void doWork() override { funcInvoker(); }
67
68private:
70 FuncInvokerType funcInvoker;
71};
72
76class FMEThread /*: public Thread*/
77{
78public:
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
101#endif
102
104 void operator()() {
106 m_pTask->doWork();
107 delete m_pTask;
108 m_pTask = nullptr;
109 }
110
112 void setTask(FMETask* pTask) { m_pTask = pTask; }
113
114private:
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
128public:
129 explicit FMEThreadPool(uint32_t numThreads);
130
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
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
155private:
156 void allocate();
157
159
160 uint32_t m_numThreads;
161
163
165};
166
167}
168}
Definition of utility functions for FME layout.
Representation of a barrier.
Definition Barrier.h:48
Class used to invoke a functor or function inside a thread.
Definition FMEThread.h:60
void doWork() override
overrides the task doWork() method and invokes the function or functor
Definition FMEThread.h:66
FMEFuncInvokerTask(FuncInvokerType f)
constructor with an invoker
Definition FMEThread.h:63
The thread task class used only as an interface.
Definition FMEThread.h:49
The fast multipole embedder work thread class.
Definition FMEThread.h:77
FMEThreadPool * threadPool() const
returns the ThreadPool this thread belongs to
Definition FMEThread.h:92
bool isMainThread() const
returns true if this is the main thread ( the main thread is always the first thread )
Definition FMEThread.h:89
uint32_t numThreads() const
returns the total number of threads in the pool
Definition FMEThread.h:86
void operator()()
the main work function
Definition FMEThread.h:104
void setTask(FMETask *pTask)
sets the actual task
Definition FMEThread.h:112
uint32_t threadNr() const
returns the index of the thread ( 0.. numThreads()-1 )
Definition FMEThread.h:83
FMEThread(FMEThreadPool *pThreadPool, uint32_t threadNr)
construtor
FMEThread & operator=(const FMEThread &)
Barrier * syncBarrier() const
returns the barrier instance used to sync the threads during execution
Definition FMEThread.h:140
void runThreads()
runs one iteration. This call blocks the main thread
uint32_t numThreads() const
returns the number of threads in this pool
Definition FMEThread.h:134
FMEThread * thread(uint32_t threadNr) const
returns the threadNr-th thread
Definition FMEThread.h:137
The namespace for all OGDF objects.