MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlMultiThreadedPageRequestProcessor.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2009, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_MULTI_THREADED_PAGE_REQUEST_PROCESSOR_H
14#define ML_MULTI_THREADED_PAGE_REQUEST_PROCESSOR_H
15
16#include "mlInitSystemML.h"
17
19#include "mlAtomicCounter.h"
21
22#include "ThirdPartyWarningsDisable.h"
23#include <boost/thread.hpp>
24#include "ThirdPartyWarningsRestore.h"
25#include <mlMutex.h>
26#include <mlWaitCondition.h>
27
29#define ML_HOST_START_SINGLETHREADED_TIMEOUT 0.0005
31#define ML_HOST_PAGEREQUEST_QUEUE_LIMIT 2
33#define ML_HOST_PAGEREQUEST_PRODUCER_BATCH_SIZE 2
35#define ML_HOST_START_SINGLETHREADED_PAGEREQUEST_LIMIT 10
37#define ML_HOST_MINIMAL_GUITHREAD_SLEEP_MS 10
38
40
41//-------------------------------------------------------------------------------------------
44//-------------------------------------------------------------------------------------------
46{
47public:
50
52 bool supportsMultiThreading() override
53 {
54 return true;
55 }
56
58 void setNumWorkerThreads(std::size_t threads) override;
59
62
64 void processAll() override;
65
67 void process(double timeBudget) override;
68
71
73 bool needsProcessing() override;
74
76 void append(PageRequest* request) override;
77
79 void enterProcessingScope() override;
81 void leaveProcessingScope() override;
82
84 bool processingWasSingleThreaded() const override
85 {
86 return _processingWasSingleThreaded;
87 }
88
91 {
92 _allowLateMultiThreadingStart = flag;
93 }
94
95private:
96 struct WorkerThreadState;
97
99 bool processNextGUIRequest(int millisecondsToWait);
100
102 void processNextWorkerRequest(WorkerThreadState* state, PerThreadStorage& perThreadStorage);
103
105 MLErrorCode processRequest(PageRequest* request, bool isGUIThread, PerThreadStorage& perThreadStorage);
106
108 static void rootTileRequestFinishedCB(void* data, TileRequest* request);
109
111 void rootTileRequestFinished(TileRequest* request);
112
114 void runPageRequestProducerLoop();
115
117 void addNewCursors();
118
120 void runWorkerThreadLoop(WorkerThreadState* state);
121
123 void notifyAllThreads();
124
126 void launchWorkerThreads();
127
129 int getCurrentNumberOfPageRequestsInQueues();
130
132 void wakeupProducerIfWaitingForQueue();
133
135 void handleErrorsInGUIThread() override;
136
138 void stopProcessingOnAllThreads();
139
141 void moveRequestsFromGUIQueuesToOtherQueues();
142
144 void getAllPageRequestQueues(std::vector<std::deque<PageRequest*>*>& queues) override;
145
146 // Starts single-threaded to evaluate if starting parallel processing is advantageous.
147 void startCreatingWorkSingleThreaded();
148
150 bool _shutdown;
151
153 bool _producerWorking;
155 int _workerThreadsWorking;
158 int _maxPageRequestsInQueues;
161 bool _producerWaitingForQueue;
162
164 bool _processingAllowed;
165
166 bool _useMultiThreading;
167 bool _processingWasSingleThreaded;
168 bool _allowLateMultiThreadingStart;
169
171 WaitCondition _stoppedProcessing;
172
174 WaitCondition _guiThreadWakeup;
175 WaitCondition _producerWakeup;
176 WaitCondition _workerWakeup;
177 WaitCondition _ioThreadWakeup;
178
180 Mutex _mutex;
181
183 std::vector<PageRequestCursor*> _newCursors;
184
186 std::deque<PageRequest*> _parallelWorkQueue;
187 std::deque<PageRequest*> _ioWorkQueue;
188
190 struct WorkerThreadState
191 {
192 WorkerThreadState();
193 ~WorkerThreadState();
194
196 boost::thread* _thread;
197
199 bool _working;
201 int _workerThreadId;
204
206 WaitCondition* _wakeup;
208 std::deque<PageRequest*>* _queue;
209
211 bool _shutdown;
212
214 TimeCounter _profilingTimer;
215 };
216
218 std::size_t _numDesiredWorkerThreads;
219
221 std::vector<WorkerThreadState*> _workerThreads;
222
224 WorkerThreadState* _ioThread;
225
227 boost::thread* _producerThread;
228
230 ProcessingTimeLine::TimeLine* _producerThreadTimeLine;
231
233 struct ProfilingInfo
234 {
235 ProfilingInfo()
236 {
237 count = 0;
238 elapsedTime = 0;
239 }
240
242 int count;
243
245 double elapsedTime;
246 };
247
249 std::map<PagedImage*, ProfilingInfo> _profilingData;
250
252 void addThreadTimesToMLProfiling();
253};
254
256
257#endif
The Host is the central image processing class in the ML.
Definition mlHost.h:116
A multithreaded processor that takes one or even multiple tile requests and can process them iterativ...
void leaveProcessingScope() override
Leaves a recursive processing scope. This is always called from main thread!
bool needsProcessing() override
Returns whether the tile request needs some more processing.
void enterProcessingScope() override
Enters a recursive processing scope. This is always called from main thread!
bool supportsMultiThreading() override
Returns whether multithreading is supported.
void process(double timeBudget) override
Process the requests for the given timeBudget given in seconds.
void processAll() override
Processes all requests until the cursors have traversed the entire tree and the queue is empty.
MultiThreadedPageRequestProcessor(Host *host=nullptr)
void setAllowLateMultiThreadingStart(bool flag)
This can be set to false to debug the single-threaded part of this class.
void addRootTileRequest(TileRequest *tileRequest) override
Adds the root tileRequest that should be processed; the ownership stays with the caller.
bool processingWasSingleThreaded() const override
Processing is always single-threaded, implemented only for testing purposes.
void launchMultiThreading()
Launches the multithreading.
void setNumWorkerThreads(std::size_t threads) override
Sets number of worker threads.
void append(PageRequest *request) override
Implements PageRequestQueueInterface interface.
Base class for single and multithreaded processor.
virtual void handleErrorsInGUIThread()
Handles all errors that have been accumulated.
virtual void getAllPageRequestQueues(std::vector< std::deque< PageRequest * > * > &queues)
Returns all page request queues (to be derived when there are more than the GUI queues).
A PageRequest represents the request for the calculation of a single page of a PagedImage.
Timeline for a single thread.
A TileRequest either represents the input subimage that is needed by a PageRequest or if it is a root...
Class to measure precise time intervals.
WaitCondition implements a wait condition for thread synchronization.
MLint32 MLErrorCode
Type of an ML Error code.
Definition mlTypeDefs.h:772
boost::mutex Mutex
Defines a non-recursive mutex.
Definition mlMutex.h:39
#define MLEXPORT
To export symbols from a DLL/shared object, we need to mark them with the MLEXPORT symbol.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.